| 1 | package fprobe |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "syscall" |
| 13 | |
| 14 | "golang.org/x/sys/unix" |
| 15 | ) |
| 16 | |
| 17 | // The runner's own binary, in its __fprobe mode, is the probes' direct |
| 18 | // reader of the kernel (fix round 1, #3): `procargs <pid>...` prints, |
| 19 | // for each pid, "procargs-start <pid>", that process's KERN_PROCARGS2 |
| 20 | // (its arguments and environment, NULs as newlines) and "procargs-end |
| 21 | // <pid> ok" (or "refused" when the kernel would not say); `fds` lists every |
| 22 | // descriptor it inherited with its kind, then "fds-done". F3 runs it |
| 23 | // inside the sandbox against the CLI's pid as the runner saw it, and the |
| 24 | // runner reads the same outside as the control. It is not ps, which the |
| 25 | // sandbox may not let run at all. |
| 26 | |
| 27 | // HelperMain is the __fprobe mode: 0 when it read the first pid it was |
| 28 | // asked for, 1 when the kernel refused that, 2 for a bad call. |
| 29 | func HelperMain(args []string, out io.Writer) int { |
| 30 | switch { |
| 31 | case len(args) > 0 && args[0] != "procargs" && args[0] != "fds": |
| 32 | return helperMain(args, os.Stdin, out) |
| 33 | case len(args) >= 2 && args[0] == "procargs": |
| 34 | code := 0 |
| 35 | for i, a := range args[1:] { |
| 36 | pid, err := strconv.Atoi(a) |
| 37 | if err != nil || pid <= 0 { |
| 38 | return 2 |
| 39 | } |
| 40 | fmt.Fprintf(out, "procargs-start %d\n", pid) |
| 41 | raw, err := ProcArgs(pid) |
| 42 | if err != nil { |
| 43 | fmt.Fprintf(out, "kern.procargs2: %v\nprocargs-end %d refused\n", err, pid) |
| 44 | if i == 0 { |
| 45 | code = 1 |
| 46 | } |
| 47 | continue |
| 48 | } |
| 49 | for i := range raw { |
| 50 | if raw[i] == 0 { |
| 51 | raw[i] = '\n' |
| 52 | } |
| 53 | } |
| 54 | out.Write(raw) |
| 55 | fmt.Fprintf(out, "\nprocargs-end %d ok\n", pid) |
| 56 | } |
| 57 | return code |
| 58 | case len(args) == 1 && args[0] == "fds": |
| 59 | for fd := 0; fd < 256; fd++ { |
| 60 | var st unix.Stat_t |
| 61 | if unix.Fstat(fd, &st) != nil { |
| 62 | continue |
| 63 | } |
| 64 | fmt.Fprintf(out, "fd %d %s\n", fd, fdKind(uint32(st.Mode))) |
| 65 | } |
| 66 | fmt.Fprintln(out, "fds-done") |
| 67 | return 0 |
| 68 | } |
| 69 | return 2 |
| 70 | } |
| 71 | |
| 72 | // The scan modes (fix round 2, #8) compare what a source gives in memory |
| 73 | // and print one bounded record, never what they read: |
| 74 | // |
| 75 | // scan <needles> <class> <name> [control] stdin: "<name> allowed|denied" |
| 76 | // (failed when the control marker, |
| 77 | // or with "+" anything, is missing) |
| 78 | // procargs-scan <needles> <name> <pid> "<name> allowed|denied <pid> ok", or "<name> refused <pid>" |
| 79 | // procs-scan <needles> <name> "<name> allowed|denied n=<N> self=yes", or "<name> failed ..." |
| 80 | // fds-scan <needles> <name> "<name> allowed|denied n=<N> pipes=<fds|->" |
| 81 | // pids this user's pids, one a line |
| 82 | // |
| 83 | // A needles file holds, per secret form, "<class> <length> <first four |
| 84 | // bytes, hex> <sha256, hex>", and "shape <literal>" lines: the secrets |
| 85 | // themselves are never written. |
| 86 | |
| 87 | // needleLines are the needles file's lines for forms of class. |
| 88 | func needleLines(class string, forms [][]byte) []string { |
| 89 | var out []string |
| 90 | for _, f := range forms { |
| 91 | if len(f) >= 8 { |
| 92 | sum := sha256.Sum256(f) |
| 93 | out = append(out, fmt.Sprintf("%s %d %s %s", class, len(f), hex.EncodeToString(f[:4]), hex.EncodeToString(sum[:]))) |
| 94 | } |
| 95 | } |
| 96 | return out |
| 97 | } |
| 98 | |
| 99 | // needle is one hashed form, or a literal shape. |
| 100 | type needle struct { |
| 101 | n int |
| 102 | prefix []byte |
| 103 | sum string |
| 104 | shape []byte |
| 105 | } |
| 106 | |
| 107 | func readNeedles(path, class string) ([]needle, error) { |
| 108 | raw, err := os.ReadFile(path) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | var out []needle |
| 113 | for _, l := range strings.Split(string(raw), "\n") { |
| 114 | f := strings.Fields(l) |
| 115 | switch { |
| 116 | case len(f) == 2 && f[0] == "shape": |
| 117 | out = append(out, needle{shape: []byte(f[1])}) |
| 118 | case len(f) == 4 && (f[0] == class || class == "*"): |
| 119 | n, err1 := strconv.Atoi(f[1]) |
| 120 | p, err2 := hex.DecodeString(f[2]) |
| 121 | if err1 == nil && err2 == nil && n >= len(p) { |
| 122 | out = append(out, needle{n: n, prefix: p, sum: f[3]}) |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | return out, nil |
| 127 | } |
| 128 | |
| 129 | // hit: data holds a needle. |
| 130 | func hit(data []byte, needles []needle) bool { |
| 131 | for _, nd := range needles { |
| 132 | if nd.shape != nil { |
| 133 | if bytes.Contains(data, nd.shape) { |
| 134 | return true |
| 135 | } |
| 136 | continue |
| 137 | } |
| 138 | for i := 0; i+nd.n <= len(data); { |
| 139 | j := bytes.Index(data[i:], nd.prefix) |
| 140 | if j < 0 || i+j+nd.n > len(data) { |
| 141 | break |
| 142 | } |
| 143 | sum := sha256.Sum256(data[i+j : i+j+nd.n]) |
| 144 | if hex.EncodeToString(sum[:]) == nd.sum { |
| 145 | return true |
| 146 | } |
| 147 | i += j + 1 |
| 148 | } |
| 149 | } |
| 150 | return false |
| 151 | } |
| 152 | |
| 153 | func outcome(b bool) string { |
| 154 | if b { |
| 155 | return "allowed" |
| 156 | } |
| 157 | return "denied" |
| 158 | } |
| 159 | |
| 160 | // helperMain is HelperMain with its input. |
| 161 | func helperMain(args []string, in io.Reader, out io.Writer) int { |
| 162 | if len(args) == 0 { |
| 163 | return 2 |
| 164 | } |
| 165 | switch args[0] { |
| 166 | case "scan": |
| 167 | if len(args) != 4 && len(args) != 5 { |
| 168 | return 2 |
| 169 | } |
| 170 | nd, err := readNeedles(args[1], args[2]) |
| 171 | data, rerr := io.ReadAll(io.LimitReader(in, 64<<20)) |
| 172 | // The control (fix after the final review): what the producer |
| 173 | // must have given for its source to count as read at all. |
| 174 | control := len(args) < 5 || args[4] == "+" && len(data) > 0 || args[4] != "+" && bytes.Contains(data, []byte(args[4])) |
| 175 | if err != nil || rerr != nil || !control { |
| 176 | fmt.Fprintf(out, "%s failed\n", args[3]) |
| 177 | return 1 |
| 178 | } |
| 179 | fmt.Fprintf(out, "%s %s\n", args[3], outcome(hit(data, nd))) |
| 180 | return 0 |
| 181 | case "procargs-scan": |
| 182 | pid, err := 0, error(nil) |
| 183 | if len(args) == 4 { |
| 184 | pid, err = strconv.Atoi(args[3]) |
| 185 | } |
| 186 | if len(args) != 4 || err != nil || pid <= 0 { |
| 187 | if len(args) >= 3 { |
| 188 | fmt.Fprintf(out, "%s failed\n", args[2]) |
| 189 | } |
| 190 | return 2 |
| 191 | } |
| 192 | nd, err := readNeedles(args[1], "token") |
| 193 | if err != nil { |
| 194 | fmt.Fprintf(out, "%s failed\n", args[2]) |
| 195 | return 2 |
| 196 | } |
| 197 | raw, err := ProcArgs(pid) |
| 198 | if err != nil { |
| 199 | fmt.Fprintf(out, "%s refused %d\n", args[2], pid) |
| 200 | return 1 |
| 201 | } |
| 202 | fmt.Fprintf(out, "%s %s %d ok\n", args[2], outcome(hit(raw, nd)), pid) |
| 203 | return 0 |
| 204 | case "procs-scan": |
| 205 | if len(args) != 3 { |
| 206 | return 2 |
| 207 | } |
| 208 | nd, err := readNeedles(args[1], "token") |
| 209 | pids, perr := listPIDs() |
| 210 | if err != nil || perr != nil { |
| 211 | fmt.Fprintf(out, "%s failed\n", args[2]) |
| 212 | return 1 |
| 213 | } |
| 214 | self, seen := os.Getppid(), false |
| 215 | found := false |
| 216 | for _, p := range pids { |
| 217 | seen = seen || p == self |
| 218 | if raw, err := ProcArgs(p); err == nil && hit(raw, nd) { |
| 219 | found = true |
| 220 | } |
| 221 | } |
| 222 | if !seen { |
| 223 | fmt.Fprintf(out, "%s failed n=%d self=no\n", args[2], len(pids)) |
| 224 | return 1 |
| 225 | } |
| 226 | fmt.Fprintf(out, "%s %s n=%d self=yes\n", args[2], outcome(found), len(pids)) |
| 227 | return 0 |
| 228 | case "fds-scan": |
| 229 | if len(args) != 3 { |
| 230 | return 2 |
| 231 | } |
| 232 | nd, err := readNeedles(args[1], "token") |
| 233 | if err != nil { |
| 234 | fmt.Fprintf(out, "%s failed\n", args[2]) |
| 235 | return 1 |
| 236 | } |
| 237 | n, found := 0, false |
| 238 | var pipes []string |
| 239 | for fd := 0; fd < 256; fd++ { |
| 240 | var st unix.Stat_t |
| 241 | if unix.Fstat(fd, &st) != nil { |
| 242 | continue |
| 243 | } |
| 244 | n++ |
| 245 | switch fdKind(uint32(st.Mode)) { |
| 246 | case "pipe": |
| 247 | if fd >= 3 { |
| 248 | pipes = append(pipes, strconv.Itoa(fd)) |
| 249 | } |
| 250 | case "file": |
| 251 | buf := make([]byte, 1<<20) |
| 252 | if k, err := unix.Pread(fd, buf, 0); err == nil && hit(buf[:k], nd) { |
| 253 | found = true |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | list := "-" |
| 258 | if len(pipes) > 0 { |
| 259 | list = strings.Join(pipes, ",") |
| 260 | } |
| 261 | fmt.Fprintf(out, "%s %s n=%d pipes=%s\n", args[2], outcome(found), n, list) |
| 262 | return 0 |
| 263 | case "pids": |
| 264 | pids, err := ownPIDs() |
| 265 | if err != nil { |
| 266 | return 1 |
| 267 | } |
| 268 | for _, p := range pids { |
| 269 | fmt.Fprintln(out, p) |
| 270 | } |
| 271 | return 0 |
| 272 | case "procargs", "fds": |
| 273 | return HelperMain(args, out) |
| 274 | } |
| 275 | return 2 |
| 276 | } |
| 277 | |
| 278 | // fdKind names a descriptor's kind from its mode. |
| 279 | func fdKind(mode uint32) string { |
| 280 | switch mode & syscall.S_IFMT { |
| 281 | case syscall.S_IFIFO: |
| 282 | return "pipe" |
| 283 | case syscall.S_IFSOCK: |
| 284 | return "socket" |
| 285 | case syscall.S_IFCHR: |
| 286 | return "char" |
| 287 | case syscall.S_IFDIR: |
| 288 | return "dir" |
| 289 | case syscall.S_IFREG: |
| 290 | return "file" |
| 291 | } |
| 292 | return "other" |
| 293 | } |
| 294 | |
| 295 | // The runner's own setup operations, where volumes are mounted, and the |
| 296 | // reader's process list (tests stand in). |
| 297 | var ( |
| 298 | listPIDs = ownPIDs |
| 299 | symlink = os.Symlink |
| 300 | hardlink = os.Link |
| 301 | rename = os.Rename |
| 302 | volumesDir = "/Volumes" |
| 303 | ) |
| 304 | |