oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package render
4
5import (
6 "math"
7 "path/filepath"
8 "slices"
9 "strconv"
10
11 "golang.org/x/sys/unix"
12)
13
14// fdKeep is the last descriptor bubblewrap is started with: 0, 1 and 2,
15// the browser and status pipes (3 to 5), the four binds (6 to 9) and
16// bubblewrap's info pipe (10).
17const fdKeep = fdInfo
18
19// ExecMain is the runner's render-exec subcommand: the process the
20// launcher starts, which closes every descriptor above fdKeep and
21// executes bubblewrap (args[0], an absolute path) with the render policy
22// and args[1:] as the sandbox's first process.
23//
24// Go cannot run code between fork and exec, and a descriptor some other
25// goroutine opens without close-on-exec just before the launcher forks is
26// inherited by the child. This process is that child: whatever it
27// inherited above the launch's own descriptors it closes before
28// bubblewrap, which passes inherited descriptors on, ever runs. It
29// returns only on failure.
30func ExecMain(args []string) int {
31 if len(args) < 2 || !filepath.IsAbs(args[0]) || filepath.Clean(args[0]) != args[0] || args[1] == "" {
32 return 2
33 }
34 if closeFrom(fdKeep+1) != nil {
35 return 2
36 }
37 for fd := 3; fd <= fdKeep; fd++ {
38 if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); err != nil {
39 return 2
40 }
41 }
42 unix.Exec(args[0], append([]string{args[0]}, bwrapArgs(args[1:]...)...), []string{})
43 return 2
44}
45
46// sandboxDescriptors is render-sandbox's first step: descriptors 3, 4
47// and 5 must be open, every other one above 2 is closed, and those three
48// are made close-on-exec (the browser gets 3 and 4 back through
49// ExtraFiles, never 5).
50func sandboxDescriptors() error {
51 for _, fd := range []int{fdBrowserIn, fdBrowserOut, fdStatus} {
52 if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); err != nil {
53 return err
54 }
55 }
56 if err := closeFrom(fdStatus + 1); err != nil {
57 return err
58 }
59 for _, fd := range []int{fdBrowserIn, fdBrowserOut, fdStatus} {
60 if _, err := unix.FcntlInt(uintptr(fd), unix.F_SETFD, unix.FD_CLOEXEC); err != nil {
61 return err
62 }
63 }
64 return nil
65}
66
67// closeFrom closes every descriptor from first up.
68func closeFrom(first int) error {
69 if unix.CloseRange(uint(first), math.MaxUint32, 0) == nil {
70 return nil
71 }
72 // Kernels before 5.8: one at a time.
73 return closeEach(first)
74}
75
76// closeEach is closeFrom by /proc/self/fd, one descriptor at a time.
77func closeEach(first int) error {
78 fds, err := openDescriptors()
79 if err != nil {
80 return err
81 }
82 for _, fd := range fds {
83 if fd >= first {
84 unix.Close(fd)
85 }
86 }
87 return nil
88}
89
90// openDescriptors lists this process's open descriptors, from
91// /proc/self/fd read with raw system calls (os.Open would start Go's
92// poller and add its own), without the one used to read it.
93func openDescriptors() ([]int, error) {
94 d, err := unix.Open("/proc/self/fd", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
95 if err != nil {
96 return nil, err
97 }
98 defer unix.Close(d)
99 var names []string
100 buf := make([]byte, 8192)
101 for {
102 n, err := unix.ReadDirent(d, buf)
103 if err != nil {
104 return nil, err
105 }
106 if n == 0 {
107 break
108 }
109 _, _, names = unix.ParseDirent(buf[:n], -1, names)
110 }
111 var fds []int
112 for _, name := range names {
113 if n, err := strconv.Atoi(name); err == nil && n != d {
114 fds = append(fds, n)
115 }
116 }
117 slices.Sort(fds)
118 return fds, nil
119}
120