[dns-client] fix double-free of mSavedResponse on duplicate response#13060
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical memory corruption issue in the DNS client where duplicate responses triggered multiple chained queries to share the same saved response object. By ensuring that duplicate responses are rejected early and that new queries are properly initialized with a null saved response pointer, the fix prevents heap corruption during the query finalization process. Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
Library files
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #13060 +/- ##
==========================================
+ Coverage 74.15% 74.23% +0.08%
==========================================
Files 696 695 -1
Lines 95114 93401 -1713
==========================================
- Hits 70528 69334 -1194
+ Misses 24586 24067 -519
🚀 New features to boost your workflow:
|
Fix a double-free of `mSavedResponse` in `Dns::Client` when processing duplicate DNS responses matching an active query. When an SRV/TXT query needs to resolve a host address (AAAA), the DNS client allocates a chained `newQuery` to handle it. If duplicate responses are processed before the query chain is finalized, they trigger multiple AAAA resolution allocations for the same parent query. Because the new query inherits `mSavedResponse` from the parent query's `QueryInfo`, multiple chained queries end up aliasing/sharing the same cloned `mSavedResponse` message. During finalization, `FreeQuery` walks the chain and frees `mSavedResponse` for each query, leading to a double-free of the shared `Message` and free-list/heap corruption. This commit resolves the issue by: 1. Rejecting duplicate responses early in `ParseResponse` if a response has already been received and saved for the query (`info.mSavedResponse != nullptr`), returning `kErrorDrop`. 2. Initializing the `mSavedResponse` field of the `QueryInfo` struct to `nullptr` before allocating the host resolution query (`newQuery`) to prevent it from inheriting a potentially non-null saved response from its parent.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request updates the DNS client to prevent pointer aliasing by ensuring mSavedResponse is nullified during query initialization and verified during response parsing. The reviewer recommends checking other functions that copy QueryInfo for similar issues and suggests centralizing the initialization logic within AllocateQuery to improve long-term robustness.
I am having trouble creating individual review comments. Click here to see my feedback.
src/core/net/dns_client.cpp (1876)
This fix correctly prevents the new sub-query from inheriting the parent's mSavedResponse pointer. It is worth verifying if other functions that allocate new queries by copying QueryInfo (such as ReplaceWithSeparateSrvTxtQueries or ReplaceWithIp4Query) also need to explicitly clear mSavedResponse and mNextQuery to avoid similar pointer aliasing issues. Centralizing this clearing logic within AllocateQuery (by having it always clear these fields in the written QueryInfo) might be a more robust long-term solution.
References
- Ensure that structures are properly initialized or cleared to maintain invariants and avoid pointer aliasing issues, similar to the requirement for Metadata structures to be zero-initialized in Clear().
…penthread#13060) Fix a double-free of `mSavedResponse` in `Dns::Client` when processing duplicate DNS responses matching an active query. When an SRV/TXT query needs to resolve a host address (AAAA), the DNS client allocates a chained `newQuery` to handle it. If duplicate responses are processed before the query chain is finalized, they trigger multiple AAAA resolution allocations for the same parent query. Because the new query inherits `mSavedResponse` from the parent query's `QueryInfo`, multiple chained queries end up aliasing/sharing the same cloned `mSavedResponse` message. During finalization, `FreeQuery` walks the chain and frees `mSavedResponse` for each query, leading to a double-free of the shared `Message` and free-list/heap corruption. This commit resolves the issue by: 1. Rejecting duplicate responses early in `ParseResponse` if a response has already been received and saved for the query (`info.mSavedResponse != nullptr`), returning `kErrorDrop`. 2. Initializing the `mSavedResponse` field of the `QueryInfo` struct to `nullptr` before allocating the host resolution query (`newQuery`) to prevent it from inheriting a potentially non-null saved response from its parent.
Fix a double-free of
mSavedResponseinDns::Clientwhen processing duplicate DNS responses matching an active query.When an SRV/TXT query needs to resolve a host address (AAAA), the DNS client allocates a chained
newQueryto handle it. If duplicate responses are processed before the query chain is finalized, they trigger multiple AAAA resolution allocations for the same parent query. Because the new query inheritsmSavedResponsefrom the parent query'sQueryInfo, multiple chained queries end up aliasing/sharing the same clonedmSavedResponsemessage. During finalization,FreeQuerywalks the chain and freesmSavedResponsefor each query, leading to a double-free of the sharedMessageand free-list/heap corruption.This commit resolves the issue by:
ResolveHostAddressIfNeededif a response has already been received and saved for the query (info.mSavedResponse != nullptr).mSavedResponsefield of theQueryInfostruct tonullptrbefore allocating the host resolution query (newQuery) to prevent it from inheriting a potentially non-null saved response from its parent.