Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure DnssdServer does not dereference a dangling fabric table. #26354

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ void DnssdServer::StartServer()

void DnssdServer::StopServer()
{
// Make sure we don't hold on to a dangling fabric table pointer.
mFabricTable = nullptr;

DeviceLayer::PlatformMgr().RemoveEventHandler(OnPlatformEventWrapper, 0);

#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
Expand Down
8 changes: 5 additions & 3 deletions src/app/server/Dnssd.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ class DLL_EXPORT DnssdServer
Inet::InterfaceId GetInterfaceId() { return mInterfaceId; }

//
// Override the referenced fabric table from the default that is present
// in Server::GetInstance().GetFabricTable() to something else.
// Set the fabric table the DnssdServer should use for operational
// advertising. This must be set before StartServer() is called for the
// first time.
//
void SetFabricTable(FabricTable * table)
{
Expand Down Expand Up @@ -94,7 +95,8 @@ class DLL_EXPORT DnssdServer
/// (Re-)starts the Dnssd server, using the provided commissioning mode.
void StartServer(Dnssd::CommissioningMode mode);

//// Stop the Dnssd server.
//// Stop the Dnssd server. After this call, SetFabricTable must be called
//// again before calling StartServer().
void StopServer();

CHIP_ERROR GenerateRotatingDeviceId(char rotatingDeviceIdHexBuffer[], size_t rotatingDeviceIdHexBufferSize);
Expand Down
15 changes: 9 additions & 6 deletions src/controller/CHIPDeviceControllerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)

// store the system state
mSystemState = chip::Platform::New<DeviceControllerSystemState>(std::move(stateParams));
mSystemState->SetTempFabricTable(tempFabricTable);
mSystemState->SetTempFabricTable(tempFabricTable, params.enableServerInteractions);
ChipLogDetail(Controller, "System State Initialized...");
return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -348,11 +348,6 @@ void DeviceControllerFactory::RetainSystemState()
void DeviceControllerFactory::ReleaseSystemState()
{
mSystemState->Release();

if (!mSystemState->IsInitialized() && mEnableServerInteractions)
{
app::DnssdServer::Instance().StopServer();
}
}

DeviceControllerFactory::~DeviceControllerFactory()
Expand Down Expand Up @@ -384,6 +379,14 @@ void DeviceControllerSystemState::Shutdown()

ChipLogDetail(Controller, "Shutting down the System State, this will teardown the CHIP Stack");

if (mTempFabricTable && mEnableServerInteractions)
{
// The DnssdServer is holding a reference to our temp fabric table,
// which we are about to destroy. Stop it, so that it will stop trying
// to use it.
app::DnssdServer::Instance().StopServer();
}

if (mFabricTableDelegate != nullptr)
{
if (mFabrics != nullptr)
Expand Down
8 changes: 7 additions & 1 deletion src/controller/CHIPDeviceControllerSystemState.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ class DeviceControllerSystemState
CASESessionManager * CASESessionMgr() const { return mCASESessionManager; }
Credentials::GroupDataProvider * GetGroupDataProvider() const { return mGroupDataProvider; }
Crypto::SessionKeystore * GetSessionKeystore() const { return mSessionKeystore; }
void SetTempFabricTable(FabricTable * tempFabricTable) { mTempFabricTable = tempFabricTable; }
void SetTempFabricTable(FabricTable * tempFabricTable, bool enableServerInteractions)
{
mTempFabricTable = tempFabricTable;
mEnableServerInteractions = enableServerInteractions;
}

private:
DeviceControllerSystemState() {}
Expand Down Expand Up @@ -220,6 +224,8 @@ class DeviceControllerSystemState

bool mHaveShutDown = false;

bool mEnableServerInteractions = false;

void Shutdown();
};

Expand Down