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
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
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::io::Read;
use std::path::{Path, PathBuf};

use a3s_box_core::error::{BoxError, Result};
use mkext4::{Meta, SpecialKind};

use super::super::ext4::sparse::SparseLayout;

pub(super) type GuestPath = Vec<Vec<u8>>;
pub(super) type NodeId = u64;

#[derive(Clone, Copy)]
pub(super) struct NamespaceEntry {
    pub(super) node: NodeId,
    generation: u64,
}

const MAX_SYMLINK_HOPS: usize = 40;
const MAX_LOGICAL_ENTRIES: usize = 1_000_000;
const MAX_GUEST_PATH_BYTES: usize = 4095;

#[derive(Clone)]
pub(super) struct EntryMetadata {
    pub(super) meta: Meta,
    pub(super) xattrs: BTreeMap<String, Vec<u8>>,
}

pub(super) enum NodeKind {
    Directory,
    Regular {
        source: PathBuf,
        size: u64,
        sparse: Option<SparseLayout>,
    },
    Symlink {
        target: Vec<u8>,
    },
    Special(SpecialKind),
}

pub(super) struct Node {
    pub(super) kind: NodeKind,
    pub(super) meta: Meta,
    pub(super) xattrs: BTreeMap<String, Vec<u8>>,
    links: usize,
}

#[derive(Debug)]
enum ResolveComponent {
    Parent,
    Normal(Vec<u8>),
}

/// A Linux namespace represented only by raw guest paths and opaque content
/// files. No guest directory name is ever materialized on the host.
pub(super) struct LogicalRootfs {
    pub(super) spool: tempfile::TempDir,
    pub(super) spool_sequence: u64,
    next_node: NodeId,
    current_generation: u64,
    pub(super) entries: BTreeMap<GuestPath, NamespaceEntry>,
    pub(super) nodes: HashMap<NodeId, Node>,
}

impl LogicalRootfs {
    pub(super) fn new(spool_parent: &Path) -> Result<Self> {
        std::fs::create_dir_all(spool_parent).map_err(BoxError::IoError)?;
        let spool = tempfile::Builder::new()
            .prefix(super::CONTENT_STAGING_PREFIX)
            .tempdir_in(spool_parent)
            .map_err(|error| {
                BoxError::BuildError(format!(
                    "Failed to create OCI ext4 content spool in {}: {error}",
                    spool_parent.display()
                ))
            })?;
        let root = Node {
            kind: NodeKind::Directory,
            meta: canonical_meta(0o755),
            xattrs: BTreeMap::new(),
            links: 1,
        };
        Ok(Self {
            spool,
            spool_sequence: 0,
            next_node: 1,
            current_generation: 0,
            entries: BTreeMap::from([(
                Vec::new(),
                NamespaceEntry {
                    node: 0,
                    generation: 0,
                },
            )]),
            nodes: HashMap::from([(0, root)]),
        })
    }

    pub(super) fn begin_layer(&mut self) -> Result<()> {
        self.current_generation = self
            .current_generation
            .checked_add(1)
            .ok_or_else(|| build_error("OCI layer generation overflow"))?;
        Ok(())
    }

    pub(super) fn directory(
        &mut self,
        archive_path: &GuestPath,
        metadata: EntryMetadata,
    ) -> Result<()> {
        if archive_path.is_empty() {
            let root = self.nodes.get_mut(&0).expect("logical root inode");
            root.meta = metadata.meta;
            root.xattrs.extend(metadata.xattrs);
            return Ok(());
        }
        let destination = self.resolve_destination(archive_path, true)?;
        if let Some(entry) = self.entries.get(&destination).copied() {
            if matches!(self.nodes[&entry.node].kind, NodeKind::Directory) {
                let node = self.nodes.get_mut(&entry.node).expect("directory inode");
                node.meta = metadata.meta;
                node.xattrs.extend(metadata.xattrs);
                self.entries
                    .get_mut(&destination)
                    .expect("directory namespace entry")
                    .generation = self.current_generation;
                return Ok(());
            }
        }
        self.replace_with_new(
            destination,
            NodeKind::Directory,
            metadata.meta,
            metadata.xattrs,
        )?;
        Ok(())
    }

