1use serde::{Deserialize, Serialize};
14
15use super::{EffectiveBoundary, Mechanism, Restriction, Weaker};
16
17pub const WEIGHTS_LINUX: [(Mechanism, u32); 9] = [
21 (Mechanism::LandlockFilesystem, 25),
22 (Mechanism::LandlockTcpPort, 10),
23 (Mechanism::Seccomp, 15),
24 (Mechanism::NoNewPrivs, 5),
25 (Mechanism::LandlockScopes, 5),
26 (Mechanism::LandlockResolveUnix, 5),
27 (Mechanism::LandlockUdp, 5),
28 (Mechanism::NetworkNamespace, 15),
29 (Mechanism::IsolatedView, 15),
30];
31
32pub const WEIGHTS_MACOS: [(Mechanism, u32); 1] = [(Mechanism::Seatbelt, 100)];
35
36pub const WEIGHTS_WINDOWS: [(Mechanism, u32); 4] = [
42 (Mechanism::RestrictedToken, 45),
43 (Mechanism::JobObject, 10),
44 (Mechanism::WfpNetwork, 30),
45 (Mechanism::WindowsReadDeny, 15),
46];
47
48#[must_use]
51pub fn weights(os: &str) -> &'static [(Mechanism, u32)] {
52 match os {
53 "linux" => &WEIGHTS_LINUX,
54 "macos" => &WEIGHTS_MACOS,
55 "windows" => &WEIGHTS_WINDOWS,
56 _ => &[],
57 }
58}
59
60fn built(name: Mechanism) -> bool {
64 !matches!(name, Mechanism::WfpNetwork | Mechanism::WindowsReadDeny)
65}
66
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
69pub struct ProtectionScore {
70 pub points: u32,
72 pub items: Vec<ScoreItem>,
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
78pub struct ScoreItem {
79 pub name: Mechanism,
81 pub weight: u32,
83 pub in_force: bool,
85 #[serde(default, skip_serializing_if = "Option::is_none")]
87 pub missing: Option<String>,
88}
89
90impl ProtectionScore {
91 #[must_use]
95 pub fn complete(&self) -> bool {
96 !self.items.is_empty() && self.items.iter().all(|i| i.in_force)
97 }
98
99 #[must_use]
101 pub fn missing(&self) -> Vec<String> {
102 self.items
103 .iter()
104 .filter_map(|i| Some(format!("{} ({})", i.name.id(), i.missing.as_ref()?)))
105 .collect()
106 }
107
108 #[must_use]
110 pub fn line(&self) -> String {
111 let missing = self.missing();
112 if self.items.is_empty() {
113 return "0/100: `openmoat run` has no sandbox on this system".to_owned();
114 }
115 if missing.is_empty() {
116 return format!(
117 "{}/100: every kernel feature it uses is in force",
118 self.points
119 );
120 }
121 format!("{}/100; missing: {}", self.points, missing.join("; "))
122 }
123}
124
125fn needs(name: Mechanism) -> &'static str {
127 match name {
128 Mechanism::LandlockFilesystem | Mechanism::LandlockTcpPort => {
129 "needs Landlock ABI 4, Linux 6.7"
130 }
131 Mechanism::LandlockScopes => "needs Landlock ABI 6, Linux 6.12",
132 Mechanism::LandlockResolveUnix => "needs Landlock ABI 9, Linux 7.1",
133 Mechanism::LandlockUdp => {
134 "needs Landlock ABI 10, Linux 7.2; the seccomp filter still refuses UDP sockets"
135 }
136 Mechanism::Seccomp => "needs seccomp",
137 Mechanism::NoNewPrivs => "needs `no_new_privs`",
138 Mechanism::NetworkNamespace => {
139 "needs bubblewrap that can make a user namespace; `openmoat doctor` names the fix"
140 }
141 Mechanism::IsolatedView => "needs the Isolated tier: bubblewrap, `openmoat run --isolate`",
142 Mechanism::Seatbelt => "needs `sandbox-exec`",
143 Mechanism::RestrictedToken => "needs `CreateRestrictedToken` and `CreateProcessAsUserW`",
144 Mechanism::JobObject => "needs a job object the agent can be put in",
145 Mechanism::WfpNetwork => {
146 "a Windows Filtering Platform filter; until then the network goes through the proxy \
147 only for programs that honour HTTP_PROXY and HTTPS_PROXY"
148 }
149 Mechanism::WindowsReadDeny => {
150 "a write-restricted token does not restrict reads, so the hook decides every read"
151 }
152 }
153}
154
155fn confirmed(r: &Restriction) -> bool {
159 r.name == Mechanism::Seatbelt || r.applied == Some(true) || r.tested == Some(true)
160}
161
162pub(super) fn of(record: &EffectiveBoundary) -> ProtectionScore {
164 let launched = record.launch.is_some();
165 let failed = record.launch.as_ref().is_some_and(|l| l.failure.is_some());
166 let tier_chosen_away = record
167 .downgrades
168 .iter()
169 .any(|d| d.what == Weaker::Tier && d.chosen);
170 let bubblewrap = |name| matches!(name, Mechanism::IsolatedView | Mechanism::NetworkNamespace);
171 let items: Vec<ScoreItem> = weights(&record.os)
172 .iter()
173 .map(|&(name, weight)| {
174 let missing = match record.restriction(name) {
175 None => Some("not part of this run's sandbox".to_owned()),
176 Some(_) if !built(name) => Some(format!(
177 "not in OpenMoat's Windows tier yet: {}",
178 needs(name)
179 )),
180 Some(r) if !r.available && tier_chosen_away && bubblewrap(name) => {
182 Some(format!("this run does not ask for it: {}", needs(name)))
183 }
184 Some(r) if !r.available => Some(format!("not on this system: {}", needs(name))),
185 Some(r) if !r.configured => {
186 Some(format!("this run does not ask for it: {}", needs(name)))
187 }
188 Some(r) if r.applied == Some(false) || r.tested == Some(false) => {
189 Some("reported not in force after the start".to_owned())
190 }
191 Some(_) if failed => Some("the start failed".to_owned()),
192 Some(r) if launched && !confirmed(r) => {
193 Some("not confirmed in force after the start".to_owned())
194 }
195 Some(_) => None,
196 };
197 ScoreItem {
198 name,
199 weight,
200 in_force: missing.is_none(),
201 missing,
202 }
203 })
204 .collect();
205 let points = items.iter().filter(|i| i.in_force).map(|i| i.weight).sum();
206 ProtectionScore { points, items }
207}