Skip to content

net/ipfilter: Lock the filter chains against IPT_SO_SET_REPLACE. - #22481

Open
casaroli wants to merge 3 commits into
apache:masterfrom
casaroli:ipfilter-replace-lock-up
Open

casaroli wants to merge 3 commits into
apache:masterfrom
casaroli:ipfilter-replace-lock-up

Conversation

@casaroli

@casaroli casaroli commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

IPT_SO_SET_REPLACE and IP6T_SO_SET_REPLACE run under the lock of their own socket only (conn_lock() in ipv4_setsockopt() and ipv6_setsockopt()). The replace frees the entries of each filter chain (ipfilter_cfg_clear()) and then adds the new entries one by one. At the same time the packet path walks the chain in ipv4_filter_match() / ipv6_filter_match() under the lock of its own device only. So the packet path can read an entry after it was freed, and it can see a chain that is empty or only partly built. @cederom found this in the review of #22367: #22367 (comment)

This PR:

  • net/ipfilter: adds g_ipfilter_lock, a mutex for the filter chains. The packet path holds it while it walks a chain. The conversion in ipt_filter.c now builds the new chains aside with sq_addlast(), and the new ipfilter_cfg_replace() swaps all chains of an address family under the lock and frees the old entries after it releases the lock. So a packet sees all the old chains or all the new chains. ipfilter_cfg_clear() and ipfilter_cfg_add() are removed (their only caller was the conversion).
  • net/netfilter: two sockets can also run the iptables socket options at the same time. replace_entries() swaps table->repl and frees the old table while get_info() / get_entries() of another socket can still read it, or another replace can free the same table. A mutex for the IPv4 tables and one for the IPv6 tables now serialize ipt_setsockopt() / ipt_getsockopt() and the ip6t functions.
  • net/netfilter: a first commit adds the blank lines that nxstyle asks for in ip6t_sockopt.c (no code change; the CI checks the whole file).

Lock order (no path takes these locks in the other order):

  • packet path: device lock -> g_ipfilter_lock
  • setsockopt/getsockopt: socket lock -> g_ipt_lock / g_ip6t_lock -> netdev list lock, NAT lock (NAT table) or g_ipfilter_lock

g_ipfilter_lock is a leaf: no other lock is taken and nothing is allocated, freed or sent while it is held. The REJECT reply (icmp_reply()) is sent after the lock is released.

Impact

  • Only with CONFIG_NET_IPFILTER / CONFIG_NET_IPTABLES (default n).
  • One mutex lock and unlock for each packet that goes through a filter chain. Packets of different devices no longer walk a chain at the same time.
  • No API change outside net/: ipfilter_cfg_replace() and the removed ipfilter_cfg_add() / ipfilter_cfg_clear() are internal to the network stack (net/ipfilter/ipfilter.h).
  • Common network code: the same in flat, protected and kernel builds and on every architecture.

Testing

