package host import ( "context" "errors" "fmt" "io" "net" "os" "os/exec" "os/signal" "runtime" "strconv" "strings" "syscall" "golang.org/x/sys/unix" ) // launcherArg makes any binary importing this package the confined // launcher: the extension's parent inside its network namespace. const launcherArg = "__extension-egress" // proxyAddr is where the launcher serves the extension's only way out. const proxyAddr = "127.0.0.1:3128" func init() { if len(os.Args) > 2 && os.Args[1] == launcherArg { os.Exit(launch(os.Args[2], os.Args[3:])) } } // confinedCommand re-execs this binary as the launcher in a fresh user and // network namespace. The namespace holds only loopback, so the child cannot // reach anything except through the launcher's relay to socket. Where the // kernel refuses the namespaces, the start fails: there is no unconfined // fallback. args reach the child as they were given to it. // // The process lives until ctx ends, which is the session's lifetime rather // than any one call's deadline; Pdeathsig ends it if this process dies. func confinedCommand(ctx context.Context, binary *os.File, args []string, socket string) (*exec.Cmd, error) { self, err := os.Executable() if err != nil { return nil, err } uid, gid := os.Getuid(), os.Getgid() launchArgs := append([]string{launcherArg, socket}, args...) //nolint:gosec // the binary is this one, named by path; the arguments are the accepted package's own. cmd := exec.CommandContext(ctx, self, launchArgs...) cmd.ExtraFiles = []*os.File{binary} cmd.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUSER | syscall.CLONE_NEWNET, UidMappings: []syscall.SysProcIDMap{{ContainerID: uid, HostID: uid, Size: 1}}, GidMappings: []syscall.SysProcIDMap{{ContainerID: gid, HostID: gid, Size: 1}}, // Only the launcher holds this, to bring loopback up; it clears it // before the extension starts. AmbientCaps: []uintptr{unix.CAP_NET_ADMIN}, Pdeathsig: syscall.SIGKILL, } return cmd, nil } // namespacesAvailable reports why this process cannot create the user and // network namespaces an extension runs in, or nil when it can. A kernel or a // container runtime that sets these limits to zero, which restricted Pod // Security Standards and hardened STIG profiles do, leaves remote endpoints as // the only way to run an extension. func namespacesAvailable() error { if err := namespaceLimit("/proc/sys/user/max_user_namespaces", "user namespaces"); err != nil { return err } return namespaceLimit("/proc/sys/user/max_net_namespaces", "network namespaces") } func namespaceLimit(path, what string) error { value, err := os.ReadFile(path) //nolint:gosec // fixed path under /proc/sys/user if err != nil { return fmt.Errorf("read the %s limit: %w", what, err) } n, err := strconv.Atoi(strings.TrimSpace(string(value))) if err != nil { return fmt.Errorf("parse the %s limit: %w", what, err) } if n == 0 { return fmt.Errorf("%s are disabled: %s is 0", what, path) } return nil } // openCommand runs the verified binary by its open descriptor with the host's // network, for a package that declares no egress hosts: a connector that // reaches hosts an operator configured, not hosts a manifest lists. func openCommand(ctx context.Context, binary *os.File, args []string) *exec.Cmd { path := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), binary.Fd()) //nolint:gosec // the binary is this one, named by descriptor; the arguments are the accepted package's own. cmd := exec.CommandContext(ctx, path, args...) cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} return cmd } // launch runs inside the namespace: loopback up, relay listening, then the // child from descriptor 3 with every capability gone. func launch(socket string, args []string) int { ctx := context.Background() if err := loopbackUp(); err != nil { fmt.Fprintln(os.Stderr, "extension egress: loopback:", err) return 1 } if socket != "" { ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", proxyAddr) if err != nil { fmt.Fprintln(os.Stderr, "extension egress: relay:", err) return 1 } go relay(ctx, ln, socket) } if err := unix.Prctl(unix.PR_CAP_AMBIENT, unix.PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0); err != nil { fmt.Fprintln(os.Stderr, "extension egress: drop capabilities:", err) return 1 } if err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); err != nil { fmt.Fprintln(os.Stderr, "extension egress: no_new_privs:", err) return 1 } syscall.CloseOnExec(3) return runExtension(ctx, fmt.Sprintf("/proc/%d/fd/3", os.Getpid()), args) } // runExtension starts the child from path and mirrors its exit. Pdeathsig ties // the child to this thread, so killing the launcher kills it. func runExtension(ctx context.Context, path string, args []string) int { runtime.LockOSThread() proxy := "http://" + proxyAddr //nolint:gosec // the binary is named by its verified descriptor; the arguments are the accepted package's own. cmd := exec.CommandContext(ctx, path, args...) // the host-verified binary, by descriptor cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr cmd.Env = append(os.Environ(), "HTTP_PROXY="+proxy, "HTTPS_PROXY="+proxy, "ALL_PROXY="+proxy, "http_proxy="+proxy, "https_proxy="+proxy, "all_proxy="+proxy, "NO_PROXY=", "no_proxy=") cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} if err := cmd.Start(); err != nil { fmt.Fprintln(os.Stderr, "extension egress: start:", err) return 1 } signals := make(chan os.Signal, 1) signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) go func() { for s := range signals { _ = cmd.Process.Signal(s) } }() err := cmd.Wait() if exit, ok := errors.AsType[*exec.ExitError](err); ok { return exit.ExitCode() } if err != nil { return 1 } return 0 } func loopbackUp() error { fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM|unix.SOCK_CLOEXEC, 0) if err != nil { return err } defer func() { _ = unix.Close(fd) }() ifr, err := unix.NewIfreq("lo") if err != nil { return err } if err := unix.IoctlIfreq(fd, unix.SIOCGIFFLAGS, ifr); err != nil { return err } ifr.SetUint16(ifr.Uint16() | unix.IFF_UP) return unix.IoctlIfreq(fd, unix.SIOCSIFFLAGS, ifr) } // relay carries each proxy connection to the host's gate socket. func relay(ctx context.Context, ln net.Listener, socket string) { for { conn, err := ln.Accept() if err != nil { return } go func() { defer func() { _ = conn.Close() }() upstream, err := (&net.Dialer{}).DialContext(ctx, "unix", socket) if err != nil { return } defer func() { _ = upstream.Close() }() go func() { _, _ = io.Copy(upstream, conn) closeWrite(upstream) }() _, _ = io.Copy(conn, upstream) }() } } func closeWrite(c net.Conn) { if cw, ok := c.(interface{ CloseWrite() error }); ok { _ = cw.CloseWrite() } }