Skip to content
This repository was archived by the owner on Nov 12, 2026. It is now read-only.

Commit 8354d68

Browse files
committed
feat(sandbox): Landlock TCP port restriction in Platform mode
When Platform mode is active, apply Landlock ABI v4 network rules to restrict TCP connect to only the proxy port (default 3128). This makes the loopback CONNECT proxy mandatory at the kernel level. Graceful degradation: if kernel ABI < v4, the network rules are skipped with a warn-level log. Also fixes rules_applied underflow via saturating_sub. Ref: NVIDIA#899 Signed-off-by: Ladislav Smola <[email protected]>
1 parent 7158b3d commit 8354d68

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

‎crates/openshell-sandbox/src/sandbox/linux/landlock.rs‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
//! Landlock filesystem sandboxing.
55
6-
use crate::policy::{LandlockCompatibility, SandboxPolicy};
6+
use crate::policy::{LandlockCompatibility, NetworkMode, SandboxPolicy};
77
use landlock::{
8-
ABI, Access, AccessFs, CompatLevel, Compatible, PathBeneath, PathFd, PathFdError, Ruleset,
9-
RulesetAttr, RulesetCreatedAttr,
8+
ABI, Access, AccessFs, AccessNet, CompatLevel, Compatible, NetPort, PathBeneath, PathFd,
9+
PathFdError, Ruleset, RulesetAttr, RulesetCreatedAttr,
1010
};
1111
use miette::{IntoDiagnostic, Result};
1212
use std::path::{Path, PathBuf};
@@ -184,6 +184,12 @@ pub fn prepare(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<Option<P
184184
.handle_access(access_all)
185185
.into_diagnostic()?;
186186

187+
if matches!(policy.network.mode, NetworkMode::Platform) {
188+
ruleset = ruleset
189+
.handle_access(AccessNet::ConnectTcp)
190+
.into_diagnostic()?;
191+
}
192+
187193
let mut ruleset = ruleset.create().into_diagnostic()?;
188194
let mut rules_applied: usize = 0;
189195

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

216+
if matches!(policy.network.mode, NetworkMode::Platform) {
217+
let proxy_port = policy
218+
.network
219+
.proxy
220+
.as_ref()
221+
.and_then(|p| p.http_addr)
222+
.map_or(3128_u16, |addr| addr.port());
223+
224+
match ruleset.add_rule(NetPort::new(proxy_port, AccessNet::ConnectTcp)) {
225+
Ok(r) => {
226+
ruleset = r;
227+
debug!(
228+
port = proxy_port,
229+
"Landlock allow TCP connect (proxy port only)"
230+
);
231+
rules_applied += 1;
232+
}
233+
Err(e) => {
234+
tracing::warn!(
235+
error = %e,
236+
"Landlock TCP port restriction unavailable (ABI v4 required). \
237+
Network enforcement degraded: agent can bypass proxy via direct \
238+
connect(). Upgrade to RHEL 9.6+ for kernel-enforced TCP port restriction."
239+
);
240+
}
241+
}
242+
}
243+
210244
if rules_applied == 0 {
211245
return Err(miette::miette!(
212246
"Landlock ruleset has zero valid paths — all {} path(s) failed to open. \
@@ -215,7 +249,7 @@ pub fn prepare(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<Option<P
215249
));
216250
}
217251

218-
let skipped = total_paths - rules_applied;
252+
let skipped = total_paths.saturating_sub(rules_applied);
219253
openshell_ocsf::ocsf_emit!(
220254
openshell_ocsf::ConfigStateChangeBuilder::new(crate::ocsf_ctx())
221255
.severity(openshell_ocsf::SeverityId::Informational)

0 commit comments

Comments
 (0)