Skip to contentSkip to conversation

netfilter: nf_tables: free flowtable hooks on hook register error

PATCH Applied Florian Westphal 2 participants 5 messages Mar 2, 2020 – Mar 5, 2020

Subsystems (3)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nf_tables: free flowtable hooks on hook register error

If hook registration fails, the hooks allocated via nft_netdev_hook_alloc need to be freed.

We can't change the goto label to 'goto 5' -- while it does fix the memleak it does cause a warning splat from the netfilter core (the hooks were not registered).

Fixes 3f0465a9ef02 ("netfilter: nf_tables: dynamically allocate hooks per net_device in flowtables")
Reported-by [email protected]
---
 net/netfilter/nf_tables_api.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +6 -1
index d1318bdf49ca..bb064aa4154b 100644
@@ -6300,8 +6300,13 @@ static int nf_tables_newflowtable(struct net *net, struct sock *nlsk,
 		goto err4;
 
 	err = nft_register_flowtable_net_hooks(ctx.net, table, flowtable);
-	if (err < 0)
+	if (err < 0) {
+		list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
+			list_del_rcu(&hook->list);
+			kfree_rcu(hook, rcu);
+		}
 		goto err4;
+	}
 
 	err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
 	if (err < 0)
Signature · 1 line
2.24.1
maintainer Applied
#1 lore raw
Quoted 6 lines · Florian Westphal

If hook registration fails, the hooks allocated via nft_netdev_hook_alloc need to be freed.

We can't change the goto label to 'goto 5' -- while it does fix the memleak it does cause a warning splat from the netfilter core (the hooks were not registered).

Applied, thanks.

#2 lore raw
In reply to Florian Westphal
Quoted 6 lines · Florian Westphal

If hook registration fails, the hooks allocated via nft_netdev_hook_alloc need to be freed.

We can't change the goto label to 'goto 5' -- while it does fix the memleak it does cause a warning splat from the netfilter core (the hooks were not registered).

It seems test/shell crashes after this, looking. It works after reverting.

Quoted 29 lines
Fixes 3f0465a9ef02 ("netfilter: nf_tables: dynamically allocate hooks per net_device in flowtables")
Reported-by [email protected]
---
 net/netfilter/nf_tables_api.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +6 -1
index d1318bdf49ca..bb064aa4154b 100644
@@ -6300,8 +6300,13 @@ static int nf_tables_newflowtable(struct net *net, struct sock *nlsk,
 		goto err4;
 
 	err = nft_register_flowtable_net_hooks(ctx.net, table, flowtable);
-	if (err < 0)
+	if (err < 0) {
+		list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
+			list_del_rcu(&hook->list);
+			kfree_rcu(hook, rcu);
+		}
 		goto err4;
+	}
 
 	err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
 	if (err < 0)
Signature · 1 line
2.24.1
author maintainer
#3 lore raw
Quoted 10 lines · Pablo Neira Ayuso
Quoted 6 lines · Florian Westphal

If hook registration fails, the hooks allocated via nft_netdev_hook_alloc need to be freed.

We can't change the goto label to 'goto 5' -- while it does fix the memleak it does cause a warning splat from the netfilter core (the hooks were not registered).

It seems test/shell crashes after this, looking. It works after reverting.

Sorry about that. Let me know if you want me to look into this.

#4 lore raw
In reply to Pablo Neira Ayuso
Quoted 10 lines · Pablo Neira Ayuso
Quoted 6 lines · Florian Westphal

If hook registration fails, the hooks allocated via nft_netdev_hook_alloc need to be freed.

We can't change the goto label to 'goto 5' -- while it does fix the memleak it does cause a warning splat from the netfilter core (the hooks were not registered).

It seems test/shell crashes after this, looking. It works after reverting.

Actually, no problem problem is not related to this patch. Sorry for the noise.