From b4b6130a59a5977dd7093140683d2c44b54521a2 Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:39:21 -0500 Subject: [PATCH] fix: checked pid conversion so debug and release agree (#179) A u32 pid was turned into a signed id with `as i32`. u32::MAX became -1, and rustix's Pid::from_raw asserts against negative values in debug builds, so the proc test panicked in debug and passed in release. Every conversion now goes through one helper, proc::checked_pid, which returns None for a value that does not fit in i32 or is zero or negative. Callers take the path they already take for a process that does not exist: no signal is sent and has_exited reports true. Sites changed (poc/reverse-attach-linux/src): - proc.rs has_exited: Pid::from_raw(child.id() as i32) -> checked_pid(child.id()).and_then(Pid::from_raw) - proc.rs Child::stop: Pid::from_raw(child.id() as i32) -> checked_pid(child.id()).and_then(Pid::from_raw) - proc.rs kill_pid_checked: Pid::from_raw(pid as i32) -> checked_pid(pid).and_then(Pid::from_raw) - jobs/mod.rs signal and supervise_once: kill(-(pid as i32), sig) -> kill only when checked_pid(pid) is Some - tools/bash.rs: child.id() as i32 -> checked_pid; the kill(-pid) runs only when Some, and the reported pid is null when None - sweep_tests.rs: Pid::from_raw(helper as i32) -> checked_pid(helper).and_then(Pid::from_raw) Tests (proc_tests.rs): - a_pid_above_i32_max_is_treated_as_no_such_process (new): 2147483648, u32::MAX, u32::MAX - 1 - a_pid_that_does_not_exist_signals_nothing_and_does_not_panic (existing, u32::MAX and 0), now passes in debug Commands run on a Linux arm64 host, in a fresh clone of the pushed branch: - cargo test - cargo test --release - cargo fmt --check - cargo clippy -- -D warnings --- poc/reverse-attach-linux/src/jobs/mod.rs | 8 ++++++-- poc/reverse-attach-linux/src/proc.rs | 13 ++++++++++--- poc/reverse-attach-linux/src/proc_tests.rs | 10 ++++++++++ poc/reverse-attach-linux/src/sweep_tests.rs | 4 +++- poc/reverse-attach-linux/src/tools/bash.rs | 9 ++++++--- 5 files changed, 35 insertions(+), 9 deletions(-) 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