[mdns] add validation for names in browser and resolver APIs (#12184)

This commit introduces validation for names provided to mDNS browser,
resolver, and querier start and stop functions.

New `ValidateNamesIn()` overloads are added to check the validity of
service types, instance names, and host names passed to APIs like
`otMdnsStartBrowser()`, `otMdnsStartSrvResolver()`, etc.
This commit is contained in:
Abtin Keshavarzian
2025-12-02 16:30:00 -08:00
committed by GitHub
parent 8274fb4ec1
commit 7a76650d8b
4 changed files with 100 additions and 10 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ extern "C" {
*
* @note This number versions both OpenThread platform and user APIs.
*/
#define OPENTHREAD_API_VERSION (555)
#define OPENTHREAD_API_VERSION (556)
/**
* @addtogroup api-instance
+14 -1
View File
@@ -684,6 +684,7 @@ typedef otPlatDnssdRecordQuerier otMdnsRecordQuerier;
* @retval OT_ERROR_NONE Browser started successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_ALREADY An identical browser (same service and callback) is already active.
* @retval OT_ERROR_INVALID_ARGS A name in @p aBrowser is invalid, or the callback is `NULL`.
*/
otError otMdnsStartBrowser(otInstance *aInstance, const otMdnsBrowser *aBrowser);
@@ -697,6 +698,7 @@ otError otMdnsStartBrowser(otInstance *aInstance, const otMdnsBrowser *aBrowser)
*
* @retval OT_ERROR_NONE Browser stopped successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_INVALID_ARGS A name in @p aBrowser is invalid, or the callback is `NULL`.
*/
otError otMdnsStopBrowser(otInstance *aInstance, const otMdnsBrowser *aBroswer);
@@ -721,6 +723,7 @@ otError otMdnsStopBrowser(otInstance *aInstance, const otMdnsBrowser *aBroswer);
* @retval OT_ERROR_NONE Resolver started successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_ALREADY An identical resolver (same service and callback) is already active.
* @retval OT_ERROR_INVALID_ARGS A name in @p aResolver is invalid, or the callback is `NULL`.
*/
otError otMdnsStartSrvResolver(otInstance *aInstance, const otMdnsSrvResolver *aResolver);
@@ -734,6 +737,7 @@ otError otMdnsStartSrvResolver(otInstance *aInstance, const otMdnsSrvResolver *a
*
* @retval OT_ERROR_NONE Resolver stopped successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_INVALID_ARGS A name in @p aResolver is invalid, or the callback is `NULL`.
*/
otError otMdnsStopSrvResolver(otInstance *aInstance, const otMdnsSrvResolver *aResolver);
@@ -758,6 +762,7 @@ otError otMdnsStopSrvResolver(otInstance *aInstance, const otMdnsSrvResolver *aR
* @retval OT_ERROR_NONE Resolver started successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_ALREADY An identical resolver (same service and callback) is already active.
* @retval OT_ERROR_INVALID_ARGS A name in @p aResolver is invalid, or the callback is `NULL`.
*/
otError otMdnsStartTxtResolver(otInstance *aInstance, const otMdnsTxtResolver *aResolver);
@@ -771,6 +776,7 @@ otError otMdnsStartTxtResolver(otInstance *aInstance, const otMdnsTxtResolver *a
*
* @retval OT_ERROR_NONE Resolver stopped successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_INVALID_ARGS A name in @p aResolver is invalid, or the callback is `NULL`.
*/
otError otMdnsStopTxtResolver(otInstance *aInstance, const otMdnsTxtResolver *aResolver);
@@ -795,6 +801,7 @@ otError otMdnsStopTxtResolver(otInstance *aInstance, const otMdnsTxtResolver *aR
* @retval OT_ERROR_NONE Resolver started successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_ALREADY An identical resolver (same host and callback) is already active.
* @retval OT_ERROR_INVALID_ARGS A name in @p aResolver is invalid, or the callback is `NULL`.
*/
otError otMdnsStartIp6AddressResolver(otInstance *aInstance, const otMdnsAddressResolver *aResolver);
@@ -808,6 +815,7 @@ otError otMdnsStartIp6AddressResolver(otInstance *aInstance, const otMdnsAddress
*
* @retval OT_ERROR_NONE Resolver stopped successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_INVALID_ARGS A name in @p aResolver is invalid, or the callback is `NULL`.
*/
otError otMdnsStopIp6AddressResolver(otInstance *aInstance, const otMdnsAddressResolver *aResolver);
@@ -833,6 +841,7 @@ otError otMdnsStopIp6AddressResolver(otInstance *aInstance, const otMdnsAddressR
* @retval OT_ERROR_NONE Resolver started successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_ALREADY An identical resolver (same host and callback) is already active.
* @retval OT_ERROR_INVALID_ARGS A name in @p aResolver is invalid, or the callback is `NULL`.
*/
otError otMdnsStartIp4AddressResolver(otInstance *aInstance, const otMdnsAddressResolver *aResolver);
@@ -846,6 +855,7 @@ otError otMdnsStartIp4AddressResolver(otInstance *aInstance, const otMdnsAddress
*
* @retval OT_ERROR_NONE Resolver stopped successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_INVALID_ARGS A name in @p aResolver is invalid, or the callback is `NULL`.
*/
otError otMdnsStopIp4AddressResolver(otInstance *aInstance, const otMdnsAddressResolver *aResolver);
@@ -886,7 +896,8 @@ otError otMdnsStopIp4AddressResolver(otInstance *aInstance, const otMdnsAddressR
* @retval OT_ERROR_NONE Record @p aQuerier started successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_ALREADY An identical querier (same name, record type, and callback) is already active.
* @retval OT_ERROR_INVALID_ARGS The `mRecordType` in @p aQuerier is invalid. MUST use browser/resolvers.
* @retval OT_ERROR_INVALID_ARGS The `mRecordType` in @p aQuerier is invalid (MUST use browser/resolvers), or
* a name in @p aQuerier is invalid, or the callback is `NULL`.
*/
otError otMdnsStartRecordQuerier(otInstance *aInstance, const otMdnsRecordQuerier *aQuerier);
@@ -903,6 +914,8 @@ otError otMdnsStartRecordQuerier(otInstance *aInstance, const otMdnsRecordQuerie
*
* @retval OT_ERROR_NONE Querier stopped successfully.
* @retval OT_ERROR_INVALID_STATE mDNS module is not enabled.
* @retval OT_ERROR_INVALID_ARGS The `mRecordType` in @p aQuerier is invalid (MUST use browser/resolvers), or
* a name in @p aQuerier is invalid, or the callback is `NULL`.
*/
otError otMdnsStopRecordQuerier(otInstance *aInstance, const otMdnsRecordQuerier *aQuerier);
+60 -1
View File
@@ -5147,6 +5147,7 @@ Error Core::Start(const BrowserResolverType &aBrowserOrResolver)
VerifyOrExit(mIsEnabled, error = kErrorInvalidState);
VerifyOrExit(aBrowserOrResolver.mCallback != nullptr, error = kErrorInvalidArgs);
SuccessOrExit(error = ValidateNamesIn(aBrowserOrResolver));
cacheEntry = GetCacheList<CacheType>().FindMatching(aBrowserOrResolver);
@@ -5172,6 +5173,7 @@ Error Core::Stop(const BrowserResolverType &aBrowserOrResolver)
VerifyOrExit(mIsEnabled, error = kErrorInvalidState);
VerifyOrExit(aBrowserOrResolver.mCallback != nullptr, error = kErrorInvalidArgs);
SuccessOrExit(error = ValidateNamesIn(aBrowserOrResolver));
cacheEntry = GetCacheList<CacheType>().FindMatching(aBrowserOrResolver);
VerifyOrExit(cacheEntry != nullptr);
@@ -5221,6 +5223,8 @@ Error Core::StartRecordQuerier(const RecordQuerier &aQuerier)
return error;
}
Error Core::StopRecordQuerier(const RecordQuerier &aQuerier) { return Stop<RecordCache, RecordQuerier>(aQuerier); }
Error Core::StopIp6AddressResolver(const AddressResolver &aResolver)
{
return Stop<Ip6AddrCache, AddressResolver>(aResolver);
@@ -5236,7 +5240,62 @@ Error Core::StopIp4AddressResolver(const AddressResolver &aResolver)
return Stop<Ip4AddrCache, AddressResolver>(aResolver);
}
Error Core::StopRecordQuerier(const RecordQuerier &aQuerier) { return Stop<RecordCache, RecordQuerier>(aQuerier); }
Error Core::ValidateNamesIn(const Browser &aBrowser) const
{
Error error;
SuccessOrExit(error = Name::ValidateName(aBrowser.mServiceType));
if (aBrowser.mSubTypeLabel != nullptr)
{
error = Name::ValidateLabel(aBrowser.mSubTypeLabel);
}
exit:
return error;
}
Error Core::ValidateNamesIn(const SrvResolver &aSrvResolver) const
{
Error error;
SuccessOrExit(error = Name::ValidateLabel(aSrvResolver.mServiceInstance));
error = Name::ValidateName(aSrvResolver.mServiceType);
exit:
return error;
}
Error Core::ValidateNamesIn(const TxtResolver &aTxtResolver) const
{
Error error;
SuccessOrExit(error = Name::ValidateLabel(aTxtResolver.mServiceInstance));
error = Name::ValidateName(aTxtResolver.mServiceType);
exit:
return error;
}
Error Core::ValidateNamesIn(const AddressResolver &aAddressResolver) const
{
return Name::ValidateName(aAddressResolver.mHostName);
}
Error Core::ValidateNamesIn(const RecordQuerier &aRecordQuerier) const
{
Error error;
SuccessOrExit(error = Name::ValidateLabel(aRecordQuerier.mFirstLabel));
if (aRecordQuerier.mNextLabels != nullptr)
{
error = Name::ValidateName(aRecordQuerier.mNextLabels);
}
exit:
return error;
}
void Core::AddPassiveSrvTxtCache(const char *aServiceInstance, const char *aServiceType)
{
+25 -7
View File
@@ -472,6 +472,7 @@ public:
* @retval kErrorNone Browser started successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorAlready An identical browser (same service and callback) is already active.
* @retval kErrorInvalidArgs A name in @p aBrowser is invalid, or the callback is `nullptr`.
*/
Error StartBrowser(const Browser &aBrowser);
@@ -483,7 +484,8 @@ public:
* @param[in] aBrowser The browser to stop.
*
* @retval kErrorNone Browser stopped successfully.
* @retval kErrorInvalidSatet mDNS module is not enabled.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorInvalidArgs A name in @p aBrowser is invalid, or the callback is `nullptr`.
*/
Error StopBrowser(const Browser &aBrowser);
@@ -507,6 +509,7 @@ public:
* @retval kErrorNone Resolver started successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorAlready An identical resolver (same service and callback) is already active.
* @retval kErrorInvalidArgs A name in @p aResolver is invalid, or the callback is `nullptr`.
*/
Error StartSrvResolver(const SrvResolver &aResolver);
@@ -519,6 +522,7 @@ public:
*
* @retval kErrorNone Resolver stopped successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorInvalidArgs A name in @p aResolver is invalid, or the callback is `nullptr`.
*/
Error StopSrvResolver(const SrvResolver &aResolver);
@@ -542,6 +546,7 @@ public:
* @retval kErrorNone Resolver started successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorAlready An identical resolver (same service and callback) is already active.
* @retval kErrorInvalidArgs A name in @p aResolver is invalid, or the callback is `nullptr`.
*/
Error StartTxtResolver(const TxtResolver &aResolver);
@@ -554,6 +559,7 @@ public:
*
* @retval kErrorNone Resolver stopped successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorInvalidArgs A name in @p aResolver is invalid, or the callback is `nullptr`.
*/
Error StopTxtResolver(const TxtResolver &aResolver);
@@ -577,6 +583,7 @@ public:
* @retval kErrorNone Resolver started successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorAlready An identical resolver (same host and callback) is already active.
* @retval kErrorInvalidArgs A name in @p aResolver is invalid, or the callback is `nullptr`.
*/
Error StartIp6AddressResolver(const AddressResolver &aResolver);
@@ -589,6 +596,7 @@ public:
*
* @retval kErrorNone Resolver stopped successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorInvalidArgs A name in @p aResolver is invalid, or the callback is `nullptr`.
*/
Error StopIp6AddressResolver(const AddressResolver &aResolver);
@@ -613,6 +621,7 @@ public:
* @retval kErrorNone Resolver started successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorAlready An identical resolver (same host and callback) is already active.
* @retval kErrorInvalidArgs A name in @p aResolver is invalid, or the callback is `nullptr`.
*/
Error StartIp4AddressResolver(const AddressResolver &aResolver);
@@ -625,6 +634,7 @@ public:
*
* @retval kErrorNone Resolver stopped successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorInvalidArgs A name in @p aResolver is invalid, or the callback is `nullptr`.
*/
Error StopIp4AddressResolver(const AddressResolver &aResolver);
@@ -653,10 +663,11 @@ public:
*
* @param[in] aQuerier The record querier to be started.
*
* @retval kErrorNone Record @p aQuerier started successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorAlready An identical querier (same name, record type, and callback) is already active.
* @retval kErrorInvalidArg The `mRecordType` in @p aQuerier is invalid. MUST use browser/resolvers.
* @retval kErrorNone Record @p aQuerier started successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorAlready An identical querier (same name, record type, and callback) is already active.
* @retval kErrorInvalidArgs The `mRecordType` in @p aQuerier is invalid (MUST use browser/resolvers), or
* a name in @p aQuerier is invalid, or the callback is `nullptr`.
*/
Error StartRecordQuerier(const RecordQuerier &aQuerier);
@@ -667,8 +678,10 @@ public:
*
* @param[in] aQuerier The record querier to be stopped.
*
* @retval kErrorNone Querier stopped successfully.
* @retval kErrorInvalidStat mDNS module is not enabled.
* @retval kErrorNone Querier stopped successfully.
* @retval kErrorInvalidState mDNS module is not enabled.
* @retval kErrorInvalidArgs The `mRecordType` in @p aQuerier is invalid (MUST use browser/resolvers), or
* a name in @p aQuerier is invalid, or the callback is `nullptr`.
*/
Error StopRecordQuerier(const RecordQuerier &aQuerier);
@@ -2328,6 +2341,11 @@ private:
Error ValidateHostName(const Host &aHost) const;
Error ValidateServiceNames(const Service &aService, bool aCheckHostAndSubTypeLabels) const;
Error ValidateKeyName(const Key &aKey) const;
Error ValidateNamesIn(const Browser &aBrowser) const;
Error ValidateNamesIn(const SrvResolver &aSrvResolver) const;
Error ValidateNamesIn(const TxtResolver &aTxtResolver) const;
Error ValidateNamesIn(const AddressResolver &aAddressResolver) const;
Error ValidateNamesIn(const RecordQuerier &aRecordQuerier) const;
void HandleInfraIfStateChanged(void);
void HandleHostAddressEvent(const Ip6::Address &aAddress, bool aAdded, uint32_t aInfraIfIndex);
void HandleHostAddressRemoveAll(uint32_t aInfraIfIndex);