Skip to content

Commit e0f1965

Browse files
ShuiRuTianShuiRuTian
and
ShuiRuTian
authored
Change the type of data of ptrace::write to make it safe (#2324)
* Change the type of data of ptrace::write to make it safe * add change log * add safe commnet --------- Co-authored-by: ShuiRuTian <[email protected]>
1 parent 358fe9f commit e0f1965

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

changelog/2324.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Change the signature of `ptrace::write` and `ptrace::write_user` to make them safe

src/sys/ptrace/linux.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -543,17 +543,15 @@ pub fn read(pid: Pid, addr: AddressType) -> Result<c_long> {
543543

544544
/// Writes a word into the processes memory at the given address, as with
545545
/// ptrace(PTRACE_POKEDATA, ...)
546-
///
547-
/// # Safety
548-
///
549-
/// The `data` argument is passed directly to `ptrace(2)`. Read that man page
550-
/// for guidance.
551-
pub unsafe fn write(
552-
pid: Pid,
553-
addr: AddressType,
554-
data: *mut c_void,
555-
) -> Result<()> {
556-
unsafe { ptrace_other(Request::PTRACE_POKEDATA, pid, addr, data).map(drop) }
546+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
547+
pub fn write(pid: Pid, addr: AddressType, data: c_long) -> Result<()> {
548+
unsafe {
549+
// Safety(not_unsafe_ptr_arg_deref):
550+
// `ptrace_other` is a common abstract
551+
// but in `PTRACE_POKEDATA` situation, `data` is exactly what will be wtitten
552+
ptrace_other(Request::PTRACE_POKEDATA, pid, addr, data as *mut c_void)
553+
.map(drop)
554+
}
557555
}
558556

559557
/// Reads a word from a user area at `offset`, as with ptrace(PTRACE_PEEKUSER, ...).
@@ -564,17 +562,13 @@ pub fn read_user(pid: Pid, offset: AddressType) -> Result<c_long> {
564562

565563
/// Writes a word to a user area at `offset`, as with ptrace(PTRACE_POKEUSER, ...).
566564
/// The user struct definition can be found in `/usr/include/sys/user.h`.
567-
///
568-
/// # Safety
569-
///
570-
/// The `data` argument is passed directly to `ptrace(2)`. Read that man page
571-
/// for guidance.
572-
pub unsafe fn write_user(
573-
pid: Pid,
574-
offset: AddressType,
575-
data: *mut c_void,
576-
) -> Result<()> {
565+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
566+
pub fn write_user(pid: Pid, offset: AddressType, data: c_long) -> Result<()> {
577567
unsafe {
578-
ptrace_other(Request::PTRACE_POKEUSER, pid, offset, data).map(drop)
568+
// Safety(not_unsafe_ptr_arg_deref):
569+
// `ptrace_other` is a common abstract
570+
// but in `PTRACE_POKEDATA` situation, `data` is exactly what will be wtitten
571+
ptrace_other(Request::PTRACE_POKEUSER, pid, offset, data as *mut c_void)
572+
.map(drop)
579573
}
580574
}

0 commit comments

Comments
 (0)