Skip to content

fix: package ops: socket fd references to network namespaces - #105

Merged
thediveo merged 2 commits into
developfrom
fix/ops-socket-fd-netns-refs
Nov 5, 2026
Merged

thediveo merged 2 commits into
developfrom
fix/ops-socket-fd-netns-refs

Conversation

@thediveo

@thediveo thediveo commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

Problem

With discover.FromFds(), discovery also finds network namespaces that are kept alive only by sockets (via pidfd_getfd + SIOCGSKNS). Such namespaces get a reference of the form /proc/<pid>/fd/<n>, which points to the socket, not to an nsfs inode. Every ops operation on such a reference fails:

lxkns.Visit: cannot reference namespace, lxkns: invalid namespace path
"/proc/32782/fd/6": open /proc/32782/fd/6: no such device or address

So these namespaces are discovered, but cannot be used: ID(), Type(), NsFd(), OpenTypedReference(), and thus Visit/Execute/Go all fail with ENXIO. Discovery additionally leaves their owning user namespace undetected, as DetectOwner is skipped for them.

Seen in the wild with WSL2's initd (PID 21) holding sockets in two netns without any process attached.

Kernel behaviour (independently verified, own and foreign process, as root)

operation on /proc/<pid>/fd/<n> → socket:[…] result
open(O_RDONLY) ENXIO
open(O_PATH) succeeds; setns and SIOCGSKNS on it fail with EBADF
stat succeeds, but returns the socket inode on sockfs (dev 9), not the netns inode
pidfd_open → pidfd_getfd → SIOCGSKNS returns a netns fd; fstat ino matches the netns; setns works

Fix

New internal openNamespacePath replaces plain path opens in ops:

  1. Try open(O_RDONLY|O_CLOEXEC). Ordinary namespace paths behave exactly as before.
  2. Only on ENXIO, and only if the path parses as …/<pid>/fd/<n> or …/<pid>/task/<tid>/fd/<n>: pidfd_open(pid) → pidfd_getfd(n) → ioctl(SIOCGSKNS), returning the netns fd.
  3. If the fallback fails, return the original ENXIO error unchanged.

There is deliberately no stat-based fallback, as that would silently yield the socket's inode instead of the namespace's.

Changes

  • ops/sockref.go (new): openNamespacePath, openNamespaceFile (returns *os.File with os.Open-style errors), socketNetnsFd, parseProcFdPath.
  • ops/path.go: Type, ID, User, Parent, OwnerUID, NsFd, OpenTypedReference use the new helpers.
  • ops/typedpath.go: Parent, OpenTypedReference likewise.
  • ops/portable/portable.go: openPath opens via NamespacePath.NsFd(); error messages for invalid paths now are InvalidNamespaceErrors.
  • discover/discovery_fd.go: set the relation for socket-derived netns too, so DetectOwner runs and these netns get their owning user namespace.
  • Docs: ops package doc and FromFds() describe socket fd references, privilege requirements, and limited lifetime.

Visit, Execute, and Go are unchanged: they solely go through OpenTypedReference/NsFd.

Compatibility

  • No API changes, no change to the reference format (model.NamespaceRef, JSON).
  • Only calls that previously failed with ENXIO on socket fd paths now succeed; all other paths take the identical code path as before.

Caveats (documented)

  • Privileges: pidfd_getfd needs ptrace attach access to the target process (CAP_SYS_PTRACE for foreign processes); SIOCGSKNS needs CAP_NET_ADMIN in the netns' owning user namespace – the same as discovery needed to find these netns in the first place.
  • Lifetime: a socket fd reference is valid only as long as the process keeps that fd open; a reused fd number may silently point to a socket in a different netns. Callers knowing the expected namespace ID should cross-check dev+ino after opening.
  • PID namespace: the PID in the path is interpreted in the caller's PID namespace.

Tests

  • ops/sockref_test.go:
    • parseProcFdPath table test (/proc/1/fd/3, /proc/42/task/43/fd/0, non-/proc procfs root; rejects self, thread-self, non-numeric, zero PID, negative and overflowing numbers).
    • ENXIO kept for unix domain socket files outside procfs (non-root and root).
    • ENXIO kept when the socket's netns is inaccessible (non-root).
    • Root, netns kept alive only by a socket: ID() yields the netns dev+ino (not the socket's), Type(), User(), NsFd(), OpenTypedReference(); Visit via NamespacePath and TypedNamespacePath enters the netns; socket held by another process (sleep inheriting the socket as fd 3).
    • Goroutine, fd, and task namespace leak checks.
  • discover/discovery_fd_test.go (root): socket-only netns found via FromFds() has a usable reference and, with WithOwnership(), a non-nil Owner().

Ran go test -race -p 1 for ./ops/... ./discover/... as non-root and root: all pass.

Separate commit: the root-only lsns comparison test (Discover from processes … finds at least the namespaces lsns finds) now also uses FromFds(), as lsns lists network namespaces only kept alive by sockets, such as those of WSL2's initd; it previously failed on WSL2 (also on unmodified develop).

Network namespaces discovered via sockets (FromFds) are referenced by
the socket's /proc/$PID/fd/$FD path, which cannot be opened (ENXIO).
NamespacePath and TypedNamespacePath now fall back to retrieving the
socket's network namespace via pidfd_open, pidfd_getfd and SIOCGSKNS,
so ID, Type, User, Parent, OwnerUID, NsFd, OpenTypedReference, and in
consequence Visit/Execute/Go work on such references. Portable
references use the same mechanism.

Discovery now also detects the owning user namespace of network
namespaces found via sockets.

Signed-off-by: thediveo <thediveo@gmx.eu>
lsns also lists network namespaces only kept alive by sockets, such as
those of WSL2's initd, so the lsns comparison failed on WSL2.

Signed-off-by: thediveo <thediveo@gmx.eu>
@thediveo
thediveo marked this pull request as ready for review November 5, 2026 21:30
@thediveo
thediveo merged commit 808aba2 into develop Nov 5, 2026
8 checks passed
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