mac-process 0.1.0

User-mode library for querying and verifying integrity of MacOS processes
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! A high-level process monitor for a process.
pub mod audit_token;
mod dead_processes;
pub mod region_iterator;
pub mod regions;
pub mod vnode;

use crate::{
    helpers::hashes,
    libproc::region_info::{RegionInfo, RegionWithPathInfo},
    monitor::{
        regions::{Region, RegionWithPath},
        vnode::VnodeStat,
    },
};
use audit_token::AuditToken;
use crossbeam::channel::Receiver;
use dashmap::DashMap;
use dead_processes::{DeadProcTracker, WatchItem};
use once_cell::sync::OnceCell;
use region_iterator::{RegionIterator, RegionWithPathIterator};
use rustix::{
    fs::{Dev, Mode, OFlags},
    process::Pid,
};
use std::{
    ffi::{OsStr, OsString},
    fmt::Debug,
    fs::File,
    io::{self},
    os::fd::OwnedFd,
    path::{Path, PathBuf},
    sync::{
        Arc, Weak,
        atomic::{AtomicBool, AtomicU64, Ordering},
    },
};

/// Primary structure for querying live processes.
pub struct ProcessMonitor {
    // A mapping of PIDs to process information.
    mapping: Arc<DashMap<Pid, Weak<Process>>>,
    // kqueue-based dead process monitor.
    dead_monitor: DeadProcTracker,
    // A stale count that informs cleanup operation. This metric is advisory-only and may not be correct.
    collector_stale_count: Arc<AtomicU64>,
}

impl Debug for ProcessMonitor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ProcessIntegrity")
            .field("mapping", &self.mapping)
            .finish()
    }
}

impl ProcessMonitor {
    /// Creates a new integrity monitor. It should be shared across an entire application to prevent excessive memory consumption.
    pub fn new() -> Result<Self, io::Error> {
        let mapping = Arc::new(DashMap::new());
        Ok(Self {
            mapping,
            dead_monitor: DeadProcTracker::build()?,
            collector_stale_count: Arc::new(AtomicU64::new(0)),
        })
    }

    /// Gets a process by PID, and returns an error if the process is not found or is dead.
    pub fn get(&self, pid: Pid) -> Result<Arc<Process>, io::Error> {
        self.auto_cleanup();
        Process::gather(self, pid)
    }

    /// Removes a process entry. The weakref in the value must match to prevent race conditions where new unrelated entries may get removed.
    fn clear_one_internal(&self, pid: Pid, weakref: &Weak<Process>) -> bool {
        self.mapping
            .remove_if(&pid, |_, weakref_other| {
                Weak::ptr_eq(weakref, weakref_other)
            })
            .is_some()
    }

    /// Removes entries that have no strong references to free memory.
    fn clear_stale(&self) {
        self.mapping
            .retain(|_, weakref| Weak::strong_count(weakref) > 0)
    }

    /// Performs automatic cleanups.
    fn auto_cleanup(&self) {
        if self.collector_stale_count.load(Ordering::Relaxed) > 200 {
            self.collector_stale_count.store(0, Ordering::Relaxed);
            self.clear_stale();
        }
    }
}

/// Structure for tracking a live process.
pub struct Process {
    pid: Pid,
    audit_token: AuditToken,
    path: OnceCell<PathBuf>,
    name: OnceCell<OsString>,
    /// The identity of the main executable is a tuple of the device and inode.
    exe_identity: OnceCell<(Dev, u64)>,
    /// The MD5 hash of the main executable.
    md5_exe: OnceCell<[u8; 16]>,
    /// The SHA256 hash of the main executable.
    sha256_exe: OnceCell<[u8; 32]>,
    /// Alive status as reported by kqueue.
    kqueue_alive: Arc<AtomicBool>,
    /// Channel to wait until process is dead.
    kqueue_notify_dead: Receiver<()>,
    /// If set to false, prevents incrementing the stale count.
    collector_mark_stale_on_drop: AtomicBool,
    /// Once dropped, the stale count increments up to a certain point, which informs the automatic cleanup operation.
    collector_stale_count: Arc<AtomicU64>,
}

impl Debug for Process {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Process")
            .field("pid", &self.pid)
            .field("audit_token", &self.audit_token)
            .field("exe_identity", &self.exe_identity)
            .field("path", &self.path)
            .field("name", &self.name)
            .finish()
    }
}

impl PartialEq for Process {
    fn eq(&self, other: &Self) -> bool {
        self.audit_token == other.audit_token
    }
}

impl Process {
    fn gather(collector: &ProcessMonitor, pid: Pid) -> Result<Arc<Self>, io::Error> {
        // When the process is uncached and not found, an error must be returned.
        // When the process is cached and is dead, the item should be expunged from cache.
        loop {
            let mut _new_proc_ref = None;
            let process = collector
                .mapping
                .entry(pid)
                .or_try_insert_with(|| -> Result<_, io::Error> {
                    let audit_token = AuditToken::from_pid(pid).map_err(|_| {
                        io::Error::new(io::ErrorKind::NotFound, "process is not alive")
                    })?;
                    let kqueue_alive = Arc::new(AtomicBool::new(true));
                    let (kqueue_dead_tx, kqueue_dead_rx) = crossbeam::channel::bounded(1);
                    let item = WatchItem {
                        pid,
                        live_flag: kqueue_alive.clone(),
                        notify: kqueue_dead_tx,
                    };
                    collector.dead_monitor.send_item(item)?;
                    let proc_ref = Arc::new(Self {
                        pid,
                        audit_token,
                        md5_exe: OnceCell::new(),
                        sha256_exe: OnceCell::new(),
                        path: OnceCell::new(),
                        name: OnceCell::new(),
                        exe_identity: OnceCell::new(),
                        kqueue_notify_dead: kqueue_dead_rx,
                        kqueue_alive,
                        collector_mark_stale_on_drop: AtomicBool::new(true),
                        collector_stale_count: collector.collector_stale_count.clone(),
                    });
                    let weak_proc_ref = Arc::downgrade(&proc_ref);
                    _new_proc_ref = Some(proc_ref);
                    Ok(weak_proc_ref)
                })?
                .clone();
            let process = match process.upgrade() {
                None => {
                    collector.clear_one_internal(pid, &process);
                    continue;
                }
                Some(p) if !p.is_alive() => {
                    // Since the entry is removed, we do not need to increment the count to avoid invoking auto_cleanup too much.
                    if collector.clear_one_internal(pid, &process) {
                        p.collector_mark_stale_on_drop
                            .store(false, Ordering::Relaxed);
                    };
                    continue;
                }
                Some(p) => p,
            };
            break Ok(process);
        }
    }

    /// Checks if the process is alive.
    pub fn is_alive(&self) -> bool {
        // Paranoid check in case pid_version loops back.
        if !self.kqueue_alive.load(Ordering::Acquire) {
            return false;
        }
        let initial_audit_token = self.audit_token();
        let current_audit_token = match AuditToken::from_pid(self.pid) {
            Ok(audit_token) => audit_token,
            Err(_) => return false,
        };
        current_audit_token.pid() == initial_audit_token.pid()
            && current_audit_token.pidversion() == initial_audit_token.pidversion()
    }

    /// Waits until process is dead.
    pub fn wait_until_dead(&self) -> Result<(), io::Error> {
        if !self.is_alive() {
            return Ok(());
        }
        self.kqueue_notify_dead.recv().map_err(io::Error::other)
    }

    /// Evaluates a cached value, and returns an error if the process is not alive.
    pub fn evaluate_cached<'a, T>(
        &'a self,
        location: &'a OnceCell<T>,
        func: impl FnOnce() -> Result<T, io::Error>,
    ) -> Result<&'a T, io::Error> {
        if !self.is_alive() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                "process is not alive",
            ));
        }
        location.get_or_try_init(move || {
            if !self.is_alive() {
                return Err(io::Error::new(
                    io::ErrorKind::NotFound,
                    "process is not alive",
                ));
            }
            func()
        })
    }

    /// Gets the path of the process. This property is cached.
    pub fn path(&self) -> Result<&Path, io::Error> {
        self.evaluate_cached(&self.path, || {
            let path = crate::libproc::proc_pid::pidpath_audittoken(*self.audit_token.raw_token())?;
            Ok(path)
        })
        .map(|path| path.as_path())
    }

    /// Gets the name of the process. This property is cached.
    pub fn name(&self) -> Result<&OsStr, io::Error> {
        self.evaluate_cached(&self.name, || {
            let name = crate::libproc::proc_pid::name(self.pid.as_raw_pid())?;
            Ok(name)
        })
        .map(|name| name.as_os_str())
    }

    /// Gets the PID of the process.
    pub fn pid(&self) -> Pid {
        self.pid
    }

    /// Gets the audit token of the process.
    pub fn audit_token(&self) -> &AuditToken {
        &self.audit_token
    }

    /// Obtains the information for a process region at a given offset.
    pub fn region_at(&self, offset: u64) -> Result<Region, io::Error> {
        let region_info =
            crate::libproc::proc_pid::pidinfo::<RegionInfo>(self.pid.as_raw_pid(), offset)?;
        // Sanity check to prevent returning a region info for a dead process or another process.
        if !self.is_alive() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                "process is not alive",
            ));
        }
        Ok(Region::from_raw(region_info))
    }

    /// Creates an iterator that yields information about virtual memory regions in a process.
    pub fn region_iterator<'a>(&'a self) -> RegionIterator<'a> {
        RegionIterator::new(self)
    }

    /// Obtains the information for a process path region at a given offset.
    pub fn region_with_path_at(&self, offset: u64) -> Result<RegionWithPath, io::Error> {
        let region_info =
            crate::libproc::proc_pid::pidinfo::<RegionWithPathInfo>(self.pid.as_raw_pid(), offset)?;
        // Sanity check to prevent returning a region info for a dead process or another process.
        if !self.is_alive() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                "process is not alive",
            ));
        }
        Ok(RegionWithPath::from_raw(region_info))
    }

    /// Creates an iterator that yields information about virtual memory regions with path information in a process.
    pub fn region_with_path_iterator<'a>(&'a self) -> RegionWithPathIterator<'a> {
        RegionWithPathIterator::new(self)
    }

    /// Retrieves the first text region with path information. This usually contains information about the main executable
    pub fn exe_region(&self) -> Result<RegionWithPath, io::Error> {
        // Approach is based on lsof: https://github.com/lsof-org/lsof/blob/6379888cc1924bf97b2cfdbc1cee38bb0aa45f5d/lib/dialects/darwin/dproc.c#L638
        let mut iterator = self.region_with_path_iterator();
        for _ in 0..10000 {
            let region = iterator.next().ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::NotFound,
                    "no executable region with path found",
                )
            })??;
            if !region.vnode().path().as_os_str().is_empty() {
                return Ok(region);
            }
        }
        Err(io::Error::new(
            io::ErrorKind::NotFound,
            "no executable region with path found",
        ))
    }

    /// Retrieves the metadata of the main executable of the process.
    pub fn exe_stats(&self) -> Result<VnodeStat, io::Error> {
        self.exe_region()
            .map(|region| region.vnode().vnode().stat())
    }

    /// Retrieves the identity of the main executable. This property is cached.
    pub fn exe_identity(&self) -> Result<(Dev, u64), io::Error> {
        self.evaluate_cached(&self.exe_identity, || {
            let stats = self.exe_stats()?;
            let dev = stats.dev();
            let ino = stats.ino();
            Ok((dev, ino))
        })
        .cloned()
    }

    /// Opens the main executable of the process as a file by using volfs (/.vol) as a [`File`].
    /// Note that behavior may be unpredictable if the volume directory is shadowed or locked
    pub fn open_exe(&self, flags: OFlags) -> Result<File, io::Error> {
        let owned_fd = self.open_exe_fd(flags)?;
        Ok(File::from(owned_fd))
    }

    /// Opens the main executable of the process as a file by using volfs (/.vol) as a [`OwnedFd`].
    /// Note that behavior may be unpredictable if the volume directory is shadowed or locked
    pub fn open_exe_fd(&self, flags: OFlags) -> Result<OwnedFd, io::Error> {
        let (dev, ino) = self.exe_identity()?;
        let stable_path = Path::new("/.vol")
            .join(dev.to_string())
            .join(ino.to_string());
        let owned_fd = rustix::fs::open(&stable_path, flags, Mode::empty())?;
        Ok(owned_fd)
    }

    /// Computes the MD5 hash of the main executable. This property is cached.
    pub fn md5_exe(&self) -> Result<[u8; 16], io::Error> {
        self.evaluate_cached(&self.md5_exe, || {
            let file = self.open_exe(OFlags::empty())?;
            hashes::compute_md5(file)
        })
        .copied()
    }

    /// Computes the SHA256 hash of the main executable. This property is cached.
    pub fn sha256_exe(&self) -> Result<[u8; 32], io::Error> {
        self.evaluate_cached(&self.sha256_exe, || {
            let file = self.open_exe(OFlags::empty())?;
            hashes::compute_sha256(file)
        })
        .copied()
    }
}

