mxc-sdk 1.1.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! RAII lifecycle for a Learning Mode capture: create a process security environment,
//! start a trace against it, hand the environment to the runner to launch the child,
//! then — after the child exits — seal the ETL and tear the environment down.
//!
//! The ordering the OS requires is:
//!
//! 1. `CreateProcessSecurityEnvironment(spec)` → env handle
//! 2. invoke the selected Learning Mode start export for `env` → trace handle
//!    (**before** the child launches, so no early denials are missed)
//! 3. attach `env` as `PROC_THREAD_ATTRIBUTE_SECURITY_ENVIRONMENT`, then call
//!    `CreateProcessW` (**runner's job**; the session exposes the handle via
//!    [`CaptureSession::environment`])
//! 4. wait for the child to exit
//! 5. `StopLearningModeTrace(trace, outputPath)` → sealed ETL (bounded retries
//!    for transient delivery failures)
//! 6. `CloseLearningModeTrace(trace)` → release broker state and staged ETL
//! 7. `CloseProcessSecurityEnvironment(env)` → teardown
//!
//! [`CaptureSession::begin`] performs steps 1–2; the runner performs steps 3–4 with the
//! handle from [`CaptureSession::environment`]; [`CaptureSession::finish`] performs steps
//! 5–7 in order. If the session is dropped without `finish` (e.g. the launch failed or a
//! `?` unwound the stack), [`Drop`] closes the trace without stopping it — the OS-supported
//! discard path — then closes the environment.

use std::path::Path;

use windows::Win32::Foundation::HANDLE;

#[cfg(test)]
use crate::learning_mode_windows::LearningModeApi;
use crate::learning_mode_windows::{
    start_trace, LearningModeError, LearningModeTraceHandle, LearningModeTraceSources,
};

#[cfg(test)]
use crate::process_container_common::secenv::SecurityEnvironmentApi;
use crate::process_container_common::secenv::{self, ProcessSecurityEnvironment};

/// An in-flight Learning Mode capture: a live security environment with a trace already
/// started against it.
///
/// Construct with [`CaptureSession::begin`]; drive the child launch with the handle from
/// [`CaptureSession::environment`]; seal and tear down with [`CaptureSession::finish`].
/// Dropping without `finish` closes and discards the trace, then closes the environment.
#[derive(Debug)]
pub struct CaptureSession {
    /// `Some` until `finish`/`Drop` closes it.
    environment: Option<ProcessSecurityEnvironment>,
    /// `Some` until `finish`/`Drop` closes it.
    trace: Option<LearningModeTraceHandle>,
}

impl CaptureSession {
    /// Create a security environment from `sandbox_specification` and start a Learning
    /// Mode trace against it. Call **before** launching the child.
    ///
    /// `flags` is normally [`crate::process_container_common::secenv::PROCESS_SECURITY_ENVIRONMENT_FLAG_NONE`].
    ///
    /// # Errors
    /// - [`LearningModeError::HResultCall`] if `CreateProcessSecurityEnvironment` fails.
    /// - [`LearningModeError::HResultCall`] if the selected Learning Mode start export
    ///   fails — in which case the just-created environment is closed before returning
    ///   so it is not leaked.
    pub fn begin(sandbox_specification: &[u8], flags: u32) -> Result<Self, LearningModeError> {
        let environment = secenv::create(sandbox_specification, flags)?;
        // SAFETY: `environment` was just created and remains live until this
        // session closes it.
        let trace = unsafe { start_trace(environment.raw()) };
        Self::finish_begin(environment, trace)
    }

    fn finish_begin(
        environment: ProcessSecurityEnvironment,
        trace: Result<LearningModeTraceHandle, LearningModeError>,
    ) -> Result<Self, LearningModeError> {
        let trace = match trace {
            Ok(trace) => trace,
            Err(start_err) => {
                environment.close();
                return Err(start_err);
            }
        };
        Ok(Self {
            environment: Some(environment),
            trace: Some(trace),
        })
    }

    #[cfg(test)]
    fn begin_with_apis(
        secenv_api: SecurityEnvironmentApi,
        learning_mode_api: LearningModeApi,
        sandbox_specification: &[u8],
        flags: u32,
    ) -> Result<Self, LearningModeError> {
        let environment = secenv_api.create(sandbox_specification, flags)?;
        // SAFETY: the fake environment remains live for the session.
        let trace = unsafe { learning_mode_api.start_trace(environment.raw()) };
        Self::finish_begin(environment, trace)
    }

    /// The `HPROCESS_SECURITY_ENVIRONMENT` handle to pass to
    /// [`crate::process_container_common::secenv::SecurityEnvironmentStartupInfo`].
    ///
    /// # Panics
    /// Panics only on an internal invariant violation — the environment is present for
    /// the entire session lifetime (set by [`begin`](Self::begin), taken only by
    /// [`finish`](Self::finish), which consumes `self`, or by [`Drop`]), so a live
    /// `&self` here always holds one. Failing fast surfaces a misuse at the call site
    /// rather than silently handing a NULL handle to a Win32 API.
    #[must_use]
    pub fn environment(&self) -> HANDLE {
        match self.environment.as_ref() {
            Some(env) => env.raw(),
            None => {
                panic!("CaptureSession::environment called after the environment was torn down")
            }
        }
    }

