cosca 0.8.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::cell::Cell;
use std::io;

use super::{kill_gate, terminate_gate, Front, Gate};
use crate::elevation::elevation_tests::report;
use crate::elevation::{Backend, ElevatedVia, ElevationReport};
use crate::error::{ElevationErrorKind, Error};

const PID: u32 = 4242;

/// The fronts a report names, and no other: pkexec execs the program, and UAC and an already-elevated
/// parent track it. Mutant: a backend's arm of `ElevationReport::front` answers another's.
#[skuld::test]
fn a_report_names_the_front_its_via_leaves_tracked() {
    for (via, name) in FRONTS {
        assert_eq!(report(via.clone()).front().map(Front::name), Some(name), "{via:?}");
    }
    for via in NOT_FRONTS.iter().flatten() {
        assert_eq!(report(via.clone()).front(), None, "{via:?}");
    }
}

/// Sudo, doas, pkexec and UAC may run the tracked process as another user; osascript's front runs
/// as this user, and an already-elevated child is the caller's own. Mutant: an arm of
/// `ElevationReport::may_run_as_another_user` flips.
#[skuld::test]
fn a_report_says_whether_the_tracked_process_may_run_as_another_user() {
    let runs_as_another_user = |via: ElevatedVia| report(via).may_run_as_another_user();
    for backend in [Backend::Sudo, Backend::Doas, Backend::Pkexec] {
        assert!(runs_as_another_user(ElevatedVia::Wrapped(backend)), "{backend:?}");
    }
    assert!(runs_as_another_user(ElevatedVia::WindowsUac));
    assert!(!runs_as_another_user(ElevatedVia::MacosOsascript));
    assert!(!runs_as_another_user(ElevatedVia::AlreadyElevated));
}

const FRONTS: [(ElevatedVia, &str); 3] = [
    (ElevatedVia::Wrapped(Backend::Sudo), "sudo"),
    (ElevatedVia::Wrapped(Backend::Doas), "doas"),
    (ElevatedVia::MacosOsascript, "osascript"),
];

const NOT_FRONTS: [Option<ElevatedVia>; 4] = [
    None,
    Some(ElevatedVia::Wrapped(Backend::Pkexec)),
    Some(ElevatedVia::WindowsUac),
    Some(ElevatedVia::AlreadyElevated),
];

/// `answers` in turn, one per call; the last is repeated once they run out.
fn in_turn(answers: Vec<io::Result<bool>>) -> impl Fn() -> io::Result<bool> {
    let answers: Vec<_> = answers
        .into_iter()
        .map(|a| a.map_err(|e| (e.kind(), e.to_string())))
        .collect();
    let next = Cell::new(0);
    move || {
        let i = next.get().min(answers.len() - 1);
        next.set(next.get() + 1);
        answers[i].clone().map_err(|(kind, text)| io::Error::new(kind, text))
    }
}

/// What a gate asked, in order: `r` for `running`, `c` for `in_cgroup`.
fn kill_gate_asking(
    report: Option<ElevationReport>,
    running: io::Result<bool>,
    in_cgroup: io::Result<bool>,
) -> (Gate, String) {
    kill_gate_asking_in_turn(report, vec![running], in_cgroup)
}

/// [`kill_gate_asking`], with `running` answering in turn.
fn kill_gate_asking_in_turn(
    report: Option<ElevationReport>,
    running: Vec<io::Result<bool>>,
    in_cgroup: io::Result<bool>,
) -> (Gate, String) {
    let asked = Cell::new(String::new());
    let note = |c: char| {
        let mut s = asked.take();
        s.push(c);
        asked.set(s);
    };
    let running = in_turn(running);
    let g = kill_gate(
        report.and_then(|report| report.front()),
        PID,
        || {
            note('r');
            running()
        },
        || {
            note('c');
            in_cgroup
        },
    );
    (g, asked.take())
}

fn refusal_detail(g: Gate) -> String {
    match g {
        Gate::Closed(Error::Elevation {
            kind: ElevationErrorKind::Unkillable,
            detail,
            ..
        }) => detail,
        other => panic!("expected a closed gate with Unkillable, got {other:?}"),
    }
}

