ruvix-cap 0.1.0

seL4-inspired capability management for the RuVix Cognition Kernel (ADR-087)
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
580
581
582
583
584
585
586
587
588
589
590
591
//! Main capability manager implementation.
//!
//! The `CapabilityManager` coordinates the capability table and derivation
//! tree to provide complete capability lifecycle management.

use crate::derivation::DerivationTree;
use crate::error::{CapError, CapResult};
use crate::grant::{validate_grant, GrantRequest};
use crate::revoke::{validate_revoke, RevokeRequest, RevokeResult};
#[cfg(feature = "alloc")]
use crate::revoke::RevokeStats;
use crate::table::{CapTableEntry, CapabilityTable};
use crate::{DEFAULT_CAP_TABLE_CAPACITY, DEFAULT_MAX_DELEGATION_DEPTH};
use ruvix_types::{CapHandle, CapRights, Capability, ObjectType, TaskHandle};

/// Configuration for the capability manager.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CapManagerConfig {
    /// Maximum delegation depth (default: 8).
    pub max_delegation_depth: u8,

    /// Whether to track derivation chains (for revocation propagation).
    pub track_derivation: bool,

    /// Global epoch for capability invalidation.
    pub initial_epoch: u64,
}

impl CapManagerConfig {
    /// Creates a new configuration with default values.
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Self {
            max_delegation_depth: DEFAULT_MAX_DELEGATION_DEPTH,
            track_derivation: true,
            initial_epoch: 0,
        }
    }

    /// Sets a custom maximum delegation depth.
    #[inline]
    #[must_use]
    pub const fn with_max_depth(mut self, depth: u8) -> Self {
        self.max_delegation_depth = depth;
        self
    }

    /// Disables derivation tracking (for performance, disables revocation propagation).
    #[inline]
    #[must_use]
    pub const fn without_derivation_tracking(mut self) -> Self {
        self.track_derivation = false;
        self
    }
}

impl Default for CapManagerConfig {
    fn default() -> Self {
        Self::new()
    }
}

/// The main capability manager.
///
/// Coordinates capability table and derivation tree to provide
/// complete capability lifecycle management including creation,
/// granting, and revocation.
pub struct CapabilityManager<const N: usize = DEFAULT_CAP_TABLE_CAPACITY> {
    /// The capability table storing all capabilities.
    table: CapabilityTable<N>,

    /// Derivation tree for revocation propagation.
    derivation: DerivationTree<N>,

    /// Configuration.
    config: CapManagerConfig,

    /// Current global epoch.
    epoch: u64,

    /// Statistics for debugging and monitoring.
    stats: ManagerStats,
}

/// Statistics about capability manager operations.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ManagerStats {
    /// Total capabilities created.
    pub caps_created: u64,

    /// Total capabilities granted (derived).
    pub caps_granted: u64,

    /// Total capabilities revoked.
    pub caps_revoked: u64,

    /// Total revoke operations.
    pub revoke_operations: u64,

    /// Maximum depth reached in derivation tree.
    pub max_depth_reached: u8,
}

impl<const N: usize> CapabilityManager<N> {
    /// Creates a new capability manager with the given configuration.
    #[inline]
    #[must_use]
    pub const fn new(config: CapManagerConfig) -> Self {
        Self {
            table: CapabilityTable::new(),
            derivation: DerivationTree::new(),
            epoch: config.initial_epoch,
            config,
            stats: ManagerStats {
                caps_created: 0,
                caps_granted: 0,
                caps_revoked: 0,
                revoke_operations: 0,
                max_depth_reached: 0,
            },
        }
    }

    /// Creates a new capability manager with default configuration.
    #[inline]
    #[must_use]
    pub const fn with_defaults() -> Self {
        Self::new(CapManagerConfig::new())
    }

    /// Returns the current configuration.
    #[inline]
    #[must_use]
    pub const fn config(&self) -> &CapManagerConfig {
        &self.config
    }

    /// Returns the current statistics.
    #[inline]
    #[must_use]
    pub const fn stats(&self) -> &ManagerStats {
        &self.stats
    }

    /// Returns the current epoch.
    #[inline]
    #[must_use]
    pub const fn epoch(&self) -> u64 {
        self.epoch
    }

