Skip to content

Commit 4f6663e

Browse files
committed
Add implementation of PTRACE_{GET,SET}REGSET
Also added `PTRACE_{GET,SET}REGS` for most platforms other than x86 using aforementioned implementation.
1 parent 4ab23c3 commit 4f6663e

File tree

2 files changed

+230
-7
lines changed

2 files changed

+230
-7
lines changed

src/sys/ptrace/linux.rs

Lines changed: 132 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ pub type AddressType = *mut ::libc::c_void;
1717
target_arch = "x86_64",
1818
any(target_env = "gnu", target_env = "musl")
1919
),
20-
all(target_arch = "x86", target_env = "gnu")
21-
)
20+
all(target_arch = "x86", target_env = "gnu"),
21+
all(target_arch = "aarch64", target_env = "gnu"),
22+
all(target_arch = "riscv64", target_env = "gnu"),
23+
),
2224
))]
2325
use libc::user_regs_struct;
2426

@@ -170,6 +172,29 @@ libc_enum! {
170172
}
171173
}
172174

175+
libc_enum! {
176+
#[cfg(all(
177+
target_os = "linux",
178+
target_env = "gnu",
179+
any(
180+
target_arch = "x86_64",
181+
target_arch = "x86",
182+
target_arch = "aarch64",
183+
target_arch = "riscv64",
184+
)
185+
))]
186+
#[repr(i32)]
187+
/// Defining a specific register set, as used in [`getregset`] and [`setregset`].
188+
#[non_exhaustive]
189+
pub enum RegisterSet {
190+
NT_PRSTATUS,
191+
NT_PRFPREG,
192+
NT_PRPSINFO,
193+
NT_TASKSTRUCT,
194+
NT_AUXV,
195+
}
196+
}
197+
173198
libc_bitflags! {
174199
/// Ptrace options used in conjunction with the PTRACE_SETOPTIONS request.
175200
/// See `man ptrace` for more details.
@@ -217,6 +242,12 @@ fn ptrace_peek(
217242
}
218243

219244
/// Get user registers, as with `ptrace(PTRACE_GETREGS, ...)`
245+
///
246+
/// Note that since `PTRACE_GETREGS` are not available on all platforms (as in [ptrace(2)]),
247+
/// `ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, ...)` is used instead to achieve the same effect
248+
/// on aarch64 and riscv64.
249+
///
250+
/// [ptrace(2)]: https://www.man7.org/linux/man-pages/man2/ptrace.2.html
220251
#[cfg(all(
221252
target_os = "linux",
222253
any(
@@ -231,7 +262,58 @@ pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
231262
ptrace_get_data::<user_regs_struct>(Request::PTRACE_GETREGS, pid)
232263
}
233264

265+
/// Get user registers, as with `ptrace(PTRACE_GETREGS, ...)`
266+
///
267+
/// Note that since `PTRACE_GETREGS` are not available on all platforms (as in [ptrace(2)]),
268+
/// `ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, ...)` is used instead to achieve the same effect
269+
/// on aarch64 and riscv64.
270+
///
271+
/// [ptrace(2)]: https://www.man7.org/linux/man-pages/man2/ptrace.2.html
272+
#[cfg(all(
273+
target_os = "linux",
274+
target_env = "gnu",
275+
any(target_arch = "aarch64", target_arch = "riscv64")
276+
))]
277+
pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
278+
getregset(pid, RegisterSet::NT_PRSTATUS)
279+
}
280+
281+
/// Get a particular set of user registers, as with `ptrace(PTRACE_GETREGSET, ...)`
282+
#[cfg(all(
283+
target_os = "linux",
284+
target_env = "gnu",
285+
any(
286+
target_arch = "x86_64",
287+
target_arch = "x86",
288+
target_arch = "aarch64",
289+
target_arch = "riscv64",
290+
)
291+
))]
292+
pub fn getregset(pid: Pid, set: RegisterSet) -> Result<user_regs_struct> {
293+
let request = Request::PTRACE_GETREGSET;
294+
let mut data = mem::MaybeUninit::<user_regs_struct>::uninit();
295+
let mut iov = libc::iovec {
296+
iov_base: data.as_mut_ptr().cast(),
297+
iov_len: mem::size_of::<user_regs_struct>(),
298+
};
299+
unsafe {
300+
ptrace_other(
301+
request,
302+
pid,
303+
set as i32 as AddressType,
304+
(&mut iov as *mut libc::iovec).cast(),
305+
)?;
306+
};
307+
Ok(unsafe { data.assume_init() })
308+
}
309+
234310
/// Set user registers, as with `ptrace(PTRACE_SETREGS, ...)`
311+
///
312+
/// Note that since `PTRACE_SETREGS` are not available on all platforms (as in [ptrace(2)]),
313+
/// `ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, ...)` is used instead to achieve the same effect
314+
/// on aarch64 and riscv64.
315+
///
316+
/// [ptrace(2)]: https://www.man7.org/linux/man-pages/man2/ptrace.2.html
235317
#[cfg(all(
236318
target_os = "linux",
237319
any(
@@ -248,12 +330,59 @@ pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
248330
Request::PTRACE_SETREGS as RequestType,
249331
libc::pid_t::from(pid),
250332
ptr::null_mut::<c_void>(),
251-
&regs as *const _ as *const c_void,
333+
&regs as *const user_regs_struct as *const c_void,
252334
)
253335
};
254336
Errno::result(res).map(drop)
255337
}
256338