#[skuld::test]
fn a_live_front_outside_a_cgroup_is_closed_with_unkillable_naming_it() {
    for (via, name) in FRONTS {
        let (g, asked) = kill_gate_asking(Some(report(via.clone())), Ok(true), Ok(false));
        assert_eq!(asked, "rcr", "{via:?}");
        let detail = refusal_detail(g);
        assert!(detail.contains(&format!("pid {PID} is")), "{detail}");
        assert!(detail.contains(name), "{detail}");
        assert!(detail.contains("no kill was sent"), "{detail}");
        assert_eq!(
            detail.contains("direct exec"),
            name != "osascript",
            "only a wrapper may track the program itself: {detail}"
        );
    }
}

/// A front that has left its cgroup (pam_systemd moving sudo into a session scope) is outside it.
#[skuld::test]
fn a_live_front_that_left_its_cgroup_is_closed() {
    let (g, _) = kill_gate_asking(Some(report(ElevatedVia::Wrapped(Backend::Sudo))), Ok(true), Ok(false));
    refusal_detail(g);
}

/// An exited front has nothing left to orphan, and its cgroup is not asked about.
#[skuld::test]
fn an_exited_front_is_exited() {
    for (via, _) in FRONTS {
        let (g, asked) = kill_gate_asking(Some(report(via.clone())), Ok(false), Ok(true));
        assert_eq!(asked, "r", "{via:?}");
        assert!(matches!(g, Gate::Exited), "{via:?}: {g:?}");
    }
}

/// A cgroup kill reaches a member whatever its credentials, so a live front in one, or one whose
/// state cannot be read, is killed through it.
#[skuld::test]
fn a_live_front_in_its_cgroup_is_killed_through_the_cgroup_only() {
    for (via, _) in FRONTS {
        for running in [Ok(true), Err(io::Error::other("peek refused"))] {
            let (g, asked) = kill_gate_asking(Some(report(via.clone())), running, Ok(true));
            assert_eq!(asked, "rc", "{via:?}");
            assert!(
                matches!(&g, Gate::CgroupOnly(f) if Some(*f) == report(via.clone()).front()),
                "{via:?}: {g:?}"
            );
        }
    }
}

const CODE: i32 = 5;

fn os_error() -> io::Error {
    io::Error::from_raw_os_error(CODE)
}

fn refusal(g: Gate) -> Error {
    match g {
        Gate::Closed(error) => error,
        other => panic!("expected a closed gate, got {other:?}"),
    }
}

#[track_caller]
fn assert_refused_by_one_read(error: &Error, words: &str) {
    let Error::Elevation {
        kind: ElevationErrorKind::Unkillable,
        detail,
        ..
    } = error
    else {
        panic!("expected Unkillable, got {error:?}");
    };
    assert!(detail.contains(words), "{detail}");
    assert!(
        !detail.contains(&os_error().to_string()),
        "the cause is in the detail: {detail}"
    );
    assert_eq!(crate::error::first_os_code(error), Some(CODE), "{error:?}");
}

#[skuld::test]
fn a_front_whose_state_cannot_be_read_is_closed_and_says_why() {
    let g = kill_gate(Some(Front::Sudo), PID, || Err(os_error()), || Ok(false));
    assert_refused_by_one_read(&refusal(g), "whether it had exited could not be read");
    let g = kill_gate(Some(Front::Sudo), PID, || Ok(true), || Err(os_error()));
    assert_refused_by_one_read(&refusal(g), "whether its cgroup kill reaches it could not be read");
}

#[skuld::test]
fn a_front_whose_place_cannot_be_read_after_the_kill_has_the_read_as_its_cause() {
    let (r, _) = reached_asking(Ok(false), Err(os_error()));
    assert_refused_by_one_read(&r.expect_err("not shown reached"), "its cgroup could not be read");
}

#[skuld::test]
fn a_front_whose_cgroup_kill_failed_has_the_failure_as_its_cause() {
    let e = super::cgroup_kill_failed(Front::Sudo, PID, Error::Io(os_error()));
    assert_refused_by_one_read(&e, "its cgroup kill failed");
}

/// A refusal of a front whose cgroup was not killed says so and carries no failure: the tree's
/// failure stands before it in a failed spawn's `cleanup`, so a `Report` of the spawn prints each
/// failure once, the tree's first.
///
/// Mutant: the refusal repeats the tree's failure in its words.
#[skuld::test]
fn a_front_whose_cgroup_was_not_killed_says_so_and_the_report_prints_each_failure_once() {
    use std::sync::Arc;

    use crate::error::{ChildFate, Failures};

    let refusal = super::cgroup_not_killed(Front::Sudo, PID);
    assert!(std::error::Error::source(&refusal).is_none(), "{refusal:?}");
    let refusal_words = refusal.to_string();
    assert!(
        refusal_words.contains(&format!("pid {PID} is what sudo left"))
            && refusal_words.ends_with(" (its cgroup was not killed)"),
        "{refusal_words}"
    );

    let tree = Error::Containment {
        detail: "the kernel refused the write of leaf/cgroup.kill".into(),
        source: Some(Arc::new(Error::Io(io::Error::from_raw_os_error(libc::EISDIR)))),
    };
    let started = Error::MayHaveStarted {
        fate: ChildFate::Running { id: None },
        wrapper_elevated: true,
        detail: None,
        source: Arc::new(Error::Io(io::Error::from_raw_os_error(libc::EPIPE))),
        unreaped: None,
        cleanup: Failures::collect([Arc::new(tree), Arc::new(refusal)]),
    };
    let epipe = io::Error::from_raw_os_error(libc::EPIPE).to_string();
    let eisdir = io::Error::from_raw_os_error(libc::EISDIR).to_string();
    assert_eq!(
        crate::error::said(&started),
        format!(
            "the program may have started, and its child may still be running; its teardown failed too (2 failures, \
             in `cleanup`): {}; cleanup: [(1) process containment failed: the kernel \
             refused the write of leaf/cgroup.kill: {}; (2) {refusal_words}]",
            crate::error::io_said(&epipe),
            crate::error::io_said(&eisdir)
        )
    );
}

/// The tracked process is the program itself, or there is no elevation: signalled like any child,
/// with nothing read.
#[skuld::test]
fn a_child_that_is_not_a_front_is_open_whatever_its_containment() {
    for via in &NOT_FRONTS {
        for in_cgroup in [false, true] {
            let reported = via.clone().map(report);
            let (g, asked) = kill_gate_asking(reported.clone(), Ok(true), Ok(in_cgroup));
            assert_eq!(asked, "", "{via:?}");
            assert!(matches!(g, Gate::Open), "{via:?}: {g:?}");
            let g = terminate_gate(reported.and_then(|reported| reported.front()), PID, || {
                panic!("not asked")
            });
            assert!(matches!(g, Gate::Open), "{via:?}: {g:?}");
        }
    }
}

/// sudo and doas relay `SIGTERM` to the program: never gated.
#[skuld::test]
fn a_sigterm_to_a_relaying_front_is_open() {
    for via in [ElevatedVia::Wrapped(Backend::Sudo), ElevatedVia::Wrapped(Backend::Doas)] {
        let g = terminate_gate(report(via.clone()).front(), PID, || panic!("not asked"));
        assert!(matches!(g, Gate::Open), "{via:?}: {g:?}");
    }
}

/// A `SIGTERM` would end osascript and orphan the program: refused while osascript runs, sent once
/// it has exited.
#[skuld::test]
fn a_sigterm_to_a_live_osascript_is_closed() {
    let osascript = report(ElevatedVia::MacosOsascript).front();
    let detail = refusal_detail(terminate_gate(osascript, PID, || Ok(true)));
    assert!(detail.contains(&format!("pid {PID} is osascript")), "{detail}");
    assert!(detail.contains("no SIGTERM was sent"), "{detail}");
    let error = refusal(terminate_gate(osascript, PID, || Err(os_error())));
    assert_refused_by_one_read(&error, "whether it had exited could not be read");
    assert!(matches!(terminate_gate(osascript, PID, || Ok(false)), Gate::Open));
}

/// What `cgroup_kill_reached` asked, in order (`e`xited, `u`nder the leaf), and its answer.
fn reached_asking(exited: io::Result<bool>, under_leaf: io::Result<bool>) -> (Result<(), Error>, String) {
    reached_asking_in_turn(vec![exited], under_leaf)
}

/// [`reached_asking`], with `exited` answering in turn.
fn reached_asking_in_turn(exited: Vec<io::Result<bool>>, under_leaf: io::Result<bool>) -> (Result<(), Error>, String) {
    let asked = Cell::new(String::new());
    let note = |c: char| {
        let mut s = asked.take();
        s.push(c);
        asked.set(s);
    };
    let exited = in_turn(exited);
    let r = super::cgroup_kill_reached(
        Front::Sudo,
        PID,
        || {
            note('e');
            exited()
        },
        || {
            note('u');
            under_leaf
        },
    );
    (r, asked.take())
}

/// A front exited, or in its leaf after the write, was there for the kill.
#[skuld::test]
fn a_front_in_its_leaf_after_the_kill_was_reached() {
    let (r, asked) = reached_asking(Ok(true), Ok(false));
    assert!(r.is_ok() && asked == "e", "{r:?} {asked}");
    let (r, asked) = reached_asking(Ok(false), Ok(true));
    assert!(r.is_ok() && asked == "eu", "{r:?} {asked}");
}

/// A front neither exited nor in its leaf left it before the kill: refused, and named.
#[skuld::test]
fn a_front_outside_its_leaf_after_the_kill_was_not_reached() {
    let (r, _) = reached_asking(Ok(false), Ok(false));
    let detail = refusal_detail(Gate::Closed(r.expect_err("not reached")));
    assert!(detail.contains("left the cgroup before its kill"), "{detail}");
}

/// The two failed reads of a refusal, as its cause: both are the `source` (a `Several`, the first
/// read first), and neither is in the detail, which says only what was done.
#[track_caller]
fn assert_refused_by_two_reads(error: &Error, words: &[&str], reads: [&str; 2]) {
    let Error::Elevation {
        kind: ElevationErrorKind::Unkillable,
        detail,
        source: Some(source),
    } = error
    else {
        panic!("expected Unkillable with a source, got {error:?}");
    };
    for word in words {
        assert!(detail.contains(word), "{detail}");
    }
    for read in reads {
        assert!(!detail.contains(read), "a cause is in the detail: {detail}");
    }
    let Error::Several(failures) = &**source else {
        panic!("expected the two reads as a Several, got {source:?}");
    };
    let texts: Vec<_> = failures.iter().map(|failure| crate::error::said(&**failure)).collect();
    assert_eq!(texts, reads.map(crate::error::io_said), "{error:?}");
}

/// A front whose place cannot be read is not shown reached: refused, with every read that failed
/// as its cause.
///
/// Mutants: only the first read is the cause; the reads are in the detail's text.
#[skuld::test]
fn a_front_whose_place_after_the_kill_cannot_be_read_is_refused() {
    let (r, _) = reached_asking(Err(io::Error::other("peek refused")), Err(io::Error::other("hidepid")));
    let error = r.expect_err("not shown reached");
    assert_refused_by_two_reads(
        &error,
        &[
            "nothing shows the cgroup kill reached it",
            "whether it had exited could not be read",
            "its cgroup could not be read",
        ],
        ["peek refused", "hidepid"],
    );
}

/// The gate before a kill, when both of its reads fail: closed, with both as the cause.
///
/// Mutant: the second failed read replaces the first, or is only in the detail.
#[skuld::test]
fn a_front_whose_two_gate_reads_fail_is_closed_with_both_as_its_cause() {
    let g = kill_gate(
        Some(Front::Sudo),
        PID,
        || Err(io::Error::other("peek refused")),
        || Err(io::Error::other("hidepid")),
    );
    assert_refused_by_two_reads(
        &refusal(g),
        &[
            "whether it had exited could not be read",
            "whether its cgroup kill reaches it could not be read",
        ],
        ["peek refused", "hidepid"],
    );
}

/// A front that exits, and is reaped by another thread's `wait`, between the gate's two reads has
/// no cgroup left to read: the gate reads it again, finds it exited, and kills nothing.
#[skuld::test]
fn a_front_reaped_between_the_gates_reads_has_exited() {
    let sudo = ElevatedVia::Wrapped(Backend::Sudo);
    let reaped = || Err(io::Error::other("ESRCH: reaped"));
    let (g, asked) = kill_gate_asking_in_turn(Some(report(sudo.clone())), vec![Ok(true), Ok(false)], reaped());
    assert!(matches!(g, Gate::Exited), "{g:?}");
    assert_eq!(asked, "rcr");
    let (g, _) = kill_gate_asking_in_turn(Some(report(sudo)), vec![Ok(true), Ok(true)], reaped());
    refusal_detail(g);
}

/// A front its cgroup kill ended, reaped by another thread's `wait` before its cgroup is read,
/// was reached: the check reads its exit again.
#[skuld::test]
fn a_front_reaped_before_its_cgroup_is_read_was_reached() {
    let reaped = || Err(io::Error::other("ESRCH: reaped"));
    let (r, asked) = reached_asking_in_turn(vec![Ok(false), Ok(true)], reaped());
    assert!(r.is_ok(), "{r:?}");
    assert_eq!(asked, "eue");
    let (r, _) = reached_asking_in_turn(vec![Ok(false), Ok(false)], reaped());
    r.expect_err("still running, unplaced");
}