Skip to contentSkip to conversation

netfilter: nf_tables: release flowtable after rcu grace period on error

PATCH Superseded Pablo Neira Ayuso 1 participant 2 messages Mar 17

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 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.

Fixes 3b49e2e94e6e ("netfilter: nf_tables: add flow table netlink frontend")
---
 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
index 3c8a60ec1cc4..ae9905b5ba72 100644
@@ -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
index 2f19c155069e..16b80e17247f 100644
@@ -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
author maintainer
#1 lore raw
Quoted 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 and nfnetlink_hook control plane.

Quoted 8 lines

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.

Scratch this, I will post v2, flowtable->name is also released inmediately, this needs to happen after rcu grace period.