| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "slices" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // Synthetic process tables. No table of the Studio's processes was |
| 12 | // recorded (task 1 recorded no ps output, only that Bash commands run in |
| 13 | // sessions of their own and that macOS 27.2 hides platform binaries' |
| 14 | // environment), so these rows are made up, 2026-11-28, in the shape the |
| 15 | // darwin table reports: ps -axo pid,ppid,pgid,sess,lstart,uid,comm, with |
| 16 | // lstart as the kernel's start in microseconds and comm as the kernel's |
| 17 | // path (proc_pidpath), not argv[0]. |
| 18 | |
| 19 | // turnBegan is the runner user's table when the turn begins: the runner, |
| 20 | // two launchd agents and another user's shell. |
| 21 | const turnBegan = ` |
| 22 | # pid ppid pgid sess start uid path |
| 23 | self 1 self self 1000 me /Users/paulequipo/.local/lib/equipo-runner/equipo-runner |
| 24 | 40100 1 40100 40100 900 me /usr/sbin/cfprefsd |
| 25 | 40200 1 40200 40200 950 me /usr/libexec/trustd |
| 26 | 40300 1 40300 40300 960 501 /bin/zsh |
| 27 | ` |
| 28 | |
| 29 | // sweepTurn begins a turn on tb (its table read as before), lets during |
| 30 | // add what the turn starts, and leaves the turn unproven, so the next |
| 31 | // take settles it with the sweep. It returns the signals sent. |
| 32 | func sweepTurn(t *testing.T, tb *table, own *Own, during func(*Turn)) []int { |
| 33 | t.Helper() |
| 34 | turns := NewTurns("").Sweep(tb) |
| 35 | turns.own, turns.Settle = own, 300*time.Millisecond |
| 36 | u, err := turns.Take(context.Background(), OpEngineering) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if err := u.launch("attempt-s"); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | during(u) |
| 44 | u.unprovenEnd() |
| 45 | u.Give() |
| 46 | _ = turns.settle(time.Now().Add(turns.settleLimit())) |
| 47 | return tb.signalled() |
| 48 | } |
| 49 | |
| 50 | func row(pid, ppid, pgid, sid int, start int64, uid int, marker string) ProcInfo { |
| 51 | return ProcInfo{ID: ProcID{PID: pid, Start: start}, PPID: ppid, PGID: pgid, SID: sid, UID: uid, Marker: marker} |
| 52 | } |
| 53 | |
| 54 | // The runner and its own helpers are spared by identity (pid and start), |
| 55 | // with what they start; a step's process running the same program is |
| 56 | // not, nor a process that has a helper's pid but another start. |
| 57 | func TestSweepSparesOwnByIdentityNotName(t *testing.T) { |
| 58 | me, self := os.Getuid(), os.Getpid() |
| 59 | tb := parseTable(t, turnBegan) |
| 60 | own := &Own{} |
| 61 | sig := sweepTurn(t, tb, own, func(*Turn) { |
| 62 | tb.add(row(50100, self, self, self, 2000, me, ""), "/usr/bin/git") |
| 63 | own.Add(ProcID{PID: 50100, Start: 2000}) |
| 64 | tb.add(row(50101, 50100, self, self, 2001, me, ""), "/usr/libexec/git-core/git-remote-http") |
| 65 | tb.add(row(50200, 1, 50200, 50200, 2100, me, ""), "/usr/bin/git") |
| 66 | tb.add(row(50300, self, self, self, 2200, me, ""), "/usr/bin/xcrun") |
| 67 | }) |
| 68 | for _, pid := range []int{self, 50100, 50101, 40100, 40200, 40300} { |
| 69 | if slices.Contains(sig, pid) || slices.Contains(sig, -pid) { |
| 70 | t.Fatalf("signalled %d, the runner's or from before the turn: %v", pid, sig) |
| 71 | } |
| 72 | } |
| 73 | for _, pid := range []int{50200, 50300} { |
| 74 | if !slices.Contains(sig, pid) && !slices.Contains(sig, -pid) { |
| 75 | t.Fatalf("%d (not the runner's, whatever its name) was spared: %v", pid, sig) |
| 76 | } |
| 77 | } |
| 78 | // A helper's pid used again by a step's process: another identity. |
| 79 | tb = parseTable(t, turnBegan) |
| 80 | own = &Own{} |
| 81 | sig = sweepTurn(t, tb, own, func(*Turn) { |
| 82 | own.Add(ProcID{PID: 50100, Start: 2000}) |
| 83 | tb.add(row(50100, 1, 50100, 50100, 2500, me, ""), "/usr/bin/git") |
| 84 | }) |
| 85 | if !slices.Contains(sig, 50100) && !slices.Contains(sig, -50100) { |
| 86 | t.Fatalf("a reused helper pid was spared: %v", sig) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Each signal follows a fresh read of that process: a pid used again |
| 91 | // between the table and the signal (here by another user's process with |
| 92 | // a later start, or the same process that changed user) is never |
| 93 | // signalled, nor its group. |
| 94 | func TestSweepRechecksIdentityBeforeEachSignal(t *testing.T) { |
| 95 | me := os.Getuid() |
| 96 | tb := parseTable(t, turnBegan) |
| 97 | sig := sweepTurn(t, tb, &Own{}, func(u *Turn) { |
| 98 | tb.add(row(60100, 1, 60100, 60100, 3000, me, ""), "/Users/paulequipo/w/escape") |
| 99 | tb.add(row(60200, 1, 60200, 60200, 3010, me, "attempt-s"), "/Users/paulequipo/w/marked") |
| 100 | u.note(span{60200: 3010}, nil) |
| 101 | tb.add(row(60300, 1, 60300, 60300, 3020, me, ""), "/Users/paulequipo/w/setuid") |
| 102 | reused := map[int]bool{} |
| 103 | tb.lookup = func(tb *table, pid int) { |
| 104 | tb.mu.Lock() |
| 105 | defer tb.mu.Unlock() |
| 106 | if reused[pid] { |
| 107 | return |
| 108 | } |
| 109 | switch pid { |
| 110 | case 60100, 60200: |
| 111 | reused[pid] = true |
| 112 | tb.procs[pid] = row(pid, 1, pid, pid, 9000, 501, "") |
| 113 | tb.paths[pid] = "/bin/zsh" |
| 114 | case 60300: |
| 115 | reused[pid] = true |
| 116 | q := tb.procs[pid] |
| 117 | q.UID = 0 |
| 118 | tb.procs[pid] = q |
| 119 | } |
| 120 | } |
| 121 | }) |
| 122 | for _, pid := range []int{60100, -60100, 60200, -60200, 60300} { |
| 123 | if slices.Contains(sig, pid) { |
| 124 | t.Fatalf("signalled %d after it stopped being the process listed: %v", pid, sig) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Another user's processes are never signalled, even started during the |
| 130 | // turn, holding an id the attempt's group had, or showing its marker; a |
| 131 | // group id of the attempt's now used by another user keeps the proof |
| 132 | // waiting. |
| 133 | func TestSweepNeverTouchesOtherUsers(t *testing.T) { |
| 134 | tb := parseTable(t, turnBegan) |
| 135 | var left []ProcInfo |
| 136 | sig := sweepTurn(t, tb, &Own{}, func(u *Turn) { |
| 137 | u.note(span{70300: 3900}, span{70300: 3900}) |
| 138 | tb.add(row(70100, 1, 70100, 70100, 4000, 501, ""), "/Users/other/evil") |
| 139 | tb.add(row(70300, 1, 70300, 70300, 4100, 501, ""), "/bin/zsh") |
| 140 | tb.add(row(70400, 70300, 70300, 70300, 4200, 0, "attempt-s"), "/usr/bin/login") |
| 141 | s := newScope(tb, []string{"attempt-s"}, u.before()) |
| 142 | s.Groups[70300] = 3900 |
| 143 | list, _ := tb.List(time.Time{}, []string{"attempt-s"}) |
| 144 | left = s.Targets(list, nil) |
| 145 | if s.Gone() { |
| 146 | t.Error("proof given while another user holds a group id the attempt made") |
| 147 | } |
| 148 | }) |
| 149 | if len(left) != 0 || len(sig) != 0 { |
| 150 | t.Fatalf("another user's process was a target %v or signalled %v", left, sig) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // #10 (the controller's ruling): no exemption by path. Every process of |
| 155 | // the runner user that started during the turn and is not the runner's |
| 156 | // own is the turn's: launchd-parented ones under /System, /usr/libexec |
| 157 | // or /usr/sbin (on-demand agents launchd starts again when needed), a |
| 158 | // double-fork from the step's own tree, a /System/Volumes path, a path |
| 159 | // that climbs out, an unknown path. What was there before is untouched. |
| 160 | func TestSweepKillsEveryNewProcessWhateverItsPath(t *testing.T) { |
| 161 | me := os.Getuid() |
| 162 | tb := parseTable(t, turnBegan) |
| 163 | sig := sweepTurn(t, tb, &Own{}, func(u *Turn) { |
| 164 | u.note(nil, span{81000: 5000}) |
| 165 | tb.add(row(80100, 1, 80100, 80100, 5100, me, ""), "/System/Library/PrivateFrameworks/X.framework/XPCServices/Y.xpc/Contents/MacOS/Y") |
| 166 | tb.add(row(80200, 1, 80200, 80200, 5200, me, ""), "/usr/libexec/xpcproxy") |
| 167 | tb.add(row(80250, 1, 80250, 80250, 5250, me, ""), "/usr/sbin/cfprefsd") |
| 168 | tb.add(row(80300, 1, 80300, 80300, 5300, me, ""), "/Users/paulequipo/w/tree/.build/escape") |
| 169 | tb.add(row(80400, 1, 80400, 80400, 5400, me, ""), "/System/Volumes/Data/Users/paulequipo/escape") |
| 170 | tb.add(row(80500, 1, 80500, 80500, 5500, me, ""), "/System/Library/../../Users/paulequipo/escape") |
| 171 | tb.add(row(80600, 80300, 80600, 80600, 5600, me, ""), "/usr/libexec/xpcproxy") |
| 172 | tb.add(row(80700, 1, 80700, 80700, 5700, me, ""), "") |
| 173 | tb.add(row(80800, 1, 80800, 81000, 5800, me, ""), "/usr/libexec/xpcproxy") |
| 174 | }) |
| 175 | for _, pid := range []int{80100, 80200, 80250, 80300, 80400, 80500, 80600, 80700, 80800} { |
| 176 | if !slices.Contains(sig, pid) && !slices.Contains(sig, -pid) { |
| 177 | t.Fatalf("%d, new during the turn, was spared: %v", pid, sig) |
| 178 | } |
| 179 | } |
| 180 | for _, pid := range []int{40100, 40200, 40300} { |
| 181 | if slices.Contains(sig, pid) || slices.Contains(sig, -pid) { |
| 182 | t.Fatalf("%d, there before the turn, was signalled: %v", pid, sig) |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // The live proof waits for every group the attempt made: one its |
| 188 | // process made (and that is gone) but whose id another user now holds |
| 189 | // keeps the end unproven until that group ends too. |
| 190 | func TestStopWaitsForEveryGroupItMade(t *testing.T) { |
| 191 | me := os.Getuid() |
| 192 | tb := parseTable(t, turnBegan) |
| 193 | x := &Executor{Procs: tb, Own: &Own{}, Grace: 50 * time.Millisecond, Settle: 400 * time.Millisecond, Poll: 10 * time.Millisecond} |
| 194 | var leader int |
| 195 | run, err := x.Start(context.Background(), Command{Argv: []string{"/bin/sleep", "0.3"}}, "attempt-g", func(id string) error { |
| 196 | if pgid, _, _ := parseRecord(id); pgid > 0 { |
| 197 | leader = pgid |
| 198 | tb.add(row(pgid, os.Getpid(), pgid, pgid, 6000, me, ""), "/bin/sleep") |
| 199 | tb.add(row(90000, pgid, 90000, pgid, 6001, me, ""), "/bin/bash") |
| 200 | } |
| 201 | return nil |
| 202 | }) |
| 203 | if err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | for deadline := time.Now().Add(2 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) { |
| 207 | run.mu.Lock() |
| 208 | _, made := run.scope.Groups[90000] |
| 209 | run.mu.Unlock() |
| 210 | if made { |
| 211 | break |
| 212 | } |
| 213 | } |
| 214 | <-run.Exited() |
| 215 | tb.mu.Lock() |
| 216 | delete(tb.procs, leader) |
| 217 | delete(tb.procs, 90000) |
| 218 | tb.procs[90000] = row(90000, 1, 90000, 90000, 7000, 501, "") |
| 219 | tb.mu.Unlock() |
| 220 | if run.Stop() { |
| 221 | t.Fatal("proven while a group the attempt made is held by another user") |
| 222 | } |
| 223 | if slices.Contains(tb.signalled(), 90000) || slices.Contains(tb.signalled(), -90000) { |
| 224 | t.Fatal("the other user's group was signalled") |
| 225 | } |
| 226 | } |
| 227 | |