| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "slices" |
| 8 | "sync/atomic" |
| 9 | "syscall" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | // Fix round 1 (Astra round 1) regressions on synthetic tables: rows |
| 15 | // made up 2026-11-28 in the darwin table's shape (see sweep_test.go). |
| 16 | |
| 17 | // liveTurn begins a sweeping turn on tb and starts a leader that exits at |
| 18 | // once; what the turn left is what the test adds to tb. |
| 19 | func liveTurn(t *testing.T, tb Procs, own *Own, settle time.Duration) (*Turns, *Turn, *Executor, *Run) { |
| 20 | t.Helper() |
| 21 | turns := NewTurns("").Sweep(tb) |
| 22 | turns.own, turns.Settle = own, settle |
| 23 | u, err := turns.Take(context.Background(), OpEngineering) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | x := &Executor{Turns: turns, Grace: 50 * time.Millisecond, Settle: settle, Poll: 10 * time.Millisecond} |
| 28 | run, err := x.Start(u.Context(context.Background()), Command{Argv: []string{"/bin/true"}}, "attempt-l", func(string) error { return nil }) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | <-run.Exited() |
| 33 | return turns, u, x, run |
| 34 | } |
| 35 | |
| 36 | func untouched(t *testing.T, sig []int, pids ...int) { |
| 37 | t.Helper() |
| 38 | for _, pid := range pids { |
| 39 | if slices.Contains(sig, pid) || slices.Contains(sig, -pid) { |
| 40 | t.Fatalf("%d (there before the turn, or spared) was signalled: %v", pid, sig) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // #10: a step that double-forked to launchd between two polls and runs a |
| 46 | // system binary (a /System stand-in for a script interpreter) is killed, |
| 47 | // and while it lives the end is not proven and no turn begins. What was |
| 48 | // there before the turn is never touched. |
| 49 | func TestSweepKillsSystemPathEscapes(t *testing.T) { |
| 50 | me := os.Getuid() |
| 51 | tb := parseTable(t, turnBegan) |
| 52 | turns, u, _, run := liveTurn(t, tb, &Own{}, 250*time.Millisecond) |
| 53 | tb.add(row(82100, 1, 82100, 82100, 5100, me, ""), "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc") |
| 54 | tb.mark(&tb.unkillable, 82100) |
| 55 | if run.Stop() { |
| 56 | t.Fatal("proven over with a double-forked system-path process alive") |
| 57 | } |
| 58 | if !slices.Contains(tb.signalled(), 82100) { |
| 59 | t.Fatalf("the escape was spared: %v", tb.signalled()) |
| 60 | } |
| 61 | u.Give() |
| 62 | if _, err := turns.Take(context.Background(), OpCheck); !errors.Is(err, ErrUnsettled) { |
| 63 | t.Fatalf("a turn began beside it: %v", err) |
| 64 | } |
| 65 | tb.unmark(&tb.unkillable, 82100) |
| 66 | next, err := turns.Take(context.Background(), OpCheck) |
| 67 | if err != nil { |
| 68 | t.Fatalf("not settled once it could die: %v", err) |
| 69 | } |
| 70 | next.Give() |
| 71 | if tb.has(82100) { |
| 72 | t.Fatal("the escape survived settling") |
| 73 | } |
| 74 | untouched(t, tb.signalled(), 40100, 40200, 40300) |
| 75 | } |
| 76 | |
| 77 | // #10: a system agent launchd started during the turn is killed like |
| 78 | // anything else new; what launchd starts again from a system path after |
| 79 | // the kill pass began is a relaunch, spared and not in the way of the |
| 80 | // proof. Something new outside the system after the kill pass is killed. |
| 81 | func TestSweepSparesLaunchdRelaunches(t *testing.T) { |
| 82 | me := os.Getuid() |
| 83 | tb := parseTable(t, turnBegan) |
| 84 | tb.after = func(tb *table, pid int, sig syscall.Signal) { |
| 85 | if (pid == 82200 || pid == -82200) && tb.procs[82201].PID == 0 { |
| 86 | tb.procs[82201] = row(82201, 1, 82201, 82201, 99000, me, "") |
| 87 | tb.paths[82201] = "/usr/libexec/trustd" |
| 88 | tb.procs[82300] = row(82300, 1, 82300, 82300, 99001, me, "") |
| 89 | tb.paths[82300] = "/Users/paulequipo/w/late" |
| 90 | } |
| 91 | } |
| 92 | _, u, _, run := liveTurn(t, tb, &Own{}, time.Second) |
| 93 | defer u.Give() |
| 94 | tb.add(row(82200, 1, 82200, 82200, 5200, me, ""), "/usr/libexec/trustd") |
| 95 | if !run.Stop() { |
| 96 | t.Fatalf("a launchd relaunch after the kill pass blocked the proof: %v", tb.signalled()) |
| 97 | } |
| 98 | sig := tb.signalled() |
| 99 | for _, pid := range []int{82200, 82300} { |
| 100 | if !slices.Contains(sig, pid) && !slices.Contains(sig, -pid) { |
| 101 | t.Fatalf("%d (new during the turn) was spared: %v", pid, sig) |
| 102 | } |
| 103 | } |
| 104 | untouched(t, sig, 82201, 40100, 40200, 40300) |
| 105 | } |
| 106 | |
| 107 | // #3: a helper's child seen while the helper lived stays the runner's |
| 108 | // own after the helper exits and the child is reparented; a process that |
| 109 | // later has the helper's pid (another start) is not, nor its child. |
| 110 | func TestSweepSparesOrphanedHelperDescendants(t *testing.T) { |
| 111 | me, self := os.Getuid(), os.Getpid() |
| 112 | tb := parseTable(t, turnBegan) |
| 113 | own := &Own{} |
| 114 | sig := sweepTurn(t, tb, own, func(*Turn) { |
| 115 | tb.add(row(51100, self, self, self, 2000, me, ""), "/usr/bin/xcrun") |
| 116 | own.Add(ProcID{PID: 51100, Start: 2000}) |
| 117 | tb.add(row(51101, 51100, 51101, 51101, 2001, me, ""), "/Applications/Xcode.app/Contents/Developer/usr/bin/simctl") |
| 118 | if _, _, err := own.Snapshot(time.Now().Add(time.Second), tb, nil); err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | tb.remove(51100) |
| 122 | tb.reparent(51101, 1) |
| 123 | tb.add(row(51100, 1, 51100, 51100, 2500, me, ""), "/Users/paulequipo/w/reused") |
| 124 | tb.add(row(51102, 51100, 51100, 51100, 2501, me, ""), "/Users/paulequipo/w/reused-child") |
| 125 | }) |
| 126 | untouched(t, sig, 51101) |
| 127 | for _, pid := range []int{51100, 51102} { |
| 128 | if tb.has(pid) { |
| 129 | t.Fatalf("%d (a reused helper pid and its child) was spared: %v", pid, sig) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // #5: a group is signalled whole only while a freshly read member is |
| 135 | // still in it: a process that left the group between the table and the |
| 136 | // re-check (setpgid, setsid) does not license a signal to the old group, |
| 137 | // in the sweep or in the restart path. |
| 138 | func TestGroupSignalNeedsFreshMembership(t *testing.T) { |
| 139 | me := os.Getuid() |
| 140 | moveOut := func(tb *table, target int) { |
| 141 | moved := false |
| 142 | tb.lookup = func(tb *table, pid int) { |
| 143 | tb.mu.Lock() |
| 144 | defer tb.mu.Unlock() |
| 145 | if pid == target && !moved { |
| 146 | moved = true |
| 147 | q := tb.procs[pid] |
| 148 | q.PGID = pid |
| 149 | tb.procs[pid] = q |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | tb := parseTable(t, turnBegan) |
| 154 | sig := sweepTurn(t, tb, &Own{}, func(u *Turn) { |
| 155 | u.note(span{61200: 3000}, nil) |
| 156 | tb.add(row(61201, 1, 61200, 61201, 3001, me, ""), "/Users/paulequipo/w/a") |
| 157 | moveOut(tb, 61201) |
| 158 | }) |
| 159 | untouched(t, sig, 61200) |
| 160 | tb = parseTable(t, turnBegan) |
| 161 | tb.add(row(61301, 1, 61300, 61301, 3100, me, "attempt-v"), "/Users/paulequipo/w/b") |
| 162 | moveOut(tb, 61301) |
| 163 | x := &Executor{Procs: tb, Own: &Own{}, Settle: 300 * time.Millisecond} |
| 164 | x.Verify(RecordID(61300, "attempt-v")) |
| 165 | untouched(t, tb.signalled(), 61300) |
| 166 | } |
| 167 | |
| 168 | // #6: every group and session the attempt made must be gone, through |
| 169 | // the live proof and through recovery: one now held only by another |
| 170 | // user's process (never signalled) keeps the attempt unproven until it |
| 171 | // ends. |
| 172 | func TestRecoveryKeepsEveryGroupAndSession(t *testing.T) { |
| 173 | me := os.Getuid() |
| 174 | for _, c := range []struct { |
| 175 | name string |
| 176 | other ProcInfo |
| 177 | }{ |
| 178 | // the group's id, now another user's group |
| 179 | {"group", row(91001, 1, 91001, 91001, 7000, 501, "")}, |
| 180 | // the session's only member left, another user's (a setuid program) |
| 181 | {"session", row(91100, 1, 91100, 91001, 7000, 501, "")}, |
| 182 | // our own process, which became root (a setuid program that |
| 183 | // called setuid): the same identity, out of our reach |
| 184 | {"setuid", row(91001, 1, 91002, 91002, 6001, 0, "")}, |
| 185 | } { |
| 186 | t.Run(c.name, func(t *testing.T) { |
| 187 | tb := parseTable(t, turnBegan) |
| 188 | // Once ours is killed, another user's process has its id (or |
| 189 | // is what is left of its session). |
| 190 | tb.after = func(tb *table, pid int, sig syscall.Signal) { |
| 191 | if (pid == 91001 || pid == -91001) && tb.procs[c.other.PID].UID != 501 { |
| 192 | tb.procs[c.other.PID] = c.other |
| 193 | tb.paths[c.other.PID] = "/usr/bin/login" |
| 194 | // Signals before this were at our own process. |
| 195 | tb.sent = nil |
| 196 | } |
| 197 | } |
| 198 | turns, u, x, run := liveTurn(t, tb, &Own{}, 200*time.Millisecond) |
| 199 | tb.add(row(91001, 1, 91001, 91001, 6001, me, "attempt-l"), "/Users/paulequipo/w/m") |
| 200 | if run.Stop() { |
| 201 | t.Fatal("proven over while another user holds what it made") |
| 202 | } |
| 203 | u.Give() |
| 204 | // Nothing of the runner user's is left: the Mac takes turns |
| 205 | // again, and the attempt still owes what another user holds. |
| 206 | next, err := turns.Take(context.Background(), OpCheck) |
| 207 | if err != nil { |
| 208 | t.Fatalf("the Mac stays blocked on another user's process: %v", err) |
| 209 | } |
| 210 | next.Give() |
| 211 | if x.Verify(RecordID(0, "attempt-l")) { |
| 212 | t.Fatal("recovery proved it over while that lasts") |
| 213 | } |
| 214 | untouched(t, tb.signalled(), c.other.PID) |
| 215 | tb.remove(c.other.PID) |
| 216 | if !x.Verify(RecordID(0, "attempt-l")) { |
| 217 | t.Fatal("not proven once it ended") |
| 218 | } |
| 219 | }) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // slowTable's List takes delay and then shows nothing: a read that |
| 224 | // returns after the deadline with good news. |
| 225 | type slowTable struct { |
| 226 | *table |
| 227 | delay time.Duration |
| 228 | slow atomic.Bool |
| 229 | } |
| 230 | |
| 231 | func (s *slowTable) List(deadline time.Time, markers []string) ([]ProcInfo, error) { |
| 232 | if s.slow.Load() { |
| 233 | time.Sleep(s.delay) |
| 234 | return nil, nil |
| 235 | } |
| 236 | return s.table.List(deadline, markers) |
| 237 | } |
| 238 | |
| 239 | // #7: one absolute deadline: a table read that returns after it never |
| 240 | // makes a proof, in the live stop, in recovery, or settling a turn. |
| 241 | func TestProofNeverAfterItsDeadline(t *testing.T) { |
| 242 | tb := &slowTable{table: parseTable(t, turnBegan), delay: 250 * time.Millisecond} |
| 243 | x := &Executor{Procs: tb, Own: &Own{}, Grace: 10 * time.Millisecond, Settle: 100 * time.Millisecond, Poll: time.Hour} |
| 244 | run, err := x.Start(context.Background(), Command{Argv: []string{"/bin/true"}}, "attempt-slow", func(string) error { return nil }) |
| 245 | if err != nil { |
| 246 | t.Fatal(err) |
| 247 | } |
| 248 | <-run.Exited() |
| 249 | tb.slow.Store(true) |
| 250 | if run.Stop() { |
| 251 | t.Fatal("the live stop proved it after its deadline") |
| 252 | } |
| 253 | if x.Verify(RecordID(0, "attempt-slow")) { |
| 254 | t.Fatal("recovery proved it after its deadline") |
| 255 | } |
| 256 | turns := NewTurns("").Sweep(tb) |
| 257 | turns.own, turns.Settle = &Own{}, 100*time.Millisecond |
| 258 | if err := turns.saveTurn(turnRecord{ID: "old", Op: OpEngineering, Runner: selfID(), Sweep: true, Before: []ProcID{}, Markers: []string{"attempt-slow"}}); err != nil { |
| 259 | t.Fatal(err) |
| 260 | } |
| 261 | if u, err := turns.Take(context.Background(), OpCheck); err == nil { |
| 262 | u.Give() |
| 263 | t.Fatal("an earlier turn was settled after the deadline") |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // #10: only a process that started after the first kill pass began can |
| 268 | // be a relaunch. One with a system path and launchd as its parent that |
| 269 | // started before it (here missing from that pass's read) is killed. |
| 270 | func TestSweepRelaunchOnlyAfterTheKillPass(t *testing.T) { |
| 271 | me := os.Getuid() |
| 272 | tb := parseTable(t, turnBegan) |
| 273 | tb.after = func(tb *table, pid int, sig syscall.Signal) { |
| 274 | if pid == 82500 || pid == -82500 { |
| 275 | delete(tb.hidden, 82400) |
| 276 | } |
| 277 | } |
| 278 | _, u, _, run := liveTurn(t, tb, &Own{}, time.Second) |
| 279 | defer u.Give() |
| 280 | tb.mark(&tb.hidden, 82400) |
| 281 | tb.add(row(82400, 1, 82400, 82400, 5400, me, ""), "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc") |
| 282 | tb.add(row(82500, 1, 82500, 82500, 5500, me, ""), "/Users/paulequipo/w/first") |
| 283 | if !run.Stop() { |
| 284 | t.Fatalf("not proven: %v", tb.signalled()) |
| 285 | } |
| 286 | if sig := tb.signalled(); !slices.Contains(sig, 82400) && !slices.Contains(sig, -82400) || tb.has(82400) { |
| 287 | t.Fatalf("a process from before the kill pass was taken for a relaunch: %v", tb.signalled()) |
| 288 | } |
| 289 | } |
| 290 | |