Skip to content

Commit 69c3309

Browse files
committed
fix(sandbox): inject baseline filesystem paths for proxy-mode sandboxes
Proxy-mode sandboxes need baseline filesystem paths (/usr, /lib, /etc, /app, /var/log read-only; /sandbox, /tmp read-write) for the child process to function under Landlock. Without these, the child can't exec binaries, resolve DNS, or load shared libraries. The supervisor now enriches the policy with these baseline paths at startup, covering both standalone (file) and gateway (gRPC) modes. For gateway mode, the enriched policy is synced back so users see the effective policy via 'nemoclaw sandbox get'. The gateway validation is relaxed to allow additive filesystem changes (new paths can be added, existing paths cannot be removed) to support the supervisor's enrichment sync-back. Includes 2 E2E tests: BFS-1 (missing filesystem_policy) and BFS-2 (incomplete filesystem_policy). Fixes #161
1 parent 9569123 commit 69c3309

4 files changed

Lines changed: 395 additions & 14 deletions

File tree

‎crates/navigator-sandbox/src/grpc_client.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ pub async fn discover_and_sync_policy(
160160
})
161161
}
162162

163+
/// Sync an enriched policy back to the gateway.
164+
///
165+
/// Used by the supervisor to push baseline-path-enriched policies so the
166+
/// gateway stores the effective policy users see via `nemoclaw sandbox get`.
167+
pub async fn sync_policy(endpoint: &str, sandbox: &str, policy: &ProtoSandboxPolicy) -> Result<()> {
168+
debug!(endpoint = %endpoint, sandbox = %sandbox, "Syncing enriched policy to gateway");
169+
let mut client = connect(endpoint).await?;
170+
sync_policy_with_client(&mut client, sandbox, policy).await
171+
}
172+
163173
/// Fetch provider environment variables for a sandbox from NemoClaw server via gRPC.
164174
///
165175
/// Returns a map of environment variable names to values derived from provider

‎crates/navigator-sandbox/src/lib.rs‎

