a3s-box-runtime 3.2.0

MicroVM runtime engine — VM lifecycle, OCI images, attestation, networking
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! Box-owned aliases for caller-owned read-only Sandbox attachments.
//!
//! A3S OCI resolves bind sources after entering the workload user namespace.
//! A readable Artifact root below a private provider directory can therefore be
//! inaccessible even though the Box service itself opened it successfully.
//! These aliases pin the already-open source into the Box-owned Sandbox tree;
//! caller permissions and storage ownership remain unchanged.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use a3s_box_core::error::{BoxError, Result};

use super::SandboxIdMappingPlan;

#[cfg(target_os = "linux")]
const ATTACHMENTS_DIRECTORY: &str = "sandbox/attachments";

#[cfg(target_os = "linux")]
pub(crate) fn sandbox_mount_alias_root(home_dir: &Path, box_id: &str) -> PathBuf {
    home_dir
        .join("boxes")
        .join(box_id)
        .join(ATTACHMENTS_DIRECTORY)
}

/// Replace each distinct caller-owned source with one read-only bind alias.
///
/// Existing aliases are removed first, so a persistent Sandbox restart never
/// reuses a mount from an older runtime generation. A partial preparation is
/// rolled back before the error is returned.
#[cfg(target_os = "linux")]
pub(crate) fn stage_read_only_mount_aliases(
    home_dir: &Path,
    box_id: &str,
    sources: &[PathBuf],
    id_mappings: &SandboxIdMappingPlan,
) -> Result<HashMap<PathBuf, PathBuf>> {
    cleanup_sandbox_mount_aliases(home_dir, box_id)?;
    if sources.is_empty() {
        return Ok(HashMap::new());
    }

    let root = sandbox_mount_alias_root(home_dir, box_id);
    create_alias_root(&root)?;
    let mut aliases = HashMap::new();

    for source in sources {
        if aliases.contains_key(source) {
            continue;
        }
        let target = root.join(format!("{:04}", aliases.len()));
        if let Err(error) = stage_one_alias(source, &target, id_mappings) {
            let rollback = cleanup_sandbox_mount_aliases(home_dir, box_id);
            return match rollback {
                Ok(()) => Err(error),
                Err(rollback_error) => Err(BoxError::BoxBootError {
                    message: format!(
                        "Failed to stage Sandbox attachment {}: {error}; rollback also failed: {rollback_error}",
                        source.display()
                    ),
                    hint: Some(
                        "Reconcile the Box attachment mounts before retrying the Sandbox".into(),
                    ),
                }),
            };
        }
        aliases.insert(source.clone(), target);
    }

    Ok(aliases)
}

#[cfg(not(target_os = "linux"))]
pub(crate) fn stage_read_only_mount_aliases(
    _home_dir: &Path,
    _box_id: &str,
    _sources: &[PathBuf],
    _id_mappings: &SandboxIdMappingPlan,
) -> Result<HashMap<PathBuf, PathBuf>> {
    Err(BoxError::ConfigError(
        "Sandbox attachment aliases require Linux".into(),
    ))
}

/// Unmount and remove every alias owned by one Sandbox generation.
///
/// Mount points are discovered from mountinfo instead of trusting directory
/// entries left by a crashed process. Nothing below the alias root is deleted
/// until every attached mount has been detached.
#[cfg(target_os = "linux")]
pub(crate) fn cleanup_sandbox_mount_aliases(home_dir: &Path, box_id: &str) -> Result<()> {
    let root = sandbox_mount_alias_root(home_dir, box_id);
    let mountinfo = std::fs::read_to_string("/proc/self/mountinfo").map_err(|error| {
        BoxError::StateError(format!(
            "Failed to inspect Sandbox attachment mounts for {box_id}: {error}"
        ))
    })?;
    let mut mount_points = mount_points_below(&mountinfo, &root);
    mount_points.sort_by(|left, right| {
        right
            .components()
            .count()
            .cmp(&left.components().count())
            .then_with(|| right.cmp(left))
    });
    for mount_point in mount_points {
        detach_mount(&mount_point).map_err(|error| {
            BoxError::StateError(format!(
                "Failed to detach Sandbox attachment alias {}: {error}",
                mount_point.display()
            ))
        })?;
    }

    match std::fs::symlink_metadata(&root) {
        Ok(metadata) if metadata.file_type().is_symlink() => {
            std::fs::remove_file(&root).map_err(BoxError::IoError)?;
            Err(BoxError::StateError(format!(
                "Sandbox attachment root was an unsafe symlink and was removed: {}",
                root.display()
            )))
        }
        Ok(metadata) if !metadata.is_dir() => Err(BoxError::StateError(format!(
            "Sandbox attachment root is not a directory: {}",
            root.display()
        ))),
        Ok(_) => std::fs::remove_dir_all(&root).map_err(BoxError::IoError),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(BoxError::IoError(error)),
    }
}

