Skip to content

fix: checked pid conversion so debug and release agree (#179) - #181

Closed
chaodu-agent wants to merge 2 commits into
mainfrom
fix/179-proc-pid
Closed

chaodu-agent wants to merge 2 commits into
mainfrom
fix/179-proc-pid

Conversation

@chaodu-agent

Copy link
Copy Markdown
Contributor

Refs #179

Follow-up to #180, which merged at b4b6130. Commit 681c594 adds one more conversion site that #180 did not cover, so it needs its own PR to reach main.

Change

Every pid as i32 (and every unchecked u32 passed to a libc call) now goes through checked_pid. A pid that does not fit in i32 is treated as "no such process" instead of wrapping to a negative value. Negative values reach kill and waitid as process-group or wildcard targets, so a wrapped pid could signal the wrong process. Pid 0 and u32::MAX used to signal the caller's own group and init respectively. Both are now skipped.

Conversion sites in poc/reverse-attach-linux/src:

  • proc.rs has_exited: Pid::from_raw(child.id() as i32) becomes checked_pid(child.id()).and_then(Pid::from_raw). None returns true.
  • proc.rs Child::stop: same conversion. None skips the group TERM.
  • proc.rs kill_pid_checked: Pid::from_raw(pid as i32) becomes checked_pid(pid).and_then(Pid::from_raw). None returns false.
  • jobs/mod.rs signal: kill(-(self.pid as i32), sig) is sent only when checked_pid(self.pid) is Some.
  • jobs/mod.rs supervise_once: kill(-(self.pid as i32), SIGKILL) is sent only when checked_pid is Some.
  • jobs/mod.rs shell_exited: waitid(P_PID, pid, ...) now takes checked_pid(pid). None returns true, the same as the ECHILD path. Some(id) passes id as u32.
  • tools/bash.rs: child.id() as i32 becomes checked_pid(child.id()). The group kill runs only on Some, and the reported pid is null on None.
  • sweep_tests.rs: Pid::from_raw(helper as i32) becomes checked_pid(helper).and_then(Pid::from_raw).

New tests

  • proc::tests::a_pid_above_i32_max_is_treated_as_no_such_process (2147483648, u32::MAX, u32::MAX - 1), from b4b6130.
  • proc::tests::a_pid_that_does_not_exist_signals_nothing_and_does_not_panic (u32::MAX, 0). In debug builds this failed before the fix with assertion failed: raw >= 0.
  • proc::tests::checked_pid_keeps_valid_pids_and_rejects_the_rest, added in 681c594. It checks the helper's mapping directly.

Pid-range guards in the jobs and bash paths have no dedicated test. Their pids come from child.id(), which cannot be out of range for a live child. The helper test covers the mapping they rely on.

Verification

Run on a fresh clone of fix/179-proc-pid at 681c594 on a Linux aarch64 build host.

Command Exit Passed Failed Failing tests
cargo test (debug) 0 601 0 none
cargo test --release 0 601 0 none
cargo fmt --check 0 n/a n/a no output
cargo clippy -- -D warnings 0 n/a n/a no warnings

Ignored count was 17 in both the debug and release runs. Each new test passed in debug and release.

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
shell_exited passed a u32 pid straight to waitid. A value that does not
fit in i32, or is zero, now returns true, the same answer the ECHILD
path already gives for a child that is gone. Valid pids are unchanged.

Adds checked_pid_keeps_valid_pids_and_rejects_the_rest, which covers
1, i32::MAX, 0, 2147483648 and u32::MAX.

Commands run on a Linux arm64 host, in a fresh clone of the branch:
- cargo test
- cargo test --release
- cargo fmt --check
- cargo clippy -- -D warnings
@chaodu-agent

Copy link
Copy Markdown
Contributor Author

Superseded by #182. This PR duplicated #180, which was already squash-merged to main as 203bbdd. Its one remaining change now lands in #182. The remote branch fix/179-proc-pid is left in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant