veridian_kernel/security/
mod.rs1pub mod audit;
10pub mod audit_enhanced;
11pub mod auth;
12pub mod boot;
13pub mod dilithium;
14pub mod fuzzing;
15pub mod kaslr;
16pub mod mac;
17pub mod memory_protection;
18pub mod smep_smap;
19pub mod spectre;
20pub mod stack_canary;
21pub mod tpm;
22pub mod tpm_commands;
23
24use crate::error::KernelError;
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub struct SecurityContext {
29 pub label: &'static str,
31 pub uid: u32,
33 pub gid: u32,
35 pub level: SecurityLevel,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
41pub enum SecurityLevel {
42 Unclassified = 0,
43 Confidential = 1,
44 Secret = 2,
45 TopSecret = 3,
46}
47
48impl SecurityContext {
49 pub const fn new(label: &'static str, uid: u32, gid: u32, level: SecurityLevel) -> Self {
51 Self {
52 label,
53 uid,
54 gid,
55 level,
56 }
57 }
58
59 pub fn can_access(&self, target: &SecurityContext, access: AccessType) -> bool {
61 if !mac::check_access(self.label, target.label, access) {
63 return false;
64 }
65
66 match access {
68 AccessType::Read => self.level >= target.level,
69 AccessType::Write => self.level <= target.level,
70 AccessType::Execute => self.level == target.level,
71 }
72 }
73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77pub enum AccessType {
78 Read,
79 Write,
80 Execute,
81}
82
83pub fn init() -> Result<(), KernelError> {
85 kprintln!("[SECURITY] Initializing security subsystem...");
86
87 memory_protection::init()?;
88 kprintln!("[SECURITY] memory_protection done");
89
90 auth::init()?;
91 kprintln!("[SECURITY] auth done");
92
93 tpm::init()?;
94 kprintln!("[SECURITY] tpm done");
95
96 mac::init()?;
97 kprintln!("[SECURITY] mac done");
98
99 audit::init()?;
100 kprintln!("[SECURITY] audit done");
101
102 boot::verify()?;
103 kprintln!("[SECURITY] boot verify done");
104
105 smep_smap::init()?;
106 kprintln!("[SECURITY] smep_smap done");
107
108 spectre::init()?;
109 kprintln!("[SECURITY] spectre done");
110
111 kaslr::init()?;
112 kprintln!("[SECURITY] kaslr done");
113
114 stack_canary::init()?;
115 kprintln!("[SECURITY] stack_canary done");
116
117 kprintln!("[SECURITY] Security subsystem initialized successfully");
118 Ok(())
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
126 fn test_security_context() {
127 let ctx1 = SecurityContext::new("user_t", 1000, 1000, SecurityLevel::Unclassified);
128 let ctx2 = SecurityContext::new("system_t", 0, 0, SecurityLevel::Secret);
129
130 assert!(ctx1.can_access(&ctx1, AccessType::Read));
132
133 assert!(!ctx1.can_access(&ctx2, AccessType::Read));
135
136 assert!(ctx2.can_access(&ctx1, AccessType::Read));
138 }
139}