Skip to content

Commit

Permalink
Fix: Prevent panic in DNS Socket when server list exceeds max count
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Jamie Bird committed Sep 12, 2024
1 parent 8025752 commit 4739cc7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/socket/dns.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::cmp::min;
#[cfg(feature = "async")]
use core::task::Waker;

Expand Down Expand Up @@ -149,27 +150,30 @@ pub struct Socket<'a> {
impl<'a> Socket<'a> {
/// Create a DNS socket.
///
/// # Panics
///
/// Panics if `servers.len() > MAX_SERVER_COUNT`
/// Truncates the server list if `servers.len() > MAX_SERVER_COUNT`
pub fn new<Q>(servers: &[IpAddress], queries: Q) -> Socket<'a>
where
Q: Into<ManagedSlice<'a, Option<DnsQuery>>>,
{
let truncated_servers = &servers[..min(servers.len(), DNS_MAX_SERVER_COUNT)];

Socket {
servers: Vec::from_slice(servers).unwrap(),
servers: Vec::from_slice(truncated_servers).unwrap(),
queries: queries.into(),
hop_limit: None,
}
}

/// Update the list of DNS servers, will replace all existing servers
///
/// # Panics
///
/// Panics if `servers.len() > MAX_SERVER_COUNT`
/// Truncates the server list if `servers.len() > MAX_SERVER_COUNT`
pub fn update_servers(&mut self, servers: &[IpAddress]) {
self.servers = Vec::from_slice(servers).unwrap();
if servers.len() > DNS_MAX_SERVER_COUNT {
net_trace!("Max DNS Servers exceeded. Increase MAX_SERVER_COUNT");
self.servers = Vec::from_slice(&servers[..DNS_MAX_SERVER_COUNT]).unwrap();
} else {
self.servers = Vec::from_slice(servers).unwrap();
}
}

/// Return the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
Expand Down

0 comments on commit 4739cc7

Please sign in to comment.