339+
/// Set user registers, as with `ptrace(PTRACE_SETREGS, ...)`
340+
///
341+
/// Note that since `PTRACE_SETREGS` are not available on all platforms (as in [ptrace(2)]),
342+
/// `ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, ...)` is used instead to achieve the same effect
343+
/// on aarch64 and riscv64.
344+
///
345+
/// [ptrace(2)]: https://www.man7.org/linux/man-pages/man2/ptrace.2.html
346+
#[cfg(all(
347+
target_os = "linux",
348+
target_env = "gnu",
349+
any(target_arch = "aarch64", target_arch = "riscv64")
350+
))]
351+
pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
352+
setregset(pid, RegisterSet::NT_PRSTATUS, regs)
353+
}
354+
355+
/// Set a particular set of user registers, as with `ptrace(PTRACE_SETREGSET, ...)`
356+
#[cfg(all(
357+
target_os = "linux",
358+
target_env = "gnu",
359+
any(
360+
target_arch = "x86_64",
361+
target_arch = "x86",
362+
target_arch = "aarch64",
363+
target_arch = "riscv64",
364+
)
365+
))]
366+
pub fn setregset(
367+
pid: Pid,
368+
set: RegisterSet,
369+
mut regs: user_regs_struct,
370+
) -> Result<()> {
371+
let mut iov = libc::iovec {
372+
iov_base: (&mut regs as *mut user_regs_struct).cast(),
373+
iov_len: mem::size_of::<user_regs_struct>(),
374+
};
375+
unsafe {
376+
ptrace_other(
377+
Request::PTRACE_SETREGSET,
378+
pid,
379+
set as i32 as AddressType,
380+
(&mut iov as *mut libc::iovec).cast(),
381+
)?;
382+
}
383+
Ok(())
384+
}
385+
257386
/// Function for ptrace requests that return values from the data field.
258387
/// Some ptrace get requests populate structs or larger elements than `c_long`
259388
/// and therefore use the data field to return values. This function handles these

