syd 3.56.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
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
// Syd: rock-solid application kernel
// src/ioctl.rs: ioctl(2) request decoder
//
// Copyright (c) 2025, 2026 Ali Polatel <alip@chesswob.org>
//
// SPDX-License-Identifier: GPL-3.0

use std::fmt;

use libseccomp::ScmpArch;
use nix::errno::Errno;
use serde::{Serialize, Serializer};

use crate::confine::SCMP_ARCH;

/// This type represents an ioctl(2) request.
pub type Ioctl = u32;

/// This enum represents an ioctl(2) name or value.
pub enum IoctlName {
    /// Request name
    Name(String),
    /// Request value
    Val(Ioctl),
}

impl fmt::Display for IoctlName {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Name(ref s) => write!(f, "{s}"),
            Self::Val(v) => write!(f, "{v:#x}"),
        }
    }
}

impl Serialize for IoctlName {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Name(ref s) => serializer.serialize_str(s),
            Self::Val(v) => serializer.serialize_u32(*v),
        }
    }
}

/// This type represents an ioctl(2) list.
pub type IoctlList = &'static [(&'static str, Ioctl, bool)];

/// Zero size field of an ioctl(2) value.
pub fn ioctl_strip_size(value: Ioctl, arch: ScmpArch) -> Ioctl {
    let size_mask: Ioctl = match arch {
        ScmpArch::Ppc
        | ScmpArch::Ppc64
        | ScmpArch::Ppc64Le
        | ScmpArch::Mips
        | ScmpArch::Mipsel
        | ScmpArch::Mips64
        | ScmpArch::Mips64N32
        | ScmpArch::Mipsel64
        | ScmpArch::Mipsel64N32 => 0x1FFF << 16, // 13 size bits
        _ => 0x3FFF << 16, // 14 size bits
    };

    value & !size_mask
}

/// Return true if the ioctl value is a variable-size sentinel.
pub fn ioctl_is_varsize(value: Ioctl, arch: ScmpArch) -> bool {
    for &(a, table) in ARCH_TABLES {
        if a == arch {
            return table
                .iter()
                .any(|&(_, v, vs)| vs && Ioctl::from(v) == value);
        }
    }
    false
}
// Include auto-generated ioctl(2) requests.
include!("ioctl/ioctls_aarch64.rs");
include!("ioctl/ioctls_arm.rs");
include!("ioctl/ioctls_loongarch64.rs");
include!("ioctl/ioctls_m68k.rs");
include!("ioctl/ioctls_mips.rs");
include!("ioctl/ioctls_mips64.rs");
include!("ioctl/ioctls_mips64n32.rs");
include!("ioctl/ioctls_mipsel.rs");
include!("ioctl/ioctls_mipsel64.rs");
include!("ioctl/ioctls_mipsel64n32.rs");
include!("ioctl/ioctls_ppc.rs");
include!("ioctl/ioctls_ppc64.rs");
include!("ioctl/ioctls_ppc64le.rs");
include!("ioctl/ioctls_riscv64.rs");
include!("ioctl/ioctls_s390.rs");
include!("ioctl/ioctls_s390x.rs");
include!("ioctl/ioctls_x32.rs");
include!("ioctl/ioctls_x86.rs");
include!("ioctl/ioctls_x8664.rs");

const ARCH_TABLES: &[(ScmpArch, IoctlList)] = &[
    (ScmpArch::Aarch64, IOCTL_ARCH_AARCH64),
    (ScmpArch::Arm, IOCTL_ARCH_ARM),
    (ScmpArch::Loongarch64, IOCTL_ARCH_LOONGARCH64),
    (ScmpArch::M68k, IOCTL_ARCH_M68K),
    (ScmpArch::Mips, IOCTL_ARCH_MIPS),
    (ScmpArch::Mips64, IOCTL_ARCH_MIPS64),
    (ScmpArch::Mips64N32, IOCTL_ARCH_MIPS64N32),
    (ScmpArch::Mipsel, IOCTL_ARCH_MIPSEL),
    (ScmpArch::Mipsel64, IOCTL_ARCH_MIPSEL64),
    (ScmpArch::Mipsel64N32, IOCTL_ARCH_MIPSEL64N32),
    (ScmpArch::Ppc, IOCTL_ARCH_PPC),
    (ScmpArch::Ppc64, IOCTL_ARCH_PPC64),
    (ScmpArch::Ppc64Le, IOCTL_ARCH_PPC64LE),
    (ScmpArch::Riscv64, IOCTL_ARCH_RISCV64),
    (ScmpArch::S390, IOCTL_ARCH_S390),
    (ScmpArch::S390X, IOCTL_ARCH_S390X),
    (ScmpArch::X32, IOCTL_ARCH_X32),
    (ScmpArch::X86, IOCTL_ARCH_X86),
    (ScmpArch::X8664, IOCTL_ARCH_X8664),
];

