Skip to content

Commit 2ae8620

Browse files
jamieknvidiaaraza008
authored andcommitted
fix(mxc): seed ETW attribution under registry lock + Device tests
Address CodeRabbit review on !31: - Prevent stale ETW attribution on a delete/launch race: register the wxc-exec pid while holding the registry lock, and bail if the sandbox entry is already gone. Previously the attribution key could be seeded after `delete` had removed the sandbox, leaving a stale key that could misroute later Sandboxing ETW events to a dead sandbox_id. Lock order (registry -> attribution) matches the delete path, so no deadlock. - Add unit tests for the new Device::windows and Device::for_current_os constructors to harden Windows/Linux OCSF device parity. Signed-off-by: Akber Raza <akberr@nvidia.com>
1 parent 337dc8f commit 2ae8620

3 files changed

Lines changed: 58 additions & 25 deletions

File tree

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

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -767,16 +767,6 @@ async fn run_lifecycle(
767767
};
768768
info!(sandbox = %sandbox_name, command = %command_line, backend = ?config.backend, "MXC agent launched");
769769

770-
// Seed ETW attribution: the wxc-exec pid we just spawned is the
771-
// collision-proof anchor that ties the `Sandboxing` provider's events back
772-
// to this `sandbox_id` (command line is a fallback matcher). No-op unless
773-
// the ETW consumer is running.
774-
if let Some(pid) = child.id() {
775-
if let Ok(mut idx) = attribution.lock() {
776-
idx.register_launch(&sandbox_id, &sandbox_name, pid, &command_line);
777-
}
778-
}
779-
780770
let ready_sandbox = make_sandbox_with_condition(
781771
&sandbox,
782772
&DriverCondition {
@@ -794,19 +784,37 @@ async fn run_lifecycle(
794784
// process exit. Holding the registry lock while spawning prevents a
795785
// completed child from being overwritten with AgentRunning.
796786
let mut registry_guard = registry.lock().await;
797-
if let Some(entry) = registry_guard.get_mut(&sandbox_id) {
798-
entry.sandbox = ready_sandbox.clone();
799-
entry.phase_state = PhaseState::Running;
800-
entry.monitor_cancel = Some(cancel_tx);
801-
entry.monitor_task = Some(tokio::spawn(monitor_exec(
802-
registry.clone(),
803-
watch_tx.clone(),
804-
sandbox.clone(),
805-
sandbox_id.clone(),
806-
cancel_rx,
807-
child,
808-
)));
787+
let Some(entry) = registry_guard.get_mut(&sandbox_id) else {
788+
// The sandbox was deleted between agent launch and readiness. Bail
789+
// without seeding ETW attribution (a stale key would misroute later
790+
// events to a dead sandbox), without reporting Ready, and without
791+
// spawning the exec monitor. `delete` already tore down the process.
792+
return;
793+
};
794+
795+
// Seed ETW attribution while holding the registry lock so a concurrent
796+
// `delete` cannot remove the sandbox after we register (which would leave
797+
// a stale key). The `wxc-exec` pid we just spawned is the collision-proof
798+
// anchor that ties the `Sandboxing` provider's events back to this
799+
// `sandbox_id` (command line is a fallback matcher). No-op unless the ETW
800+
// consumer is running.
801+
if let Some(pid) = child.id() {
802+
if let Ok(mut idx) = attribution.lock() {
803+
idx.register_launch(&sandbox_id, &sandbox_name, pid, &command_line);
804+
}
809805
}
806+
807+
entry.sandbox = ready_sandbox.clone();
808+
entry.phase_state = PhaseState::Running;
809+
entry.monitor_cancel = Some(cancel_tx);
810+
entry.monitor_task = Some(tokio::spawn(monitor_exec(
811+
registry.clone(),
812+
watch_tx.clone(),
813+
sandbox.clone(),
814+
sandbox_id.clone(),
815+
cancel_rx,
816+
child,
817+
)));
810818
}
811819
let _ = watch_tx.send(sandbox_event(ready_sandbox));
812820
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,12 @@ fn process_event(index: &Mutex<AttributionIndex>, ev: DecodedEtwEvent) {
990990
// coverage. `SandboxProxyConfigured` (network/proxy setup — the one
991991
// network-plane event the provider emits) and `SandboxConsoleReferencePlumbed`
992992
// (console-handle plumbing) are additional per-sandbox setup state changes.
993-
"SandboxConfig" | "Win32kLockdownApplied" | "ApplyUILimits" | "EnforceOsPolicy"
994-
| "SandboxProxyConfigured" | "SandboxConsoleReferencePlumbed" => {
993+
"SandboxConfig"
994+
| "Win32kLockdownApplied"
995+
| "ApplyUILimits"
996+
| "EnforceOsPolicy"
997+
| "SandboxProxyConfigured"
998+
| "SandboxConsoleReferencePlumbed" => {
995999
let ctx = etw_ctx(&sandbox_id, &sandbox_name);
9961000
emit_ocsf(&sandbox_id, map_config_state(&ctx, &ev));
9971001
}
@@ -1144,7 +1148,9 @@ fn map_process_started(ctx: &SandboxContext, ev: &DecodedEtwEvent) -> OcsfEvent
11441148
.status(StatusId::Success)
11451149
.process(Process::new("sandboxed-process", pid))
11461150
.actor_process(Process::new("wxc-exec", i64::from(ev.process_id)))
1147-
.message(format!("MXC sandbox process started (pid: {pid}{tid_suffix})"))
1151+
.message(format!(
1152+
"MXC sandbox process started (pid: {pid}{tid_suffix})"
1153+
))
11481154
.build()
11491155
}
11501156

‎crates/openshell-ocsf/src/objects/device.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,23 @@ mod tests {
7575
assert_eq!(json["hostname"], "sandbox-abc123");
7676
assert_eq!(json["os"]["name"], "Linux");
7777
}
78+
79+
#[test]
80+
fn test_device_windows() {
81+
let device = Device::windows("gateway-host");
82+
let json = serde_json::to_value(&device).unwrap();
83+
assert_eq!(json["hostname"], "gateway-host");
84+
assert_eq!(json["os"]["name"], "Windows");
85+
}
86+
87+
#[test]
88+
fn test_device_for_current_os() {
89+
let device = Device::for_current_os("host");
90+
let json = serde_json::to_value(&device).unwrap();
91+
assert_eq!(json["hostname"], "host");
92+
#[cfg(target_os = "windows")]
93+
assert_eq!(json["os"]["name"], "Windows");
94+
#[cfg(not(target_os = "windows"))]
95+
assert_eq!(json["os"]["name"], "Linux");
96+
}
7897
}

0 commit comments

Comments
 (0)