Skip to content

netfilter: xtables: fix module load and teardown races

Florian Westphal netfilter-devel 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest8 patches 1 replyIn review
  2. v28 patches 0 repliesSuperseded
  3. v15 patches 2 repliesSuperseded

Changes in v3 from the cover letter

- sashiko spotted a wrong error unwind in iptable_nat.c in last patch.
- alter Reported-by tag for Tristan Madani as requested
- no other changes.

1) Allow initial x_tables table replacement without emitting an audit log message. Delay the register message until after hooks are wired up to avoid unnecessary unregister logs during error unwinding.

2) Fix a NULL dereference by allocating hook ops before adding the table to the per-netns list. Use synchronize_rcu() during error unwinding to ensure the table stops processing packets before teardown. Defer audit log register message until all operations succeed.

3) Refactor xtables to use a single xt_unregister_table_pre_exit function. Eliminate code duplication by centralizing table unregistration logic within the xtables core. ebtables cannot be changed due to incompatibility.

4) Unregister xtables templates before module removal. This prevents a race condition where userspace instantiates a new table after the pernet unreg removed the current table.

5) Add xtables_unregister_table_exit to fully unregister netfilter tables during module removal. Unlink the table from dying lists, then free hook operations.

6) Implement a two-stage removal scheme for ebtables following the x_tables pattern. Assign table->ops while holding the ebt mutex to prevent exposing partially-filled structures.

7) Fix ebtables module initialization race. Register the template last in table initialization functions. Prevent table instantiation before pernet operations are available.

8) Fix a race condition in x_tables module initialization. Ensure pernet ops are fully set up before exposing the table to userspace.

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/8netfilter: x_tables: allow initial table replace without emitting audit log messagenet/netfilter/x_tables.c+20 -90 replies
2/8netfilter: xtables: allocate hook ops while under mutexinclude/linux/netfilter/x_tables.h net/ipv4/netfilter/arp_tables.c net/ipv4/netfilter/ip_tables.c +2 more+55 -1100 replies
3/8netfilter: x_tables: add and use xt_unregister_table_pre_exitinclude/linux/netfilter/x_tables.h include/linux/netfilter_arp/arp_tables.h include/linux/netfilter_ipv4/ip_tables.h +16 more+41 -390 replies
4/8netfilter: x_tables: unregister the templates firstnet/ipv4/netfilter/arptable_filter.c net/ipv4/netfilter/iptable_filter.c net/ipv4/netfilter/iptable_mangle.c +6 more+9 -90 replies
5/8netfilter: x_tables: add and use xtables_unregister_table_exitinclude/linux/netfilter/x_tables.h net/ipv4/netfilter/arp_tables.c net/ipv4/netfilter/ip_tables.c +4 more+83 -370 replies
6/8netfilter: ebtables: move to two-stage removal schemenet/bridge/netfilter/ebtable_broute.c net/bridge/netfilter/ebtable_filter.c net/bridge/netfilter/ebtable_nat.c +1 more+40 -260 replies
7/8netfilter: ebtables: close dangling table module init racenet/bridge/netfilter/ebtable_broute.c net/bridge/netfilter/ebtable_filter.c net/bridge/netfilter/ebtable_nat.c+14 -200 replies
8/8netfilter: x_tables: close dangling table module init racenet/ipv4/netfilter/arptable_filter.c net/ipv4/netfilter/iptable_filter.c net/ipv4/netfilter/iptable_mangle.c +6 more+105 -990 replies

Reviews trailers per patch

5 of 8 patches have a Reviewed-by or Acked-by.

PatchTristan Madani
1/8
2/8
3/8R-b
4/8R-b
5/8R-b
6/8R-b
7/8R-b
8/8

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH v3 nf 0/8] netfilter: xtables: fix module load and teardown races

v3:
- sashiko spotted a wrong error unwind in iptable_nat.c in last patch.
- alter Reported-by tag for Tristan Madani as requested
- no other changes.

1) Allow initial x_tables table replacement without emitting an audit log message. Delay the register message until after hooks are wired up to avoid unnecessary unregister logs during error unwinding.

2) Fix a NULL dereference by allocating hook ops before adding the table to the per-netns list. Use synchronize_rcu() during error unwinding to ensure the table stops processing packets before teardown. Defer audit log register message until all operations succeed.

3) Refactor xtables to use a single xt_unregister_table_pre_exit function. Eliminate code duplication by centralizing table unregistration logic within the xtables core. ebtables cannot be changed due to incompatibility.

4) Unregister xtables templates before module removal. This prevents a race condition where userspace instantiates a new table after the pernet unreg removed the current table.

5) Add xtables_unregister_table_exit to fully unregister netfilter tables during module removal. Unlink the table from dying lists, then free hook operations.

6) Implement a two-stage removal scheme for ebtables following the x_tables pattern. Assign table->ops while holding the ebt mutex to prevent exposing partially-filled structures.

7) Fix ebtables module initialization race. Register the template last in table initialization functions. Prevent table instantiation before pernet operations are available.

8) Fix a race condition in x_tables module initialization. Ensure pernet ops are fully set up before exposing the table to userspace.

Florian Westphal (8):
netfilter: x_tables: allow initial table replace without emitting
audit log message
netfilter: xtables: allocate hook ops while under mutex
netfilter: x_tables: add and use xt_unregister_table_pre_exit
netfilter: x_tables: unregister the templates first
netfilter: x_tables: add and use xtables_unregister_table_exit
netfilter: ebtables: move to two-stage removal scheme
netfilter: ebtables: close dangling table module init race
netfilter: x_tables: close dangling table module init race

 include/linux/netfilter/x_tables.h        |   4 +-
 include/linux/netfilter_arp/arp_tables.h  |   1 -
 include/linux/netfilter_ipv4/ip_tables.h  |   1 -
 include/linux/netfilter_ipv6/ip6_tables.h |   1 -
 net/bridge/netfilter/ebtable_broute.c     |  14 +-
 net/bridge/netfilter/ebtable_filter.c     |  14 +-
 net/bridge/netfilter/ebtable_nat.c        |  12 +-
 net/bridge/netfilter/ebtables.c           |  60 +++++---
 net/ipv4/netfilter/arp_tables.c           |  53 +------
 net/ipv4/netfilter/arptable_filter.c      |  27 ++--
 net/ipv4/netfilter/ip_tables.c            |  59 +-------
 net/ipv4/netfilter/iptable_filter.c       |  27 ++--
 net/ipv4/netfilter/iptable_mangle.c       |  29 ++--
 net/ipv4/netfilter/iptable_nat.c          |   6 +-
 net/ipv4/netfilter/iptable_raw.c          |  26 ++--
 net/ipv4/netfilter/iptable_security.c     |  27 ++--
 net/ipv6/netfilter/ip6_tables.c           |  56 +------
 net/ipv6/netfilter/ip6table_filter.c      |  26 ++--
 net/ipv6/netfilter/ip6table_mangle.c      |  27 ++--
 net/ipv6/netfilter/ip6table_nat.c         |   6 +-
 net/ipv6/netfilter/ip6table_raw.c         |  24 +--
 net/ipv6/netfilter/ip6table_security.c    |  27 ++--
 net/netfilter/x_tables.c                  | 177 ++++++++++++++++++----
 23 files changed, 361 insertions(+), 343 deletions(-)
Signature · 1 line
2.53.0