capctl 0.2.4

A pure-Rust interface to prctl() and Linux capabilities.
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
//! Interfaces to Linux capabilities.

use core::fmt;

mod cap_text;
mod capset;
mod capstate;
mod helpers;

#[cfg(feature = "serde")]
mod serde_impl;

#[cfg(feature = "std")]
mod file;
#[cfg(feature = "std")]
pub use file::{FileCaps, ParseFileCapsError};

#[cfg(feature = "std")]
mod fullcapstate;
#[cfg(feature = "std")]
pub use fullcapstate::FullCapState;

pub mod ambient;
pub mod bounding;
pub use capset::{CapSet, CapSetIterator};
pub use capstate::{CapState, ParseCapStateError};
pub use helpers::cap_set_ids;

/// Given a series of "paths" (i.e. `a::b`), yield the last one.
macro_rules! last_path {
    ($last:path $(,)?) => {
        $last
    };

    ($first:path$(, $rest:path)+ $(,)?) => {
        last_path! { $($rest),+ }
    }
}

macro_rules! define_cap {
    ($($name:ident = $val:literal,)+) => {
        /// An enum representing all of the possible Linux capabilities.
        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
        #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
        #[repr(u8)]
        #[allow(non_camel_case_types, clippy::upper_case_acronyms)]
        #[non_exhaustive]
        pub enum Cap {
            $($name = $val,)+
        }

        impl Cap {
            #[inline]
            fn from_u8(val: u8) -> Option<Self> {
                match val {
                    $($val => Some(Self::$name),)*
                    _ => None,
                }
            }
        }

        // WARNING: Unsafe code trusts these constants to be correct!

        const LAST_CAP: Cap = last_path!($(Cap::$name,)+);

        // Some other useful values derived from LAST_CAP
        const CAP_MAX: u8 = LAST_CAP as u8;
        const NUM_CAPS: u8 = CAP_MAX + 1;
        // Get the lower bits filled with ones
        const CAP_BITMASK: u64 = u64::MAX >> (63 - CAP_MAX);

        static CAP_NAMES: [&str; NUM_CAPS as usize] = [$(stringify!($name),)+];
    };
}

define_cap! {
    CHOWN = 0,
    DAC_OVERRIDE = 1,
    DAC_READ_SEARCH = 2,
    FOWNER = 3,
    FSETID = 4,
    KILL = 5,
    SETGID = 6,
    SETUID = 7,
    SETPCAP = 8,
    LINUX_IMMUTABLE = 9,
    NET_BIND_SERVICE = 10,
    NET_BROADCAST = 11,
    NET_ADMIN = 12,
    NET_RAW = 13,
    IPC_LOCK = 14,
    IPC_OWNER = 15,
    SYS_MODULE = 16,
    SYS_RAWIO = 17,
    SYS_CHROOT = 18,
    SYS_PTRACE = 19,
    SYS_PACCT = 20,
    SYS_ADMIN = 21,
    SYS_BOOT = 22,
    SYS_NICE = 23,
    SYS_RESOURCE = 24,
    SYS_TIME = 25,
    SYS_TTY_CONFIG = 26,
    MKNOD = 27,
    LEASE = 28,
    AUDIT_WRITE = 29,
    AUDIT_CONTROL = 30,
    SETFCAP = 31,
    MAC_OVERRIDE = 32,
    MAC_ADMIN = 33,
    SYSLOG = 34,
    WAKE_ALARM = 35,
    BLOCK_SUSPEND = 36,
    AUDIT_READ = 37,
    PERFMON = 38,
    BPF = 39,
    CHECKPOINT_RESTORE = 40,
    // Adding a new capability here is sufficient to make the library aware of it (though the
    // capability numbers MUST be consecutive)
}

impl Cap {
    /// Return an iterator over all of the capabilities enumerated by `Cap`.
    #[inline]
    pub fn iter() -> CapIter {
        CapIter { i: 0 }
    }

    #[inline]
    fn to_single_bitfield(self) -> u64 {
        // Sanity check to help ensure CAP_MAX is set correctly (note that this will only catch some
        // cases)
        debug_assert!((self as u8) <= CAP_MAX);

        1u64 << (self as u8)
    }

    /// Checks whether the specified capability is supported on the current kernel.
    #[inline]
    pub fn is_supported(self) -> bool {
        bounding::read(self).is_some()
    }

    /// Determines the set of capabilities supported by the running kernel.
    ///
    /// This uses a binary search combined with [`Cap::is_supported()`] to determine the supported
    /// capabilities. It is more efficient than a simple `Cap::iter()`/`Cap::is_supported()` loop.
    ///
    /// [`Cap::is_supported()`]: #method.is_supported
    pub fn probe_supported() -> CapSet {
        // Do a binary search

        // Rust currently supports kernel 2.6.32+. CAP_MAC_ADMIN was the last capability added
        // before that release (in kernel 2.6.25).
        let mut min = Self::MAC_ADMIN as u8;
        let mut max = CAP_MAX;

        debug_assert!(Self::from_u8(min).unwrap().is_supported());

        while min != max {
            // This basically does `mid = ceil((min + max) / 2)`.
            // If we don't do ceiling division, the way binary search works, we'll get stuck at
            // `max = min + 1` forever.
            let mid = (min + max + 1) >> 1;

            if Self::from_u8(mid).unwrap().is_supported() {
                min = mid;
            } else {
                max = mid - 1;
            }

            debug_assert!(max >= min);
        }

        CapSet::from_bitmask_truncate(u64::MAX >> (63 - min))
    }

    pub(crate) fn name(self) -> &'static str {
        CAP_NAMES[self as usize]
    }
}

impl core::str::FromStr for Cap {
    type Err = ParseCapError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.len() > 4 && s[..4].eq_ignore_ascii_case("CAP_") {
            let s = &s[4..];

            for (i, cap_name) in CAP_NAMES.iter().enumerate() {
                if cap_name.eq_ignore_ascii_case(s) {
                    return Ok(Cap::from_u8(i as u8).unwrap());
                }
            }
        }

        Err(ParseCapError(()))
    }
}

impl fmt::Display for Cap {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("CAP_")?;
        fmt::Debug::fmt(self, f)
    }
}

/// Represents an error when parsing a `Cap` from a string.
#[derive(Clone, Eq, PartialEq)]
pub struct ParseCapError(());

impl fmt::Debug for ParseCapError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("ParseCapError")
            .field("message", &"Unknown capability")
            .finish()
    }
}

impl fmt::Display for ParseCapError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Unknown capability")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ParseCapError {}

/// An iterator over all the capabilities enumerated in `Cap`.
///
/// This is constructed by [`Cap::iter()`].
///
/// [`Cap::iter()`]: ./enum.Cap.html#method.iter
#[derive(Clone)]
pub struct CapIter {
    i: u8,
}

impl Iterator for CapIter {
    type Item = Cap;

    fn next(&mut self) -> Option<Cap> {
        debug_assert!(self.i <= NUM_CAPS);

        let cap = Cap::from_u8(self.i)?;
        self.i += 1;
        Some(cap)
    }

    fn nth(&mut self, n: usize) -> Option<Cap> {
        if n < self.len() {
            self.i += n as u8;
            self.next()
        } else {
            // The specified index would exhaust the iterator
            self.i = NUM_CAPS;
            None
        }
    }

    #[inline]
    fn last(self) -> Option<Cap> {
        if self.i < NUM_CAPS {
            Some(LAST_CAP)
        } else {
            None
        }
    }

    #[inline]
    fn count(self) -> usize {
        self.len()
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.len();
        (len, Some(len))
    }
}

impl ExactSizeIterator for CapIter {
    #[inline]
    fn len(&self) -> usize {
        (NUM_CAPS - self.i) as usize
    }
}

impl core::iter::FusedIterator for CapIter {}

#[cfg(test)]
mod tests {
    use core::str::FromStr;

    use super::*;

    #[allow(clippy::eq_op)]
    #[test]
    fn test_last_path() {
        // Briefly test the last_path! macro since unsafe code relies on it to be correct
        assert_eq!(last_path!(Cap::CHOWN), Cap::CHOWN);
        assert_eq!(last_path!(Cap::CHOWN, Cap::SETUID), Cap::SETUID);
        assert_eq!(
            last_path!(Cap::CHOWN, CAP::SETUID, Cap::SETGID,),
            Cap::SETGID
        );
    }

    #[test]
    fn test_cap_u8() {
        for i in 0..NUM_CAPS {
            assert_eq!(Cap::from_u8(i).unwrap() as u8, i);
        }

        for i in NUM_CAPS..=u8::MAX {
            assert_eq!(Cap::from_u8(i), None);
        }
    }

