Skip to content

Commit c1a3427

Browse files
committed
Add WaitStatus::PtraceSyscall for use with PTRACE_O_TRACESYSGOOD
The recommended way to trace syscalls with ptrace is to set the PTRACE_O_TRACESYSGOOD option, to distinguish syscall stops from receiving an actual SIGTRAP. In C, this would cause WSTOPSIG to return SIGTRAP | 0x80, but nix wants to parse that as an actual signal. Add another wait status type for syscall stops (in the language of the ptrace(2) manpage, "PTRACE_EVENT stops" and "Syscall-stops" are different things), and mask out bit 0x80 from signals before trying to parse it. Closes #550
1 parent 26c35c0 commit c1a3427

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2626
- Added `cfmakeraw`, `cfsetspeed`, and `tcgetsid`. ([#527](https://github.com/nix-rust/nix/pull/527))
2727
- Added "bad none", "bad write_ptr", "bad write_int", and "bad readwrite" variants to the `ioctl!`
2828
macro. ([#670](https://github.com/nix-rust/nix/pull/670))
29+
- On Linux and Android, added support for receiving `PTRACE_O_TRACESYSGOOD`
30+
events from `wait` and `waitpid` using `WaitStatus::PtraceSyscall`
31+
([#566](https://github.com/nix-rust/nix/pull/566)).
2932

3033
### Changed
3134
- Changed `ioctl!(write ...)` into `ioctl!(write_ptr ...)` and `ioctl!(write_int ..)` variants

src/sys/wait.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub enum WaitStatus {
4747
Stopped(Pid, Signal),
4848
#[cfg(any(target_os = "linux", target_os = "android"))]
4949
PtraceEvent(Pid, Signal, c_int),
50+
#[cfg(any(target_os = "linux", target_os = "android"))]
51+
PtraceSyscall(Pid),
5052
Continued(Pid),
5153
StillAlive
5254
}
@@ -56,6 +58,7 @@ pub enum WaitStatus {
5658
mod status {
5759
use sys::signal::Signal;
5860
use libc::c_int;
61+
use libc::SIGTRAP;
5962

6063
pub fn exited(status: i32) -> bool {
6164
(status & 0x7F) == 0
@@ -82,7 +85,17 @@ mod status {
8285
}
8386

8487
pub fn stop_signal(status: i32) -> Signal {
85-
Signal::from_c_int((status & 0xFF00) >> 8).unwrap()
88+
// Keep only 7 bits of the signal: the high bit
89+
// is used to indicate syscall stops, below.
90+
Signal::from_c_int((status & 0x7F00) >> 8).unwrap()
91+
}
92+
93+
pub fn syscall_stop(status: i32) -> bool {
94+
// From ptrace(2), setting PTRACE_O_TRACESYSGOOD has the effect
95+
// of delivering SIGTRAP | 0x80 as the signal number for syscall
96+
// stops. This allows easily distinguishing syscall stops from
97+
// genuine SIGTRAP signals.
98+
((status & 0xFF00) >> 8) == SIGTRAP | 0x80
8699
}
87100

88101
pub fn stop_additional(status: i32) -> c_int {
@@ -196,7 +209,9 @@ fn decode(pid : Pid, status: i32) -> WaitStatus {
196209
if #[cfg(any(target_os = "linux", target_os = "android"))] {
197210
fn decode_stopped(pid: Pid, status: i32) -> WaitStatus {
198211
let status_additional = status::stop_additional(status);
199-
if status_additional == 0 {
212+
if status::syscall_stop(status) {
213+
WaitStatus::PtraceSyscall(pid)
214+
} else if status_additional == 0 {
200215
WaitStatus::Stopped(pid, status::stop_signal(status))
201216
} else {
202217
WaitStatus::PtraceEvent(pid, status::stop_signal(status), status::stop_additional(status))

test/sys/test_wait.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,53 @@ fn test_wait_exit() {
3434
Err(_) => panic!("Error: Fork Failed")
3535
}
3636
}
37+
38+
#[cfg(any(target_os = "linux", target_os = "android"))]
39+
// FIXME: qemu-user doesn't implement ptrace on most arches
40+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
41+
mod ptrace {
42+
use nix::sys::ptrace::*;
43+
use nix::sys::ptrace::ptrace::*;
44+
use nix::sys::signal::*;
45+
use nix::sys::wait::*;
46+
use nix::unistd::*;
47+
use nix::unistd::ForkResult::*;
48+
use std::{ptr, process};
49+
50+
fn ptrace_child() -> ! {
51+
let _ = ptrace(PTRACE_TRACEME, Pid::from_raw(0), ptr::null_mut(), ptr::null_mut());
52+
// As recommended by ptrace(2), raise SIGTRAP to pause the child
53+
// until the parent is ready to continue
54+
let _ = raise(SIGTRAP);
55+
process::exit(0)
56+
}
57+
58+
fn ptrace_parent(child: Pid) {
59+
// Wait for the raised SIGTRAP
60+
assert_eq!(waitpid(child, None), Ok(WaitStatus::Stopped(child, SIGTRAP)));
61+
// We want to test a syscall stop and a PTRACE_EVENT stop
62+
assert!(ptrace_setoptions(child, PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXIT).is_ok());
63+
64+
// First, stop on the next system call, which will be exit()
65+
assert!(ptrace(PTRACE_SYSCALL, child, ptr::null_mut(), ptr::null_mut()).is_ok());
66+
assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceSyscall(child)));
67+
// Then get the ptrace event for the process exiting
68+
assert!(ptrace(PTRACE_CONT, child, ptr::null_mut(), ptr::null_mut()).is_ok());
69+
assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceEvent(child, SIGTRAP, PTRACE_EVENT_EXIT)));
70+
// Finally get the normal wait() result, now that the process has exited
71+
assert!(ptrace(PTRACE_CONT, child, ptr::null_mut(), ptr::null_mut()).is_ok());
72+
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 0)));
73+
}
74+
75+
#[test]
76+
fn test_wait_ptrace() {
77+
#[allow(unused_variables)]
78+
let m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");
79+
80+
match fork() {
81+
Ok(Child) => ptrace_child(),
82+
Ok(Parent { child }) => ptrace_parent(child),
83+
Err(_) => panic!("Error: Fork Failed")
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)