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

Prevent Panic in DNS Socket by Truncating Server List #986

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

birdistheword96
Copy link

Summary
This PR addresses an issue in the Socket constructor where passing a server list longer than DNS_MAX_SERVER_COUNT would cause a panic. The number of DNS servers available is often dictated by the network configuration, especially when using DHCP, and is not something the developer can directly control. As such, the previous behaviour of panicking when more servers are provided than allowed feels like a landmine.
The code now safely truncates the server list to ensure it does not exceed the maximum allowed number of servers, preventing any runtime panics.

Changes

  • Truncated the input servers slice to a maximum of DNS_MAX_SERVER_COUNT before creating the Socket.
  • When tracing/logging is enabled, it warns that the list would be overflowed
  • This modification makes the function resilient to server lists longer than the allowed maximum.

Why This Fix?
This fix is essential for environments where the number of servers may dynamically change or where input server lists might inadvertently exceed the allowed limit. The developer cannot know ahead of time how many DNS servers might be available to a user, and although setting DNS_MAX_SERVER_COUNT to a higher value limits the likelihood of this happening, it can still happen. The previous behaviour would result in a panic, whereas the new implementation gracefully handles the case by truncating the server list.

- Truncate the servers list to DNS_MAX_SERVER_COUNT to prevent panics.
- Ensure only the first `DNS_MAX_SERVER_COUNT` servers are used when constructing the `Socket`.
- This prevents overflow issues when the provided server list is larger than the allowed maximum.
Copy link
Member

@Dirbaio Dirbaio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this has caused enough trouble already 👍

@Dirbaio Dirbaio added this pull request to the merge queue Sep 12, 2024
Merged via the queue into smoltcp-rs:main with commit 8602c01 Sep 12, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants