-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptrace.rs
More file actions
executable file
·109 lines (98 loc) · 3.38 KB
/
Copy pathptrace.rs
File metadata and controls
executable file
·109 lines (98 loc) · 3.38 KB
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
use std::process::{Command, Child};
use std::os::unix::process::CommandExt;
use log::{info, debug};
use nix::sys::ptrace;
use nix::sys::signal::Signal;
use nix::sys::wait::{waitpid, wait};
use nix::unistd::Pid;
use nix::sys::wait::WaitStatus;
use crate::error::Result;
use super::registers::{Registers, SysReg, Word};
pub unsafe fn create_process(mut cmd : Command) -> Result<Child> {
// ! pre_exec use fork, can't handle child's error, so make it unsafe
cmd.pre_exec(move || {
match ptrace::traceme() {
Ok(()) => Ok(()),
Err(_) => Err(
std::io::Error::new(
std::io::ErrorKind::Other,
"traceme failed"
)
),
}
});
let child = cmd.spawn()
.expect("command spawn failed");
configure(child.id() as i32)?;
Ok(child)
}
fn configure(pid : i32) -> Result<()> {
let pid = Pid::from_raw(pid);
if let WaitStatus::Stopped(_, sig) = waitpid(pid, None).unwrap() {
assert_eq!(sig, Signal::SIGTRAP);
ptrace_set_option(pid)?;
ptrace::syscall(pid, None)?;
} else {
panic!("child not stop");
}
Ok(())
}
fn ptrace_set_option(pid : Pid) -> Result<()> {
use ptrace::Options;
ptrace::setoptions(pid,
Options::PTRACE_O_TRACESYSGOOD |
Options::PTRACE_O_TRACEFORK |
Options::PTRACE_O_TRACEVFORK |
Options::PTRACE_O_TRACECLONE |
Options::PTRACE_O_TRACEEXIT
)?;
Ok(())
}
pub trait Tracer {
//Kernel 实现 Tracer,可以通过实现 enter/exit 来使用ptrace。
fn enter_syscall(&mut self, pid : Pid);
fn exit_syscall(&mut self, pid : Pid);
fn event_loop(&mut self) -> Result<()> {
info!("ptrace event loop start");
let mut entering = true;
loop {
let status = wait().expect("wait failed!");
debug!("{:?}", status);
match status {
WaitStatus::PtraceSyscall(pid) if entering => {
self.enter_syscall(pid);
ptrace::syscall(pid, None)
.expect("ptrace::syscall failed");
entering = !entering;
}
WaitStatus::PtraceSyscall(pid) if !entering => {
self.exit_syscall(pid);
ptrace::syscall(pid, None)
.expect("ptrace::syscall failed");
entering = !entering;
}
WaitStatus::Stopped(pid, sig) => {
info!("{} stopped with {}", pid, sig);
ptrace_set_option(pid).unwrap();
ptrace::syscall(pid, None)
.expect("ptrace::syscall failed");
}
WaitStatus::PtraceEvent(pid, Signal::SIGTRAP, i) => {
info!("{} SIGTRAP with {}", pid, i);
ptrace_set_option(pid).unwrap();
ptrace::syscall(pid, None)
.expect("ptrace::syscall failed");
}
WaitStatus::Exited(pid, i) => {
info!("process {} exit with {}", pid, i);
break;
}
_ => {
panic!("unimplement event!");
}
}
};
info!("ptrace end");
Ok(())
}
}