netfilter: nf_tables: release flowtable after rcu grace period on error
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: nf_tables: release flowtable after rcu grace period on error
Use kfree_rcu() to release flowtable from error path, since a hook that already refers to this flowtable can be already registered, exposing this flowtable to packet path and nfnetlink_hook control plane.
Uncovered by KASAN reported as use-after-free from nfnetlink_hook path when dumping hooks.
The number of flowtable objects in a ruleset are expected to be small, the increment is memory consumption should be negligible. In older kernels, users could mistype device names leading to this error path, I prefer struct rcu_head here instead of explicit synchronize_rcu() call.
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -1483,6 +1483,7 @@ void nft_unregister_obj(struct nft_object_type *obj_type);* @genmask: generation mask * @use: number of references to this flow table * @handle: unique object handle +* @rcu_head: deferred release for error path * @hook_list: hook list for hooks per net_device in flowtables * @data: rhashtable and garbage collector */
@@ -1495,6 +1496,7 @@ struct nft_flowtable { u32 genmask:2;u32 use;u64 handle; +struct rcu_head rcu_head;/* runtime data below here */ struct list_head hook_list____cacheline_aligned ;struct nf_flowtable data;
net/netfilter/nf_tables_api.c +1 -1
@@ -9229,7 +9229,7 @@ static int nf_tables_newflowtable(struct sk_buff *skb, err2:kfree (flowtable->name); err1: -kfree (flowtable); +kfree_rcu (flowtable, rcu_head); flowtable_alloc:nft_use_dec_restore (&table->use);
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerQuoted 4 lines · Pablo Neira Ayuso
Use
kfree_rcu()to release flowtable from error path, since a hook that already refers to this flowtable can be already registered, exposing this flowtable to packet path andnfnetlink_hookcontrol plane.
Quoted 8 lines
Uncovered by KASAN reported as use-after-free from
nfnetlink_hookpath when dumping hooks.The number of flowtable objects in a ruleset are expected to be small, the increment is memory consumption should be negligible. In older kernels, users could mistype device names leading to this error path, I prefer struct
rcu_headhere instead of explicitsynchronize_rcu()call.
Scratch this, I will post v2, flowtable->name is also released inmediately, this needs to happen after rcu grace period.