Repository navigation
fix: package ops: socket fd references to network namespaces - #105
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 5, 2026 21:30
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.
Problem
With
discover.FromFds(), discovery also finds network namespaces that are kept alive only by sockets (viapidfd_getfd+SIOCGSKNS). Such namespaces get a reference of the form/proc/<pid>/fd/<n>, which points to the socket, not to an nsfs inode. Everyopsoperation on such a reference fails:So these namespaces are discovered, but cannot be used:
ID(),Type(),NsFd(),OpenTypedReference(), and thusVisit/Execute/Goall fail withENXIO. Discovery additionally leaves their owning user namespace undetected, asDetectOwneris 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)
/proc/<pid>/fd/<n>→socket:[…]open(O_RDONLY)ENXIOopen(O_PATH)setnsandSIOCGSKNSon it fail withEBADFstatpidfd_open→pidfd_getfd→SIOCGSKNSfstatino matches the netns;setnsworksFix
New internal
openNamespacePathreplaces plain path opens inops:open(O_RDONLY|O_CLOEXEC). Ordinary namespace paths behave exactly as before.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.ENXIOerror 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.Filewithos.Open-style errors),socketNetnsFd,parseProcFdPath.ops/path.go:Type,ID,User,Parent,OwnerUID,NsFd,OpenTypedReferenceuse the new helpers.ops/typedpath.go:Parent,OpenTypedReferencelikewise.ops/portable/portable.go:openPathopens viaNamespacePath.NsFd(); error messages for invalid paths now areInvalidNamespaceErrors.discover/discovery_fd.go: set the relation for socket-derived netns too, soDetectOwnerruns and these netns get their owning user namespace.opspackage doc andFromFds()describe socket fd references, privilege requirements, and limited lifetime.Visit,Execute, andGoare unchanged: they solely go throughOpenTypedReference/NsFd.Compatibility
model.NamespaceRef, JSON).ENXIOon socket fd paths now succeed; all other paths take the identical code path as before.Caveats (documented)
pidfd_getfdneeds ptrace attach access to the target process (CAP_SYS_PTRACEfor foreign processes);SIOCGSKNSneedsCAP_NET_ADMINin the netns' owning user namespace – the same as discovery needed to find these netns in the first place.Tests
ops/sockref_test.go:parseProcFdPathtable test (/proc/1/fd/3,/proc/42/task/43/fd/0, non-/procprocfs root; rejectsself,thread-self, non-numeric, zero PID, negative and overflowing numbers).ENXIOkept for unix domain socket files outside procfs (non-root and root).ENXIOkept when the socket's netns is inaccessible (non-root).ID()yields the netns dev+ino (not the socket's),Type(),User(),NsFd(),OpenTypedReference();VisitviaNamespacePathandTypedNamespacePathenters the netns; socket held by another process (sleepinheriting the socket as fd 3).discover/discovery_fd_test.go(root): socket-only netns found viaFromFds()has a usable reference and, withWithOwnership(), a non-nilOwner().Ran
go test -race -p 1for./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 usesFromFds(), aslsnslists network namespaces only kept alive by sockets, such as those of WSL2'sinitd; it previously failed on WSL2 (also on unmodifieddevelop).