netfilter: nftables: accept all dummy chain when table is dormant
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v2 1/1 netfilter: nftables: accept all dummy chain when table is dormant
The dormant flag need to be updated from the preparation phase, otherwise, two consecutive requests to dorm a table in the same batch might try to remove the same hooks twice, resulting in the following warning:
hook not found, pf 3 num 0
WARNING: CPU: 0 PID: 334 at net/netfilter/core.c:480 __nf_unregister_net_hook+0x1eb/0x610 net/netfilter/core.c:480 Modules linked in: CPU: 0 PID: 334 Comm: kworker/u4:5 Not tainted 5.12.0-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Workqueue: netns cleanup_net RIP: 0010:__nf_unregister_net_hook+0x1eb/0x610 net/netfilter/core.c:480
This patch is a partial revert of 0ce7cf4127f1 ("netfilter: nftables:
update table flags from the commit phase") to restore the previous behaviour, which updates the dormant flag from the preparation phase to address this issue.
However, there is still another problem: A batch containing a series of dorm-wakeup-dorm table and vice-versa also trigger the warning above since hook unregistration happens from the preparation phase, while hook registration occurs from the commit phase.
To fix this problem, this patch adds a dummy accept-all-rule that is used in case the table enters dormant state. This patch removes nf_tables_table_enable() and nf_tables_table_disable() since chain hooks are always registered, therefore, they are not needed anymore.
The new __NFT_TABLE_F_UPDATE internal flag is used to signal the commit phase that the dormant flag has been updated.
---
v2: untoggle __NFT_TABLE_F_UPDATE from the commit transaction loop instead of
doing this right after nf_tables_commit_chain_prepare().
include/net/netfilter/nf_tables.h | 3 - net/netfilter/nf_tables_api.c | 148 +++++++++++++++--------------- 2 files changed, 75 insertions(+), 76 deletions(-)
include/net/netfilter/nf_tables.h +0 -3
@@ -1507,15 +1507,12 @@ struct nft_trans_chain {struct nft_trans_table {bool update;u8 state; -u32 flags; };#define nft_trans_table_update (trans) \ (((struct nft_trans_table *)trans->data)->update)#define nft_trans_table_state (trans) \ (((struct nft_trans_table *)trans->data)->state) -#define nft_trans_table_flags (trans) \ - (((struct nft_trans_table *)trans->data)->flags)struct nft_trans_elem {struct nft_set *set;
net/netfilter/nf_tables_api.c +75 -73
258 lines not on this page · raw message
Signature · 1 line
2.30.2
Florian Westphal
maintainerQuoted 18 lines · Pablo Neira Ayuso
The dormant flag need to be updated from the preparation phase, otherwise, two consecutive requests to dorm a table in the same batch might try to remove the same hooks twice, resulting in the following warning:
hook not found, pf 3 num 0
WARNING: CPU: 0 PID: 334 at net/netfilter/core.c:480 __nf_unregister_net_hook+0x1eb/0x610 net/netfilter/core.c:480 Modules linked in: CPU: 0 PID: 334 Comm: kworker/u4:5 Not tainted 5.12.0-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Workqueue: netns cleanup_net RIP: 0010:__nf_unregister_net_hook+0x1eb/0x610 net/netfilter/core.c:480This patch is a partial revert of
0ce7cf4127f1("netfilter: nftables:
update table flags from the commit phase") to restore the previous behaviour, which updates the dormant flag from the preparation phase to address this issue.
Quoted 4 lines
However, there is still another problem: A batch containing a series of dorm-wakeup-dorm table and vice-versa also trigger the warning above since hook unregistration happens from the preparation phase, while hook registration occurs from the commit phase.
You could add nf_unregister_net_hook_try() or somesuch that elides the WARN().
AFAIU this would not be needed at all if the WARN would not exist. We could also just remove the WARN but it did catch the earlier NFPROTO_ARP bug, so I would refer to keep it.
Pablo Neira Ayuso
maintainerQuoted 19 lines · Florian Westphal
Quoted 18 lines · Pablo Neira Ayuso
The dormant flag need to be updated from the preparation phase, otherwise, two consecutive requests to dorm a table in the same batch might try to remove the same hooks twice, resulting in the following warning:
hook not found, pf 3 num 0
WARNING: CPU: 0 PID: 334 at net/netfilter/core.c:480 __nf_unregister_net_hook+0x1eb/0x610 net/netfilter/core.c:480 Modules linked in: CPU: 0 PID: 334 Comm: kworker/u4:5 Not tainted 5.12.0-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Workqueue: netns cleanup_net RIP: 0010:__nf_unregister_net_hook+0x1eb/0x610 net/netfilter/core.c:480This patch is a partial revert of
0ce7cf4127f1("netfilter: nftables:
update table flags from the commit phase") to restore the previous behaviour, which updates the dormant flag from the preparation phase to address this issue.
Quoted 7 lines
Quoted 4 lines
However, there is still another problem: A batch containing a series of dorm-wakeup-dorm table and vice-versa also trigger the warning above since hook unregistration happens from the preparation phase, while hook registration occurs from the commit phase.
You could add
nf_unregister_net_hook_try()or somesuch that elides theWARN().
Let me elaborate a bit more the problem here.
The hook registration happens from the preparation phase (because it might fail for several reasons) and hook unregistration happens from the commit phase (because registration from the abort phase is not possible, since registration might fail). Hence, nf_register_net_hook() cannot be called from the commit and abort phases, only from the preparation phase.
I'm assuming that looking at dormant flags from commit/abort phase to decide what to do.
Let's have a look a several possible scenarios:
Scenario A) batch containing two commands: dorm + wake up
From preparation phase.
- dorm: preparation phase sets the dormant flag (hooks are still registered).
- wake up: unset the dormant flag. This needs to skip hook registration, because they are already registered. (it needs a way to check that hooks are registered).
From commit phase.
- dorm: dormant flag is unset, no-op.
- wake-up: dormant flag is unset, no-op.
From abort phase (reversed), it undoes preparation phase.
- wake-up: set the dormant flag, unregister hooks.
- dorm: unset the dormant flag, register hooks (not possible)
Problems: Needs a function to check if hooks are present.
abort phase needs to register hooks.
Scenario B) batch containing two commands: wake up + dorm
From preparation phase.
- wake up: unset the dormant flag. This needs to register hooks.
- dorm: preparation phase sets the dormant flag (hooks are still registered).
From commit phase.
- wake-up: dormant flag is set, unregister hooks.
- dorm: dormant flag is set, unregister hooks (again).
From abort phase (reversed), it undoes preparation phase.
- dorm: unset the dormant flag, register hooks (not possible)
- wake-up: set dormant flag, unregister hooks.
Problems: commit phase needs try_unregister hook function.
abort phase needs to unregister hooks.
I can also describe dorm + wake up + dorm, and wake up + dorm + wake up scenarios.
I also tried looking at the transaction state instead of the dormant flags, similar problems.
I also tried adding more internal flags to annotate context. I looked at adding fields to nft_table to count for the number of pending hook registration / unregistration. It's all tricky.
The patch I posted is addressing the issue by skipping hook registration / unregistration for dormant flags updates.
What's your concern with the approach I'm proposing?
Florian Westphal
maintainerQuoted 25 lines · Pablo Neira Ayuso
Quoted 24 lines · Florian Westphal
Quoted 22 lines · Pablo Neira Ayuso
The dormant flag need to be updated from the preparation phase, otherwise, two consecutive requests to dorm a table in the same batch might try to remove the same hooks twice, resulting in the following warning:
hook not found, pf 3 num 0
WARNING: CPU: 0 PID: 334 at net/netfilter/core.c:480 __nf_unregister_net_hook+0x1eb/0x610 net/netfilter/core.c:480 Modules linked in: CPU: 0 PID: 334 Comm: kworker/u4:5 Not tainted 5.12.0-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Workqueue: netns cleanup_net RIP: 0010:__nf_unregister_net_hook+0x1eb/0x610 net/netfilter/core.c:480This patch is a partial revert of
0ce7cf4127f1("netfilter: nftables:
update table flags from the commit phase") to restore the previous behaviour, which updates the dormant flag from the preparation phase to address this issue.However, there is still another problem: A batch containing a series of dorm-wakeup-dorm table and vice-versa also trigger the warning above since hook unregistration happens from the preparation phase, while hook registration occurs from the commit phase.
Quoted 4 lines
Quoted 2 lines
You could add
nf_unregister_net_hook_try()or somesuch that elides theWARN().Let me elaborate a bit more the problem here.
Thanks, that makes it much clearer.
Quoted 27 lines
The hook registration happens from the preparation phase (because it might fail for several reasons) and hook unregistration happens from the commit phase (because registration from the abort phase is not possible, since registration might fail). Hence,
nf_register_net_hook()cannot be called from the commit and abort phases, only from the preparation phase.I'm assuming that looking at dormant flags from commit/abort phase to decide what to do.
Let's have a look a several possible scenarios:
Scenario A) batch containing two commands: dorm + wake up
From preparation phase.
- dorm: preparation phase sets the dormant flag (hooks are still registered).
- wake up: unset the dormant flag. This needs to skip hook registration, because they are already registered. (it needs a way to check that hooks are registered).From commit phase.
- dorm: dormant flag is unset, no-op.
- wake-up: dormant flag is unset, no-op.
Quoted 7 lines
From abort phase (reversed), it undoes preparation phase.
- wake-up: set the dormant flag, unregister hooks.
- dorm: unset the dormant flag, register hooks (not possible)Problems: Needs a function to check if hooks are present.
abort phase needs to register hooks.
I agree that abort and/or commit phases cannot register hooks.
Quoted 13 lines
Scenario B) batch containing two commands: wake up + dorm
From preparation phase.
- wake up: unset the dormant flag. This needs to register hooks.
- dorm: preparation phase sets the dormant flag (hooks are still registered).From commit phase.
- wake-up: dormant flag is set, unregister hooks.
- dorm: dormant flag is set, unregister hooks (again).
Quoted 7 lines
From abort phase (reversed), it undoes preparation phase.
- dorm: unset the dormant flag, register hooks (not possible)
- wake-up: set dormant flag, unregister hooks.Problems: commit phase needs
try_unregisterhook function.
abort phase needs to unregister hooks.
... but that is doable in the sense that unregister can't fail.
Quoted 6 lines
I also tried looking at the transaction state instead of the dormant flags, similar problems.
I also tried adding more internal flags to annotate context. I looked at adding fields to
nft_tableto count for the number of pending hook registration / unregistration. It's all tricky.
Ok, too bad.
Quoted 4 lines
The patch I posted is addressing the issue by skipping hook registration / unregistration for dormant flags updates.
What's your concern with the approach I'm proposing?
No concern, I did not understand the problem with hook register in abort/commit.
I also dislike that dormat tables now retrain the hook overhead, but I guess dormat tables are an exception and not the norm, so maybe unfounded concern.
Pablo Neira Ayuso
maintainerQuoted 1 line · Florian Westphal
Pablo Neira Ayuso <[email protected]> wrote:
[...]
Quoted 42 lines
Quoted 17 lines
Let's have a look a several possible scenarios:
Scenario A) batch containing two commands: dorm + wake up
From preparation phase.
- dorm: preparation phase sets the dormant flag (hooks are still registered).
- wake up: unset the dormant flag. This needs to skip hook registration, because they are already registered. (it needs a way to check that hooks are registered).From commit phase.
- dorm: dormant flag is unset, no-op.
- wake-up: dormant flag is unset, no-op.Quoted 7 lines
From abort phase (reversed), it undoes preparation phase.
- wake-up: set the dormant flag, unregister hooks.
- dorm: unset the dormant flag, register hooks (not possible)Problems: Needs a function to check if hooks are present.
abort phase needs to register hooks.I agree that abort and/or commit phases cannot register hooks.
Quoted 15 lines
Scenario B) batch containing two commands: wake up + dorm
From preparation phase.
- wake up: unset the dormant flag. This needs to register hooks.
- dorm: preparation phase sets the dormant flag (hooks are still registered).From commit phase.
- wake-up: dormant flag is set, unregister hooks.
- dorm: dormant flag is set, unregister hooks (again).From abort phase (reversed), it undoes preparation phase.
Quoted 7 lines
Quoted 5 lines
- dorm: unset the dormant flag, register hooks (not possible)
- wake-up: set dormant flag, unregister hooks.Problems: commit phase needs
try_unregisterhook function.
abort phase needs to unregister hooks.... but that is doable in the sense that unregister can't fail.
Right, but adding "unregister hooks" to the abort path breaks a different scenario. This might unregister a hook that, because of a later wake-up action, needs to stay there, because you cannot call register a hook from the abort path, it's a bit of a whac-a-mole game.
Quoted 12 lines
Quoted 6 lines
I also tried looking at the transaction state instead of the dormant flags, similar problems.
I also tried adding more internal flags to annotate context. I looked at adding fields to
nft_tableto count for the number of pending hook registration / unregistration. It's all tricky.Ok, too bad.
Quoted 3 lines
The patch I posted is addressing the issue by skipping hook registration / unregistration for dormant flags updates.
Quoted 8 lines
Quoted 1 line
What's your concern with the approach I'm proposing?
No concern, I did not understand the problem with hook register in abort/commit.
I also dislike that dormat tables now retrain the hook overhead, but I guess dormat tables are an exception and not the norm, so maybe unfounded concern.
You are right that this approach incurs in the hook evaluation penalty from the packet path. But I don't think there's a need to optimize this feature at this stage. If it turns out that this needs to be optimized, maybe it should be possible to add a core feature to disable hook while leaving in registered (ie. hook "dormant" state).
So I'm just inclined to keep it simple while making sure that any possible (silly) robot-generated sequence with this toggle works fine.
Florian Westphal
maintainerQuoted 7 lines · Pablo Neira Ayuso
Quoted 1 line · Florian Westphal
... but that is doable in the sense that unregister can't fail.
Right, but adding "unregister hooks" to the abort path breaks a different scenario. This might unregister a hook that, because of a later wake-up action, needs to stay there, because you cannot call register a hook from the abort path, it's a bit of a whac-a-mole game.
Argh, indeed. We'd have to re-scan the transaction log during preparation phase for each dormant on/off and chain add/delete to see if the action un-does an earlier pending one, then remove both if they cancel each other.
Quoted 6 lines
Quoted 2 lines
I guess dormat tables are an exception and not the norm, so maybe unfounded concern.
You are right that this approach incurs in the hook evaluation penalty from the packet path. But I don't think there's a need to optimize this feature at this stage
Ok, I dislike optimizing too early as well.
Quoted 2 lines
So I'm just inclined to keep it simple while making sure that any possible (silly) robot-generated sequence with this toggle works fine.
Ok, lets use your approach then.