Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions crates/openshell-sandbox/src/sandbox/linux/landlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

//! Landlock filesystem sandboxing.

use crate::policy::{LandlockCompatibility, SandboxPolicy};
use crate::policy::{LandlockCompatibility, NetworkMode, SandboxPolicy};
use landlock::{
ABI, Access, AccessFs, CompatLevel, Compatible, PathBeneath, PathFd, PathFdError, Ruleset,
RulesetAttr, RulesetCreatedAttr,
ABI, Access, AccessFs, AccessNet, CompatLevel, Compatible, NetPort, PathBeneath, PathFd,
PathFdError, Ruleset, RulesetAttr, RulesetCreatedAttr,
};
use miette::{IntoDiagnostic, Result};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -184,6 +184,12 @@ pub fn prepare(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<Option<P
.handle_access(access_all)
.into_diagnostic()?;

if matches!(policy.network.mode, NetworkMode::Platform) {
ruleset = ruleset
.handle_access(AccessNet::ConnectTcp)
.into_diagnostic()?;
}

let mut ruleset = ruleset.create().into_diagnostic()?;
let mut rules_applied: usize = 0;

Expand All @@ -207,6 +213,34 @@ pub fn prepare(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<Option<P
}
}

if matches!(policy.network.mode, NetworkMode::Platform) {
let proxy_port = policy
.network
.proxy
.as_ref()
.and_then(|p| p.http_addr)
.map_or(3128_u16, |addr| addr.port());

match ruleset.add_rule(NetPort::new(proxy_port, AccessNet::ConnectTcp)) {
Ok(r) => {
ruleset = r;
debug!(
port = proxy_port,
"Landlock allow TCP connect (proxy port only)"
);
rules_applied += 1;
}
Err(e) => {
tracing::warn!(
error = %e,
"Landlock TCP port restriction unavailable (ABI v4 required). \
Network enforcement degraded: agent can bypass proxy via direct \
connect(). Upgrade to RHEL 9.6+ for kernel-enforced TCP port restriction."
);
}
}
}

if rules_applied == 0 {
return Err(miette::miette!(
"Landlock ruleset has zero valid paths — all {} path(s) failed to open. \
Expand All @@ -215,7 +249,7 @@ pub fn prepare(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<Option<P
));
}

let skipped = total_paths - rules_applied;
let skipped = total_paths.saturating_sub(rules_applied);
openshell_ocsf::ocsf_emit!(
openshell_ocsf::ConfigStateChangeBuilder::new(crate::ocsf_ctx())
.severity(openshell_ocsf::SeverityId::Informational)
Expand Down