From c5d077a2c62edb4d704fc64dc6eeea5a3ed0dcb0 Mon Sep 17 00:00:00 2001 From: Gasper Stukelj Date: Sun, 28 Jul 2024 21:50:52 +0200 Subject: [PATCH] embassy-net: dns: bound the dns_servers slice `smoltcp::socket::dns::Socket::update_servers()` will panic if a slice exceeding a fixed length is passed to it. This is can be especially inconvenient when using DHCP config. Avoid panicking by using at most `DNS_MAX_SERVER_COUNT` DNS servers from the config. --- embassy-net/src/lib.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 12f5f30b4..524799a1b 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs @@ -34,6 +34,8 @@ use embassy_sync::waitqueue::WakerRegistration; use embassy_time::{Instant, Timer}; #[allow(unused_imports)] use heapless::Vec; +#[cfg(feature = "dns")] +pub use smoltcp::config::DNS_MAX_SERVER_COUNT; #[cfg(feature = "igmp")] pub use smoltcp::iface::MulticastError; #[allow(unused_imports)] @@ -823,9 +825,17 @@ impl Inner { // Apply DNS servers #[cfg(feature = "dns")] - s.sockets - .get_mut::(self.dns_socket) - .update_servers(&dns_servers[..]); + if !dns_servers.is_empty() { + let count = if dns_servers.len() > DNS_MAX_SERVER_COUNT { + warn!("Number of DNS servers exceeds DNS_MAX_SERVER_COUNT, truncating list."); + DNS_MAX_SERVER_COUNT + } else { + dns_servers.len() + }; + s.sockets + .get_mut::(self.dns_socket) + .update_servers(&dns_servers[..count]); + } self.config_waker.wake(); }