Skip to content

Fix addrset matching when a broader CIDR follows a narrower one - #3573

Closed
protonnegativo wants to merge 1 commit into
nmap:masterfrom
protonnegativo:fix/3565-addrset-cidr-order
Closed

protonnegativo wants to merge 1 commit into
nmap:masterfrom
protonnegativo:fix/3565-addrset-cidr-order

Conversation

@protonnegativo

Copy link
Copy Markdown

Fixes #3565

When a CIDR spec is added after a narrower one, and its prefix is exactly the common prefix of the two, the addrset trie does not cover the whole broader range. This affects nmap --exclude/--excludefile and ncat --allow/--deny.

Repro (list scan, no packets sent):

$ ./nmap -sL -n 10.150.0.20 --exclude 10.200.0.0/16,10.0.0.0/8
Nmap scan report for 10.150.0.20
$ ./nmap -sL -n 10.150.0.20 --exclude 10.0.0.0/8,10.200.0.0/16
WARNING: No targets were specified, so 0 hosts scanned.

Same with Ncat on loopback: ncat -l 127.0.0.1 31565 --deny 127.200.0.0/16,127.0.0.0/8 accepts a client connecting from 127.150.0.20 (ncat -s 127.150.0.20 127.0.0.1 31565). With the specs in the other order it is denied.

Cause: in trie_split(), a node is replaced by a match-all node only when the new mask is strictly shorter than the common prefix (new_mask[i] > mask[i]). When they are equal (10.0.0.0/8 against the 10.200.0.0/16 node, common prefix /8), the node is split at /8 instead. 10.200.0.0/16 stays on the "1" branch, so 10.150.x.x goes down that branch and does not match. The fix treats equality as superseding too, but only in the word where the new mask ends (mask[i] != U32_ALL_BITS), so it also works for masks that end on a 32-bit boundary (IPv6 /32, or IPv4 0.0.0.0/0, which also wasn't covering everything after a narrower spec).

Testing:

  • Added overlapping-CIDR cases to ncat/test/test-addrset.sh (both orders, three specs, a narrower spec followed by 0.0.0.0/0, and an IPv6 /48 + /32 case). Without the fix 4 of them fail (52/56); with it, 56/56. make -C ncat check passes.
  • Ran a random differential test of ncat/test/addrset against Python's ipaddress (2-6 random IPv4/IPv6 CIDRs per set, probes at and around the range edges). Before the fix, 460 of 3000 sets gave wrong results. After it, 0 of 6500.
  • The nmap and ncat commands above now exclude/deny 10.150.0.20 / 127.150.0.20 in both orders.

@protonnegativo
protonnegativo marked this pull request as ready for review October 24, 2026 16:30
@nmap-bot nmap-bot closed this in 0b2b71a Oct 29, 2026
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.

Nmap 7.992: broader CIDR added after narrower CIDR can bypass Ncat --deny and Nmap --exclude

1 participant