test/sys/test_ptrace.rs

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#[cfg(all(
22
target_os = "linux",
3-
any(target_arch = "x86_64", target_arch = "x86"),
4-
target_env = "gnu"
3+
target_env = "gnu",
4+
any(
5+
target_arch = "x86_64",
6+
target_arch = "x86",
7+
target_arch = "aarch64",
8+
target_arch = "riscv64",
9+
)
510
))]
611
use memoffset::offset_of;
712
use nix::errno::Errno;
@@ -179,8 +184,13 @@ fn test_ptrace_interrupt() {
179184
// ptrace::{setoptions, getregs} are only available in these platforms
180185
#[cfg(all(
181186
target_os = "linux",
182-
any(target_arch = "x86_64", target_arch = "x86"),
183-
target_env = "gnu"
187+
target_env = "gnu",
188+
any(
189+
target_arch = "x86_64",
190+
target_arch = "x86",
191+
target_arch = "aarch64",
192+
target_arch = "riscv64",
193+
)
184194
))]
185195
#[test]
186196
fn test_ptrace_syscall() {
@@ -226,12 +236,21 @@ fn test_ptrace_syscall() {
226236
let get_syscall_id =
227237
|| ptrace::getregs(child).unwrap().orig_eax as libc::c_long;
228238

239+
#[cfg(target_arch = "aarch64")]
240+
let get_syscall_id =
241+
|| ptrace::getregs(child).unwrap().regs[8] as libc::c_long;
242+
243+
#[cfg(target_arch = "riscv64")]
244+
let get_syscall_id =
245+
|| ptrace::getregs(child).unwrap().a7 as libc::c_long;
246+
229247
// this duplicates `get_syscall_id` for the purpose of testing `ptrace::read_user`.
230248
#[cfg(target_arch = "x86_64")]
231249
let rax_offset = offset_of!(libc::user_regs_struct, orig_rax);
232250
#[cfg(target_arch = "x86")]
233251
let rax_offset = offset_of!(libc::user_regs_struct, orig_eax);
234252

253+
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
235254
let get_syscall_from_user_area = || {
236255
// Find the offset of `user.regs.rax` (or `user.regs.eax` for x86)
237256
let rax_offset = offset_of!(libc::user, regs) + rax_offset;
@@ -246,6 +265,7 @@ fn test_ptrace_syscall() {
246265
Ok(WaitStatus::PtraceSyscall(child))
247266
);
248267
assert_eq!(get_syscall_id(), ::libc::SYS_kill);
268+
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
249269
assert_eq!(get_syscall_from_user_area(), ::libc::SYS_kill);
250270

251271
// kill exit
@@ -255,6 +275,7 @@ fn test_ptrace_syscall() {
255275
Ok(WaitStatus::PtraceSyscall(child))
256276
);
257277
assert_eq!(get_syscall_id(), ::libc::SYS_kill);
278+
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
258279
assert_eq!(get_syscall_from_user_area(), ::libc::SYS_kill);
259280

260281
// receive signal
@@ -273,3 +294,76 @@ fn test_ptrace_syscall() {
273294
}
274295
}
275296
}
297+
298+
#[cfg(all(
299+
target_os = "linux",
300+
target_env = "gnu",
301+
any(
302+
target_arch = "x86_64",
303+
target_arch = "x86",
304+
target_arch = "aarch64",
305+
target_arch = "riscv64",
306+
)
307+
))]
308+
#[test]
309+
fn test_ptrace_regsets() {
310+
use nix::sys::ptrace::{self, getregset, setregset, RegisterSet};
311+
use nix::sys::signal::*;
312+
use nix::sys::wait::{waitpid, WaitStatus};
313+
use nix::unistd::fork;
314+
use nix::unistd::ForkResult::*;
315+
316+
require_capability!("test_ptrace_regsets", CAP_SYS_PTRACE);
317+
318+
let _m = crate::FORK_MTX.lock();
319+
320+
match unsafe { fork() }.expect("Error: Fork Failed") {
321+
Child => {
322+
ptrace::traceme().unwrap();
323+
// As recommended by ptrace(2), raise SIGTRAP to pause the child
324+
// until the parent is ready to continue
325+
loop {
326+
raise(Signal::SIGTRAP).unwrap();
327+
}
328+
}
329+
330+
Parent { child } => {
331+
assert_eq!(
332+
waitpid(child, None),
333+
Ok(WaitStatus::Stopped(child, Signal::SIGTRAP))
334+
);
335+
let mut regstruct =
336+
getregset(child, RegisterSet::NT_PRSTATUS).unwrap();
337+
338+
#[cfg(target_arch = "x86_64")]
339+
let reg = &mut regstruct.r15;
340+
#[cfg(target_arch = "x86")]
341+
let reg = &mut regstruct.edx;
342+
#[cfg(target_arch = "aarch64")]
343+
let reg = &mut regstruct.regs[16];
344+
#[cfg(target_arch = "riscv64")]
345+
let reg = &mut regstruct.regs[16];
346+
347+
*reg = 0xdeadbeefu32 as _;
348+
let _ = setregset(child, RegisterSet::NT_PRSTATUS, regstruct);
349+
regstruct = getregset(child, RegisterSet::NT_PRSTATUS).unwrap();
350+
351+
#[cfg(target_arch = "x86_64")]
352+
let reg = regstruct.r15;
353+
#[cfg(target_arch = "x86")]
354+
let reg = regstruct.edx;
355+
#[cfg(target_arch = "aarch64")]
356+
let reg = regstruct.regs[16];
357+
#[cfg(target_arch = "riscv64")]
358+
let reg = regstruct.regs[16];
359+
assert_eq!(reg, 0xdeadbeefu32 as _);
360+
361+
ptrace::cont(child, Some(Signal::SIGKILL)).unwrap();
362+
match waitpid(child, None) {
363+
Ok(WaitStatus::Signaled(pid, Signal::SIGKILL, _))
364+
if pid == child => {}
365+
_ => panic!("The process should have been killed"),
366+
}
367+
}
368+
}
369+
}

0 commit comments

Comments
 (0)