Repository navigation
Fix addrset matching when a broader CIDR follows a narrower one - #3573
Closed
protonnegativo wants to merge 1 commit into
Closed
protonnegativo wants to merge 1 commit into
protonnegativo wants to merge 1 commit into
Conversation
protonnegativo
marked this pull request as ready for review
October 24, 2026 16:30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/--excludefileandncat --allow/--deny.Repro (list scan, no packets sent):
Same with Ncat on loopback:
ncat -l 127.0.0.1 31565 --deny 127.200.0.0/16,127.0.0.0/8accepts 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 IPv40.0.0.0/0, which also wasn't covering everything after a narrower spec).Testing:
ncat/test/test-addrset.sh(both orders, three specs, a narrower spec followed by0.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 checkpasses.ncat/test/addrsetagainst Python'sipaddress(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.