hstrace 0.0.5

Syscall tracing from command line and as a library
Documentation
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use nix::libc;
use nix::sys::{ptrace, wait};
use nix::unistd::Pid;
use std::{mem, process::Child, process::Command};

use super::{GetPtraceInfo, Tracer};
use crate::from_c;
use crate::TraceError;
use crate::TraceType;

#[derive(Debug)]
pub struct SyscallPtrace {
    pid: usize,
    cmd: Option<Child>,
    iteration: usize,
    current_pid: usize,
    is_exited: bool,
    is_finalized: bool,
}

const PTRACE_EVENT_STOP: i32 = 128; // FIXME move to system-dependant

impl SyscallPtrace {
    pub fn new(trace_type: TraceType) -> Result<Self, TraceError> {
        let (pid, cmd) = match trace_type {
            TraceType::Program(command, args) => {
                let cmd = Command::new(&command)
                    .args(&args)
                    //.stdout(std::process::Stdio::null())
                    //.stderr(std::process::Stdio::null())
                    .spawn()
                    .map_err(|e| TraceError::StringError(format!("{:?}", e)))?;

                let pid = cmd.id() as usize;

                log::debug!(
                    "Spawned command {:?} with args {:?} -> PID: {:?}",
                    command,
                    args,
                    pid
                );

                (pid, Some(cmd))
            }
            TraceType::Pid(pid) => (pid, None),
        };

        let current_pid = pid.clone();

        Ok(SyscallPtrace {
            pid,
            cmd,
            iteration: 0,
            current_pid,
            is_exited: false,
            is_finalized: false,
        })
    }

    #[inline]
    fn run_syscall(&self) -> Result<(), TraceError> {
        log::debug!("ptrace(PTRACE_SYSCALL, {}, ...)", self.current_pid);

        if let Err(e) = ptrace::syscall(Pid::from_raw(self.current_pid as i32), None) {
            log::error!(
                "ptrace(PTRACE_SYSCALL, {}) error: {:?}",
                self.current_pid,
                e
            );

            return Err(TraceError::NixError(e));
        }

        Ok(())
    }

    /// Waitpid loop
    #[inline]
    fn run_waitpid(&mut self) -> Result<bool, TraceError> {
        let ws = match wait::waitpid(
            Pid::from_raw(-1),
            Some(
                wait::WaitPidFlag::__WALL
                    | wait::WaitPidFlag::WSTOPPED
                    | wait::WaitPidFlag::WCONTINUED,
            ),
        ) {
            Err(e) => {
                log::error!("wait::waitpid(-1) error: {:?}", e);
                return Err(TraceError::NixError(e));
            }
            Ok(ws) => ws,
        };

        match ws {
            wait::WaitStatus::PtraceEvent(pid, _, event) => {
                let pid = pid.as_raw() as usize;

                match event {
                    libc::PTRACE_EVENT_FORK
                    | libc::PTRACE_EVENT_VFORK
                    | libc::PTRACE_EVENT_CLONE
                    | libc::PTRACE_EVENT_EXEC
                    | libc::PTRACE_EVENT_EXIT
                    | PTRACE_EVENT_STOP => {
                        // fork event, continue ptrace
                        log::debug!("wait::waitpid(-1) -> (pid: {}, event: {})", pid, event);
                        self.current_pid = pid;
                        self.run_syscall()?;
                        self.run_waitpid()?;
                    }

                    _ => {
                        panic!("wait::waitpid(-1) -> Unknown ptrace_event: {:?}", event);
                    }
                }
            }

            wait::WaitStatus::PtraceSyscall(pid) => {
                let pid = pid.as_raw() as usize;

                // ptrace_wait, switch pid if different
                if self.current_pid != pid {
                    log::debug!(
                        "wait::waitpid(-1) = {} (previous pid was {})",
                        pid,
                        self.current_pid
                    );
                    self.current_pid = pid;
                } else {
                    log::debug!("wait::waitpid(-1) = {}", pid);
                }
            }

            wait::WaitStatus::Exited(pid, status) => {
                let pid = pid.as_raw() as usize;

                // pid exited, run waitpid again
                if pid != self.pid {
                    // if not root pid, continue waiting
                    log::debug!(
                        "wait::waitpid(-1) -> child process exited (pid: {}, status: {})",
                        pid,
                        status
                    );

                    self.run_waitpid()?;
                } else {
                    log::debug!(
                        "wait::waitpid(-1) -> main exited (pid: {}, status: {})",
                        pid,
                        status
                    );

                    self.is_exited = true;

                    return Ok(false);
                }
            }

            _ => {
                log::debug!("wait::waitpid(-1) -> Unknown event: {:?}", ws);
            }
        }

        Ok(true)
    }
}

impl Drop for SyscallPtrace {
    fn drop(&mut self) {
        if !self.is_finalized {
            // make sure that finalize happens
            self.finalize().unwrap();
        }
    }
}

impl Tracer for SyscallPtrace {
    #[inline]
    fn initialize(&mut self) -> Result<(), TraceError> {
        if let Err(e) = ptrace::seize(
            Pid::from_raw(self.pid as i32),
            ptrace::Options::PTRACE_O_TRACESYSGOOD
                | ptrace::Options::PTRACE_O_TRACEFORK
                | ptrace::Options::PTRACE_O_TRACEVFORK
                | ptrace::Options::PTRACE_O_TRACECLONE
                | ptrace::Options::PTRACE_O_TRACEEXEC
                | ptrace::Options::PTRACE_O_TRACEEXIT,
        ) {
            log::debug!("ptrace::seize ERROR: {:?}", e);
            return Err(TraceError::NixError(e));
        }

        //if let Err(e) = ptrace::interrupt(Pid::from_raw(self.pid as i32)) { // see https://github.com/nix-rust/nix/pull/1422
        if let Err(e) = unsafe {
            ptrace::ptrace(
                ptrace::Request::PTRACE_INTERRUPT,
                Pid::from_raw(self.pid as i32),
                std::ptr::null_mut(),
                std::ptr::null_mut(),
            )
        } {
            log::error!("ptrace_interrupt error in initialization: {:?}", e);
            return Err(TraceError::NixError(e));
        }

        match wait::waitpid(Pid::from_raw(self.pid as i32), None) {
            Err(e) => {
                log::error!("Waitpid error in initialization: {:?}", e);
                return Err(TraceError::NixError(e));
            }
            Ok(_ws) => {
                //println!("Received ws {:?}", ws);
                // TODO: should we check that the WaitStatus matches `PtraceEvent(Pid(self.pid), SIGTRAP, 128`?
            }
        }

        log::debug!("Initialize ok");

        Ok(())
    }

    #[inline]
    fn prepare_next(&mut self) -> Result<bool, TraceError> {
        self.run_syscall()?;
        let has_more = self.run_waitpid()?;

        Ok(has_more)
    }

    #[inline]
    fn get_ptrace(
        &mut self,
        data_ptr: *mut from_c::ptrace_syscall_info,
    ) -> Result<GetPtraceInfo, TraceError> {
        log::debug!(
            "libc::ptrace(PTRACE_GET_SYSCALL_INFO, {}, ...)",
            self.current_pid
        );

        let ret_bytes = unsafe {
            libc::ptrace(
                from_c::PTRACE_GET_SYSCALL_INFO,
                libc::pid_t::from(Pid::from_raw(self.current_pid as i32)),
                mem::size_of::<from_c::ptrace_syscall_info>(),
                data_ptr,
            )
        };

        if ret_bytes < 0 {
            log::debug!(
                "ptrace(PTRACE_GET_SYSCALL_INFO, ...) returned {}",
                ret_bytes
            );

            if self.iteration > 0 {
                // at least one success, probably no more data, return
                return Ok(GetPtraceInfo {
                    has_more: false,
                    pid: self.current_pid,
                });
            } else {
                // no successes, kernel might not support this operation
                return Err(TraceError::PtraceGetSyscallInfo);
            }
        }

        self.iteration += 1;

        Ok(GetPtraceInfo {
            has_more: true,
            pid: self.current_pid,
        })
    }

    fn finalize(&mut self) -> Result<(), TraceError> {
        self.is_finalized = true;

        if let Some(mut cmd) = self.cmd.take() {
            if !self.is_exited {
                match cmd.kill() {
                    Err(e) => {
                        log::debug!(
                            "Could not kill command, error: {:} (this is probably ok)",
                            e
                        );
                    }
                    Ok(_) => {
                        log::debug!("cmd killed");
                    }
                }
            }
        }

        Ok(())
    }

    #[inline]
    fn read_memory_to_destination<T>(
        &mut self,
        pid: usize,
        address: usize,
        dest: *mut T,
    ) -> Result<(), TraceError> {
        let len = mem::size_of::<T>();
        let slice = unsafe { std::slice::from_raw_parts_mut(dest as *mut u8, len) };

        let read_len = match nix::sys::uio::process_vm_readv(
            Pid::from_raw(pid as i32),
            &[nix::sys::uio::IoVec::from_mut_slice(slice)],
            &[nix::sys::uio::RemoteIoVec { base: address, len }],
        ) {
            Ok(read_len) => read_len,
            Err(e) => return Err(TraceError::MemoryReadError(e, address, len, pid)),
        };

        assert_eq!(
            read_len, len,
            "Read enough memory in read_memory_to_destination"
        );

        Ok(())
    }

    #[inline]
    fn find_string_from_memory(
        &mut self,
        pid: usize,
        address: usize,
    ) -> Result<String, TraceError> {
        // FIXME 1656 used by strace, where does it come from?
        let buf = self.read_memory_to_vec(pid, address, 16560)?; // FIXME how to determine len? use 1656
        let null_byte_pos = match buf.iter().position(|b| *b == 0) {
            Some(pos) => pos,
            None => {
                panic!("Could not find null byte position, buffer: {:?}", buf);
            }
        };

        let slice = &buf[..null_byte_pos + 1];
        let c_str = std::ffi::CStr::from_bytes_with_nul(slice).unwrap();

        assert!(null_byte_pos <= buf.len());

        // FIXME this fails if we try to read utf8 and it's not utf8
        let ret = match c_str.to_str() {
            Ok(st) => st.to_owned(),
            Err(e) => {
                return Err(TraceError::StringError(format!(
                    "C_str conv: {:?}; addr 0x{:x} nullbyte {}",
                    e, address, null_byte_pos
                )));
            }
        };

        Ok(ret)
    }

    #[inline]
    fn read_memory_to_vec(
        &mut self,
        pid: usize,
        address: usize,
        len: usize,
    ) -> Result<Vec<u8>, TraceError> {
        self.assert_memory_len(len)?;

        if address == 0 {
            return Err(TraceError::StringError("address = nullptr".to_string()));
        }

        // FIXME add limiter

        log::debug!(
            "Read memory (pid: {}), address: {}, len: {}",
            pid,
            address,
            len
        );

        let mut buf = vec![0u8; len];

        let ret = match nix::sys::uio::process_vm_readv(
            Pid::from_raw(pid as i32),
            &[nix::sys::uio::IoVec::from_mut_slice(&mut buf)],
            &[nix::sys::uio::RemoteIoVec { base: address, len }],
        ) {
            Ok(ret) => ret,
            Err(e) => {
                return Err(TraceError::StringError(format!(
                    "read_memory_to_vec error: {:?}, pid {}, address {} len {}",
                    e, pid, address, len
                )));
            }
        };

        if ret < buf.len() {
            buf.truncate(ret);
        }

        assert!(ret <= len);

        Ok(buf)
    }

    #[inline]
    fn get_pid(&self) -> usize {
        self.current_pid
    }
}