Skip to content

Commit 205a03f

Browse files
committed
fix(mxc): reject isolation filesystem grants
Signed-off-by: Shailendra Singh <[email protected]>
1 parent 25b9d57 commit 205a03f

7 files changed

Lines changed: 149 additions & 12 deletions

File tree

‎crates/openshell-driver-mxc/src/driver.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,6 +4149,24 @@ mod lifecycle_tests {
41494149
assert!(crate::mxc::mock_recorded_config("sb-iso-ui").is_none());
41504150
}
41514151

4152+
#[tokio::test]
4153+
async fn isolation_session_rejects_filesystem_before_lifecycle_side_effects() {
4154+
let backend = MxcComputeBackend::new_mocked(MxcComputeConfig {
4155+
backend: MxcBackend::IsolationSession,
4156+
..Default::default()
4157+
});
4158+
let policy = fs_policy(&["C:/work/demo"]);
4159+
let sandbox = with_policy(driver_sandbox("sb-iso-filesystem"), policy);
4160+
let error = backend
4161+
.create_sandbox(&sandbox)
4162+
.await
4163+
.expect_err("isolation filesystem grants must be rejected synchronously");
4164+
assert_eq!(error.code(), tonic::Code::InvalidArgument);
4165+
assert!(error.message().contains("filesystem_policy"));
4166+
assert!(backend.list_sandboxes().await.is_empty());
4167+
assert!(crate::mxc::mock_recorded_config("sb-iso-filesystem").is_none());
4168+
}
4169+
41524170
#[tokio::test]
41534171
async fn negative_out_of_policy_write_is_denied_with_event() {
41544172
let share_tmp = tempfile::tempdir().unwrap();

‎crates/openshell-driver-mxc/src/mxc.rs‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,18 @@ impl WxcExecInvoker {
506506

507507
/// Run the provision phase and return the `sandboxId` from the response.
508508
pub async fn provision(&self, filesystem: MxcFilesystem) -> Result<String, InvokerError> {
509+
if !filesystem.readwrite_paths.is_empty()
510+
|| !filesystem.readonly_paths.is_empty()
511+
|| !filesystem.denied_paths.is_empty()
512+
{
513+
return Err(InvokerError::Mxc {
514+
code: "policy_validation".into(),
515+
message: "MXC 1.0 isolation_session cannot enforce filesystem grants".into(),
516+
});
517+
}
509518
if self.mock {
510-
// Mock provision: mint a synthetic `iso:` id and record the granted
511-
// read-write paths so the mock exec can enforce the policy.
519+
// Mock provision: mint a synthetic `iso:` id. IsolationSession has
520+
// no filesystem grants in MXC 1.0, so the recorded grant set is empty.
512521
let id = format!("iso:mock-{}", uuid::Uuid::new_v4());
513522
let grants: Vec<String> = filesystem
514523
.readwrite_paths
@@ -1039,6 +1048,25 @@ mod tests {
10391048
assert!(config.get("sandboxId").is_none());
10401049
}
10411050

1051+
#[tokio::test]
1052+
async fn isolation_provision_rejects_filesystem_grants() {
1053+
let invoker = WxcExecInvoker::mocked("unused");
1054+
let error = invoker
1055+
.provision(MxcFilesystem {
1056+
readwrite_paths: vec![r"C:\work\demo".into()],
1057+
..Default::default()
1058+
})
1059+
.await
1060+
.expect_err("isolation_session must not silently drop filesystem grants");
1061+
match error {
1062+
InvokerError::Mxc { code, message } => {
1063+
assert_eq!(code, "policy_validation");
1064+
assert!(message.contains("cannot enforce filesystem grants"));
1065+
}
1066+
other => panic!("expected MXC policy validation error, got {other}"),
1067+
}
1068+
}
1069+
10421070
#[test]
10431071
fn invoker_error_maps_backend_unavailable_to_unavailable() {
10441072
let err = InvokerError::Mxc {

‎crates/openshell-driver-mxc/src/policy.rs‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ impl PolicyMapper for EmbeddedPolicyMapper {
158158
)
159159
})?;
160160

161+
if ctx.containment == "isolation_session"
162+
&& policy.filesystem.as_ref().is_some_and(|filesystem| {
163+
filesystem.include_workdir
164+
|| !filesystem.read_only.is_empty()
165+
|| !filesystem.read_write.is_empty()
166+
})
167+
{
168+
return Err(MapError::Unsupported(vec![LossItem {
169+
rule_kind: "filesystem_policy".into(),
170+
detail: "MXC 1.0 isolation_session cannot enforce OpenShell filesystem grants"
171+
.into(),
172+
}]));
173+
}
174+
161175
let (config, loss, trimmed_policy, proxy_addr) = if let Some(addr) = ctx.egress {
162176
// Pattern C: MXC handles filesystem + a proxy redirect, while the
163177
// host CONNECT proxy receives the network-only trimmed policy.
@@ -267,7 +281,7 @@ mod tests {
267281
fn embedded_maps_policy_read_write_to_share() {
268282
let mapper = EmbeddedPolicyMapper;
269283
let policy = fs_policy(&["C:/work/openshell-mxc-demo"], &["C:/tools"]);
270-
let ctx = demo_ctx();
284+
let ctx = processcontainer_ctx();
271285
let config = mapper.map(Some(&policy), &ctx).unwrap();
272286
// Forward slashes normalized to Windows backslashes by the bridge.
273287
assert!(
@@ -307,6 +321,23 @@ mod tests {
307321
assert!(matches!(err, MapError::Unsupported(_)));
308322
}
309323

324+
#[test]
325+
fn embedded_rejects_filesystem_policy_on_isolation_session() {
326+
let mapper = EmbeddedPolicyMapper;
327+
let policy = fs_policy(&["C:/work/demo"], &[]);
328+
let err = mapper.map(Some(&policy), &demo_ctx()).unwrap_err();
329+
match err {
330+
MapError::Unsupported(items) => {
331+
assert_eq!(items.len(), 1);
332+
assert_eq!(items[0].rule_kind, "filesystem_policy");
333+
assert!(items[0].detail.contains("cannot enforce"));
334+
}
335+
MapError::Internal(message) => {
336+
panic!("expected unsupported filesystem policy, got internal error: {message}")
337+
}
338+
}
339+
}
340+
310341
#[test]
311342
fn embedded_rejects_explicit_ui_on_isolation_session() {
312343
use openshell_core::proto::UiPolicy;

‎crates/openshell-driver-mxc/src/policy_map/config.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,29 @@ pub fn add_backend_network_loss(
104104
_ => {}
105105
}
106106
}
107+
108+
/// Reject filesystem grants on stable-schema backends that cannot enforce
109+
/// them. Empty filesystem lists remain valid because they do not claim a grant.
110+
pub fn add_backend_filesystem_loss(
111+
policy: &SandboxPolicy,
112+
containment: &str,
113+
items: &mut Vec<LossItem>,
114+
) {
115+
let Some(filesystem) = &policy.filesystem else {
116+
return;
117+
};
118+
if containment == "isolation_session"
119+
&& (filesystem.include_workdir
120+
|| !filesystem.read_only.is_empty()
121+
|| !filesystem.read_write.is_empty())
122+
{
123+
add_loss(
124+
items,
125+
"filesystem_policy.grants",
126+
"error",
127+
"MXC 1.0 isolation_session cannot enforce OpenShell filesystem grants.",
128+
"filesystem path grants",
129+
"The caller must reject the mapping instead of silently dropping the grants.",
130+
);
131+
}
132+
}

‎crates/openshell-driver-mxc/src/policy_map/map.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use openshell_core::proto::{
1717
use serde_json::{Value, json};
1818

1919
use super::config::{
20-
DEFAULT_COMMAND, DEFAULT_CONTAINMENT, DEFAULT_MXC_VERSION, add_backend_network_loss,
21-
add_backend_specific_config, filesystem_default_deny_message,
20+
DEFAULT_COMMAND, DEFAULT_CONTAINMENT, DEFAULT_MXC_VERSION, add_backend_filesystem_loss,
21+
add_backend_network_loss, add_backend_specific_config, filesystem_default_deny_message,
2222
};
2323
use super::loss::{LossItem, add_loss};
2424

@@ -137,6 +137,7 @@ fn build_split_mxc_config(
137137
}
138138

139139
let filesystem = map_filesystem(policy, opts, items);
140+
add_backend_filesystem_loss(policy, &opts.containment, items);
140141

141142
let proxy_supported = matches!(opts.containment.as_str(), "processcontainer" | "process");
142143
if !proxy_supported {
@@ -245,6 +246,7 @@ fn build_mxc_config(
245246
}
246247

247248
let filesystem = map_filesystem(policy, opts, items);
249+
add_backend_filesystem_loss(policy, &opts.containment, items);
248250
let allow_rules = map_network(policy, opts, items);
249251
let has_direct_egress = !allow_rules.is_empty();
250252
let mut egress = json!({ "default": "deny" });

‎crates/openshell-driver-mxc/tests/policy_mapper_examples.rs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,19 @@ fn split_policy_rejects_proxy_redirect_on_isolation_session() {
410410
.collect();
411411
assert_eq!(
412412
errors.len(),
413-
1,
414-
"expected one containment error: {errors:?}"
413+
2,
414+
"expected filesystem and containment errors: {errors:?}"
415415
);
416-
assert_eq!(errors[0].path, "containment");
417-
assert!(errors[0].message.contains("processcontainer"));
416+
let filesystem = errors
417+
.iter()
418+
.find(|item| item.path == "filesystem_policy.grants")
419+
.expect("isolation_session filesystem error");
420+
assert!(filesystem.message.contains("cannot enforce"));
421+
let containment = errors
422+
.iter()
423+
.find(|item| item.path == "containment")
424+
.expect("isolation_session proxy-containment error");
425+
assert!(containment.message.contains("processcontainer"));
418426
assert!(result.mxc_config["network"].get("proxy").is_none());
419427
}
420428

‎crates/openshell-driver-mxc/tests/policy_mapper_matrix.rs‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -953,12 +953,12 @@ fn b_run_as_group_warning() {
953953
assert_single_loss(&r.loss, "run_as_group", "warning", "process.run_as_group");
954954
}
955955

956-
/// Seam-level: EmbeddedPolicyMapper.map over a policy with one error-class field
957-
/// (a port) via MapCtx{egress: None} returns Err(MapError::Unsupported(_)).
956+
/// Seam-level: EmbeddedPolicyMapper.map over a policy with unsupported fields
957+
/// via MapCtx{egress: None} returns Err(MapError::Unsupported(_)).
958958
#[test]
959959
fn b_seam_returns_unsupported_on_error_field() {
960960
let mapper = EmbeddedPolicyMapper;
961-
// isolation_session containment + network policy → error loss from add_backend_specific_config.
961+
// isolation_session containment cannot enforce filesystem or network policy.
962962
let mut policy = SandboxPolicy {
963963
filesystem: Some(FilesystemPolicy {
964964
read_write: vec!["C:/work".into()],
@@ -990,6 +990,30 @@ fn b_seam_returns_unsupported_on_error_field() {
990990
);
991991
}
992992

993+
#[test]
994+
fn b_isolation_session_reports_filesystem_grants_as_error() {
995+
let policy = SandboxPolicy {
996+
filesystem: Some(FilesystemPolicy {
997+
read_write: vec!["C:/work".into()],
998+
..Default::default()
999+
}),
1000+
..Default::default()
1001+
};
1002+
let result = map_to_mxc(
1003+
&policy,
1004+
&MxcMappingOptions {
1005+
containment: "isolation_session".into(),
1006+
..Default::default()
1007+
},
1008+
);
1009+
assert_single_loss(
1010+
&result.loss,
1011+
"filesystem_policy.grants",
1012+
"error",
1013+
"isolation_session filesystem grants",
1014+
);
1015+
}
1016+
9931017
// ─── QUADRANT C: restrictive defaults ("default deny posture") ───────────────
9941018

9951019
/// Empty SandboxPolicy (all None/empty) with default options produces the most

0 commit comments

Comments
 (0)