    /// Returns the number of active capabilities.
    #[inline]
    #[must_use]
    pub const fn len(&self) -> usize {
        self.table.len()
    }

    /// Returns true if there are no active capabilities.
    #[inline]
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.table.is_empty()
    }

    /// Increments the global epoch, invalidating all capabilities.
    #[inline]
    pub fn increment_epoch(&mut self) {
        self.epoch = self.epoch.wrapping_add(1);
    }

    /// Creates a root capability for a new kernel object.
    ///
    /// Root capabilities have full rights (ALL) and are at depth 0.
    /// They are typically created when a new kernel object is allocated.
    pub fn create_root_capability(
        &mut self,
        object_id: u64,
        object_type: ObjectType,
        badge: u64,
        owner: TaskHandle,
    ) -> CapResult<CapHandle> {
        let capability = Capability::new(
            object_id,
            object_type,
            CapRights::ALL,
            badge,
            self.epoch,
        );

        let handle = self.table.allocate_root(capability, owner)?;

        if self.config.track_derivation {
            self.derivation.add_root(handle)?;
        }

        self.stats.caps_created += 1;
        Ok(handle)
    }

    /// Creates a root capability with specific rights (not ALL).
    ///
    /// Used when the kernel needs to create a capability with restricted
    /// rights from the start (e.g., read-only memory regions).
    pub fn create_root_capability_with_rights(
        &mut self,
        object_id: u64,
        object_type: ObjectType,
        rights: CapRights,
        badge: u64,
        owner: TaskHandle,
    ) -> CapResult<CapHandle> {
        let capability = Capability::new(
            object_id,
            object_type,
            rights,
            badge,
            self.epoch,
        );

        let handle = self.table.allocate_root(capability, owner)?;

        if self.config.track_derivation {
            self.derivation.add_root(handle)?;
        }

        self.stats.caps_created += 1;
        Ok(handle)
    }

    /// Grants a derived capability to another task.
    ///
    /// Implements the cap_grant syscall semantics from ADR-087 Section 6.2:
    /// - The source capability must have GRANT or GRANT_ONCE right
    /// - Granted rights must be a subset of source rights
    /// - Delegation depth is enforced
    /// - GRANT_ONCE prevents further delegation
    pub fn grant(
        &mut self,
        source_handle: CapHandle,
        rights: CapRights,
        badge: u64,
        _caller: TaskHandle,
        target: TaskHandle,
    ) -> CapResult<CapHandle> {
        // Look up source capability
        let source_entry = self.table.lookup(source_handle)?;

        // Validate the grant
        let request = GrantRequest::new(source_handle, rights, badge)
            .with_max_depth(self.config.max_delegation_depth);

        let grant_result = validate_grant(source_entry, &request)?;

        // Allocate the derived capability in the target's table
        let derived_handle = self.table.allocate_derived(
            grant_result.capability,
            target,
            grant_result.depth,
            source_handle,
        )?;

        // Track in derivation tree
        if self.config.track_derivation {
            self.derivation.add_child(source_handle, derived_handle, grant_result.depth)?;
        }

        // Update statistics
        self.stats.caps_granted += 1;
        if grant_result.depth > self.stats.max_depth_reached {
            self.stats.max_depth_reached = grant_result.depth;
        }

        Ok(derived_handle)
    }

    /// Revokes a capability and all its descendants.
    ///
    /// When a capability is revoked:
    /// 1. The capability itself is invalidated
    /// 2. All derived capabilities are recursively invalidated
    /// 3. The derivation tree is pruned
    pub fn revoke(&mut self, handle: CapHandle, _request: RevokeRequest) -> CapResult<RevokeResult> {
        // Validate revocation
        let entry = self.table.lookup(handle)?;
        validate_revoke(entry)?;

        // Revoke in derivation tree (this handles descendants)
        let revoked_count = if self.config.track_derivation {
            self.derivation.revoke(handle)?
        } else {
            // Without derivation tracking, only revoke the single capability
            self.table.deallocate(handle)?;
            1
        };

        // Deallocate from table (derivation tree marks as invalid but we need
        // to update the table too for all affected handles)
        // Note: The derivation tree's revoke_subtree already marks nodes invalid,
        // but we need to synchronize with the table
        self.deallocate_revoked_caps(handle)?;

        // Update statistics
        self.stats.caps_revoked += revoked_count as u64;
        self.stats.revoke_operations += 1;

        Ok(RevokeResult::new(revoked_count))
    }

    /// Deallocates capabilities that have been marked as revoked in the derivation tree.
    fn deallocate_revoked_caps(&mut self, handle: CapHandle) -> CapResult<()> {
        // Deallocate the primary handle
        // Ignore errors since the derivation tree may have already invalidated it
        let _ = self.table.deallocate(handle);
        Ok(())
    }

    /// Revokes a capability without propagation (target only).
    pub fn revoke_single(&mut self, handle: CapHandle) -> CapResult<()> {
        let entry = self.table.lookup(handle)?;
        validate_revoke(entry)?;

        self.table.deallocate(handle)?;

        // Note: This leaves orphaned children in the derivation tree.
        // They will become invalid when their parent is looked up.

        self.stats.caps_revoked += 1;
        self.stats.revoke_operations += 1;

        Ok(())
    }

    /// Looks up a capability by handle.
    pub fn lookup(&self, handle: CapHandle) -> CapResult<&CapTableEntry> {
        let entry = self.table.lookup(handle)?;

        // Check epoch
        if entry.capability.epoch != self.epoch {
            return Err(CapError::Revoked);
        }

        // Check derivation validity (if tracking)
        if self.config.track_derivation && !self.derivation.is_valid(handle) {
            return Err(CapError::Revoked);
        }

        Ok(entry)
    }

    /// Checks if a handle points to a valid capability.
    pub fn is_valid(&self, handle: CapHandle) -> bool {
        self.lookup(handle).is_ok()
    }

    /// Checks if a task has a specific right on a capability.
    pub fn has_right(&self, handle: CapHandle, right: CapRights) -> CapResult<bool> {
        let entry = self.lookup(handle)?;
        Ok(entry.capability.rights.contains(right))
    }

    /// Checks multiple rights at once.
    pub fn has_rights(&self, handle: CapHandle, rights: CapRights) -> CapResult<bool> {
        let entry = self.lookup(handle)?;
        Ok(entry.capability.rights.contains(rights))
    }

    /// Gets the derivation depth of a capability.
    pub fn depth(&self, handle: CapHandle) -> CapResult<u8> {
        let entry = self.lookup(handle)?;
        Ok(entry.depth)
    }

    /// Returns revocation statistics for a potential revoke operation.
    #[cfg(feature = "alloc")]
    pub fn preview_revoke(&self, handle: CapHandle) -> CapResult<RevokeStats> {
        let entry = self.table.lookup(handle)?;

        if !self.config.track_derivation {
            return Ok(RevokeStats {
                roots_revoked: 1,
                derived_revoked: 0,
                max_depth_reached: entry.depth,
            });
        }

        let descendants = self.derivation.collect_descendants(handle);
        let derived_count = descendants.len().saturating_sub(1);

        Ok(RevokeStats {
            roots_revoked: 1,
            derived_revoked: derived_count,
            max_depth_reached: entry.depth,
        })
    }

    /// Returns an iterator over all valid capabilities.
    pub fn iter(&self) -> impl Iterator<Item = (CapHandle, &CapTableEntry)> {
        self.table.iter().filter(|(h, _)| {
            !self.config.track_derivation || self.derivation.is_valid(*h)
        })
    }
}

