Skip to content

proxy: carry a patch making execInNetworkNamespace restore the calling THREAD's netns (/proc/thread-self), with the cross-pod landing proof signal (#1022) - #1028

Merged
bpalermo merged 3 commits into
mainfrom
fix/1022-thread-self-netns
Sep 28, 2026

Conversation

@bpalermo

@bpalermo bpalermo commented Sep 27, 2026 •

Copy link
Copy Markdown
Owner

Mechanism

Network::Utility::execInNetworkNamespace (source/common/network/utility.h ~383 at the pinned 726d7ac) records the namespace to return to by opening /proc/self/ns/net. In a multi-threaded process /proc/self resolves to the thread-group leader, which is the main thread. setns(CLONE_NEWNET) moves only the calling thread.

The main thread enters pod namespaces briefly and often: health-check connects every 5 s per local pod, and listener socket creation. If a worker calls the helper (every app_<pod> connection) during one of those windows, it "restores" itself into that pod's netns and stays there until its next call. While it is stuck, its host-intended upstream sockets are created inside the pod. Redirect-all capture then diverts them to the pod's capture listener, and #1007's use_original_dst handoff passes them to the newest pod's inbound :18008 listener, where the client's SAN check rejects them. That is the working explanation for #1007's cross-pod L4 landings. The talos proof below is what confirms or refutes it.

/proc/self/ns/net is the only place in the Envoy tree that saves and restores a netns this way. I grepped for /proc/self, ns/net and setns, and every setns call site is inside this helper.

The patch

proxy/bazel/patches/envoy-aether1022-exec-in-netns-thread-self.patch is the third entry in single_version_override(patches=…). It is applied after #47743 and #47740, and git apply --check passes on the three in order at 726d7ac.

  • utility.h: opens /proc/thread-self/ns/net. On ENOENT (kernels older than 3.17) it falls back to /proc/self/task/<gettid()>/ns/net. The error message names whichever path failed.
  • utility_test.cc: ExecInNetnsTest.Basic's expected path string follows the new spelling.
  • New test/common/network:exec_in_netns_thread_test, in the patch's BUILD hunk.
  • //bazel/patches:carried_patch_tests is a new test_suite that runs that test plus utility_test from the proxy workspace's bazel test //.... //... never reaches @envoy tests on its own, so without this the test would not run in CI.

Nothing is filed upstream. That is the maintainer's call, and #980 notes the third carried patch.

The test, and why it cannot pass vacuously

