casaroli opened a new pull request, #22481:
URL: https://github.com/apache/nuttx/pull/22481

   ## 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: 
https://github.com/apache/nuttx/pull/22367#issuecomment-7207363577
   
   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 `ipfilter_cfg_add()`, 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()` is removed (its 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 `ipt_filter.c` and `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_add()` / 
`ipfilter_cfg_replace()` 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`. Master is 0f1701da1d1 with nuttx-apps master 9a1d292722e. 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) and qemu-armv8a:knsh 
(kernel, the commands run from /system/bin):
   
   - `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 same functional run passes on this PR merged with #22367 (flat). The two 
PRs change the same files; `git merge-tree` shows a conflict only in the 
nxstyle blank lines of `ipt_filter.c` (the switch blocks that #22367 removes). 
The code changes merge without a conflict.
   
   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, qemu-armv8a:knsh (kernel, apps imported), 
rv-virt:netnsh64 with IPv4 + IPv6, with IPv4 only and with IPv6 only, 
rv-virt:knetnsh64 (kernel, apps imported). 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 613012 -> 613376 
(+364), data 17553 -> 17625 (+72, the three mutexes). No change without 
`NET_IPFILTER` / `NET_IPTABLES`.
   
   `tools/checkpatch.sh -c -u -m -g`: pass.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to