use serde::{Deserialize, Serialize};
use super::{EffectiveBoundary, Mechanism, Restriction, Weaker};
pub const WEIGHTS_LINUX: [(Mechanism, u32); 9] = [
(Mechanism::LandlockFilesystem, 25),
(Mechanism::LandlockTcpPort, 10),
(Mechanism::Seccomp, 15),
(Mechanism::NoNewPrivs, 5),
(Mechanism::LandlockScopes, 5),
(Mechanism::LandlockResolveUnix, 5),
(Mechanism::LandlockUdp, 5),
(Mechanism::NetworkNamespace, 15),
(Mechanism::IsolatedView, 15),
];
pub const WEIGHTS_MACOS: [(Mechanism, u32); 1] = [(Mechanism::Seatbelt, 100)];
pub const WEIGHTS_WINDOWS: [(Mechanism, u32); 4] = [
(Mechanism::RestrictedToken, 45),
(Mechanism::JobObject, 10),
(Mechanism::WfpNetwork, 30),
(Mechanism::WindowsReadDeny, 15),
];
#[must_use]
pub fn weights(os: &str) -> &'static [(Mechanism, u32)] {
match os {
"linux" => &WEIGHTS_LINUX,
"macos" => &WEIGHTS_MACOS,
"windows" => &WEIGHTS_WINDOWS,
_ => &[],
}
}
fn built(name: Mechanism) -> bool {
!matches!(name, Mechanism::WfpNetwork | Mechanism::WindowsReadDeny)
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProtectionScore {
pub points: u32,
pub items: Vec<ScoreItem>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScoreItem {
pub name: Mechanism,
pub weight: u32,
pub in_force: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub missing: Option<String>,
}
impl ProtectionScore {
#[must_use]
pub fn complete(&self) -> bool {
!self.items.is_empty() && self.items.iter().all(|i| i.in_force)
}
#[must_use]
pub fn missing(&self) -> Vec<String> {
self.items
.iter()
.filter_map(|i| Some(format!("{} ({})", i.name.id(), i.missing.as_ref()?)))
.collect()
}
#[must_use]
pub fn line(&self) -> String {
let missing = self.missing();
if self.items.is_empty() {
return "0/100: `openmoat run` has no sandbox on this system".to_owned();
}
if missing.is_empty() {
return format!(
"{}/100: every kernel feature it uses is in force",
self.points
);
}
format!("{}/100; missing: {}", self.points, missing.join("; "))
}
}
fn needs(name: Mechanism) -> &'static str {
match name {
Mechanism::LandlockFilesystem | Mechanism::LandlockTcpPort => {
"needs Landlock ABI 4, Linux 6.7"
}
Mechanism::LandlockScopes => "needs Landlock ABI 6, Linux 6.12",
Mechanism::LandlockResolveUnix => "needs Landlock ABI 9, Linux 7.1",
Mechanism::LandlockUdp => {
"needs Landlock ABI 10, Linux 7.2; the seccomp filter still refuses UDP sockets"
}
Mechanism::Seccomp => "needs seccomp",
Mechanism::NoNewPrivs => "needs `no_new_privs`",
Mechanism::NetworkNamespace => {
"needs bubblewrap that can make a user namespace; `openmoat doctor` names the fix"
}
Mechanism::IsolatedView => "needs the Isolated tier: bubblewrap, `openmoat run --isolate`",
Mechanism::Seatbelt => "needs `sandbox-exec`",
Mechanism::RestrictedToken => "needs `CreateRestrictedToken` and `CreateProcessAsUserW`",
Mechanism::JobObject => "needs a job object the agent can be put in",
Mechanism::WfpNetwork => {
"a Windows Filtering Platform filter; until then the network goes through the proxy \
only for programs that honour HTTP_PROXY and HTTPS_PROXY"
}
Mechanism::WindowsReadDeny => {
"a write-restricted token does not restrict reads, so the hook decides every read"
}
}
}
fn confirmed(r: &Restriction) -> bool {
r.name == Mechanism::Seatbelt || r.applied == Some(true) || r.tested == Some(true)
}
pub(super) fn of(record: &EffectiveBoundary) -> ProtectionScore {
let launched = record.launch.is_some();
let failed = record.launch.as_ref().is_some_and(|l| l.failure.is_some());
let tier_chosen_away = record
.downgrades
.iter()
.any(|d| d.what == Weaker::Tier && d.chosen);
let bubblewrap = |name| matches!(name, Mechanism::IsolatedView | Mechanism::NetworkNamespace);
let items: Vec<ScoreItem> = weights(&record.os)
.iter()
.map(|&(name, weight)| {
let missing = match record.restriction(name) {
None => Some("not part of this run's sandbox".to_owned()),
Some(_) if !built(name) => Some(format!(
"not in OpenMoat's Windows tier yet: {}",
needs(name)
)),
Some(r) if !r.available && tier_chosen_away && bubblewrap(name) => {
Some(format!("this run does not ask for it: {}", needs(name)))
}
Some(r) if !r.available => Some(format!("not on this system: {}", needs(name))),
Some(r) if !r.configured => {
Some(format!("this run does not ask for it: {}", needs(name)))
}
Some(r) if r.applied == Some(false) || r.tested == Some(false) => {
Some("reported not in force after the start".to_owned())
}
Some(_) if failed => Some("the start failed".to_owned()),
Some(r) if launched && !confirmed(r) => {
Some("not confirmed in force after the start".to_owned())
}
Some(_) => None,
};
ScoreItem {
name,
weight,
in_force: missing.is_none(),
missing,
}
})
.collect();
let points = items.iter().filter(|i| i.in_force).map(|i| i.weight).sum();
ProtectionScore { points, items }
}