Repository navigation
netfilter_nft: keep the netlink socket in sync when a rule-cache refresh errors - #941
Merged
Merged
Conversation
…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.
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.
Problem
refresh_nft_cache()dumps a chain's rules over the long-lived netlink socket(
static struct mnl_socket *mnl_sock, opened once ininit_redirect()). Itsreceive loop runs
mnl_cb_run(..., table_cb, ...), and the momenttable_cbreturns
MNL_CB_ERRORit doesreturn -1without reading the rest of themultipart dump:
table_cbreturnsMNL_CB_ERRORon three per-message conditions:nftnl_rule_alloc()failure,nftnl_rule_nlmsg_parse()failure, and themalloc(sizeof(rule_t))failure. When the chain has more rules than fit in oneMNL_SOCKET_BUFFER_SIZErecv, the dump spans several datagrams; aborting on thefirst error leaves the remainder — including the final
NLMSG_DONE— queued onthe socket. Because the socket is long-lived, the next
refresh_nft_cache()reads those stale, previous-sequence messages first, so
mnl_cb_runkeepsreturning -1 on every subsequent refresh. Rule listing then stays broken
(
mnl_cb_run returned -1logged repeatedly) until the daemon is restarted.A missing
strdup()result in the same callback is also stored unchecked andlater dereferenced.
Fix
Don't abort the dump on a per-message error.
table_cbrecords the failure inits callback data and returns
MNL_CB_OK, somnl_cb_rundrains the dump toNLMSG_DONEand the socket stays in sync;refresh_nft_cache()reports theincomplete 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_cbagainst real nft rules in a network namespace, with 80 redirect rulesso the dump spans several recv buffers. A single allocation failure was injected
(
-Wl,--wrap=malloc, failing the firstrule_t) on one refresh:injected at all — keep logging
mnl_cb_run returned -1, i.e. the socket staysdesynced.
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.