    #[test]
    fn test_cap_string() {
        assert_eq!(Cap::from_str("CAP_CHOWN"), Ok(Cap::CHOWN));
        assert_eq!(Cap::from_str("cap_CHOWN"), Ok(Cap::CHOWN));
        assert_eq!(Cap::from_str("Cap_CHOWN"), Ok(Cap::CHOWN));

        assert_eq!(Cap::from_str("CAP_sys_chroot"), Ok(Cap::SYS_CHROOT));
        assert_eq!(Cap::from_str("cap_sys_chroot"), Ok(Cap::SYS_CHROOT));
        assert_eq!(Cap::from_str("Cap_Sys_chroot"), Ok(Cap::SYS_CHROOT));

        assert!(Cap::from_str("").is_err());
        assert!(Cap::from_str("CAP_").is_err());
        assert!(Cap::from_str("CHOWN").is_err());
        assert!(Cap::from_str("CAP_NOEXIST").is_err());

        #[cfg(feature = "std")]
        assert_eq!(Cap::CHOWN.to_string(), "CAP_CHOWN");

        #[cfg(feature = "std")]
        for cap in Cap::iter() {
            let s = cap.to_string();
            assert_eq!(Cap::from_str(&s), Ok(cap));
            assert_eq!(Cap::from_str(&s.to_lowercase()), Ok(cap));
            assert_eq!(Cap::from_str(&s.to_uppercase()), Ok(cap));
        }

        for (cap, name) in Cap::iter().zip(&CAP_NAMES) {
            // Concatenate strings without allocating
            let mut full_name = [0u8; 30];
            full_name[..4].copy_from_slice(b"cap_");
            full_name[4..name.len() + 4].copy_from_slice(name.as_bytes());

            assert_eq!(
                Cap::from_str(core::str::from_utf8(&full_name[..name.len() + 4]).unwrap()),
                Ok(cap)
            );
        }
    }

    #[cfg(feature = "std")]
    #[allow(deprecated)]
    #[test]
    fn test_cap_string_error() {
        let err = ParseCapError(());

        // Make sure clone() and eq() work
        // This will probably be optimized away because it's zero-sized, but it checks that the
        // struct derives Clone and Eq.
        assert_eq!(err, err.clone());

        // Make sure the string representations match
        assert_eq!(err.to_string(), "Unknown capability");
        assert_eq!(
            format!("{:?}", err),
            "ParseCapError { message: \"Unknown capability\" }"
        );
    }

    #[test]
    fn test_cap_iter_last() {
        assert_eq!(Cap::iter().last(), Some(LAST_CAP));

        let mut last = None;
        for cap in Cap::iter() {
            last = Some(cap);
        }
        assert_eq!(last, Some(LAST_CAP));

        let mut it = Cap::iter();
        for _ in it.by_ref() {}
        assert_eq!(it.len(), 0);
        assert_eq!(it.last(), None);
    }

    #[allow(clippy::iter_nth_zero)]
    #[test]
    fn test_cap_iter_nth() {
        let mut it = Cap::iter();
        while let Some(cap) = it.clone().next() {
            assert_eq!(cap, it.nth(0).unwrap());
        }
        assert_eq!(it.nth(0), None);

        assert_eq!(Cap::iter().nth(0), Some(Cap::CHOWN));
        assert_eq!(Cap::iter().nth(1), Some(Cap::DAC_OVERRIDE));
        assert_eq!(Cap::iter().nth(NUM_CAPS as usize - 1), Some(LAST_CAP));
    }

    #[allow(clippy::iter_nth_zero)]
    #[test]
    fn test_cap_iter_fused() {
        let mut it = Cap::iter();
        for _ in it.by_ref() {}

        for _ in 0..256 {
            assert_eq!(it.next(), None);
            assert_eq!(it.nth(0), None);
        }
    }

    #[test]
    fn test_cap_iter_count() {
        let mut it = Cap::iter();

        let mut count = it.len();

        assert_eq!(it.clone().count(), count);
        assert_eq!(it.size_hint(), (count, Some(count)));

        while let Some(_cap) = it.next() {
            count -= 1;
            assert_eq!(it.len(), count);
            assert_eq!(it.clone().count(), count);
            assert_eq!(it.size_hint(), (count, Some(count)));
        }

        assert_eq!(count, 0);

        assert_eq!(it.len(), 0);
        assert_eq!(it.clone().count(), 0);
        assert_eq!(it.size_hint(), (0, Some(0)));
    }

    #[test]
    fn test_cap_bits() {
        let mut mask: u64 = 0;

        for cap in Cap::iter() {
            let cap_bits = cap.to_single_bitfield();
            assert_eq!(2u64.pow(cap as u32), cap_bits);
            mask |= cap_bits;
        }

        assert_eq!(mask, CAP_BITMASK);
    }

    #[test]
    fn test_supported_caps() {
        let supported_caps = Cap::probe_supported();

        // Check that the binary search worked properly
        for cap in Cap::iter() {
            assert_eq!(supported_caps.has(cap), cap.is_supported(), "{:?}", cap);
        }
    }
}