Lines changed: 109 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub async fn run_sandbox(
137137

138138
// Load policy and initialize OPA engine
139139
let navigator_endpoint_for_proxy = navigator_endpoint.clone();
140-
let (mut policy, opa_engine) = load_policy(
140+
let (policy, opa_engine) = load_policy(
141141
sandbox_id.clone(),
142142
sandbox,
143143
navigator_endpoint.clone(),
@@ -180,8 +180,9 @@ pub async fn run_sandbox(
180180
let tls_dir = std::path::Path::new("/etc/navigator-tls");
181181
match write_ca_files(&ca, tls_dir) {
182182
Ok(paths) => {
183-
// Make the TLS directory readable under Landlock
184-
policy.filesystem.read_only.push(tls_dir.to_path_buf());
183+
// /etc/navigator-tls is subsumed by the /etc baseline
184+
// path injected by enrich_*_baseline_paths(), so no
185+
// explicit Landlock entry is needed here.
185186

186187
let upstream_config = build_upstream_client_config();
187188
let cert_cache = CertCache::new(ca);
@@ -680,6 +681,87 @@ fn spawn_route_refresh(
680681
});
681682
}
682683

684+
// ============================================================================
685+
// Baseline filesystem path enrichment
686+
// ============================================================================
687+
688+
/// Minimum read-only paths required for a proxy-mode sandbox child process to
689+
/// function: dynamic linker, shared libraries, DNS resolution, CA certs,
690+
/// Python venv, and navigator logs.
691+
const PROXY_BASELINE_READ_ONLY: &[&str] = &["/usr", "/lib", "/etc", "/app", "/var/log"];
692+
693+
/// Minimum read-write paths required for a proxy-mode sandbox child process:
694+
/// user working directory and temporary files.
695+
const PROXY_BASELINE_READ_WRITE: &[&str] = &["/sandbox", "/tmp"];
696+
697+
/// Ensure a proto `SandboxPolicy` includes the baseline filesystem paths
698+
/// required for proxy-mode sandboxes. Paths are only added if missing;
699+
/// user-specified paths are never removed.
700+
///
701+
/// Returns `true` if the policy was modified (caller may want to sync back).
702+
fn enrich_proto_baseline_paths(proto: &mut navigator_core::proto::SandboxPolicy) -> bool {
703+
// Only enrich if network_policies are present (proxy mode indicator).
704+
if proto.network_policies.is_empty() {
705+
return false;
706+
}
707+
708+
let fs = proto
709+
.filesystem
710+
.get_or_insert_with(|| navigator_core::proto::FilesystemPolicy {
711+
include_workdir: true,
712+
..Default::default()
713+
});
714+
715+
let mut modified = false;
716+
for &path in PROXY_BASELINE_READ_ONLY {
717+
if !fs.read_only.iter().any(|p| p.as_str() == path) {
718+
fs.read_only.push(path.to_string());
719+
modified = true;
720+
}
721+
}
722+
for &path in PROXY_BASELINE_READ_WRITE {
723+
if !fs.read_write.iter().any(|p| p.as_str() == path) {
724+
fs.read_write.push(path.to_string());
725+
modified = true;
726+
}
727+
}
728+
729+
if modified {
730+
info!("Enriched policy with baseline filesystem paths for proxy mode");
731+
}
732+
733+
modified
734+
}
735+
736+
/// Ensure a `SandboxPolicy` (Rust type) includes the baseline filesystem
737+
/// paths required for proxy-mode sandboxes. Used for the local-file code
738+
/// path where no proto is available.
739+
fn enrich_sandbox_baseline_paths(policy: &mut SandboxPolicy) {
740+
if !matches!(policy.network.mode, NetworkMode::Proxy) {
741+
return;
742+
}
743+
744+
let mut modified = false;
745+
for &path in PROXY_BASELINE_READ_ONLY {
746+
let p = std::path::PathBuf::from(path);
747+
if !policy.filesystem.read_only.contains(&p) {
748+
policy.filesystem.read_only.push(p);
749+
modified = true;
750+
}
751+
}
752+
for &path in PROXY_BASELINE_READ_WRITE {
753+
let p = std::path::PathBuf::from(path);
754+
if !policy.filesystem.read_write.contains(&p) {
755+
policy.filesystem.read_write.push(p);
756+
modified = true;
757+
}
758+
}
759+
760+
if modified {
761+
info!("Enriched policy with baseline filesystem paths for proxy mode");
762+
}
763+
}
764+
683765
/// Load sandbox policy from local files or gRPC.
684766
///
685767
/// Priority:
@@ -706,7 +788,7 @@ async fn load_policy(
706788
std::path::Path::new(data_file),
707789
)?;
708790
let config = engine.query_sandbox_config()?;
709-
let policy = SandboxPolicy {
791+
let mut policy = SandboxPolicy {
710792
version: 1,
711793
filesystem: config.filesystem,
712794
network: NetworkPolicy {
@@ -716,6 +798,7 @@ async fn load_policy(
716798
landlock: config.landlock,
717799
process: config.process,
718800
};
801+
enrich_sandbox_baseline_paths(&mut policy);
719802
return Ok((policy, Some(Arc::new(engine))));
720803
}
721804

@@ -728,14 +811,17 @@ async fn load_policy(
728811
);
729812
let proto_policy = grpc_client::fetch_policy(endpoint, id).await?;
730813

731-
let proto_policy = match proto_policy {
814+
let mut proto_policy = match proto_policy {
732815
Some(p) => p,
733816
None => {
734817
// No policy configured on the server. Discover from disk or
735818
// fall back to the restrictive default, then sync to the
736819
// gateway so it becomes the authoritative baseline.
737820
info!("Server returned no policy; attempting local discovery");
738-
let discovered = discover_policy_from_disk_or_default();
821+
let mut discovered = discover_policy_from_disk_or_default();
822+
// Enrich before syncing so the gateway baseline includes
823+
// baseline paths from the start.
824+
enrich_proto_baseline_paths(&mut discovered);
739825
let sandbox = sandbox.as_deref().ok_or_else(|| {
740826
miette::miette!(
741827
"Cannot sync discovered policy: sandbox not available.\n\
@@ -749,6 +835,23 @@ async fn load_policy(
749835
}
750836
};
751837

838+
// Ensure baseline filesystem paths are present for proxy-mode
839+
// sandboxes. If the policy was enriched, sync the updated version
840+
// back to the gateway so users can see the effective policy.
841+
let enriched = enrich_proto_baseline_paths(&mut proto_policy);
842+
if enriched {
843+
if let Some(sandbox_name) = sandbox.as_deref() {
844+
if let Err(e) =
845+
grpc_client::sync_policy(endpoint, sandbox_name, &proto_policy).await
846+
{
847+
warn!(
848+
error = %e,
849+
"Failed to sync enriched policy back to gateway (non-fatal)"
850+
);
851+
}
852+
}
853+
}
854+
752855
// Build OPA engine from baked-in rules + typed proto data.
753856
// In cluster mode, proxy networking is always enabled so OPA is
754857
// always required for allow/deny decisions.

‎crates/navigator-server/src/grpc.rs‎

Lines changed: 145 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,11 +1541,14 @@ fn validate_static_fields_unchanged(
15411541
baseline: &ProtoSandboxPolicy,
15421542
new: &ProtoSandboxPolicy,
15431543
) -> Result<(), Status> {
1544-
if baseline.filesystem != new.filesystem {
1545-
return Err(Status::invalid_argument(
1546-
"filesystem policy cannot be changed on a live sandbox (applied at startup)",
1547-
));
1548-
}
1544+
// Filesystem: allow additive changes (new paths can be added, but
1545+
// existing paths cannot be removed and include_workdir cannot change).
1546+
// This supports the supervisor's baseline path enrichment at startup.
1547+
// Note: Landlock is a one-way door — adding paths to the stored policy
1548+
// has no effect on a running child process; the enriched paths only
1549+
// take effect on the next restart.
1550+
validate_filesystem_additive(baseline.filesystem.as_ref(), new.filesystem.as_ref())?;
1551+
15491552
if baseline.landlock != new.landlock {
15501553
return Err(Status::invalid_argument(
15511554
"landlock policy cannot be changed on a live sandbox (applied at startup)",
@@ -1559,6 +1562,49 @@ fn validate_static_fields_unchanged(
15591562
Ok(())
15601563
}
15611564

1565+
/// Validate that a filesystem policy update is purely additive: all baseline
1566+
/// paths must still be present, `include_workdir` must not change, but new
1567+
/// paths may be added.
1568+
fn validate_filesystem_additive(
1569+
baseline: Option<&navigator_core::proto::FilesystemPolicy>,
1570+
new: Option<&navigator_core::proto::FilesystemPolicy>,
1571+
) -> Result<(), Status> {
1572+
match (baseline, new) {
1573+
(Some(base), Some(upd)) => {
1574+
if base.include_workdir != upd.include_workdir {
1575+
return Err(Status::invalid_argument(
1576+
"filesystem include_workdir cannot be changed on a live sandbox",
1577+
));
1578+
}
1579+
for path in &base.read_only {
1580+
if !upd.read_only.contains(path) {
1581+
return Err(Status::invalid_argument(format!(
1582+
"filesystem read_only path '{path}' cannot be removed on a live sandbox"
1583+
)));
1584+
}
1585+
}
1586+
for path in &base.read_write {
1587+
if !upd.read_write.contains(path) {
1588+
return Err(Status::invalid_argument(format!(
1589+
"filesystem read_write path '{path}' cannot be removed on a live sandbox"
1590+
)));
1591+
}
1592+
}
1593+
}
1594+
(None, Some(_)) => {
1595+
// Baseline had no filesystem policy, new one adds it — allowed
1596+
// (enrichment from empty).
1597+
}
1598+
(Some(_), None) => {
1599+
return Err(Status::invalid_argument(
1600+
"filesystem policy cannot be removed on a live sandbox",
1601+
));
1602+
}
1603+
(None, None) => {}
1604+
}
1605+
Ok(())
1606+
}
1607+
15621608
/// Validate that network mode hasn't changed (Block ↔ Proxy).
15631609
/// Adding network_policies when none existed (or removing all) changes the mode.
15641610
fn validate_network_mode_unchanged(
@@ -2762,7 +2808,7 @@ mod tests {
27622808
}
27632809

27642810
#[test]
2765-
fn validate_static_fields_rejects_filesystem_change() {
2811+
fn validate_static_fields_allows_additive_filesystem() {
27662812
use super::validate_static_fields_unchanged;
27672813
use navigator_core::proto::{FilesystemPolicy, SandboxPolicy as ProtoSandboxPolicy};
27682814

@@ -2773,16 +2819,107 @@ mod tests {
27732819
}),
27742820
..Default::default()
27752821
};
2776-
let changed = ProtoSandboxPolicy {
2822+
// Adding /lib is purely additive — should be allowed.
2823+
let additive = ProtoSandboxPolicy {
27772824
filesystem: Some(FilesystemPolicy {
27782825
read_only: vec!["/usr".into(), "/lib".into()],
27792826
..Default::default()
27802827
}),
27812828
..Default::default()
27822829
};
2830+
assert!(validate_static_fields_unchanged(&baseline, &additive).is_ok());
2831+
}
2832+
2833+
#[test]
2834+
fn validate_static_fields_rejects_filesystem_removal() {
2835+
use super::validate_static_fields_unchanged;
2836+
use navigator_core::proto::{FilesystemPolicy, SandboxPolicy as ProtoSandboxPolicy};
2837+
2838+
let baseline = ProtoSandboxPolicy {
2839+
filesystem: Some(FilesystemPolicy {
2840+
read_only: vec!["/usr".into(), "/lib".into()],
2841+
..Default::default()
2842+
}),
2843+
..Default::default()
2844+
};
2845+
// Removing /lib should be rejected.
2846+
let removed = ProtoSandboxPolicy {
2847+
filesystem: Some(FilesystemPolicy {
2848+
read_only: vec!["/usr".into()],
2849+
..Default::default()
2850+
}),
2851+
..Default::default()
2852+
};
2853+
let result = validate_static_fields_unchanged(&baseline, &removed);
2854+
assert!(result.is_err());
2855+
assert!(result.unwrap_err().message().contains("/lib"));
2856+
}
2857+
2858+
#[test]
2859+
fn validate_static_fields_rejects_filesystem_deletion() {
2860+
use super::validate_static_fields_unchanged;
2861+
use navigator_core::proto::{FilesystemPolicy, SandboxPolicy as ProtoSandboxPolicy};
2862+
2863+
let baseline = ProtoSandboxPolicy {
2864+
filesystem: Some(FilesystemPolicy {
2865+
read_only: vec!["/usr".into()],
2866+
..Default::default()
2867+
}),
2868+
..Default::default()
2869+
};
2870+
// Removing filesystem entirely should be rejected.
2871+
let deleted = ProtoSandboxPolicy {
2872+
filesystem: None,
2873+
..Default::default()
2874+
};
2875+
let result = validate_static_fields_unchanged(&baseline, &deleted);
2876+
assert!(result.is_err());
2877+
assert!(result.unwrap_err().message().contains("removed"));
2878+
}
2879+
2880+
#[test]
2881+
fn validate_static_fields_allows_filesystem_enrichment_from_none() {
2882+
use super::validate_static_fields_unchanged;
2883+
use navigator_core::proto::{FilesystemPolicy, SandboxPolicy as ProtoSandboxPolicy};
2884+
2885+
let baseline = ProtoSandboxPolicy {
2886+
filesystem: None,
2887+
..Default::default()
2888+
};
2889+
// Adding filesystem when baseline had none — enrichment, allowed.
2890+
let enriched = ProtoSandboxPolicy {
2891+
filesystem: Some(FilesystemPolicy {
2892+
read_only: vec!["/usr".into(), "/lib".into(), "/etc".into()],
2893+
read_write: vec!["/sandbox".into(), "/tmp".into()],
2894+
include_workdir: true,
2895+
}),
2896+
..Default::default()
2897+
};
2898+
assert!(validate_static_fields_unchanged(&baseline, &enriched).is_ok());
2899+
}
2900+
2901+
#[test]
2902+
fn validate_static_fields_rejects_include_workdir_change() {
2903+
use super::validate_static_fields_unchanged;
2904+
use navigator_core::proto::{FilesystemPolicy, SandboxPolicy as ProtoSandboxPolicy};
2905+
2906+
let baseline = ProtoSandboxPolicy {
2907+
filesystem: Some(FilesystemPolicy {
2908+
include_workdir: true,
2909+
..Default::default()
2910+
}),
2911+
..Default::default()
2912+
};
2913+
let changed = ProtoSandboxPolicy {
2914+
filesystem: Some(FilesystemPolicy {
2915+
include_workdir: false,
2916+
..Default::default()
2917+
}),
2918+
..Default::default()
2919+
};
27832920
let result = validate_static_fields_unchanged(&baseline, &changed);
27842921
assert!(result.is_err());
2785-
assert!(result.unwrap_err().message().contains("filesystem"));
2922+
assert!(result.unwrap_err().message().contains("include_workdir"));
27862923
}
27872924

27882925
#[test]

0 commit comments

Comments
 (0)