Skip to content

netfilter_nft: keep the netlink socket in sync when a rule-cache refresh errors - #941

Merged
miniupnp merged 1 commit into
miniupnp:masterfrom
tritsystem:fix/nft-refresh-desync
Nov 2, 2026
Merged

miniupnp merged 1 commit into
miniupnp:masterfrom
tritsystem:fix/nft-refresh-desync

Conversation

@tritsystem

Copy link
Copy Markdown
Contributor

Problem

refresh_nft_cache() dumps a chain's rules over the long-lived netlink socket
(static struct mnl_socket *mnl_sock, opened once in init_redirect()). Its
receive loop runs mnl_cb_run(..., table_cb, ...), and the moment table_cb
returns MNL_CB_ERROR it does return -1 without reading the rest of the
multipart dump:

ret = mnl_cb_run(buf, n, mnl_seq, mnl_portid, table_cb, &data);
if (ret <= -1 /*== MNL_CB_ERROR*/) {
    syslog(LOG_ERR, "%s: mnl_cb_run returned %d: %m", "refresh_nft_cache", ret);
    return -1;                 /* leaves the rest of the dump queued */
}

table_cb returns MNL_CB_ERROR on three per-message conditions:
nftnl_rule_alloc() failure, nftnl_rule_nlmsg_parse() failure, and the
malloc(sizeof(rule_t)) failure. When the chain has more rules than fit in one
MNL_SOCKET_BUFFER_SIZE recv, the dump spans several datagrams; aborting on the
first error leaves the remainder — including the final NLMSG_DONE — queued on
the socket. Because the socket is long-lived, the next refresh_nft_cache()
reads those stale, previous-sequence messages first, so mnl_cb_run keeps
returning -1 on every subsequent refresh. Rule listing then stays broken
(mnl_cb_run returned -1 logged repeatedly) until the daemon is restarted.

A missing strdup() result in the same callback is also stored unchecked and
later dereferenced.

Fix

Don't abort the dump on a per-message error. table_cb records the failure in
its callback data and returns MNL_CB_OK, so mnl_cb_run drains the dump to
NLMSG_DONE and the socket stays in sync; refresh_nft_cache() reports the
incomplete refresh (and flushes the half-built cache) only after the dump is
fully read. The strdup() results are now checked.

Verification

Built the nftables backend at current master and drove the real path
delete_redirect_and_filter_rules -> refresh_nft_cache -> mnl_cb_run -> table_cb against real nft rules in a network namespace, with 80 redirect rules
so the dump spans several recv buffers. A single allocation failure was injected
(-Wl,--wrap=malloc, failing the first rule_t) on one refresh:

  • Before: that refresh aborts mid-dump; the next refreshes — with no failure
    injected at all — keep logging mnl_cb_run returned -1, i.e. the socket stays
    desynced.
  • After: the failing refresh reports the incomplete cache once and recovers;
    later refreshes are clean.

This may also be relevant to the log-spam in #824, though I could not reproduce
that report's specific conditions, so I'm filing it as a robustness fix for the
desync itself rather than a claimed fix for #824.

AI disclosure

I used Claude Code as an assistant to investigate, fix and verify this; I
reviewed the analysis and the fix before submitting.

…esh errors

refresh_nft_cache() aborts the rule dump with `return -1` the moment
table_cb() returns MNL_CB_ERROR, without reading the rest of the
multipart response. On the long-lived mnl socket this leaves the
remaining datagrams (including NLMSG_DONE) queued, so the next refresh
reads stale, previous-sequence messages and mnl_cb_run keeps returning
-1 on every subsequent refresh until the daemon is restarted.

Record the per-message failure in the callback data and return
MNL_CB_OK so mnl_cb_run drains the dump to NLMSG_DONE; report the
incomplete refresh (and flush the half-built cache) only afterwards.
Also check the strdup() results, which were previously stored unchecked.
miniupnp added a commit that referenced this pull request Nov 2, 2026
@miniupnp
miniupnp merged commit f6b66ff into miniupnp:master Nov 2, 2026
29 of 34 checks passed
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.

2 participants