    /// Event sources collected by the native trace.
    ///
    /// Callers can use this to distinguish an access-only fallback trace from an
    /// option-aware trace with verified support for network decisions.
    #[must_use]
    pub fn trace_sources(&self) -> LearningModeTraceSources {
        self.trace
            .as_ref()
            .expect("a live CaptureSession always owns its trace")
            .sources()
    }

    /// Stop the trace and deliver it to `output_path` (or skip delivery when
    /// `None`), retry transient delivery failures, close the trace, then close
    /// the security environment. Call **after** the child has exited.
    ///
    /// # Errors
    /// - [`LearningModeError::HResultCall`] from `StopLearningModeTrace`.
    pub fn finish(mut self, output_path: Option<&Path>) -> Result<(), LearningModeError> {
        let stop_result = match self.trace.as_ref() {
            Some(trace) => trace.stop_with_retry(output_path),
            None => Ok(()),
        };
        if let Some(trace) = self.trace.take() {
            trace.close();
        }
        if let Some(environment) = self.environment.take() {
            environment.close();
        }
        stop_result
    }
}

impl Drop for CaptureSession {
    fn drop(&mut self) {
        // Close without Stop is the OS-supported early-exit discard path.
        drop(self.trace.take());
        if let Some(environment) = self.environment.take() {
            drop(environment);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::ffi::c_void;
    use std::sync::atomic::{AtomicI32, AtomicUsize, Ordering};
    use std::sync::{Mutex, MutexGuard};
    use windows::Win32::Foundation::{ERROR_SHARING_VIOLATION, E_FAIL, S_OK};
    use windows_core::HRESULT;

    /// Serializes access to the shared fake-call event log and result knobs.
    static TEST_LOCK: Mutex<()> = Mutex::new(());
    /// Ordered log of the fake export calls, as they happen across both APIs.
    static EVENTS: Mutex<Vec<&'static str>> = Mutex::new(Vec::new());

    static CREATE_RESULT: AtomicI32 = AtomicI32::new(S_OK.0);
    static START_RESULT: AtomicI32 = AtomicI32::new(S_OK.0);
    static STOP_RESULT: AtomicI32 = AtomicI32::new(S_OK.0);
    static STOP_FAILURE_RESULT: AtomicI32 = AtomicI32::new(E_FAIL.0);
    static STOP_FAILURES_REMAINING: AtomicUsize = AtomicUsize::new(0);

    fn record(event: &'static str) {
        EVENTS.lock().unwrap().push(event);
    }

    fn take_events() -> Vec<&'static str> {
        std::mem::take(&mut *EVENTS.lock().unwrap())
    }

    fn dangling_handle() -> HANDLE {
        HANDLE(std::ptr::dangling_mut::<c_void>())
    }

    unsafe extern "system" fn fake_create(
        _: *const c_void,
        _: u32,
        _: u32,
        out: *mut HANDLE,
    ) -> HRESULT {
        record("create");
        let result = HRESULT(CREATE_RESULT.load(Ordering::SeqCst));
        if result.is_ok() {
            unsafe { *out = dangling_handle() };
        }
        result
    }

    unsafe extern "system" fn fake_query(_: *mut u64) -> HRESULT {
        S_OK
    }

    unsafe extern "system" fn fake_env_close(_: HANDLE) {
        record("env_close");
    }

    unsafe extern "system" fn fake_start(_: HANDLE, out: *mut HANDLE) -> HRESULT {
        record("start");
        let result = HRESULT(START_RESULT.load(Ordering::SeqCst));
        if result.is_ok() {
            unsafe { *out = dangling_handle() };
        }
        result
    }

    unsafe extern "system" fn fake_start_with_options(
        _: HANDLE,
        _: i32,
        out: *mut HANDLE,
    ) -> HRESULT {
        record("start_options");
        let result = HRESULT(START_RESULT.load(Ordering::SeqCst));
        if result.is_ok() {
            unsafe { *out = dangling_handle() };
        }
        result
    }

    unsafe extern "system" fn fake_stop(_: HANDLE, _: *const u16) -> HRESULT {
        record("stop");
        if STOP_FAILURES_REMAINING
            .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |remaining| {
                remaining.checked_sub(1)
            })
            .is_ok()
        {
            return HRESULT(STOP_FAILURE_RESULT.load(Ordering::SeqCst));
        }
        HRESULT(STOP_RESULT.load(Ordering::SeqCst))
    }

    unsafe extern "system" fn fake_trace_close(_: HANDLE) {
        record("trace_close");
    }

