sim-platform-ubuntu-pc 0.3.0

Ubuntu PC reference platform capsule
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
// conformance: Ubuntu sandbox realization refuses undeclared authority before spawning.

use sim_lib_exec::{
    BindingValue, MountAccess, ProcessAttempt, ProcessBudget, ProcessCancellation, ProcessRequest,
    ProgramRef, ProjectRootRef, SandboxAttempt, SandboxControl, SandboxEvidence, SandboxLauncher,
    SandboxRefusal, SandboxReport, SandboxRequest, SandboxResult,
};
use std::{
    collections::BTreeMap,
    path::{Path, PathBuf},
    process::{Command, Stdio},
};

/// Linux bubblewrap realization of the runtime-owned sandbox authority boundary.
#[derive(Clone, Debug)]
pub struct BwrapLauncher {
    bwrap: PathBuf,
    prlimit: PathBuf,
    programs: BTreeMap<ProgramRef, PathBuf>,
    sources: BTreeMap<String, PathBuf>,
}

/// Live readiness of the Ubuntu bubblewrap effect membrane.
///
/// This evidence certifies only bounded-effect confinement. It deliberately
/// cannot represent projector purity or source qualification: bubblewrap
/// exposes `/proc` and `/dev`, and declared mounts may contain semantic inputs
/// outside a projector's selected immutable view.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BwrapConfinementStatus {
    /// Stable membrane implementation identity.
    pub membrane: &'static str,
    /// Whether both boot-authorized executables exist as files now.
    pub available: bool,
    /// Human-readable readiness detail.
    pub detail: String,
}

impl BwrapLauncher {
    /// Creates a boot-configured launcher. Paths are authority supplied, never request supplied.
    #[must_use]
    pub fn new(
        bwrap: PathBuf,
        prlimit: PathBuf,
        programs: BTreeMap<ProgramRef, PathBuf>,
        sources: BTreeMap<String, PathBuf>,
    ) -> Self {
        Self {
            bwrap,
            prlimit,
            programs,
            sources,
        }
    }

    /// Probes the exact boot-resolved membrane executables without dispatch.
    #[must_use]
    pub fn confinement_status(&self) -> BwrapConfinementStatus {
        let bwrap = self.bwrap.is_file();
        let prlimit = self.prlimit.is_file();
        BwrapConfinementStatus {
            membrane: "platform/sandbox/ubuntu-bwrap",
            available: bwrap && prlimit,
            detail: format!("bwrap-file={bwrap};prlimit-file={prlimit};purity-qualified=false"),
        }
    }
    fn refuse(&self, reason: impl Into<String>) -> SandboxAttempt {
        SandboxAttempt::Refused(SandboxRefusal {
            launcher: self.id().into(),
            reason: reason.into(),
            report: None,
        })
    }
    fn command(&self, request: &SandboxRequest) -> Result<Command, String> {
        if !self.bwrap.is_file() {
            return Err("bubblewrap is unavailable".into());
        }
        if !self.prlimit.is_file() {
            return Err("prlimit is unavailable".into());
        }
        let program = canonical_file(
            self.programs
                .get(&request.program)
                .ok_or("program is not boot-authorized")?,
        )?;
        let mut command = Command::new(&self.bwrap);
        command
            .args([
                "--die-with-parent",
                "--new-session",
                "--unshare-all",
                "--unshare-net",
                "--clearenv",
                "--tmpfs",
                "/",
                "--proc",
                "/proc",
                "--dev",
                "/dev",
                "--dir",
                "/work",
                "--chdir",
                "/work",
                "--ro-bind",
            ])
            .arg(&program)
            .arg("/sim-program")
            .args(["--ro-bind"])
            .arg(&self.prlimit)
            .arg("/sim-prlimit");
        for mount in request.policy.mounts() {
            let source = canonical(
                self.sources
                    .get(&mount.source)
                    .ok_or("mount source is not boot-authorized")?,
            )?;
            command
                .arg(match mount.access {
                    MountAccess::ReadOnly => "--ro-bind",
                    MountAccess::Writable => "--bind",
                })
                .arg(source)
                .arg(&mount.guest_path);
        }
        for (name, value) in request.environment.iter() {
            let BindingValue::Literal(value) = value else {
                return Err("sandbox environment permits literal bindings only".into());
            };
            command.arg("--setenv").arg(name).arg(value);
        }
        let limits = request.policy.limits();
        command
            .args(["--", "/sim-prlimit"])
            .arg(format!("--cpu={}", limits.cpu_seconds))
            .arg(format!("--as={}", limits.memory_bytes))
            .arg(format!("--nproc={}", limits.process_count))
            .arg(format!("--fsize={}", limits.file_bytes))
            .args(["--", "/sim-program"])
            .args(request.argv.iter().map(sim_lib_exec::ArgAtom::as_str))
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        Ok(command)
    }
}
impl SandboxLauncher for BwrapLauncher {
    fn id(&self) -> &'static str {
        "platform/sandbox/ubuntu-bwrap"
    }
    fn launch(
        &self,
        request: &SandboxRequest,
        cancellation: &ProcessCancellation,
    ) -> SandboxAttempt {
        let mut command = match self.command(request) {
            Ok(v) => v,
            Err(e) => return self.refuse(e),
        };
        let root = ProjectRootRef::new("sandbox-root").expect("constant is valid");
        let process_request = ProcessRequest {
            program: request.program.clone(),
            argv: request.argv.clone(),
            root,
            environment: request.environment.clone(),
            private_artifacts: vec![],
            budget: ProcessBudget {
                timeout_ms: request.policy.limits().wall_time_ms,
                max_output_bytes: request.policy.limits().output_bytes,
                stdin: Some(request.stdin.clone()),
            },
        };
        let mut child = match command.spawn() {
            Ok(v) => v,
            Err(e) => return self.refuse(format!("bubblewrap spawn failed: {e}")),
        };
        let outcome = super::process::run_child(&mut child, &process_request, cancellation);
        report(request, outcome, &self.sources)
    }
}
fn canonical(path: &Path) -> Result<PathBuf, String> {
    path.canonicalize()
        .map_err(|e| format!("declared mount unavailable: {e}"))
}
fn canonical_file(path: &Path) -> Result<PathBuf, String> {
    let path = canonical(path)?;
    if !path.is_file() {
        return Err("authorized program is not a file".into());
    }
    Ok(path)
}
fn report(
    request: &SandboxRequest,
    outcome: ProcessAttempt,
    sources: &BTreeMap<String, PathBuf>,
) -> SandboxAttempt {
    let usage = writable_usage(request, sources);
    let usage_observed = usage.is_ok();
    let (files, bytes) = usage.unwrap_or((u64::MAX, u64::MAX));
    let controls = request
        .policy
        .requirements()
        .keys()
        .map(|control| SandboxEvidence {
            control: *control,
            achieved: !matches!(
                control,
                SandboxControl::FileCount | SandboxControl::FileBytes
            ) || usage_observed,
            detail: match control {
                SandboxControl::Network => "bubblewrap network namespace has no interfaces",
                SandboxControl::Mounts => "only canonical boot-resolved mounts were bound",
                SandboxControl::Root => "anonymous tmpfs root; no home or workspace mount",
                SandboxControl::Environment => "bubblewrap clearenv plus literal declared bindings",
                SandboxControl::Identity => "user and mount namespaces isolate host identity",
                SandboxControl::Cpu => "RLIMIT_CPU applied by prlimit",
                SandboxControl::Memory => "RLIMIT_AS applied by prlimit",
                SandboxControl::WallTime => "capsule monotonic deadline",
                SandboxControl::ProcessCount => "RLIMIT_NPROC applied by prlimit",
                SandboxControl::FileCount if usage_observed => {
                    "writable roots were inspected recursively at completion"
                }
                SandboxControl::FileCount => "writable-root file count could not be observed",
                SandboxControl::FileBytes if usage_observed => {
                    "RLIMIT_FSIZE plus recursive writable-root byte inspection"
                }
                SandboxControl::FileBytes => "writable-root file bytes could not be observed",
                SandboxControl::Output => "shared bounded capture",
                SandboxControl::Stdin => "validated bounded pipe",
                SandboxControl::ProcessTree => "new session killed and reaped by capsule",
            }
            .into(),
        })
        .collect();
    match outcome {
        ProcessAttempt::Completed { receipt } => {
            let mut hits = vec![];
            if receipt.result.truncated {
                hits.push("output_bytes".into());
            }
            if usage_observed {
                if files > request.policy.limits().file_count {
                    hits.push("file_count".into());
                }
                if bytes > request.policy.limits().file_bytes {
                    hits.push("file_bytes".into());
                }
            } else {
                hits.push("writable_root_observation".into());
            }
            SandboxAttempt::Completed(SandboxResult {
                stdout: receipt.result.stdout.into_bytes(),
                stderr: receipt.result.stderr.into_bytes(),
                exit_code: receipt.result.exit_code,
                report: SandboxReport {
                    launcher: "platform/sandbox/ubuntu-bwrap".into(),
                    controls,
                    limit_hits: hits,
                    cleanup: "normal completion; process group empty after pipe closure".into(),
                },
            })
        }
        ProcessAttempt::StoppedAfterTimeout { receipt } => SandboxAttempt::Stopped(SandboxReport {
            launcher: "platform/sandbox/ubuntu-bwrap".into(),
            controls,
            limit_hits: vec!["wall_time".into()],
            cleanup: receipt.cleanup,
        }),
        ProcessAttempt::StoppedAfterCancel { receipt } => SandboxAttempt::Stopped(SandboxReport {
            launcher: "platform/sandbox/ubuntu-bwrap".into(),
            controls,
            limit_hits: vec!["cancellation".into()],
            cleanup: receipt.cleanup,
        }),
        ProcessAttempt::NotDispatched { refusal } => SandboxAttempt::Refused(SandboxRefusal {
            launcher: "platform/sandbox/ubuntu-bwrap".into(),
            reason: match refusal {
                sim_lib_exec::ProcessRefusal::Invalid(detail) => format!("invalid: {detail}"),
                sim_lib_exec::ProcessRefusal::Refused(detail) => format!("refused: {detail}"),
                sim_lib_exec::ProcessRefusal::SpawnFailed(detail) => {
                    format!("spawn failed: {detail}")
                }
            },
            report: None,
        }),
        ProcessAttempt::UnknownAfterDispatch { evidence } => {
            SandboxAttempt::Unknown(SandboxRefusal {
                launcher: "platform/sandbox/ubuntu-bwrap".into(),
                reason: format!("{}: {}", evidence.stage, evidence.detail),
                report: None,
            })
        }
    }
}

fn writable_usage(
    request: &SandboxRequest,
    sources: &BTreeMap<String, PathBuf>,
) -> Result<(u64, u64), String> {
    let mut total = (0u64, 0u64);
    for mount in request
        .policy
        .mounts()
        .iter()
        .filter(|mount| mount.access == MountAccess::Writable)
    {
        let root = canonical(
            sources
                .get(&mount.source)
                .ok_or("writable mount source is not boot-authorized")?,
        )?;
        accumulate_usage(&root, &mut total)?;
    }
    Ok(total)
}

fn accumulate_usage(path: &Path, total: &mut (u64, u64)) -> Result<(), String> {
    for entry in std::fs::read_dir(path).map_err(|error| format!("writable root: {error}"))? {
        let entry = entry.map_err(|error| format!("writable entry: {error}"))?;
        let metadata = std::fs::symlink_metadata(entry.path())
            .map_err(|error| format!("writable metadata: {error}"))?;
        if metadata.file_type().is_symlink() {
            total.0 = total.0.saturating_add(1);
        } else if metadata.is_dir() {
            accumulate_usage(&entry.path(), total)?;
        } else {
            total.0 = total.0.saturating_add(1);
            total.1 = total.1.saturating_add(metadata.len());
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use sim_lib_exec::{
        ArgAtom, SandboxLimits, SandboxMount, SandboxPolicy, SandboxRequirement, SealedBindings,
    };
    fn policy() -> SandboxPolicy {
        let controls = [
            SandboxControl::Network,
            SandboxControl::Mounts,
            SandboxControl::Root,
            SandboxControl::Environment,
            SandboxControl::Identity,
            SandboxControl::Cpu,
            SandboxControl::Memory,
            SandboxControl::WallTime,
            SandboxControl::ProcessCount,
            SandboxControl::FileCount,
            SandboxControl::FileBytes,
            SandboxControl::Output,
            SandboxControl::Stdin,
            SandboxControl::ProcessTree,
        ];
        SandboxPolicy::new(
            controls
                .into_iter()
                .map(|c| (c, SandboxRequirement::Required)),
            vec![SandboxMount {
                source: "input".into(),
                guest_path: "/input".into(),
                access: MountAccess::ReadOnly,
            }],
            SandboxLimits {
                cpu_seconds: 1,
                memory_bytes: 1024 * 1024,
                wall_time_ms: 100,
                process_count: 2,
                file_count: 2,
                file_bytes: 1024,
                output_bytes: 1024,
                stdin_bytes: 16,
            },
        )
        .unwrap()
    }
    #[test]
    fn missing_bwrap_refuses_before_dispatch() {
        let launcher = BwrapLauncher::new(
            "/definitely/missing/bwrap".into(),
            "/usr/bin/prlimit".into(),
            BTreeMap::new(),
            BTreeMap::new(),
        );
        let status = launcher.confinement_status();
        assert!(!status.available);
        assert_eq!(status.membrane, "platform/sandbox/ubuntu-bwrap");
        assert!(status.detail.contains("purity-qualified=false"));
        assert_eq!(launcher.id(), "platform/sandbox/ubuntu-bwrap");
        let request = SandboxRequest::new(
            ProgramRef::new("tool").unwrap(),
            vec![],
            SealedBindings::empty(),
            vec![],
            policy(),
        )
        .unwrap();
        assert!(matches!(
            launcher.launch(&request, &ProcessCancellation::default()),
            SandboxAttempt::Refused(_)
        ));
    }
    #[test]
    fn command_is_anonymous_networkless_and_keeps_hostile_argument_literal() {
        let executable = std::env::current_exe().unwrap();
        let launcher = BwrapLauncher::new(
            executable.clone(),
            executable.clone(),
            BTreeMap::from([(ProgramRef::new("tool").unwrap(), executable)]),
            BTreeMap::from([("input".into(), PathBuf::from("/tmp"))]),
        );
        let hostile = "$(cat /etc/shadow); nc 127.0.0.1 1";
        let request = SandboxRequest::new(
            ProgramRef::new("tool").unwrap(),
            vec![ArgAtom::new(hostile).unwrap()],
            SealedBindings::empty(),
            vec![],
            policy(),
        )
        .unwrap();
        let command = launcher.command(&request).unwrap();
        let args = command
            .get_args()
            .map(|v| v.to_string_lossy().into_owned())
            .collect::<Vec<_>>();
        assert!(
            args.iter().any(|v| v == "--unshare-net") && args.iter().any(|v| v == "--clearenv")
        );
        assert!(args.iter().any(|v| v == hostile));
        assert!(!args.iter().any(|v| v == "/home" || v == "/workspace"));
    }
}