Skip to contentSkip to conversation

netfilter: nf_tables: fix use-after-free in nf_tables_addchain()

PATCH In review Steffen Pfendtner 2 participants 2 messages Oct 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nf_tables: fix use-after-free in nf_tables_addchain()

From: Inseo An <[email protected]>

[ Upstream commit 71e99ee20fc3f662555118cf1159443250647533 ]

nf_tables_addchain() publishes the chain to table->chains via list_add_tail_rcu() (in nft_chain_add()) before registering hooks. If nf_tables_register_hook() then fails, the error path calls nft_chain_del() (list_del_rcu()) followed by nf_tables_chain_destroy() with no RCU grace period in between.

This creates two use-after-free conditions:

1) Control-plane: nf_tables_dump_chains() traverses table->chains under rcu_read_lock(). A concurrent dump can still be walking the chain when the error path frees it.

2) Packet path: for NFPROTO_INET, nf_register_net_hook() briefly installs the IPv4 hook before IPv6 registration fails. Packets entering nft_do_chain() via the transient IPv4 hook can still be dereferencing chain->blob_gen_X when the error path frees the chain.

Add synchronize_rcu() between nft_chain_del() and the chain destroy so that all RCU readers -- both dump threads and in-flight packet evaluation -- have finished before the chain is freed.

Fixes 91c7b38dc9f0 ("netfilter: nf_tables: use new transaction infrastructure to handle chain")

[ Backport to 5.10.y: 5.10 lacks d472e9853d7b ("netfilter: nf_tables:
register hooks last when adding new chain/flowtable"), so the hooks are registered before the chain is added to table->chains and there is no err_register_hook label. A failing nf_tables_register_hook() can still have exposed the chain to the packet path through a transiently registered hook (the IPv4 hook of an inet chain when the IPv6 hook fails to register), so call synchronize_rcu() before the chain is destroyed on that path. This mirrors how the 5.15.y backport of d73f4b53aaae handles the same situation for flowtables. ]

Assisted-by Claude:claude-opus-5-5

---
Not a clean cherry-pick (see the bracketed note): 5.15 registers the chain hook before adding the chain and has no err_register_hook label, so the synchronize_rcu() is placed in the hook-registration failure branch. A review from the netfilter side would be welcome.

Build-tested on arm64; boot-tested under QEMU with KASAN: nf_tables chain add/list/delete across families works, ruleset flush is clean, no KASAN reports.

 net/netfilter/nf_tables_api.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +3 -1
index 63592c73a7a1..9862a34010d0 100644
@@ -2341,8 +2341,10 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
 	rcu_assign_pointer(chain->rules_gen_1, rules);
 
 	err = nf_tables_register_hook(net, table, chain);
-	if (err < 0)
+	if (err < 0) {
+		synchronize_rcu();
 		goto err_destroy_chain;
+	}
 
 	if (!nft_use_inc(&table->use)) {
 		err = -EMFILE;
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#1 lore raw
Quoted 4 lines

[ Backport to 5.10.y: 5.10 lacks d472e9853d7b ("netfilter: nf_tables:
register hooks last when adding new chain/flowtable"), so the hooks are registered before the chain is added to table->chains and there is no err_register_hook label.

Same as the 5.15 patch: this is a custom stable-only fix rather than a backport of 71e99ee20fc3 ("netfilter: nf_tables: fix use-after-free in nf_tables_addchain()"). The synchronize_rcu() goes in the register_hook failure branch instead of upstream's err_register_hook label, and only the packet-path half of the upstream commit applies on this tree.

It needs an ack from a netfilter maintainer on the thread before stable takes it. Pablo, Florian: could you take a look? Please resend with the ack once you have it.

Signature · 2 lines
Thanks,
Sasha