    pub(super) fn regular<R: Read>(
        &mut self,
        archive_path: &GuestPath,
        metadata: EntryMetadata,
        reader: &mut R,
        expected_size: u64,
    ) -> Result<()> {
        let destination = self.resolve_destination(archive_path, true)?;
        let (source, sparse) = self.spool(reader, expected_size)?;
        self.replace_with_new(
            destination,
            NodeKind::Regular {
                source,
                size: expected_size,
                sparse,
            },
            metadata.meta,
            metadata.xattrs,
        )?;
        Ok(())
    }

    pub(super) fn symlink(
        &mut self,
        archive_path: &GuestPath,
        metadata: EntryMetadata,
        target: Vec<u8>,
    ) -> Result<()> {
        validate_symlink_target(&target)?;
        let destination = self.resolve_destination(archive_path, true)?;
        self.replace_with_new(
            destination,
            NodeKind::Symlink { target },
            metadata.meta,
            metadata.xattrs,
        )?;
        Ok(())
    }

    pub(super) fn special(
        &mut self,
        archive_path: &GuestPath,
        metadata: EntryMetadata,
        kind: SpecialKind,
    ) -> Result<()> {
        let destination = self.resolve_destination(archive_path, true)?;
        self.replace_with_new(
            destination,
            NodeKind::Special(kind),
            metadata.meta,
            metadata.xattrs,
        )?;
        Ok(())
    }

    pub(super) fn hardlink(&mut self, archive_path: &GuestPath, target: &GuestPath) -> Result<()> {
        let target = self
            .resolve_existing_entry(target, false)?
            .ok_or_else(|| build_error("OCI hardlink target does not exist"))?;
        let target_id = self.entries[&target].node;
        if matches!(self.nodes[&target_id].kind, NodeKind::Directory) {
            return Err(build_error("OCI hardlink target is a directory"));
        }
        let destination = self.resolve_destination(archive_path, true)?;
        if destination == target {
            return Ok(());
        }
        if !self.entries.contains_key(&destination) && self.entries.len() >= MAX_LOGICAL_ENTRIES {
            return Err(build_error("OCI rootfs exceeds the logical entry limit"));
        }
        self.nodes
            .get_mut(&target_id)
            .expect("hardlink target inode")
            .links += 1;
        self.remove_tree(&destination, true)?;
        self.entries.insert(
            destination,
            NamespaceEntry {
                node: target_id,
                generation: self.current_generation,
            },
        );
        Ok(())
    }

    pub(super) fn whiteout(&mut self, victim: &GuestPath) -> Result<()> {
        if let Some(path) = self.resolve_existing_entry(victim, false)? {
            self.remove_older_tree(&path, true)?;
        }
        Ok(())
    }

    pub(super) fn opaque(&mut self, directory: &GuestPath) -> Result<()> {
        if let Some(path) = self.resolve_existing_directory(directory)? {
            self.remove_older_tree(&path, false)?;
        }
        Ok(())
    }

    fn resolve_destination(&mut self, path: &GuestPath, create: bool) -> Result<GuestPath> {
        let (name, parent) = path
            .split_last()
            .ok_or_else(|| build_error("OCI entry has no filename"))?;
        let mut resolved = self
            .resolve_directory(&parent.to_vec(), create)?
            .ok_or_else(|| build_error("OCI entry parent does not exist"))?;
        self.touch_path_and_ancestors(&resolved);
        resolved.push(name.clone());
        Ok(resolved)
    }

    fn resolve_existing_entry(
        &mut self,
        path: &GuestPath,
        follow_final: bool,
    ) -> Result<Option<GuestPath>> {
        let resolved = if follow_final {
            self.resolve_path(path, true, false)?
        } else if path.is_empty() {
            Some(Vec::new())
        } else {
            let (name, parent) = path
                .split_last()
                .ok_or_else(|| build_error("OCI entry has no filename"))?;
            let Some(mut parent) = self.resolve_directory(&parent.to_vec(), false)? else {
                return Ok(None);
            };
            parent.push(name.clone());
            self.entries.contains_key(&parent).then_some(parent)
        };
        Ok(resolved.filter(|path| self.entries.contains_key(path)))
    }

    pub(super) fn resolve_existing_directory(
        &mut self,
        path: &GuestPath,
    ) -> Result<Option<GuestPath>> {
        let Some(path) = self.resolve_path(path, true, false)? else {
            return Ok(None);
        };
        let id = self.entries[&path].node;
        if matches!(self.nodes[&id].kind, NodeKind::Directory) {
            Ok(Some(path))
        } else {
            Err(build_error("OCI path is not a directory"))
        }
    }

    pub(super) fn resolve_directory(
        &mut self,
        path: &GuestPath,
        create: bool,
    ) -> Result<Option<GuestPath>> {
        let resolved = self.resolve_path(path, true, create)?;
        if let Some(path) = resolved.as_ref() {
            if create && !self.entries.contains_key(path) {
                self.replace_with_new(
                    path.clone(),
                    NodeKind::Directory,
                    canonical_meta(0o755),
                    BTreeMap::new(),
                )?;
            }
            let id = self.entries[path].node;
            if !matches!(self.nodes[&id].kind, NodeKind::Directory) {
                return Err(build_error("OCI path is not a directory"));
            }
        }
        Ok(resolved)
    }

    pub(super) fn resolve_path(
        &mut self,
        path: &GuestPath,
        follow_final: bool,
        create_missing: bool,
    ) -> Result<Option<GuestPath>> {
        let mut pending = path
            .iter()
            .cloned()
            .map(ResolveComponent::Normal)
            .collect::<VecDeque<_>>();
        let mut resolved = GuestPath::new();
        let mut hops = 0;
        while let Some(component) = pending.pop_front() {
            let ResolveComponent::Normal(component) = component else {
                if resolved.pop().is_none() {
                    return Err(build_error("Rootfs symlink target escapes the guest root"));
                }
                continue;
            };
            let mut candidate = resolved.clone();
            candidate.push(component.clone());
            let is_final = pending.is_empty();
            let Some(entry) = self.entries.get(&candidate).copied() else {
                if !create_missing {
                    return Ok(None);
                }
                if !is_final || !follow_final {
                    self.replace_with_new(
                        candidate.clone(),
                        NodeKind::Directory,
                        canonical_meta(0o755),
                        BTreeMap::new(),
                    )?;
                }
                resolved.push(component);
                continue;
            };
            match &self.nodes[&entry.node].kind {
                NodeKind::Symlink { target } if !is_final || follow_final => {
                    hops += 1;
                    if hops > MAX_SYMLINK_HOPS {
                        return Err(build_error("Too many rootfs symlink hops"));
                    }
                    let (absolute, target) = parse_symlink_target(target)?;
                    if absolute {
                        resolved.clear();
                    }
                    for component in target.into_iter().rev() {
                        pending.push_front(component);
                    }
                }
                NodeKind::Directory => resolved.push(component),
                NodeKind::Regular { .. } | NodeKind::Special(_) if is_final && follow_final => {
                    resolved.push(component)
                }
                _ => return Err(build_error("Rootfs path component is not a directory")),
            }
        }
        Ok(Some(resolved))
    }

    pub(super) fn replace_with_new(
        &mut self,
        path: GuestPath,
        kind: NodeKind,
        meta: Meta,
        xattrs: BTreeMap<String, Vec<u8>>,
    ) -> Result<()> {
        self.remove_tree(&path, true)?;
        if self.entries.len() >= MAX_LOGICAL_ENTRIES {
            return Err(build_error("OCI rootfs exceeds the logical entry limit"));
        }
        let id = self.next_node;
        self.next_node = self
            .next_node
            .checked_add(1)
            .ok_or_else(|| build_error("OCI inode identity overflow"))?;
        self.nodes.insert(
            id,
            Node {
                kind,
                meta,
                xattrs,
                links: 1,
            },
        );
        self.entries.insert(
            path,
            NamespaceEntry {
                node: id,
                generation: self.current_generation,
            },
        );
        Ok(())
    }

