Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the type of data of ptrace::write to make it safe #2324

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2324.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change the signature of `ptrace::write` and `ptrace::write_user` to make them safe
38 changes: 16 additions & 22 deletions src/sys/ptrace/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,15 @@ pub fn read(pid: Pid, addr: AddressType) -> Result<c_long> {

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

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

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