impl<const N: usize> Default for CapabilityManager<N> {
    fn default() -> Self {
        Self::with_defaults()
    }
}

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

    #[test]
    fn test_create_root_capability() {
        let mut manager = CapabilityManager::<64>::with_defaults();
        let owner = TaskHandle::new(1, 0);

        let handle = manager.create_root_capability(
            0x1000,
            ObjectType::VectorStore,
            42,
            owner,
        ).unwrap();

        assert_eq!(manager.len(), 1);

        let entry = manager.lookup(handle).unwrap();
        assert_eq!(entry.capability.object_id, 0x1000);
        assert_eq!(entry.capability.rights, CapRights::ALL);
        assert_eq!(entry.depth, 0);
    }

    #[test]
    fn test_grant_capability() {
        let mut manager = CapabilityManager::<64>::with_defaults();
        let owner = TaskHandle::new(1, 0);
        let target = TaskHandle::new(2, 0);

        let root_handle = manager.create_root_capability(
            0x1000,
            ObjectType::Region,
            0,
            owner,
        ).unwrap();

        let derived_handle = manager.grant(
            root_handle,
            CapRights::READ | CapRights::WRITE,
            100,
            owner,
            target,
        ).unwrap();

        assert_eq!(manager.len(), 2);

        let derived_entry = manager.lookup(derived_handle).unwrap();
        assert!(derived_entry.capability.rights.contains(CapRights::READ));
        assert!(derived_entry.capability.rights.contains(CapRights::WRITE));
        assert!(!derived_entry.capability.rights.contains(CapRights::GRANT));
        assert_eq!(derived_entry.depth, 1);
    }

    #[test]
    fn test_revoke_propagation() {
        let mut manager = CapabilityManager::<64>::with_defaults();
        let owner = TaskHandle::new(1, 0);
        let target1 = TaskHandle::new(2, 0);
        let target2 = TaskHandle::new(3, 0);

        // Create root capability
        let root = manager.create_root_capability(
            0x1000,
            ObjectType::Queue,
            0,
            owner,
        ).unwrap();

        // Grant to target1
        let child1 = manager.grant(
            root,
            CapRights::READ | CapRights::GRANT,
            1,
            owner,
            target1,
        ).unwrap();

        // Grant from child1 to target2
        let grandchild = manager.grant(
            child1,
            CapRights::READ,
            2,
            target1,
            target2,
        ).unwrap();

        assert_eq!(manager.len(), 3);

        // Revoke root - should revoke all descendants
        let result = manager.revoke(root, RevokeRequest::new()).unwrap();
        assert_eq!(result.revoked_count, 3);

        // All should be invalid now
        assert!(!manager.is_valid(root));
        assert!(!manager.is_valid(child1));
        assert!(!manager.is_valid(grandchild));
    }

    #[test]
    fn test_delegation_depth_limit() {
        let config = CapManagerConfig::new().with_max_depth(2);
        let mut manager = CapabilityManager::<64>::new(config);
        let owner = TaskHandle::new(1, 0);

        // Create chain: root -> d1 -> d2 -> d3 (should fail)
        let root = manager.create_root_capability(
            0x1000,
            ObjectType::Timer,
            0,
            owner,
        ).unwrap();

        let d1 = manager.grant(
            root,
            CapRights::READ | CapRights::GRANT,
            1,
            owner,
            owner,
        ).unwrap();

        let d2 = manager.grant(
            d1,
            CapRights::READ | CapRights::GRANT,
            2,
            owner,
            owner,
        ).unwrap();

        // This should fail - depth limit exceeded
        let result = manager.grant(
            d2,
            CapRights::READ,
            3,
            owner,
            owner,
        );

        assert_eq!(result, Err(CapError::DelegationDepthExceeded));
    }

    #[test]
    fn test_has_right() {
        let mut manager = CapabilityManager::<64>::with_defaults();
        let owner = TaskHandle::new(1, 0);

        let handle = manager.create_root_capability_with_rights(
            0x1000,
            ObjectType::Region,
            CapRights::READ | CapRights::WRITE,
            0,
            owner,
        ).unwrap();

        assert!(manager.has_right(handle, CapRights::READ).unwrap());
        assert!(manager.has_right(handle, CapRights::WRITE).unwrap());
        assert!(!manager.has_right(handle, CapRights::EXECUTE).unwrap());
    }

    #[test]
    fn test_epoch_invalidation() {
        let mut manager = CapabilityManager::<64>::with_defaults();
        let owner = TaskHandle::new(1, 0);

        let handle = manager.create_root_capability(
            0x1000,
            ObjectType::VectorStore,
            0,
            owner,
        ).unwrap();

        assert!(manager.is_valid(handle));

        // Increment epoch
        manager.increment_epoch();

        // Old capability should now be invalid (epoch mismatch)
        assert!(!manager.is_valid(handle));
    }
}