a3s-box-core 3.1.0

Core types, config, and error handling for A3S Box MicroVM runtime
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Backend-neutral execution isolation resolution.

use serde::{Deserialize, Serialize};

use crate::config::{BoxConfig, ExecutionIsolation, TeeConfig};
use crate::error::{BoxError, Result};
use crate::network::NetworkMode;

/// Concrete backend selected for an execution request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ExecutionBackend {
    /// libkrun-backed MicroVM execution.
    Krun,
    /// OCI execution through the certified crun runtime.
    Crun,
}

/// Security boundary provided by the resolved backend.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum IsolationClass {
    /// A hardware-backed virtual-machine boundary.
    HardwareVm,
    /// Linux namespaces and controls sharing the host kernel.
    SharedKernel,
}

/// Deterministic result of resolving one execution request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ResolvedExecutionPlan {
    /// Isolation requested by the caller or selected by the implicit default.
    pub requested_isolation: ExecutionIsolation,
    /// Concrete runtime backend.
    pub backend: ExecutionBackend,
    /// Effective security-boundary class.
    pub isolation_class: IsolationClass,
    /// Controls that the selected backend must prove before launch.
    pub required_controls: Vec<String>,
}

const SANDBOX_REQUIRED_CONTROLS: &[&str] = &[
    "user-namespace",
    "mount-namespace",
    "pid-namespace",
    "ipc-namespace",
    "uts-namespace",
    "network-namespace",
    "seccomp",
    "capability-bounding-set",
    "no-new-privileges",
    "cgroup-v2",
];

const SANDBOX_ALLOWED_ADDED_CAPABILITIES: &[&str] = &[
    "AUDIT_WRITE",
    "CHOWN",
    "DAC_OVERRIDE",
    "FOWNER",
    "FSETID",
    "KILL",
    "MKNOD",
    "NET_BIND_SERVICE",
    "SETFCAP",
    "SETGID",
    "SETPCAP",
    "SETUID",
    "SYS_CHROOT",
];

/// Resolve a box configuration without probing or mutating the host.
///
/// Host capabilities are checked separately immediately before preparation.
/// Keeping this function pure makes unsupported feature combinations fail
/// before image pulls, rootfs mounts, state changes, or runtime processes.
pub fn resolve_execution(config: &BoxConfig) -> Result<ResolvedExecutionPlan> {
    match config.isolation {
        ExecutionIsolation::Microvm => {
            validate_microvm_compatibility(config)?;
            Ok(ResolvedExecutionPlan {
                requested_isolation: ExecutionIsolation::Microvm,
                backend: ExecutionBackend::Krun,
                isolation_class: IsolationClass::HardwareVm,
                required_controls: Vec::new(),
            })
        }
        ExecutionIsolation::Sandbox => {
            validate_sandbox_compatibility(config)?;
            Ok(ResolvedExecutionPlan {
                requested_isolation: ExecutionIsolation::Sandbox,
                backend: ExecutionBackend::Crun,
                isolation_class: IsolationClass::SharedKernel,
                required_controls: SANDBOX_REQUIRED_CONTROLS
                    .iter()
                    .map(|control| (*control).to_string())
                    .collect(),
            })
        }
    }
}

/// Validate features that cannot be represented safely by the MicroVM backend.
pub fn validate_microvm_compatibility(config: &BoxConfig) -> Result<()> {
    if config.isolation != ExecutionIsolation::Microvm {
        return Ok(());
    }

    validate_security_options(config, SecurityBackend::Microvm)
}

/// Validate features that cannot be represented safely by the sandbox MVP.
pub fn validate_sandbox_compatibility(config: &BoxConfig) -> Result<()> {
    if !config.isolation.is_sandbox() {
        return Ok(());
    }

    validate_security_options(config, SecurityBackend::Sandbox)?;

    let mut unsupported = Vec::new();

    if !matches!(config.tee, TeeConfig::None) {
        unsupported.push("TEE and attestation");
    }
    if config.pool.enabled || config.pool.snapshot_fork {
        unsupported.push("warm pools and snapshot-fork");
    }
    if config.deferred_main {
        unsupported.push("deferred main execution");
    }
    if config.ksm {
        unsupported.push("KSM");
    }
    if config.snapshot_mem_file.is_some()
        || config.snapshot_sock.is_some()
        || config.restore_from.is_some()
    {
        unsupported.push("VM snapshots and restore");
    }
    if config.privileged {
        unsupported.push("privileged mode");
    }
    if config.sidecar.is_some() {
        unsupported.push("vsock sidecars");
    }
    if !config.port_map.is_empty() {
        unsupported.push("published ports");
    }
    if matches!(config.network, NetworkMode::Bridge { .. }) {
        unsupported.push("named bridge networking");
    }
    if !config.sysctls.is_empty() {
        unsupported.push("custom sysctls");
    }
    let disallowed_capabilities: Vec<String> = config
        .cap_add
        .iter()
        .map(|capability| normalize_capability(capability))
        .filter(|capability| !SANDBOX_ALLOWED_ADDED_CAPABILITIES.contains(&capability.as_str()))
        .collect();
    if !disallowed_capabilities.is_empty() {
        return Err(BoxError::ConfigError(format!(
            "sandbox isolation rejects added capabilities outside its allowlist: {}",
            disallowed_capabilities.join(", ")
        )));
    }

    if unsupported.is_empty() {
        Ok(())
    } else {
        Err(BoxError::ConfigError(format!(
            "sandbox isolation does not support: {}",
            unsupported.join(", ")
        )))
    }
}

#[derive(Debug, Clone, Copy)]
enum SecurityBackend {
    Microvm,
    Sandbox,
}

impl SecurityBackend {
    fn label(self) -> &'static str {
        match self {
            Self::Microvm => "microVM",
            Self::Sandbox => "sandbox",
        }
    }
}

fn validate_security_options(config: &BoxConfig, backend: SecurityBackend) -> Result<()> {
    for raw_option in &config.security_opt {
        let option = raw_option.trim();
        if option.is_empty() {
            return Err(BoxError::ConfigError(format!(
                "{} isolation does not accept an empty security option",
                backend.label()
            )));
        }

        if option.eq_ignore_ascii_case("no-new-privileges") {
            continue;
        }

        let Some((key, value)) = option.split_once('=') else {
            return Err(unsupported_security_option(backend, option));
        };
        let key = key.trim();
        let value = value.trim();

        if key.eq_ignore_ascii_case("seccomp") {
            if value.eq_ignore_ascii_case("default") {
                continue;
            }
            if value.eq_ignore_ascii_case("unconfined") {
                if matches!(backend, SecurityBackend::Microvm) {
                    continue;
                }
                return Err(BoxError::ConfigError(
                    "sandbox isolation does not support unconfined seccomp".to_string(),
                ));
            }
            if value.is_empty() {
                return Err(BoxError::ConfigError(format!(
                    "{} isolation requires a seccomp mode",
                    backend.label()
                )));
            }
            return Err(BoxError::ConfigError(format!(
                "{} isolation does not support custom seccomp profile '{}'",
                backend.label(),
                value
            )));
        }

        if key.eq_ignore_ascii_case("no-new-privileges") {
            if value.eq_ignore_ascii_case("true") {
                continue;
            }
            if value.eq_ignore_ascii_case("false") {
                if matches!(backend, SecurityBackend::Microvm) {
                    continue;
                }
                return Err(BoxError::ConfigError(
                    "sandbox isolation requires no-new-privileges and cannot disable it"
                        .to_string(),
                ));
            }
            return Err(BoxError::ConfigError(format!(
                "{} isolation requires no-new-privileges to be true or false, got '{}'",
                backend.label(),
                value
            )));
        }

        if key.eq_ignore_ascii_case("apparmor") {
            return Err(BoxError::ConfigError(format!(
                "{} isolation does not support AppArmor security option '{}'",
                backend.label(),
                option
            )));
        }

        if key.eq_ignore_ascii_case("label") {
            return Err(BoxError::ConfigError(format!(
                "{} isolation does not support SELinux label security option '{}'",
                backend.label(),
                option
            )));
        }

        return Err(unsupported_security_option(backend, option));
    }

    Ok(())
}

fn unsupported_security_option(backend: SecurityBackend, option: &str) -> BoxError {
    BoxError::ConfigError(format!(
        "{} isolation does not support security option '{}'",
        backend.label(),
        option
    ))
}

fn normalize_capability(capability: &str) -> String {
    let normalized = capability.trim().to_ascii_uppercase();
    normalized
        .strip_prefix("CAP_")
        .unwrap_or(&normalized)
        .to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{PoolConfig, SidecarConfig};

    fn sandbox_config() -> BoxConfig {
        BoxConfig {
            isolation: ExecutionIsolation::Sandbox,
            ..Default::default()
        }
    }

    #[test]
    fn default_resolves_only_to_krun_hardware_vm() {
        let plan = resolve_execution(&BoxConfig::default()).unwrap();
        assert_eq!(plan.backend, ExecutionBackend::Krun);
        assert_eq!(plan.isolation_class, IsolationClass::HardwareVm);
        assert!(plan.required_controls.is_empty());
    }

    #[test]
    fn microvm_rejects_host_kernel_and_custom_security_profiles() {
        for (option, expected) in [
            ("apparmor=runtime/default", "AppArmor"),
            ("label=type:container_t", "SELinux"),
            ("seccomp=/profiles/restricted.json", "custom seccomp"),
            ("systempaths=unconfined", "security option"),
        ] {
            let config = BoxConfig {
                security_opt: vec![option.to_string()],
                ..Default::default()
            };
            let error = resolve_execution(&config).unwrap_err().to_string();
            assert!(
                error.contains(expected),
                "expected {option:?} rejection to mention {expected:?}, got {error:?}"
            );
        }
    }

    #[test]
    fn microvm_accepts_guest_enforceable_security_options() {
        let config = BoxConfig {
            security_opt: vec![
                " SECCOMP=DEFAULT ".to_string(),
                "seccomp=unconfined".to_string(),
                "no-new-privileges".to_string(),
                "no-new-privileges=false".to_string(),
            ],
            cap_add: vec!["NET_ADMIN".to_string()],
            cap_drop: vec!["NET_RAW".to_string()],
            privileged: true,
            ..Default::default()
        };

        assert!(resolve_execution(&config).is_ok());
    }

    #[test]
    fn sandbox_resolves_to_crun_shared_kernel_with_mandatory_controls() {
        let plan = resolve_execution(&sandbox_config()).unwrap();
        assert_eq!(plan.backend, ExecutionBackend::Crun);
        assert_eq!(plan.isolation_class, IsolationClass::SharedKernel);
        for required in SANDBOX_REQUIRED_CONTROLS {
            assert!(plan.required_controls.iter().any(|value| value == required));
        }
    }

    #[test]
    fn sandbox_rejects_vm_only_features_together() {
        let config = BoxConfig {
            isolation: ExecutionIsolation::Sandbox,
            tee: TeeConfig::Tdx {
                workload_id: "test".to_string(),
                simulate: true,
            },
            pool: PoolConfig {
                enabled: true,
                ..Default::default()
            },
            sidecar: Some(SidecarConfig::default()),
            port_map: vec!["8080:80".to_string()],
            privileged: true,
            ..Default::default()
        };

        let error = resolve_execution(&config).unwrap_err().to_string();
        assert!(error.contains("TEE and attestation"));
        assert!(error.contains("warm pools"));
        assert!(error.contains("vsock sidecars"));
        assert!(error.contains("published ports"));
        assert!(error.contains("privileged mode"));
    }

    #[test]
    fn sandbox_rejects_unconfined_seccomp() {
        let config = BoxConfig {
            security_opt: vec!["seccomp=unconfined".to_string()],
            ..sandbox_config()
        };
        assert!(resolve_execution(&config)
            .unwrap_err()
            .to_string()
            .contains("unconfined seccomp"));
    }

    #[test]
    fn sandbox_rejects_security_options_not_wired_to_oci() {
        for (option, expected) in [
            ("apparmor=runtime/default", "AppArmor"),
            ("label=type:container_t", "SELinux"),
            ("seccomp=/profiles/restricted.json", "custom seccomp"),
            ("no-new-privileges=false", "requires no-new-privileges"),
        ] {
            let config = BoxConfig {
                security_opt: vec![option.to_string()],
                ..sandbox_config()
            };
            let error = resolve_execution(&config).unwrap_err().to_string();
            assert!(
                error.contains(expected),
                "expected {option:?} rejection to mention {expected:?}, got {error:?}"
            );
        }
    }

    #[test]
    fn sandbox_accepts_security_options_compiled_by_oci_backend() {
        let config = BoxConfig {
            security_opt: vec![
                "seccomp=default".to_string(),
                "no-new-privileges".to_string(),
                "no-new-privileges=true".to_string(),
            ],
            cap_add: vec!["cap_chown".to_string()],
            cap_drop: vec!["NET_RAW".to_string()],
            ..sandbox_config()
        };

        assert!(resolve_execution(&config).is_ok());
    }

    #[test]
    fn sandbox_normalizes_and_allows_baseline_capabilities() {
        let config = BoxConfig {
            cap_add: vec!["cap_chown".to_string(), "NET_BIND_SERVICE".to_string()],
            ..sandbox_config()
        };
        assert!(resolve_execution(&config).is_ok());
    }

    #[test]
    fn sandbox_rejects_powerful_added_capability() {
        let config = BoxConfig {
            cap_add: vec!["CAP_SYS_ADMIN".to_string()],
            ..sandbox_config()
        };
        let error = resolve_execution(&config).unwrap_err().to_string();
        assert!(error.contains("SYS_ADMIN"));
    }
}