Skip to content
Open
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/2739.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added set_syscall_info to ptrace on linux
17 changes: 17 additions & 0 deletions src/sys/ptrace/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ libc_enum! {
PTRACE_SYSEMU_SINGLESTEP,
#[cfg(all(target_os = "linux", target_env = "gnu"))]
PTRACE_GET_SYSCALL_INFO,
#[cfg(all(target_os = "linux", target_env = "gnu"))]
PTRACE_SET_SYSCALL_INFO,
}
}

Expand Down Expand Up @@ -576,6 +578,21 @@ pub fn syscall_info(pid: Pid) -> Result<libc::ptrace_syscall_info> {
ptrace_get_data::<libc::ptrace_syscall_info>(Request::PTRACE_GET_SYSCALL_INFO, pid)
}

/// Set the information of the syscall that caused the stop, as with
/// `ptrace(PTRACE_SET_SYSCALL_INFO, ...`.
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pub fn set_syscall_info(pid: Pid, syscall_info: &libc::ptrace_syscall_info) -> Result<()> {
let res = unsafe {
libc::ptrace(
Request::PTRACE_SET_SYSCALL_INFO as RequestType,
libc::pid_t::from(pid),
mem::size_of::<libc::ptrace_syscall_info>(),
syscall_info as *const _ as *const c_void,
)
};
Errno::result(res).map(drop)
}

/// Sets the process as traceable, as with `ptrace(PTRACE_TRACEME, ...)`
///
/// Indicates that this process is to be traced by its parent.
Expand Down
72 changes: 70 additions & 2 deletions test/sys/test_ptrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use nix::unistd::getpid;
#[cfg(linux_android)]
use std::mem;

use crate::*;

#[test]
fn test_ptrace() {
// Just make sure ptrace can be called at all, for now.
Expand Down Expand Up @@ -410,3 +408,73 @@ fn test_ptrace_syscall_info() {
},
}
}

#[cfg(all(target_os = "linux", target_env = "gnu"))]
#[test]
fn test_ptrace_set_syscall_info() {
use nix::sys::mman::{mmap_anonymous, MapFlags, ProtFlags};
use nix::sys::ptrace;
use nix::sys::signal::{raise, Signal::*};
use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::{fork, ForkResult::*};
use std::num::NonZero;

require_capability!("test_ptrace_set_syscall_info", CAP_SYS_PTRACE);

let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => {
ptrace::traceme().unwrap();
raise(SIGSTOP).unwrap();

unsafe {
let my_memory = mmap_anonymous(
None,
NonZero::new(1).unwrap(),
ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE,
)
.unwrap_or_else(|_| ::libc::_exit(1))
.cast::<u8>();

*my_memory.as_ptr() = 42;
::libc::_exit(0);
}
}
Parent { child } => {
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Stopped(child, SIGSTOP))
);
ptrace::setoptions(child, Options::PTRACE_O_TRACESYSGOOD).unwrap();
ptrace::syscall(child, None).unwrap();

// Hijack the syscall and remove PROT_WRITE to force a SEGFAULT in the child
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::PtraceSyscall(child))
);
let mut si = ptrace::syscall_info(child).unwrap();
assert_eq!(si.op, libc::PTRACE_SYSCALL_INFO_ENTRY);
unsafe {
si.u.entry.args[2] = ProtFlags::PROT_NONE.bits() as u64;
}
let set_syscall_res = ptrace::set_syscall_info(child, &si);
ptrace::cont(child, None).unwrap();

if set_syscall_res.is_err() {
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Exited(child, 0))
);
crate::skip!("PTRACE_SET_SYSCALL_INFO failed: Linux >= 6.16 is required, skipping test.");
}

assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Stopped(child, SIGSEGV))
);
ptrace::detach(child, SIGSEGV).unwrap();
}
}
}