| 1 |
//go:build linux |
| 2 |
|
| 3 |
package socket |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"runtime" |
| 10 |
|
| 11 |
"golang.org/x/sync/errgroup" |
| 12 |
"golang.org/x/sys/unix" |
| 13 |
) |
| 14 |
|
| 15 |
// errNetNSDisabled is returned when network namespaces are unavailable on |
| 16 |
// a given system. |
| 17 |
var errNetNSDisabled = errors.New("socket: Linux network namespaces are not enabled on this system") |
| 18 |
|
| 19 |
// withNetNS invokes fn within the context of the network namespace specified by |
| 20 |
// fd, while also managing the logic required to safely do so by manipulating |
| 21 |
// thread-local state. |
| 22 |
func withNetNS(fd int, fn func() (*Conn, error)) (*Conn, error) { |
| 23 |
var ( |
| 24 |
eg errgroup.Group |
| 25 |
conn *Conn |
| 26 |
) |
| 27 |
|
| 28 |
eg.Go(func() error { |
| 29 |
// Retrieve and store the calling OS thread's network namespace so the |
| 30 |
// thread can be reassigned to it after creating a socket in another network |
| 31 |
// namespace. |
| 32 |
runtime.LockOSThread() |
| 33 |
|
| 34 |
ns, err := threadNetNS() |
| 35 |
if err != nil { |
| 36 |
// No thread-local manipulation, unlock. |
| 37 |
runtime.UnlockOSThread() |
| 38 |
return err |
| 39 |
} |
| 40 |
defer ns.Close() |
| 41 |
|
| 42 |
// Beyond this point, the thread's network namespace is poisoned. Do not |
| 43 |
// unlock the OS thread until all network namespace manipulation completes |
| 44 |
// to avoid returning to the caller with altered thread-local state. |
| 45 |
|
| 46 |
// Assign the current OS thread the goroutine is locked to to the given |
| 47 |
// network namespace. |
| 48 |
if err := ns.Set(fd); err != nil { |
| 49 |
return err |
| 50 |
} |
| 51 |
|
| 52 |
// Attempt Conn creation and unconditionally restore the original namespace. |
| 53 |
c, err := fn() |
| 54 |
if nerr := ns.Restore(); nerr != nil { |
| 55 |
// Failed to restore original namespace. Return an error and allow the |
| 56 |
// runtime to terminate the thread. |
| 57 |
if err == nil { |
| 58 |
_ = c.Close() |
| 59 |
} |
| 60 |
|
| 61 |
return nerr |
| 62 |
} |
| 63 |
|
| 64 |
// No more thread-local state manipulation; return the new Conn. |
| 65 |
runtime.UnlockOSThread() |
| 66 |
conn = c |
| 67 |
return err |
| 68 |
}) |
| 69 |
|
| 70 |
if err := eg.Wait(); err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
|
| 74 |
return conn, nil |
| 75 |
} |
| 76 |
|
| 77 |
// A netNS is a handle that can manipulate network namespaces. |
| 78 |
// |
| 79 |
// Operations performed on a netNS must use runtime.LockOSThread before |
| 80 |
// manipulating any network namespaces. |
| 81 |
type netNS struct { |
| 82 |
// The handle to a network namespace. |
| 83 |
f *os.File |
| 84 |
|
| 85 |
// Indicates if network namespaces are disabled on this system, and thus |
| 86 |
// operations should become a no-op or return errors. |
| 87 |
disabled bool |
| 88 |
} |
| 89 |
|
| 90 |
// threadNetNS constructs a netNS using the network namespace of the calling |
| 91 |
// thread. If the namespace is not the default namespace, runtime.LockOSThread |
| 92 |
// should be invoked first. |
| 93 |
func threadNetNS() (*netNS, error) { |
| 94 |
return fileNetNS(fmt.Sprintf("/proc/self/task/%d/ns/net", unix.Gettid())) |
| 95 |
} |
| 96 |
|
| 97 |
// fileNetNS opens file and creates a netNS. fileNetNS should only be called |
| 98 |
// directly in tests. |
| 99 |
func fileNetNS(file string) (*netNS, error) { |
| 100 |
f, err := os.Open(file) |
| 101 |
switch { |
| 102 |
case err == nil: |
| 103 |
return &netNS{f: f}, nil |
| 104 |
case os.IsNotExist(err): |
| 105 |
// Network namespaces are not enabled on this system. Use this signal |
| 106 |
// to return errors elsewhere if the caller explicitly asks for a |
| 107 |
// network namespace to be set. |
| 108 |
return &netNS{disabled: true}, nil |
| 109 |
default: |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// Close releases the handle to a network namespace. |
| 115 |
func (n *netNS) Close() error { |
| 116 |
return n.do(func() error { return n.f.Close() }) |
| 117 |
} |
| 118 |
|
| 119 |
// FD returns a file descriptor which represents the network namespace. |
| 120 |
func (n *netNS) FD() int { |
| 121 |
if n.disabled { |
| 122 |
// No reasonable file descriptor value in this case, so specify a |
| 123 |
// non-existent one. |
| 124 |
return -1 |
| 125 |
} |
| 126 |
|
| 127 |
return int(n.f.Fd()) |
| 128 |
} |
| 129 |
|
| 130 |
// Restore restores the original network namespace for the calling thread. |
| 131 |
func (n *netNS) Restore() error { |
| 132 |
return n.do(func() error { return n.Set(n.FD()) }) |
| 133 |
} |
| 134 |
|
| 135 |
// Set sets a new network namespace for the current thread using fd. |
| 136 |
func (n *netNS) Set(fd int) error { |
| 137 |
return n.do(func() error { |
| 138 |
return os.NewSyscallError("setns", unix.Setns(fd, unix.CLONE_NEWNET)) |
| 139 |
}) |
| 140 |
} |
| 141 |
|
| 142 |
// do runs fn if network namespaces are enabled on this system. |
| 143 |
func (n *netNS) do(fn func() error) error { |
| 144 |
if n.disabled { |
| 145 |
return errNetNSDisabled |
| 146 |
} |
| 147 |
|
| 148 |
return fn() |
| 149 |
} |