starnix_uapi/
auth.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#![allow(dead_code)]
6
7use crate::errors::{Errno, error};
8use crate::{gid_t, uapi, uid_t};
9use bitflags::bitflags;
10use std::ops;
11
12// We don't use bitflags for this because capability sets can have bits set that don't have defined
13// meaning as capabilities. init has all 64 bits set, even though only 40 of them are valid.
14#[derive(Clone, Copy, Eq, PartialEq)]
15pub struct Capabilities {
16    mask: u64,
17}
18
19impl Capabilities {
20    pub fn empty() -> Self {
21        Self { mask: 0 }
22    }
23
24    pub fn all() -> Self {
25        Self { mask: u64::MAX }
26    }
27
28    pub fn union(&self, caps: Capabilities) -> Self {
29        let mut new_caps = *self;
30        new_caps.insert(caps);
31        new_caps
32    }
33
34    pub fn difference(&self, caps: Capabilities) -> Self {
35        let mut new_caps = *self;
36        new_caps.remove(caps);
37        new_caps
38    }
39
40    pub fn contains(self, caps: Capabilities) -> bool {
41        (self & caps) == caps
42    }
43
44    pub fn insert(&mut self, caps: Capabilities) {
45        *self |= caps;
46    }
47
48    pub fn remove(&mut self, caps: Capabilities) {
49        *self &= !caps;
50    }
51
52    pub fn as_abi_v1(self) -> u32 {
53        self.mask as u32
54    }
55
56    pub fn from_abi_v1(bits: u32) -> Self {
57        Self { mask: bits as u64 }
58    }
59
60    pub fn as_abi_v3(self) -> (u32, u32) {
61        (self.mask as u32, (self.mask >> 32) as u32)
62    }
63
64    pub fn from_abi_v3(u32s: (u32, u32)) -> Self {
65        Self { mask: u32s.0 as u64 | ((u32s.1 as u64) << 32) }
66    }
67}
68
69impl std::convert::TryFrom<u64> for Capabilities {
70    type Error = Errno;
71
72    fn try_from(capability_num: u64) -> Result<Self, Self::Error> {
73        match 1u64.checked_shl(capability_num as u32) {
74            Some(mask) => Ok(Self { mask }),
75            _ => error!(EINVAL),
76        }
77    }
78}
79
80impl ops::BitAnd for Capabilities {
81    type Output = Self;
82
83    // rhs is the "right-hand side" of the expression `a & b`
84    fn bitand(self, rhs: Self) -> Self::Output {
85        Self { mask: self.mask & rhs.mask }
86    }
87}
88
89impl ops::BitAndAssign for Capabilities {
90    // rhs is the "right-hand side" of the expression `a & b`
91    fn bitand_assign(&mut self, rhs: Self) {
92        self.mask &= rhs.mask;
93    }
94}
95
96impl ops::BitOr for Capabilities {
97    type Output = Self;
98
99    fn bitor(self, rhs: Self) -> Self::Output {
100        Self { mask: self.mask | rhs.mask }
101    }
102}
103
104impl ops::BitOrAssign for Capabilities {
105    fn bitor_assign(&mut self, rhs: Self) {
106        self.mask |= rhs.mask;
107    }
108}
109
110impl ops::Not for Capabilities {
111    type Output = Self;
112
113    fn not(self) -> Self::Output {
114        Self { mask: !self.mask }
115    }
116}
117
118impl std::fmt::Debug for Capabilities {
119    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
120        write!(f, "Capabilities({:#x})", self.mask)
121    }
122}
123
124impl std::str::FromStr for Capabilities {
125    type Err = Errno;
126    fn from_str(s: &str) -> Result<Self, Self::Err> {
127        Ok(match s {
128            "CHOWN" => CAP_CHOWN,
129            "DAC_OVERRIDE" => CAP_DAC_OVERRIDE,
130            "DAC_READ_SEARCH" => CAP_DAC_READ_SEARCH,
131            "FOWNER" => CAP_FOWNER,
132            "FSETID" => CAP_FSETID,
133            "KILL" => CAP_KILL,
134            "SETGID" => CAP_SETGID,
135            "SETUID" => CAP_SETUID,
136            "SETPCAP" => CAP_SETPCAP,
137            "LINUX_IMMUTABLE" => CAP_LINUX_IMMUTABLE,
138            "NET_BIND_SERVICE" => CAP_NET_BIND_SERVICE,
139            "NET_BROADCAST" => CAP_NET_BROADCAST,
140            "NET_ADMIN" => CAP_NET_ADMIN,
141            "NET_RAW" => CAP_NET_RAW,
142            "IPC_LOCK" => CAP_IPC_LOCK,
143            "IPC_OWNER" => CAP_IPC_OWNER,
144            "SYS_MODULE" => CAP_SYS_MODULE,
145            "SYS_RAWIO" => CAP_SYS_RAWIO,
146            "SYS_CHROOT" => CAP_SYS_CHROOT,
147            "SYS_PTRACE" => CAP_SYS_PTRACE,
148            "SYS_PACCT" => CAP_SYS_PACCT,
149            "SYS_ADMIN" => CAP_SYS_ADMIN,
150            "SYS_BOOT" => CAP_SYS_BOOT,
151            "SYS_NICE" => CAP_SYS_NICE,
152            "SYS_RESOURCE" => CAP_SYS_RESOURCE,
153            "SYS_TIME" => CAP_SYS_TIME,
154            "SYS_TTY_CONFIG" => CAP_SYS_TTY_CONFIG,
155            "MKNOD" => CAP_MKNOD,
156            "LEASE" => CAP_LEASE,
157            "AUDIT_WRITE" => CAP_AUDIT_WRITE,
158            "AUDIT_CONTROL" => CAP_AUDIT_CONTROL,
159            "SETFCAP" => CAP_SETFCAP,
160            "MAC_OVERRIDE" => CAP_MAC_OVERRIDE,
161            "MAC_ADMIN" => CAP_MAC_ADMIN,
162            "SYSLOG" => CAP_SYSLOG,
163            "WAKE_ALARM" => CAP_WAKE_ALARM,
164            "BLOCK_SUSPEND" => CAP_BLOCK_SUSPEND,
165            "AUDIT_READ" => CAP_AUDIT_READ,
166            "PERFMON" => CAP_PERFMON,
167            "BPF" => CAP_BPF,
168            "CHECKPOINT_RESTORE" => CAP_CHECKPOINT_RESTORE,
169            _ => return error!(EINVAL),
170        })
171    }
172}
173
174pub const CAP_CHOWN: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_CHOWN };
175pub const CAP_DAC_OVERRIDE: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_DAC_OVERRIDE };
176pub const CAP_DAC_READ_SEARCH: Capabilities =
177    Capabilities { mask: 1u64 << uapi::CAP_DAC_READ_SEARCH };
178pub const CAP_FOWNER: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_FOWNER };
179pub const CAP_FSETID: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_FSETID };
180pub const CAP_KILL: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_KILL };
181pub const CAP_SETGID: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SETGID };
182pub const CAP_SETUID: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SETUID };
183pub const CAP_SETPCAP: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SETPCAP };
184pub const CAP_LINUX_IMMUTABLE: Capabilities =
185    Capabilities { mask: 1u64 << uapi::CAP_LINUX_IMMUTABLE };
186pub const CAP_NET_BIND_SERVICE: Capabilities =
187    Capabilities { mask: 1u64 << uapi::CAP_NET_BIND_SERVICE };
188pub const CAP_NET_BROADCAST: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_NET_BROADCAST };
189pub const CAP_NET_ADMIN: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_NET_ADMIN };
190pub const CAP_NET_RAW: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_NET_RAW };
191pub const CAP_IPC_LOCK: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_IPC_LOCK };
192pub const CAP_IPC_OWNER: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_IPC_OWNER };
193pub const CAP_SYS_MODULE: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYS_MODULE };
194pub const CAP_SYS_RAWIO: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYS_RAWIO };
195pub const CAP_SYS_CHROOT: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYS_CHROOT };
196pub const CAP_SYS_PTRACE: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYS_PTRACE };
197pub const CAP_SYS_PACCT: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYS_PACCT };
198pub const CAP_SYS_ADMIN: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYS_ADMIN };
199pub const CAP_SYS_BOOT: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYS_BOOT };
200pub const CAP_SYS_NICE: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYS_NICE };
201pub const CAP_SYS_RESOURCE: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYS_RESOURCE };
202pub const CAP_SYS_TIME: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYS_TIME };
203pub const CAP_SYS_TTY_CONFIG: Capabilities =
204    Capabilities { mask: 1u64 << uapi::CAP_SYS_TTY_CONFIG };
205pub const CAP_MKNOD: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_MKNOD };
206pub const CAP_LEASE: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_LEASE };
207pub const CAP_AUDIT_WRITE: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_AUDIT_WRITE };
208pub const CAP_AUDIT_CONTROL: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_AUDIT_CONTROL };
209pub const CAP_SETFCAP: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SETFCAP };
210pub const CAP_MAC_OVERRIDE: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_MAC_OVERRIDE };
211pub const CAP_MAC_ADMIN: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_MAC_ADMIN };
212pub const CAP_SYSLOG: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_SYSLOG };
213pub const CAP_WAKE_ALARM: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_WAKE_ALARM };
214pub const CAP_BLOCK_SUSPEND: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_BLOCK_SUSPEND };
215pub const CAP_AUDIT_READ: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_AUDIT_READ };
216pub const CAP_PERFMON: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_PERFMON };
217pub const CAP_BPF: Capabilities = Capabilities { mask: 1u64 << uapi::CAP_BPF };
218pub const CAP_CHECKPOINT_RESTORE: Capabilities =
219    Capabilities { mask: 1u64 << uapi::CAP_CHECKPOINT_RESTORE };
220pub const CAP_LAST_CAP: u32 = uapi::CAP_LAST_CAP;
221
222bitflags! {
223    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
224    pub struct PtraceAccessMode: u32 {
225        const READ      = 1 << 0;
226        const ATTACH    = 1 << 1;
227        const FSCREDS   = 1 << 2;
228        const REALCREDS = 1 << 3;
229        const NOAUDIT   = 1 << 4;
230    }
231}
232
233pub const PTRACE_MODE_READ: PtraceAccessMode = PtraceAccessMode::READ;
234pub const PTRACE_MODE_ATTACH: PtraceAccessMode = PtraceAccessMode::ATTACH;
235pub const PTRACE_MODE_FSCREDS: PtraceAccessMode = PtraceAccessMode::FSCREDS;
236pub const PTRACE_MODE_REALCREDS: PtraceAccessMode = PtraceAccessMode::REALCREDS;
237pub const PTRACE_MODE_READ_FSCREDS: PtraceAccessMode = PtraceAccessMode::from_bits_truncate(
238    PtraceAccessMode::READ.bits() | PtraceAccessMode::FSCREDS.bits(),
239);
240pub const PTRACE_MODE_READ_REALCREDS: PtraceAccessMode = PtraceAccessMode::from_bits_truncate(
241    PtraceAccessMode::READ.bits() | PtraceAccessMode::REALCREDS.bits(),
242);
243pub const PTRACE_MODE_ATTACH_FSCREDS: PtraceAccessMode = PtraceAccessMode::from_bits_truncate(
244    PtraceAccessMode::ATTACH.bits() | PtraceAccessMode::FSCREDS.bits(),
245);
246pub const PTRACE_MODE_ATTACH_REALCREDS: PtraceAccessMode = PtraceAccessMode::from_bits_truncate(
247    PtraceAccessMode::ATTACH.bits() | PtraceAccessMode::REALCREDS.bits(),
248);
249pub const PTRACE_MODE_NOAUDIT: PtraceAccessMode = PtraceAccessMode::NOAUDIT;
250
251#[derive(Debug, Clone)]
252pub struct Credentials {
253    pub uid: uid_t,
254    pub gid: gid_t,
255    pub euid: uid_t,
256    pub egid: gid_t,
257    pub saved_uid: uid_t,
258    pub saved_gid: gid_t,
259    pub groups: Vec<gid_t>,
260
261    /// See https://man7.org/linux/man-pages/man2/setfsuid.2.html
262    pub fsuid: uid_t,
263
264    /// See https://man7.org/linux/man-pages/man2/setfsgid.2.html
265    pub fsgid: gid_t,
266
267    /// From https://man7.org/linux/man-pages/man7/capabilities.7.html
268    ///
269    /// > This is a limiting superset for the effective capabilities that the thread may assume. It
270    /// > is also a limiting superset for the capabilities that may be added to the inheritable set
271    /// > by a thread that does not have the CAP_SETPCAP capability in its effective set.
272    ///
273    /// > If a thread drops a capability from its permitted set, it can never reacquire that
274    /// > capability (unless it execve(2)s either a set-user-ID-root program, or a program whose
275    /// > associated file capabilities grant that capability).
276    pub cap_permitted: Capabilities,
277
278    /// From https://man7.org/linux/man-pages/man7/capabilities.7.html
279    ///
280    /// > This is the set of capabilities used by the kernel to perform permission checks for the
281    /// > thread.
282    pub cap_effective: Capabilities,
283
284    /// From https://man7.org/linux/man-pages/man7/capabilities.7.html
285    ///
286    /// > This is a set of capabilities preserved across an execve(2).  Inheritable capabilities
287    /// > remain inheritable when executing any program, and inheritable capabilities are added to
288    /// > the permitted set when executing a program that has the corresponding bits set in the file
289    /// > inheritable set.
290    ///
291    /// > Because inheritable capabilities are not generally preserved across execve(2) when running
292    /// > as a non-root user, applications that wish to run helper programs with elevated
293    /// > capabilities should consider using ambient capabilities, described below.
294    pub cap_inheritable: Capabilities,
295
296    /// From https://man7.org/linux/man-pages/man7/capabilities.7.html
297    ///
298    /// > The capability bounding set is a mechanism that can be used to limit the capabilities that
299    /// > are gained during execve(2).
300    ///
301    /// > Since Linux 2.6.25, this is a per-thread capability set. In older kernels, the capability
302    /// > bounding set was a system wide attribute shared by all threads on the system.
303    pub cap_bounding: Capabilities,
304
305    /// From https://man7.org/linux/man-pages/man7/capabilities.7.html
306    ///
307    /// > This is a set of capabilities that are preserved across an execve(2) of a program that is
308    /// > not privileged.  The ambient capability set obeys the invariant that no capability can
309    /// > ever be ambient if it is not both permitted and inheritable.
310    ///
311    /// > Executing a program that changes UID or GID due to the set-user-ID or set-group-ID bits
312    /// > or executing a program that has any file capabilities set will clear the ambient set.
313    pub cap_ambient: Capabilities,
314
315    /// From https://man7.org/linux/man-pages/man7/capabilities.7.html
316    ///
317    /// > Starting with kernel 2.6.26, and with a kernel in which file capabilities are enabled,
318    /// > Linux implements a set of per-thread securebits flags that can be used to disable special
319    /// > handling of capabilities for UID 0 (root).
320    ///
321    /// > The securebits flags can be modified and retrieved using the prctl(2)
322    /// > PR_SET_SECUREBITS and PR_GET_SECUREBITS operations.  The CAP_SETPCAP capability is
323    /// > required to modify the flags.
324    pub securebits: SecureBits,
325}
326
327bitflags! {
328    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
329    pub struct SecureBits: u32 {
330        const KEEP_CAPS = 1 << uapi::SECURE_KEEP_CAPS;
331        const KEEP_CAPS_LOCKED = 1 <<  uapi::SECURE_KEEP_CAPS_LOCKED;
332        const NO_SETUID_FIXUP = 1 << uapi::SECURE_NO_SETUID_FIXUP;
333        const NO_SETUID_FIXUP_LOCKED = 1 << uapi::SECURE_NO_SETUID_FIXUP_LOCKED;
334        const NOROOT = 1 << uapi::SECURE_NOROOT;
335        const NOROOT_LOCKED = 1 << uapi::SECURE_NOROOT_LOCKED;
336        const NO_CAP_AMBIENT_RAISE = 1 << uapi::SECURE_NO_CAP_AMBIENT_RAISE;
337        const NO_CAP_AMBIENT_RAISE_LOCKED = 1 << uapi::SECURE_NO_CAP_AMBIENT_RAISE_LOCKED;
338    }
339}
340
341impl Credentials {
342    /// Creates a set of credentials with all possible permissions and capabilities.
343    pub fn root() -> Self {
344        Self::with_ids(0, 0)
345    }
346
347    /// Creates a set of credentials with the given uid and gid. If the uid is 0, the credentials
348    /// will grant superuser access.
349    pub fn with_ids(uid: uid_t, gid: gid_t) -> Credentials {
350        let caps = if uid == 0 { Capabilities::all() } else { Capabilities::empty() };
351        Credentials {
352            uid,
353            gid,
354            euid: uid,
355            egid: gid,
356            saved_uid: uid,
357            saved_gid: gid,
358            groups: vec![],
359            fsuid: uid,
360            fsgid: gid,
361            cap_permitted: caps,
362            cap_effective: caps,
363            cap_inheritable: Capabilities::empty(),
364            cap_bounding: Capabilities::all(),
365            cap_ambient: Capabilities::empty(),
366            securebits: SecureBits::empty(),
367        }
368    }
369
370    pub fn is_superuser(&self) -> bool {
371        self.euid == 0
372    }
373
374    pub fn is_in_group(&self, gid: gid_t) -> bool {
375        self.egid == gid || self.groups.contains(&gid)
376    }
377
378    /// Returns whether or not the task has the given `capability`.
379    pub fn has_capability(&self, capability: Capabilities) -> bool {
380        self.cap_effective.contains(capability)
381    }
382
383    fn apply_suid_and_sgid(&mut self, maybe_set: UserAndOrGroupId) {
384        if maybe_set.is_none() {
385            return;
386        }
387
388        let prev = self.copy_user_credentials();
389
390        if let Some(uid) = maybe_set.uid {
391            self.euid = uid;
392            self.fsuid = uid;
393        }
394
395        if let Some(gid) = maybe_set.gid {
396            self.egid = gid;
397            self.fsgid = gid;
398        }
399
400        self.update_capabilities(prev);
401    }
402
403    pub fn exec(&mut self, maybe_set: UserAndOrGroupId) {
404        let is_suid_or_sgid = maybe_set.is_some();
405        // From <https://man7.org/linux/man-pages/man2/execve.2.html>:
406        //
407        //   If the set-user-ID bit is set on the program file referred to by
408        //   pathname, then the effective user ID of the calling process is
409        //   changed to that of the owner of the program file.  Similarly, if
410        //   the set-group-ID bit is set on the program file, then the
411        //   effective group ID of the calling process is set to the group of
412        //   the program file.
413        self.apply_suid_and_sgid(maybe_set);
414
415        // From <https://man7.org/linux/man-pages/man2/execve.2.html>:
416        //
417        //   The effective user ID of the process is copied to the saved set-
418        //   user-ID; similarly, the effective group ID is copied to the saved
419        //   set-group-ID.  This copying takes place after any effective ID
420        //   changes that occur because of the set-user-ID and set-group-ID
421        //   mode bits.
422        self.saved_uid = self.euid;
423        self.saved_gid = self.egid;
424
425        // From <https://man7.org/linux/man-pages/man7/capabilities.7.html>:
426        //
427        //   During an execve(2), the kernel calculates the new capabilities
428        //   of the process using the following algorithm:
429        //   P'(ambient)     = (file is privileged) ? 0 : P(ambient)
430        //   P'(permitted)   = (P(inheritable) & F(inheritable)) |
431        //                     (F(permitted) & P(bounding)) | P'(ambient)
432        //   P'(effective)   = F(effective) ? P'(permitted) : P'(ambient)
433        //   P'(inheritable) = P(inheritable)    [i.e., unchanged]
434        //   P'(bounding)    = P(bounding)       [i.e., unchanged]
435        // where:
436        //   P()    denotes the value of a thread capability set before
437        //          the execve(2)
438        //   P'()   denotes the value of a thread capability set after the
439        //          execve(2)
440        //   F()    denotes a file capability set
441
442        // a privileged file is one that has capabilities or
443        // has the set-user-ID or set-group-ID bit set.
444        // TODO(https://fxbug.dev/328629782): Add support for file capabilities.
445        let file_is_privileged = is_suid_or_sgid;
446
447        // After having performed any changes to the process effective ID
448        // that were triggered by the set-user-ID mode bit of the binary—
449        // e.g., switching the effective user ID to 0 (root) because a set-
450        // user-ID-root program was executed—the kernel calculates the file
451        // capability sets as follows:
452
453        // (1)  If the real or effective user ID of the process is 0 (root),
454        //  then the file inheritable and permitted sets are ignored;
455        //  instead they are notionally considered to be all ones (i.e.,
456        //  all capabilities enabled).
457        let (file_permitted, file_inheritable) = if self.uid == 0 || self.euid == 0 {
458            (Capabilities::all(), Capabilities::all())
459        } else {
460            (Capabilities::empty(), Capabilities::empty())
461        };
462
463        // (2)  If the effective user ID of the process is 0 (root) or the
464        //  file effective bit is in fact enabled, then the file
465        //  effective bit is notionally defined to be one (enabled).
466        let file_effective = self.euid == 0;
467
468        // TODO(https://fxbug.dev/328629782): File capabilities are honored for set-user-ID-root
469        // binaries with capabilities executed by non-root users. See "Set-user-ID-root programs
470        // that have file capabilities" in the man page.
471
472        //   P'(ambient)     = (file is privileged) ? 0 : P(ambient)
473        self.cap_ambient =
474            if file_is_privileged { Capabilities::empty() } else { self.cap_ambient };
475
476        //   P'(permitted)   = (P(inheritable) & F(inheritable)) |
477        //                     (F(permitted) & P(bounding)) | P'(ambient)
478        self.cap_permitted = (self.cap_inheritable & file_inheritable)
479            | (file_permitted & self.cap_bounding)
480            | self.cap_ambient;
481
482        //   P'(effective)   = F(effective) ? P'(permitted) : P'(ambient)
483        self.cap_effective = if file_effective { self.cap_permitted } else { self.cap_ambient };
484
485        self.securebits.remove(SecureBits::KEEP_CAPS);
486    }
487
488    pub fn as_fscred(&self) -> FsCred {
489        FsCred { uid: self.fsuid, gid: self.fsgid }
490    }
491
492    pub fn euid_as_fscred(&self) -> FsCred {
493        FsCred { uid: self.euid, gid: self.egid }
494    }
495
496    pub fn uid_as_fscred(&self) -> FsCred {
497        FsCred { uid: self.uid, gid: self.gid }
498    }
499
500    pub fn copy_user_credentials(&self) -> UserCredentials {
501        UserCredentials {
502            uid: self.uid,
503            euid: self.euid,
504            fsuid: self.fsuid,
505            saved_uid: self.saved_uid,
506        }
507    }
508
509    pub fn update_capabilities(&mut self, prev: UserCredentials) {
510        // https://man7.org/linux/man-pages/man7/capabilities.7.html
511        // If one or more of the real, effective, or saved set user IDs
512        // was previously 0, and as a result of the UID changes all of
513        // these IDs have a nonzero value, then all capabilities are
514        // cleared from the permitted, effective, and ambient capability
515        // sets.
516        //
517        // SECBIT_KEEP_CAPS: Setting this flag allows a thread that has one or more 0
518        // UIDs to retain capabilities in its permitted set when it
519        // switches all of its UIDs to nonzero values.
520        // The setting of the SECBIT_KEEP_CAPS flag is ignored if the
521        // SECBIT_NO_SETUID_FIXUP flag is set.  (The latter flag
522        // provides a superset of the effect of the former flag.)
523        // SECBIT_NO_SETUID_FIXUP: Setting  this  flag  stops  the  kernel from adjusting
524        // the process's permitted, effective, and ambient capability sets when the thread's
525        // effective and filesystem UIDs are switched between zero and nonzero values.
526        if self.securebits.contains(SecureBits::NO_SETUID_FIXUP) {
527            return;
528        }
529        if !self.securebits.contains(SecureBits::KEEP_CAPS)
530            && (prev.uid == 0 || prev.euid == 0 || prev.saved_uid == 0)
531            && (self.uid != 0 && self.euid != 0 && self.saved_uid != 0)
532        {
533            self.cap_permitted = Capabilities::empty();
534            self.cap_effective = Capabilities::empty();
535            self.cap_ambient = Capabilities::empty();
536        }
537        // If the effective user ID is changed from 0 to nonzero, then
538        // all capabilities are cleared from the effective set.
539        if prev.euid == 0 && self.euid != 0 {
540            self.cap_effective = Capabilities::empty();
541        } else if prev.euid != 0 && self.euid == 0 {
542            // If the effective user ID is changed from nonzero to 0, then
543            // the permitted set is copied to the effective set.
544            self.cap_effective = self.cap_permitted;
545        }
546
547        // If the filesystem user ID is changed from 0 to nonzero (see
548        // setfsuid(2)), then the following capabilities are cleared from
549        // the effective set: CAP_CHOWN, CAP_DAC_OVERRIDE,
550        // CAP_DAC_READ_SEARCH, CAP_FOWNER, CAP_FSETID,
551        // CAP_LINUX_IMMUTABLE (since Linux 2.6.30), CAP_MAC_OVERRIDE,
552        // and CAP_MKNOD (since Linux 2.6.30).
553        let fs_capabilities = CAP_CHOWN
554            | CAP_DAC_OVERRIDE
555            | CAP_DAC_READ_SEARCH
556            | CAP_FOWNER
557            | CAP_FSETID
558            | CAP_LINUX_IMMUTABLE
559            | CAP_MAC_OVERRIDE
560            | CAP_MKNOD;
561        if prev.fsuid == 0 && self.fsuid != 0 {
562            self.cap_effective &= !fs_capabilities;
563        } else if prev.fsuid != 0 && self.fsuid == 0 {
564            // If the filesystem UID is changed from nonzero to 0, then any
565            // of these capabilities that are enabled in the permitted set
566            // are enabled in the effective set.
567            self.cap_effective |= self.cap_permitted & fs_capabilities;
568        }
569    }
570}
571
572/// The owner and group of a file. Used as a parameter for functions that create files.
573#[derive(Debug, Clone, Copy)]
574pub struct FsCred {
575    pub uid: uid_t,
576    pub gid: gid_t,
577}
578
579impl FsCred {
580    pub const fn root() -> Self {
581        Self { uid: 0, gid: 0 }
582    }
583}
584
585impl From<Credentials> for FsCred {
586    fn from(c: Credentials) -> Self {
587        c.as_fscred()
588    }
589}
590
591#[derive(Debug, Clone, Copy)]
592pub struct UserCredentials {
593    pub uid: uid_t,
594    pub euid: uid_t,
595    pub saved_uid: uid_t,
596    pub fsuid: uid_t,
597}
598
599#[derive(Debug, Default, Clone)]
600pub struct UserAndOrGroupId {
601    pub uid: Option<uid_t>,
602    pub gid: Option<gid_t>,
603}
604
605impl UserAndOrGroupId {
606    pub fn is_none(&self) -> bool {
607        self.uid.is_none() && self.gid.is_none()
608    }
609
610    pub fn is_some(&self) -> bool {
611        !self.is_none()
612    }
613
614    pub fn clear(&mut self) {
615        self.uid = None;
616        self.gid = None;
617    }
618}
619
620#[cfg(test)]
621mod tests {
622    use super::*;
623
624    #[::fuchsia::test]
625    fn test_empty() {
626        assert_eq!(Capabilities::empty().mask, 0);
627    }
628
629    #[::fuchsia::test]
630    fn test_all() {
631        // all() should be every bit set, not just all the CAP_* constants.
632        assert_eq!(Capabilities::all().mask, u64::MAX);
633    }
634
635    #[::fuchsia::test]
636    fn test_union() {
637        let expected = Capabilities { mask: CAP_BLOCK_SUSPEND.mask | CAP_AUDIT_READ.mask };
638        assert_eq!(CAP_BLOCK_SUSPEND.union(CAP_AUDIT_READ), expected);
639        assert_eq!(CAP_BLOCK_SUSPEND.union(CAP_BLOCK_SUSPEND), CAP_BLOCK_SUSPEND);
640    }
641
642    #[::fuchsia::test]
643    fn test_difference() {
644        let base = CAP_BPF | CAP_AUDIT_WRITE;
645        let expected = CAP_BPF;
646        assert_eq!(base.difference(CAP_AUDIT_WRITE), expected);
647        assert_eq!(base.difference(CAP_AUDIT_WRITE | CAP_BPF), Capabilities::empty());
648    }
649
650    #[::fuchsia::test]
651    fn test_contains() {
652        let base = CAP_BPF | CAP_AUDIT_WRITE;
653        assert!(base.contains(CAP_AUDIT_WRITE));
654        assert!(base.contains(CAP_BPF));
655        assert!(base.contains(CAP_AUDIT_WRITE | CAP_BPF));
656
657        assert!(!base.contains(CAP_AUDIT_CONTROL));
658        assert!(!base.contains(CAP_AUDIT_WRITE | CAP_BPF | CAP_AUDIT_CONTROL));
659    }
660
661    #[::fuchsia::test]
662    fn test_insert() {
663        let mut capabilities = CAP_BLOCK_SUSPEND;
664        capabilities.insert(CAP_BLOCK_SUSPEND);
665        assert_eq!(capabilities, CAP_BLOCK_SUSPEND);
666
667        capabilities.insert(CAP_AUDIT_READ);
668        let expected = Capabilities { mask: CAP_BLOCK_SUSPEND.mask | CAP_AUDIT_READ.mask };
669        assert_eq!(capabilities, expected);
670    }
671
672    #[::fuchsia::test]
673    fn test_remove() {
674        let mut capabilities = CAP_BLOCK_SUSPEND;
675        capabilities.remove(CAP_BLOCK_SUSPEND);
676        assert_eq!(capabilities, Capabilities::empty());
677
678        let mut capabilities = CAP_BLOCK_SUSPEND | CAP_AUDIT_READ;
679        capabilities.remove(CAP_AUDIT_READ);
680        assert_eq!(capabilities, CAP_BLOCK_SUSPEND);
681    }
682
683    #[::fuchsia::test]
684    fn test_try_from() {
685        let capabilities = CAP_BLOCK_SUSPEND;
686        assert_eq!(Capabilities::try_from(uapi::CAP_BLOCK_SUSPEND as u64), Ok(capabilities));
687
688        assert_eq!(Capabilities::try_from(200000), error!(EINVAL));
689    }
690}