syd 3.52.0

rock-solid application kernel
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//
// Syd: rock-solid application kernel
// src/hash.rs: Utilities for caching
//
// Copyright (c) 2024, 2025, 2026 Ali Polatel <alip@chesswob.org>
//
// SPDX-License-Identifier: GPL-3.0

// SAFETY: This module has been liberated from unsafe code!
#![forbid(unsafe_code)]

use std::sync::{Arc, Condvar, Mutex, RwLock};

use ahash::HashMapExt;
use libc::c_long;
use libseccomp::ScmpSyscall;
use nix::{errno::Errno, sys::socket::UnixAddr, unistd::Pid};
use serde::{ser::SerializeMap, Serializer};

use crate::{
    config::HASH_CACHE,
    confine::{ScmpNotifReq, SydArch},
    fd::SafeOwnedFd,
    hash::{hash_pipe, SydHashMap},
    sigset::SydSigSet,
};

/// Metadata on a blocking syscall invocation
#[derive(Debug)]
pub(crate) struct SysInterrupt {
    /// Syd handler thread ID
    pub(crate) handler: Pid,
    /// Thread group ID of sandbox process
    pub(crate) tgid: Pid,
    /// System call request
    pub(crate) request: ScmpNotifReq,
    /// proc_pid_status(5) file handle
    pub(crate) status: Option<SafeOwnedFd>,
    /// Used by syd_emu to signal syd_int to delete the entry and close the file.
    /// This is because the status file descriptor is not valid in syd_emu's fs space.
    pub(crate) delete: bool,
    /// Used by syd_mon to signal syd_int to signal stuck emulators manually,
    /// when not enough resources are available to spawn new emulator threads.
    /// This is because the status file descriptor is not valid in syd_mon's fs space.
    pub(crate) signal: bool,
    /// True if `SA_RESTART` is ignored
    /// (e.g. due to a socket timeout).
    pub(crate) ignore_restart: bool,
}

/// Map of metadata on blocking syscall invocations.
pub(crate) type BlockVec = Vec<SysInterrupt>;

/// Map of restarting signals by TGID.
pub(crate) type RestartMap = SydHashMap<Pid, SydSigSet>;

/// This is the data type used to handle syscall interrupts.
#[derive(Debug)]
pub(crate) struct SysInterruptMap {
    /// Map of blocking syscalls by request id.
    pub(crate) sys_block: Arc<(Mutex<BlockVec>, Condvar)>,
    /// Map of restarting signals by TGID.
    /// Used for SA_RESTART tracking.
    pub(crate) sig_restart: Arc<Mutex<RestartMap>>,
}

/// Syscall-agnostic error map.
pub(crate) type ErrorMap = SydHashMap<Pid, Option<Errno>>;

/// chdir(2) result map.
///
/// c_long is the system call number: chdir or fchdir.
pub(crate) type ChdirMap = SydHashMap<Pid, c_long>;

/// mmap(2) pid map.
///
/// c_long is the system call number: mmap or mmap2.
/// Arguments are from syscall entry.
pub(crate) type MmapMap = SydHashMap<Pid, (c_long, [u64; 6])>;

// [inode,(pid,path)] map of unix binds.
// Path is only used for UNIX domain sockets.
//
// SAFETY:
// 1. /proc/net/unix only gives inode information,
//    and does not include information on device id
//    or mount id so unfortunately we cannot check
//    for that here.
// 2. Pid is used for SO_PEERCRED getsockopt(2).
#[derive(Clone)]
pub(crate) struct UnixVal {
    // Thread group ID of the socket owner.
    pub(crate) pid: Pid,
    // bind(2) address of this socket, if filesystem-bound.
    pub(crate) addr: Option<UnixAddr>,
    // Peer address from connect(2), if connected.
    pub(crate) peer: Option<UnixAddr>,
    // Device ID and inode of recent send(2) destinations.
    // Used at recv(2) to match the receiver's VFS identity.
    pub(crate) dest: Vec<(u32, u32)>,
}

impl Default for UnixVal {
    fn default() -> Self {
        Self {
            pid: Pid::from_raw(0),
            addr: None,
            peer: None,
            dest: Vec::new(),
        }
    }
}

pub(crate) type UnixMap = Arc<RwLock<SydHashMap<u64, UnixVal>>>;

// [tid, tgid] map for ptrace(PTRACE_TRACEME) calling tids.
// This is used to prevent ptrace(2) detection efficiently.
pub(crate) type PtraceMap = Arc<RwLock<SydHashMap<Pid, Pid>>>;

// Results map for ptrace(2) hooks chdir, execve, sigaction and sigreturn.
#[derive(Debug)]
pub(crate) struct SysResultMap {
    // syscall-agnostic error map
    pub(crate) trace_error: Arc<Mutex<ErrorMap>>,
    // chdir(2) result map
    pub(crate) trace_chdir: Arc<Mutex<ChdirMap>>,
    // mmap(2) pid set.
    pub(crate) trace_mmap: Arc<Mutex<MmapMap>>,
}

// Maximum outstanding signal-delivery checksums per TID.
pub(crate) const SIG_NEST_MAX: usize = 128;

// Deep-nesting threshold for SROP detection.
pub(crate) const SIG_NEST_DEEP: usize = 2;

// Per-process sigreturn(2) trampoline IP.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct SigreturnTrampolineIP {
    pub(crate) lo: u64,
    pub(crate) hi: u64,
}

impl SigreturnTrampolineIP {
    // Distance from cookie base to trampoline syscall instruction.
    pub(crate) const DISTANCE: u64 = 16;

    #[expect(clippy::arithmetic_side_effects)]
    pub(crate) fn matches(self, ip: u64) -> bool {
        let lo_ok = ip >= self.lo && ip - self.lo <= Self::DISTANCE;
        let hi_ok = self.hi != self.lo && ip >= self.hi && ip - self.hi <= Self::DISTANCE;
        lo_ok || hi_ok
    }
}

#[derive(Clone, Debug)]
pub(crate) struct SighandleInfo {
    // Depth of outstanding signal deliveries.
    pub(crate) depth: u8,
    // Per-depth ring; Some marks an outstanding delivery.
    pub(crate) frames: [Option<()>; SIG_NEST_MAX],
    // True between sigreturn(2) sysenter and sysexit.
    pub(crate) in_sigreturn: bool,
    // True between PTRACE_SINGLESTEP at signal delivery and SIGTRAP.
    pub(crate) in_singlestep: bool,
    // sigreturn(2) trampoline IP, captured at first sigreturn(2).
    pub(crate) trampoline_ip: Option<SigreturnTrampolineIP>,
}

// Per-TID signal tracking for SROP mitigation.
pub(crate) type SighandleMap = SydHashMap<Pid, SighandleInfo>;

// Signal map, used by signal counting for SROP mitigation:
// If a TID is not in sig_handle_map at the entry of sigreturn(2),
// we terminate the process because the sigreturn(2) is artificial.
#[derive(Debug)]
pub(crate) struct SignalMap {
    // Set of TIDs that have received count signals for handled signals.
    pub(crate) sig_handle: Arc<Mutex<SighandleMap>>,
}

impl SysInterrupt {
    pub(crate) fn new(
        request: ScmpNotifReq,
        handler: Pid,
        tgid: Pid,
        ignore_restart: bool,
    ) -> Result<Self, Errno> {
        Ok(Self {
            handler,
            tgid,
            request,
            ignore_restart,
            status: None,
            delete: false,
            signal: false,
        })
    }

    // Marks the interrupt for deletion as needed.
    //
    // Returns true if drop should be handled by syd_int.
    pub(crate) fn delete(&mut self) -> bool {
        // interrupt.status is Some if syd_int thread has already opened
        // proc_pid_status(5). In this case we let it close the file
        // because the file descriptor is not valid in syd_emu's FS
        // space.
        if self.status.is_some() {
            self.delete = true;
            true // syd_int drops interrupt.
        } else {
            false // syd_emu drops interrupt.
        }
    }
}

impl serde::Serialize for SysInterrupt {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(Some(6))?;

        let data = &self.request.data;
        let syscall = ScmpSyscall::get_name_by_arch(data.syscall, data.arch)
            .unwrap_or_else(|_| format!("{}", i32::from(data.syscall)));
        let _ = map.serialize_entry("pid", &self.request.pid);
        let _ = map.serialize_entry("sys", &syscall);
        let _ = map.serialize_entry("arch", &SydArch::from(data.arch));
        let _ = map.serialize_entry("args", &data.args);
        let _ = map.serialize_entry("handler", &self.handler.as_raw());
        let _ = map.serialize_entry("ignore_restart", &self.ignore_restart);

        map.end()
    }
}

/// Create a new UnixMap.
pub(crate) fn unix_map_new() -> UnixMap {
    Arc::new(RwLock::new(SydHashMap::default()))
}

/// Create a new PtraceMap.
pub(crate) fn ptrace_map_new() -> PtraceMap {
    Arc::new(RwLock::new(SydHashMap::default()))
}

/// Create a new SysInterruptMap.
pub(crate) fn sys_interrupt_map_new() -> SysInterruptMap {
    SysInterruptMap {
        sys_block: Arc::new((Mutex::new(BlockVec::new()), Condvar::new())),
        sig_restart: Arc::new(Mutex::new(RestartMap::new())),
    }
}

/// Create a new SysResultMap.
pub(crate) fn sys_result_map_new() -> SysResultMap {
    SysResultMap {
        trace_error: Arc::new(Mutex::new(ErrorMap::new())),
        trace_chdir: Arc::new(Mutex::new(ChdirMap::new())),
        trace_mmap: Arc::new(Mutex::new(MmapMap::new())),
    }
}

