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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#[cfg(use_libunwind)]
pub mod libunwind;
#[cfg(use_libunwind)]
mod symbolication;
use libc::pid_t;
use nix::{self, sys::wait, sys::ptrace, {sched::{setns, CloneFlags}}};
use std::collections::HashMap;
use std::convert::TryInto;
use std::io::Read;
use std::os::unix::io::AsRawFd;
use std::fs::File;
use super::Error;
#[cfg(use_libunwind)]
pub use self::symbolication::*;
#[cfg(use_libunwind)]
pub use self::libunwind::Unwinder;
use read_process_memory::{CopyAddress, ProcessHandle};
pub type Pid = pid_t;
pub type Tid = pid_t;
pub struct Process {
pub pid: Pid,
}
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
pub struct Thread {
tid: nix::unistd::Pid
}
impl Process {
pub fn new(pid: Pid) -> Result<Process, Error> {
Ok(Process{pid})
}
pub fn exe(&self) -> Result<String, Error> {
let path = std::fs::read_link(format!("/proc/{}/exe", self.pid))?;
Ok(path.to_string_lossy().to_string())
}
pub fn cwd(&self) -> Result<String, Error> {
let path = std::fs::read_link(format!("/proc/{}/cwd", self.pid))?;
Ok(path.to_string_lossy().to_string())
}
pub fn cmdline(&self) -> Result<Vec<String>, Error> {
let mut f = std::fs::File::open(format!("/proc/{}/cmdline", self.pid))?;
let mut buffer = Vec::new();
f.read_to_end(&mut buffer)?;
let mut ret = Vec::new();
for arg in buffer.split(|b| *b == 0).filter(|b| b.len() > 0) {
ret.push(String::from_utf8(arg.to_vec())
.map_err(|e| Error::Other(format!("Failed to convert utf8 {}", e)))?)
}
Ok(ret)
}
pub fn lock(&self) -> Result<Lock, Error> {
let mut locks = Vec::new();
let mut locked = std::collections::HashSet::new();
let mut done = false;
while !done {
done = true;
for thread in self.threads()? {
let threadid = thread.id()?;
if !locked.contains(&threadid) {
locks.push(thread.lock()?);
locked.insert(threadid);
done = false;
}
}
}
Ok(Lock{locks})
}
pub fn threads(&self) -> Result<Vec<Thread>, Error> {
let mut ret = Vec::new();
let path = format!("/proc/{}/task", self.pid);
let tasks = std::fs::read_dir(path)?;
for entry in tasks {
let entry = entry?;
let filename = entry.file_name();
let thread = match filename.to_str() {
Some(thread) => thread,
None => continue
};
if let Ok(threadid) = thread.parse::<i32>() {
ret.push(Thread{tid: nix::unistd::Pid::from_raw(threadid)});
}
}
Ok(ret)
}
pub fn child_processes(&self) -> Result<Vec<(Pid, Pid)>, Error> {
let processes = get_process_tree()?;
Ok(crate::filter_child_pids(self.pid, &processes))
}
#[cfg(use_libunwind)]
pub fn unwinder(&self) -> Result<Unwinder, Error> {
Ok(Unwinder::new()?)
}
#[cfg(use_libunwind)]
pub fn symbolicator(&self) -> Result<Symbolicator, Error> {
Ok(Symbolicator::new(self.pid)?)
}
}
impl super::ProcessMemory for Process {
fn read(&self, addr: usize, buf: &mut [u8]) -> Result<(), Error> {
let handle: ProcessHandle = self.pid.try_into()?;
Ok(handle.copy_address(addr, buf)?)
}
}
impl Thread {
pub fn new(threadid: i32) -> Result<Thread, Error> {
Ok(Thread{tid: nix::unistd::Pid::from_raw(threadid)})
}
pub fn lock(&self) -> Result<ThreadLock, Error> {
Ok(ThreadLock::new(self.tid)?)
}
pub fn id(&self) -> Result<Tid, Error> {
Ok(self.tid.as_raw())
}
pub fn active(&self) -> Result<bool, Error> {
let mut file = File::open(format!("/proc/{}/stat", self.tid))?;
let mut buf=[0u8; 512];
file.read(&mut buf)?;
match get_active_status(&buf) {
Some(stat) => Ok(stat == b'R'),
None => Err(Error::Other(format!("Failed to parse /proc/{}/stat", self.tid)))
}
}
}
fn get_process_tree() -> Result<HashMap<Pid, Pid>, Error> {
let mut ret = HashMap::new();
for entry in std::fs::read_dir("/proc")? {
let entry = entry?;
let filename = entry.file_name();
let pid = match filename.to_str() {
Some(pid) => pid,
None => continue
};
if let Ok(pid) = pid.parse::<Pid>() {
match get_parent_pid(pid) {
Ok(ppid) => { ret.insert(pid, ppid) },
Err(_) => continue
};
}
}
Ok(ret)
}
pub struct Lock {
#[allow(dead_code)]
locks: Vec<ThreadLock>
}
pub struct ThreadLock {
tid: nix::unistd::Pid
}
impl ThreadLock {
fn new(tid: nix::unistd::Pid) -> Result<ThreadLock, nix::Error> {
ptrace::attach(tid)?;
wait::waitpid(tid, Some(wait::WaitPidFlag::WSTOPPED | wait::WaitPidFlag::__WALL))?;
debug!("attached to thread {}", tid);
Ok(ThreadLock{tid})
}
}
impl Drop for ThreadLock {
fn drop(&mut self) {
if let Err(e) = ptrace::detach(self.tid, None) {
warn!("Failed to detach from thread {} : {}", self.tid, e);
}
debug!("detached from thread {}", self.tid);
}
}
pub struct Namespace {
ns_file: Option<File>
}
impl Namespace {
pub fn new(pid: Pid) -> Result<Namespace, Error> {
let target_ns_filename = format!("/proc/{}/ns/mnt", pid);
let self_mnt = std::fs::read_link("/proc/self/ns/mnt")?;
let target_mnt = std::fs::read_link(&target_ns_filename)?;
if self_mnt != target_mnt {
info!("Process {} appears to be running in a different namespace - setting namespace to match", pid);
let target = File::open(target_ns_filename)?;
let self_ns = File::open("/proc/self/ns/mnt")?;
setns(target.as_raw_fd(), CloneFlags::from_bits_truncate(0))?;
Ok(Namespace{ns_file: Some(self_ns)})
} else {
info!("Target process is running in same namespace - not changing");
Ok(Namespace{ns_file: None})
}
}
pub fn is_set(self) -> bool {
self.ns_file.is_some()
}
}
impl Drop for Namespace {
fn drop(&mut self) {
if let Some(ns_file) = self.ns_file.as_ref() {
setns(ns_file.as_raw_fd(), CloneFlags::from_bits_truncate(0)).unwrap();
info!("Restored process namespace");
}
}
}
fn get_active_status(stat: &[u8]) -> Option<u8> {
let mut iter = stat.iter().skip_while(|x| **x != b')');
match (iter.next(), iter.next(), iter.next()) {
(Some(b')'), Some(b' '), ret) => ret.map(|x| *x),
_ => None
}
}
fn get_parent_pid(pid: Pid) -> Result<Pid, Error> {
let mut file = File::open(format!("/proc/{}/stat", pid))?;
let mut buf=[0u8; 512];
file.read(&mut buf)?;
get_ppid_status(&buf).ok_or_else(|| Error::Other(format!("Failed to parse /proc/{}/stat", pid)))
}
fn get_ppid_status(stat: &[u8]) -> Option<Pid> {
lazy_static! {
static ref RE: regex::bytes::Regex = regex::bytes::Regex::new(r"^\d+ \(.+\) \w (\d+)").unwrap();
}
let caps = RE.captures(stat)?;
std::str::from_utf8(caps.get(1)?.as_bytes()).ok()?.parse::<Pid>().ok()
}
#[test]
fn test_parse_active_stat() {
assert_eq!(get_active_status(b"1234 (bash) S 1233"), Some(b'S'));
assert_eq!(get_active_status(b"1234 (with space) R 1233"), Some(b'R'));
assert_eq!(get_active_status(b"1234"), None);
assert_eq!(get_active_status(b")"), None);
assert_eq!(get_active_status(b")))"), None);
assert_eq!(get_active_status(b"1234 (bash)S"), None);
assert_eq!(get_active_status(b"1234)SSSS"), None);
assert_eq!(get_active_status(b"15379 (ipython) t 9898 15379 9898 34816"), Some(b't'));
}
#[test]
fn test_parse_ppid_stat() {
assert_eq!(get_ppid_status(b"1234 (bash) S 1233"), Some(1233));
assert_eq!(get_ppid_status(b"1234 (with space) R 1233"), Some(1233));
assert_eq!(get_ppid_status(b"1234"), None);
assert_eq!(get_ppid_status(b")"), None);
assert_eq!(get_ppid_status(b")))"), None);
assert_eq!(get_ppid_status(b"1234 (bash)S"), None);
assert_eq!(get_ppid_status(b"1234)SSSS"), None);
assert_eq!(get_ppid_status(b"15379 (ipython) t 9898 15379 9898 34816"), Some(9898));
}