package mac import ( "context" "os" "slices" "testing" "time" ) // Synthetic process tables. No table of the Studio's processes was // recorded (task 1 recorded no ps output, only that Bash commands run in // sessions of their own and that macOS 27.2 hides platform binaries' // environment), so these rows are made up, 2026-11-28, in the shape the // darwin table reports: ps -axo pid,ppid,pgid,sess,lstart,uid,comm, with // lstart as the kernel's start in microseconds and comm as the kernel's // path (proc_pidpath), not argv[0]. // turnBegan is the runner user's table when the turn begins: the runner, // two launchd agents and another user's shell. const turnBegan = ` # pid ppid pgid sess start uid path self 1 self self 1000 me /Users/paulequipo/.local/lib/equipo-runner/equipo-runner 40100 1 40100 40100 900 me /usr/sbin/cfprefsd 40200 1 40200 40200 950 me /usr/libexec/trustd 40300 1 40300 40300 960 501 /bin/zsh ` // sweepTurn begins a turn on tb (its table read as before), lets during // add what the turn starts, and leaves the turn unproven, so the next // take settles it with the sweep. It returns the signals sent. func sweepTurn(t *testing.T, tb *table, own *Own, during func(*Turn)) []int { t.Helper() turns := NewTurns("").Sweep(tb) turns.own, turns.Settle = own, 300*time.Millisecond u, err := turns.Take(context.Background(), OpEngineering) if err != nil { t.Fatal(err) } if err := u.launch("attempt-s"); err != nil { t.Fatal(err) } during(u) u.unprovenEnd() u.Give() _ = turns.settle(time.Now().Add(turns.settleLimit())) return tb.signalled() } func row(pid, ppid, pgid, sid int, start int64, uid int, marker string) ProcInfo { return ProcInfo{ID: ProcID{PID: pid, Start: start}, PPID: ppid, PGID: pgid, SID: sid, UID: uid, Marker: marker} } // The runner and its own helpers are spared by identity (pid and start), // with what they start; a step's process running the same program is // not, nor a process that has a helper's pid but another start. func TestSweepSparesOwnByIdentityNotName(t *testing.T) { me, self := os.Getuid(), os.Getpid() tb := parseTable(t, turnBegan) own := &Own{} sig := sweepTurn(t, tb, own, func(*Turn) { tb.add(row(50100, self, self, self, 2000, me, ""), "/usr/bin/git") own.Add(ProcID{PID: 50100, Start: 2000}) tb.add(row(50101, 50100, self, self, 2001, me, ""), "/usr/libexec/git-core/git-remote-http") tb.add(row(50200, 1, 50200, 50200, 2100, me, ""), "/usr/bin/git") tb.add(row(50300, self, self, self, 2200, me, ""), "/usr/bin/xcrun") }) for _, pid := range []int{self, 50100, 50101, 40100, 40200, 40300} { if slices.Contains(sig, pid) || slices.Contains(sig, -pid) { t.Fatalf("signalled %d, the runner's or from before the turn: %v", pid, sig) } } for _, pid := range []int{50200, 50300} { if !slices.Contains(sig, pid) && !slices.Contains(sig, -pid) { t.Fatalf("%d (not the runner's, whatever its name) was spared: %v", pid, sig) } } // A helper's pid used again by a step's process: another identity. tb = parseTable(t, turnBegan) own = &Own{} sig = sweepTurn(t, tb, own, func(*Turn) { own.Add(ProcID{PID: 50100, Start: 2000}) tb.add(row(50100, 1, 50100, 50100, 2500, me, ""), "/usr/bin/git") }) if !slices.Contains(sig, 50100) && !slices.Contains(sig, -50100) { t.Fatalf("a reused helper pid was spared: %v", sig) } } // Each signal follows a fresh read of that process: a pid used again // between the table and the signal (here by another user's process with // a later start, or the same process that changed user) is never // signalled, nor its group. func TestSweepRechecksIdentityBeforeEachSignal(t *testing.T) { me := os.Getuid() tb := parseTable(t, turnBegan) sig := sweepTurn(t, tb, &Own{}, func(u *Turn) { tb.add(row(60100, 1, 60100, 60100, 3000, me, ""), "/Users/paulequipo/w/escape") tb.add(row(60200, 1, 60200, 60200, 3010, me, "attempt-s"), "/Users/paulequipo/w/marked") u.note(span{60200: 3010}, nil) tb.add(row(60300, 1, 60300, 60300, 3020, me, ""), "/Users/paulequipo/w/setuid") reused := map[int]bool{} tb.lookup = func(tb *table, pid int) { tb.mu.Lock() defer tb.mu.Unlock() if reused[pid] { return } switch pid { case 60100, 60200: reused[pid] = true tb.procs[pid] = row(pid, 1, pid, pid, 9000, 501, "") tb.paths[pid] = "/bin/zsh" case 60300: reused[pid] = true q := tb.procs[pid] q.UID = 0 tb.procs[pid] = q } } }) for _, pid := range []int{60100, -60100, 60200, -60200, 60300} { if slices.Contains(sig, pid) { t.Fatalf("signalled %d after it stopped being the process listed: %v", pid, sig) } } } // Another user's processes are never signalled, even started during the // turn, holding an id the attempt's group had, or showing its marker; a // group id of the attempt's now used by another user keeps the proof // waiting. func TestSweepNeverTouchesOtherUsers(t *testing.T) { tb := parseTable(t, turnBegan) var left []ProcInfo sig := sweepTurn(t, tb, &Own{}, func(u *Turn) { u.note(span{70300: 3900}, span{70300: 3900}) tb.add(row(70100, 1, 70100, 70100, 4000, 501, ""), "/Users/other/evil") tb.add(row(70300, 1, 70300, 70300, 4100, 501, ""), "/bin/zsh") tb.add(row(70400, 70300, 70300, 70300, 4200, 0, "attempt-s"), "/usr/bin/login") s := newScope(tb, []string{"attempt-s"}, u.before()) s.Groups[70300] = 3900 list, _ := tb.List(time.Time{}, []string{"attempt-s"}) left = s.Targets(list, nil) if s.Gone() { t.Error("proof given while another user holds a group id the attempt made") } }) if len(left) != 0 || len(sig) != 0 { t.Fatalf("another user's process was a target %v or signalled %v", left, sig) } } // #10 (the controller's ruling): no exemption by path. Every process of // the runner user that started during the turn and is not the runner's // own is the turn's: launchd-parented ones under /System, /usr/libexec // or /usr/sbin (on-demand agents launchd starts again when needed), a // double-fork from the step's own tree, a /System/Volumes path, a path // that climbs out, an unknown path. What was there before is untouched. func TestSweepKillsEveryNewProcessWhateverItsPath(t *testing.T) { me := os.Getuid() tb := parseTable(t, turnBegan) sig := sweepTurn(t, tb, &Own{}, func(u *Turn) { u.note(nil, span{81000: 5000}) tb.add(row(80100, 1, 80100, 80100, 5100, me, ""), "/System/Library/PrivateFrameworks/X.framework/XPCServices/Y.xpc/Contents/MacOS/Y") tb.add(row(80200, 1, 80200, 80200, 5200, me, ""), "/usr/libexec/xpcproxy") tb.add(row(80250, 1, 80250, 80250, 5250, me, ""), "/usr/sbin/cfprefsd") tb.add(row(80300, 1, 80300, 80300, 5300, me, ""), "/Users/paulequipo/w/tree/.build/escape") tb.add(row(80400, 1, 80400, 80400, 5400, me, ""), "/System/Volumes/Data/Users/paulequipo/escape") tb.add(row(80500, 1, 80500, 80500, 5500, me, ""), "/System/Library/../../Users/paulequipo/escape") tb.add(row(80600, 80300, 80600, 80600, 5600, me, ""), "/usr/libexec/xpcproxy") tb.add(row(80700, 1, 80700, 80700, 5700, me, ""), "") tb.add(row(80800, 1, 80800, 81000, 5800, me, ""), "/usr/libexec/xpcproxy") }) for _, pid := range []int{80100, 80200, 80250, 80300, 80400, 80500, 80600, 80700, 80800} { if !slices.Contains(sig, pid) && !slices.Contains(sig, -pid) { t.Fatalf("%d, new during the turn, was spared: %v", pid, sig) } } for _, pid := range []int{40100, 40200, 40300} { if slices.Contains(sig, pid) || slices.Contains(sig, -pid) { t.Fatalf("%d, there before the turn, was signalled: %v", pid, sig) } } } // The live proof waits for every group the attempt made: one its // process made (and that is gone) but whose id another user now holds // keeps the end unproven until that group ends too. func TestStopWaitsForEveryGroupItMade(t *testing.T) { me := os.Getuid() tb := parseTable(t, turnBegan) x := &Executor{Procs: tb, Own: &Own{}, Grace: 50 * time.Millisecond, Settle: 400 * time.Millisecond, Poll: 10 * time.Millisecond} var leader int run, err := x.Start(context.Background(), Command{Argv: []string{"/bin/sleep", "0.3"}}, "attempt-g", func(id string) error { if pgid, _, _ := parseRecord(id); pgid > 0 { leader = pgid tb.add(row(pgid, os.Getpid(), pgid, pgid, 6000, me, ""), "/bin/sleep") tb.add(row(90000, pgid, 90000, pgid, 6001, me, ""), "/bin/bash") } return nil }) if err != nil { t.Fatal(err) } for deadline := time.Now().Add(2 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) { run.mu.Lock() _, made := run.scope.Groups[90000] run.mu.Unlock() if made { break } } <-run.Exited() tb.mu.Lock() delete(tb.procs, leader) delete(tb.procs, 90000) tb.procs[90000] = row(90000, 1, 90000, 90000, 7000, 501, "") tb.mu.Unlock() if run.Stop() { t.Fatal("proven while a group the attempt made is held by another user") } if slices.Contains(tb.signalled(), 90000) || slices.Contains(tb.signalled(), -90000) { t.Fatal("the other user's group was signalled") } }