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
//! Per-Sandbox A3S OCI Runtime owner startup.

use std::os::fd::AsRawFd;
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};

use a3s_box_core::error::{BoxError, Result};
use a3s_oci_sdk::{
    ContainerId, ContainerTarget, CreateAttachments, CreateRequest, DeleteMode, DeleteRequest,
    DriverKind, IoMode, IsolationRequest, OciBundle, OciContainerState, OperationContext,
    OperationId, ProcessIo, RuntimeOperation, StartRequest,
};

use super::a3s_oci_client::A3sOciClient;
use super::a3s_oci_handler::{validate_record, A3sOciHandler, A3sOciHandlerSpec};
use super::controller::{
    bind_control_listener, create_private_dir, duplicate_for_inheritance, open_log, read_log_tail,
    reap_failed_log_worker, start_log_worker, write_json_atomic, SandboxLaunchSpec,
    EXEC_LISTENER_FD, INIT_LOG_FD, PTY_LISTENER_FD, START_TIMEOUT,
};
use super::runtime_record::SandboxRuntimeRecord;
use super::CertifiedA3sOci;

const START_FAILURE_LOG_LIMIT_BYTES: u64 = 4 * 1024;
/// Controller pinned to one verified runtime/agent artifact pair.
pub struct A3sOciController {
    runtime: CertifiedA3sOci,
}

impl A3sOciController {
    pub fn new(runtime: CertifiedA3sOci) -> Self {
        Self { runtime }
    }

    /// Refuse to overwrite any existing owner root for the same generation.
    pub fn require_absent(&self, runtime_root: &Path, container_id: &str) -> Result<()> {
        match std::fs::symlink_metadata(runtime_root) {
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Err(error) => Err(BoxError::IoError(error)),
            Ok(_) => Err(BoxError::BoxBootError {
                message: format!(
                    "A3S OCI runtime root already exists for Sandbox {container_id}: {}",
                    runtime_root.display()
                ),
                hint: Some(
                    "Reconcile or remove the existing Sandbox generation before restarting it"
                        .to_string(),
                ),
            }),
        }
    }

