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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading