diff --git a/poc/reverse-attach-linux/src/jobs/mod.rs b/poc/reverse-attach-linux/src/jobs/mod.rs index 46c32ed..cb8d0cc 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 { 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..205a472 100644 --- a/poc/reverse-attach-linux/src/proc_tests.rs +++ b/poc/reverse-attach-linux/src/proc_tests.rs @@ -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")] 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