diff --git a/poc/reverse-attach-linux/src/jobs/mod.rs b/poc/reverse-attach-linux/src/jobs/mod.rs index 46c32ed..71c68d5 100644 --- a/poc/reverse-attach-linux/src/jobs/mod.rs +++ b/poc/reverse-attach-linux/src/jobs/mod.rs @@ -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 } @@ -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 { @@ -218,13 +222,17 @@ fn finish(g: &mut Inner, status: Option, timed_out: bool, late: Opti /// The job's shell (our child `pid`) has exited. It is not reaped (`WNOWAIT`), /// so its pid stays reserved. fn shell_exited(pid: u32) -> bool { + // A pid that names no process has no child to wait for: same answer as ECHILD below. + let Some(id) = crate::proc::checked_pid(pid) else { + return true; + }; // siginfo_t is 128 bytes on Linux; `si_signo` (first int) is set only when // a child was found, so a zeroed buffer distinguishes WNOHANG's "not yet". let mut info = [0i32; 32]; let r = unsafe { waitid( P_PID, - pid, + id as u32, info.as_mut_ptr().cast(), WEXITED | WNOHANG | WNOWAIT, ) diff --git a/poc/reverse-attach-linux/src/proc.rs b/poc/reverse-attach-linux/src/proc.rs index d23e73b..0f34e5d 100644 --- a/poc/reverse-attach-linux/src/proc.rs +++ b/poc/reverse-attach-linux/src/proc.rs @@ -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::try_from(pid).ok().filter(|p| *p > 0) +} + pub struct Spec { pub exe: PathBuf, pub args: Vec, @@ -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!( @@ -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); @@ -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 { diff --git a/poc/reverse-attach-linux/src/proc_tests.rs b/poc/reverse-attach-linux/src/proc_tests.rs index 1d463cf..cf69f72 100644 --- a/poc/reverse-attach-linux/src/proc_tests.rs +++ b/poc/reverse-attach-linux/src/proc_tests.rs @@ -325,6 +325,25 @@ fn a_pid_that_does_not_exist_signals_nothing_and_does_not_panic() { assert!(!kill_pid_checked(top + 1, &|_| true)); } +#[test] +fn checked_pid_keeps_valid_pids_and_rejects_the_rest() { + assert_eq!(checked_pid(1), Some(1)); + assert_eq!(checked_pid(2_147_483_647), Some(i32::MAX)); + assert_eq!(checked_pid(0), None); + assert_eq!(checked_pid(2_147_483_648), None); + assert_eq!(checked_pid(u32::MAX), None); +} + +/// 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")] diff --git a/poc/reverse-attach-linux/src/sweep_tests.rs b/poc/reverse-attach-linux/src/sweep_tests.rs index 7597854..2aa9b63 100644 --- a/poc/reverse-attach-linux/src/sweep_tests.rs +++ b/poc/reverse-attach-linux/src/sweep_tests.rs @@ -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(); diff --git a/poc/reverse-attach-linux/src/tools/bash.rs b/poc/reverse-attach-linux/src/tools/bash.rs index de30baf..ee0a1fe 100644 --- a/poc/reverse-attach-linux/src/tools/bash.rs +++ b/poc/reverse-attach-linux/src/tools/bash.rs @@ -51,7 +51,7 @@ pub fn tool_bash(arguments: &Value, cancel: Option<&AtomicBool>) -> Result thread::JoinHandle<(Vec, bool)> { @@ -95,8 +95,11 @@ pub fn tool_bash(arguments: &Value, cancel: Option<&AtomicBool>) -> Result