| 1 | //go:build linux |
| 2 | |
| 3 | package render |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "net" |
| 11 | "net/http" |
| 12 | "os" |
| 13 | "os/exec" |
| 14 | "runtime" |
| 15 | "syscall" |
| 16 | "time" |
| 17 | |
| 18 | "golang.org/x/sys/unix" |
| 19 | ) |
| 20 | |
| 21 | // SandboxMain is the sandbox's first process, the runner's render-sandbox |
| 22 | // subcommand. Bubblewrap gives it the browser's pipe on descriptors 3 and |
| 23 | // 4 and its status pipe on 5. It serves /site on 127.0.0.1, starts the |
| 24 | // browser with that pipe and HOME=/tmp (its environment is otherwise |
| 25 | // empty), reports ready with the site's address, then the browser's exit, |
| 26 | // and ends. It takes no arguments and reads nothing else from the host. |
| 27 | // Exit 0 is a browser that started and ended; 1 a failure it reported; 2 |
| 28 | // a launch that was not the runner's (arguments, or descriptors 3 to 5). |
| 29 | func SandboxMain(args []string, stderr io.Writer) int { |
| 30 | if len(args) != 0 { |
| 31 | return 2 |
| 32 | } |
| 33 | // Only 0, 1, 2, the browser's pipe and the status pipe stay open, |
| 34 | // all close-on-exec: the browser gets 0, 1, 2 and its pipe |
| 35 | // (ExtraFiles) and nothing else, not the status pipe or the server's |
| 36 | // socket. |
| 37 | if sandboxDescriptors() != nil { |
| 38 | return 2 |
| 39 | } |
| 40 | statusPipe := os.NewFile(fdStatus, "status") |
| 41 | report := func(st status) { |
| 42 | b, _ := json.Marshal(st) |
| 43 | statusPipe.Write(append(b, '\n')) |
| 44 | } |
| 45 | // Every process from here on, the browser's included, has the |
| 46 | // call's memory limit (memory_linux.go). |
| 47 | if err := limitMemory(); err != nil { |
| 48 | fmt.Fprintf(stderr, "render-sandbox: memory limit: %v\n", err) |
| 49 | report(status{Event: "failed", Reason: reasonLimits}) |
| 50 | return 1 |
| 51 | } |
| 52 | server, site, err := serveSite() |
| 53 | if err != nil { |
| 54 | fmt.Fprintf(stderr, "render-sandbox: site: %v\n", err) |
| 55 | report(status{Event: "failed", Reason: reasonSite}) |
| 56 | return 1 |
| 57 | } |
| 58 | defer server.Close() |
| 59 | browserIn, browserOut := os.NewFile(fdBrowserIn, "browser-in"), os.NewFile(fdBrowserOut, "browser-out") |
| 60 | cmd := exec.Command(BrowserPath, browserArgs()...) |
| 61 | cmd.Env = []string{"HOME=" + browserHome} |
| 62 | cmd.Dir = browserHome |
| 63 | cmd.Stdout = os.Stdout |
| 64 | cmd.Stderr = os.Stderr |
| 65 | cmd.ExtraFiles = []*os.File{browserIn, browserOut} |
| 66 | cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} |
| 67 | runtime.LockOSThread() |
| 68 | err = cmd.Start() |
| 69 | // The browser holds its pipe now; this process keeps no end of it, |
| 70 | // so the runner sees the pipe close when the browser ends. |
| 71 | browserIn.Close() |
| 72 | browserOut.Close() |
| 73 | if err != nil { |
| 74 | fmt.Fprintf(stderr, "render-sandbox: browser: %v\n", err) |
| 75 | report(status{Event: "failed", Reason: reasonBrowser}) |
| 76 | return 1 |
| 77 | } |
| 78 | report(status{Event: "ready", Site: site}) |
| 79 | err = cmd.Wait() |
| 80 | st := status{Event: "exit"} |
| 81 | var exit *exec.ExitError |
| 82 | if errors.As(err, &exit) { |
| 83 | if ws, ok := exit.Sys().(syscall.WaitStatus); ok && ws.Signaled() { |
| 84 | st.Signal = unix.SignalName(ws.Signal()) |
| 85 | } else { |
| 86 | st.Code = exit.ExitCode() |
| 87 | } |
| 88 | } else if err != nil { |
| 89 | st.Code = -1 |
| 90 | } |
| 91 | report(st) |
| 92 | return 0 |
| 93 | } |
| 94 | |
| 95 | // serveSite serves /site, read only, on 127.0.0.1 at a port the kernel |
| 96 | // picks. Only GET and HEAD are answered; os.Root keeps every request |
| 97 | // inside /site. |
| 98 | func serveSite() (*http.Server, string, error) { |
| 99 | root, err := os.OpenRoot(SiteDir) |
| 100 | if err != nil { |
| 101 | return nil, "", err |
| 102 | } |
| 103 | ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 104 | if err != nil { |
| 105 | root.Close() |
| 106 | return nil, "", err |
| 107 | } |
| 108 | files := http.FileServerFS(root.FS()) |
| 109 | server := &http.Server{ |
| 110 | Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 111 | if r.Method != http.MethodGet && r.Method != http.MethodHead { |
| 112 | w.WriteHeader(http.StatusMethodNotAllowed) |
| 113 | return |
| 114 | } |
| 115 | files.ServeHTTP(w, r) |
| 116 | }), |
| 117 | ReadHeaderTimeout: 10 * time.Second, |
| 118 | MaxHeaderBytes: 64 << 10, |
| 119 | } |
| 120 | go server.Serve(ln) |
| 121 | return server, fmt.Sprintf("http://%s/", ln.Addr()), nil |
| 122 | } |
| 123 | |