Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions poc/reverse-attach-linux/src/jobs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ impl Job {
}
g.late_signal = Some(sig);
}
unsafe { kill(-(self.pid as i32), sig) };
if let Some(pgid) = crate::proc::checked_pid(self.pid) {
unsafe { kill(-pgid, sig) };
}
true
}

Expand All @@ -181,7 +183,9 @@ impl Job {
if exited {
g.late_signal = Some(SIGKILL);
}
unsafe { kill(-(self.pid as i32), SIGKILL) };
if let Some(pgid) = crate::proc::checked_pid(self.pid) {
unsafe { kill(-pgid, SIGKILL) };
}
g.timed_out = true;
}
if !exited {
Expand Down
13 changes: 10 additions & 3 deletions poc/reverse-attach-linux/src/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ use rustix::process::{Pid, Signal};

pub const RING_CAP: usize = 64 * 1024;

/// A `u32` pid as the signed id the kernel and rustix use. `None` when it does not fit in `i32`,
/// or is zero or negative: such a value names no process, so callers take their no-such-process
/// path. A bare `as i32` would turn `u32::MAX` into `-1`, which rustix rejects in debug builds.
pub(crate) fn checked_pid(pid: u32) -> Option<i32> {
i32::try_from(pid).ok().filter(|p| *p > 0)
}

pub struct Spec {
pub exe: PathBuf,
pub args: Vec<String>,
Expand Down Expand Up @@ -269,7 +276,7 @@ impl Child {
if self.reaped {
return true;
}
let Some(pid) = Pid::from_raw(self.child.id() as i32) else {
let Some(pid) = checked_pid(self.child.id()).and_then(Pid::from_raw) else {
return true;
};
matches!(
Expand Down Expand Up @@ -372,7 +379,7 @@ impl Child {
let pgid = if self.reaped {
None
} else {
Pid::from_raw(self.child.id() as i32)
checked_pid(self.child.id()).and_then(Pid::from_raw)
};
if let Some(g) = pgid {
let _ = rustix::process::kill_process_group(g, Signal::TERM);
Expand Down Expand Up @@ -460,7 +467,7 @@ pub fn kill_matching(matches: &dyn Fn(&Path) -> bool, protect: &dyn Fn(u32) -> b
/// (or fail with `ESRCH` if it has exited). A process that fails the recheck is never signalled.
#[cfg(target_os = "linux")]
pub fn kill_pid_checked(pid: u32, recheck: &dyn Fn(u32) -> bool) -> bool {
let Some(p) = Pid::from_raw(pid as i32) else {
let Some(p) = checked_pid(pid).and_then(Pid::from_raw) else {
return false;
};
let Ok(fd) = rustix::process::pidfd_open(p, rustix::process::PidfdFlags::empty()) else {
Expand Down
10 changes: 10 additions & 0 deletions poc/reverse-attach-linux/src/proc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ fn a_pid_that_does_not_exist_signals_nothing_and_does_not_panic() {
assert!(!kill_pid_checked(top + 1, &|_| true));
}

/// Pids above `i32::MAX` (2^31, and `u32::MAX`, which `as i32` makes `-1`) name no process. The
/// call must answer "no" without reaching rustix with a negative id, so debug and release agree.
#[cfg(target_os = "linux")]
#[test]
fn a_pid_above_i32_max_is_treated_as_no_such_process() {
assert!(!kill_pid_checked(2_147_483_648, &|_| true));
assert!(!kill_pid_checked(u32::MAX, &|_| true));
assert!(!kill_pid_checked(u32::MAX - 1, &|_| true));
}

/// The recheck sees the process the pidfd names, not the one the scan saw: when it is called, the
/// pidfd already exists, so it is the last word before the signal.
#[cfg(target_os = "linux")]
Expand Down
4 changes: 3 additions & 1 deletion poc/reverse-attach-linux/src/sweep_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ fn a_detached_grandchild_of_a_recorded_leader_is_claimed_and_a_stray_is_not() {
assert!(claimed(helper, &live), "the parent chain did not claim it");
assert!(!claimed(stray.id(), &live), "a stray was claimed");
let _ = rustix::process::kill_process(
rustix::process::Pid::from_raw(helper as i32).unwrap(),
crate::proc::checked_pid(helper)
.and_then(rustix::process::Pid::from_raw)
.unwrap(),
rustix::process::Signal::KILL,
);
leader.kill().unwrap();
Expand Down
9 changes: 6 additions & 3 deletions poc/reverse-attach-linux/src/tools/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn tool_bash(arguments: &Value, cancel: Option<&AtomicBool>) -> Result<Value

let started = std::time::Instant::now();
let mut child = cmd.spawn().map_err(|e| (-32000, format!("spawn: {e}")))?;
let pid = child.id() as i32;
let pid = crate::proc::checked_pid(child.id());

// Drain both pipes on threads so a chatty child cannot block on a full pipe.
fn drain(mut r: impl Read + Send + 'static, cap: usize) -> thread::JoinHandle<(Vec<u8>, bool)> {
Expand Down Expand Up @@ -95,8 +95,11 @@ pub fn tool_bash(arguments: &Value, cancel: Option<&AtomicBool>) -> Result<Value
if expired || is_cancelled() {
timed_out = expired;
cancelled = !expired;
unsafe {
kill(-pid, 9); // whole process group (setsid made pid the leader)
if let Some(pgid) = pid {
// whole process group (setsid made pid the leader)
unsafe {
kill(-pgid, 9);
}
}
break child.wait().ok();
}
Expand Down
Loading