Repository navigation
Conversation
|
| * | ||
| ****************************************************************************/ | ||
|
|
||
| void ipfilter_cfg_add(FAR struct ipfilter_entry_s *entry, |
Contributor
There was a problem hiding this comment.
why not remove ipfilter_cfg_add and call sq_addlast directly
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
force-pushed
the
ipfilter-replace-lock-up
branch
from
November 28, 2026 21:19
9976449 to
98e1047
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IPT_SO_SET_REPLACEandIP6T_SO_SET_REPLACErun under the lock of their own socket only (conn_lock()inipv4_setsockopt()andipv6_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 inipv4_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:
g_ipfilter_lock, a mutex for the filter chains. The packet path holds it while it walks a chain. The conversion inipt_filter.cnow builds the new chains aside withsq_addlast(), and the newipfilter_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()andipfilter_cfg_add()are removed (their only caller was the conversion).replace_entries()swapstable->repland frees the old table whileget_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 serializeipt_setsockopt()/ipt_getsockopt()and theip6tfunctions.ip6t_sockopt.c(no code change; the CI checks the whole file).Lock order (no path takes these locks in the other order):
g_ipfilter_lockg_ipt_lock/g_ip6t_lock-> netdev list lock, NAT lock (NAT table) org_ipfilter_lockg_ipfilter_lockis 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
CONFIG_NET_IPFILTER/CONFIG_NET_IPTABLES(default n).net/:ipfilter_cfg_replace()and the removedipfilter_cfg_add()/ipfilter_cfg_clear()are internal to the network stack (net/ipfilter/ipfilter.h).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, andiptables -F OUTPUTruns while the walk sleeps:flink0xa5a5a5a5a5a5a5a5 from a freed entry and stops in a data abort (FAR_ELn: 0xa5a5a5a5a5a5a5a5, task hpwork).iptables -F OUTPUTwaits 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; afteriptables -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; afterip6tables -F INPUT: 2 of 2.iptables/ip6tables-Aand-Fcommands while a background ping sends 40 echo requests: 40 of 40 replies,iptables -Landip6tables -Lshow empty chains at the end.Exiting with status 0in 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 ACCEPTandiptables -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 -Fand 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
-Werrorin the CI image, all withNET_IPFILTER,NET_IPTABLESand 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.