Skip to content

Commit 1069328

Browse files
vivien-applepull[bot]
authored andcommitted
[darwin-framework-tool] heap-use-after-free when calling chip::app::DnssdServer::StartServer at startup (#26000)
1 parent 4ea3cd6 commit 1069328

9 files changed

+73
-6
lines changed

src/app/server/Dnssd.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,30 @@ void DnssdServer::StartServer()
343343
return StartServer(mode);
344344
}
345345

346+
void DnssdServer::StopServer()
347+
{
348+
DeviceLayer::PlatformMgr().RemoveEventHandler(OnPlatformEventWrapper, 0);
349+
350+
#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
351+
if (mExtendedDiscoveryExpiration != kTimeoutCleared)
352+
{
353+
DeviceLayer::SystemLayer().CancelTimer(HandleExtendedDiscoveryExpiration, nullptr);
354+
mExtendedDiscoveryExpiration = kTimeoutCleared;
355+
}
356+
#endif // CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
357+
358+
if (Dnssd::ServiceAdvertiser::Instance().IsInitialized())
359+
{
360+
auto err = Dnssd::ServiceAdvertiser::Instance().RemoveServices();
361+
if (err != CHIP_NO_ERROR)
362+
{
363+
ChipLogError(Discovery, "Failed to remove advertised services: %" CHIP_ERROR_FORMAT, err.Format());
364+
}
365+
366+
Dnssd::ServiceAdvertiser::Instance().Shutdown();
367+
}
368+
}
369+
346370
void DnssdServer::StartServer(Dnssd::CommissioningMode mode)
347371
{
348372
ChipLogProgress(Discovery, "Updating services using commissioning mode %d", static_cast<int>(mode));

src/app/server/Dnssd.h

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class DLL_EXPORT DnssdServer
9494
/// (Re-)starts the Dnssd server, using the provided commissioning mode.
9595
void StartServer(Dnssd::CommissioningMode mode);
9696

97+
//// Stop the Dnssd server.
98+
void StopServer();
99+
97100
CHIP_ERROR GenerateRotatingDeviceId(char rotatingDeviceIdHexBuffer[], size_t rotatingDeviceIdHexBufferSize);
98101

99102
/// Generates the (random) instance name that a CHIP device is to use for pre-commissioning DNS-SD

src/controller/CHIPDeviceControllerFactory.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,21 @@ CHIP_ERROR DeviceControllerFactory::ServiceEvents()
340340
return CHIP_NO_ERROR;
341341
}
342342

343+
void DeviceControllerFactory::RetainSystemState()
344+
{
345+
(void) mSystemState->Retain();
346+
}
347+
348+
void DeviceControllerFactory::ReleaseSystemState()
349+
{
350+
mSystemState->Release();
351+
352+
if (!mSystemState->IsInitialized() && mEnableServerInteractions)
353+
{
354+
app::DnssdServer::Instance().StopServer();
355+
}
356+
}
357+
343358
DeviceControllerFactory::~DeviceControllerFactory()
344359
{
345360
Shutdown();

src/controller/CHIPDeviceControllerFactory.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class DeviceControllerFactory
174174
// created to permit retention of the underlying system state.
175175
//
176176
// NB: The system state will still be freed in Shutdown() regardless of this call.
177-
void RetainSystemState() { (void) mSystemState->Retain(); }
177+
void RetainSystemState();
178178

179179
//
180180
// To initiate shutdown of the stack upon termination of all resident controllers in the
@@ -183,7 +183,7 @@ class DeviceControllerFactory
183183
//
184184
// This should only be invoked if a matching call to RetainSystemState() was called prior.
185185
//
186-
void ReleaseSystemState() { mSystemState->Release(); }
186+
void ReleaseSystemState();
187187

188188
//
189189
// Retrieve a read-only pointer to the system state object that contains pointers to key stack

src/lib/dnssd/Advertiser.h

+7
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ class ServiceAdvertiser
299299
*/
300300
virtual CHIP_ERROR Init(chip::Inet::EndPointManager<chip::Inet::UDPEndPoint> * udpEndPointManager) = 0;
301301

302+
/**
303+
* Returns whether the advertiser has completed the initialization.
304+
*
305+
* Returns true if the advertiser is ready to advertise services.
306+
*/
307+
virtual bool IsInitialized() = 0;
308+
302309
/**
303310
* Shuts down the advertiser.
304311
*/

src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,12 @@ class AdvertiserMinMdns : public ServiceAdvertiser,
181181

182182
// Service advertiser
183183
CHIP_ERROR Init(chip::Inet::EndPointManager<chip::Inet::UDPEndPoint> * udpEndPointManager) override;
184+
bool IsInitialized() override { return mIsInitialized; }
184185
void Shutdown() override;
185186
CHIP_ERROR RemoveServices() override;
186187
CHIP_ERROR Advertise(const OperationalAdvertisingParameters & params) override;
187188
CHIP_ERROR Advertise(const CommissionAdvertisingParameters & params) override;
188-
CHIP_ERROR FinalizeServiceUpdate() override { return CHIP_NO_ERROR; }
189+
CHIP_ERROR FinalizeServiceUpdate() override;
189190
CHIP_ERROR GetCommissionableInstanceName(char * instanceName, size_t maxLength) const override;
190191
CHIP_ERROR UpdateCommissionableInstanceName() override;
191192

@@ -363,6 +364,8 @@ CHIP_ERROR AdvertiserMinMdns::Init(chip::Inet::EndPointManager<chip::Inet::UDPEn
363364

364365
void AdvertiserMinMdns::Shutdown()
365366
{
367+
VerifyOrReturn(mIsInitialized);
368+
366369
AdvertiseRecords(BroadcastAdvertiseType::kRemovingAll);
367370

368371
GlobalMinimalMdnsServer::Server().Shutdown();
@@ -371,6 +374,8 @@ void AdvertiserMinMdns::Shutdown()
371374

372375
CHIP_ERROR AdvertiserMinMdns::RemoveServices()
373376
{
377+
VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE);
378+
374379
// Send a "goodbye" packet for each RR being removed, as defined in RFC 6762.
375380
// This allows mDNS clients to remove stale cached records which may not be re-added with
376381
// subsequent Advertise() calls. In the case the same records are re-added, this extra
@@ -446,6 +451,8 @@ OperationalQueryAllocator::Allocator * AdvertiserMinMdns::FindEmptyOperationalAl
446451

447452
CHIP_ERROR AdvertiserMinMdns::Advertise(const OperationalAdvertisingParameters & params)
448453
{
454+
VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE);
455+
449456
char nameBuffer[Operational::kInstanceNameMaxLength + 1] = "";
450457

451458
// need to set server name
@@ -547,6 +554,12 @@ CHIP_ERROR AdvertiserMinMdns::Advertise(const OperationalAdvertisingParameters &
547554
return CHIP_NO_ERROR;
548555
}
549556

557+
CHIP_ERROR AdvertiserMinMdns::FinalizeServiceUpdate()
558+
{
559+
VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE);
560+
return CHIP_NO_ERROR;
561+
}
562+
550563
CHIP_ERROR AdvertiserMinMdns::GetCommissionableInstanceName(char * instanceName, size_t maxLength) const
551564
{
552565
if (maxLength < (Commission::kInstanceNameMaxLength + 1))
@@ -568,6 +581,8 @@ CHIP_ERROR AdvertiserMinMdns::UpdateCommissionableInstanceName()
568581

569582
CHIP_ERROR AdvertiserMinMdns::Advertise(const CommissionAdvertisingParameters & params)
570583
{
584+
VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE);
585+
571586
if (params.GetCommissionAdvertiseMode() == CommssionAdvertiseMode::kCommissionableNode)
572587
{
573588
mQueryResponderAllocatorCommissionable.Clear();

src/lib/dnssd/Advertiser_ImplNone.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class NoneAdvertiser : public ServiceAdvertiser
3232
return CHIP_ERROR_NOT_IMPLEMENTED;
3333
}
3434

35+
bool IsInitialized() override { return false; }
36+
3537
void Shutdown() override {}
3638

3739
CHIP_ERROR RemoveServices() override

src/lib/dnssd/Discovery_ImplPlatform.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,6 @@ CHIP_ERROR DiscoveryImplPlatform::PublishService(const char * serviceType, TextE
486486
Inet::InterfaceId interfaceId, const chip::ByteSpan & mac,
487487
DnssdServiceProtocol protocol, PeerId peerId)
488488
{
489-
VerifyOrReturnError(mState == State::kInitialized, CHIP_ERROR_INCORRECT_STATE);
490-
491489
DnssdService service;
492490
ReturnErrorOnFailure(MakeHostName(service.mHostName, sizeof(service.mHostName), mac));
493491
ReturnErrorOnFailure(protocol == DnssdServiceProtocol::kDnssdProtocolTcp
@@ -525,6 +523,7 @@ CHIP_ERROR DiscoveryImplPlatform::PublishService(const char * serviceType, TextE
525523
}
526524

527525
#define PREPARE_RECORDS(Type) \
526+
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INCORRECT_STATE); \
528527
TextEntry textEntries[Type##AdvertisingParameters::kTxtMaxNumber]; \
529528
size_t textEntrySize = 0; \
530529
const char * subTypes[Type::kSubTypeMaxNumber]; \
@@ -593,13 +592,15 @@ CHIP_ERROR DiscoveryImplPlatform::Advertise(const CommissionAdvertisingParameter
593592

594593
CHIP_ERROR DiscoveryImplPlatform::RemoveServices()
595594
{
595+
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INCORRECT_STATE);
596596
ReturnErrorOnFailure(ChipDnssdRemoveServices());
597597

598598
return CHIP_NO_ERROR;
599599
}
600600

601601
CHIP_ERROR DiscoveryImplPlatform::FinalizeServiceUpdate()
602602
{
603+
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INCORRECT_STATE);
603604
return ChipDnssdFinalizeServiceUpdate();
604605
}
605606

src/lib/dnssd/Discovery_ImplPlatform.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver
3838
public:
3939
// Members that implement both ServiceAdveriser and Resolver interfaces.
4040
CHIP_ERROR Init(Inet::EndPointManager<Inet::UDPEndPoint> *) override { return InitImpl(); }
41+
bool IsInitialized() override;
4142
void Shutdown() override;
4243

4344
// Members that implement ServiceAdvertiser interface.
@@ -49,7 +50,6 @@ class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver
4950
CHIP_ERROR UpdateCommissionableInstanceName() override;
5051

5152
// Members that implement Resolver interface.
52-
bool IsInitialized() override;
5353
void SetOperationalDelegate(OperationalResolveDelegate * delegate) override { mResolverProxy.SetOperationalDelegate(delegate); }
5454
void SetCommissioningDelegate(CommissioningResolveDelegate * delegate) override
5555
{

0 commit comments

Comments
 (0)