Skip to contentSkip to conversation

netfilter: nf_tables: always synchronize with readers before releasing tables

PATCH In review Alexander Atanasov 2 participants 7 messages Feb 27, 2023 – Feb 28, 2023 2 messages not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 netfilter: nf_tables: always synchronize with readers before releasing tables

general protection fault, probably for non-canonical
address 0xdead000000000115: 0000 [#1] PREEMPT SMP NOPTI
RIP: 0010:__nf_tables_dump_rules+0x10d/0x170 [nf_tables]

__nf_tables_dump_rules runs under rcu_read_lock while __nft_release_table is called from nf_tables_exit_net. commit_mutex is held inside nf_tables_exit_net but this is not enough to guard against lockless readers. When __nft_release_table does list_del(&rule->list) next ptr is poisoned and it crashes while walking the list.

Before calling __nft_release_tables all lockless readers must be done - to ensure this a call to synchronize_rcu() is required.

nf_tables_exit_net does this in case there is something to abort inside __nf_tables_abort but it does not do so otherwise. Fix this by add the missing synchronize_rcu() call before calling __nft_release_table in the nothing to abort case.

Fixes 6001a930ce03 ("netfilter: nftables: introduce table ownership")
---
 net/netfilter/nf_tables_api.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/netfilter/nf_tables_api.c +6 -0
index d73edbd4eec4..849523ece109 100644
@@ -10333,9 +10333,15 @@ static void __net_exit nf_tables_exit_net(struct net *net)
 	struct nftables_pernet *nft_net = nft_pernet(net);
  	mutex_lock(&nft_net->commit_mutex);
+	/* Need to call synchronize_rcu() to let any active rcu lockless
+	 * readers to finish. __nf_tables_abort does this internaly so
+	 * only call it here if there is nothing to abort.
+	 */
 	if (!list_empty(&nft_net->commit_list) ||
 	    !list_empty(&nft_net->module_list))
 		__nf_tables_abort(net, NFNL_ABORT_NONE);
+	else
+		synchronize_rcu();
 	__nft_release_tables(net);
 	mutex_unlock(&nft_net->commit_mutex);
 	WARN_ON_ONCE(!list_empty(&nft_net->tables));
Signature · 1 line
2.31.1
maintainer
#2 lore raw
Quoted 31 lines · Alexander Atanasov
general protection fault, probably for non-canonical
address 0xdead000000000115: 0000 [#1] PREEMPT SMP NOPTI
RIP: 0010:__nf_tables_dump_rules+0x10d/0x170 [nf_tables]

__nf_tables_dump_rules runs under rcu_read_lock while __nft_release_table is called from nf_tables_exit_net. commit_mutex is held inside nf_tables_exit_net but this is not enough to guard against lockless readers. When __nft_release_table does list_del(&rule->list) next ptr is poisoned and it crashes while walking the list.

Before calling __nft_release_tables all lockless readers must be done - to ensure this a call to synchronize_rcu() is required.

nf_tables_exit_net does this in case there is something to abort inside __nf_tables_abort but it does not do so otherwise. Fix this by add the missing synchronize_rcu() call before calling __nft_release_table in the nothing to abort case.

Fixes 6001a930ce03 ("netfilter: nftables: introduce table ownership")
---
 net/netfilter/nf_tables_api.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/netfilter/nf_tables_api.c +0 -0
index d73edbd4eec4..849523ece109 100644
@@ -10333,9 +10333,15 @@ static void __net_exit nf_tables_exit_net(struct
 net *net)
 	struct nftables_pernet *nft_net = nft_pernet(net);

net/netfilter/nf_tables_api.c static void __net_exit nf_tables_exit_net(struct · line 10344

Quoted 10 lines
net/netfilter/nf_tables_api.c +6 -0
  	mutex_lock(&nft_net->commit_mutex);
+	/* Need to call synchronize_rcu() to let any active rcu lockless
+	 * readers to finish. __nf_tables_abort does this internaly so
+	 * only call it here if there is nothing to abort.
+	 */
 	if (!list_empty(&nft_net->commit_list) ||
 	    !list_empty(&nft_net->module_list))
 		__nf_tables_abort(net, NFNL_ABORT_NONE);
+	else
+		synchronize_rcu();

Wouldn't it be better to just drop those list_empty() checks? AFAICS __nf_tables_abort will DTRT in that case.

You can still add a comment like the one you added above to make it clear that we also need to wait for those readers to finish.

Lastly, that list_del() in __nft_release_basechain should probably be list_del_rcu()?

#3 lore raw

Hello,

Quoted 37 lines · Florian Westphal
Quoted 36 lines · Alexander Atanasov
general protection fault, probably for non-canonical
address 0xdead000000000115: 0000 [#1] PREEMPT SMP NOPTI
RIP: 0010:__nf_tables_dump_rules+0x10d/0x170 [nf_tables]

__nf_tables_dump_rules runs under rcu_read_lock while __nft_release_table is called from nf_tables_exit_net. commit_mutex is held inside nf_tables_exit_net but this is not enough to guard against lockless readers. When __nft_release_table does list_del(&rule->list) next ptr is poisoned and it crashes while walking the list.

Before calling __nft_release_tables all lockless readers must be done - to ensure this a call to synchronize_rcu() is required.

nf_tables_exit_net does this in case there is something to abort inside __nf_tables_abort but it does not do so otherwise. Fix this by add the missing synchronize_rcu() call before calling __nft_release_table in the nothing to abort case.

Fixes 6001a930ce03 ("netfilter: nftables: introduce table ownership")
---
  net/netfilter/nf_tables_api.c | 6 ++++++
  1 file changed, 6 insertions(+)
net/netfilter/nf_tables_api.c +4 -0
index d73edbd4eec4..849523ece109 100644
@@ -10333,9 +10333,15 @@ static void __net_exit nf_tables_exit_net(struct
 net *net)
  	struct nftables_pernet *nft_net = nft_pernet(net);
   	mutex_lock(&nft_net->commit_mutex);
+	/* Need to call synchronize_rcu() to let any active rcu lockless
+	 * readers to finish. __nf_tables_abort does this internaly so
+	 * only call it here if there is nothing to abort.
+	 */
Quoted 8 lines

net/netfilter/nf_tables_api.c static void __net_exit nf_tables_exit_net(struct net *net) · line 10343

Quoted 5 lines
net/netfilter/nf_tables_api.c +2 -0
  	if (!list_empty(&nft_net->commit_list) ||
  	    !list_empty(&nft_net->module_list))
  		__nf_tables_abort(net, NFNL_ABORT_NONE);
+	else
+		synchronize_rcu();

Wouldn't it be better to just drop those list_empty() checks? AFAICS __nf_tables_abort will DTRT in that case.

Ok, i will drop the checks.

Quoted 2 lines

You can still add a comment like the one you added above to make it clear that we also need to wait for those readers to finish.

Ok.

Quoted 2 lines

Lastly, that list_del() in __nft_release_basechain should probably be list_del_rcu()?

I am still in process of untwisting that place but so far.
Simple change to list_del_rcu wouldn't help as it wouldn't in __nft_release_table:

list_del(&rule->list);
ctx->chain->use--;

nf_tables_rule_release(ctx, rule) {
	nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
	nf_tables_rule_destroy(ctx, rule) {
		kfree(rule); <-- freed here
	}
}

List traversal would work but instead of crash it would become use after free.
Adding synchronize_rcu() before list iterattion there will probably do, it is already under commit_mutex when called from nf_tables_netdev_event.

Signature · 2 lines
Regards,
Alexander Atanasov

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 22 lines · Florian Westphal
Quoted 16 lines · Alexander Atanasov
Quoted 2 lines

Lastly, that list_del() in __nft_release_basechain should probably be list_del_rcu()?

I am still in process of untwisting that place but so far. Simple change to list_del_rcu wouldn't help as it wouldn't in __nft_release_table:

list_del(&rule->list);
ctx->chain->use--;

nf_tables_rule_release(ctx, rule) {
	nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
	nf_tables_rule_destroy(ctx, rule) {
		kfree(rule); <-- freed here
	}
}
Quoted 4 lines

List traversal would work but instead of crash it would become use after free.
Adding synchronize_rcu() before list iterattion there will probably do, it is already under commit_mutex when called from nf_tables_netdev_event.

Quoted 4 lines

Hmm, please wait. I have to look at this in more detail. I don't see a race conditon in the first place.

netns dismantling already does synchronize_rcu(), so I don't see how we > can have this uaf in the first place.

As i said i am still trying to figure out the basechain place, where is that synchronize_rcu() call done?

Quoted 2 lines

Do you see this with current kernels or did the splat happen with an older version?

It's with a bit older kernel but there is no significant difference wrt nf_tables_api code.
I will prepare a more detailed report for you. Unfortunately there is no reproducer.

Signature · 2 lines
Regards,
Alexander Atanasov
maintainer
#6 lore raw
Quoted 2 lines · Alexander Atanasov

As i said i am still trying to figure out the basechain place, where is that synchronize_rcu() call done?

cleanup_net() in net/core/net_namespace.c.

pre_exit handlers run, then synchronize_rcu, then the normal exit handlers, then exit_batch.

Quoted 6 lines
Quoted 2 lines

Do you see this with current kernels or did the splat happen with an older version?

It's with a bit older kernel but there is no significant difference wrt nf_tables_api code.
I will prepare a more detailed report for you.

Thanks.

#7 lore raw
Quoted 8 lines · Florian Westphal
Quoted 2 lines · Alexander Atanasov

As i said i am still trying to figure out the basechain place, where is that synchronize_rcu() call done?

cleanup_net() in net/core/net_namespace.c.

pre_exit handlers run, then synchronize_rcu, then the normal exit handlers, then exit_batch.

It prevents anyone new to find the namespace but it does not guard against the ones that have already found it. What stops them to enter a rcu_read_lock() section after the synchronize call in cleanup_net() is done and race with the exit handler?

synchronize_rcu() must be called with the commit_mutex held to be safe against lock less readers using data protected with commit_mutext.

Signature · 2 lines
Regards,
Alexander Atanasov
maintainer
#8 lore raw
Quoted 5 lines · Alexander Atanasov
Quoted 4 lines · Florian Westphal
Quoted 2 lines · Alexander Atanasov

As i said i am still trying to figure out the basechain place, where is that synchronize_rcu() call done?

Quoted 7 lines
Quoted 4 lines

cleanup_net() in net/core/net_namespace.c.

pre_exit handlers run, then synchronize_rcu, then the normal exit handlers, then exit_batch.

It prevents anyone new to find the namespace but it does not guard against the ones that have already found it.

The netns is being dismantled, how can there be any process left?

Quoted 2 lines

What stops them to enter a rcu_read_lock() section after the synchronize call in cleanup_net() is done and race with the exit handler?

There should be no task in the first place.

Quoted 2 lines

synchronize_rcu() must be called with the commit_mutex held to be safe against lock less readers using data protected with commit_mutext.

Sorry, I do not understand this bug nor the fix.