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

Commit 59d148a

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 silently skipped with a warn-level log and enforcement falls back to the cooperative proxy + NetworkPolicy. Also fixes rules_applied underflow via saturating_sub. Ref: NVIDIA#899 Signed-off-by: Ladislav Smola <[email protected]>
1 parent ec5655e commit 59d148a

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

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

Lines changed: 41 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,14 @@ pub fn prepare(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<Option<P
184184
.handle_access(access_all)
185185
.into_diagnostic()?;
186186

187+
// Platform mode: also handle TCP connect access so Landlock can
188+
// restrict which ports the agent connects to. Requires ABI v4.
189+
if matches!(policy.network.mode, NetworkMode::Platform) {
190+
ruleset = ruleset
191+
.handle_access(AccessNet::ConnectTcp)
192+
.into_diagnostic()?;
193+
}
194+
187195
let mut ruleset = ruleset.create().into_diagnostic()?;
188196
let mut rules_applied: usize = 0;
189197

@@ -207,6 +215,35 @@ pub fn prepare(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<Option<P
207215
}
208216
}
209217

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

218-
let skipped = total_paths - rules_applied;
255+
let skipped = total_paths.saturating_sub(rules_applied);
219256
openshell_ocsf::ocsf_emit!(
220257
openshell_ocsf::ConfigStateChangeBuilder::new(crate::ocsf_ctx())
221258
.severity(openshell_ocsf::SeverityId::Informational)

0 commit comments

Comments
 (0)