//go:build linux package render import ( "math" "path/filepath" "slices" "strconv" "golang.org/x/sys/unix" ) // fdKeep is the last descriptor bubblewrap is started with: 0, 1 and 2, // the browser and status pipes (3 to 5), the four binds (6 to 9) and // bubblewrap's info pipe (10). const fdKeep = fdInfo // ExecMain is the runner's render-exec subcommand: the process the // launcher starts, which closes every descriptor above fdKeep and // executes bubblewrap (args[0], an absolute path) with the render policy // and args[1:] as the sandbox's first process. // // Go cannot run code between fork and exec, and a descriptor some other // goroutine opens without close-on-exec just before the launcher forks is // inherited by the child. This process is that child: whatever it // inherited above the launch's own descriptors it closes before // bubblewrap, which passes inherited descriptors on, ever runs. It // returns only on failure. func ExecMain(args []string) int { if len(args) < 2 || !filepath.IsAbs(args[0]) || filepath.Clean(args[0]) != args[0] || args[1] == "" { return 2 } if closeFrom(fdKeep+1) != nil { return 2 } for fd := 3; fd <= fdKeep; fd++ { if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); err != nil { return 2 } } unix.Exec(args[0], append([]string{args[0]}, bwrapArgs(args[1:]...)...), []string{}) return 2 } // sandboxDescriptors is render-sandbox's first step: descriptors 3, 4 // and 5 must be open, every other one above 2 is closed, and those three // are made close-on-exec (the browser gets 3 and 4 back through // ExtraFiles, never 5). func sandboxDescriptors() error { for _, fd := range []int{fdBrowserIn, fdBrowserOut, fdStatus} { if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); err != nil { return err } } if err := closeFrom(fdStatus + 1); err != nil { return err } for _, fd := range []int{fdBrowserIn, fdBrowserOut, fdStatus} { if _, err := unix.FcntlInt(uintptr(fd), unix.F_SETFD, unix.FD_CLOEXEC); err != nil { return err } } return nil } // closeFrom closes every descriptor from first up. func closeFrom(first int) error { if unix.CloseRange(uint(first), math.MaxUint32, 0) == nil { return nil } // Kernels before 5.8: one at a time. return closeEach(first) } // closeEach is closeFrom by /proc/self/fd, one descriptor at a time. func closeEach(first int) error { fds, err := openDescriptors() if err != nil { return err } for _, fd := range fds { if fd >= first { unix.Close(fd) } } return nil } // openDescriptors lists this process's open descriptors, from // /proc/self/fd read with raw system calls (os.Open would start Go's // poller and add its own), without the one used to read it. func openDescriptors() ([]int, error) { d, err := unix.Open("/proc/self/fd", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) if err != nil { return nil, err } defer unix.Close(d) var names []string buf := make([]byte, 8192) for { n, err := unix.ReadDirent(d, buf) if err != nil { return nil, err } if n == 0 { break } _, _, names = unix.ParseDirent(buf[:n], -1, names) } var fds []int for _, name := range names { if n, err := strconv.Atoi(name); err == nil && n != d { fds = append(fds, n) } } slices.Sort(fds) return fds, nil }