Leadaxe 报告 sing-box-lx 在 Linux 停止自动重定向后未恢复 rp_filter

Leadaxe 报告,Linux 上的 TUN auto_redirect 会将各接口、all 和 default 的 rp_filter 设为 0,停止后仍不恢复,使反向路径过滤保持关闭;其复现来自 Ubuntu runner 的网络命名空间,Android、macOS 和单独使用 auto_route 不在该报告范围。他说明,其分支在清零前记录原有非零值,在该命名空间最后一个 auto_redirect 关闭时恢复,且只恢复仍为 0 的键,保留运行期间他人作出的修改。新建接口继承的 0,以及 SIGKILL、崩溃或 OOM 后的恢复仍不覆盖。

作者原文

Symptom

On Linux, a tun inbound with auto_redirect sets net.ipv4.conf.*.rp_filter to 0 on start and leaves it there after sing-box stops. Reverse path filtering (protection against spoofed source addresses) stays off on every interface, on all and on default (so on interfaces created later too) until the next reboot. On OpenWrt, where all and default are 1 out of the box, a router running the core with auto_redirect silently loses this protection once the core is stopped or removed.

Affected: v1.14.6-lx.41 … v1.14.7-lx.45, Linux only (OpenWrt routers, Linux desktop). Android and macOS are not affected. Plain auto_route without auto_redirect does not touch the global keys.

Reproduced in a network namespace on an Ubuntu runner, values before start all=1 default=1 lo=0 vn=2:

| | while running | after stop |
|---|---|---|
| lx.45, start → stop | all=0 default=0 vn=0 | all=0 default=0 vn=0 |
| lx.45, start → SIGHUP → stop | all=0 default=0 vn=0 | all=0 default=0 vn=0 |

Cause

The code is upstream's (sing-tun redirect_linux.go, enableForwarding, commit 7247a83, 2026-11-29). Auto-redirect needs rp_filter=0 while it runs: packets of LAN clients arrive on the LAN interface, get a mark, and policy routing sends them to the tun table, so a strict reverse path check drops them. sing-tun writes 0 into every entry of /proc/sys/net/ipv4/conf and Close never writes the old values back.

Two minutes earlier upstream removed the restore of the forwarding sysctls on close on purpose (0ec7ffa, "Keep forwarding sysctls enabled after auto-redirect close"); rp_filter was added without a restore as well. No reason is given. Writing IPv6 forwarding back is disruptive by itself (the kernel propagates it to every interface and touches RA default routes), writing rp_filter back is not. Leaving forwarding on keeps a capability, leaving rp_filter off removes a protection, so I treat the latter as a defect. sing-tun dev has the same code.

Narrowing it down is not an option: the effective value is max(all, interface), so all has to go to 0 anyway, and the drop happens on the ingress interface, not on the tun. Loose mode 2 instead of 0 can still drop sources routed through WireGuard and other tunnels in separate policy routing tables.

Fix

  • Right before zeroing, the fork remembers the non-zero rp_filter values of every conf entry.
  • When the last auto-redirect of that network namespace closes, they are written back.
  • A key is restored only if it still reads 0; a key someone changed while sing-box ran is left alone.
  • Forwarding and accept_ra keep the upstream behaviour.

Limits: interfaces created while sing-box runs inherit default=0 and keep it; after SIGKILL, a crash or OOM nothing is restored (a state file in /run would cover that and is noted in the SPEC as a possible follow-up).