Skip to content

Commit 8a93cab

Browse files
authored
fix(supervisor): reject unsupported live filesystem policy changes (#5327)
Reject live filesystem policy changes that cannot affect the running sandbox. Compare candidates against the filesystem policy installed at startup, restoring only the proxy baseline paths installed there, so global-policy retries and network-only updates continue to load. Report rejected revisions through the existing policy validation failure handling and document that filesystem changes require sandbox recreation. Include regression coverage for sandbox/global policies, retries, reordered paths, and recovery. The CI pin repairs were merged separately in #5736. Existing CI verification and review are recorded on this PR. Closes #5326 Signed-off-by: Rahool Paliwal <rpaliwal@nvidia.com> Signed-off-by: Shailendra Singh <shailendras@nvidia.com>
1 parent f0db2ee commit 8a93cab

2 files changed

Lines changed: 315 additions & 24 deletions

File tree

‎crates/openshell-supervisor/src/lib.rs‎

Lines changed: 314 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,6 +3098,49 @@ async fn reload_gateway_policy_runtime(
30983098
.await
30993099
}
31003100

3101+
fn validate_live_filesystem_policy(
3102+
installed: Option<&openshell_core::proto::SandboxPolicy>,
3103+
candidate: Option<&openshell_core::proto::SandboxPolicy>,
3104+
) -> Result<()> {
3105+
let (Some(applied), Some(candidate)) = (installed, candidate) else {
3106+
return Ok(());
3107+
};
3108+
let mut candidate = candidate.clone();
3109+
if !applied.network_policies.is_empty() {
3110+
let (ro, rw) = proxy_baseline_paths();
3111+
// Global policies retain their raw gateway form. Restore only the
3112+
// proxy prerequisites that startup actually installed.
3113+
enrich_proto_baseline_paths_with(&mut candidate, &ro, &rw, |path| {
3114+
applied.filesystem.as_ref().is_some_and(|filesystem| {
3115+
filesystem
3116+
.read_only
3117+
.iter()
3118+
.chain(&filesystem.read_write)
3119+
.any(|installed| installed == path)
3120+
})
3121+
});
3122+
}
3123+
let same_paths = |left: &[String], right: &[String]| {
3124+
left.iter().collect::<std::collections::HashSet<_>>() == right.iter().collect()
3125+
};
3126+
let unchanged = match (&applied.filesystem, &candidate.filesystem) {
3127+
(Some(applied), Some(candidate)) => {
3128+
applied.include_workdir == candidate.include_workdir
3129+
&& same_paths(&applied.read_only, &candidate.read_only)
3130+
&& same_paths(&applied.read_write, &candidate.read_write)
3131+
}
3132+
(None, None) => true,
3133+
_ => false,
3134+
};
3135+
if !unchanged {
3136+
// Reloading the network engine cannot change installed filesystem permissions.
3137+
return Err(miette::miette!(
3138+
"filesystem policy cannot be changed on a running sandbox; recreate the sandbox to apply filesystem changes"
3139+
));
3140+
}
3141+
Ok(())
3142+
}
3143+
31013144
async fn reload_gateway_configuration_runtime(
31023145
engine: &OpaEngine,
31033146
policy: Option<&openshell_core::proto::SandboxPolicy>,
@@ -3807,7 +3850,9 @@ struct PolicyPollLoopContext {
38073850
endpoint_observation_tx: Option<openshell_core::endpoint_status::EndpointObservationSender>,
38083851
/// Single FIFO consumed by the endpoint status reporter.
38093852
endpoint_status_rx: Option<openshell_core::endpoint_status::EndpointStatusReceiver>,
3810-
/// Gateway policy currently installed in OPA, absent for local overrides.
3853+
/// Policy installed at startup, including its filesystem prerequisites.
3854+
/// Kept unchanged when gateway updates replace the policy in OPA.
3855+
/// Absent for local overrides.
38113856
endpoint_policy: Option<openshell_core::proto::SandboxPolicy>,
38123857
/// Session accepted by `ConnectSupervisor`; `None` suspends endpoint reports.
38133858
supervisor_session_id: tokio::sync::watch::Receiver<Option<String>>,
@@ -4689,28 +4734,36 @@ async fn run_policy_poll_loop_with_client<C: PolicyGatewayClient>(
46894734

46904735
if policy_runtime_changed {
46914736
let pid = ctx.entrypoint_pid.load(Ordering::Acquire);
4692-
let runtime_result = reload_gateway_configuration_runtime(
4693-
&ctx.opa_engine,
4737+
let runtime_result = match validate_live_filesystem_policy(
4738+
ctx.endpoint_policy.as_ref(),
46944739
result.policy.as_ref(),
4695-
pid,
4696-
MiddlewareReloadContext {
4697-
desired_services: &result.supervisor_middleware_services,
4698-
authentication: &MiddlewareAuthentication {
4699-
credentials: middleware_credentials.clone(),
4700-
enabled: result.extension_authentication_enabled,
4701-
},
4702-
registry_changed: middleware_registry_changed,
4703-
connector: &ctx.middleware_connector,
4704-
},
4705-
ctx.transparent_tcp,
4706-
ctx.vm_identity,
4707-
|| {
4708-
if let Some(prepared) = prepared_provider.as_ref() {
4709-
ctx.provider_credentials.install_prepared(prepared);
4710-
}
4711-
},
4712-
)
4713-
.await;
4740+
) {
4741+
Ok(()) => {
4742+
reload_gateway_configuration_runtime(
4743+
&ctx.opa_engine,
4744+
result.policy.as_ref(),
4745+
pid,
4746+
MiddlewareReloadContext {
4747+
desired_services: &result.supervisor_middleware_services,
4748+
authentication: &MiddlewareAuthentication {
4749+
credentials: middleware_credentials.clone(),
4750+
enabled: result.extension_authentication_enabled,
4751+
},
4752+
registry_changed: middleware_registry_changed,
4753+
connector: &ctx.middleware_connector,
4754+
},
4755+
ctx.transparent_tcp,
4756+
ctx.vm_identity,
4757+
|| {
4758+
if let Some(prepared) = prepared_provider.as_ref() {
4759+
ctx.provider_credentials.install_prepared(prepared);
4760+
}
4761+
},
4762+
)
4763+
.await
4764+
}
4765+
Err(error) => Err(GatewayRuntimeReloadError::PolicyValidation(error)),
4766+
};
47144767

47154768
match runtime_result {
47164769
Ok(generation) => {
@@ -8353,8 +8406,11 @@ network_policies:
83538406
1,
83548407
openshell_core::proto::PolicySource::Sandbox,
83558408
);
8409+
// Keep the applied filesystem grants so only the TCP expansion differs.
8410+
let mut tcp_policy = proto_policy_fixture();
8411+
tcp_policy.network_policies = proto_tcp_policy_fixture().network_policies;
83568412
let v2 = settings_poll_result(
8357-
Some(proto_tcp_policy_fixture()),
8413+
Some(tcp_policy),
83588414
2,
83598415
openshell_core::proto::PolicySource::Sandbox,
83608416
);
@@ -8394,6 +8450,241 @@ network_policies:
83948450
handle.abort();
83958451
}
83968452

8453+
#[tokio::test]
8454+
async fn live_filesystem_changes_reject_polled_policy_without_advancing_active_revision() {
8455+
use openshell_core::proto::{FilesystemPolicy, PolicySource};
8456+
8457+
for source in [PolicySource::Sandbox, PolicySource::Global] {
8458+
for change in ["read_only", "read_write", "from_none"] {
8459+
let mut policy = proto_policy_fixture();
8460+
if change == "from_none" {
8461+
policy.filesystem = None;
8462+
} else {
8463+
// Startup may already have added the proxy's filesystem prerequisites.
8464+
policy
8465+
.filesystem
8466+
.as_mut()
8467+
.unwrap()
8468+
.read_only
8469+
.push("/proxy-baseline".into());
8470+
}
8471+
let initial = settings_poll_result(Some(policy.clone()), 1, source);
8472+
let engine = Arc::new(OpaEngine::from_proto(&policy).expect("build OPA engine"));
8473+
let mut ctx = policy_poll_test_context(
8474+
engine.clone(),
8475+
LoadedPolicyOrigin::Gateway {
8476+
revision: Some(LoadedPolicyRevision::from_snapshot(&initial)),
8477+
has_last_valid_policy: true,
8478+
},
8479+
default_middleware_connector(),
8480+
);
8481+
ctx.endpoint_policy = Some(policy.clone());
8482+
let (client, polls, mut reports) = scripted_policy_gateway();
8483+
let observed_polls = client.polled_sandboxes.clone();
8484+
let wait_for_polls = |count: usize| {
8485+
let observed_polls = observed_polls.clone();
8486+
async move {
8487+
timeout(Duration::from_secs(1), async {
8488+
while observed_polls.lock().await.len() < count {
8489+
tokio::task::yield_now().await;
8490+
}
8491+
})
8492+
.await
8493+
.expect("policy poll loop stalled");
8494+
}
8495+
};
8496+
polls.send(initial).unwrap();
8497+
let handle = tokio::spawn(run_policy_poll_loop_with_client(ctx, client));
8498+
if source == PolicySource::Sandbox {
8499+
expect_policy_report(&mut reports, 1).await;
8500+
}
8501+
wait_for_polls(2).await;
8502+
let original_generation = engine.current_generation();
8503+
8504+
let filesystem = policy
8505+
.filesystem
8506+
.get_or_insert_with(FilesystemPolicy::default);
8507+
if change == "read_write" {
8508+
filesystem.read_write.push("/extra-readwrite".into());
8509+
} else {
8510+
filesystem.read_only.push("/extra-readonly".into());
8511+
}
8512+
let mut candidate = settings_poll_result(Some(policy), 2, source);
8513+
let fail_closed = source == PolicySource::Global && change == "read_only";
8514+
candidate.policy_validation_failure_mode = if fail_closed {
8515+
PolicyValidationFailureMode::FailClosed
8516+
} else {
8517+
PolicyValidationFailureMode::RetainLastValid
8518+
};
8519+
polls.send(candidate).unwrap();
8520+
wait_for_polls(3).await;
8521+
8522+
if source == PolicySource::Sandbox {
8523+
let (version, loaded, error) = timeout(Duration::from_secs(1), reports.recv())
8524+
.await
8525+
.expect("filesystem rejection report timed out")
8526+
.expect("policy reporter stopped");
8527+
assert_eq!(version, 2, "{source:?}: {change}");
8528+
assert!(!loaded, "{source:?}: {change}");
8529+
assert!(
8530+
error.contains("filesystem"),
8531+
"{source:?}: {change}: {error}"
8532+
);
8533+
} else {
8534+
expect_no_policy_report(&mut reports).await;
8535+
}
8536+
assert_eq!(
8537+
engine.fail_closed_reason().is_some(),
8538+
fail_closed,
8539+
"{source:?}: {change}"
8540+
);
8541+
assert_eq!(
8542+
engine.current_generation(),
8543+
original_generation + u64::from(fail_closed),
8544+
"{source:?}: {change}"
8545+
);
8546+
handle.abort();
8547+
}
8548+
}
8549+
}
8550+
8551+
#[tokio::test]
8552+
async fn global_policy_retry_uses_installed_filesystem_after_missing_initial_acknowledgement() {
8553+
use openshell_core::proto::{FilesystemPolicy, PolicySource};
8554+
8555+
let mut policy = proto_tcp_policy_fixture();
8556+
policy.filesystem = Some(FilesystemPolicy {
8557+
include_workdir: true,
8558+
read_only: vec!["/usr".into(), "/tmp".into()],
8559+
..Default::default()
8560+
});
8561+
let initial = settings_poll_result(Some(policy.clone()), 1, PolicySource::Global);
8562+
let mut installed = policy.clone();
8563+
let (ro, rw) = proxy_baseline_paths();
8564+
assert!(enrich_proto_baseline_paths_with(
8565+
&mut installed,
8566+
&ro,
8567+
&rw,
8568+
|path| matches!(path, "/proc" | "/dev/null"),
8569+
));
8570+
let engine = Arc::new(OpaEngine::from_proto(&installed).expect("build OPA engine"));
8571+
let mut ctx = policy_poll_test_context(
8572+
engine.clone(),
8573+
LoadedPolicyOrigin::Gateway {
8574+
revision: Some(LoadedPolicyRevision::from_snapshot(&initial)),
8575+
has_last_valid_policy: true,
8576+
},
8577+
default_middleware_connector(),
8578+
);
8579+
ctx.endpoint_policy = Some(installed);
8580+
ctx.transparent_tcp = TransparentTcpReloadState {
8581+
capable: true,
8582+
substrate_ready: true,
8583+
};
8584+
let (client, polls, mut reports) = scripted_policy_gateway();
8585+
let observed_polls = client.polled_sandboxes.clone();
8586+
let wait_for_polls = |count: usize| {
8587+
let observed_polls = observed_polls.clone();
8588+
async move {
8589+
timeout(Duration::from_secs(1), async {
8590+
while observed_polls.lock().await.len() < count {
8591+
tokio::task::yield_now().await;
8592+
}
8593+
})
8594+
.await
8595+
.expect("policy poll loop stalled");
8596+
}
8597+
};
8598+
// A global policy is never acknowledged by the first poll, so its raw
8599+
// gateway form is reconciled against the policy startup installed.
8600+
polls.send(initial).unwrap();
8601+
let handle = tokio::spawn(run_policy_poll_loop_with_client(ctx, client));
8602+
wait_for_polls(2).await;
8603+
assert!(engine.fail_closed_reason().is_none());
8604+
assert_eq!(engine.current_generation(), 1);
8605+
8606+
let mut network_only = policy;
8607+
network_only
8608+
.network_policies
8609+
.values_mut()
8610+
.next()
8611+
.expect("network rule")
8612+
.endpoints[0]
8613+
.port = 6380;
8614+
let mut changed = network_only.clone();
8615+
let network_only = settings_poll_result(Some(network_only), 2, PolicySource::Global);
8616+
polls.send(network_only).unwrap();
8617+
wait_for_polls(3).await;
8618+
assert!(engine.fail_closed_reason().is_none());
8619+
assert_eq!(engine.current_generation(), 2);
8620+
8621+
changed
8622+
.filesystem
8623+
.as_mut()
8624+
.expect("filesystem policy")
8625+
.read_only
8626+
.push("/app".into());
8627+
let mut changed = settings_poll_result(Some(changed), 3, PolicySource::Global);
8628+
changed.policy_validation_failure_mode = PolicyValidationFailureMode::FailClosed;
8629+
polls.send(changed).unwrap();
8630+
wait_for_polls(4).await;
8631+
assert!(engine.fail_closed_reason().is_some());
8632+
expect_no_policy_report(&mut reports).await;
8633+
handle.abort();
8634+
}
8635+
8636+
#[tokio::test]
8637+
async fn live_filesystem_change_poll_reports_failure_then_accepts_network_only_repair() {
8638+
use openshell_core::proto::PolicySource;
8639+
8640+
let policy = proto_policy_fixture();
8641+
let initial = settings_poll_result(Some(policy.clone()), 1, PolicySource::Sandbox);
8642+
let mut rejected = policy.clone();
8643+
rejected
8644+
.filesystem
8645+
.as_mut()
8646+
.unwrap()
8647+
.read_only
8648+
.push("/extra-readonly".into());
8649+
let mut rejected = settings_poll_result(Some(rejected), 2, PolicySource::Sandbox);
8650+
rejected.policy_validation_failure_mode = PolicyValidationFailureMode::RetainLastValid;
8651+
let mut repaired = policy.clone();
8652+
repaired.network_policies = proto_tcp_policy_fixture().network_policies;
8653+
repaired.filesystem.as_mut().unwrap().read_only.reverse();
8654+
let repaired = settings_poll_result(Some(repaired), 3, PolicySource::Sandbox);
8655+
let engine = Arc::new(OpaEngine::from_proto(&policy).expect("build OPA engine"));
8656+
let mut ctx = policy_poll_test_context(
8657+
engine.clone(),
8658+
LoadedPolicyOrigin::Gateway {
8659+
revision: Some(LoadedPolicyRevision::from_snapshot(&initial)),
8660+
has_last_valid_policy: true,
8661+
},
8662+
default_middleware_connector(),
8663+
);
8664+
ctx.endpoint_policy = Some(policy);
8665+
ctx.transparent_tcp = TransparentTcpReloadState {
8666+
capable: true,
8667+
substrate_ready: true,
8668+
};
8669+
let (client, polls, mut reports) = scripted_policy_gateway();
8670+
polls.send(initial).unwrap();
8671+
let handle = tokio::spawn(run_policy_poll_loop_with_client(ctx, client));
8672+
expect_policy_report(&mut reports, 1).await;
8673+
polls.send(rejected).unwrap();
8674+
let (version, loaded, error) = timeout(Duration::from_secs(1), reports.recv())
8675+
.await
8676+
.expect("filesystem rejection report timed out")
8677+
.expect("policy reporter stopped");
8678+
assert_eq!(version, 2);
8679+
assert!(!loaded);
8680+
assert!(error.contains("filesystem"));
8681+
assert_eq!(engine.current_generation(), 0);
8682+
polls.send(repaired).unwrap();
8683+
expect_policy_report(&mut reports, 3).await;
8684+
assert_eq!(engine.current_generation(), 1);
8685+
handle.abort();
8686+
}
8687+
83978688
#[tokio::test]
83988689
async fn same_hash_ack_waits_for_failed_middleware_reconciliation_and_retries_once() {
83998690
let mut v1 = settings_poll_result(

‎docs/how-it-works/policies/manage-policies.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ running sandbox. The startup sections do not:
191191
| Network rules | The sandbox loads the new rules. Connections opened under the previous rules close. | Apply the change, then retry the request. |
192192
| Middleware in `network_middlewares` | You can add, remove, or reconfigure built-in middleware and external services already registered with the gateway. | Apply the change with `openshell policy set`. |
193193
| External middleware registration | Policy changes cannot register a service or change its gateway connection settings. | Update the gateway configuration and restart the gateway. |
194-
| Added filesystem paths | The saved policy can change, but the running workload keeps its existing filesystem permissions. | Recreate the sandbox. |
194+
| Added filesystem paths | The sandbox rejects the entire revision, including network changes. With the default `fail_closed` failure mode, it blocks network traffic until a valid revision loads. | Recreate the sandbox. |
195195
| Removed filesystem paths, or changed `include_workdir`, Landlock, or process settings | OpenShell rejects the change after the workload starts. | Recreate the sandbox. |
196196

197197
When new network rules take effect, OpenShell closes connections that were

0 commit comments

Comments
 (0)