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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
//go:build linux
package wireguard
import (
"encoding/base64"
"encoding/hex"
"fmt"
"net"
"net/netip"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"git.theodohertyfamily.com/tools/wg-wrap/pkg/wgconf"
"golang.org/x/sys/unix"
"golang.zx2c4.com/wireguard/conn"
"golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/tun"
)
// Tunnel represents an active Userspace WireGuard tunnel inside a network namespace.
type Tunnel struct {
Device *device.Device
Tun tun.Device
}
// StartTunnel creates a TUN device, launches wireguard-go over it, and configures IPs/routes.
func StartTunnel(cfg *wgconf.Config, dnsServer string) (t *Tunnel, err error) {
var cleanups []func()
defer func() {
if err != nil {
for i := len(cleanups) - 1; i >= 0; i-- {
cleanups[i]()
}
}
}()
// 1. Create the TUN device inside the current (isolated) namespace
tunName := "tun0"
mtu := 1420
if err := unix.Mount("", "/", "", unix.MS_REC|unix.MS_PRIVATE, ""); err != nil {
fmt.Printf("warning: failed to make mount namespace private: %v\n", err)
}
if err := BlockHostServices(); err != nil {
fmt.Printf("warning: failed to block host services: %v\n", err)
}
tunDev, err := tun.CreateTUN(tunName, mtu)
if err != nil {
return nil, fmt.Errorf("failed to create TUN device %s: %w", tunName, err)
}
cleanups = append(cleanups, func() { tunDev.Close() })
// 2. Instantiate the userspace WireGuard device
logger := device.NewLogger(device.LogLevelSilent, "[wg-wrap] ")
var bind conn.Bind
if hostSocketFdStr := os.Getenv("WG_WRAP_HOST_SOCKET_FD"); hostSocketFdStr != "" {
if fd, err := strconv.Atoi(hostSocketFdStr); err == nil && fd > 0 {
if fdBind, err := NewFDBind(fd); err == nil {
bind = fdBind
}
}
}
if bind == nil {
bind = conn.NewDefaultBind()
if hostNetNSFdStr := os.Getenv("WG_WRAP_HOST_NETNS_FD"); hostNetNSFdStr != "" {
if fd, err := strconv.Atoi(hostNetNSFdStr); err == nil && fd > 0 {
bind = NewHostBind(bind, fd)
}
}
}
wgDev := device.NewDevice(tunDev, bind, logger)
cleanups = append(cleanups, func() { wgDev.Close() })
// 3. Formulate the UAPI configuration string to configure peers/keys
uapiConf, err := buildUAPIConfig(cfg)
if err != nil {
return nil, fmt.Errorf("failed to build UAPI config: %w", err)
}
if err := wgDev.IpcSet(uapiConf); err != nil {
return nil, fmt.Errorf("failed to configure WireGuard device: %w", err)
}
if err := wgDev.Up(); err != nil {
return nil, fmt.Errorf("failed to bring up WireGuard device: %w", err)
}
// 4. Configure network interface using standard Linux network commands (iproute2)
if err := configureInterface(tunName, cfg.Address, mtu); err != nil {
return nil, fmt.Errorf("failed to configure network interface %s: %w", tunName, err)
}
if err := ConfigureResolvConf(dnsServer); err != nil {
fmt.Printf("warning: failed to configure DNS resolver: %v\n", err)
}
return &Tunnel{
Device: wgDev,
Tun: tunDev,
}, nil
}
// Close shuts down the userspace WireGuard device and closes the TUN interface.
func (t *Tunnel) Close() {
if t.Device != nil {
t.Device.Close()
}
}
// keyToHex ensures a WireGuard key is in hexadecimal format, converting from base64 if needed.
func keyToHex(key string) (string, error) {
// Try base64 decoding first
decoded, err := base64.StdEncoding.DecodeString(key)
if err == nil && len(decoded) == 32 {
return hex.EncodeToString(decoded), nil
}
// Try decoding as hex
if len(key) == 64 {
if _, err := hex.DecodeString(key); err == nil {
return strings.ToLower(key), nil
}
}
return "", fmt.Errorf("key is neither valid base64 nor hex 32-byte key: %s", key)
}
// buildUAPIConfig translates our wgconf.Config into the standard WireGuard UAPI format
func buildUAPIConfig(cfg *wgconf.Config) (string, error) {
var sb strings.Builder
// Global section
if cfg.PrivateKey != "" {
hexKey, err := keyToHex(cfg.PrivateKey)
if err != nil {
return "", fmt.Errorf("invalid PrivateKey: %w", err)
}
_, _ = fmt.Fprintf(&sb, "private_key=%s\n", hexKey)
}
// If there are existing peers, remove them first to have a clean state
sb.WriteString("replace_peers=true\n")
// Peer sections
for _, peer := range cfg.Peers {
if peer.PublicKey == "" {
continue
}
hexKey, err := keyToHex(peer.PublicKey)
if err != nil {
return "", fmt.Errorf("invalid Peer PublicKey: %w", err)
}
_, _ = fmt.Fprintf(&sb, "public_key=%s\n", hexKey)
if peer.Endpoint != "" {
_, _ = fmt.Fprintf(&sb, "endpoint=%s\n", peer.Endpoint)
}
for _, allowedIP := range peer.AllowedIPs {
trimmed := strings.TrimSpace(allowedIP)
if trimmed != "" {
_, _ = fmt.Fprintf(&sb, "allowed_ip=%s\n", trimmed)
}
}
}
return sb.String(), nil
}
// configureInterface uses the 'ip' command to set address, MTU, and default routing table
func configureInterface(name, address string, mtu int) error {
// Set MTU and bring up link
// ip link set dev tun0 mtu 1420 up
cmd := exec.Command("ip", "link", "set", "dev", name, "mtu", fmt.Sprintf("%d", mtu), "up")
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to set link %s state/mtu: %v", name, err)
}
// Add IP address
// ip addr add <address> dev tun0
cmd = exec.Command("ip", "addr", "add", address, "dev", name)
if _, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to add address %s to link %s: %v", address, name, err)
}
// Set default route or peer routes.
// For transparent userspace tunneling inside an isolated network namespace,
// we route all traffic (0.0.0.0/0) through our TUN device 'tun0'.
cmd = exec.Command("ip", "route", "add", "default", "dev", name)
if err := cmd.Run(); err != nil {
// If a default route already exists, we replace it or log the warning
// We try to replace first
cmdReplace := exec.Command("ip", "route", "replace", "default", "dev", name)
if errReplace := cmdReplace.Run(); errReplace != nil {
return fmt.Errorf("failed to configure default route to %s: %v", name, errReplace)
}
}
return nil
}
// GetTunnelLocalIP extracts the local IP address (without CIDR) from the config.
func GetTunnelLocalIP(cfg *wgconf.Config) (string, error) {
if cfg.Address == "" {
return "", fmt.Errorf("profile has no Address configured")
}
parts := strings.Split(cfg.Address, "/")
ipStr := parts[0]
ip, err := netip.ParseAddr(ipStr)
if err != nil {
return "", fmt.Errorf("invalid IP address in config '%s': %w", ipStr, err)
}
return ip.String(), nil
}
func ConfigureResolvConf(dns string) error {
if dns == "" {
return nil
}
tmpFile, err := os.CreateTemp("", "resolvconf")
if err != nil {
return fmt.Errorf("failed to create temp resolv.conf: %w", err)
}
launcherPath := tmpFile.Name()
defer func() {
_ = tmpFile.Close()
_ = os.Remove(launcherPath)
}()
content := fmt.Sprintf("nameserver %s\n", dns)
if _, err := tmpFile.WriteString(content); err != nil {
return fmt.Errorf("failed to write to temp resolv.conf: %w", err)
}
if err := unix.Mount(launcherPath, "/etc/resolv.conf", "", unix.MS_BIND, ""); err != nil {
return fmt.Errorf("failed to bind-mount %s to /etc/resolv.conf: %w", launcherPath, err)
}
if err := unix.Mount("", "/etc/resolv.conf", "", unix.MS_PRIVATE, ""); err != nil {
fmt.Printf("warning: failed to make /etc/resolv.conf mount private: %v\n", err)
}
return nil
}
// BlockHostServices blocks local D-Bus and name service cache daemon (nscd) sockets
// inside the mount namespace. This prevents glibc from bypassing the network namespace
// isolation via host services (e.g. systemd-resolved via D-Bus).
func BlockHostServices() error {
tmpDir, err := os.MkdirTemp("", "wg-wrap-block-")
if err != nil {
return fmt.Errorf("failed to create temp dir: %w", err)
}
defer os.RemoveAll(tmpDir)
tmpFile, err := os.CreateTemp("", "wg-wrap-block-file-")
if err != nil {
return fmt.Errorf("failed to create temp file: %w", err)
}
tmpFileName := tmpFile.Name()
_ = tmpFile.Close()
defer os.Remove(tmpFileName)
pathsToBlock := []string{
"/run/dbus/system_bus_socket",
"/run/systemd/resolve/io.systemd.Resolve",
"/run/systemd/resolve/io.systemd.Resolve.Monitor",
"/run/nscd/socket",
"/var/run/dbus/system_bus_socket",
"/var/run/systemd/resolve/io.systemd.Resolve",
"/var/run/systemd/resolve/io.systemd.Resolve.Monitor",
"/var/run/nscd/socket",
}
for _, p := range pathsToBlock {
stat, err := os.Stat(p)
if err == nil {
source := tmpFileName
if stat.IsDir() {
source = tmpDir
}
if err := unix.Mount(source, p, "", unix.MS_BIND, ""); err != nil {
fmt.Printf("warning: failed to bind-mount block over %s: %v\n", p, err)
} else {
_ = unix.Mount("", p, "", unix.MS_PRIVATE, "")
}
}
}
return nil
}
// HostBind wraps a standard conn.Bind so that its socket creation (Open)
// is forced to execute within a host network namespace.
type HostBind struct {
inner conn.Bind
hostNetNSFd int
}
func NewHostBind(inner conn.Bind, hostNetNSFd int) *HostBind {
return &HostBind{inner: inner, hostNetNSFd: hostNetNSFd}
}
func (h *HostBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Open/save a reference to our current isolated network namespace to switch back to.
isolatedFd, err := unix.Open("/proc/self/ns/net", unix.O_RDONLY, 0)
if err != nil {
return nil, 0, fmt.Errorf("failed to open isolated netns: %w", err)
}
defer func() { _ = unix.Close(isolatedFd) }()
// Temporarily switch this thread to the host network namespace
if err := unix.Setns(h.hostNetNSFd, unix.CLONE_NEWNET); err != nil {
return nil, 0, fmt.Errorf("failed to join host netns: %w", err)
}
// Sockets are opened in the host network namespace!
fns, actualPort, err = h.inner.Open(port)
if err != nil {
return nil, 0, fmt.Errorf("failed to open sockets in host netns: %w", err)
}
// Switch this thread back to the isolated network namespace
if err := unix.Setns(isolatedFd, unix.CLONE_NEWNET); err != nil {
_ = h.inner.Close()
// CRITICAL: The thread is stuck in the host network namespace. Returning it to the Go runtime pool
// will cause other goroutines to run in the host namespace, breaching isolation. We must panic
// immediately to abort the process and prevent a namespace escape.
panic(fmt.Sprintf("CRITICAL: failed to restore isolated netns: %v", err))
}
return fns, actualPort, nil
}
func (h *HostBind) Close() error {
return h.inner.Close()
}
func (h *HostBind) SetMark(mark uint32) error {
return h.inner.SetMark(mark)
}
func (h *HostBind) Send(bufs [][]byte, endpoint conn.Endpoint) error {
// Linux socket routing maps to the namespace in which the socket was created,
// so h.inner.Send will automatically route via host namespace without Setns here!
return h.inner.Send(bufs, endpoint)
}
func (h *HostBind) ParseEndpoint(s string) (conn.Endpoint, error) {
return h.inner.ParseEndpoint(s)
}
func (h *HostBind) BatchSize() int {
return h.inner.BatchSize()
}
// FDBind implements the conn.Bind interface by wrapping a pre-opened
// host UDP socket file descriptor. This allows unprivileged processes inside
// network namespaces to communicate over the host network loop.
type FDBind struct {
originalFd int
conn *net.UDPConn
}
type FDEndpoint struct {
addr netip.AddrPort
}
func (e *FDEndpoint) DstIP() netip.Addr {
return e.addr.Addr()
}
func (e *FDEndpoint) DstToString() string {
return e.addr.String()
}
func (e *FDEndpoint) DstToBytes() []byte {
return e.addr.Addr().AsSlice()
}
func (e *FDEndpoint) ClearSrc() {}
func (e *FDEndpoint) SrcIP() netip.Addr {
return netip.Addr{}
}
func (e *FDEndpoint) SrcToString() string {
return ""
}
func (e *FDEndpoint) SrcIfidx() int32 {
return 0
}
func NewFDBind(fd int) (*FDBind, error) {
return &FDBind{originalFd: fd}, nil
}
func (b *FDBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) {
// Duplicate the original fd so we can close the duplicated socket during
// transitions or shutdown, while preserving the ability to re-open/re-bind it later.
dupFd, err := unix.Dup(b.originalFd)
if err != nil {
return nil, 0, fmt.Errorf("failed to duplicate host socket fd: %w", err)
}
file := os.NewFile(uintptr(dupFd), "host-udp-socket")
pconn, err := net.FilePacketConn(file)
if err != nil {
_ = file.Close()
return nil, 0, fmt.Errorf("failed to wrap fd %d as packet conn: %w", dupFd, err)
}
udpConn, ok := pconn.(*net.UDPConn)
if !ok {
_ = pconn.Close()
return nil, 0, fmt.Errorf("fd %d is not a UDP socket", dupFd)
}
b.conn = udpConn
laddr, ok := b.conn.LocalAddr().(*net.UDPAddr)
if !ok {
return nil, 0, fmt.Errorf("local address is not a UDP address")
}
actualPort = uint16(laddr.Port)
receive := func(packets [][]byte, sizes []int, eps []conn.Endpoint) (n int, err error) {
if len(packets) == 0 {
return 0, nil
}
if b.conn == nil {
return 0, net.ErrClosed
}
nBytes, addr, err := b.conn.ReadFromUDP(packets[0])
if err != nil {
return 0, err
}
sizes[0] = nBytes
addrPort := addr.AddrPort()
eps[0] = &FDEndpoint{addr: addrPort}
return 1, nil
}
return []conn.ReceiveFunc{receive}, actualPort, nil
}
func (b *FDBind) Close() error {
if b.conn != nil {
err := b.conn.Close()
b.conn = nil
return err
}
return nil
}
func (b *FDBind) SetMark(mark uint32) error {
return nil
}
func (b *FDBind) Send(bufs [][]byte, endpoint conn.Endpoint) error {
if b.conn == nil {
return net.ErrClosed
}
addrPort, err := netip.ParseAddrPort(endpoint.DstToString())
if err != nil {
return fmt.Errorf("failed to parse destination endpoint %s: %w", endpoint.DstToString(), err)
}
addr := net.UDPAddrFromAddrPort(addrPort)
for _, buf := range bufs {
_, err := b.conn.WriteToUDP(buf, addr)
if err != nil {
return fmt.Errorf("failed to write to UDP socket: %w", err)
}
}
return nil
}
func (b *FDBind) ParseEndpoint(s string) (conn.Endpoint, error) {
addrPort, err := netip.ParseAddrPort(s)
if err != nil {
return nil, fmt.Errorf("failed to parse endpoint address %s: %w", s, err)
}
return &FDEndpoint{addr: addrPort}, nil
}
func (b *FDBind) BatchSize() int {
return 1
}
|