Skip to main content

byteflow/scheduler/
link_admin.rs

1//! LINK / MONITOR / ADMIN as real capabilities, not ambient authority.
2//!
3//! Before: LINK/MONITOR took a bare FlowId (or a Cap resolved with empty
4//! rights) and succeeded if the target existed — knowing the id was enough.
5//! Now: the Cap must target that exact flow and carry the matching right.
6
7// Docs ratchet: public items here predate `missing_docs`. Delete this
8// allow once they are documented; new files get the lint by default.
9#![allow(missing_docs)]
10
11use crate::bytecode::{Cap, CapRights, CapTarget, RevocationCell};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum LinkError {
15    WrongTarget,
16    MissingRight,
17    CapRevoked,
18}
19
20impl std::fmt::Display for LinkError {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            LinkError::WrongTarget => f.write_str("cap does not target this flow"),
24            LinkError::MissingRight => f.write_str("cap lacks LINK/MONITOR right"),
25            LinkError::CapRevoked => f.write_str("capability revoked"),
26        }
27    }
28}
29
30impl std::error::Error for LinkError {}
31
32fn check_flow_right(
33    cap: &Cap,
34    cell: &RevocationCell,
35    target: u64,
36    right: CapRights,
37) -> Result<(), LinkError> {
38    if !cap.is_valid(cell) {
39        return Err(LinkError::CapRevoked);
40    }
41    if cap.target() != CapTarget::Flow(target) {
42        return Err(LinkError::WrongTarget);
43    }
44    if !cap.rights().contains(right) {
45        return Err(LinkError::MissingRight);
46    }
47    Ok(())
48}
49
50pub fn check_link(cap: &Cap, cell: &RevocationCell, target: u64) -> Result<(), LinkError> {
51    check_flow_right(cap, cell, target, CapRights::LINK)
52}
53
54pub fn check_monitor(cap: &Cap, cell: &RevocationCell, target: u64) -> Result<(), LinkError> {
55    check_flow_right(cap, cell, target, CapRights::MONITOR)
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub enum AdminError {
60    MissingRight,
61    CapRevoked,
62    WrongTarget,
63}
64
65impl std::fmt::Display for AdminError {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        match self {
68            AdminError::MissingRight => f.write_str("cap lacks ADMIN right"),
69            AdminError::CapRevoked => f.write_str("capability revoked"),
70            AdminError::WrongTarget => f.write_str("ADMIN requires CapTarget::Scheduler"),
71        }
72    }
73}
74
75impl std::error::Error for AdminError {}
76
77/// Scheduler-level ops (kill, inspect, quota top-up) all pass this check.
78pub fn check_admin(cap: &Cap, cell: &RevocationCell) -> Result<(), AdminError> {
79    if !cap.is_valid(cell) {
80        return Err(AdminError::CapRevoked);
81    }
82    if cap.target() != CapTarget::Scheduler {
83        return Err(AdminError::WrongTarget);
84    }
85    if !cap.rights().contains(CapRights::ADMIN) {
86        return Err(AdminError::MissingRight);
87    }
88    Ok(())
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn knowing_flow_id_alone_is_not_enough_to_link() {
97        let cell = RevocationCell::new();
98        let cap = Cap::root(CapTarget::Flow(1), CapRights::SEND, None, &cell);
99        assert!(matches!(
100            check_link(&cap, &cell, 1),
101            Err(LinkError::MissingRight)
102        ));
103        let cap2 = Cap::root(CapTarget::Flow(2), CapRights::LINK, None, &cell);
104        assert!(matches!(
105            check_link(&cap2, &cell, 1),
106            Err(LinkError::WrongTarget)
107        ));
108    }
109
110    #[test]
111    fn admin_requires_scheduler_target_and_right() {
112        let cell = RevocationCell::new();
113        let cap = Cap::root(CapTarget::Scheduler, CapRights::ADMIN, None, &cell);
114        assert!(check_admin(&cap, &cell).is_ok());
115        let cap2 = Cap::root(CapTarget::Flow(1), CapRights::ADMIN, None, &cell);
116        assert!(matches!(
117            check_admin(&cap2, &cell),
118            Err(AdminError::WrongTarget)
119        ));
120    }
121}