#[cfg(not(target_os = "linux"))]
pub(crate) fn cleanup_sandbox_mount_aliases(_home_dir: &Path, _box_id: &str) -> Result<()> {
    Ok(())
}

#[cfg(target_os = "linux")]
fn create_alias_root(root: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;

    let sandbox_dir = root.parent().ok_or_else(|| {
        BoxError::StateError(format!(
            "Sandbox attachment root has no managed parent: {}",
            root.display()
        ))
    })?;
    let box_dir = sandbox_dir.parent().ok_or_else(|| {
        BoxError::StateError(format!(
            "Sandbox attachment parent has no managed box directory: {}",
            sandbox_dir.display()
        ))
    })?;
    require_plain_managed_directory(box_dir, "box")?;
    match std::fs::create_dir(sandbox_dir) {
        Ok(()) => {}
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
        Err(error) => {
            return Err(BoxError::BoxBootError {
                message: format!(
                    "Failed to create Sandbox attachment parent {}: {error}",
                    sandbox_dir.display()
                ),
                hint: None,
            })
        }
    }
    require_plain_managed_directory(sandbox_dir, "attachment parent")?;
    std::fs::create_dir(root).map_err(|error| BoxError::BoxBootError {
        message: format!(
            "Failed to create Sandbox attachment root {}: {error}",
            root.display()
        ),
        hint: None,
    })?;
    std::fs::set_permissions(root, std::fs::Permissions::from_mode(0o711)).map_err(|error| {
        BoxError::BoxBootError {
            message: format!(
                "Failed to secure Sandbox attachment root {}: {error}",
                root.display()
            ),
            hint: None,
        }
    })
}

#[cfg(target_os = "linux")]
fn require_plain_managed_directory(path: &Path, label: &str) -> Result<()> {
    let metadata = std::fs::symlink_metadata(path).map_err(|error| BoxError::BoxBootError {
        message: format!(
            "Failed to inspect Sandbox {label} directory {}: {error}",
            path.display()
        ),
        hint: None,
    })?;
    if metadata.file_type().is_symlink() || !metadata.is_dir() {
        return Err(BoxError::StateError(format!(
            "Sandbox {label} is not a managed directory: {}",
            path.display()
        )));
    }
    Ok(())
}

#[cfg(target_os = "linux")]
fn stage_one_alias(source: &Path, target: &Path, id_mappings: &SandboxIdMappingPlan) -> Result<()> {
    use std::os::fd::{AsRawFd, FromRawFd};
    use std::os::unix::ffi::OsStrExt;

    let source_metadata = std::fs::symlink_metadata(source).map_err(BoxError::IoError)?;
    let canonical_source = source.canonicalize().map_err(BoxError::IoError)?;
    if source_metadata.file_type().is_symlink() || canonical_source != source {
        return Err(BoxError::ConfigError(format!(
            "Sandbox attachment source must be a canonical plain path: {}",
            source.display()
        )));
    }

    let source_c = std::ffi::CString::new(source.as_os_str().as_bytes()).map_err(|_| {
        BoxError::ConfigError(format!(
            "Sandbox attachment source contains NUL: {}",
            source.display()
        ))
    })?;
    let raw_fd = unsafe {
        libc::open(
            source_c.as_ptr(),
            libc::O_PATH | libc::O_CLOEXEC | libc::O_NOFOLLOW,
        )
    };
    if raw_fd < 0 {
        return Err(BoxError::IoError(std::io::Error::last_os_error()));
    }
    let source_file = unsafe { std::fs::File::from_raw_fd(raw_fd) };
    let metadata = source_file.metadata().map_err(BoxError::IoError)?;
    super::rootfs::validate_external_mount_root_metadata(source, &metadata, id_mappings, true)?;

    if metadata.is_dir() {
        std::fs::create_dir(target).map_err(BoxError::IoError)?;
    } else {
        std::fs::File::create(target).map_err(BoxError::IoError)?;
    }

    let proc_source = std::ffi::CString::new(format!("/proc/self/fd/{}", source_file.as_raw_fd()))
        .map_err(|error| {
            BoxError::ConfigError(format!("Invalid attachment descriptor: {error}"))
        })?;
    let target_c = std::ffi::CString::new(target.as_os_str().as_bytes()).map_err(|_| {
        BoxError::ConfigError(format!(
            "Sandbox attachment alias contains NUL: {}",
            target.display()
        ))
    })?;
    let mounted = unsafe {
        libc::mount(
            proc_source.as_ptr(),
            target_c.as_ptr(),
            std::ptr::null(),
            libc::MS_BIND,
            std::ptr::null(),
        )
    };
    if mounted != 0 {
        return Err(BoxError::BoxBootError {
            message: format!(
                "Failed to bind Sandbox attachment {} at {}: {}",
                source.display(),
                target.display(),
                std::io::Error::last_os_error()
            ),
            hint: Some("Run the Sandbox service with its required mount capability".into()),
        });
    }

    let private = unsafe {
        libc::mount(
            std::ptr::null(),
            target_c.as_ptr(),
            std::ptr::null(),
            libc::MS_PRIVATE | libc::MS_REC,
            std::ptr::null(),
        )
    };
    if private != 0 {
        let error = std::io::Error::last_os_error();
        let _ = detach_mount(target);
        return Err(BoxError::BoxBootError {
            message: format!(
                "Failed to make Sandbox attachment alias {} private: {error}",
                target.display()
            ),
            hint: None,
        });
    }

    let read_only = unsafe {
        libc::mount(
            std::ptr::null(),
            target_c.as_ptr(),
            std::ptr::null(),
            libc::MS_BIND | libc::MS_REMOUNT | libc::MS_RDONLY | libc::MS_NOSUID | libc::MS_NODEV,
            std::ptr::null(),
        )
    };
    if read_only != 0 {
        let error = std::io::Error::last_os_error();
        let _ = detach_mount(target);
        return Err(BoxError::BoxBootError {
            message: format!(
                "Failed to make Sandbox attachment alias {} read-only: {error}",
                target.display()
            ),
            hint: None,
        });
    }

    let mountinfo = std::fs::read_to_string("/proc/self/mountinfo").map_err(BoxError::IoError)?;
    if !mount_is_read_only(&mountinfo, target) {
        let _ = detach_mount(target);
        return Err(BoxError::BoxBootError {
            message: format!(
                "Sandbox attachment alias did not become read-only: {}",
                target.display()
            ),
            hint: None,
        });
    }
    Ok(())
}

