netfilter: nf_tables: fix use-after-free in nf_tables_addchain()
Subsystems (2)
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.
[ 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. ]
---
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
@@ -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)
Sasha Levin
maintainerQuoted 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 totable->chainsand there is noerr_register_hooklabel.
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