Skip to content

address: refuse CIDR blocks too large to expand - #974

Open
qianeric-backup wants to merge 1 commit into
bee-san:masterfrom
qianeric-backup:refuse-oversized-cidr
Open

qianeric-backup wants to merge 1 commit into
bee-san:masterfrom
qianeric-backup:refuse-oversized-cidr

Conversation

@qianeric-backup

Copy link
Copy Markdown
Contributor

Expanding a CIDR block builds a Vec<IpAddr>, 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, which trips the allocator's own capacity check; the second asks for 68 GiB at 16 bytes per address. Neither prints anything the 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.

$ rustscan -a 10.0.0.0/0 -p 1
[!] Host "10.0.0.0/0" spans 4294967296 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. That keeps every IPv4 block down to a /8 and every IPv6 block from /104 up, and it is a single constant to raise if it 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, and cargo clippy -- --deny warnings, cargo clippy --tests -- --deny warnings and cargo fmt --check on the file are clean.

Expanding a CIDR block builds a `Vec<IpAddr>`, 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.
@sonarqubecloud

Copy link
Copy Markdown

@qianeric-backup

Copy link
Copy Markdown
Contributor Author

Note on the red Test Suite (ubuntu-latest) job: it fails before touching this diff.

cargo test
warning: `package.homepage` is redundant with `package.repository`
 --> Cargo.toml:7:12
  = note: `cargo::redundant_homepage` is set to `warn` by default
help: consider removing `package.homepage`
error: warnings are denied by `build.warnings` configuration
error: recipe `test` failed on line 2 with exit code 101

The last run on master (b46f982) was on 2026-11-04 and passed; the runner's stable toolchain has picked up the redundant_homepage manifest lint since then, and just test under build-warnings: deny turns it into a hard error, so cargo test never starts. The other three matrix jobs are cancelled, not failed.

sonar passes, and the same cargo test / cargo clippy --deny warnings / rustfmt --check all pass on cargo 1.99.0 locally. Dropping homepage from Cargo.toml fixes it, but that is a separate change to published crate metadata, so I left it out of this PR – happy to send it separately if you want the job green.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant