Skip to content
Merged
Changes from 1 commit
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
22 changes: 21 additions & 1 deletion host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ impl AllowList {

fn learn_ip(&self, ip: IpAddr) {
if let Ok(mut learned) = self.learned_ips.lock() {
learned.insert(ip);
if learned.len() < MAX_LEARNED_IPS {
learned.insert(ip);
}
}
}
}
Expand Down Expand Up @@ -956,6 +958,9 @@ struct HostSocket {
sock_type: i32,
}

/// Maximum number of IPs learned from DNS responses for AllowList policy.
const MAX_LEARNED_IPS: usize = 256;

const MAX_SOCKETS: usize = 1024;

struct SocketTable {
Expand Down Expand Up @@ -3214,4 +3219,19 @@ mod tests {
"expected 'too large' error for net_sendto, got: {resp}"
);
}

#[test]
fn allowlist_learned_ips_capped() {
use std::net::{IpAddr, Ipv4Addr};
let al = AllowList::from_hosts(&["example.com"]).unwrap();
// Fill up to the cap
for i in 0..MAX_LEARNED_IPS {
let ip = IpAddr::V4(Ipv4Addr::new(10, 0, (i / 256) as u8, (i % 256) as u8));
al.learn_ip(ip);
}
assert_eq!(al.learned_ips.lock().unwrap().len(), MAX_LEARNED_IPS);
// One more should NOT be added
al.learn_ip(IpAddr::V4(Ipv4Addr::new(10, 1, 0, 1)));
assert_eq!(al.learned_ips.lock().unwrap().len(), MAX_LEARNED_IPS);
}
}
Loading