| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "syscall" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | // However attest verify ends while the cue it started to vet statements |
| 15 | // runs, that cue ends with it: the system kills it as attest dies |
| 16 | // (dieWithParent), and this process, the reaper of what attest leaves, |
| 17 | // waits for it. The cue is a real process attest started, not a stand-in |
| 18 | // for attest. |
| 19 | func TestVerifysCueEndsWithIt(t *testing.T) { |
| 20 | for _, signal := range []syscall.Signal{syscall.SIGTERM, syscall.SIGKILL} { |
| 21 | t.Run(signal.String(), func(t *testing.T) { |
| 22 | reapOrphans(t) |
| 23 | r := newVerifyRig(t, "repo", true) |
| 24 | fifo := filepath.Join(r.dir, "cue.pid") |
| 25 | if err := syscall.Mkfifo(fifo, 0o600); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | one := r.note("one", r.statement("one", r.tree, "", ""), r.worker) |
| 29 | two := r.note("two", r.statement("two", r.tree, "", ""), r.worker) |
| 30 | cmd := exec.Command(os.Args[0], "verify", "--repo", r.repo, "--known-keys", r.known, |
| 31 | "--schema", r.schema, "--verdicts", "--note", one, "--note", two) |
| 32 | cmd.Env = append(os.Environ(), "ATTEST_AS_MAIN=1", "ATTEST_CUE_STALL="+fifo, |
| 33 | "PATH="+r.bin+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 34 | if err := cmd.Start(); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | exited := make(chan error, 1) |
| 38 | go func() { exited <- cmd.Wait() }() |
| 39 | said := make(chan int, 1) |
| 40 | go func() { |
| 41 | data, _ := os.ReadFile(fifo) |
| 42 | n, _ := strconv.Atoi(strings.TrimSpace(string(data))) |
| 43 | said <- n |
| 44 | }() |
| 45 | var vetting int |
| 46 | select { |
| 47 | case vetting = <-said: |
| 48 | case err := <-exited: |
| 49 | t.Fatalf("attest ended before it started cue: %v", err) |
| 50 | case <-time.After(time.Minute): |
| 51 | cmd.Process.Kill() |
| 52 | <-exited |
| 53 | t.Fatal("attest did not start cue in a minute") |
| 54 | } |
| 55 | cmd.Process.Signal(signal) |
| 56 | <-exited |
| 57 | if ws, ok := waitOrphan(t, vetting); ok && (!ws.Signaled() || ws.Signal() != syscall.SIGKILL) { |
| 58 | t.Errorf("the cue ended with %v, not killed as attest died", ws) |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // A run whose verdicts cannot be written gives its reader no verdict: |
| 65 | // it exits non-zero, though every note verified. |
| 66 | func TestVerdictsThatCannotBeWrittenFailTheRun(t *testing.T) { |
| 67 | r := newVerifyRig(t, "repo", true) |
| 68 | one := r.note("one", r.statement("one", r.tree, "", ""), r.worker) |
| 69 | full, err := os.OpenFile("/dev/full", os.O_WRONLY, 0) |
| 70 | if err != nil { |
| 71 | t.Skip("no /dev/full to write to") |
| 72 | } |
| 73 | defer full.Close() |
| 74 | cmd := exec.Command(os.Args[0], "verify", "--repo", r.repo, "--known-keys", r.known, |
| 75 | "--schema", r.schema, "--verdicts", "--note", one) |
| 76 | cmd.Env = append(os.Environ(), "ATTEST_AS_MAIN=1", "ATTEST_CUE_LOG="+r.log, |
| 77 | "PATH="+r.bin+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 78 | cmd.Stdout = full |
| 79 | var stderr strings.Builder |
| 80 | cmd.Stderr = &stderr |
| 81 | if err := cmd.Run(); err == nil { |
| 82 | t.Errorf("attest verify --verdicts exited 0 with its verdicts unwritten: %q", stderr.String()) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // reapOrphans makes this process the reaper of the processes its children |
| 87 | // leave when they die (PR_SET_CHILD_SUBREAPER), until the test ends. |
| 88 | func reapOrphans(t *testing.T) { |
| 89 | t.Helper() |
| 90 | const setChildSubreaper = 36 |
| 91 | if _, _, e := syscall.RawSyscall(syscall.SYS_PRCTL, setChildSubreaper, 1, 0); e != 0 { |
| 92 | t.Fatalf("could not reap orphans: %v", e) |
| 93 | } |
| 94 | t.Cleanup(func() { syscall.RawSyscall(syscall.SYS_PRCTL, setChildSubreaper, 0, 0) }) |
| 95 | } |
| 96 | |
| 97 | // waitOrphan waits for pid, a process reapOrphans makes this one's, to |
| 98 | // end. One still running after a minute has outlived what started it: it |
| 99 | // is killed, and ok is false. |
| 100 | func waitOrphan(t *testing.T, pid int) (ws syscall.WaitStatus, ok bool) { |
| 101 | t.Helper() |
| 102 | ended := make(chan syscall.WaitStatus, 1) |
| 103 | go func() { |
| 104 | var ws syscall.WaitStatus |
| 105 | syscall.Wait4(pid, &ws, 0, nil) |
| 106 | ended <- ws |
| 107 | }() |
| 108 | select { |
| 109 | case ws := <-ended: |
| 110 | return ws, true |
| 111 | case <-time.After(time.Minute): |
| 112 | syscall.Kill(pid, syscall.SIGKILL) |
| 113 | <-ended |
| 114 | t.Errorf("process %d outlived what started it", pid) |
| 115 | return 0, false |
| 116 | } |
| 117 | } |