Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2653.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed `nix::sys::ptrace::syscall_info`, which was not setting the `data` argument properly, causing garbage values to be returned.
39 changes: 39 additions & 0 deletions examples/ptrace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Traces a child process using `ptrace`.
//!
//! The child issues a single `write` syscall, which is printed upon entry and exit.

#[cfg(all(target_os = "linux", target_env = "gnu"))]
fn main() {
let pid = unsafe { nix::unistd::fork().unwrap() };

match pid {
nix::unistd::ForkResult::Child => {
nix::sys::ptrace::traceme().unwrap();
nix::sys::signal::raise(nix::sys::signal::Signal::SIGCONT).unwrap();
println!("I'm issuing a syscall!");
}
nix::unistd::ForkResult::Parent { child } => {
nix::sys::wait::waitpid(Some(child), None).unwrap();
nix::sys::ptrace::setoptions(
child,
nix::sys::ptrace::Options::PTRACE_O_TRACESYSGOOD,
)
.unwrap();

nix::sys::ptrace::syscall(child, None).unwrap();
nix::sys::wait::waitpid(Some(child), None).unwrap();
let syscall_info = nix::sys::ptrace::syscall_info(child).unwrap();
println!("{syscall_info:?}");
assert!(syscall_info.op == libc::PTRACE_SYSCALL_INFO_ENTRY);

nix::sys::ptrace::syscall(child, None).unwrap();
nix::sys::wait::waitpid(Some(child), None).unwrap();
let syscall_info = nix::sys::ptrace::syscall_info(child).unwrap();
println!("{syscall_info:?}");
assert!(syscall_info.op == libc::PTRACE_SYSCALL_INFO_EXIT);
}
}
}

#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
fn main() {}
2 changes: 1 addition & 1 deletion src/sys/ptrace/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ fn ptrace_get_data<T>(request: Request, pid: Pid) -> Result<T> {
libc::ptrace(
request as RequestType,
libc::pid_t::from(pid),
ptr::null_mut::<T>(),
std::mem::size_of::<T>(),
data.as_mut_ptr(),
)
};
Expand Down