    fn reset() -> MutexGuard<'static, ()> {
        let guard = TEST_LOCK
            .lock()
            .unwrap_or_else(|poison| poison.into_inner());
        take_events();
        CREATE_RESULT.store(S_OK.0, Ordering::SeqCst);
        START_RESULT.store(S_OK.0, Ordering::SeqCst);
        STOP_RESULT.store(S_OK.0, Ordering::SeqCst);
        STOP_FAILURE_RESULT.store(E_FAIL.0, Ordering::SeqCst);
        STOP_FAILURES_REMAINING.store(0, Ordering::SeqCst);
        guard
    }

    fn fake_secenv_api() -> SecurityEnvironmentApi {
        SecurityEnvironmentApi::from_raw_parts(fake_create, fake_query, fake_env_close)
    }

    fn fake_learning_mode_api() -> LearningModeApi {
        LearningModeApi::from_raw_parts(fake_start, fake_stop, fake_trace_close)
    }

    fn fake_options_learning_mode_api() -> LearningModeApi {
        LearningModeApi::from_options_raw_parts(
            fake_start_with_options,
            fake_stop,
            fake_trace_close,
        )
    }

    fn begin_session() -> Result<CaptureSession, LearningModeError> {
        CaptureSession::begin_with_apis(
            fake_secenv_api(),
            fake_learning_mode_api(),
            b"PSEC-fake-spec",
            crate::process_container_common::secenv::PROCESS_SECURITY_ENVIRONMENT_FLAG_NONE,
        )
    }

    #[test]
    fn begin_creates_environment_before_starting_trace() {
        let _guard = reset();
        let session = begin_session().expect("begin should succeed with passing fakes");

        // The environment must be created first so the trace keys on a live handle.
        assert_eq!(take_events(), vec!["create", "start"]);
        assert_eq!(session.environment(), dangling_handle());
        assert_eq!(
            session.trace_sources(),
            LearningModeTraceSources::AccessOnly
        );

        // Tidy up deterministically so Drop bookkeeping does not leak into siblings.
        drop(session);
    }

    #[test]
    fn begin_exposes_option_aware_trace_sources() {
        let _guard = reset();
        let session = CaptureSession::begin_with_apis(
            fake_secenv_api(),
            fake_options_learning_mode_api(),
            b"PSEC-fake-spec",
            crate::process_container_common::secenv::PROCESS_SECURITY_ENVIRONMENT_FLAG_NONE,
        )
        .expect("begin should succeed with option-aware fakes");

        assert_eq!(take_events(), vec!["create", "start_options"]);
        assert_eq!(
            session.trace_sources(),
            LearningModeTraceSources::AccessAndNetwork
        );

        drop(session);
    }

    #[test]
    fn begin_start_failure_closes_environment_and_leaves_no_trace() {
        let _guard = reset();
        START_RESULT.store(E_FAIL.0, Ordering::SeqCst);

        let error = begin_session().expect_err("start failure must propagate");
        assert!(matches!(
            error,
            LearningModeError::HResultCall {
                function: "StartLearningModeTrace",
                code
            } if code == E_FAIL.0
        ));

        // The just-created environment is torn down; the trace was never created so
        // it is never closed, and Stop is never attempted.
        assert_eq!(take_events(), vec!["create", "start", "env_close"]);
    }

    #[test]
    fn finish_retries_stop_then_closes_trace_then_environment() {
        let _guard = reset();
        STOP_FAILURE_RESULT.store(
            HRESULT::from_win32(ERROR_SHARING_VIOLATION.0).0,
            Ordering::SeqCst,
        );
        STOP_FAILURES_REMAINING.store(2, Ordering::SeqCst);

        let session = begin_session().expect("begin should succeed");
        assert_eq!(take_events(), vec!["create", "start"]);

        session
            .finish(None)
            .expect("finish should succeed after retries");

        // Stop is retried until it succeeds, THEN the trace closes, THEN the
        // environment closes — the exact teardown ordering the OS requires.
        assert_eq!(
            take_events(),
            vec!["stop", "stop", "stop", "trace_close", "env_close"]
        );
    }

    #[test]
    fn finish_propagates_permanent_stop_failure_but_still_tears_down() {
        let _guard = reset();
        STOP_RESULT.store(E_FAIL.0, Ordering::SeqCst);

        let session = begin_session().expect("begin should succeed");
        take_events();

        let error = session
            .finish(None)
            .expect_err("a permanent stop failure must surface");
        assert!(matches!(
            error,
            LearningModeError::HResultCall {
                function: "StopLearningModeTrace",
                ..
            }
        ));

        // Even when Stop fails permanently, the trace and environment are still
        // closed, in order, so nothing leaks.
        assert_eq!(take_events(), vec!["stop", "trace_close", "env_close"]);
    }

    #[test]
    fn drop_without_finish_discards_trace_then_closes_environment() {
        let _guard = reset();
        let session = begin_session().expect("begin should succeed");
        take_events();

        drop(session);

        // Dropping without `finish` closes (discards) the trace WITHOUT calling
        // Stop, then closes the environment.
        assert_eq!(take_events(), vec!["trace_close", "env_close"]);
    }
}