//go:build linux package render import ( "encoding/json" "errors" "fmt" "io" "net" "net/http" "os" "os/exec" "runtime" "syscall" "time" "golang.org/x/sys/unix" ) // SandboxMain is the sandbox's first process, the runner's render-sandbox // subcommand. Bubblewrap gives it the browser's pipe on descriptors 3 and // 4 and its status pipe on 5. It serves /site on 127.0.0.1, starts the // browser with that pipe and HOME=/tmp (its environment is otherwise // empty), reports ready with the site's address, then the browser's exit, // and ends. It takes no arguments and reads nothing else from the host. // Exit 0 is a browser that started and ended; 1 a failure it reported; 2 // a launch that was not the runner's (arguments, or descriptors 3 to 5). func SandboxMain(args []string, stderr io.Writer) int { if len(args) != 0 { return 2 } // Only 0, 1, 2, the browser's pipe and the status pipe stay open, // all close-on-exec: the browser gets 0, 1, 2 and its pipe // (ExtraFiles) and nothing else, not the status pipe or the server's // socket. if sandboxDescriptors() != nil { return 2 } statusPipe := os.NewFile(fdStatus, "status") report := func(st status) { b, _ := json.Marshal(st) statusPipe.Write(append(b, '\n')) } // Every process from here on, the browser's included, has the // call's memory limit (memory_linux.go). if err := limitMemory(); err != nil { fmt.Fprintf(stderr, "render-sandbox: memory limit: %v\n", err) report(status{Event: "failed", Reason: reasonLimits}) return 1 } server, site, err := serveSite() if err != nil { fmt.Fprintf(stderr, "render-sandbox: site: %v\n", err) report(status{Event: "failed", Reason: reasonSite}) return 1 } defer server.Close() browserIn, browserOut := os.NewFile(fdBrowserIn, "browser-in"), os.NewFile(fdBrowserOut, "browser-out") cmd := exec.Command(BrowserPath, browserArgs()...) cmd.Env = []string{"HOME=" + browserHome} cmd.Dir = browserHome cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.ExtraFiles = []*os.File{browserIn, browserOut} cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} runtime.LockOSThread() err = cmd.Start() // The browser holds its pipe now; this process keeps no end of it, // so the runner sees the pipe close when the browser ends. browserIn.Close() browserOut.Close() if err != nil { fmt.Fprintf(stderr, "render-sandbox: browser: %v\n", err) report(status{Event: "failed", Reason: reasonBrowser}) return 1 } report(status{Event: "ready", Site: site}) err = cmd.Wait() st := status{Event: "exit"} var exit *exec.ExitError if errors.As(err, &exit) { if ws, ok := exit.Sys().(syscall.WaitStatus); ok && ws.Signaled() { st.Signal = unix.SignalName(ws.Signal()) } else { st.Code = exit.ExitCode() } } else if err != nil { st.Code = -1 } report(st) return 0 } // serveSite serves /site, read only, on 127.0.0.1 at a port the kernel // picks. Only GET and HEAD are answered; os.Root keeps every request // inside /site. func serveSite() (*http.Server, string, error) { root, err := os.OpenRoot(SiteDir) if err != nil { return nil, "", err } ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { root.Close() return nil, "", err } files := http.FileServerFS(root.FS()) server := &http.Server{ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet && r.Method != http.MethodHead { w.WriteHeader(http.StatusMethodNotAllowed) return } files.ServeHTTP(w, r) }), ReadHeaderTimeout: 10 * time.Second, MaxHeaderBytes: 64 << 10, } go server.Serve(ln) return server, fmt.Sprintf("http://%s/", ln.Addr()), nil }