Skip to content

Commit

Permalink
Fallback to /proc/$pid/mem when process_vm_readv() is unavailabe
Browse files Browse the repository at this point in the history
If the Linux kernel was built without support for process_vm_readv()
(the CONFIG_CROSS_MEMORY_ATTACH is unset in memory) a call to this
function will fail with Function not implemented (os error 38) aka
ENOSYS. To still support reading process memory on this platform we
fallback to reading /proc/$pid/mem.
  • Loading branch information
sebageek committed Jan 24, 2021
1 parent 1d5a44f commit 98110bf
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ mod platform {
use libc::{pid_t, c_void, iovec, process_vm_readv};
use std::convert::TryFrom;
use std::io;
use std::fs;
use std::io::Seek;
use std::io::Read;
use std::process::Child;

use super::{CopyAddress};
Expand Down Expand Up @@ -105,7 +108,15 @@ mod platform {
};
let result = unsafe { process_vm_readv(self.0, &local_iov, 1, &remote_iov, 1, 0) };
if result == -1 {
Err(io::Error::last_os_error())
if let Some(libc::ENOSYS) = io::Error::last_os_error().raw_os_error() {
// fallback to reading /proc/$pid/mem if kernel does not
// implement process_vm_readv()
let mut procmem = fs::File::open(format!("/proc/{}/mem", self.0))?;
procmem.seek(io::SeekFrom::Start(addr as u64))?;
return procmem.read_exact(buf);
} else {
Err(io::Error::last_os_error())
}
} else {
Ok(())
}
Expand Down

0 comments on commit 98110bf

Please sign in to comment.