impl Drop for Process {
    fn drop(&mut self) {
        if self.collector_mark_stale_on_drop.load(Ordering::Relaxed) {
            self.collector_stale_count
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        }
    }
}

#[cfg(test)]
use std::process::Child;

#[cfg(test)]
fn spawn_example_process() -> Child {
    use std::process::Command;
    use std::process::Stdio;
    Command::new("/bin/bash")
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .stdin(Stdio::piped())
        .spawn()
        .expect("Failed to spawn process")
}

#[cfg(test)]
mod tests {
    use std::fs::File;

    use crate::{ProcessMonitor, helpers::hashes};
    use rustix::{fs::OFlags, process::Pid};

    use super::spawn_example_process;

    #[test]
    fn test_process_liveness_and_path() {
        let mut child = spawn_example_process();
        let child_pid = child.id();
        let integrity = ProcessMonitor::new().expect("failed to create integrity object");
        let process = integrity
            .get(Pid::from_raw(child_pid as i32).expect("failed to convert pid"))
            .expect("failed to get process");
        let path = process.path().expect("failed to get process path");
        println!("Process path: {}", path.display());
        child.kill().expect("failed to kill process");
        let _ = child.wait_with_output();
        assert!(!process.is_alive(), "process should be dead");
    }

    #[test]
    fn test_process_kqueue_wait() {
        let mut child = spawn_example_process();
        let child_pid = child.id();
        let integrity = ProcessMonitor::new().expect("failed to create integrity object");
        let process = integrity
            .get(Pid::from_raw(child_pid as i32).expect("failed to convert pid"))
            .expect("failed to get process");
        child.kill().expect("failed to kill process");
        process
            .wait_until_dead()
            .expect("failed to wait for process");
        let _ = child.wait_with_output();
        assert!(!process.is_alive(), "process should be dead");
    }

    #[test]
    fn test_audit_token() {
        let mut child = spawn_example_process();
        let child_pid = child.id();
        let integrity = ProcessMonitor::new().expect("failed to create integrity object");
        let process = integrity
            .get(Pid::from_raw(child_pid as i32).expect("failed to convert pid"))
            .expect("failed to get process");
        let audit_token = process.audit_token();
        println!("Audit token: {:?}", audit_token);
        assert!(
            audit_token.pid() == Pid::from_raw(child_pid as i32).expect("failed to convert pid")
        );
        child.kill().expect("failed to kill process");
        let _ = child.wait_with_output();
    }

    #[test]
    fn test_open_exe() {
        let mut child = spawn_example_process();
        let child_pid = child.id();
        let integrity = ProcessMonitor::new().expect("failed to create integrity object");
        let process = integrity
            .get(Pid::from_raw(child_pid as i32).expect("failed to convert pid"))
            .expect("failed to get process");
        let _exe = process
            .open_exe(OFlags::empty())
            .expect("failed to open exe");
        child.kill().expect("failed to kill process");
        let _ = child.wait_with_output();
    }

    #[test]
    fn test_hashes() {
        let mut child = spawn_example_process();
        let child_pid = child.id();
        let integrity = ProcessMonitor::new().expect("failed to create integrity object");
        let process = integrity
            .get(Pid::from_raw(child_pid as i32).expect("failed to convert pid"))
            .expect("failed to get process");
        let md5 = process.md5_exe().expect("failed to get md5 hash");
        let sha256 = process.sha256_exe().expect("failed to get sha256 hash");
        println!("MD5 hash: {:?}", md5);
        println!("SHA256 hash: {:?}", sha256);
        child.kill().expect("failed to kill process");
        let _ = child.wait_with_output();
        // Compare with the original /bin/bash
        let file = File::open("/bin/bash").expect("failed to open /bin/bash");
        let md5_original = hashes::compute_md5(file).expect("failed to compute md5 hash");
        let file = File::open("/bin/bash").expect("failed to open /bin/bash");
        let sha256_original = hashes::compute_sha256(file).expect("failed to compute sha256 hash");
        println!("MD5 hash (original): {:?}", md5_original);
        println!("SHA256 hash (original): {:?}", sha256_original);
        assert!(md5 == md5_original);
        assert!(sha256 == sha256_original);
    }
}