Skip to content

Commit 5281b30

Browse files
committed
Add ptrace::read_user and ptrace::write_user
1 parent 256707e commit 5281b30

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
6464
- Added `Ipv6DontFrag` for android, iOS, linux and macOS.
6565
- Added `IpDontFrag` for iOS, macOS.
6666
(#[1692](https://github.com/nix-rust/nix/pull/1692))
67+
- Added `ptrace::read_user` and `ptrace::write_user` for Linux.
6768

6869
### Changed
6970

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,36 @@ fn test_ptrace_syscall() {
197197
#[cfg(target_arch = "x86")]
198198
let get_syscall_id = || ptrace::getregs(child).unwrap().orig_eax as libc::c_long;
199199

200+
// this duplicates `get_syscall_id` for the purpose of testing `ptrace::read_user`.
201+
#[cfg(target_arch = "x86_64")]
202+
let get_rax_offset = |user_struct_ptr: *const libc::user| {
203+
unsafe { &(*user_struct_ptr).regs.orig_rax as *const _ }
204+
};
205+
#[cfg(target_arch = "x86")]
206+
let get_rax_offset = |user_struct_ptr: *const libc::user| {
207+
unsafe { &(*user_struct_ptr).regs.orig_eax as *const _ }
208+
};
209+
210+
let get_syscall_from_user_area = || {
211+
// Find the offset of `user.regs.rax` (or `eax` for x86)
212+
let user_struct = MaybeUninit::<libc::user>::uninit();
213+
let user_struct_ptr = user_struct.as_ptr();
214+
let rax_offset = get_rax_offset(user_struct_ptr) as usize - user_struct_ptr as usize;
215+
216+
ptrace::read_user(child, rax_offset as _).unwrap() as libc::c_long
217+
};
218+
200219
// kill entry
201220
ptrace::syscall(child, None).unwrap();
202221
assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceSyscall(child)));
203222
assert_eq!(get_syscall_id(), ::libc::SYS_kill);
223+
assert_eq!(get_syscall_from_user_area(), ::libc::SYS_kill);
204224

205225
// kill exit
206226
ptrace::syscall(child, None).unwrap();
207227
assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceSyscall(child)));
208228
assert_eq!(get_syscall_id(), ::libc::SYS_kill);
229+
assert_eq!(get_syscall_from_user_area(), ::libc::SYS_kill);
209230

210231
// receive signal
211232
ptrace::syscall(child, None).unwrap();

0 commit comments

Comments
 (0)