From 4f32e97ad197ac4b475bd22a30d441b10965bd96 Mon Sep 17 00:00:00 2001 From: QianEric Date: Sat, 21 Nov 2026 07:23:41 +0800 Subject: [PATCH] address: refuse CIDR blocks too large to expand Expanding a CIDR block builds a `Vec`, so a block that is too wide does not fail politely - it takes the process down: $ rustscan -a 2001:db8::/64 -p 80 thread 'main' panicked at library/alloc/src/raw_vec/mod.rs:28:5: capacity overflow $ rustscan -a 10.0.0.0/0 -p 1 memory allocation of 73014444032 bytes failed The first asks for 2^64 entries and trips the allocator's own capacity check; the second asks for 68 GiB at 16 bytes per address. Neither prints anything a user can act on, and both are one command-line argument away. The block size is now computed before any expansion, and a block wider than `MAX_CIDR_ADDRESSES` is skipped with a message that says why: $ rustscan -a 2001:db8::/64 -p 80 [!] Host "2001:db8::/64" spans 18446744073709551616 addresses, more than the 16777216 RustScan expands; split the block up. [!] No IPs could be resolved, aborting scan. The limit is 2^24 addresses: 256 MiB of `IpAddr`, which keeps every IPv4 block down to a /8 and every IPv6 block from /104 up, and is the constant to raise if that turns out too tight. Reporting it in the existing `unresolved_addresses` loop keeps `parse_address`'s signature, and that loop now names the real cause instead of calling a valid block "could not be resolved". ### Tests Two unit tests in `src/address.rs`: - `cidr_size_counts_addresses` covers /24, IPv4 /0, IPv6 /64 and `::/0` (`1u128 << 128` would be a shift overflow, so that one is `u128::MAX`) - `oversized_cidr_is_skipped_instead_of_aborting` parses both crashing targets and asserts they come back empty With the guard removed the second test panics with `capacity overflow`. `cargo test` is 99 passed, `cargo clippy -- --deny warnings`, `cargo clippy --tests -- --deny warnings` and `cargo fmt --check` on the file are clean. --- src/address.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/src/address.rs b/src/address.rs index 8a43a7f1..de7b558e 100644 --- a/src/address.rs +++ b/src/address.rs @@ -65,8 +65,24 @@ fn parse_addresses_with_resolver( } // If we got to this point this can only be a file path or the wrong input. - for file_path in unresolved_addresses { - let file_path = Path::new(file_path); + for address in unresolved_addresses { + // A block too wide to expand is valid notation, not a missing host, so + // name the real reason it was skipped. + if let Ok(net_addr) = IpInet::from_str(address) { + warning!( + format!( + "Host {address:?} spans {} addresses, more than the \ + {MAX_CIDR_ADDRESSES} RustScan expands; split the block up.", + cidr_size(&net_addr) + ), + input.greppable, + input.accessible + ); + + continue; + } + + let file_path = Path::new(address); if !file_path.is_file() { warning!( @@ -115,6 +131,27 @@ pub fn parse_address(address: &str, resolver: &Resolver) -> Vec { parse_address_with_resolver(address, &|| resolver) } +/// Largest CIDR block that gets expanded into addresses. +/// +/// A block is expanded eagerly into a `Vec`, 16 bytes per address, so +/// 2^24 already costs 256 MiB. Anything wider does not fail politely: an IPv4 +/// `/0` asks for 68 GiB and the allocation failure aborts the process, and an +/// IPv6 `/64` asks for 2^64 entries, which trips the allocator's own capacity +/// check and aborts just as hard. Both are one command-line argument away, so +/// the expansion is refused before it starts. +const MAX_CIDR_ADDRESSES: u128 = 1 << 24; + +/// Number of addresses a CIDR block contains. +fn cidr_size(net: &IpInet) -> u128 { + let address_bits = if net.is_ipv4() { 32 } else { 128 }; + let host_bits = address_bits - u32::from(net.network_length()); + if host_bits >= 128 { + u128::MAX + } else { + 1u128 << host_bits + } +} + fn parse_address_with_resolver<'a>( address: &str, resolver: &impl Fn() -> &'a Resolver, @@ -124,6 +161,9 @@ fn parse_address_with_resolver<'a>( vec![addr] } else if let Ok(net_addr) = IpInet::from_str(address) { // `address` is a CIDR string + if cidr_size(&net_addr) > MAX_CIDR_ADDRESSES { + return Vec::new(); + } net_addr.network().into_iter().addresses().collect() } else { // `address` is a hostname or DNS name @@ -315,7 +355,7 @@ fn read_ips_from_file<'a>( #[cfg(test)] mod tests { - use super::{parse_addresses, parse_addresses_with_resolver, Opts}; + use super::{cidr_size, parse_addresses, parse_addresses_with_resolver, Opts}; use std::net::{IpAddr, Ipv4Addr}; #[test] @@ -357,6 +397,33 @@ mod tests { .is_empty()); } + #[test] + fn cidr_size_counts_addresses() { + assert_eq!(cidr_size(&"10.0.0.0/24".parse().unwrap()), 1 << 8); + assert_eq!(cidr_size(&"10.0.0.0/0".parse().unwrap()), 1 << 32); + assert_eq!(cidr_size(&"2001:db8::/64".parse().unwrap()), 1u128 << 64); + // A zero-length IPv6 prefix is wider than u128 can count. + assert_eq!(cidr_size(&"::/0".parse().unwrap()), u128::MAX); + } + + #[test] + fn oversized_cidr_is_skipped_instead_of_aborting() { + // Both of these used to abort the process while being expanded: the + // IPv6 block asks for 2^64 entries and the IPv4 one for 68 GiB. + for address in ["2001:db8::/64", "10.0.0.0/0"] { + let opts = Opts { + addresses: vec![address.to_owned()], + ..Default::default() + }; + + assert!( + parse_addresses(&opts).is_empty(), + "{} should not be expanded", + address + ); + } + } + #[test] fn parse_correct_addresses() { let opts = Opts {