diff options
| author | James O'Doherty <james@theodohertyfamily.com> | 2026-05-29 18:29:12 -0400 |
|---|---|---|
| committer | James O'Doherty <james@theodohertyfamily.com> | 2026-05-29 18:29:12 -0400 |
| commit | ee2f5d545825752af63da36e2b9ec7a92985a875 (patch) | |
| tree | 7328f73ac157dd19fa60e887fd243f0855935cce /internal/namespace/namespace.go | |
| parent | 135f6edbd9389bc4783f13c26aed0a74d3c8aca0 (diff) | |
feat: implement userspace wireguard data-path and unprivileged host fd-passing
- Implement complete rootless network namespace bootstrap via C launcher using unshare(CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWNET).
- Resolve unprivileged network isolation blackhole via host-socket preservation (FD passing): open client UDP sockets on the host pre-isolation, clear O_CLOEXEC, and ingest them via custom `FDBind` inside the sandbox.
- Implement isolated routing table automation over `tun0` (addresses, MTU, default routes).
- Implement persistent, multi-process namespace sharing and joining using reference-counted PID files and the setns system call.
- Write robust, self-contained E2E data plane test suites in `tests/e2e/e2e_test.go` using a mock UDP listener.
- Update project documentation (`README.md` and `AGENTS.md`) to reflect completed milestones.
- Ensure 100% test passing rate and zero lint/staticcheck warnings.
Diffstat (limited to 'internal/namespace/namespace.go')
| -rw-r--r-- | internal/namespace/namespace.go | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/internal/namespace/namespace.go b/internal/namespace/namespace.go index b0794a4..a1e7ad9 100644 --- a/internal/namespace/namespace.go +++ b/internal/namespace/namespace.go @@ -3,9 +3,12 @@ package namespace import ( _ "embed" "fmt" + "net" "os" "os/exec" "syscall" + + "golang.org/x/sys/unix" ) //go:embed launcher.bin @@ -123,7 +126,34 @@ func Bootstrap() error { } } } - err = syscall.Exec(launcherPath, args, os.Environ()) + + // Open the host network namespace file descriptor before unsharing. + hostNetFd, err := syscall.Open("/proc/self/ns/net", syscall.O_RDONLY, 0) + if err != nil { + return fmt.Errorf("failed to open host netns: %w", err) + } + // Clear close-on-exec so it remains open across syscall.Exec + if flags, err := unix.FcntlInt(uintptr(hostNetFd), unix.F_GETFD, 0); err == nil { + _, _ = unix.FcntlInt(uintptr(hostNetFd), unix.F_SETFD, flags&^unix.FD_CLOEXEC) + } + + env := append(os.Environ(), fmt.Sprintf("WG_WRAP_HOST_NETNS_FD=%d", hostNetFd)) + + // Open a host UDP socket on 0.0.0.0:0 before unsharing network namespace. + laddr, err := net.ResolveUDPAddr("udp", "0.0.0.0:0") + if err == nil { + if conn, err := net.ListenUDP("udp", laddr); err == nil { + if file, err := conn.File(); err == nil { + hostSocketFd := file.Fd() + if flags, err := unix.FcntlInt(hostSocketFd, unix.F_GETFD, 0); err == nil { + _, _ = unix.FcntlInt(hostSocketFd, unix.F_SETFD, flags&^unix.FD_CLOEXEC) + } + env = append(env, fmt.Sprintf("WG_WRAP_HOST_SOCKET_FD=%d", hostSocketFd)) + } + } + } + + err = syscall.Exec(launcherPath, args, env) if err != nil { return fmt.Errorf("launcher exec failed: %w", err) } |