Hosts: macOS arm64 (Arm GNU 13.2.Rel1, QEMU 11.1 TCG, cortex-a53, 1 CPU) for the QEMU runs; Fedora x86_64 with the NuttX CI image (aarch64-none-elf-gcc 13.2.1, riscv-none-elf-gcc 14.3.0) for the CI-image builds. All builds use -Werror. The branch is on master f9d3eb1 (with #22367) and the tests use nuttx-apps master 0a37af9866b. The race test and the kernel runs are from the first version of this PR on master 0f1701d (before #22367); the rebase did not change the code of this PR. The configs add the loopback, IPv4, IPv6, ICMP/ICMPv6 sockets, NET_IPFILTER, NET_IPTABLES, iptables, ip6tables, ping, ping6 and ostest.

The race, with a test-only hook (not part of this PR) on qemu-armv8a:nsh: the IPv4 packet path sleeps 300 ms before each entry of the OUTPUT chain when it walks an ICMP echo request, and a freed filter entry is filled with 0xa5 (and not given back to the heap, so the heap does not reuse it). With 4 rules in OUTPUT, ping -c 1 127.0.0.1 & starts a walk, and iptables -F OUTPUT runs while the walk sleeps:

  • master: the replace frees the entries during the walk. The walk then reads flink 0xa5a5a5a5a5a5a5a5 from a freed entry and stops in a data abort (FAR_ELn: 0xa5a5a5a5a5a5a5a5, task hpwork).
  • this PR: iptables -F OUTPUT waits until the walk ends (1.1 s), then frees the old entries. The ping gets its reply, and the system continues.

Functional runs of this PR on qemu-armv8a:nsh (flat; on both bases) and qemu-armv8a:knsh (kernel, the commands run from /system/bin; first base):

  • iptables -A INPUT -p icmp -j DROP: ping 127.0.0.1 0 of 2; after iptables -F INPUT: 2 of 2. iptables -P INPUT DROP: 0 of 2; -P INPUT ACCEPT: 2 of 2.
  • ip6tables -A INPUT -p icmpv6 -j DROP: ping6 ::1 0 of 2; after ip6tables -F INPUT: 2 of 2.
  • 32 iptables/ip6tables -A and -F commands while a background ping sends 40 echo requests: 40 of 40 replies, iptables -L and ip6tables -L show empty chains at the end.
  • ostest: Exiting with status 0 in the flat and the kernel build.

The TCP flags and rate matches of #22367 with this PR, on qemu-armv8a:nsh (flat) with the iptables options of apache/nuttx-apps#4149, and 2000 SYNs from spoofed sources (200/s for 10 s) written to a TUN device:

  • iptables -A INPUT -p tcp --syn -m limit --limit 150/s --limit-burst 10 -j ACCEPT and iptables -A INPUT -p tcp --syn -j DROP: 1521 of 2000 SYNs pass (as net/ipfilter: Match TCP flags and a packet rate. #22367 alone).
  • iptables -F and the same rules with --limit 50/s: 506 of 2000 pass. So the new rule starts with its own full bucket.
  • iptables -F: 2000 of 2000 pass.

Builds with -Werror in the CI image, all with NET_IPFILTER, NET_IPTABLES and the iptables/ip6tables commands: qemu-armv8a:netnsh with IPv4 + IPv6 + NAT + IP forwarding, rv-virt:netnsh64 with IPv4 only and with IPv6 only (both bases); qemu-armv8a:knsh (kernel, apps imported), rv-virt:netnsh64 with IPv4 + IPv6 and rv-virt:knetnsh64 (kernel, apps imported) (first base). Also built natively for the QEMU runs: qemu-armv8a:nsh and qemu-armv8a:knsh. 0 warnings (rv-virt has the RWX segment linker warning of master).

Size, qemu-armv8a:netnsh with the filter and NAT: text 615456 -> 615768 (+312), data 17538 -> 17610 (+72, the three mutexes). No change without NET_IPFILTER / NET_IPTABLES.

tools/checkpatch.sh -c -u -m -g: pass.

@casaroli
casaroli requested a review from jerpelea as a code owner November 27, 2026 09:34
@github-actions github-actions Bot added Area: Networking Effects networking subsystem Size: M The size of the change in this PR is medium labels Nov 27, 2026
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Comment thread net/ipfilter/ipfilter.c Outdated
*
****************************************************************************/

void ipfilter_cfg_add(FAR struct ipfilter_entry_s *entry,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not remove ipfilter_cfg_add and call sq_addlast directly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Add the blank lines after the declarations that nxstyle asks for.  No
code change.  A next commit changes this file, and the CI runs nxstyle
on the whole file.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
IPT_SO_SET_REPLACE and IP6T_SO_SET_REPLACE run under the lock of their
own socket only.  ipfilter_cfg_clear() freed the entries of a chain, and
ipfilter_cfg_add() then added the new entries one by one.  At the same
time the packet path walks the chain in ipv4_filter_match() or
ipv6_filter_match() under the lock of its own device only.  So the
packet path can read an entry after it was freed, and it can see a
chain that is empty or only partly built.

Add g_ipfilter_lock, a mutex for the chains.  The packet path holds it
while it walks a chain.  The conversion now builds the new chains aside
with sq_addlast(), and the new ipfilter_cfg_replace() swaps all chains
of an address family under the lock and frees the old entries after it
releases the lock.  ipfilter_cfg_clear() and ipfilter_cfg_add() are
removed.

The lock is a leaf: no other lock is taken and nothing is allocated or
freed while it is held.  The order is device lock -> g_ipfilter_lock on
the packet path, and socket lock -> g_ipfilter_lock on the setsockopt()
path.

Reported-by: Tomasz 'CeDeROM' CEDRO <tomek@cedro.info>
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
ipt_setsockopt(), ipt_getsockopt() and the ip6t functions run under the
lock of their own socket only, so two sockets can use them at the same
time.  replace_entries() swaps table->repl and frees the old table,
while get_info() or get_entries() of another socket can still read it,
or another replace_entries() can swap and free the same table.

Add one mutex for the IPv4 tables and one for the IPv6 tables, and hold
it in the setsockopt and getsockopt functions.  The order is socket lock
-> g_ipt_lock (or g_ip6t_lock) -> netdev list lock, NAT lock or
g_ipfilter_lock.  No path takes g_ipt_lock or g_ip6t_lock under one of
those locks.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli force-pushed the ipfilter-replace-lock-up branch from 9976449 to 98e1047 Compare November 28, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Networking Effects networking subsystem Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants