nucleus/security/
seccomp_trace.rs1use crate::error::{NucleusError, Result};
13use serde::{Deserialize, Serialize};
14use std::collections::BTreeMap;
15use std::io::{BufRead, BufReader, Write};
16use std::path::{Path, PathBuf};
17use std::sync::atomic::{AtomicBool, Ordering};
18use std::sync::Arc;
19use std::thread::JoinHandle;
20use tracing::{debug, info, warn};
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct TraceRecord {
25 pub syscall: i64,
27 pub name: Option<String>,
29 pub count: u64,
31}
32
33pub struct SeccompTraceReader {
35 pid: u32,
36 output_path: PathBuf,
37 stop: Arc<AtomicBool>,
38 handle: Option<JoinHandle<()>>,
39}
40
41impl SeccompTraceReader {
42 pub fn new(pid: u32, output_path: &Path) -> Self {
44 Self {
45 pid,
46 output_path: output_path.to_path_buf(),
47 stop: Arc::new(AtomicBool::new(false)),
48 handle: None,
49 }
50 }
51
52 pub fn start_recording(&mut self) -> Result<()> {
57 let pid = self.pid;
58 let output_path = self.output_path.clone();
59 let stop = self.stop.clone();
60
61 let handle = std::thread::spawn(move || {
62 if let Err(e) = record_loop(pid, &output_path, &stop) {
63 warn!("Seccomp trace reader error: {}", e);
64 }
65 });
66
67 self.handle = Some(handle);
68 info!("Seccomp trace reader started for PID {}", self.pid);
69 Ok(())
70 }
71
72 pub fn stop_and_flush(mut self) {
74 self.stop.store(true, Ordering::Release);
75 if let Some(handle) = self.handle.take() {
76 let _ = handle.join();
77 }
78 info!(
79 "Seccomp trace reader stopped, output at {:?}",
80 self.output_path
81 );
82 }
83}
84
85impl Drop for SeccompTraceReader {
86 fn drop(&mut self) {
87 self.stop.store(true, Ordering::Release);
88 if let Some(handle) = self.handle.take() {
89 let _ = handle.join();
90 }
91 }
92}
93
94fn record_loop(pid: u32, output_path: &Path, stop: &AtomicBool) -> Result<()> {
96 let mut syscalls: BTreeMap<i64, u64> = BTreeMap::new();
97
98 let kmsg_path = std::path::Path::new("/dev/kmsg");
100 if let Ok(meta) = std::fs::symlink_metadata(kmsg_path) {
101 if meta.file_type().is_symlink() {
102 warn!("/dev/kmsg is a symlink — refusing to open for seccomp tracing");
103 write_trace_file(output_path, &syscalls)?;
104 return Ok(());
105 }
106 }
107
108 let file = match std::fs::File::open(kmsg_path) {
110 Ok(f) => f,
111 Err(e) => {
112 warn!(
113 "Cannot open /dev/kmsg for seccomp tracing: {} \
114 (requires root or CAP_SYSLOG). Falling back to no-trace mode.",
115 e
116 );
117 write_trace_file(output_path, &syscalls)?;
119 return Ok(());
120 }
121 };
122
123 use std::os::unix::io::AsRawFd;
128 let fd = file.as_raw_fd();
129 unsafe {
130 let flags = libc::fcntl(fd, libc::F_GETFL);
131 if flags >= 0 {
132 libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK);
133 }
134 }
135
136 let reader = BufReader::new(file);
137 let pid_pattern = format!("pid={}", pid);
138
139 for line in reader.lines() {
140 if stop.load(Ordering::Acquire) {
141 break;
142 }
143
144 let line = match line {
145 Ok(l) => l,
146 Err(e) => {
147 if e.kind() == std::io::ErrorKind::WouldBlock {
148 let mut pfd = libc::pollfd {
150 fd,
151 events: libc::POLLIN,
152 revents: 0,
153 };
154 unsafe { libc::poll(&mut pfd, 1, 2000) };
155 continue;
156 }
157 debug!("kmsg read error: {}", e);
158 continue;
159 }
160 };
161
162 if line.contains("type=1326") && line.contains(&pid_pattern) {
165 if let Some(nr) = extract_syscall_nr(&line) {
166 *syscalls.entry(nr).or_insert(0) += 1;
167 }
168 }
169 }
170
171 write_trace_file(output_path, &syscalls)?;
172 info!("Seccomp trace: recorded {} unique syscalls", syscalls.len());
173 Ok(())
174}
175
176fn extract_syscall_nr(line: &str) -> Option<i64> {
178 line.split_whitespace()
180 .find(|s| s.starts_with("syscall="))
181 .and_then(|s| s.strip_prefix("syscall="))
182 .and_then(|s| s.parse().ok())
183}
184
185fn write_trace_file(path: &Path, syscalls: &BTreeMap<i64, u64>) -> Result<()> {
187 let mut file = std::fs::File::create(path).map_err(|e| {
188 NucleusError::ConfigError(format!("Failed to create trace file {:?}: {}", path, e))
189 })?;
190
191 for (&nr, &count) in syscalls {
192 let record = TraceRecord {
193 syscall: nr,
194 name: super::seccomp_generate::syscall_number_to_name(nr).map(String::from),
195 count,
196 };
197 let line =
198 serde_json::to_string(&record).unwrap_or_else(|e| format!("{{\"error\":\"{}\"}}", e));
199 writeln!(file, "{}", line).map_err(|e| {
200 NucleusError::ConfigError(format!("Failed to write trace record: {}", e))
201 })?;
202 }
203
204 Ok(())
205}
206
207#[cfg(test)]
208mod tests {
209 use super::*;
210
211 #[test]
212 fn test_extract_syscall_nr() {
213 let line = "6,1234,5678,-;audit: type=1326 audit(123:456): auid=0 uid=0 gid=0 ses=1 pid=42 comm=\"test\" exe=\"/bin/test\" sig=0 arch=c000003e syscall=257 compat=0 ip=0x7f action=0x7fff0000";
214 assert_eq!(extract_syscall_nr(line), Some(257));
215 }
216
217 #[test]
218 fn test_extract_syscall_nr_missing() {
219 assert_eq!(extract_syscall_nr("no syscall here"), None);
220 }
221
222 fn extract_fn_body<'a>(source: &'a str, fn_signature: &str) -> &'a str {
225 let fn_start = source
226 .find(fn_signature)
227 .unwrap_or_else(|| panic!("function '{}' not found in source", fn_signature));
228 let after = &source[fn_start..];
229 let open = after
230 .find('{')
231 .unwrap_or_else(|| panic!("no opening brace found for '{}'", fn_signature));
232 let mut depth = 0u32;
233 let mut end = open;
234 for (i, ch) in after[open..].char_indices() {
235 match ch {
236 '{' => depth += 1,
237 '}' => {
238 depth -= 1;
239 if depth == 0 {
240 end = open + i + 1;
241 break;
242 }
243 }
244 _ => {}
245 }
246 }
247 &after[..end]
248 }
249
250 #[test]
251 fn test_reader_uses_nonblocking_io() {
252 let source = include_str!("seccomp_trace.rs");
256 let fn_body = extract_fn_body(source, "fn record_loop");
257 assert!(
258 fn_body.contains("O_NONBLOCK"),
259 "record_loop must use O_NONBLOCK for non-blocking reads on /dev/kmsg"
260 );
261 assert!(
262 fn_body.contains("libc::poll"),
263 "record_loop must use poll() for timed waits on /dev/kmsg"
264 );
265 let setsockopt_lines: Vec<&str> = fn_body
267 .lines()
268 .filter(|l| !l.trim().starts_with("//"))
269 .filter(|l| l.contains("setsockopt"))
270 .collect();
271 assert!(
272 setsockopt_lines.is_empty(),
273 "record_loop must not call setsockopt on /dev/kmsg"
274 );
275 }
276
277 #[test]
278 fn test_trace_record_serialization() {
279 let record = TraceRecord {
280 syscall: 0,
281 name: Some("read".to_string()),
282 count: 42,
283 };
284 let json = serde_json::to_string(&record).unwrap();
285 assert!(json.contains("\"syscall\":0"));
286 assert!(json.contains("\"name\":\"read\""));
287 assert!(json.contains("\"count\":42"));
288 }
289}