a3s-box-runtime 3.2.2

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
//! Durable filesystem baseline used by `a3s-box diff`.

use std::collections::HashMap;
use std::io::Read;
use std::path::{Component, Path};

use a3s_box_core::error::{BoxError, Result};
pub use a3s_box_core::rootfs_baseline::RootfsFileInfo;
use a3s_box_core::rootfs_baseline::{
    GuestDiffBaseline, GUEST_DIFF_BASELINE_FILE_NAME, MAX_GUEST_DIFF_BASELINE_BYTES,
};

/// Filename stored beside each box rootfs.
pub const DIFF_BASELINE_FILE: &str = "rootfs_snapshot.json";

/// Return whether the next guest generation must capture the first baseline.
///
/// An invalid existing destination is an error rather than a reason to replace
/// it. This preserves first-writer-wins semantics and fails before VM launch.
pub fn guest_diff_baseline_required(box_dir: &Path) -> Result<bool> {
    existing_baseline_is_regular(&box_dir.join(DIFF_BASELINE_FILE)).map(|exists| !exists)
}

/// Capture a rootfs tree while excluding runtime-owned control files.
pub fn walk_rootfs(root: &Path) -> Result<HashMap<String, RootfsFileInfo>> {
    let mut entries = HashMap::new();
    walk_recursive(root, root, &mut entries)?;
    Ok(entries)
}

/// Persist the first pristine baseline for a box without ever replacing it.
///
/// Runtime callers invoke this after all host-side rootfs preparation and before
/// the workload starts. Later starts and monitor recovery therefore preserve the
/// original generation's baseline instead of racing a fast container command.
pub fn create_diff_baseline_if_absent(box_dir: &Path, rootfs: &Path) -> Result<()> {
    if existing_baseline_is_regular(&box_dir.join(DIFF_BASELINE_FILE))? {
        return Ok(());
    }

    let entries = walk_rootfs(rootfs)?;
    let encoded = serde_json::to_vec(&entries).map_err(|error| {
        BoxError::BuildError(format!("failed to encode rootfs baseline: {error}"))
    })?;
    install_baseline_bytes_if_absent(box_dir, &encoded)
}

/// Validate and publish the baseline captured inside guest-init.
///
/// The guest writes only to a private pre-opened control file. The host treats
/// that payload as untrusted, enforces its schema and bounds, then atomically
/// installs the legacy map representation consumed by `a3s-box diff`.
pub fn publish_guest_diff_baseline(box_dir: &Path) -> Result<()> {
    let destination = box_dir.join(DIFF_BASELINE_FILE);
    if existing_baseline_is_regular(&destination)? {
        consume_guest_diff_baseline_handoff(box_dir);
        return Ok(());
    }

    let source = box_dir
        .join("runtime-control")
        .join(GUEST_DIFF_BASELINE_FILE_NAME);
    let metadata = std::fs::symlink_metadata(&source).map_err(|error| {
        BoxError::BuildError(format!(
            "failed to inspect guest diff baseline {}: {error}",
            source.display()
        ))
    })?;
    if !metadata.is_file()
        || metadata.file_type().is_symlink()
        || metadata.len() == 0
        || metadata.len() > MAX_GUEST_DIFF_BASELINE_BYTES as u64
    {
        return Err(BoxError::BuildError(format!(
            "guest diff baseline is not a bounded plain file: {}",
            source.display()
        )));
    }

    let mut options = std::fs::OpenOptions::new();
    options.read(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW);
    }
    let file = options.open(&source).map_err(|error| {
        BoxError::BuildError(format!(
            "failed to open guest diff baseline {}: {error}",
            source.display()
        ))
    })?;
    let mut bytes = Vec::with_capacity(metadata.len() as usize);
    file.take(MAX_GUEST_DIFF_BASELINE_BYTES as u64 + 1)
        .read_to_end(&mut bytes)
        .map_err(BoxError::IoError)?;
    if bytes.is_empty() || bytes.len() > MAX_GUEST_DIFF_BASELINE_BYTES {
        return Err(BoxError::BuildError(format!(
            "guest diff baseline exceeded its {} byte limit",
            MAX_GUEST_DIFF_BASELINE_BYTES
        )));
    }
    let baseline: GuestDiffBaseline = serde_json::from_slice(&bytes).map_err(|error| {
        BoxError::BuildError(format!("failed to decode guest diff baseline: {error}"))
    })?;
    baseline
        .validate()
        .map_err(|error| BoxError::BuildError(format!("invalid guest diff baseline: {error}")))?;
    let encoded = serde_json::to_vec(&baseline.entries).map_err(|error| {
        BoxError::BuildError(format!("failed to encode published diff baseline: {error}"))
    })?;
    install_baseline_bytes_if_absent(box_dir, &encoded)?;
    consume_guest_diff_baseline_handoff(box_dir);
    Ok(())
}