ExecInNetnsTest.Basic mocks open() to hand back an opaque fd for any path. Under that mock every spelling "restores correctly", so the test can never catch this bug. The new test has three cases:

  1. WorkerReturnsToItsOwnNetnsWhileMainThreadIsInAnother (model, runs everywhere).
    • The mocked open()/close()/setns() drive a model of the kernel's per-thread netns state. The model resolves /proc/self/ns/net to the leader's (getpid()) namespace, /proc/thread-self/ns/net to the calling thread's, and /proc/self/task/<tid>/ns/net to thread <tid>'s. setns() moves only the caller.
    • The leader sits in pod-a, and a real std::thread worker calls the helper into pod-b.
    • The main assertion is where the worker ends up (host), not how the opened path is spelled. The path (/proc/thread-self/ns/net, opened by the worker's tid) is asserted as well.
    • A guard asserts that the model tells the two /proc views apart from a non-main thread.
  2. FallsBackToTaskTidWhenThreadSelfIsAbsent (model). /proc/thread-self returns ENOENT. The test asserts the open sequence /proc/thread-self/ns/net, then /proc/self/task/<worker tid>/ns/net, then pod-b, and that the worker ends in host.
  3. RealKernelWorkerReturnsToItsOwnNetns (real kernel, compared by netns st_ino).
    • It runs in a forked child. The child tries unshare(CLONE_NEWNET) first, then an unprivileged user namespace.
    • The child builds three namespaces. The main thread stays in n2, the worker setnses to n1, and the helper targets n3.
    • The assertion is that the worker's /proc/thread-self/ns/net inode after the call equals n1's inode, and that the main thread is still in n2.
    • Where namespaces cannot be created it SKIPs (exit 77), and cases 1 and 2 are the gate. On BuildBuddy RBE it did run, as the red output below shows, so CI checks the inode against the real kernel and not only the model.

Shown red on the unpatched helper

The first commit (3e9a6d2) added the test without the utility.h fix. That proxy run failed on both arches (run 36359455791), with all three cases red:

[  FAILED  ] ExecInNetnsThreadTest.WorkerReturnsToItsOwnNetnsWhileMainThreadIsInAnother
    Which is: "/var/run/netns/pod-a"   vs   "host"   -- the worker was restored into the main thread's namespace
[  FAILED  ] ExecInNetnsThreadTest.FallsBackToTaskTidWhenThreadSelfIsAbsent
[  FAILED  ] ExecInNetnsThreadTest.RealKernelWorkerReturnsToItsOwnNetns
    worker restored into netns inode 4026537090 (main thread's is 4026537090), expected its own 4026537016   (amd64)
    worker restored into netns inode 4026532828 (main thread's is 4026532828), expected its own 4026532766   (arm64)
Executed 8 out of 8 tests: 7 tests pass and 1 fails remotely.   (utility_test green)

Green with the fix

The second commit (1072f91) makes proxy green on both legs (run 36360025650): amd64 9m36s, arm64 8m50s, Executed 8 out of 8 tests: 8 tests pass. That set includes exec_in_netns_thread_test, utility_test and //:image_test on the patched Envoy.

Proof recipe for the next talos deploy

This is in the runbook section "Cross-pod L4 landings (#1007/#1022)" and in the soak README's zero-reading gates table.

# Stray landings: the default TCP-floor chain of every pod except tcp-echo (TCP-primary);
# the per-port chains (…_<port>_…) are legitimate and excluded. RAW counters.
sum by (__name__) ({__name__=~"envoy_tcp_in_tcp_.*_downstream_cx_total",
                    __name__!~"envoy_tcp_in_tcp_tcp_echo_.*|envoy_tcp_in_tcp_.*_[0-9]+_downstream_cx_total"})
max_over_time(envoy_cluster_ssl_fail_verify_san_total{aether_cluster=~"aether-test/(tcp-echo|mixed-svc)"}[8h])
max_over_time(envoy_listener_inbound_ssl_connection_error_total[8h]) > 0

Charts are untouched. The proxy pin bot re-pins after merge.

Refs #1022. The talos deploy closes it, once the counter reads zero on the patched proxy.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

bpalermo and others added 2 commits September 27, 2026 20:39
…ix (#1022, expected RED)

Adds @envoy//test/common/network:exec_in_netns_thread_test through a carried
patch and runs it (with utility_test) from //bazel/patches:carried_patch_tests,
WITHOUT the utility.h fix, so the proxy CI run on this commit shows the test
fails against the unpatched helper. The fix follows in the next commit.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
…roc/thread-self), plus the cross-pod landing proof signal (#1022)

The carried patch now fixes Network::Utility::execInNetworkNamespace: it
records the namespace to return to from /proc/thread-self/ns/net (fallback
/proc/self/task/<tid>/ns/net on ENOENT) instead of /proc/self/ns/net, which is
the thread-group leader's namespace. setns(CLONE_NEWNET) is per thread, so a
worker calling the helper while the main thread was briefly inside a pod netns
"restored" itself into that pod's netns and kept creating upstream sockets
there -- the hypothesised source of #1007's cross-pod L4 landings.
ExecInNetnsTest.Basic's path expectation follows the new spelling.

The previous commit ran the new exec_in_netns_thread_test without the fix:
all three cases failed on both arches, including the real-kernel case
("worker restored into netns inode X (main thread's is X), expected its own Y").

Docs: runbook "Cross-pod L4 landings (#1007/#1022)" with the TCP-floor
landing PromQL and the verify_san / inbound ssl_connection_error join; the
soak README's zero-reading gates table gets the landing counter with rev242
as the negative control; proxy/README lists the third carried patch.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant