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
//! Unit tests for `child.rs`.
#[cfg(target_os = "macos")]
use crate::containment::fdmarker::is_teardown_mechanism_failure;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use crate::test_child::Console;
/// Regression test: the group-signal step's ordinary refusal outcomes (`Error::Containment` /
/// `Error::Unassessable { source: None, .. }`, distinguished from a genuine teardown-mechanism
/// failure since #61) were being stringified into an opaque `Error::Io` on the way out of
/// `Marker::sweep`, so an entirely ordinary outcome classified as a teardown-mechanism failure
/// — reintroducing the bug #61 fixed.
///
/// `fdmarker_tests.rs` calls `Marker::hard_kill`/`terminate` DIRECTLY, bypassing
/// `dispatch.rs`'s `Attached::FdMarker` arm where the laundering sat, so none of those tests
/// could catch this. This test goes through the real public path instead: `Command::spawn` →
/// `dispatch.rs`'s `Attached::FdMarker` → `Child::kill_tree`/`Drop` →
/// `is_teardown_mechanism_failure`.
///
/// A live cross-uid refuser needs real root to construct (see `tests/group_teardown_setuid.rs`
/// for why that is not reliably provisionable on macOS: SIP). This instead drives the group
/// channel's OTHER real, privilege-free refusal path: `containment::unix::signal_group`'s
/// `pgid <= 0` guard, reached via `Child::test_force_fdmarker_pgid` the same way this codebase's
/// other otherwise-untriggerable branches already are (`force_blind_snapshot_for_next_call` and
/// friends).
#[cfg(target_os = "macos")]
#[skuld::test]
fn kill_tree_reports_an_ordinary_group_refusal_through_the_real_dispatch_and_classifier_path() {
let mut child = crate::test_child::command(Console::Hidden)
.executable("/usr/bin/true")
.arg("true") // argv[0]; `executable` alone selects the loaded image, not argv
.contain_with(crate::ContainMode::Strongest)
.spawn()
.expect("spawn a contained macOS root");
child.test_force_fdmarker_pgid(0);
let err = child
.kill_tree()
.expect_err("an unsignallable (pgid 0) group channel must report Err, not silently succeed");
assert!(
matches!(err, crate::error::Error::Unassessable { source: None, .. }),
"an invalid-pgid refusal is the ORDINARY, expected outcome unix::signal_group's own \
guard documents — got {err:?} instead"
);
assert!(
!is_teardown_mechanism_failure(&err),
"an ordinary group-signal refusal must never classify as a teardown MECHANISM \
failure — got {err:?}"
);
// `Drop` (`kill_on_drop` defaults to true) re-runs the teardown with the forced pgid: it must
// return normally. The classification assert above is the sole guard for the laundering bug.
drop(child);
}
/// A failed `cgroup.kill` write during `Child::drop`'s own teardown is a real OS outcome: Drop
/// must warn, not panic. Forced via the EISDIR technique of
/// `hard_kill_propagates_a_kill_the_kernel_refused` (`containment/cgroup/leaf_tests.rs`):
/// `open(O_WRONLY)` on a directory always fails.
#[cfg(target_os = "linux")]
#[skuld::test]
fn drop_warns_instead_of_asserting_on_a_real_teardown_mechanism_failure() {
crate::log_capture::install();
let dir = tempfile::tempdir().expect("tempdir");
let leaf_path = dir.path().join("cosca-drop-kill-fail-leaf");
std::fs::create_dir(&leaf_path).expect("create the leaf");
std::fs::write(leaf_path.join("occupant"), "").expect("keep the leaf unremovable");
std::fs::create_dir(leaf_path.join("cgroup.kill")).expect("make cgroup.kill a directory");
crate::child::spawn::fault::set_attachment_override(crate::containment::Attachment {
containment: crate::containment::Containment::CgroupV2,
attached: crate::containment::Attached::Cgroup(crate::containment::cgroup::test_support::entered_leaf_at(
leaf_path.clone(),
)),
graceful: crate::graceful::GracefulMechanism::Process,
});
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
// The override is consumed by this spawn; `kill_on_drop` defaults to true.
let mut child = cmd.spawn().expect("spawn");
// Released once the drop's root kill has returned: the drop blocks until the root is reaped.
let stdin = child.stdin().expect("piped stdin");
let blocker = crate::child::spawn::fault::release_at_drop_kill(stdin);
// Pin that the forced failure is the mechanism class this test claims (a raw `EISDIR` from
// the `cgroup.kill` write, surfaced as `Error::Io`), so it cannot go vacuous if the forcing
// stops reaching the kill.
let forced = child
.kill_tree()
.expect_err("the forced cgroup.kill failure must surface from kill_tree");
assert!(
matches!(&forced, crate::error::Error::Io(io) if io.raw_os_error() == Some(libc::EISDIR)),
"the forced failure must be a mechanism-class Error::Io(EISDIR), got {forced:?}"
);
let mark = crate::log_capture::mark();
// A same-text record from ANOTHER thread, fixed before the drop by the join: the thread-filtered
// scan below must not count it (a concurrent test's identical record would look the same).
let marker = "Child::drop: contained-tree teardown did not fully succeed";
std::thread::spawn(move || log::warn!("{marker}: from another thread"))
.join()
.expect("emit from another thread");
let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(child)));
assert!(
unwound.is_ok(),
"Child::drop must not panic on a real teardown-mechanism failure: {unwound:?}"
);
blocker.assert_killed();
// `Drop` logs on the dropping thread (the tree kill runs there before any reaper hand-off),
// so the current-thread scan sees exactly its record.
let records = crate::log_capture::records_since_on_current_thread(mark, marker);
assert_eq!(
records.iter().map(|(level, _)| *level).collect::<Vec<_>>(),
[log::Level::Warn],
"a real teardown-mechanism failure during Drop must be logged at warn, got {records:?}"
);
}
/// `Child::kill`'s refusal is the OS's own: the error carries the raw errno (`EPERM`, or Windows'
/// `ERROR_ACCESS_DENIED`), not a message about it.
///
/// Mutant: the error is wrapped in a new `io::Error`, or its payload is stringified.
#[skuld::test]
fn child_kill_error_keeps_the_raw_errno() {
use crate::os_process::child::PeekOutcome;
use crate::os_process::fake::FakeProcess;
#[cfg(unix)]
const REFUSED: i32 = libc::EPERM;
#[cfg(windows)]
const REFUSED: i32 = 5;
// macOS reads the root's state first; nothing else peeks.
let fake = FakeProcess::new([])
.peeks([Ok(PeekOutcome::Running)])
.signals([Err(std::io::Error::from_raw_os_error(REFUSED))]);
let child = crate::test_child::over_fake::child_over(&fake);
let err = child.kill().expect_err("the scripted signal is refused");
assert!(
matches!(&err, crate::error::Error::Io(e) if e.raw_os_error() == Some(REFUSED)),
"{err:?}"
);
assert_eq!(fake.signal_calls(), [crate::signal::Sig::Kill]);
}