fn consume_guest_diff_baseline_handoff(box_dir: &Path) {
    let control_dir = box_dir.join("runtime-control");
    let source = control_dir.join(GUEST_DIFF_BASELINE_FILE_NAME);
    let result = match std::fs::symlink_metadata(&source) {
        Ok(metadata) if metadata.is_file() || metadata.file_type().is_symlink() => {
            std::fs::remove_file(&source).and_then(|()| sync_directory(&control_dir))
        }
        Ok(_) => Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!(
                "guest diff baseline handoff is not removable as a file: {}",
                source.display()
            ),
        )),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error),
    };
    if let Err(error) = result {
        // The canonical baseline is already durable. Cleanup failure must not
        // turn a successful boot into a failed generation.
        tracing::warn!(
            path = %source.display(),
            error = %error,
            "Failed to consume one-shot guest diff baseline handoff"
        );
    }
}

fn existing_baseline_is_regular(destination: &Path) -> Result<bool> {
    match std::fs::symlink_metadata(destination) {
        Ok(metadata) if metadata.is_file() && !metadata.file_type().is_symlink() => Ok(true),
        Ok(_) => Err(BoxError::BuildError(format!(
            "diff baseline destination is not a plain file: {}",
            destination.display()
        ))),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(BoxError::IoError(error)),
    }
}

fn install_baseline_bytes_if_absent(box_dir: &Path, encoded: &[u8]) -> Result<()> {
    let destination = box_dir.join(DIFF_BASELINE_FILE);
    let temporary = box_dir.join(format!(
        ".{DIFF_BASELINE_FILE}.{}.tmp",
        uuid::Uuid::new_v4().simple()
    ));
    {
        use std::io::Write;
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&temporary)
            .map_err(BoxError::IoError)?;
        file.write_all(encoded).map_err(BoxError::IoError)?;
        file.sync_all().map_err(BoxError::IoError)?;
    }

    // hard_link installs the fully-written file only when the destination is
    // still absent. A concurrent lifecycle helper that won the race remains the
    // authority; unlike rename, this never overwrites its earlier baseline.
    let install = std::fs::hard_link(&temporary, &destination);
    let _ = std::fs::remove_file(&temporary);
    match install {
        Ok(()) => sync_directory(box_dir).map_err(BoxError::IoError),
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
            existing_baseline_is_regular(&destination).map(|_| ())
        }
        Err(error) => Err(BoxError::IoError(error)),
    }
}

#[cfg(unix)]
fn sync_directory(path: &Path) -> std::io::Result<()> {
    std::fs::File::open(path)?.sync_all()
}

#[cfg(not(unix))]
fn sync_directory(_path: &Path) -> std::io::Result<()> {
    Ok(())
}

fn walk_recursive(
    root: &Path,
    current: &Path,
    entries: &mut HashMap<String, RootfsFileInfo>,
) -> Result<()> {
    let directory = match std::fs::read_dir(current) {
        Ok(directory) => directory,
        Err(error) if error.kind() == std::io::ErrorKind::PermissionDenied => return Ok(()),
        Err(error) => return Err(BoxError::IoError(error)),
    };

    for entry in directory {
        let entry = entry.map_err(BoxError::IoError)?;
        let path = entry.path();
        let relative = match path.strip_prefix(root) {
            Ok(relative) => relative,
            Err(_) => continue,
        };
        if a3s_box_core::rootfs_metadata::is_runtime_internal_rootfs_path(relative) {
            continue;
        }

        // Do not follow symlinks while walking a container-controlled tree.
        let metadata = match std::fs::symlink_metadata(&path) {
            Ok(metadata) => metadata,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue,
            Err(error) if error.kind() == std::io::ErrorKind::PermissionDenied => continue,
            Err(error) => return Err(BoxError::IoError(error)),
        };

        #[cfg(unix)]
        let mode = {
            use std::os::unix::fs::MetadataExt;
            metadata.mode()
        };
        #[cfg(not(unix))]
        let mode = 0;

        entries.insert(
            rootfs_path_string(relative),
            RootfsFileInfo {
                size: metadata.len(),
                mode,
                is_dir: metadata.is_dir(),
            },
        );
        if metadata.is_dir() {
            walk_recursive(root, &path, entries)?;
        }
    }
    Ok(())
}

