-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
224 lines (193 loc) · 6.95 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate gtrace_derive;
//extern crate libc;
extern crate nix;
use nix::libc;
use libc::pid_t;
use nix::sys::wait::WaitStatus;
use nix::sys::ptrace::*;
use nix::sys::ptrace::ptrace::*;
use nix::Result;
pub mod arch;
pub mod decode;
pub mod syscall;
pub fn traceme() -> std::io::Result<()> {
match ptrace(PTRACE_TRACEME, 0, 0 as *mut _, 0 as *mut _) {
Ok(_) => Ok(()),
Err(e) => Err(std::io::Error::from(e))
}
}
pub enum TraceEvent {
SysEnter,
SysExit,
Signal(u8),
Exit(i8),
}
enum State {
Userspace,
Kernelspace,
}
pub struct Tracee {
pid: pid_t,
state: State,
}
impl Tracee {
pub fn new(pid: pid_t) -> Tracee {
ptrace_setoptions(pid, PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXEC).unwrap();
Tracee {
pid: pid,
state: State::Userspace,
}
}
pub fn step(&mut self, status: WaitStatus) -> TraceEvent {
match status {
WaitStatus::PtraceSyscall(_) => {
match self.state {
State::Userspace => {
self.state = State::Kernelspace;
TraceEvent::SysEnter
},
State::Kernelspace => {
self.state = State::Userspace;
TraceEvent::SysExit
},
}
}
WaitStatus::Exited(_, status) => TraceEvent::Exit(status),
WaitStatus::Stopped(_, sig) => TraceEvent::Signal(sig as u8),
_ => panic!("unexpected status {:?}", status)
}
}
pub fn run(&mut self) -> Result<()> {
ptrace(PTRACE_SYSCALL, self.pid, 0 as *mut _, 0 as *mut _).map(drop)
}
pub fn get_syscall(&mut self) -> Result<u64> {
ptrace(PTRACE_PEEKUSER, self.pid, arch::x86_64::ORIG_RAX as *mut _, 0 as *mut _).map(|x| x as u64)
}
pub fn get_arg(&mut self, reg: u8) -> Result<u64> {
let offset = match reg {
0 => arch::x86_64::RDI,
1 => arch::x86_64::RSI,
2 => arch::x86_64::RDX,
3 => arch::x86_64::R10,
4 => arch::x86_64::R8,
5 => arch::x86_64::R9,
_ => panic!("there aren't that many registers")
};
ptrace(PTRACE_PEEKUSER, self.pid, offset as *mut _, 0 as *mut _).map(|x| x as u64)
}
pub fn get_return(&mut self) -> Result<i64> {
ptrace(PTRACE_PEEKUSER, self.pid, arch::x86_64::RAX as *mut _, 0 as *mut _)
}
/// Read len bytes from addr. May return fewer than len bytes if
/// part of the range is unmapped, or an error if addr itself is
/// unmapped.
pub fn copy_from(&mut self, addr: usize, len: usize) -> Result<Vec<u8>> {
use nix::sys::uio::*;
let mut res = Vec::with_capacity(len);
unsafe {
res.set_len(len);
let target: Vec<_> = PageIter::new(addr, len, arch::x86_64::PAGE_SIZE)
.map(|(a, l)| IoVec::from_slice(std::slice::from_raw_parts(a as *const _, l)))
.collect();
let n = {
try!(process_vm_readv(self.pid,
&mut [IoVec::from_mut_slice(&mut res)],
&target[..]))
};
res.set_len(n);
}
Ok(res)
}
/// Read a NUL-terminated C string of up to len bytes from addr.
/// Returns an error if none of the string could be read,
/// Ok(vec, false) if some of the string could be read but we ran into an unmapped page, or
/// Ok(vec, true) if all of the string could be read, up to either len or
/// the terminating NUL. As with strncpy, if there is no terminating NUL in
/// the first len bytes, no terminating NUL will be in the output.
pub fn strncpy_from(&mut self, addr: usize, len: usize) -> Result<(Vec<u8>, bool)> {
use nix::sys::uio::*;
use nix::Error::Sys;
use nix::Errno::EFAULT;
let mut remote_pages = PageIter::new(addr, len, arch::x86_64::PAGE_SIZE);
let mut res = Vec::with_capacity(len);
// Read the first chunk, returning an error if it didn't work
match remote_pages.next() {
None => { return Ok((Vec::new(), true)); }
Some((chunkaddr, chunklen)) => unsafe {
res.set_len(chunklen);
let n = {
try!(process_vm_readv(self.pid,
&mut [IoVec::from_mut_slice(&mut res)],
&[IoVec::from_slice(std::slice::from_raw_parts(chunkaddr as *const u8, chunklen))]))
};
res.set_len(n);
}
}
let mut oldlen = 0;
loop {
// Try to find a terminating NUL
if let Some(nul) = res[oldlen..].iter().position(|&b| b == 0) {
res.truncate(oldlen + nul + 1);
return Ok((res, true));
}
oldlen = res.len();
// If not, read the next page, but don't error on EFAULT,
// just report the partial read. Report all other errors as
// errors.
match remote_pages.next() {
None => { return Ok((res, true)); }
Some((chunkaddr, chunklen)) => unsafe {
res.set_len(oldlen + chunklen);
match { process_vm_readv(self.pid,
&mut [IoVec::from_mut_slice(&mut res[oldlen..])],
&[IoVec::from_slice(std::slice::from_raw_parts(chunkaddr as *const u8, chunklen))]) } {
Ok(n) => { res.set_len(n); }
Err(Sys(EFAULT)) => { return Ok((res, false)); }
Err(e) => { return Err(e); }
}
}
}
}
}
}
struct PageIter {
addr: usize,
max_addr: usize,
page_size: usize,
}
impl PageIter {
fn new(addr: usize, len: usize, page_size: usize) -> PageIter {
PageIter { addr: addr, max_addr: addr + len, page_size: page_size }
}
}
impl Iterator for PageIter {
type Item = (usize, usize);
fn next(&mut self) -> Option<(usize, usize)> {
if self.addr == self.max_addr {
return None;
}
let mut len = self.page_size - (self.addr % self.page_size);
if self.addr + len > self.max_addr {
len = self.max_addr - self.addr;
}
let ret = Some((self.addr, len));
self.addr += len;
ret
}
}
#[test]
fn test_page_iter() {
fn check(addr: usize, len: usize, v: Vec<(usize, usize)>) {
let iov: Vec<(usize, usize)> = PageIter::new(addr, len, 10).collect();
assert_eq!(iov, v);
}
check(3, 5, vec![(3, 5)]);
check(3, 21, vec![(3, 7), (10, 10), (20, 4)]);
check(10, 10, vec![(10, 10)]);
check(10, 11, vec![(10, 10), (20, 1)]);
check(10, 0, vec![]);
}