byssus 0.1.0

Live, read-only filesystem attachments between isolated workspaces
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! Pure capability-normalization planning.
//!
//! Given the process's current credentials and what the caller needs, decide
//! the exact sequence of credential operations that brings the process to a
//! minimal state. See `docs/DESIGN.md`, "Capability normalization".

use rustix::thread::{CapabilitiesSecureBits, CapabilitySet, CapabilitySets};

/// Current credentials of the process.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Credentials {
    /// Real, effective and saved user IDs.
    pub uids: [u32; 3],
    /// Capability sets.
    pub caps: CapabilitySets,
}

impl Credentials {
    fn any_uid_is_root(&self) -> bool {
        self.uids.contains(&0)
    }
}

/// A resolved service user.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServiceUser {
    /// User name.
    pub name: String,
    /// User ID.
    pub uid: u32,
    /// Primary group ID.
    pub gid: u32,
    /// Supplementary group IDs.
    pub groups: Vec<u32>,
}

impl ServiceUser {
    /// The groups a switch to this user sets: its primary group, with the
    /// primary group also among the supplementary groups.
    fn final_groups(&self) -> FinalGroups {
        let mut groups = self.groups.clone();
        if !groups.contains(&self.gid) {
            groups.insert(0, self.gid);
        }
        FinalGroups {
            gid: self.gid,
            groups,
        }
    }
}

/// What the caller needs after normalization.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Goal {
    /// Keep exactly `CAP_SYS_ADMIN` (daemon, `byssus reconcile`).
    KeepSysAdmin,
    /// Keep no capabilities (`byssus dry-run` simulating the service user).
    DropAll,
}

/// A normalization request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request {
    /// Capabilities to keep.
    pub goal: Goal,
    /// Service user to switch to if running as root.
    pub user: Option<ServiceUser>,
    /// Permit remaining UID 0 when no service user is configured.
    pub allow_root: bool,
}

/// One credential operation, applied in order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Op {
    /// `PR_CAP_AMBIENT_CLEAR_ALL`.
    ClearAmbient,
    /// `capset` to the given sets.
    SetCaps(CapabilitySets),
    /// `PR_CAPBSET_DROP` for every capability not in the set.
    DropBoundingExcept(CapabilitySet),
    /// `PR_SET_SECUREBITS` adding these bits; bits already set are kept.
    SetSecureBits(CapabilitiesSecureBits),
    /// `PR_SET_KEEPCAPS`.
    SetKeepCaps(bool),
    /// `setgroups`.
    SetGroups(Vec<u32>),
    /// `setresgid` with all three IDs equal.
    SetResGid(u32),
    /// `setresuid` with all three IDs equal.
    SetResUid(u32),
    /// `PR_SET_NO_NEW_PRIVS`.
    SetNoNewPrivs,
}

/// A non-fatal observation about the plan.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Warning {
    /// Remaining UID 0 because `allow_root` was given.
    RunningAsRoot,
    /// A service user is configured but the process is not root, so no switch
    /// happens.
    UserNotApplied {
        /// Configured user name.
        user: String,
        /// The effective UID the process keeps.
        effective_uid: u32,
    },
}

/// Normalization cannot proceed.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum PlanError {
    /// `CAP_SYS_ADMIN` is required but not permitted.
    #[error(
        "CAP_SYS_ADMIN is not permitted; run under systemd with AmbientCapabilities=CAP_SYS_ADMIN, \
         or apply 'setcap cap_sys_admin+ep' to the binary"
    )]
    MissingSysAdmin,
    /// Running as root with no service user and no `allow_root`.
    #[error(
        "refusing to run as root: configure daemon.user (or pass --user) so privileges can be \
         dropped, or pass --allow-root for development and testing"
    )]
    RootWithoutUser,
    /// The configured service user is root.
    #[error("service user '{0}' has UID 0; choose an unprivileged user")]
    ServiceUserIsRoot(String),
    /// Cannot switch user because a needed capability is missing.
    #[error("cannot switch to user '{user}': {missing} is not permitted")]
    CannotSwitchUser {
        /// Configured user name.
        user: String,
        /// The missing capability.
        missing: &'static str,
    },
}

/// The planned operations and the expected final state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrivilegePlan {
    /// Operations in order.
    pub ops: Vec<Op>,
    /// Non-fatal warnings.
    pub warnings: Vec<Warning>,
    /// UID expected after applying (all of real, effective, saved).
    pub final_uid: u32,
    /// Capabilities expected in the effective and permitted sets afterwards;
    /// the inheritable and ambient sets are empty.
    pub final_caps: CapabilitySet,
    /// Groups expected after switching to the service user, or `None` when
    /// no switch is planned and groups are left unchanged.
    pub final_groups: Option<FinalGroups>,
}

/// Groups expected after a user switch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FinalGroups {
    /// GID expected (all of real, effective, saved).
    pub gid: u32,
    /// Supplementary groups expected, including `gid`.
    pub groups: Vec<u32>,
}

/// Securebits set and locked when `CAP_SETPCAP` is available: never grant
/// capabilities to root on exec, never adjust capabilities on UID changes,
/// never keep capabilities via `PR_SET_KEEPCAPS`, never raise ambient
/// capabilities.
pub const LOCKED_SECURE_BITS: CapabilitiesSecureBits = CapabilitiesSecureBits::NO_ROOT
    .union(CapabilitiesSecureBits::NO_ROOT_LOCKED)
    .union(CapabilitiesSecureBits::NO_SETUID_FIXUP)
    .union(CapabilitiesSecureBits::NO_SETUID_FIXUP_LOCKED)
    .union(CapabilitiesSecureBits::KEEP_CAPS_LOCKED)
    .union(CapabilitiesSecureBits::NO_CAP_AMBIENT_RAISE)
    .union(CapabilitiesSecureBits::NO_CAP_AMBIENT_RAISE_LOCKED);

/// Plans normalization.
pub fn plan(current: &Credentials, request: &Request) -> Result<PrivilegePlan, PlanError> {
    let keep = match request.goal {
        Goal::KeepSysAdmin => CapabilitySet::SYS_ADMIN,
        Goal::DropAll => CapabilitySet::empty(),
    };
    let permitted = current.caps.permitted;
    if request.goal == Goal::KeepSysAdmin && !permitted.contains(CapabilitySet::SYS_ADMIN) {
        return Err(PlanError::MissingSysAdmin);
    }

    let mut warnings = Vec::new();
    let effective_uid = current.uids[1];
    let switch_to = match (current.any_uid_is_root(), &request.user) {
        (true, Some(user)) if user.uid == 0 => {
            return Err(PlanError::ServiceUserIsRoot(user.name.clone()));
        }
        (true, Some(user)) => Some(user),
        (true, None) => {
            if request.goal == Goal::KeepSysAdmin {
                if !request.allow_root {
                    return Err(PlanError::RootWithoutUser);
                }
                warnings.push(Warning::RunningAsRoot);
            }
            None
        }
        (false, Some(user)) => {
            if effective_uid != user.uid {
                warnings.push(Warning::UserNotApplied {
                    user: user.name.clone(),
                    effective_uid,
                });
            }
            None
        }
        (false, None) => None,
    };

    if let Some(user) = switch_to {
        for (cap, label) in [
            (CapabilitySet::SETUID, "CAP_SETUID"),
            (CapabilitySet::SETGID, "CAP_SETGID"),
        ] {
            if !permitted.contains(cap) {
                return Err(PlanError::CannotSwitchUser {
                    user: user.name.clone(),
                    missing: label,
                });
            }
        }
    }

    let has_setpcap = permitted.contains(CapabilitySet::SETPCAP);
    let mut ops = vec![Op::ClearAmbient];

    // Capabilities we will use along the way must be effective first.
    let mut needed = CapabilitySet::empty();
    if has_setpcap {
        needed |= CapabilitySet::SETPCAP;
    }
    if switch_to.is_some() {
        needed |= CapabilitySet::SETUID | CapabilitySet::SETGID;
    }
    if !current.caps.effective.contains(needed) {
        ops.push(Op::SetCaps(CapabilitySets {
            effective: current.caps.effective | needed,
            permitted,
            inheritable: CapabilitySet::empty(),
        }));
    }

    // Changing the bounding set and securebits needs CAP_SETPCAP, which only a
    // root start has; otherwise NO_NEW_PRIVS below makes the bounding set moot.
    if has_setpcap {
        ops.push(Op::DropBoundingExcept(keep));
        ops.push(Op::SetSecureBits(LOCKED_SECURE_BITS));
    }

    if let Some(user) = switch_to {
        // With NO_SETUID_FIXUP locked, capabilities survive the UID change.
        // Without CAP_SETPCAP we cannot set securebits, so use KEEPCAPS.
        if !has_setpcap {
            ops.push(Op::SetKeepCaps(true));
        }
        let groups = user.final_groups();
        ops.push(Op::SetGroups(groups.groups));
        ops.push(Op::SetResGid(groups.gid));
        ops.push(Op::SetResUid(user.uid));
        if !has_setpcap {
            ops.push(Op::SetKeepCaps(false));
        }
    }

    ops.push(Op::SetCaps(CapabilitySets {
        effective: keep,
        permitted: keep,
        inheritable: CapabilitySet::empty(),
    }));
    ops.push(Op::SetNoNewPrivs);

    Ok(PrivilegePlan {
        ops,
        warnings,
        final_uid: switch_to.map_or(effective_uid, |u| u.uid),
        final_caps: keep,
        final_groups: switch_to.map(ServiceUser::final_groups),
    })
}

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

    fn caps(effective: CapabilitySet, permitted: CapabilitySet) -> CapabilitySets {
        CapabilitySets {
            effective,
            permitted,
            inheritable: CapabilitySet::empty(),
        }
    }

    fn creds(uid: u32, sets: CapabilitySets) -> Credentials {
        Credentials {
            uids: [uid; 3],
            caps: sets,
        }
    }

    fn user() -> ServiceUser {
        ServiceUser {
            name: "byssus".into(),
            uid: 991,
            gid: 991,
            groups: vec![],
        }
    }

    fn request(goal: Goal, user: Option<ServiceUser>, allow_root: bool) -> Request {
        Request {
            goal,
            user,
            allow_root,
        }
    }

    const SYS_ADMIN: CapabilitySet = CapabilitySet::SYS_ADMIN;

    fn keep_final(set: CapabilitySet) -> Op {
        Op::SetCaps(caps(set, set))
    }

    #[test]
    fn systemd_launch_only_sys_admin() {
        let current = creds(991, caps(SYS_ADMIN, SYS_ADMIN));
        let p = plan(&current, &request(Goal::KeepSysAdmin, None, false)).unwrap();
        assert_eq!(
            p.ops,
            [Op::ClearAmbient, keep_final(SYS_ADMIN), Op::SetNoNewPrivs]
        );
        assert_eq!(p.final_uid, 991);
        assert_eq!(p.final_caps, SYS_ADMIN);
        assert!(p.warnings.is_empty());
    }

    #[test]
    fn setcap_launch_with_extra_caps_drops_them() {
        let extra = SYS_ADMIN | CapabilitySet::NET_ADMIN | CapabilitySet::DAC_OVERRIDE;
        let current = creds(1000, caps(extra, extra));
        let p = plan(&current, &request(Goal::KeepSysAdmin, None, false)).unwrap();
        assert_eq!(
            p.ops,
            [Op::ClearAmbient, keep_final(SYS_ADMIN), Op::SetNoNewPrivs]
        );
    }

    #[test]
    fn missing_sys_admin_is_an_error() {
        let current = creds(991, caps(CapabilitySet::empty(), CapabilitySet::empty()));
        assert_eq!(
            plan(&current, &request(Goal::KeepSysAdmin, None, false)),
            Err(PlanError::MissingSysAdmin)
        );
    }

    #[test]
    fn root_without_user_refused() {
        let current = creds(0, caps(CapabilitySet::all(), CapabilitySet::all()));
        assert_eq!(
            plan(&current, &request(Goal::KeepSysAdmin, None, false)),
            Err(PlanError::RootWithoutUser)
        );
    }

    #[test]
    fn partially_root_uids_count_as_root() {
        let current = Credentials {
            uids: [1000, 1000, 0],
            caps: caps(CapabilitySet::all(), CapabilitySet::all()),
        };
        assert_eq!(
            plan(&current, &request(Goal::KeepSysAdmin, None, false)),
            Err(PlanError::RootWithoutUser)
        );
    }

    #[test]
    fn root_with_allow_root_drops_caps_and_locks_bits() {
        let all = CapabilitySet::all();
        let current = creds(0, caps(all, all));
        let p = plan(&current, &request(Goal::KeepSysAdmin, None, true)).unwrap();
        assert_eq!(
            p.ops,
            [
                Op::ClearAmbient,
                Op::DropBoundingExcept(SYS_ADMIN),
                Op::SetSecureBits(LOCKED_SECURE_BITS),
                keep_final(SYS_ADMIN),
                Op::SetNoNewPrivs,
            ]
        );
        assert_eq!(p.final_uid, 0);
        assert_eq!(p.final_groups, None);
        assert_eq!(p.warnings, [Warning::RunningAsRoot]);
    }

    #[test]
    fn root_switches_to_service_user() {
        let all = CapabilitySet::all();
        let current = creds(0, caps(all, all));
        let mut u = user();
        u.groups = vec![5, 7];
        let p = plan(&current, &request(Goal::KeepSysAdmin, Some(u), false)).unwrap();
        assert_eq!(
            p.ops,
            [
                Op::ClearAmbient,
                Op::DropBoundingExcept(SYS_ADMIN),
                Op::SetSecureBits(LOCKED_SECURE_BITS),
                Op::SetGroups(vec![991, 5, 7]),
                Op::SetResGid(991),
                Op::SetResUid(991),
                keep_final(SYS_ADMIN),
                Op::SetNoNewPrivs,
            ]
        );
        assert_eq!(p.final_uid, 991);
        assert_eq!(
            p.final_groups,
            Some(FinalGroups {
                gid: 991,
                groups: vec![991, 5, 7],
            })
        );
        assert!(p.warnings.is_empty());
    }

    #[test]
    fn primary_group_not_duplicated() {
        let all = CapabilitySet::all();
        let current = creds(0, caps(all, all));
        let mut u = user();
        u.groups = vec![3, 991];
        let p = plan(&current, &request(Goal::KeepSysAdmin, Some(u), false)).unwrap();
        assert!(p.ops.contains(&Op::SetGroups(vec![3, 991])));
    }

    #[test]
    fn switch_without_setpcap_uses_keepcaps() {
        let permitted = SYS_ADMIN | CapabilitySet::SETUID | CapabilitySet::SETGID;
        let current = creds(0, caps(SYS_ADMIN, permitted));
        let p = plan(&current, &request(Goal::KeepSysAdmin, Some(user()), false)).unwrap();
        assert_eq!(
            p.ops,
            [
                Op::ClearAmbient,
                Op::SetCaps(caps(permitted, permitted)),
                Op::SetKeepCaps(true),
                Op::SetGroups(vec![991]),
                Op::SetResGid(991),
                Op::SetResUid(991),
                Op::SetKeepCaps(false),
                keep_final(SYS_ADMIN),
                Op::SetNoNewPrivs,
            ]
        );
    }

    #[test]
    fn needed_caps_raised_to_effective_first() {
        let all = CapabilitySet::all();
        let current = creds(0, caps(SYS_ADMIN, all));
        let p = plan(&current, &request(Goal::KeepSysAdmin, Some(user()), false)).unwrap();
        assert_eq!(
            p.ops[1],
            Op::SetCaps(caps(
                SYS_ADMIN | CapabilitySet::SETPCAP | CapabilitySet::SETUID | CapabilitySet::SETGID,
                all
            ))
        );
    }

    #[test]
    fn cannot_switch_without_setuid_or_setgid() {
        let current = creds(0, caps(SYS_ADMIN, SYS_ADMIN | CapabilitySet::SETGID));
        assert_eq!(
            plan(&current, &request(Goal::KeepSysAdmin, Some(user()), false)),
            Err(PlanError::CannotSwitchUser {
                user: "byssus".into(),
                missing: "CAP_SETUID"
            })
        );
        let current = creds(0, caps(SYS_ADMIN, SYS_ADMIN | CapabilitySet::SETUID));
        assert!(matches!(
            plan(&current, &request(Goal::KeepSysAdmin, Some(user()), false)),
            Err(PlanError::CannotSwitchUser {
                missing: "CAP_SETGID",
                ..
            })
        ));
    }

    #[test]
    fn service_user_must_not_be_root() {
        let all = CapabilitySet::all();
        let current = creds(0, caps(all, all));
        let mut u = user();
        u.uid = 0;
        assert_eq!(
            plan(&current, &request(Goal::KeepSysAdmin, Some(u), false)),
            Err(PlanError::ServiceUserIsRoot("byssus".into()))
        );
    }

    #[test]
    fn user_ignored_when_not_root() {
        let current = creds(1000, caps(SYS_ADMIN, SYS_ADMIN));
        let p = plan(&current, &request(Goal::KeepSysAdmin, Some(user()), false)).unwrap();
        assert_eq!(
            p.warnings,
            [Warning::UserNotApplied {
                user: "byssus".into(),
                effective_uid: 1000
            }]
        );
        assert_eq!(p.final_uid, 1000);

        // Already the service user: no warning.
        let current = creds(991, caps(SYS_ADMIN, SYS_ADMIN));
        let p = plan(&current, &request(Goal::KeepSysAdmin, Some(user()), false)).unwrap();
        assert!(p.warnings.is_empty());
    }

    #[test]
    fn drop_all_as_unprivileged_user() {
        let current = creds(1000, caps(CapabilitySet::empty(), CapabilitySet::empty()));
        let p = plan(&current, &request(Goal::DropAll, None, false)).unwrap();
        assert_eq!(
            p.ops,
            [
                Op::ClearAmbient,
                keep_final(CapabilitySet::empty()),
                Op::SetNoNewPrivs
            ]
        );
        assert_eq!(p.final_caps, CapabilitySet::empty());
    }

    #[test]
    fn drop_all_as_root_switches_user_or_stays_root_without_error() {
        let all = CapabilitySet::all();
        let current = creds(0, caps(all, all));
        let p = plan(&current, &request(Goal::DropAll, Some(user()), false)).unwrap();
        assert_eq!(p.final_uid, 991);
        assert!(
            p.ops
                .contains(&Op::DropBoundingExcept(CapabilitySet::empty()))
        );

        let p = plan(&current, &request(Goal::DropAll, None, false)).unwrap();
        assert_eq!(p.final_uid, 0);
        assert!(p.warnings.is_empty());
        assert_eq!(p.ops.last(), Some(&Op::SetNoNewPrivs));
    }

    #[test]
    fn plans_always_end_minimal() {
        let all = CapabilitySet::all();
        for current in [
            creds(0, caps(all, all)),
            creds(991, caps(SYS_ADMIN, SYS_ADMIN)),
            creds(0, caps(SYS_ADMIN, all)),
        ] {
            for user in [None, Some(user())] {
                let Ok(p) = plan(&current, &request(Goal::KeepSysAdmin, user, true)) else {
                    continue;
                };
                assert_eq!(p.ops[0], Op::ClearAmbient);
                assert_eq!(p.ops[p.ops.len() - 2], keep_final(SYS_ADMIN));
                assert_eq!(p.ops[p.ops.len() - 1], Op::SetNoNewPrivs);
            }
        }
    }
}