fn rootfs_path_string(relative: &Path) -> String {
    let segments = relative
        .components()
        .filter_map(|component| match component {
            Component::Normal(segment) => Some(segment.to_string_lossy()),
            _ => None,
        })
        .collect::<Vec<_>>();
    format!("/{}", segments.join("/"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeMap;

    #[test]
    fn baseline_is_first_writer_wins_and_excludes_runtime_control_files() {
        let directory = tempfile::tempdir().unwrap();
        let box_dir = directory.path().join("box");
        let rootfs = box_dir.join("rootfs");
        std::fs::create_dir_all(rootfs.join("root")).unwrap();
        std::fs::write(rootfs.join("root/original.txt"), b"original").unwrap();
        std::fs::write(rootfs.join(".a3s-box-exec.json"), b"runtime").unwrap();

        create_diff_baseline_if_absent(&box_dir, &rootfs).unwrap();
        std::fs::write(rootfs.join("root/later.txt"), b"later").unwrap();
        create_diff_baseline_if_absent(&box_dir, &rootfs).unwrap();

        let encoded = std::fs::read(box_dir.join(DIFF_BASELINE_FILE)).unwrap();
        let baseline: HashMap<String, RootfsFileInfo> = serde_json::from_slice(&encoded).unwrap();
        assert!(baseline.contains_key("/root/original.txt"));
        assert!(!baseline.contains_key("/root/later.txt"));
        assert!(!baseline.contains_key("/.a3s-box-exec.json"));
    }

    #[test]
    fn guest_baseline_is_validated_and_published_as_legacy_map() {
        let directory = tempfile::tempdir().unwrap();
        let box_dir = directory.path().join("box");
        let control_dir = box_dir.join("runtime-control");
        std::fs::create_dir_all(&control_dir).unwrap();
        let baseline = GuestDiffBaseline::new(BTreeMap::from([(
            "/usr/bin/tool".to_string(),
            RootfsFileInfo {
                size: 17,
                mode: 0o100755,
                is_dir: false,
            },
        )]));
        std::fs::write(
            control_dir.join(GUEST_DIFF_BASELINE_FILE_NAME),
            serde_json::to_vec(&baseline).unwrap(),
        )
        .unwrap();

        publish_guest_diff_baseline(&box_dir).unwrap();

        let published: HashMap<String, RootfsFileInfo> =
            serde_json::from_slice(&std::fs::read(box_dir.join(DIFF_BASELINE_FILE)).unwrap())
                .unwrap();
        assert_eq!(
            published.get("/usr/bin/tool"),
            baseline.entries.get("/usr/bin/tool")
        );
        assert!(!control_dir.join(GUEST_DIFF_BASELINE_FILE_NAME).exists());
        assert!(!guest_diff_baseline_required(&box_dir).unwrap());
    }

    #[test]
    fn guest_baseline_is_requested_only_for_the_first_generation() {
        let directory = tempfile::tempdir().unwrap();
        let box_dir = directory.path().join("box");
        std::fs::create_dir_all(&box_dir).unwrap();

        assert!(guest_diff_baseline_required(&box_dir).unwrap());
        std::fs::write(box_dir.join(DIFF_BASELINE_FILE), b"{}").unwrap();
        assert!(!guest_diff_baseline_required(&box_dir).unwrap());
    }

    #[test]
    fn guest_baseline_never_replaces_the_first_published_generation() {
        let directory = tempfile::tempdir().unwrap();
        let box_dir = directory.path().join("box");
        let rootfs = box_dir.join("rootfs");
        let control_dir = box_dir.join("runtime-control");
        std::fs::create_dir_all(&rootfs).unwrap();
        std::fs::create_dir_all(&control_dir).unwrap();
        std::fs::write(rootfs.join("original"), b"one").unwrap();
        create_diff_baseline_if_absent(&box_dir, &rootfs).unwrap();

        let replacement = GuestDiffBaseline::new(BTreeMap::from([(
            "/replacement".to_string(),
            RootfsFileInfo {
                size: 3,
                mode: 0o100644,
                is_dir: false,
            },
        )]));
        std::fs::write(
            control_dir.join(GUEST_DIFF_BASELINE_FILE_NAME),
            serde_json::to_vec(&replacement).unwrap(),
        )
        .unwrap();

        publish_guest_diff_baseline(&box_dir).unwrap();

        let published: HashMap<String, RootfsFileInfo> =
            serde_json::from_slice(&std::fs::read(box_dir.join(DIFF_BASELINE_FILE)).unwrap())
                .unwrap();
        assert!(published.contains_key("/original"));
        assert!(!published.contains_key("/replacement"));
        assert!(!control_dir.join(GUEST_DIFF_BASELINE_FILE_NAME).exists());
    }

    #[test]
    fn invalid_guest_baseline_is_not_published() {
        let directory = tempfile::tempdir().unwrap();
        let box_dir = directory.path().join("box");
        let control_dir = box_dir.join("runtime-control");
        std::fs::create_dir_all(&control_dir).unwrap();
        std::fs::write(
            control_dir.join(GUEST_DIFF_BASELINE_FILE_NAME),
            br#"{"schema":"a3s.box.guest-diff-baseline.v2","entries":{}}"#,
        )
        .unwrap();

        assert!(publish_guest_diff_baseline(&box_dir).is_err());
        assert!(!box_dir.join(DIFF_BASELINE_FILE).exists());
    }

    #[cfg(unix)]
    #[test]
    fn guest_baseline_control_path_must_not_be_a_symlink() {
        let directory = tempfile::tempdir().unwrap();
        let box_dir = directory.path().join("box");
        let control_dir = box_dir.join("runtime-control");
        let outside = directory.path().join("outside.json");
        std::fs::create_dir_all(&control_dir).unwrap();
        std::fs::write(&outside, b"{}").unwrap();
        std::os::unix::fs::symlink(&outside, control_dir.join(GUEST_DIFF_BASELINE_FILE_NAME))
            .unwrap();

        assert!(publish_guest_diff_baseline(&box_dir).is_err());
        assert!(!box_dir.join(DIFF_BASELINE_FILE).exists());
    }

    #[cfg(unix)]
    #[test]
    fn walk_does_not_follow_rootfs_symlinks() {
        let directory = tempfile::tempdir().unwrap();
        let outside = directory.path().join("outside");
        let rootfs = directory.path().join("rootfs");
        std::fs::create_dir_all(&outside).unwrap();
        std::fs::create_dir_all(&rootfs).unwrap();
        std::fs::write(outside.join("secret"), b"secret").unwrap();
        std::os::unix::fs::symlink(&outside, rootfs.join("escape")).unwrap();

        let entries = walk_rootfs(&rootfs).unwrap();
        assert!(entries.contains_key("/escape"));
        assert!(!entries.contains_key("/escape/secret"));
    }
}