    pub async fn start(&self, launch: SandboxLaunchSpec) -> Result<A3sOciHandler> {
        self.require_absent(&launch.runtime_root, &launch.container_id)?;
        let runtime_parent = launch.runtime_root.parent().ok_or_else(|| {
            BoxError::ConfigError("A3S OCI runtime root has no parent".to_string())
        })?;
        create_private_dir(runtime_parent)?;

        let exec_listener = bind_control_listener(&launch.exec_socket_path)?;
        let pty_listener = bind_control_listener(&launch.pty_socket_path)?;
        let stdout = open_log(&launch.stdout_path)?;
        let stderr = open_log(&launch.stderr_path)?;
        let init_log = open_log(&launch.init_log_path)?;

        let inherited_exec = duplicate_for_inheritance(exec_listener.as_raw_fd())?;
        let inherited_pty = duplicate_for_inheritance(pty_listener.as_raw_fd())?;
        let inherited_log = duplicate_for_inheritance(init_log.as_raw_fd())?;
        let exec_fd = inherited_exec.as_raw_fd();
        let pty_fd = inherited_pty.as_raw_fd();
        let log_fd = inherited_log.as_raw_fd();

        let mut command = Command::new(&self.runtime.runtime_path);
        command
            .arg("native-linux-service")
            .arg("--root")
            .arg(&launch.runtime_root)
            .arg("--agent")
            .arg(&self.runtime.agent_path)
            .arg("--container-id")
            .arg(&launch.container_id)
            .arg("--a3s-box-control-fds")
            .env("LC_ALL", "C")
            .stdin(Stdio::null())
            .stdout(Stdio::from(stdout))
            .stderr(Stdio::from(stderr));

        // Sources are duplicated above 10, so installing the fixed Box roles
        // cannot clobber another source. `dup2` clears CLOEXEC on 3/4/5.
        unsafe {
            command.pre_exec(move || {
                for (source, destination) in [
                    (exec_fd, EXEC_LISTENER_FD),
                    (pty_fd, PTY_LISTENER_FD),
                    (log_fd, INIT_LOG_FD),
                ] {
                    if libc::dup2(source, destination) < 0 {
                        return Err(std::io::Error::last_os_error());
                    }
                }
                Ok(())
            });
        }

        let mut owner = command.spawn().map_err(|error| BoxError::BoxBootError {
            message: format!("Failed to start A3S OCI runtime owner: {error}"),
            hint: None,
        })?;
        drop((inherited_exec, inherited_pty, inherited_log));
        drop((exec_listener, pty_listener, init_log));

        let owner_pid = owner.id();
        let owner_pid_start_time = crate::process::pid_start_time(owner_pid).ok_or_else(|| {
            let _ = owner.kill();
            let _ = owner.wait();
            BoxError::BoxBootError {
                message: "Failed to capture A3S OCI owner process identity".to_string(),
                hint: None,
            }
        })?;
        let runtime_socket = launch.runtime_root.join("runtime.sock");
        if let Err(error) = wait_for_private_socket(&mut owner, &runtime_socket).await {
            cleanup_failed_owner(&mut owner, owner_pid_start_time, None, None, &launch);
            return Err(with_start_diagnostics(error, &launch));
        }

        let mut client = None;
        let mut target = None;
        let lifecycle = async {
            let connected = A3sOciClient::connect(runtime_socket.clone()).await?;
            validate_required_operations(&connected.features()?)?;
            client = Some(connected);
            let lifecycle_client = client.as_ref().ok_or_else(|| {
                BoxError::StateError("A3S OCI client ownership was not retained".to_string())
            })?;

            let bundle = OciBundle::load(&launch.bundle_dir)
                .await
                .map_err(sdk_boot_error)?;
            let container_id =
                ContainerId::new(launch.container_id.clone()).map_err(sdk_boot_error)?;
            let attachments = CreateAttachments::from_bundle(
                &bundle,
                ProcessIo {
                    stdin: IoMode::Null,
                    stdout: IoMode::Inherit,
                    stderr: IoMode::Inherit,
                    terminal_size: None,
                },
            )
            .map_err(sdk_boot_error)?;
            let created = lifecycle_client.create(CreateRequest {
                context: operation_context(&launch.container_id, "create")?,
                id: container_id.clone(),
                bundle,
                isolation: IsolationRequest::SharedHostKernel,
                attachments,
            })?;
            let exact_target = ContainerTarget::exact(container_id, created.generation);
            validate_record(&created, &exact_target, None)?;
            if *created.state.status() != OciContainerState::Created {
                return Err(BoxError::StateError(
                    "A3S OCI create did not return the created state".to_string(),
                ));
            }
            let created_init_pid = created
                .state
                .pid()
                .and_then(|pid| u32::try_from(pid).ok())
                .ok_or_else(|| {
                    BoxError::StateError("A3S OCI create returned no valid init PID".to_string())
                })?;
            target = Some(exact_target.clone());

            let started = lifecycle_client.start(StartRequest {
                context: operation_context(&launch.container_id, "start")?,
                target: exact_target.clone(),
            })?;
            validate_record(&started, &exact_target, None)?;
            let started_status = *started.state.status();
            if started.driver != DriverKind::NativeLinux {
                return Err(BoxError::StateError(
                    "A3S OCI start did not return a native Linux container".to_string(),
                ));
            }
            let init_pid = started_generation_init_pid(
                created_init_pid,
                started_status,
                *started.state.pid(),
            )?;
            Ok((init_pid, started.generation))
        }
        .await;

        let (init_pid, generation) = match lifecycle {
            Ok(result) => result,
            Err(error) => {
                cleanup_failed_owner(
                    &mut owner,
                    owner_pid_start_time,
                    client.as_ref(),
                    target.as_ref(),
                    &launch,
                );
                return Err(with_start_diagnostics(error, &launch));
            }
        };
        let client = match client {
            Some(client) => client,
            None => {
                cleanup_failed_owner(&mut owner, owner_pid_start_time, None, None, &launch);
                return Err(BoxError::StateError(
                    "A3S OCI lifecycle completed without a retained SDK client".to_string(),
                ));
            }
        };
        let target = match target {
            Some(target) => target,
            None => {
                client.close();
                cleanup_failed_owner(&mut owner, owner_pid_start_time, None, None, &launch);
                return Err(BoxError::StateError(
                    "A3S OCI lifecycle completed without an exact target".to_string(),
                ));
            }
        };

        let mut log_worker = match start_log_worker(&launch, owner_pid, owner_pid_start_time) {
            Ok(worker) => worker,
            Err(error) => {
                cleanup_failed_owner(
                    &mut owner,
                    owner_pid_start_time,
                    Some(&client),
                    Some(&target),
                    &launch,
                );
                return Err(error);
            }
        };
        let log_worker_pid = log_worker.id();
        let log_worker_pid_start_time = match crate::process::pid_start_time(log_worker_pid) {
            Some(start_time) => start_time,
            None => {
                reap_failed_log_worker(&mut log_worker);
                cleanup_failed_owner(
                    &mut owner,
                    owner_pid_start_time,
                    Some(&client),
                    Some(&target),
                    &launch,
                );
                return Err(BoxError::BoxBootError {
                    message: "Failed to capture Sandbox log worker identity".to_string(),
                    hint: None,
                });
            }
        };

        let record = SandboxRuntimeRecord::a3s_oci(
            launch.container_id.clone(),
            self.runtime.runtime_path.clone(),
            self.runtime.runtime_sha256.clone(),
            self.runtime.agent_path.clone(),
            self.runtime.agent_sha256.clone(),
            launch.runtime_root.clone(),
            runtime_socket.clone(),
            launch.bundle_dir.clone(),
            init_pid,
            generation.0,
            owner_pid,
            owner_pid_start_time,
            log_worker_pid,
            log_worker_pid_start_time,
        );
        if let Err(error) = write_json_atomic(&launch.runtime_record, &record) {
            reap_failed_log_worker(&mut log_worker);
            cleanup_failed_owner(
                &mut owner,
                owner_pid_start_time,
                Some(&client),
                Some(&target),
                &launch,
            );
            return Err(error);
        }

        Ok(A3sOciHandler::from_child(
            A3sOciHandlerSpec {
                runtime_socket,
                runtime_root: launch.runtime_root,
                container_id: target.id,
                generation,
                init_pid,
                owner_pid,
                owner_pid_start_time,
                bundle_dir: launch.bundle_dir,
                runtime_record: launch.runtime_record,
            },
            client,
            owner,
            log_worker,
            log_worker_pid_start_time,
        ))
    }
}

