Skip to content

Commit 067f937

Browse files
committed
Add high-level wrappers for some common ptrace routines and mark nix::sys::ptrace::ptrace as unsafe.
`nix::sys::ptrace::ptrace` is unsafe by design. Its incorrect usage may lead to race conditions or crashes, and according to this post [1] should be marked unsafe. Additionally, wrappers with typically Rusty API have been introduced for some of the common ptrace routines. It is not a complete coverage and this subject should be revisited in the future. Some of these are `unsafe` by the sheer specification and have been marked so.
1 parent ff99768 commit 067f937

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/sys/ptrace.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ mod ffi {
7070
}
7171
}
7272

73-
/// Performs a ptrace request. If the request in question is provided by a specialised function
73+
/// A low-level wrapper for `ptrace`. If available, the higher-level wrappers should be considered instead.
74+
/// Performs a `ptrace` request. If the request in question is provided by a specialised function
7475
/// this function will return an unsupported operation error.
75-
pub fn ptrace(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
76+
///
77+
/// When used incorrectly, this function may crash the tracer or the tracee, thus is marked `unsafe`.
78+
pub unsafe fn ptrace(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
7679
use self::ptrace::*;
7780

7881
match request {
@@ -202,12 +205,19 @@ pub fn traceme() -> Result<()> {
202205
}
203206

204207
/// Makes the `PTRACE_PEEKDATA` request to ptrace
205-
pub fn peekdata(pid: Pid, addr: c_long) -> Result<c_long> {
208+
///
209+
/// This function allows to access arbitrary data in the traced process
210+
/// and may crash the inferior if used incorrectly and is thus marked `unsafe`.
211+
pub unsafe fn peekdata(pid: Pid, addr: c_long) -> Result<c_long> {
206212
ptrace(ptrace::PTRACE_PEEKDATA, pid, addr as *mut c_void, ptr::null_mut())
207213
}
208214

209215
/// Makes the `PTRACE_PEEKDATA` request to ptrace
210-
pub fn pokedata(pid: Pid, addr: c_long, val: c_long) -> Result<()> {
216+
///
217+
/// This function allows to access arbitrary data in the traced process
218+
/// and may crash the inferior or introduce race conditions if used
219+
/// incorrectly and is thus marked `unsafe`.
220+
pub unsafe fn pokedata(pid: Pid, addr: c_long, val: c_long) -> Result<()> {
211221
ptrace(
212222
ptrace::PTRACE_POKEDATA,
213223
pid,

0 commit comments

Comments
 (0)