| 1 | //go:build linux |
| 2 | |
| 3 | package webread |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "runtime" |
| 9 | "unsafe" |
| 10 | |
| 11 | "golang.org/x/sys/unix" |
| 12 | ) |
| 13 | |
| 14 | // The fetcher starts no other program, and it may not: a compromised |
| 15 | // parser could otherwise multiply its per-process memory and CPU bounds, |
| 16 | // and keep the host network, in children. noChildren installs a seccomp |
| 17 | // filter on every thread of the process (TSYNC) before the fetcher reads |
| 18 | // its plan: fork, vfork, execve and execveat fail with EPERM; clone3 |
| 19 | // fails with ENOSYS (so the Go runtime, should it ever try clone3, falls |
| 20 | // back to clone); clone fails with EPERM unless it makes a thread |
| 21 | // (CLONE_THREAD). Everything else is allowed. A system call made under |
| 22 | // another architecture's numbering ends the process. On an architecture |
| 23 | // the filter does not know (auditArch is 0), the fetcher refuses to run |
| 24 | // rather than run unfiltered. |
| 25 | |
| 26 | // errNoFilter: this architecture has no filter, so the fetcher does not run. |
| 27 | var errNoFilter = errors.New("webread: no system call filter for " + runtime.GOARCH) |
| 28 | |
| 29 | // x32Bit marks x86-64's x32 system call numbers. |
| 30 | const x32Bit = 0x40000000 |
| 31 | |
| 32 | // Offsets into struct seccomp_data: nr, arch, then args[0]'s low 32 bits |
| 33 | // (both known architectures are little-endian). |
| 34 | const ( |
| 35 | offNr = 0 |
| 36 | offArch = 4 |
| 37 | offArg0 = 16 |
| 38 | filterOK = unix.SECCOMP_RET_ALLOW |
| 39 | ) |
| 40 | |
| 41 | // insn is one BPF instruction whose jumps name labels; label marks the |
| 42 | // instruction others jump to. |
| 43 | type insn struct { |
| 44 | f unix.SockFilter |
| 45 | jt, jf string |
| 46 | label string |
| 47 | } |
| 48 | |
| 49 | // childFilter is the filter's program for this architecture. |
| 50 | func childFilter() ([]unix.SockFilter, error) { |
| 51 | if auditArch == 0 { |
| 52 | return nil, errNoFilter |
| 53 | } |
| 54 | ld := func(off uint32) insn { |
| 55 | return insn{f: unix.SockFilter{Code: unix.BPF_LD | unix.BPF_W | unix.BPF_ABS, K: off}} |
| 56 | } |
| 57 | jeq := func(k uint32, jt, jf string) insn { |
| 58 | return insn{f: unix.SockFilter{Code: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, K: k}, jt: jt, jf: jf} |
| 59 | } |
| 60 | ret := func(label string, k uint32) insn { |
| 61 | return insn{f: unix.SockFilter{Code: unix.BPF_RET | unix.BPF_K, K: k}, label: label} |
| 62 | } |
| 63 | prog := []insn{ |
| 64 | ld(offArch), |
| 65 | jeq(auditArch, "", "kill"), |
| 66 | ld(offNr), |
| 67 | } |
| 68 | if checkX32 { |
| 69 | prog = append(prog, insn{f: unix.SockFilter{Code: unix.BPF_JMP | unix.BPF_JGE | unix.BPF_K, K: x32Bit}, jt: "eperm"}) |
| 70 | } |
| 71 | for _, nr := range deniedSyscalls { |
| 72 | prog = append(prog, jeq(nr, "eperm", "")) |
| 73 | } |
| 74 | prog = append(prog, |
| 75 | jeq(unix.SYS_CLONE3, "enosys", ""), |
| 76 | jeq(unix.SYS_CLONE, "", "allow"), |
| 77 | ld(offArg0), |
| 78 | insn{f: unix.SockFilter{Code: unix.BPF_JMP | unix.BPF_JSET | unix.BPF_K, K: unix.CLONE_THREAD}, jt: "allow", jf: "eperm"}, |
| 79 | ret("allow", filterOK), |
| 80 | ret("eperm", unix.SECCOMP_RET_ERRNO|uint32(unix.EPERM)), |
| 81 | ret("enosys", unix.SECCOMP_RET_ERRNO|uint32(unix.ENOSYS)), |
| 82 | ret("kill", unix.SECCOMP_RET_KILL_PROCESS), |
| 83 | ) |
| 84 | at := map[string]int{} |
| 85 | for i, in := range prog { |
| 86 | if in.label != "" { |
| 87 | at[in.label] = i |
| 88 | } |
| 89 | } |
| 90 | out := make([]unix.SockFilter, len(prog)) |
| 91 | for i, in := range prog { |
| 92 | f := in.f |
| 93 | for _, j := range []struct { |
| 94 | label string |
| 95 | to *uint8 |
| 96 | }{{in.jt, &f.Jt}, {in.jf, &f.Jf}} { |
| 97 | if j.label == "" { |
| 98 | continue |
| 99 | } |
| 100 | n := at[j.label] - i - 1 |
| 101 | if n < 0 || n > 255 { |
| 102 | return nil, fmt.Errorf("webread: filter jump to %s", j.label) |
| 103 | } |
| 104 | *j.to = uint8(n) |
| 105 | } |
| 106 | out[i] = f |
| 107 | } |
| 108 | return out, nil |
| 109 | } |
| 110 | |
| 111 | // noChildren installs the filter on every thread of this process. It |
| 112 | // cannot be removed afterwards (no_new_privs, and filters only add). |
| 113 | func noChildren() error { |
| 114 | prog, err := childFilter() |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | runtime.LockOSThread() |
| 119 | defer runtime.UnlockOSThread() |
| 120 | if err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); err != nil { |
| 121 | return fmt.Errorf("webread: no_new_privs: %w", err) |
| 122 | } |
| 123 | fp := unix.SockFprog{Len: uint16(len(prog)), Filter: &prog[0]} |
| 124 | r, _, errno := unix.Syscall(unix.SYS_SECCOMP, unix.SECCOMP_SET_MODE_FILTER, unix.SECCOMP_FILTER_FLAG_TSYNC, uintptr(unsafe.Pointer(&fp))) |
| 125 | runtime.KeepAlive(prog) |
| 126 | if errno != 0 { |
| 127 | return fmt.Errorf("webread: seccomp: %w", errno) |
| 128 | } |
| 129 | if r != 0 { |
| 130 | // TSYNC names a thread it could not synchronise. |
| 131 | return fmt.Errorf("webread: seccomp: thread %d not synchronised", r) |
| 132 | } |
| 133 | return nil |
| 134 | } |
| 135 | |