#[cfg(target_os = "linux")]
fn detach_mount(target: &Path) -> std::io::Result<()> {
    use std::os::unix::ffi::OsStrExt;

    let target = std::ffi::CString::new(target.as_os_str().as_bytes())
        .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "mount path has NUL"))?;
    if unsafe { libc::umount2(target.as_ptr(), 0) } == 0 {
        return Ok(());
    }
    let error = std::io::Error::last_os_error();
    if matches!(
        error.raw_os_error(),
        Some(libc::EINVAL) | Some(libc::ENOENT)
    ) {
        return Ok(());
    }
    if unsafe { libc::umount2(target.as_ptr(), libc::MNT_DETACH) } == 0 {
        return Ok(());
    }
    Err(std::io::Error::last_os_error())
}

#[cfg(target_os = "linux")]
fn mount_points_below(mountinfo: &str, root: &Path) -> Vec<PathBuf> {
    mountinfo
        .lines()
        .filter_map(|line| line.split_whitespace().nth(4))
        .map(decode_mountinfo_path)
        .map(PathBuf::from)
        .filter(|path| path == root || path.starts_with(root))
        .collect()
}

#[cfg(target_os = "linux")]
fn mount_is_read_only(mountinfo: &str, target: &Path) -> bool {
    mountinfo.lines().any(|line| {
        let mut fields = line.split_whitespace();
        let mount_point = fields.nth(4).map(decode_mountinfo_path);
        let options = fields.next();
        mount_point.as_deref() == target.to_str()
            && options.is_some_and(|options| options.split(',').any(|option| option == "ro"))
    })
}

#[cfg(target_os = "linux")]
fn decode_mountinfo_path(value: &str) -> String {
    value
        .replace("\\040", " ")
        .replace("\\011", "\t")
        .replace("\\012", "\n")
        .replace("\\134", "\\")
}

#[cfg(all(test, target_os = "linux"))]
mod tests {
    use std::os::unix::fs::PermissionsExt;

    use super::*;
    use crate::sandbox::{validate_external_mount_access, IdMapping};

    fn mappings() -> SandboxIdMappingPlan {
        SandboxIdMappingPlan {
            uid_mappings: vec![IdMapping {
                container_id: 0,
                host_id: 100_000,
                size: 1,
            }],
            gid_mappings: vec![IdMapping {
                container_id: 0,
                host_id: 100_000,
                size: 1,
            }],
            maximum_container_uid: 0,
            maximum_container_gid: 0,
        }
    }

    fn can_mount() -> bool {
        if unsafe { libc::geteuid() } != 0 {
            return false;
        }
        std::fs::read_to_string("/proc/self/status")
            .ok()
            .and_then(|status| {
                status
                    .lines()
                    .find_map(|line| line.strip_prefix("CapEff:\t"))
                    .and_then(|value| u64::from_str_radix(value, 16).ok())
            })
            .is_some_and(|capabilities| capabilities & (1 << 21) != 0)
    }

    fn set_mode(path: &Path, mode: u32) {
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)).unwrap();
    }

    #[test]
    fn alias_root_creates_its_missing_managed_sandbox_parent() {
        let fixture = tempfile::tempdir().unwrap();
        let home = fixture.path().join("a3s");
        std::fs::create_dir_all(home.join("boxes/execution-1")).unwrap();
        let root = sandbox_mount_alias_root(&home, "execution-1");

        create_alias_root(&root).unwrap();

        assert!(root.is_dir());
        assert_eq!(
            std::fs::metadata(&root).unwrap().permissions().mode() & 0o7777,
            0o711
        );
        require_plain_managed_directory(root.parent().unwrap(), "attachment parent").unwrap();
    }

    #[test]
    fn alias_root_rejects_a_symlinked_sandbox_parent() {
        let fixture = tempfile::tempdir().unwrap();
        let home = fixture.path().join("a3s");
        let box_dir = home.join("boxes/execution-1");
        let outside = fixture.path().join("outside");
        std::fs::create_dir_all(&box_dir).unwrap();
        std::fs::create_dir(&outside).unwrap();
        std::os::unix::fs::symlink(&outside, box_dir.join("sandbox")).unwrap();
        let root = sandbox_mount_alias_root(&home, "execution-1");

        let error = create_alias_root(&root).unwrap_err();

        assert!(error.to_string().contains("not a managed directory"));
        assert!(!outside.join("attachments").exists());
    }

    #[test]
    fn aliases_a_read_only_source_below_a_private_caller_root() {
        if !can_mount() {
            return;
        }
        let fixture = tempfile::tempdir().unwrap();
        set_mode(fixture.path(), 0o755);
        let private = fixture.path().join("provider");
        let source = private.join("artifact/root");
        std::fs::create_dir_all(&source).unwrap();
        std::fs::write(source.join("model.bin"), b"immutable").unwrap();
        set_mode(&private, 0o700);
        set_mode(&source, 0o755);

        let home = fixture.path().join("a3s");
        std::fs::create_dir_all(home.join("boxes/execution-1/sandbox")).unwrap();
        assert!(validate_external_mount_access(&source, &mappings(), true).is_err());

        let aliases = stage_read_only_mount_aliases(
            &home,
            "execution-1",
            std::slice::from_ref(&source),
            &mappings(),
        )
        .unwrap();
        let alias = aliases.get(&source).unwrap();
        assert_eq!(
            std::fs::read(alias.join("model.bin")).unwrap(),
            b"immutable"
        );
        assert!(validate_external_mount_access(alias, &mappings(), true).is_ok());
        let mountinfo = std::fs::read_to_string("/proc/self/mountinfo").unwrap();
        assert!(mount_is_read_only(&mountinfo, alias));
        assert_eq!(
            std::fs::metadata(&private).unwrap().permissions().mode() & 0o7777,
            0o700
        );

        cleanup_sandbox_mount_aliases(&home, "execution-1").unwrap();
        assert!(!sandbox_mount_alias_root(&home, "execution-1").exists());
        assert_eq!(
            std::fs::read(source.join("model.bin")).unwrap(),
            b"immutable"
        );
    }

    #[test]
    fn failed_alias_set_rolls_back_earlier_mounts() {
        if !can_mount() {
            return;
        }
        let fixture = tempfile::tempdir().unwrap();
        set_mode(fixture.path(), 0o755);
        let readable = fixture.path().join("readable");
        let unreadable = fixture.path().join("unreadable");
        std::fs::create_dir(&readable).unwrap();
        std::fs::create_dir(&unreadable).unwrap();
        set_mode(&readable, 0o755);
        set_mode(&unreadable, 0o700);
        let home = fixture.path().join("a3s");
        std::fs::create_dir_all(home.join("boxes/execution-2/sandbox")).unwrap();

        assert!(stage_read_only_mount_aliases(
            &home,
            "execution-2",
            &[readable, unreadable],
            &mappings(),
        )
        .is_err());
        let root = sandbox_mount_alias_root(&home, "execution-2");
        assert!(!root.exists());
        assert!(mount_points_below(
            &std::fs::read_to_string("/proc/self/mountinfo").unwrap(),
            &root
        )
        .is_empty());
    }
}