From 217310664258050c0d46644f18fb9ac41f9eda07 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 11 Jan 2022 13:36:07 -0500 Subject: [PATCH] Fix unsafe use of strncpy in OpenThread dns-sd code. (#13392) For the three uses where we are copying data of some computed size into a buffer, add checks that the size is not too big for the buffer (and in particular that we will be able to null-terminate after copying). For the one use where we are copying the entire buffer between two identical-sized buffers: 1) Assert that the target buffer us not smaller than the source buffer. 2) Use CopyString to ensure null-termination even if the source is not null-terminated. --- ...GenericThreadStackManagerImpl_OpenThread.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp index 950bb04f01574c..134cca16c7bcb2 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp @@ -47,6 +47,7 @@ #include #include +#include #include #include #include @@ -2073,6 +2074,10 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::FromOtDnsRespons // Extract from the .. the part. size_t substringSize = strchr(serviceInfo.mHostNameBuffer, '.') - serviceInfo.mHostNameBuffer; + if (substringSize >= ArraySize(mdnsService.mHostName)) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } strncpy(mdnsService.mHostName, serviceInfo.mHostNameBuffer, substringSize); // Append string terminating character. mdnsService.mHostName[substringSize] = '\0'; @@ -2082,6 +2087,10 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::FromOtDnsRespons // Extract from the ... the part. substringSize = strchr(serviceType, '.') - serviceType; + if (substringSize >= ArraySize(mdnsService.mType)) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } strncpy(mdnsService.mType, serviceType, substringSize); // Append string terminating character. mdnsService.mType[substringSize] = '\0'; @@ -2093,6 +2102,10 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::FromOtDnsRespons return CHIP_ERROR_INVALID_ARGUMENT; substringSize = strchr(protocolSubstringStart, '.') - protocolSubstringStart; + if (substringSize >= ArraySize(protocol)) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } strncpy(protocol, protocolSubstringStart, substringSize); // Append string terminating character. protocol[substringSize] = '\0'; @@ -2211,7 +2224,9 @@ void GenericThreadStackManagerImpl_OpenThread::OnDnsBrowseResult(otEr // Invoke callback for every service one by one instead of for the whole list due to large memory size needed to // allocate on // stack. - strncpy(dnsResult->mMdnsService.mName, serviceName, sizeof(serviceName)); + static_assert(ArraySize(dnsResult->mMdnsService.mName) >= ArraySize(serviceName), + "The target buffer must be big enough"); + Platform::CopyString(dnsResult->mMdnsService.mName, serviceName); DeviceLayer::PlatformMgr().ScheduleWork(DispatchBrowse, reinterpret_cast(dnsResult)); wasAnythingBrowsed = true; }