-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmod.rs
More file actions
103 lines (88 loc) · 2.52 KB
/
mod.rs
File metadata and controls
103 lines (88 loc) · 2.52 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
mod execve;
mod getdents;
mod open;
mod stat;
use std::{
borrow::Cow,
ffi::{OsStr, c_int},
io,
os::unix::ffi::OsStrExt,
path::{Path, PathBuf},
};
use fspy_seccomp_unotify::{
impl_handler,
supervisor::handler::arg::{CStrPtr, Caller, Fd},
};
use fspy_shared::ipc::{AccessMode, PathAccess};
use crate::arena::PathAccessArena;
const PATH_MAX: usize = libc::PATH_MAX as usize;
#[derive(Debug)]
pub struct SyscallHandler {
arena: PathAccessArena,
path_read_buf: [u8; PATH_MAX],
}
impl Default for SyscallHandler {
fn default() -> Self {
Self { arena: PathAccessArena::default(), path_read_buf: [0; PATH_MAX] }
}
}
impl SyscallHandler {
pub fn into_arena(self) -> PathAccessArena {
self.arena
}
fn handle_open(
&mut self,
caller: Caller,
dir_fd: Fd,
path_ptr: CStrPtr,
flags: c_int,
) -> io::Result<()> {
let Some(path_len) = path_ptr.read(caller, &mut self.path_read_buf)? else {
// Ignore paths that are too long to fit in PATH_MAX
return Ok(());
};
let mut path = Cow::Borrowed(Path::new(OsStr::from_bytes(&self.path_read_buf[..path_len])));
if !path.is_absolute() {
let mut resolved_path = PathBuf::from(dir_fd.get_path(caller)?);
if !nix::NixPath::is_empty(path.as_ref()) {
resolved_path.push(&path);
}
path = Cow::Owned(resolved_path);
}
self.arena.add(PathAccess {
mode: match flags & libc::O_ACCMODE {
libc::O_RDWR => AccessMode::READ | AccessMode::WRITE,
libc::O_WRONLY => AccessMode::WRITE,
_ => AccessMode::READ,
},
path: path.as_os_str().into(),
});
Ok(())
}
fn handle_open_dir(&mut self, caller: Caller, fd: Fd) -> io::Result<()> {
let path = fd.get_path(caller)?;
self.arena.add(PathAccess {
mode: AccessMode::READ_DIR,
path: OsStr::from_bytes(path.as_bytes()).into(),
});
Ok(())
}
}
impl_handler!(
SyscallHandler:
#[cfg(target_arch = "x86_64")] open,
openat,
openat2,
#[cfg(target_arch = "x86_64")] getdents,
getdents64,
#[cfg(target_arch = "x86_64")] stat,
#[cfg(target_arch = "x86_64")] lstat,
#[cfg(target_arch = "x86_64")] newfstatat,
#[cfg(target_arch = "aarch64")] fstatat,
statx,
#[cfg(target_arch = "x86_64")] access,
faccessat,
faccessat2,
execve,
execveat,
);