Skip to content

pbr-1.2.4: count Tor connections only after all rule matches - #191

Merged
egc112 merged 1 commit into
mossdef-org:1.2.4from
ZyPulse-zy:fix/tor-counter-position
Oct 18, 2026
Merged

egc112 merged 1 commit into
mossdef-org:1.2.4from
ZyPulse-zy:fix/tor-counter-position

Conversation

@ZyPulse-zy

@ZyPulse-zy ZyPulse-zy commented Oct 17, 2026 •

Copy link
Copy Markdown

With nft_rule_counter enabled, the Tor NAT rules increment their counters before checking the address, transport protocol and destination port. Unrelated connections therefore increment every rule they reach. Move rule_params after the final protocol/dport match, immediately before redirect; the redirect targets and counter-disabled output stay unchanged.

Keep the 24-check IPv4/IPv6 emitter regression in the existing unit-test runner. As requested, this revision removes tests/kernel/ and the TOR_COUNTER_EMIT mode from the ordinary test. The PR now changes only the emitter and its unit test.

Validation:

  • The reduced regression still reports 14/24 on the original implementation and 24/24 with the fix.
  • The complete existing runner passes 72/72 tests on both pinned ucode revisions: oldest supported 85922056ef7abeace3cca3ab28bc1ac2d88e31b1 and current cc323341c31828b2126d90c6b8a95bb4e7de88b0.
  • The oldest-supported syntax gate passes all 17 files, including its rejecting canary.
  • Independent CI for this revision passes both matrices and verifies the exact source commit, sign-off, reduced file list and unchanged emitter hash.

The earlier isolated kernel validation and original integration-test source remain available as independent records outside this PR's diff. That run exercised the ten actual emitted IPv4/IPv6 rules with 36 controlled connections and reproduced the original failure. The production emitter in this revision is byte-for-byte identical to that tested fix. NAT-chain counters here count connections; this was not a Tor-daemon or deployed-router test.

Developed with AI assistance; validation claims above refer to executed tests.

Fixes #188

Copilot AI balanced review requested due to automatic review settings October 17, 2026 17:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@egc112

egc112 commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this, and for the thorough testing. The fix is right: counting has to happen after the port match, directly before redirect, and placing it after the address alone isn't enough, as you found.

For transparency: we had an equivalent fix for #188 on a local branch, built as [match, action] pairs rather than inline. The generated rules are identical: your test passes against our variant and ours against yours. Yours came first and also covers IPv6 in the unit test, so we'd like to take yours.

Independent confirmation on real hardware (OpenWrt 25.12, nftables 1.1.6), with a Tor policy for a real LAN client and a burst of 3 DNS queries, 2 HTTP connections and 1 HTTPS connection:

rule 1.2.4 as released with this change
Tor-DNS-UDP +9 3
Tor-HTTP-TCP +6 2
Tor-HTTP-UDP +4 0
Tor-HTTPS-TCP +4 1
Tor-HTTPS-UDP +3 0

One detail that complements your 36-vs-1 finding: on a real router the released counters aren't all equal, they step down. redirect ends evaluation, so the first Tor rule counts every new connection reaching pbr_dstnat, and each later one only those an earlier Tor rule didn't redirect. The near-identical numbers in #188 just mean those Tor rules rarely matched. Also, pbr_dstnat is a NAT chain, so these counters count connections, not packets.

One request before merging: could you drop tests/kernel/ and the TOR_COUNTER_EMIT branch from 10_tor_counter_position? The kernel test is careful work, but it's a second test system in a second language. It needs root and its own network namespace, neither run_tests.sh nor CI runs it, and it makes the ordinary unit test carry a special mode just to serve it, so in practice nobody would keep it working. The unit test on its own pins the fix well: 24 checks, 14/24 on the unfixed code. Your PR description and your fork keep the kernel results on record.

With that removed, this is good to go from our side. The CI run here passes as well.

The Tor emitter puts counter before family and address matches, so
unrelated connections increment every visited rule. Moving it only past
the address still counts unmatched protocols and ports. Place it after
the final dport match and before redirect.

Add a 24-check regression using the existing emitter and test runner,
covering IPv4 and IPv6 rules. It passes only 14/24 checks without the fix.
Keep privileged kernel integration testing in independent validation
records rather than the upstream unit-test tree.

Signed-off-by: Yuen Su <zypulsecore@gmail.com>
@ZyPulse-zy
ZyPulse-zy force-pushed the fix/tor-counter-position branch from 5ebe0ee to e33109b Compare October 18, 2026 15:49
@ZyPulse-zy ZyPulse-zy changed the title pbr-1.2.4: count Tor packets only after all rule matches pbr-1.2.4: count Tor connections only after all rule matches Oct 18, 2026
@egc112

egc112 commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Thanks !

@egc112
egc112 merged commit 59d00cb into mossdef-org:1.2.4 Oct 18, 2026
3 checks passed
@ZyPulse-zy

Copy link
Copy Markdown
Author

Thanks !

Thanks for the review and helpful feedback! Glad I could contribute.

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.

3 participants