Skip to content

Commit

Permalink
[mdns-mdnssd] fix access of freed mHostsRef in Stop()
Browse files Browse the repository at this point in the history
This commit updates `Stop()` method to clear the host and service
registrations `mHostRegistrations` and `mServiceRegistrations` before
de-allocating `mHostsRef`. This ensures that `mHostsRef` and all its
child `DNSRecordRef`s remain valid when the `DnssdHostRegistration`
destructor is called which accesses and uses these to update the
published AAAA (IPv6 Address) records.

This commit also updates how `kDNSServiceErr_ServiceNotRunning` is
handled. Upon getting this error we need to clear all existing
`ServiceRef`s immediately, so we de-allocate `mHostsRef` and then
call `Stop()` followed by `Start()`. This commit also adds a check
to ensure `mHostsRef` is valid in the `DnssdHostRegistration`
destructor. This way the `Stop()` works correctly during normal
stop process and also `kDNSServiceErr_ServiceNotRunning`
  • Loading branch information
abtink committed Sep 12, 2023
1 parent 8fc6a4a commit af1cd58
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/mdns/mdns_mdnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,10 @@ bool PublisherMDnsSd::IsStarted(void) const

void PublisherMDnsSd::Stop(void)
{
ServiceRegistrationMap serviceRegistrations;
HostRegistrationMap hostRegistrations;

VerifyOrExit(mState == State::kReady);

std::swap(mServiceRegistrations, serviceRegistrations);
std::swap(mHostRegistrations, hostRegistrations);
mServiceRegistrations.clear();
mHostRegistrations.clear();

if (mHostsRef != nullptr)
{
Expand Down Expand Up @@ -370,6 +367,13 @@ void PublisherMDnsSd::Process(const MainloopContext &aMainloop)
if (error == kDNSServiceErr_ServiceNotRunning)
{
otbrLogWarning("Need to reconnect to mdnsd");

if (mHostsRef != nullptr)
{
DNSServiceRefDeallocate(mHostsRef);
mHostsRef = nullptr;
}

Stop();
Start();
ExitNow();
Expand Down Expand Up @@ -403,6 +407,7 @@ PublisherMDnsSd::DnssdHostRegistration::~DnssdHostRegistration(void)
{
int dnsError;

VerifyOrExit(GetPublisher().mHostsRef != nullptr);
VerifyOrExit(mServiceRef != nullptr);

for (const auto &recordRefAndAddress : GetRecordRefMap())
Expand Down
2 changes: 2 additions & 0 deletions src/mdns/mdns_mdnssd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class PublisherMDnsSd : public MainloopProcessor, public Publisher
std::map<DNSRecordRef, Ip6Address> &GetRecordRefMap() { return mRecordRefMap; }

private:
PublisherMDnsSd &GetPublisher(void) { return *static_cast<PublisherMDnsSd *>(mPublisher); }

DNSServiceRef mServiceRef;

public:
Expand Down

0 comments on commit af1cd58

Please sign in to comment.