Skip to content

monitor: test the message-type bit, and always pass NFT_MSG_NEWGEN - #377

Open
0z5a wants to merge 1 commit into
google:mainfrom
0z5a:monitor-filter-bit-test
Open

0z5a wants to merge 1 commit into
google:mainfrom
0z5a:monitor-filter-bit-test

Conversation

@0z5a

@0z5a 0z5a commented Nov 19, 2026

Copy link
Copy Markdown

I started from #374 and checked it against the code. The precedence bug is as described there: monitor.monitorFlags&1<<msgType is (monitorFlags&1)<<msgType. Fixing only the parentheses is not enough, though, because the same check also filters NFT_MSG_NEWGEN, and that message is what hands the collected changes to the caller. NFT_MSG_NEWGEN is only in the MonitorActionAny/MonitorObjectAny mask, so with the parentheses fixed alone, every filtered monitor would stop receiving events, including the Ruleset and MonitorActionNew ones that work today.

This PR tests the bit for msgType and always lets NFT_MSG_NEWGEN through.

How I found it

After reading #374 I went through monitor() to see what the filter feeds into. Since the change to deliver events per batch, nothing is sent until NFT_MSG_NEWGEN arrives, so whether a monitor sees anything depends on whether its mask lets that message through. Today that comes down to bit 0 (NFT_MSG_NEWTABLE):

  • Masks with bit 0 let every message through, unfiltered: Any/Tables, Any/Ruleset, New/Any, New/Tables, New/Ruleset, and the combinations that fall back to them in NewMonitor.
  • Masks without bit 0 drop every message, NFT_MSG_NEWGEN included, so nothing is delivered: Any/Chains, Any/Rules, Any/Sets, Any/Elements, New/Chains, New/Rules, New/Sets, and Del/Any.

I then wrote a system test with three filters and ran it against main, against the parentheses-only change, and against this change (results below).

Where this can show up in practice

  • A program that only cares about rules or chains, for example one that re-applies its own rule when someone else deletes it, uses WithMonitorObject(MonitorObjectRules) or WithMonitorAction(MonitorActionDel): the monitor starts without error and never sends anything, so the program silently never reacts.
  • A program that watches new tables with MonitorActionNew and MonitorObjectTables also gets every chain, rule and set change, and has to filter by type itself. Code that assumes Data is a *Table fails on the other events.
  • Callers that rely on today's behaviour of Ruleset or New/* masks, such as sing-tun (Any/Ruleset, generational) or netbird (New with Chains|Tables, which falls back to New/Any): these would stop getting events if only the parentheses were fixed. With this change they keep getting the events they asked for.

This is my estimate; I have not seen a field report.

What changes

  • monitor.go: the filter tests monitorFlags&(1<<msgType) and skips the check for NFT_MSG_NEWGEN.
  • monitor_test.go: TestMonitorFilter adds a table, a chain and a rule in one batch and checks which changes arrive for a Rules monitor, a New/Tables monitor and a Ruleset monitor. The Ruleset case already passes on main; it is there to catch the parentheses-only version.

Two behaviour changes to be aware of:

  • New/* masks, including the New/Any fallback, no longer receive Del* and object events, which they got only because of the bug. Unfiltered (Any/Any) and Any/Ruleset monitors behave as before.
  • An AddGenerationalMonitor caller with a filter now gets a MonitorEvents with empty Changes for a batch that only touched other objects, the same way the generation event is reported for every batch. AddMonitor callers don't see this, since it only forwards Changes.

Testing

Cross-compiled on macOS (GOOS=linux GOARCH=arm64 go test -c) and ran in a privileged Ubuntu 24.04 container on a local Linux VM (arm64, kernel 6.8, nf_tables), with -run_system_tests:

case main parentheses only this PR
rules no events within 5s no events within 5s PASS
new tables got [0 3 6], want [0] no events within 5s PASS
ruleset PASS no events within 5s PASS

TestMonitor passes on all three. With the patch, all package tests (., alignedbuff, binaryutil, expr, userdata, xt) pass except TestTableOwnership and TestSocketDrainingOnErrors, which fail the same way on unpatched main in that container ("operation not supported" from the kernel). Without -run_system_tests the new test skips like the other system tests. GOOS=linux go vet ./... and gofmt are clean.

I did not run the integration tests, because the container has no nft binary; they don't use the monitor. I have not run this on x86_64.

Fixes #374

monitorFlags&1<<msgType parses as (monitorFlags&1)<<msgType, since & and
<< have the same precedence in Go. Masks with bit 0 (NEWTABLE) therefore
let every message through, and masks without it, such as
MonitorObjectRules or MonitorActionDel, drop every message, including
NFT_MSG_NEWGEN, which is what delivers the batch. Rules-only monitors
never get an event, and a new-tables monitor also gets chains and rules.

Test the bit for msgType, and never filter out NFT_MSG_NEWGEN, which is
not in any mask but MonitorObjectAny with MonitorActionAny. Without that,
Ruleset and MonitorActionNew monitors, which work today because their
masks contain bit 0, would stop receiving events.

Fixes google#374
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.

Monitor filters drop selected rule events because the message-type bit test is mis-grouped

1 participant