Skip to main content

rvm_types/
capability.rs

1//! Capability types for the RVM access-control model.
2//!
3//! Every resource in RVM is accessed through an unforgeable capability token.
4//! Capabilities carry a type tag and a rights bitmap that constrains the
5//! operations a holder may perform.
6//!
7//! During partition split, capabilities follow the objects they reference
8//! (DC-8). Capabilities referencing shared objects are attenuated to
9//! `READ` only in both new partitions.
10
11use bitflags::bitflags;
12
13bitflags! {
14    /// Access rights bitmap carried by a capability (ADR-132, DC-3/DC-8).
15    ///
16    /// Multiple rights can be combined. The `GRANT_ONCE` right is consumed
17    /// after a single delegation.
18    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19    pub struct CapRights: u8 {
20        /// Permission to read / inspect the resource.
21        const READ       = 0x01;
22        /// Permission to write / mutate the resource.
23        const WRITE      = 0x02;
24        /// Permission to grant (copy) this capability to another partition.
25        const GRANT      = 0x04;
26        /// Permission to revoke derived capabilities.
27        const REVOKE     = 0x08;
28        /// Permission to execute code within the resource's context.
29        const EXECUTE    = 0x10;
30        /// Permission to create a proof referencing this capability.
31        const PROVE      = 0x20;
32        /// One-time grant: capability is consumed after a single delegation.
33        const GRANT_ONCE = 0x40;
34    }
35}
36
37/// The type of resource a capability refers to.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
39#[repr(u8)]
40pub enum CapType {
41    /// Authority over a partition (create, destroy, split, merge).
42    Partition = 0,
43    /// Authority over a memory region (map, transfer, tier change).
44    Region = 1,
45    /// Authority over a communication edge (create, destroy, send).
46    CommEdge = 2,
47    /// Authority over a device lease (grant, revoke, renew).
48    Device = 3,
49    /// Authority over the scheduler (mode switch, priority override).
50    Scheduler = 4,
51    /// Authority over the witness log (query, export).
52    WitnessLog = 5,
53    /// Authority over the proof verifier (escalation, deep proof).
54    Proof = 6,
55    /// Authority over a virtual CPU.
56    Vcpu = 7,
57    /// Authority over a coherence observer.
58    Coherence = 8,
59    /// Authority over a governed `ruv://` context scope.
60    Context = 9,
61}
62
63/// Unique identifier for a capability in the system-wide capability space.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
65#[repr(transparent)]
66pub struct CapabilityId(u64);
67
68impl CapabilityId {
69    /// The root capability (bootstrap authority).
70    pub const ROOT: Self = Self(0);
71
72    /// Create a new capability identifier.
73    #[must_use]
74    pub const fn new(id: u64) -> Self {
75        Self(id)
76    }
77
78    /// Return the raw identifier value.
79    #[must_use]
80    pub const fn as_u64(self) -> u64 {
81        self.0
82    }
83}
84
85/// An unforgeable capability token.
86///
87/// Capability tokens are the sole mechanism for accessing RVM resources.
88/// They are created by the kernel and cannot be forged by partitions.
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
90pub struct CapToken {
91    /// Globally unique identifier for this capability.
92    id: u64,
93    /// The type of resource this capability grants access to.
94    cap_type: CapType,
95    /// Access rights bitmap.
96    rights: CapRights,
97    /// Monotonic epoch for stale-handle detection.
98    epoch: u32,
99}
100
101impl CapToken {
102    /// Create a new capability token.
103    #[must_use]
104    pub const fn new(id: u64, cap_type: CapType, rights: CapRights, epoch: u32) -> Self {
105        Self {
106            id,
107            cap_type,
108            rights,
109            epoch,
110        }
111    }
112
113    /// Return the capability identifier.
114    #[must_use]
115    pub const fn id(self) -> u64 {
116        self.id
117    }
118
119    /// Return the capability type.
120    #[must_use]
121    pub const fn cap_type(self) -> CapType {
122        self.cap_type
123    }
124
125    /// Return the access rights.
126    #[must_use]
127    pub const fn rights(self) -> CapRights {
128        self.rights
129    }
130
131    /// Return the epoch counter.
132    #[must_use]
133    pub const fn epoch(self) -> u32 {
134        self.epoch
135    }
136
137    /// Check whether this token carries the given rights.
138    #[must_use]
139    pub const fn has_rights(self, required: CapRights) -> bool {
140        self.rights.contains(required)
141    }
142
143    /// Return a truncated 32-bit hash for witness record embedding.
144    ///
145    /// This is NOT the full capability -- it is a truncated hash used
146    /// for identification without leaking the full token contents.
147    #[must_use]
148    #[allow(clippy::cast_possible_truncation)]
149    pub const fn truncated_hash(self) -> u32 {
150        // Intentional truncation: mixing 64-bit id into 32-bit hash.
151        let mut h = self.id as u32;
152        h ^= (self.id >> 32) as u32;
153        h ^= self.epoch;
154        h ^= (self.rights.bits() as u32) << 24;
155        h
156    }
157}
158
159/// Unforgeable capability with full delegation metadata.
160///
161/// This is the kernel-internal representation. [`CapToken`] is the
162/// user-visible handle.
163#[derive(Debug, Clone, Copy)]
164pub struct Capability {
165    /// Unique identifier for this capability.
166    pub id: CapabilityId,
167    /// The kernel object this capability authorizes access to.
168    pub object_id: u64,
169    /// Kind of object targeted.
170    pub object_type: CapType,
171    /// Rights granted by this capability.
172    pub rights: CapRights,
173    /// Opaque badge value carried through IPC for endpoint identification.
174    pub badge: u32,
175    /// Epoch in which this capability was created (for revocation ordering).
176    pub epoch: u32,
177    /// Parent capability from which this was derived (`ROOT` = root).
178    pub parent: CapabilityId,
179    /// Current delegation depth (decremented on each grant; 0 = non-delegable).
180    pub delegation_depth: u8,
181}
182
183/// Maximum delegation depth for capabilities (ADR-132).
184///
185/// Limits how many times a capability can be re-granted. Prevents unbounded
186/// authority chains that complicate revocation.
187pub const MAX_DELEGATION_DEPTH: u8 = 8;