package fprobe import ( "bytes" "crypto/sha256" "encoding/hex" "fmt" "io" "os" "strconv" "strings" "syscall" "golang.org/x/sys/unix" ) // The runner's own binary, in its __fprobe mode, is the probes' direct // reader of the kernel (fix round 1, #3): `procargs ...` prints, // for each pid, "procargs-start ", that process's KERN_PROCARGS2 // (its arguments and environment, NULs as newlines) and "procargs-end // ok" (or "refused" when the kernel would not say); `fds` lists every // descriptor it inherited with its kind, then "fds-done". F3 runs it // inside the sandbox against the CLI's pid as the runner saw it, and the // runner reads the same outside as the control. It is not ps, which the // sandbox may not let run at all. // HelperMain is the __fprobe mode: 0 when it read the first pid it was // asked for, 1 when the kernel refused that, 2 for a bad call. func HelperMain(args []string, out io.Writer) int { switch { case len(args) > 0 && args[0] != "procargs" && args[0] != "fds": return helperMain(args, os.Stdin, out) case len(args) >= 2 && args[0] == "procargs": code := 0 for i, a := range args[1:] { pid, err := strconv.Atoi(a) if err != nil || pid <= 0 { return 2 } fmt.Fprintf(out, "procargs-start %d\n", pid) raw, err := ProcArgs(pid) if err != nil { fmt.Fprintf(out, "kern.procargs2: %v\nprocargs-end %d refused\n", err, pid) if i == 0 { code = 1 } continue } for i := range raw { if raw[i] == 0 { raw[i] = '\n' } } out.Write(raw) fmt.Fprintf(out, "\nprocargs-end %d ok\n", pid) } return code case len(args) == 1 && args[0] == "fds": for fd := 0; fd < 256; fd++ { var st unix.Stat_t if unix.Fstat(fd, &st) != nil { continue } fmt.Fprintf(out, "fd %d %s\n", fd, fdKind(uint32(st.Mode))) } fmt.Fprintln(out, "fds-done") return 0 } return 2 } // The scan modes (fix round 2, #8) compare what a source gives in memory // and print one bounded record, never what they read: // // scan [control] stdin: " allowed|denied" // (failed when the control marker, // or with "+" anything, is missing) // procargs-scan " allowed|denied ok", or " refused " // procs-scan " allowed|denied n= self=yes", or " failed ..." // fds-scan " allowed|denied n= pipes=" // pids this user's pids, one a line // // A needles file holds, per secret form, " ", and "shape " lines: the secrets // themselves are never written. // needleLines are the needles file's lines for forms of class. func needleLines(class string, forms [][]byte) []string { var out []string for _, f := range forms { if len(f) >= 8 { sum := sha256.Sum256(f) out = append(out, fmt.Sprintf("%s %d %s %s", class, len(f), hex.EncodeToString(f[:4]), hex.EncodeToString(sum[:]))) } } return out } // needle is one hashed form, or a literal shape. type needle struct { n int prefix []byte sum string shape []byte } func readNeedles(path, class string) ([]needle, error) { raw, err := os.ReadFile(path) if err != nil { return nil, err } var out []needle for _, l := range strings.Split(string(raw), "\n") { f := strings.Fields(l) switch { case len(f) == 2 && f[0] == "shape": out = append(out, needle{shape: []byte(f[1])}) case len(f) == 4 && (f[0] == class || class == "*"): n, err1 := strconv.Atoi(f[1]) p, err2 := hex.DecodeString(f[2]) if err1 == nil && err2 == nil && n >= len(p) { out = append(out, needle{n: n, prefix: p, sum: f[3]}) } } } return out, nil } // hit: data holds a needle. func hit(data []byte, needles []needle) bool { for _, nd := range needles { if nd.shape != nil { if bytes.Contains(data, nd.shape) { return true } continue } for i := 0; i+nd.n <= len(data); { j := bytes.Index(data[i:], nd.prefix) if j < 0 || i+j+nd.n > len(data) { break } sum := sha256.Sum256(data[i+j : i+j+nd.n]) if hex.EncodeToString(sum[:]) == nd.sum { return true } i += j + 1 } } return false } func outcome(b bool) string { if b { return "allowed" } return "denied" } // helperMain is HelperMain with its input. func helperMain(args []string, in io.Reader, out io.Writer) int { if len(args) == 0 { return 2 } switch args[0] { case "scan": if len(args) != 4 && len(args) != 5 { return 2 } nd, err := readNeedles(args[1], args[2]) data, rerr := io.ReadAll(io.LimitReader(in, 64<<20)) // The control (fix after the final review): what the producer // must have given for its source to count as read at all. control := len(args) < 5 || args[4] == "+" && len(data) > 0 || args[4] != "+" && bytes.Contains(data, []byte(args[4])) if err != nil || rerr != nil || !control { fmt.Fprintf(out, "%s failed\n", args[3]) return 1 } fmt.Fprintf(out, "%s %s\n", args[3], outcome(hit(data, nd))) return 0 case "procargs-scan": pid, err := 0, error(nil) if len(args) == 4 { pid, err = strconv.Atoi(args[3]) } if len(args) != 4 || err != nil || pid <= 0 { if len(args) >= 3 { fmt.Fprintf(out, "%s failed\n", args[2]) } return 2 } nd, err := readNeedles(args[1], "token") if err != nil { fmt.Fprintf(out, "%s failed\n", args[2]) return 2 } raw, err := ProcArgs(pid) if err != nil { fmt.Fprintf(out, "%s refused %d\n", args[2], pid) return 1 } fmt.Fprintf(out, "%s %s %d ok\n", args[2], outcome(hit(raw, nd)), pid) return 0 case "procs-scan": if len(args) != 3 { return 2 } nd, err := readNeedles(args[1], "token") pids, perr := listPIDs() if err != nil || perr != nil { fmt.Fprintf(out, "%s failed\n", args[2]) return 1 } self, seen := os.Getppid(), false found := false for _, p := range pids { seen = seen || p == self if raw, err := ProcArgs(p); err == nil && hit(raw, nd) { found = true } } if !seen { fmt.Fprintf(out, "%s failed n=%d self=no\n", args[2], len(pids)) return 1 } fmt.Fprintf(out, "%s %s n=%d self=yes\n", args[2], outcome(found), len(pids)) return 0 case "fds-scan": if len(args) != 3 { return 2 } nd, err := readNeedles(args[1], "token") if err != nil { fmt.Fprintf(out, "%s failed\n", args[2]) return 1 } n, found := 0, false var pipes []string for fd := 0; fd < 256; fd++ { var st unix.Stat_t if unix.Fstat(fd, &st) != nil { continue } n++ switch fdKind(uint32(st.Mode)) { case "pipe": if fd >= 3 { pipes = append(pipes, strconv.Itoa(fd)) } case "file": buf := make([]byte, 1<<20) if k, err := unix.Pread(fd, buf, 0); err == nil && hit(buf[:k], nd) { found = true } } } list := "-" if len(pipes) > 0 { list = strings.Join(pipes, ",") } fmt.Fprintf(out, "%s %s n=%d pipes=%s\n", args[2], outcome(found), n, list) return 0 case "pids": pids, err := ownPIDs() if err != nil { return 1 } for _, p := range pids { fmt.Fprintln(out, p) } return 0 case "procargs", "fds": return HelperMain(args, out) } return 2 } // fdKind names a descriptor's kind from its mode. func fdKind(mode uint32) string { switch mode & syscall.S_IFMT { case syscall.S_IFIFO: return "pipe" case syscall.S_IFSOCK: return "socket" case syscall.S_IFCHR: return "char" case syscall.S_IFDIR: return "dir" case syscall.S_IFREG: return "file" } return "other" } // The runner's own setup operations, where volumes are mounted, and the // reader's process list (tests stand in). var ( listPIDs = ownPIDs symlink = os.Symlink hardlink = os.Link rename = os.Rename volumesDir = "/Volumes" )