oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package mac
2
3import (
4 "context"
5 "errors"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/runner/mac/proc"
13)
14
15// Follow-up fix round 1 (Astra 9f round 1). Synthetic tables, rows made
16// up 2026-11-29 in the darwin table's shape (see sweep_test.go).
17
18// #1: a process that left the runner user (a setuid program that called
19// setuid), in no group or session the record got, is invisible to an
20// idle read. A setuid program cannot run under Seatbelt on 27.2 (task 1,
21// R4), so an idle read clears only a turn whose record shows nothing ran
22// outside a sandbox; any other clears only by a reboot.
23func TestIdleRefusedWhereUnsandboxedCodeRan(t *testing.T) {
24 for name, c := range map[string]struct {
25 edit func(*proc.Obligation)
26 clear bool
27 }{
28 "unsandboxed code ran": {func(o *proc.Obligation) { o.UnsandboxedRan = true }, false},
29 "the flag not on record": {func(o *proc.Obligation) { o.FlagsKnown = false }, false},
30 "everything in a sandbox": {func(*proc.Obligation) {}, true},
31 } {
32 tb := parseTable(t, turnBegan)
33 // The survivor, now root's, that the record never got.
34 tb.add(row(99001, 1, 99001, 99001, 5000, 0, ""), "/usr/bin/login")
35 turns, x, _, before := clearRig(t, tb, sameBoot, nil)
36 o := owed(before)
37 c.edit(&o)
38 if err := turns.debts.Owe(o); err != nil {
39 t.Fatal(err)
40 }
41 if got := x.Verify(RecordID(0, "attempt-c")); got != c.clear {
42 t.Fatalf("%s: proven %v, want %v", name, got, c.clear)
43 }
44 }
45}
46
47// #1: an unsandboxed check puts the flag on its turn's record before
48// anything of it starts; a flag that cannot be written starts nothing;
49// a sandboxed check leaves it unset.
50func TestUnsandboxedCheckFlagsItsTurnFirst(t *testing.T) {
51 for _, sandboxed := range []bool{false, true} {
52 g, dir := gateFixture(t, sandboxed)
53 g.Turns = NewTurns(filepath.Join(t.TempDir(), "lock"))
54 got := false
55 res := g.Run(context.Background(), "checks-u-2", dir, []string{"/bin/sh", "-c", "true"}, time.Minute, func(id string) error {
56 if strings.HasPrefix(id, "pg:0:") {
57 r, ok, _ := g.Turns.loadTurn()
58 got = ok && r.FlagsKnown && r.UnsandboxedRan
59 }
60 return nil
61 })
62 if res.Code != 0 || got == sandboxed {
63 t.Fatalf("sandboxed %v: flag on record before the start %v (%+v)", sandboxed, got, res)
64 }
65 }
66 g, dir := gateFixture(t, false)
67 g.Turns = NewTurns(filepath.Join(t.TempDir(), "lock"))
68 writes := 0
69 g.Turns.beforeWrite = func() error {
70 // The turn's own record, then the flag.
71 if writes++; writes == 2 {
72 return errors.New("disk full")
73 }
74 return nil
75 }
76 ran := filepath.Join(t.TempDir(), "ran")
77 if res, _ := run(g, dir, "checks-u-3", time.Minute, "touch "+quote(ran)); res.Reason != "checks_unavailable" || exists(ran) {
78 t.Fatalf("ran without its flag on record: %+v", res)
79 }
80}
81
82// #2: both idle reads and the acceptance happen holding the turn. Every
83// exempt process predates every unresolved turn, and every process of
84// the old turn was absent from both reads, so none can fork after the
85// second: a process an exempt service starts after it is not a
86// survivor. The clearance stands, and what the turn owed was gone.
87func TestAcceptanceAfterTheSecondRead(t *testing.T) {
88 me := os.Getuid()
89 tb := parseTable(t, turnBegan)
90 turns, x, _, before := clearRig(t, tb, sameBoot, nil)
91 o := owed(before)
92 o.Groups, o.Killed = proc.Span{99100: 4000}, []ProcID{{PID: 99101, Start: 4001}}
93 if err := turns.debts.Owe(o); err != nil {
94 t.Fatal(err)
95 }
96 reads := 0
97 tb.afterList = func(tb *table, _ int, _ []string) {
98 // The recovery turn's read, then the two idle reads: right after
99 // the second, the exempt trustd (40200) starts a process.
100 if reads++; reads == 3 {
101 tb.procs[99200] = row(99200, 40200, 99200, 99200, 9000, me, "")
102 tb.paths[99200] = "/usr/libexec/trustd-helper"
103 }
104 }
105 if !x.Verify(RecordID(0, "attempt-c")) || turns.debts.Owes("attempt-c") {
106 t.Fatal("not proven over")
107 }
108 if !tb.has(99200) {
109 t.Fatal("the service's new process was touched")
110 }
111 for pid, q := range tb.procs {
112 if q.PGID == 99100 || pid == 99101 {
113 t.Fatalf("what the turn owed was not gone: %+v", q)
114 }
115 }
116}
117
118// #3: a process is exempt only if it existed before the earliest
119// unresolved turn began. P, born in turn A and still owed by A, is in
120// later turn B's table: B does not clear while P lives. Once A and B are
121// resolved, a later turn's table exempts as before.
122func TestEarliestUnresolvedTurnIsTheBaseline(t *testing.T) {
123 me := os.Getuid()
124 tb := parseTable(t, turnBegan)
125 turns, x, _, beforeA := clearRig(t, tb, sameBoot, nil)
126 p := row(99300, 1, 99300, 99300, 5000, me, "")
127 tb.add(p, "/Users/paulequipo/w/p")
128 beforeB := append(append([]ProcID{}, beforeA...), p.ID)
129 a := owed(beforeA)
130 a.Markers = []string{"attempt-a"}
131 b := owed(beforeB)
132 b.Markers, b.Began = []string{"attempt-b"}, turnStart.Add(time.Hour)
133 for _, o := range []proc.Obligation{a, b} {
134 if err := turns.debts.Owe(o); err != nil {
135 t.Fatal(err)
136 }
137 }
138 if x.Verify(RecordID(0, "attempt-b")) {
139 t.Fatal("B cleared while A's survivor lives")
140 }
141 tb.remove(p.PID)
142 if !x.Verify(RecordID(0, "attempt-a")) || !x.Verify(RecordID(0, "attempt-b")) {
143 t.Fatal("A and B not proven once P is gone")
144 }
145 r := row(99301, 1, 99301, 99301, 6000, me, "")
146 tb.add(r, "/Users/paulequipo/w/r")
147 c := owed(append(append([]ProcID{}, beforeA...), r.ID))
148 c.Markers, c.Began = []string{"attempt-c2"}, turnStart.Add(2*time.Hour)
149 if err := turns.debts.Owe(c); err != nil {
150 t.Fatal(err)
151 }
152 if !x.Verify(RecordID(0, "attempt-c2")) {
153 t.Fatal("with nothing older owed, a turn's own table no longer exempts")
154 }
155}
156
157// #4: a proven reboot is a clearance qualified by boot: the old boot's
158// process checks no longer apply. Its recorded group id, now another
159// process's in this boot, neither blocks the proof nor is signalled; nor
160// does a pending user-temp cleanup's.
161func TestRebootIgnoresTheOldBootsGroups(t *testing.T) {
162 me := os.Getuid()
163 for _, viaRecord := range []bool{true, false} {
164 tb := parseTable(t, turnBegan)
165 turns, x, _, before := clearRig(t, tb, laterBoot, nil)
166 tb.add(row(99400, 1, 99400, 99400, 5000, me, ""), "/Users/paulequipo/w/this-boot")
167 if viaRecord {
168 if err := turns.saveTurn(turnRecord{ID: "old", Op: OpEngineering, Runner: selfID(), Sweep: true, Before: before, Markers: []string{"attempt-c"}, Began: turnStart, BootID: "boot-1"}); err != nil {
169 t.Fatal(err)
170 }
171 } else {
172 if err := turns.debts.Owe(owed(before)); err != nil {
173 t.Fatal(err)
174 }
175 // And a user-temp cleanup the old boot's attempt left.
176 _, temp := userTempFixture(t)
177 x.UserTemp = temp
178 s, err := openTemp(temp, nil)
179 if err != nil {
180 t.Fatal(err)
181 }
182 if err := turns.writePending(pendingRecord{Attempt: "attempt-c", PGID: 99400, Before: s.before, Runner: ProcID{PID: 1 << 30, Start: 1}, Began: turnStart, BootID: "boot-1"}); err != nil {
183 t.Fatal(err)
184 }
185 s.close()
186 }
187 if !x.Verify(RecordID(99400, "attempt-c")) || turns.Pending() {
188 t.Fatalf("record %v: a reused group id of the old boot blocked a proven reboot", viaRecord)
189 }
190 // Proven again after a runner restart, twice: the completion stays
191 // until both consumers have acknowledged it.
192 again := NewTurns(turns.lock).Sweep(tb)
193 again.own, again.Settle, again.boot = turns.own, turns.Settle, turns.boot
194 y := &Executor{Turns: again, Settle: x.Settle}
195 first, second := y.Verify(RecordID(99400, "attempt-c")), y.Verify(RecordID(99400, "attempt-c"))
196 if !first || !second {
197 t.Fatalf("record %v: a later proof of the same record failed", viaRecord)
198 }
199 y.Acknowledge(RecordID(99400, "attempt-c"), ConsumerStep)
200 if !again.debts.Owes("attempt-c") {
201 t.Fatalf("record %v: the completion went before the Keychain acknowledged it", viaRecord)
202 }
203 y.Acknowledge(RecordID(99400, "attempt-c"), ConsumerKeychain)
204 if again.debts.Owes("attempt-c") {
205 t.Fatalf("record %v: the completion stayed after every consumer had it", viaRecord)
206 }
207 if sig := tb.signalled(); len(sig) != 0 {
208 t.Fatalf("record %v: signalled %v", viaRecord, sig)
209 }
210 }
211}
212
213// #5: the clearance is under Verify's one deadline and removes nothing
214// unless the whole proof succeeds in time: a read slow enough to spend
215// the budget leaves the debt in place.
216func TestClearanceUnderTheOneDeadline(t *testing.T) {
217 tb := &slowTable{table: parseTable(t, turnBegan), delay: 120 * time.Millisecond}
218 turns, x, _, before := clearRig(t, tb, sameBoot, nil)
219 if err := turns.debts.Owe(owed(before)); err != nil {
220 t.Fatal(err)
221 }
222 tb.slow.Store(true)
223 if x.Verify(RecordID(0, "attempt-c")) {
224 t.Fatal("proven after its deadline")
225 }
226 tb.slow.Store(false)
227 if !turns.debts.Owes("attempt-c") {
228 t.Fatal("the debt went though the proof expired")
229 }
230}
231
232// #1: the flag goes from the turn's record into what it owes once a
233// restart settles it, so an unsandboxed turn left incomplete is never
234// idle-cleared later.
235func TestUnsandboxedFlagSurvivesIntoTheDebt(t *testing.T) {
236 tb := parseTable(t, turnBegan)
237 turns, x, _, before := clearRig(t, tb, sameBoot, nil)
238 if err := turns.saveTurn(turnRecord{ID: "old", Op: OpCheck, Runner: selfID(), Sweep: true, Before: before, Markers: []string{"attempt-c"}, Began: turnStart, BootID: "boot-1", FlagsKnown: true, UnsandboxedRan: true}); err != nil {
239 t.Fatal(err)
240 }
241 if x.Verify(RecordID(0, "attempt-c")) || !turns.Incomplete() {
242 t.Fatal("an unsandboxed turn, left incomplete, was idle-cleared")
243 }
244}
245
246// #5: the debt goes only with the whole proof: met by an idle read, but
247// its recorded group, held by a process from before the turn, still
248// alive, the proof fails and the debt stays; once that group ends, the
249// proof holds and the debt goes.
250func TestDebtKeptWhenTheRestOfTheProofFails(t *testing.T) {
251 me := os.Getuid()
252 tb := parseTable(t, turnBegan)
253 tb.add(row(99500, 1, 99500, 99500, 900, me, ""), "/Users/paulequipo/w/old")
254 turns, x, _, before := clearRig(t, tb, sameBoot, nil)
255 if err := turns.debts.Owe(owed(before)); err != nil {
256 t.Fatal(err)
257 }
258 if x.Verify(RecordID(99500, "attempt-c")) || !turns.debts.Owes("attempt-c") {
259 t.Fatal("the debt went with the proof not whole")
260 }
261 tb.remove(99500)
262 if !x.Verify(RecordID(99500, "attempt-c")) || turns.debts.Owes("attempt-c") {
263 t.Fatal("not proven, or the debt stayed, once everything ended")
264 }
265}
266