async fn wait_for_private_socket(owner: &mut Child, socket_path: &Path) -> Result<()> {
    let deadline = Instant::now() + START_TIMEOUT;
    loop {
        if let Some(status) = owner.try_wait().map_err(BoxError::IoError)? {
            return Err(BoxError::BoxBootError {
                message: format!("A3S OCI runtime owner exited before readiness: {status}"),
                hint: None,
            });
        }
        match std::fs::symlink_metadata(socket_path) {
            Ok(metadata)
                if metadata.file_type().is_socket()
                    && metadata.permissions().mode() & 0o777 == 0o600
                    && metadata.uid() == unsafe { libc::geteuid() } =>
            {
                return Ok(())
            }
            Ok(_) => {
                return Err(BoxError::BoxBootError {
                    message: format!(
                        "A3S OCI runtime endpoint failed its socket, owner, or mode contract: {}",
                        socket_path.display()
                    ),
                    hint: None,
                })
            }
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => return Err(BoxError::IoError(error)),
        }
        if Instant::now() >= deadline {
            return Err(BoxError::BoxBootError {
                message: "Timed out waiting for the A3S OCI runtime endpoint".to_string(),
                hint: None,
            });
        }
        tokio::time::sleep(Duration::from_millis(25)).await;
    }
}

fn validate_required_operations(info: &a3s_oci_sdk::RuntimeInfo) -> Result<()> {
    for operation in [
        RuntimeOperation::Create,
        RuntimeOperation::State,
        RuntimeOperation::Start,
        RuntimeOperation::Kill,
        RuntimeOperation::Delete,
        RuntimeOperation::Wait,
        RuntimeOperation::Pause,
        RuntimeOperation::Resume,
        RuntimeOperation::Update,
        RuntimeOperation::Stats,
    ] {
        if !info.operations.contains(&operation) {
            return Err(BoxError::BoxBootError {
                message: format!("A3S OCI runtime does not advertise {operation:?}"),
                hint: Some("Install the matching A3S OCI Runtime package".to_string()),
            });
        }
    }
    Ok(())
}

fn started_generation_init_pid(
    created_init_pid: u32,
    status: OciContainerState,
    started_init_pid: Option<i32>,
) -> Result<u32> {
    match status {
        OciContainerState::Running => {
            let started_init_pid = started_init_pid
                .and_then(|pid| u32::try_from(pid).ok())
                .ok_or_else(|| {
                    BoxError::StateError(
                        "A3S OCI running state returned no valid init PID".to_string(),
                    )
                })?;
            if started_init_pid != created_init_pid {
                return Err(BoxError::StateError(
                    "A3S OCI start changed the init PID".to_string(),
                ));
            }
            Ok(started_init_pid)
        }
        // OCI stopped state deliberately has no live PID. Retain the exact
        // PID allocated behind the create/start barrier so the durable Box
        // record still identifies this generation.
        OciContainerState::Stopped => Ok(created_init_pid),
        _ => Err(BoxError::StateError(
            "A3S OCI start did not return a running or stopped container".to_string(),
        )),
    }
}

fn operation_context(container_id: &str, operation: &str) -> Result<OperationContext> {
    OperationId::new(format!("{container_id}-{operation}"))
        .map(OperationContext::new)
        .map_err(sdk_boot_error)
}

fn cleanup_failed_owner(
    owner: &mut Child,
    owner_pid_start_time: u64,
    client: Option<&A3sOciClient>,
    target: Option<&ContainerTarget>,
    launch: &SandboxLaunchSpec,
) {
    if let (Some(client), Some(target)) = (client, target) {
        if let Ok(context) = operation_context(&launch.container_id, "failed-create-delete") {
            let _ = client.delete_if_present(DeleteRequest {
                context,
                target: target.clone(),
                mode: DeleteMode::Force,
            });
        }
        client.close();
    }
    let pid = owner.id();
    if super::a3s_oci_owner::stop(pid, owner_pid_start_time).is_err() {
        let _ = owner.kill();
    }
    let _ = owner.wait();
    let _ = std::fs::remove_file(&launch.runtime_record);
    let _ = std::fs::remove_dir_all(&launch.runtime_root);
}

fn with_start_diagnostics(error: BoxError, launch: &SandboxLaunchSpec) -> BoxError {
    let diagnostics = [
        ("runtime/container stderr", &launch.stderr_path),
        ("guest-init log", &launch.init_log_path),
    ]
    .into_iter()
    .filter_map(|(label, path)| {
        read_log_tail(path, START_FAILURE_LOG_LIMIT_BYTES)
            .map(|excerpt| format!("{label}: {excerpt}"))
    })
    .collect::<Vec<_>>();
    if diagnostics.is_empty() {
        error
    } else {
        BoxError::BoxBootError {
            message: format!("{error} ({})", diagnostics.join("; ")),
            hint: None,
        }
    }
}

fn sdk_boot_error(error: a3s_oci_sdk::Error) -> BoxError {
    BoxError::BoxBootError {
        message: format!("A3S OCI SDK rejected Sandbox launch: {error}"),
        hint: None,
    }
}

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

    #[test]
    fn stopped_start_retains_the_created_generation_pid() {
        let init_pid =
            started_generation_init_pid(4_242, OciContainerState::Stopped, None).unwrap();

        assert_eq!(init_pid, 4_242);
    }

    #[test]
    fn running_start_requires_the_same_generation_pid() {
        let error = started_generation_init_pid(4_242, OciContainerState::Running, Some(4_243))
            .unwrap_err();

        assert!(error.to_string().contains("changed the init PID"));
    }

    #[test]
    fn start_rejects_non_terminal_contract_states() {
        let error = started_generation_init_pid(4_242, OciContainerState::Created, Some(4_242))
            .unwrap_err();

        assert!(error
            .to_string()
            .contains("did not return a running or stopped container"));
    }
}