/// This structure represents ioctl maps.
///
/// It offers an API to query ioctls by name and by value.
/// This implementation uses zero-allocation static lookups.
pub struct IoctlMap {
    target: Option<ScmpArch>,
    native: bool,
}

impl IoctlMap {
    /// Initialize a new IoctlMap.
    ///
    /// The `target` and `native` parameters control which architectures are considered
    /// during lookups, acting as a filter.
    pub fn new(target: Option<ScmpArch>, native: bool) -> Self {
        Self { target, native }
    }

    fn should_check(&self, arch: ScmpArch) -> bool {
        if let Some(target_arch) = self.target {
            if arch != target_arch {
                return false;
            }
        } else if self.native && !SCMP_ARCH.contains(&arch) {
            return false;
        }
        true
    }

    /// Return symbol names for the given Ioctl.
    /// Performs a linear scan of the static table (O(N)).
    pub fn get_names(
        &self,
        value: Ioctl,
        arch: ScmpArch,
    ) -> Result<Option<Vec<&'static str>>, Errno> {
        if !self.should_check(arch) {
            return Ok(None);
        }

        for &(a, table) in ARCH_TABLES {
            if a == arch {
                // Linear scan to find all matches
                let mut names = Vec::new();
                for &(n, v, _) in table {
                    if Ioctl::from(v) == value {
                        if names.len() == names.capacity() {
                            names.try_reserve(1).or(Err(Errno::ENOMEM))?;
                        }
                        names.push(n);
                    }
                }
                if names.is_empty() {
                    return Ok(None);
                }
                return Ok(Some(names));
            }
        }
        Ok(None)
    }

    /// Return IoctlName list for the given Ioctl.
    /// Uses fallible allocation.
    pub fn get_log(&self, value: Ioctl, arch: ScmpArch) -> Result<Option<Vec<IoctlName>>, Errno> {
        if !self.should_check(arch) {
            return Ok(None);
        }

        for &(a, table) in ARCH_TABLES {
            if a == arch {
                let mut names = Vec::new();
                for &(n, v, _) in table {
                    if Ioctl::from(v) == value {
                        if names.len() == names.capacity() {
                            names.try_reserve(1).or(Err(Errno::ENOMEM))?;
                        }
                        let mut s = String::new();
                        s.try_reserve(n.len()).or(Err(Errno::ENOMEM))?;
                        s.push_str(n);
                        names.push(IoctlName::Name(s));
                    }
                }
                if names.is_empty() {
                    return Ok(None);
                }
                return Ok(Some(names));
            }
        }
        Ok(None)
    }

    /// Return Ioctl request number for the given symbol name.
    /// Performs a binary search on the static table (O(log N)).
    pub fn get_value(&self, name: &str, arch: ScmpArch) -> Option<Ioctl> {
        if !self.should_check(arch) {
            return None;
        }

        for &(a, table) in ARCH_TABLES {
            if a == arch {
                // Table is sorted by name. Use binary search.
                return table
                    .binary_search_by_key(&name, |&(n, _, _)| n)
                    .ok()
                    .map(|idx| Ioctl::from(table[idx].1));
            }
        }
        None
    }

    /// Return an iterator over all Ioctls for the given architecture.
    pub fn iter(&self, arch: ScmpArch) -> Option<impl Iterator<Item = (&'static str, Ioctl)>> {
        if !self.should_check(arch) {
            return None;
        }

        for &(a, table) in ARCH_TABLES {
            if a == arch {
                return Some(table.iter().map(|&(name, val, _)| (name, Ioctl::from(val))));
            }
        }
        None
    }
}

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

    #[test]
    fn test_ioctlmap_1() {
        let map = IoctlMap::new(None, false);
        assert!(map.target.is_none());
        assert!(!map.native);
    }

    #[test]
    fn test_ioctlmap_2() {
        let map = IoctlMap::new(Some(ScmpArch::X8664), true);
        assert_eq!(map.target, Some(ScmpArch::X8664));
        assert!(map.native);
    }

    #[test]
    fn test_ioctlmap_3() {
        let map = IoctlMap::new(Some(ScmpArch::X8664), false);
        let result = map.get_names(0x5413, ScmpArch::X8664).unwrap();
        if let Some(names) = result {
            assert!(names.contains(&"TIOCGWINSZ"));
        }
    }

    #[test]
    fn test_ioctlmap_4() {
        let map = IoctlMap::new(Some(ScmpArch::X8664), false);
        let result = map.get_names(0xDEADBEEF, ScmpArch::X8664).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_ioctlmap_5() {
        let map = IoctlMap::new(Some(ScmpArch::Arm), false);
        let result = map.get_names(0x5413, ScmpArch::X8664).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_ioctlmap_6() {
        let map = IoctlMap::new(Some(ScmpArch::X8664), false);
        let result = map.get_value("TIOCGWINSZ", ScmpArch::X8664);
        assert_eq!(result, Some(0x5413));
    }

    #[test]
    fn test_ioctlmap_7() {
        let map = IoctlMap::new(Some(ScmpArch::X8664), false);
        let result = map.get_value("NONEXISTENT_IOCTL", ScmpArch::X8664);
        assert!(result.is_none());
    }

    #[test]
    fn test_ioctlmap_8() {
        let map = IoctlMap::new(Some(ScmpArch::Arm), false);
        let result = map.get_value("TIOCGWINSZ", ScmpArch::X8664);
        assert!(result.is_none());
    }

    #[test]
    fn test_ioctlmap_9() {
        let map = IoctlMap::new(Some(ScmpArch::X8664), false);
        let result = map.get_log(0x5413, ScmpArch::X8664).unwrap();
        if let Some(names) = result {
            assert!(!names.is_empty());
            let display = format!("{}", names[0]);
            assert!(display.contains("TIOCGWINSZ"));
        }
    }

    #[test]
    fn test_ioctlmap_10() {
        let map = IoctlMap::new(Some(ScmpArch::X8664), false);
        let result = map.get_log(0xDEADBEEF, ScmpArch::X8664).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_ioctlmap_11() {
        let map = IoctlMap::new(Some(ScmpArch::X8664), false);
        let iter = map.iter(ScmpArch::X8664);
        assert!(iter.is_some());
        let count = iter.unwrap().count();
        assert!(count > 0);
    }

    #[test]
    fn test_ioctlmap_12() {
        let map = IoctlMap::new(Some(ScmpArch::Arm), false);
        let iter = map.iter(ScmpArch::X8664);
        assert!(iter.is_none());
    }

    #[test]
    fn test_ioctlname_1() {
        let name = IoctlName::Name("TIOCGWINSZ".into());
        assert_eq!(format!("{name}"), "TIOCGWINSZ");
    }

    #[test]
    fn test_ioctlname_2() {
        let val = IoctlName::Val(0x5413);
        assert_eq!(format!("{val}"), "0x5413");
    }

    #[test]
    fn test_ioctlname_3() {
        let name = IoctlName::Name("TIOCGWINSZ".into());
        let json = serde_json::to_string(&name).unwrap();
        assert_eq!(json, "\"TIOCGWINSZ\"");
    }

    #[test]
    fn test_ioctlname_4() {
        let val = IoctlName::Val(0x5413);
        let json = serde_json::to_string(&val).unwrap();
        assert_eq!(json, "21523");
    }

    #[test]
    fn test_should_check_1() {
        let map = IoctlMap::new(None, false);
        assert!(map.should_check(ScmpArch::X8664));
        assert!(map.should_check(ScmpArch::Arm));
    }

    #[test]
    fn test_should_check_2() {
        let map = IoctlMap::new(Some(ScmpArch::X8664), false);
        assert!(map.should_check(ScmpArch::X8664));
        assert!(!map.should_check(ScmpArch::Arm));
    }

    #[test]
    fn test_ioctl_strip_size_1() {
        let stripped = ioctl_strip_size(0x40806b00, ScmpArch::X8664);
        assert_eq!(stripped, 0x40006b00);
    }

    #[test]
    fn test_ioctl_strip_size_2() {
        let stripped = ioctl_strip_size(0x40206b00, ScmpArch::X8664);
        assert_eq!(stripped, 0x40006b00);
    }

    #[test]
    fn test_ioctl_strip_size_3() {
        let stripped = ioctl_strip_size(0x40006b00, ScmpArch::X8664);
        assert_eq!(stripped, 0x40006b00);
    }

    #[test]
    fn test_ioctl_strip_size_4() {
        let stripped = ioctl_strip_size(0x00000000, ScmpArch::X8664);
        assert_eq!(stripped, 0x00000000);
    }

    #[test]
    fn test_ioctl_strip_size_5() {
        let stripped = ioctl_strip_size(0xFFFFFFFF, ScmpArch::X8664);
        assert_eq!(stripped, 0xC000FFFF);
    }

    #[test]
    fn test_ioctl_strip_size_6() {
        let stripped = ioctl_strip_size(0x80806b00, ScmpArch::Ppc);
        assert_eq!(stripped, 0x80006b00);
    }

    #[test]
    fn test_ioctl_strip_size_7() {
        let stripped = ioctl_strip_size(0x80806b00, ScmpArch::Mips);
        assert_eq!(stripped, 0x80006b00);
    }

    #[test]
    fn test_ioctl_strip_size_8() {
        let stripped = ioctl_strip_size(0xFFFFFFFF, ScmpArch::Ppc);
        assert_eq!(stripped, 0xE000FFFF);
    }

    #[test]
    fn test_ioctl_strip_size_9() {
        let a = ioctl_strip_size(0x40806b00, ScmpArch::X8664);
        let b = ioctl_strip_size(0x40206b00, ScmpArch::X8664);
        let c = ioctl_strip_size(0x40FF6b00, ScmpArch::X8664);
        assert_eq!(a, b);
        assert_eq!(b, c);
    }

    #[test]
    fn test_ioctl_strip_size_10() {
        let orig: Ioctl = 0x40806b00;
        let stripped = ioctl_strip_size(orig, ScmpArch::X8664);
        assert_eq!(orig & 0xC000FFFF, stripped);
        assert_eq!(stripped & 0x0000FF00, 0x6b00);
        assert_eq!(stripped & 0x000000FF, 0x00);
        assert_eq!(stripped >> 30, 1);
    }

    #[test]
    fn test_ioctl_is_varsize_1() {
        assert!(ioctl_is_varsize(0x40006b00, ScmpArch::X8664));
    }

    #[test]
    fn test_ioctl_is_varsize_2() {
        assert!(!ioctl_is_varsize(0x40806b00, ScmpArch::X8664));
    }

    #[test]
    fn test_ioctl_is_varsize_3() {
        assert!(!ioctl_is_varsize(0x00005401, ScmpArch::X8664));
    }

    #[test]
    fn test_ioctl_is_varsize_4() {
        assert!(!ioctl_is_varsize(0x00000000, ScmpArch::X8664));
    }

    #[test]
    fn test_ioctl_is_varsize_5() {
        assert!(ioctl_is_varsize(0x80006b00, ScmpArch::Ppc));
    }

    #[test]
    fn test_ioctl_is_varsize_6() {
        assert!(!ioctl_is_varsize(0x80806b00, ScmpArch::Ppc));
    }

    #[test]
    fn test_ioctl_is_varsize_7() {
        assert!(ioctl_is_varsize(0x80006b00, ScmpArch::Mips));
    }

    #[test]
    fn test_ioctl_is_varsize_8() {
        assert!(!ioctl_is_varsize(0x80806b00, ScmpArch::Mips64));
    }

    #[test]
    fn test_ioctl_is_varsize_9() {
        assert!(ioctl_is_varsize(0x80004506, ScmpArch::X8664));
    }

    #[test]
    fn test_ioctl_is_varsize_10() {
        assert!(!ioctl_is_varsize(0x80085413, ScmpArch::X8664));
    }
}