    fn remove_tree(&mut self, path: &GuestPath, include_root: bool) -> Result<()> {
        let victims = self
            .entries
            .keys()
            .filter(|candidate| {
                candidate.starts_with(path) && (include_root || candidate.len() > path.len())
            })
            .cloned()
            .collect::<Vec<_>>();
        for victim in victims {
            let Some(entry) = self.entries.remove(&victim) else {
                continue;
            };
            self.release_node(entry.node)?;
        }
        Ok(())
    }

    fn remove_older_tree(&mut self, path: &GuestPath, include_root: bool) -> Result<()> {
        let victims = self
            .entries
            .iter()
            .filter(|(candidate, entry)| {
                candidate.starts_with(path)
                    && (include_root || candidate.len() > path.len())
                    && entry.generation < self.current_generation
            })
            .map(|(path, _)| path.clone())
            .collect::<Vec<_>>();
        for victim in victims {
            let Some(entry) = self.entries.remove(&victim) else {
                continue;
            };
            self.release_node(entry.node)?;
        }
        Ok(())
    }

    fn release_node(&mut self, node_id: NodeId) -> Result<()> {
        let node = self
            .nodes
            .get_mut(&node_id)
            .ok_or_else(|| build_error("Logical OCI namespace references a missing inode"))?;
        node.links = node
            .links
            .checked_sub(1)
            .ok_or_else(|| build_error("Logical OCI inode link count underflow"))?;
        if node.links != 0 {
            return Ok(());
        }
        let node = self
            .nodes
            .remove(&node_id)
            .ok_or_else(|| build_error("Logical OCI inode disappeared during release"))?;
        if let NodeKind::Regular { source, .. } = node.kind {
            std::fs::remove_file(&source).map_err(|error| {
                build_error(format!(
                    "Failed to release superseded OCI content {}: {error}",
                    source.display()
                ))
            })?;
        }
        Ok(())
    }

    fn touch_path_and_ancestors(&mut self, path: &GuestPath) {
        for depth in 0..=path.len() {
            if let Some(entry) = self.entries.get_mut(&path[..depth]) {
                entry.generation = self.current_generation;
            }
        }
    }
}

pub(super) fn normalize_archive_path(raw: &[u8]) -> Result<GuestPath> {
    if raw.starts_with(b"/") || raw.contains(&0) || raw.len() > MAX_GUEST_PATH_BYTES {
        return Err(build_error(
            "OCI layer path must be a relative NUL-free Linux path of at most 4095 bytes",
        ));
    }
    let mut path = GuestPath::new();
    for component in raw.split(|byte| *byte == b'/') {
        match component {
            b"" | b"." => {}
            b".." => return Err(build_error("OCI layer path contains '..'")),
            value if value.len() > 255 => {
                return Err(build_error("OCI filename exceeds 255 bytes"))
            }
            value => path.push(value.to_vec()),
        }
    }
    Ok(path)
}

pub(super) fn guest_path(path: &str) -> Result<GuestPath> {
    normalize_archive_path(path.as_bytes())
}

fn parse_symlink_target(raw: &[u8]) -> Result<(bool, Vec<ResolveComponent>)> {
    validate_symlink_target(raw)?;
    let absolute = raw.starts_with(b"/");
    let mut components = Vec::new();
    for component in raw.split(|byte| *byte == b'/') {
        match component {
            b"" | b"." => {}
            b".." => components.push(ResolveComponent::Parent),
            value => components.push(ResolveComponent::Normal(value.to_vec())),
        }
    }
    Ok((absolute, components))
}

fn validate_symlink_target(target: &[u8]) -> Result<()> {
    if target.is_empty() || target.len() > 4095 || target.contains(&0) {
        return Err(build_error(
            "OCI symlink target must be 1..=4095 NUL-free bytes",
        ));
    }
    Ok(())
}

pub(super) fn canonical_meta(mode: u16) -> Meta {
    Meta::new(mode, 0, 0, (0, 0))
}

pub(super) fn build_error(message: impl Into<String>) -> BoxError {
    BoxError::BuildError(message.into())
}