Skip to content

Commit 7eb3189

Browse files
committed
Add ptrace::read_user and ptrace::write_user
1 parent 1647189 commit 7eb3189

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1515
- Fixed compilation and updated support on Haiku
1616
- Added support for the `x86_64-unknown-haiku` target.
1717
(#[1703](https://github.com/nix-rust/nix/pull/1703))
18+
- Added `ptrace::read_user` and `ptrace::write_user` for Linux.
19+
(#[1697](https://github.com/nix-rust/nix/pull/1697))
1820

1921
### Changed
2022

src/sys/ptrace/linux.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,24 @@ pub unsafe fn write(
481481
{
482482
ptrace_other(Request::PTRACE_POKEDATA, pid, addr, data).map(drop)
483483
}
484+
485+
/// Reads a word from a user area at `offset`.
486+
/// The user struct definition can be found in `/usr/include/sys/user.h`.
487+
pub fn read_user(pid: Pid, offset: AddressType) -> Result<c_long> {
488+
ptrace_peek(Request::PTRACE_PEEKUSER, pid, offset, ptr::null_mut())
489+
}
490+
491+
/// Writes a word to a user area at `offset`.
492+
/// The user struct definition can be found in `/usr/include/sys/user.h`.
493+
///
494+
/// # Safety
495+
///
496+
/// The `data` argument is passed directly to `ptrace(2)`. Read that man page
497+
/// for guidance.
498+
pub unsafe fn write_user(
499+
pid: Pid,
500+
offset: AddressType,
501+
data: *mut c_void) -> Result<()>
502+
{
503+
ptrace_other(Request::PTRACE_POKEUSER, pid, offset, data).map(drop)
504+
}

test/sys/test_ptrace.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use memoffset::offset_of;
12
use nix::errno::Errno;
23
use nix::unistd::getpid;
34
use nix::sys::ptrace;
@@ -197,15 +198,29 @@ fn test_ptrace_syscall() {
197198
#[cfg(target_arch = "x86")]
198199
let get_syscall_id = || ptrace::getregs(child).unwrap().orig_eax as libc::c_long;
199200

201+
// this duplicates `get_syscall_id` for the purpose of testing `ptrace::read_user`.
202+
#[cfg(target_arch = "x86_64")]
203+
let rax_offset = offset_of!(libc::user_regs_struct, orig_rax);
204+
#[cfg(target_arch = "x86")]
205+
let rax_offset = offset_of!(libc::user_regs_struct, orig_eax);
206+
207+
let get_syscall_from_user_area = || {
208+
// Find the offset of `user.regs.rax` (or `user.regs.eax` for x86)
209+
let rax_offset = offset_of!(libc::user, regs) + rax_offset;
210+
ptrace::read_user(child, rax_offset as _).unwrap() as libc::c_long
211+
};
212+
200213
// kill entry
201214
ptrace::syscall(child, None).unwrap();
202215
assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceSyscall(child)));
203216
assert_eq!(get_syscall_id(), ::libc::SYS_kill);
217+
assert_eq!(get_syscall_from_user_area(), ::libc::SYS_kill);
204218

205219
// kill exit
206220
ptrace::syscall(child, None).unwrap();
207221
assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceSyscall(child)));
208222
assert_eq!(get_syscall_id(), ::libc::SYS_kill);
223+
assert_eq!(get_syscall_from_user_area(), ::libc::SYS_kill);
209224

210225
// receive signal
211226
ptrace::syscall(child, None).unwrap();

0 commit comments

Comments
 (0)