//go:build linux package webread import ( "errors" "fmt" "runtime" "unsafe" "golang.org/x/sys/unix" ) // The fetcher starts no other program, and it may not: a compromised // parser could otherwise multiply its per-process memory and CPU bounds, // and keep the host network, in children. noChildren installs a seccomp // filter on every thread of the process (TSYNC) before the fetcher reads // its plan: fork, vfork, execve and execveat fail with EPERM; clone3 // fails with ENOSYS (so the Go runtime, should it ever try clone3, falls // back to clone); clone fails with EPERM unless it makes a thread // (CLONE_THREAD). Everything else is allowed. A system call made under // another architecture's numbering ends the process. On an architecture // the filter does not know (auditArch is 0), the fetcher refuses to run // rather than run unfiltered. // errNoFilter: this architecture has no filter, so the fetcher does not run. var errNoFilter = errors.New("webread: no system call filter for " + runtime.GOARCH) // x32Bit marks x86-64's x32 system call numbers. const x32Bit = 0x40000000 // Offsets into struct seccomp_data: nr, arch, then args[0]'s low 32 bits // (both known architectures are little-endian). const ( offNr = 0 offArch = 4 offArg0 = 16 filterOK = unix.SECCOMP_RET_ALLOW ) // insn is one BPF instruction whose jumps name labels; label marks the // instruction others jump to. type insn struct { f unix.SockFilter jt, jf string label string } // childFilter is the filter's program for this architecture. func childFilter() ([]unix.SockFilter, error) { if auditArch == 0 { return nil, errNoFilter } ld := func(off uint32) insn { return insn{f: unix.SockFilter{Code: unix.BPF_LD | unix.BPF_W | unix.BPF_ABS, K: off}} } jeq := func(k uint32, jt, jf string) insn { return insn{f: unix.SockFilter{Code: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, K: k}, jt: jt, jf: jf} } ret := func(label string, k uint32) insn { return insn{f: unix.SockFilter{Code: unix.BPF_RET | unix.BPF_K, K: k}, label: label} } prog := []insn{ ld(offArch), jeq(auditArch, "", "kill"), ld(offNr), } if checkX32 { prog = append(prog, insn{f: unix.SockFilter{Code: unix.BPF_JMP | unix.BPF_JGE | unix.BPF_K, K: x32Bit}, jt: "eperm"}) } for _, nr := range deniedSyscalls { prog = append(prog, jeq(nr, "eperm", "")) } prog = append(prog, jeq(unix.SYS_CLONE3, "enosys", ""), jeq(unix.SYS_CLONE, "", "allow"), ld(offArg0), insn{f: unix.SockFilter{Code: unix.BPF_JMP | unix.BPF_JSET | unix.BPF_K, K: unix.CLONE_THREAD}, jt: "allow", jf: "eperm"}, ret("allow", filterOK), ret("eperm", unix.SECCOMP_RET_ERRNO|uint32(unix.EPERM)), ret("enosys", unix.SECCOMP_RET_ERRNO|uint32(unix.ENOSYS)), ret("kill", unix.SECCOMP_RET_KILL_PROCESS), ) at := map[string]int{} for i, in := range prog { if in.label != "" { at[in.label] = i } } out := make([]unix.SockFilter, len(prog)) for i, in := range prog { f := in.f for _, j := range []struct { label string to *uint8 }{{in.jt, &f.Jt}, {in.jf, &f.Jf}} { if j.label == "" { continue } n := at[j.label] - i - 1 if n < 0 || n > 255 { return nil, fmt.Errorf("webread: filter jump to %s", j.label) } *j.to = uint8(n) } out[i] = f } return out, nil } // noChildren installs the filter on every thread of this process. It // cannot be removed afterwards (no_new_privs, and filters only add). func noChildren() error { prog, err := childFilter() if err != nil { return err } runtime.LockOSThread() defer runtime.UnlockOSThread() if err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); err != nil { return fmt.Errorf("webread: no_new_privs: %w", err) } fp := unix.SockFprog{Len: uint16(len(prog)), Filter: &prog[0]} r, _, errno := unix.Syscall(unix.SYS_SECCOMP, unix.SECCOMP_SET_MODE_FILTER, unix.SECCOMP_FILTER_FLAG_TSYNC, uintptr(unsafe.Pointer(&fp))) runtime.KeepAlive(prog) if errno != 0 { return fmt.Errorf("webread: seccomp: %w", errno) } if r != 0 { // TSYNC names a thread it could not synchronise. return fmt.Errorf("webread: seccomp: thread %d not synchronised", r) } return nil }