/// Create a new SignalMap.
pub(crate) fn signal_map_new() -> SignalMap {
    SignalMap {
        sig_handle: Arc::new(Mutex::new(SighandleMap::new())),
    }
}

/// Cache for AF_ALG hash algorithm probing results.
pub(crate) struct HashCache {
    map: SydHashMap<String, Result<Vec<u8>, Errno>>,
}

impl HashCache {
    // Creates an empty hash cache (map not yet allocated).
    pub(crate) fn new() -> Self {
        Self {
            map: SydHashMap::new(),
        }
    }

    // Probes an algorithm lazily, returning the cached Result.
    // Ok(empty_digest) = supported, Err(errno) = unsupported.
    fn probe(&mut self, alg: &str) -> &Result<Vec<u8>, Errno> {
        if !self.map.contains_key(alg) {
            let result = hash_pipe(alg, None::<SafeOwnedFd>);
            self.map.insert(alg.to_string(), result);
        }
        &self.map[alg]
    }

    /// Returns `true` if the algorithm is supported by the running kernel.
    pub(crate) fn is_supported(alg: &str) -> bool {
        HASH_CACHE
            .lock()
            .unwrap_or_else(|err| err.into_inner())
            .probe(alg)
            .is_ok()
    }

    /// Validates a checksum against the cached algorithm metadata.
    pub(crate) fn is_valid_checksum(alg: &str, key: &[u8]) -> bool {
        match HASH_CACHE
            .lock()
            .unwrap_or_else(|err| err.into_inner())
            .probe(alg)
        {
            Ok(sum) => key.len() == sum.len() && key != sum.as_slice(),
            Err(_) => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_unix_map_new() {
        let map = unix_map_new();
        assert!(map.read().unwrap().is_empty());
    }

    #[test]
    fn test_ptrace_map_new() {
        let map = ptrace_map_new();
        assert!(map.read().unwrap().is_empty());
    }

    #[test]
    fn test_sys_interrupt_map_new() {
        let map = sys_interrupt_map_new();
        assert!(map.sys_block.0.lock().unwrap().is_empty());
        assert!(map.sig_restart.lock().unwrap().is_empty());
    }

    #[test]
    fn test_sys_result_map_new() {
        let map = sys_result_map_new();
        assert!(map.trace_error.lock().unwrap().is_empty());
        assert!(map.trace_chdir.lock().unwrap().is_empty());
        assert!(map.trace_mmap.lock().unwrap().is_empty());
    }

    #[test]
    fn test_signal_map_new() {
        let map = signal_map_new();
        assert!(map.sig_handle.lock().unwrap().is_empty());
    }

    #[test]
    fn test_hash_cache_1() {
        let cache = HashCache::new();
        assert!(cache.map.is_empty());
    }

    #[test]
    fn test_hash_cache_2() {
        // sha256 should be available on most kernels, but skip gracefully.
        if HashCache::is_supported("sha256") {
            assert!(HashCache::is_supported("sha256"));
        } else {
            eprintln!("sha256 not supported by kernel, skipping.");
        }
    }

    #[test]
    fn test_hash_cache_3() {
        assert!(!HashCache::is_supported("Pink Floyd"));
    }

    #[test]
    fn test_hash_cache_4() {
        // Unsupported algorithm: always reject.
        assert!(!HashCache::is_valid_checksum("Pink Floyd", &[0u8; 32]));

        if !HashCache::is_supported("sha256") {
            eprintln!("sha256 not available, skipping checksum tests.");
            return;
        }

        // Wrong length: reject.
        assert!(!HashCache::is_valid_checksum("sha256", &[0u8; 16]));

        // Empty-digest hardening: reject checksum equal to hash of empty input.
        let empty = HASH_CACHE
            .lock()
            .unwrap()
            .probe("sha256")
            .as_ref()
            .unwrap()
            .clone();
        assert!(!HashCache::is_valid_checksum("sha256", &empty));

        // Valid checksum (right length, not empty-digest): accept.
        let mut valid = vec![0xffu8; 32];
        valid[0] ^= 0x01;
        assert!(HashCache::is_valid_checksum("sha256", &valid));
    }

    #[test]
    fn test_hash_cache_5() {
        let first = {
            HASH_CACHE
                .lock()
                .unwrap_or_else(|err| err.into_inner())
                .probe("sha256")
                .clone()
        };
        let second = {
            HASH_CACHE
                .lock()
                .unwrap_or_else(|err| err.into_inner())
                .probe("sha256")
                .clone()
        };
        match (&first, &second) {
            (Ok(a), Ok(b)) => assert_eq!(a, b),
            (Err(a), Err(b)) => assert_eq!(a, b),
            _ => panic!("probe returned different Result variants"),
        }
    }
}