1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// Package namespace provides primitives for managing Linux user and network
// namespaces, including bootstrapping and pinning.
//
// Rootless Bootstrap Loop & Host-Socket Preservation:
// To achieve rootless network isolation without interfering with the Go runtime's multi-threaded
// scheduler, and to maintain encrypted UDP socket connectivity over the host's network,
// wg-wrap employs an advanced bootstrap loop:
//
// 1. Host-Bound Socket Creation: During the initial host-level start, a UDP socket is opened
// on 0.0.0.0:0 on the host, and its FD is stored in the environment (WG_WRAP_HOST_SOCKET_FD).
// 2. Helper Deployment: An embedded single-threaded C launcher is used to bridge the transition.
// 3. Namespace Transition: The process replaces itself with the C launcher via syscall.Exec.
// 4. Isolation: The launcher performs the unshare(CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWNET)
// sequence to isolate Mount, User, and Network environments.
// 5. Re-entry: The launcher then execvp's the original wg-wrap binary.
// 6. FDBind Tunnel Initialization: The second instance of wg-wrap wraps the host socket FD
// inside a custom FDBind struct to initialize wireguard-go.
//
// User Namespace Sequence:
// To create a network namespace without root, wg-wrap follows the sequence:
// CLONE_NEWUSER -> CLONE_NEWNET -> Setuid/Setgid -> Configure Interfaces.
package namespace
import (
_ "embed"
"fmt"
"net"
"os"
"syscall"
"git.theodohertyfamily.com/wg-wrap/internal/network"
"golang.org/x/sys/unix"
)
// MountOps abstracts the filesystem mount operations.
type MountOps interface {
Mount(source, target, fstype string, flags uintptr, data string) error
Unmount(target string, flags int) error
}
// realMountOps is the production implementation using unix.Mount.
type realMountOps struct{}
func (r *realMountOps) Mount(source, target, fstype string, flags uintptr, data string) error {
return unix.Mount(source, target, fstype, flags, data)
}
func (r *realMountOps) Unmount(target string, flags int) error {
return unix.Unmount(target, flags)
}
// DefaultMountOps is the global instance used by the package functions.
var DefaultMountOps MountOps = &realMountOps{}
// FileSystem abstracts the basic filesystem operations used for isolation.
type FileSystem interface {
Stat(name string) (os.FileInfo, error)
MkdirAll(path string, perm os.FileMode) error
CreateTemp(dir, pattern string) (*os.File, error)
MkdirTemp(dir, pattern string) (string, error)
Remove(name string) error
}
// realFS is the production implementation using the os package.
type realFS struct{}
func (r *realFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
func (r *realFS) MkdirAll(path string, perm os.FileMode) error {
return os.MkdirAll(path, perm)
}
func (r *realFS) CreateTemp(dir, pattern string) (*os.File, error) {
return os.CreateTemp(dir, pattern)
}
func (r *realFS) MkdirTemp(dir, pattern string) (string, error) {
return os.MkdirTemp(dir, pattern)
}
func (r *realFS) Remove(name string) error { return os.Remove(name) }
// DefaultFS is the global instance used by the package functions.
var DefaultFS FileSystem = &realFS{}
// ResetDefaults restores the default implementations of MountOps and FileSystem.
func ResetDefaults() {
DefaultMountOps = &realMountOps{}
DefaultFS = &realFS{}
}
// BootstrapConfig contains the environment and arguments needed to execute the bootstrap launcher.
type BootstrapConfig struct {
Args []string
Env []string
Fds []int
ExecPath string
}
//go:embed launcher.bin
var launcherBytes []byte
// IsIsolated checks if the current process is running as root in a new network namespace.
func IsIsolated() bool {
return os.Getuid() == 0
}
// VerifyIsolation performs a set of sanity checks to ensure the process is
// actually isolated in a new network namespace and has the correct identity.
func VerifyIsolation() (bool, string) {
// 1. Check UID
if os.Getuid() != 0 {
return false, fmt.Sprintf("Expected UID 0, got %d", os.Getuid())
}
// 2. Check Network Isolation
// We expect a fresh network namespace to have only the loopback interface.
interfaces, err := network.ListInterfaces()
if err != nil {
return false, fmt.Sprintf("failed to list interfaces: %v", err)
}
// In a fresh netns, we typically only see 'lo'.
// If we see more than just loopback, or loopback is missing, it might not be isolated.
if len(interfaces) == 0 {
return false, "no network interfaces found"
}
hasLo := false
for _, iface := range interfaces {
if iface.Name == "lo" {
hasLo = true
} else {
// If we find any other interface (eth0, wlan0, etc.), we aren't isolated.
return false, fmt.Sprintf("detected non-isolated interface: %s", iface.Name)
}
}
if !hasLo {
return false, "loopback interface missing"
}
// 3. Check Filesystem Transparency
home := os.Getenv("HOME")
if home != "" {
if _, err := os.ReadDir(home); err != nil {
return false, fmt.Sprintf("cannot read home directory: %v", err)
}
}
return true, "Isolated and root"
}
// VerifyArguments prints the current process arguments as hex-encoded strings.
// This is used for E2E testing to verify that the data path is 8-bit clean
// and that no bytes are mutated during the bootstrap loop.
func VerifyArguments(args []string) error {
for i, arg := range args {
fmt.Printf("%d:%x\n", i, arg)
}
return nil
}
// Bootstrap ensures the process is running in an isolated user and network namespace.
// It uses memfd_create to run the embedded C launcher from memory, bypassing
// disk-based noexec restrictions.
func Bootstrap() (err error) {
if IsIsolated() {
return nil
}
config, err := PrepareBootstrap()
if err != nil {
return err
}
defer func() {
for _, fd := range config.Fds {
_ = syscall.Close(fd)
}
}()
err = syscall.Exec(config.ExecPath, config.Args, config.Env)
if err != nil {
return fmt.Errorf("launcher exec failed: %w", err)
}
return nil
}
// PrepareBootstrap calculates the environment and arguments needed for the bootstrap launcher.
func PrepareBootstrap() (*BootstrapConfig, error) {
// 0. Validate current arguments for null bytes before proceeding.
for i, arg := range os.Args {
for j := 0; j < len(arg); j++ {
if arg[j] == 0 {
return nil, fmt.Errorf("argument %d contains null byte at position %d", i, j)
}
}
}
self, err := os.Executable()
if err != nil {
return nil, fmt.Errorf("failed to get executable path: %w", err)
}
execFd, err := prepareLauncher()
if err != nil {
return nil, err
}
// Clear close-on-exec
if flags, err := unix.FcntlInt(uintptr(execFd), unix.F_GETFD, 0); err == nil {
_, _ = unix.FcntlInt(uintptr(execFd), unix.F_SETFD, flags&^unix.FD_CLOEXEC)
}
// Prepare arguments for the launcher.
args := []string{self}
args = append(args, os.Args[1:]...)
for i, arg := range args {
for j := 0; j < len(arg); j++ {
if arg[j] == 0 {
return nil, fmt.Errorf("launcher argument %d contains null byte at position %d", i, j)
}
}
}
// 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 nil, fmt.Errorf("failed to open host netns: %w", err)
}
// Clear close-on-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, errAddr := net.ResolveUDPAddr("udp", "0.0.0.0:0")
if errAddr == nil {
if conn, errConn := net.ListenUDP("udp", laddr); errConn == nil {
if file, errFile := conn.File(); errFile == nil {
hostSocketFd := file.Fd()
if flags, fcntlErr := unix.FcntlInt(hostSocketFd, unix.F_GETFD, 0); fcntlErr == nil {
_, _ = unix.FcntlInt(hostSocketFd, unix.F_SETFD, flags&^unix.FD_CLOEXEC)
}
env = append(env, fmt.Sprintf("WG_WRAP_HOST_SOCKET_FD=%d", hostSocketFd))
_ = conn.Close()
}
}
}
return &BootstrapConfig{
Args: args,
Env: env,
Fds: []int{execFd, hostNetFd},
ExecPath: fmt.Sprintf("/proc/self/fd/%d", execFd),
}, nil
}
// BootstrapJoin joins the namespaces of the target PID and replaces the current process.
func BootstrapJoin(targetPid int) (err error) {
if IsIsolated() {
return nil
}
config, err := PrepareBootstrapJoin(targetPid)
if err != nil {
return err
}
defer func() {
for _, fd := range config.Fds {
_ = syscall.Close(fd)
}
}()
err = syscall.Exec(config.ExecPath, config.Args, config.Env)
if err != nil {
return fmt.Errorf("launcher exec failed: %w", err)
}
return nil
}
// PrepareBootstrapJoin calculates the environment and arguments needed to join a namespace.
func PrepareBootstrapJoin(targetPid int) (*BootstrapConfig, error) {
for i, arg := range os.Args {
for j := 0; j < len(arg); j++ {
if arg[j] == 0 {
return nil, fmt.Errorf("argument %d contains null byte at position %d", i, j)
}
}
}
self, err := os.Executable()
if err != nil {
return nil, fmt.Errorf("failed to get executable path: %w", err)
}
execFd, err := prepareLauncher()
if err != nil {
return nil, err
}
if flags, err := unix.FcntlInt(uintptr(execFd), unix.F_GETFD, 0); err == nil {
_, _ = unix.FcntlInt(uintptr(execFd), unix.F_SETFD, flags&^unix.FD_CLOEXEC)
}
args := []string{self}
args = append(args, os.Args[1:]...)
for i, arg := range args {
for j := 0; j < len(arg); j++ {
if arg[j] == 0 {
return nil, fmt.Errorf("launcher argument %d contains null byte at position %d", i, j)
}
}
}
env := append(os.Environ(),
fmt.Sprintf("WG_WRAP_JOIN_PID=%d", targetPid),
"WG_WRAP_JOINED=1",
)
return &BootstrapConfig{
Args: args,
Env: env,
Fds: []int{execFd},
ExecPath: fmt.Sprintf("/proc/self/fd/%d", execFd),
}, nil
}
func prepareLauncher() (int, error) {
// Use memfd_create to create an anonymous file in memory.
// This bypasses the need for a temporary disk file and avoids noexec restrictions.
fd, err := unix.MemfdCreate("wg-wrap-launcher", 0)
if err != nil {
return 0, fmt.Errorf("failed to create memfd: %w", err)
}
if _, err = unix.Write(fd, launcherBytes); err != nil {
_ = unix.Close(fd)
return 0, fmt.Errorf("failed to write launcher binary to memfd: %w", err)
}
return fd, nil
}
|