From e33109b0c08b6c9d61b36ea18021f47747abbd1b Mon Sep 17 00:00:00 2001 From: Yuen Su Date: Sun, 18 Oct 2026 01:31:30 +0800 Subject: [PATCH] pbr-1.2.4: count Tor connections only after all rule matches 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 --- files/lib/pbr/pbr.uc | 15 ++--- tests/04_policies/10_tor_counter_position | 74 +++++++++++++++++++++++ 2 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 tests/04_policies/10_tor_counter_position diff --git a/files/lib/pbr/pbr.uc b/files/lib/pbr/pbr.uc index d2f5bee..3a62ae5 100644 --- a/files/lib/pbr/pbr.uc +++ b/files/lib/pbr/pbr.uc @@ -585,15 +585,16 @@ function create_pbr(fs_mod, uci_mod, ubus_mod) { } chain = 'dstnat'; let p4_base = nft_insert + ' rule inet ' + nft_table + ' ' + nft_prefix + '_' + chain + - rule_params + ' meta nfproto ipv4 ' + param4; + ' meta nfproto ipv4 ' + param4; let p6_base = nft_insert + ' rule inet ' + nft_table + ' ' + nft_prefix + '_' + chain + - rule_params + ' meta nfproto ipv6 ' + param6; + ' meta nfproto ipv6 ' + param6; + // Counters run in order; count only after every match, including dport. let tor_rules = [ - 'udp dport 53 redirect to :' + env.tor_dns_port + ' comment "Tor-DNS-UDP"', - 'tcp dport 80 redirect to :' + env.tor_traffic_port + ' comment "Tor-HTTP-TCP"', - 'udp dport 80 redirect to :' + env.tor_traffic_port + ' comment "Tor-HTTP-UDP"', - 'tcp dport 443 redirect to :' + env.tor_traffic_port + ' comment "Tor-HTTPS-TCP"', - 'udp dport 443 redirect to :' + env.tor_traffic_port + ' comment "Tor-HTTPS-UDP"', + 'udp dport 53' + rule_params + ' redirect to :' + env.tor_dns_port + ' comment "Tor-DNS-UDP"', + 'tcp dport 80' + rule_params + ' redirect to :' + env.tor_traffic_port + ' comment "Tor-HTTP-TCP"', + 'udp dport 80' + rule_params + ' redirect to :' + env.tor_traffic_port + ' comment "Tor-HTTP-UDP"', + 'tcp dport 443' + rule_params + ' redirect to :' + env.tor_traffic_port + ' comment "Tor-HTTPS-TCP"', + 'udp dport 443' + rule_params + ' redirect to :' + env.tor_traffic_port + ' comment "Tor-HTTPS-UDP"', ]; for (let dest_rule in tor_rules) { if (!src_inline_set_ipv4_empty && !dest_inline_set_ipv4_empty) { diff --git a/tests/04_policies/10_tor_counter_position b/tests/04_policies/10_tor_counter_position new file mode 100644 index 0000000..e767ff6 --- /dev/null +++ b/tests/04_policies/10_tor_counter_position @@ -0,0 +1,74 @@ +Tor counters must follow family, address, protocol and port matches. + +-- File uci/pbr.json -- +{ + "pbr": [ + { + ".name": "config", + "enabled": "1", + "fw_mask": "00ff0000", + "ipv6_enabled": "1", + "nft_rule_counter": "1", + "resolver_set": "none", + "uplink_interface": "wan", + "uplink_interface6": "wan6", + "uplink_ip_rules_priority": "30000", + "uplink_mark": "00010000", + "verbosity": "2", + "lan_device": [ + "br-lan" + ], + "resolver_instance": [ + "*" + ] + } + ], + "policy": [ + { + ".name": "policy_tor", + "name": "Tor Counters", + "enabled": "1", + "interface": "tor", + "dest_addr": "192.0.2.10 2001:db8::10" + } + ] +} +-- End -- + +-- File fs/open~_etc_tor_torrc.txt -- +TransPort 0.0.0.0:9040 +DNSPort 0.0.0.0:9053 +-- End -- + +-- File fs/stat~_etc_tor_torrc.json -- +{ "type": "file", "size": 100 } +-- End -- + +-- Testcase -- +import create_pbr from 'pbr'; +let pbr = create_pbr(); +let result = pbr.start_service(['on_start']); +let nft = global.mocklib.read_captured('/var/run/pbr.nft') || ''; +let rules = filter(split(nft, '\n'), l => index(l, 'comment "Tor-') >= 0); + +let checks = [ + ['ten_rules', length(rules) == 10], + ['five_ipv4', length(filter(rules, l => index(l, 'meta nfproto ipv4') >= 0)) == 5], + ['five_ipv6', length(filter(rules, l => index(l, 'meta nfproto ipv6') >= 0)) == 5], + ['accepted', length(result.errors || []) == 0], +]; +for (let r in rules) { + push(checks, ['counter_after_every_match', !!match(r, / (udp|tcp) dport (53|80|443) counter redirect to :[0-9]+ /)]); + push(checks, ['one_counter', length(split(r, ' counter ')) == 2]); +} +let pass = 0; +for (let c in checks) { + if (c[1]) pass++; + else printf("FAIL: %s\n", c[0]); +} +printf("tor_counter_position: %d/%d passed\n", pass, length(checks)); +-- End -- + +-- Expect stdout -- +tor_counter_position: 24/24 passed +-- End --