harn-vm 0.10.163

Async bytecode virtual machine for the Harn programming language
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//! The alternate renderer for the same opened Linux filesystem grants.
//! Bubblewrap creates the namespaces and mounts, then installs Harn's existing
//! seccomp filter. Neither mount sources nor that filter can change after
//! preparation: the launch carries owned descriptors through every exec.

use std::io::{Seek, Write};
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
use std::os::unix::fs::{FileTypeExt, PermissionsExt};
use std::path::{Path, PathBuf};
#[cfg(test)]
use std::process::Command;

use super::{
    compile_seccomp_program, filesystem_profile, policy_allows_network, read_only_access,
    sandbox_rejection, workspace_access, DescriptorTransfer, FilesystemProfile,
    ProcessFilesystemScope, TransferableConfinement, LANDLOCK_ACCESS_FS_EXECUTE,
    LANDLOCK_ACCESS_FS_READ_DIR, LANDLOCK_ACCESS_FS_READ_FILE, LANDLOCK_ACCESS_FS_WRITE_FILE,
};
use crate::orchestration::{CapabilityPolicy, SandboxProfile};
use crate::stdlib::sandbox::{PrepareOutcome, SandboxMechanism, SandboxMechanismAvailability};
use crate::VmError;

#[path = "linux_bwrap_probe.rs"]
mod probe;
use probe::probe;
#[cfg(test)]
use probe::probe_output_is_available;

fn executable() -> Option<PathBuf> {
    ["/usr/bin/bwrap", "/bin/bwrap"]
        .into_iter()
        .map(PathBuf::from)
        .find(|path| path.is_file())
}

/// A known nonempty host file must disappear through the same real wrapper
/// path. A binary that merely exists, or a probe that observes nothing, fails.
pub(super) fn available() -> bool {
    static AVAILABLE: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
    cached_availability(&AVAILABLE, probe)
}

fn cached_availability(
    cache: &std::sync::Mutex<Option<bool>>,
    probe: impl FnOnce() -> probe::ProbeOutcome,
) -> bool {
    // Keep one setup probe in flight, as the original once initializer did.
    // Interrupted attempts leave the environment observation uninitialized.
    let mut cached = cache
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    if let Some(available) = *cached {
        return available;
    }
    match probe() {
        probe::ProbeOutcome::Completed(available) => {
            *cached = Some(available);
            available
        }
        probe::ProbeOutcome::Interrupted | probe::ProbeOutcome::Incomplete => false,
    }
}

pub(in crate::stdlib::sandbox) fn prepare(
    program: &str,
    args: &[String],
    policy: &CapabilityPolicy,
    profile: SandboxProfile,
) -> Result<PrepareOutcome, VmError> {
    let available = available();
    // A caller's cancellation or deadline is control flow, not a missing host
    // mechanism. The probe's own setup budget is never reported here: an
    // incomplete probe falls through to the typed refusal below.
    if let Some(error) = crate::op_interrupt::requested_error() {
        return Err(error);
    }
    if !available {
        let mut refusal = super::super::SandboxMechanismUnavailable::new(
            SandboxMechanism::LinuxBubblewrap,
            SandboxMechanismAvailability::AbsentOnHost,
            profile,
        );
        refusal.unconfined = super::super::enforcement::ConfinementDimension::ALL
            .into_iter()
            .filter(|dimension| dimension.required_by(policy))
            .collect();
        return Err(refusal.into_error());
    }
    if policy.process_network_proxy.is_some() {
        return Err(sandbox_rejection(
            "bubblewrap cannot express the managed proxy-only egress grant".into(),
        ));
    }
    let filesystem = filesystem_profile(
        program,
        policy,
        u64::MAX,
        ProcessFilesystemScope::PrivatePidNamespace,
    )?;
    let mut helper_grant = FilesystemProfile {
        rules: Vec::new(),
        symlinks: Default::default(),
        handled_access_fs: filesystem.handled_access_fs,
        read_deny_roots: filesystem.read_deny_roots.clone(),
    };
    let seccomp = TransferableConfinement {
        ruleset: None,
        seccomp: compile_seccomp_program(policy)?,
    };
    let filter = sealed_filter(&seccomp.seccomp_bytes()).map_err(|error| {
        sandbox_rejection(format!(
            "could not pin the bubblewrap syscall filter: {error}"
        ))
    })?;
    let MountPlan {
        mut argv,
        mut descriptors,
        device_descriptors,
    } = mounts(filesystem)?;
    argv.splice(
        0..0,
        [
            "--die-with-parent".into(),
            "--unshare-user".into(),
            "--unshare-pid".into(),
            "--unshare-ipc".into(),
        ],
    );
    // An abstract Unix socket belongs to its network namespace too. The
    // existing filter still withholds connect for a serve-only socket grant.
    if !policy_allows_network(policy) {
        argv.push("--unshare-net".into());
    }
    argv.extend(["--seccomp".into(), filter.as_raw_fd().to_string()]);
    descriptors.push(filter);
    // Allocator tuning the launch environment carries is kept out of
    // bubblewrap's own environment and re-applied to the payload through
    // these arguments, written once the final environment is known
    // (`launch_environment`). Empty until then, which adds nothing.
    let payload_env = payload_env_args().map_err(|error| {
        sandbox_rejection(format!(
            "could not create the bubblewrap payload environment descriptor: {error}"
        ))
    })?;
    argv.extend([
        PAYLOAD_ENV_ARGS_FLAG.into(),
        payload_env.as_raw_fd().to_string(),
    ]);
    descriptors.push(payload_env);
    let mut finalizer_args = Vec::new();
    if !device_descriptors.is_empty() {
        let launcher = policy.process_sandbox.netns_launcher_path.as_ref()
            .map(PathBuf::from)
            .filter(|path| path.is_absolute() && path.is_file())
            .map(|path| super::super::normalize_for_policy(&path))
            .ok_or_else(|| sandbox_rejection(
                "bubblewrap needs the admitted Harn namespace helper to close device setup descriptors before the payload".into()
            ))?;
        super::push_rule(
            &mut helper_grant,
            launcher.clone(),
            read_only_access(),
            false,
        )?;
        if !helper_grant.symlinks.is_empty() {
            return Err(sandbox_rejection(
                "bubblewrap finalizer path changed after normalization".into(),
            ));
        }
        let helper_mount = mounts(helper_grant)?;
        if !helper_mount.device_descriptors.is_empty() {
            return Err(sandbox_rejection(
                "bubblewrap finalizer must be a regular executable".into(),
            ));
        }
        argv.extend(helper_mount.argv);
        descriptors.extend(helper_mount.descriptors);
        finalizer_args.push(launcher.display().to_string());
        finalizer_args.push(crate::process_sandbox::NETNS_LAUNCH_SUBCOMMAND.into());
        for device in device_descriptors {
            finalizer_args.extend([
                crate::process_sandbox::NETNS_CLOSE_FD_FLAG.into(),
                device.to_string(),
            ]);
        }
        finalizer_args.push("--".into());
    }
    // Seal namespace scaffolding after every payload and helper mount. This
    // is nonrecursive, preserving only the explicitly writable child mounts.
    argv.extend(["--remount-ro".into(), "/".into(), "--".into()]);
    argv.extend(finalizer_args);
    argv.push(program.into());
    argv.extend_from_slice(args);
    Ok(PrepareOutcome::BubblewrapExec {
        wrapper: executable()
            .expect("the functional probe found bubblewrap")
            .to_string_lossy()
            .into_owned(),
        args: argv,
        descriptors: DescriptorTransfer::new(descriptors),
    })
}

struct MountPlan {
    argv: Vec<String>,
    descriptors: Vec<OwnedFd>,
    device_descriptors: Vec<super::DeviceMountFinalization>,
}

fn mounts(filesystem: FilesystemProfile) -> Result<MountPlan, VmError> {
    let mut argv = Vec::new();
    let mut descriptors = Vec::new();
    let mut device_descriptors = Vec::new();
    // Create aliases before any host mount so this never modifies a writable
    // host grant. Rebuilding an alias grants no target the handles omit.
    for (alias, target) in filesystem.symlinks {
        argv.extend([
            "--symlink".into(),
            target.to_string_lossy().into_owned(),
            alias.to_string_lossy().into_owned(),
        ]);
    }
    for rule in filesystem.rules {
        let metadata = rule.file.metadata().map_err(|error| {
            sandbox_rejection(format!(
                "cannot inspect pinned grant {}: {error}",
                rule.path.display()
            ))
        })?;
        if rule.path == Path::new("/proc") {
            // A fresh procfs in the PID namespace cannot expose host siblings.
            // A files-only grant cannot be widened to directory enumeration.
            if rule.allowed_access & LANDLOCK_ACCESS_FS_READ_DIR == 0 {
                return Err(sandbox_rejection(
                    "bubblewrap cannot express the files-only process introspection grant".into(),
                ));
            }
            argv.extend([
                "--proc".into(),
                "/proc".into(),
                "--remount-ro".into(),
                "/proc".into(),
            ]);
            continue;
        }
        let reads = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR;
        let writable = rule.allowed_access & LANDLOCK_ACCESS_FS_WRITE_FILE != 0;
        if metadata.is_dir()
            && rule.allowed_access != read_only_access()
            && rule.allowed_access != workspace_access(&CapabilityPolicy::default())
        {
            return Err(sandbox_rejection(format!(
                "bubblewrap cannot express selective filesystem rights for {}",
                rule.path.display(),
            )));
        }
        if rule.allowed_access & reads == 0
            || (rule.allowed_access & LANDLOCK_ACCESS_FS_EXECUTE == 0
                && metadata.permissions().mode() & 0o111 != 0
                && !metadata.file_type().is_char_device())
        {
            return Err(sandbox_rejection(format!(
                "bubblewrap cannot express selective read/execute rights for {}",
                rule.path.display(),
            )));
        }
        let destination = rule.path.to_string_lossy().into_owned();
        let source = rule.file.as_raw_fd();
        if metadata.file_type().is_char_device() {
            // Unlike --bind-fd, --dev-bind does not consume its source fd.
            // Finalization closes it so procfs cannot reopen the host device
            // outside the mounted grant's permissions.
            device_descriptors.push(super::DeviceMountFinalization {
                descriptor: source,
                destination: rule.path.clone(),
            });
            // --dev-bind has no fd spelling. Naming our still-owned descriptor
            // through procfs keeps the same pinned source, not a mutable path.
            argv.extend([
                "--dev-bind".into(),
                format!("/proc/self/fd/{source}"),
                destination.clone(),
            ]);
            if !writable {
                argv.extend(["--remount-ro".into(), destination]);
            }
        } else {
            argv.extend([
                if writable {
                    "--bind-fd"
                } else {
                    "--ro-bind-fd"
                }
                .into(),
                source.to_string(),
                destination,
            ]);
        }
        descriptors.push(rule.file.into());
    }
    // Workspace scratch is a named existing writable grant, not a broad host
    // /tmp bind or an unreported extra writable tmpfs.
    Ok(MountPlan {
        argv,
        descriptors,
        device_descriptors,
    })
}

/// Bubblewrap reads NUL-separated options from the descriptor given here, at
/// the point in its argv where the flag appears.
pub(in crate::stdlib::sandbox) const PAYLOAD_ENV_ARGS_FLAG: &str = "--args";

pub(in crate::stdlib::sandbox) fn payload_env_args() -> std::io::Result<OwnedFd> {
    let raw = unsafe { libc::memfd_create(c"harn-bwrap-env".as_ptr(), libc::MFD_CLOEXEC) };
    if raw < 0 {
        return Err(std::io::Error::last_os_error());
    }
    Ok(unsafe { OwnedFd::from_raw_fd(raw) })
}

fn sealed_filter(bytes: &[u8]) -> std::io::Result<OwnedFd> {
    let raw = unsafe {
        libc::memfd_create(
            c"harn-seccomp".as_ptr(),
            libc::MFD_CLOEXEC | libc::MFD_ALLOW_SEALING,
        )
    };
    if raw < 0 {
        return Err(std::io::Error::last_os_error());
    }
    let descriptor = unsafe { OwnedFd::from_raw_fd(raw) };
    let mut file = std::fs::File::from(descriptor);
    file.write_all(bytes)?;
    file.rewind()?;
    if unsafe {
        libc::fcntl(
            file.as_raw_fd(),
            libc::F_ADD_SEALS,
            libc::F_SEAL_WRITE | libc::F_SEAL_GROW | libc::F_SEAL_SHRINK | libc::F_SEAL_SEAL,
        )
    } < 0
    {
        return Err(std::io::Error::last_os_error());
    }
    Ok(file.into())
}

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

    fn live() -> bool {
        let available = available()
            && std::env::var("BWRAP_LAUNCHER").is_ok_and(|path| Path::new(&path).is_file());
        if !available {
            eprintln!("[linux-bwrap] exercised=0: functional namespace/mount probe or BWRAP_LAUNCHER test helper unavailable");
            assert_ne!(std::env::var("BWRAP_REQUIRE_TESTS").as_deref(), Ok("1"));
        }
        available
    }

    #[test]
    fn functional_probe_requires_the_confinement_marker_not_loader_exit_zero() {
        if !matches!(probe(), probe::ProbeOutcome::Completed(true)) {
            eprintln!("[linux-bwrap] exercised=0: functional namespace/mount probe unavailable");
            assert_ne!(std::env::var("BWRAP_REQUIRE_TESTS").as_deref(), Ok("1"));
            return;
        }
        let diagnostic = Command::new(executable().unwrap())
            .env("LD_TRACE_LOADED_OBJECTS", "1")
            .output()
            .unwrap();
        assert!(diagnostic.status.success());
        assert!(!diagnostic.stdout.is_empty());
        assert!(
            !probe_output_is_available(&diagnostic),
            "loader exit zero is not a confinement marker"
        );
    }

    fn policy(root: &Path) -> CapabilityPolicy {
        CapabilityPolicy {
            workspace_roots: vec![root.display().to_string()],
            sandbox_profile: SandboxProfile::Worktree,
            process_sandbox: Box::new(ProcessSandboxPolicy {
                presets: Some(vec![]),
                allow_child_workspace_write: true,
                allow_process_self_introspection: true,
                netns_launcher_path: std::env::var("BWRAP_LAUNCHER").ok(),
                ..Default::default()
            }),
            ..Default::default()
        }
    }

    fn launch(prepared: PrepareOutcome, cwd: &Path) -> std::process::Output {
        let PrepareOutcome::BubblewrapExec {
            wrapper,
            args,
            descriptors,
        } = prepared
        else {
            panic!("the bubblewrap owner did not prepare a pinned launch")
        };
        let mut command = Command::new(wrapper);
        command.args(args).current_dir(cwd);
        descriptors.attach(&mut command);
        command.output().unwrap()
    }

    /// Proven on a real bubblewrap launch: inherited allocator tuning reaches
    /// the confined payload, bubblewrap itself never carries it, and a
    /// behavior-changing control is still refused.
    #[test]
    fn allocator_tuning_reaches_the_payload_and_not_bubblewrap() {
        if !live() {
            return;
        }
        let root = tempfile::tempdir().unwrap();
        let policy = policy(root.path());
        let script = vec![
            "-c".into(),
            "printf '%s|%s' \"$MALLOC_ARENA_MAX\" \"${MALLOC_CHECK_-unset}\"".into(),
        ];
        let PrepareOutcome::BubblewrapExec {
            wrapper,
            args,
            descriptors,
        } = prepare("/usr/bin/sh", &script, &policy, policy.sandbox_profile).unwrap()
        else {
            panic!("the bubblewrap owner did not prepare a pinned launch")
        };
        let mut command = Command::new(wrapper);
        command
            .args(args)
            .current_dir(root.path())
            .env_clear()
            .env("PATH", "/usr/bin:/bin")
            .env("MALLOC_ARENA_MAX", "2");
        descriptors.attach(&mut command);
        crate::stdlib::sandbox::launch_environment::reapply_allocator_tuning(&mut command, true)
            .unwrap();
        assert!(!command
            .get_envs()
            .any(|(name, value)| name == "MALLOC_ARENA_MAX" && value.is_some()));
        let output = command.output().unwrap();
        assert!(output.status.success(), "{output:?}");
        assert_eq!(String::from_utf8_lossy(&output.stdout), "2|unset");

        // Negative control: the validator still refuses a loader control.
        assert!(crate::security::validate_process_environment(
            crate::security::ProcessEnvironmentBoundary::TrustedSetup,
            std::iter::empty(),
            [("GLIBC_TUNABLES".into(), Some("glibc.malloc.check=3".into()))],
        )
        .is_err());
    }

    #[test]
    fn workspace_write_requires_the_existing_child_write_grant() {
        if !live() {
            return;
        }
        let root = tempfile::tempdir().unwrap();
        std::fs::write(root.path().join("ordinary"), "readable").unwrap();
        let mut policy = policy(root.path());
        policy.capabilities =
            std::collections::BTreeMap::from([("workspace".into(), vec!["read_text".into()])]);
        policy.process_sandbox.allow_child_workspace_write = false;
        let read_only = vec!["-c".into(),
            "test \"$(cat ordinary)\" = readable; if printf escaped > marker; then exit 31; fi; printf denied-write".into()];
        let refused = launch(
            prepare("/usr/bin/sh", &read_only, &policy, policy.sandbox_profile).unwrap(),
            root.path(),
        );
        assert!(refused.status.success(), "{refused:?}");
        assert_eq!(refused.stdout, b"denied-write");
        assert!(!root.path().join("marker").exists());
        policy.process_sandbox.allow_child_workspace_write = true;
        let writable = vec!["-c".into(), "printf granted > marker".into()];
        let allowed = launch(
            prepare("/usr/bin/sh", &writable, &policy, policy.sandbox_profile).unwrap(),
            root.path(),
        );
        assert!(allowed.status.success(), "{allowed:?}");
        assert_eq!(
            std::fs::read(root.path().join("marker")).unwrap(),
            b"granted"
        );
    }

    #[test]
    fn opened_grant_survives_path_replacement_and_path_mount_control_does_not() {
        if !live() {
            return;
        }
        let tree = tempfile::tempdir().unwrap();
        let granted = tree.path().join("granted");
        std::fs::create_dir(&granted).unwrap();
        std::fs::write(granted.join("marker"), "original").unwrap();
        let policy = policy(&granted);
        let args = vec![
            "-c".into(),
            "cat \"$1/marker\"; printf reached > \"$1/new\"".into(),
            "probe".into(),
            granted.display().to_string(),
        ];
        let pinned = prepare("/usr/bin/sh", &args, &policy, policy.sandbox_profile).unwrap();
        let mut path_control =
            prepare("/usr/bin/sh", &args, &policy, policy.sandbox_profile).unwrap();
        let PrepareOutcome::BubblewrapExec { args, .. } = &mut path_control else {
            unreachable!()
        };
        let index = args
            .windows(3)
            .position(|args| args[0] == "--bind-fd" && args[2] == granted.display().to_string())
            .expect("actual workspace grant was rendered");
        args[index] = "--bind".into();
        args[index + 1] = granted.display().to_string();
        let admitted_inode = tree.path().join("admitted-inode");
        std::fs::rename(&granted, &admitted_inode).unwrap();
        std::fs::create_dir(&granted).unwrap();
        std::fs::write(granted.join("marker"), "replacement").unwrap();

        let pinned = launch(pinned, tree.path());
        assert!(pinned.status.success(), "{pinned:?}");
        assert_eq!(pinned.stdout, b"original");
        assert_eq!(
            std::fs::read(admitted_inode.join("new")).unwrap(),
            b"reached"
        );
        assert!(!granted.join("new").exists());
        let control = launch(path_control, tree.path());
        assert!(control.status.success(), "{control:?}");
        assert_eq!(control.stdout, b"replacement");
        assert_eq!(std::fs::read(granted.join("new")).unwrap(), b"reached");
    }

    #[test]
    fn outside_read_readonly_write_and_ungranted_scaffolding_are_denied() {
        if !live() {
            return;
        }
        let outside = tempfile::tempdir().unwrap();
        let root = tempfile::tempdir().unwrap();
        let readonly = tempfile::tempdir().unwrap();
        std::fs::write(outside.path().join("sentinel"), "outside").unwrap();
        std::fs::write(readonly.path().join("marker"), "readonly").unwrap();
        let mut policy = policy(root.path());
        policy.process_sandbox.read_roots = vec![readonly.path().display().to_string()];
        let args = vec![
            "-c".into(),
            concat!(
                "set -eu; test ! -e \"$1/sentinel\"; ",
                "test \"$(cat \"$2/marker\")\" = readonly; ",
                "if printf changed > \"$2/marker\" 2>/dev/null; then exit 31; fi; ",
                "if mkdir /ungranted 2>/dev/null; then exit 32; fi; ",
                "if printf 1 > /proc/self/oom_score_adj 2>/dev/null; then exit 33; fi; ",
                "printf inside > marker; printf boundary-reached"
            )
            .into(),
            "probe".into(),
            outside.path().display().to_string(),
            readonly.path().display().to_string(),
        ];
        let result = launch(
            prepare("/usr/bin/sh", &args, &policy, policy.sandbox_profile).unwrap(),
            root.path(),
        );
        assert!(result.status.success(), "{result:?}");
        assert_eq!(result.stdout, b"boundary-reached");
        assert_eq!(
            std::fs::read(root.path().join("marker")).unwrap(),
            b"inside"
        );
        assert_eq!(
            std::fs::read(readonly.path().join("marker")).unwrap(),
            b"readonly"
        );
        assert_eq!(
            std::fs::read(outside.path().join("sentinel")).unwrap(),
            b"outside"
        );
    }

    #[test]
    fn mount_failure_precedes_payload_and_pinned_handles_do_not_escape() {
        if !live() {
            return;
        }
        let root = tempfile::tempdir().unwrap();
        let policy = policy(root.path());
        let args = vec![
            "-c".into(),
            concat!(
                "set -eu; for fd in \"$@\"; do ",
                "test ! -e /proc/self/fd/$fd; done; ",
                "printf reached > marker; printf closed-pinned-handles"
            )
            .into(),
            "probe".into(),
        ];
        let mut valid = prepare("/usr/bin/sh", &args, &policy, policy.sandbox_profile).unwrap();
        let PrepareOutcome::BubblewrapExec {
            args, descriptors, ..
        } = &mut valid
        else {
            unreachable!()
        };
        args.extend(descriptors.numbers().into_iter().map(|fd| fd.to_string()));
        let mut invalid = prepare("/usr/bin/sh", &[], &policy, policy.sandbox_profile).unwrap();
        let PrepareOutcome::BubblewrapExec { args, .. } = &mut invalid else {
            unreachable!()
        };
        let index = args.iter().position(|arg| arg == "--bind-fd").unwrap();
        args[index + 1] = "-1".into();
        let payload = args.iter().position(|arg| arg == "--").unwrap();
        args.truncate(payload + 1);
        args.extend([
            "/usr/bin/sh".into(),
            "-c".into(),
            "printf escaped > marker".into(),
        ]);
        let failed = launch(invalid, root.path());
        assert!(
            !failed.status.success(),
            "invalid mount reached the payload: {failed:?}"
        );
        assert!(!root.path().join("marker").exists());
        let reached = launch(valid, root.path());
        assert!(reached.status.success(), "{reached:?}");
        assert_eq!(reached.stdout, b"closed-pinned-handles");
        assert_eq!(
            std::fs::read(root.path().join("marker")).unwrap(),
            b"reached"
        );
    }

    #[test]
    fn a_profile_without_device_grants_executes_without_the_finalizer() {
        if !live() {
            return;
        }
        let root = tempfile::tempdir().unwrap();
        let mut policy = policy(root.path());
        policy.process_sandbox.read_deny_roots = vec!["/dev".into()];
        policy.process_sandbox.netns_launcher_path = None;
        let payload = vec!["-c".into(), "printf reached > marker".into()];
        let prepared = prepare("/usr/bin/sh", &payload, &policy, policy.sandbox_profile).unwrap();
        let PrepareOutcome::BubblewrapExec { args, .. } = &prepared else {
            unreachable!()
        };
        let payload = args.iter().position(|arg| arg == "--").unwrap();
        assert_eq!(args[payload + 1], "/usr/bin/sh");
        let reached = launch(prepared, root.path());
        assert!(reached.status.success(), "{reached:?}");
        assert_eq!(
            std::fs::read(root.path().join("marker")).unwrap(),
            b"reached"
        );
    }

    #[test]
    fn device_grants_without_a_finalizer_refuse_before_payload() {
        if !live() {
            return;
        }
        let root = tempfile::tempdir().unwrap();
        let mut policy = policy(root.path());
        policy.process_sandbox.netns_launcher_path = None;
        let filesystem = filesystem_profile(
            "/usr/bin/sh",
            &policy,
            u64::MAX,
            ProcessFilesystemScope::PrivatePidNamespace,
        )
        .unwrap();
        assert!(!mounts(filesystem).unwrap().device_descriptors.is_empty());
        let payload = vec!["-c".into(), "printf reached > marker".into()];
        let refused = match prepare("/usr/bin/sh", &payload, &policy, policy.sandbox_profile) {
            Err(error) => error,
            Ok(_) => panic!("device setup was admitted without its finalizer"),
        };
        assert!(refused.to_string().contains("namespace helper"));
        assert!(!root.path().join("marker").exists());
    }

    #[test]
    fn replaced_device_mount_is_refused_before_payload() {
        if !live() {
            return;
        }
        let root = tempfile::tempdir().unwrap();
        let policy = policy(root.path());
        let payload_args = vec!["-c".into(), "printf reached > marker".into()];
        let mut changed = prepare(
            "/usr/bin/sh",
            &payload_args,
            &policy,
            policy.sandbox_profile,
        )
        .unwrap();
        let PrepareOutcome::BubblewrapExec { args, .. } = &mut changed else {
            unreachable!()
        };
        let index = args
            .windows(3)
            .position(|args| args[0] == "--dev-bind" && args[2] == "/dev/null")
            .expect("the null device was admitted by its pinned descriptor");
        args[index + 1] = "/dev/zero".into();
        let refused = launch(changed, root.path());
        assert!(!refused.status.success(), "{refused:?}");
        assert!(
            String::from_utf8_lossy(&refused.stderr)
                .contains("mounted device differs from its pinned source"),
            "{refused:?}"
        );
        assert!(!root.path().join("marker").exists());
        let reached = launch(
            prepare(
                "/usr/bin/sh",
                &payload_args,
                &policy,
                policy.sandbox_profile,
            )
            .unwrap(),
            root.path(),
        );
        assert!(reached.status.success(), "{reached:?}");
        assert_eq!(
            std::fs::read(root.path().join("marker")).unwrap(),
            b"reached"
        );
    }

    #[test]
    fn both_renderers_subtract_credential_aliases_and_terminate_alias_cycles() {
        if !live() {
            return;
        }
        let root = tempfile::tempdir().unwrap();
        let read = tempfile::tempdir().unwrap();
        let secret = read.path().join("credential");
        std::fs::write(&secret, "synthetic-denied").unwrap();
        std::fs::write(read.path().join("ordinary"), "ordinary").unwrap();
        let alias = read.path().join("alias");
        std::os::unix::fs::symlink(&secret, &alias).unwrap();
        std::os::unix::fs::symlink(read.path(), read.path().join("cycle")).unwrap();
        assert_eq!(std::fs::read(&alias).unwrap(), b"synthetic-denied");
        let mut policy = policy(root.path());
        policy.process_sandbox.read_roots = vec![read.path().display().to_string()];
        policy.process_sandbox.read_deny_roots = vec![secret.display().to_string()];
        let args = vec![
            "-c".into(),
            concat!(
                "set -eu; test \"$(cat \"$1/ordinary\")\" = ordinary; ",
                "for name in credential alias cycle/credential; do ",
                "if cat \"$1/$name\" >/dev/null 2>&1; then exit 34; fi; done; ",
                "printf credential-boundary-reached"
            )
            .into(),
            "probe".into(),
            read.path().display().to_string(),
        ];
        let result = launch(
            prepare("/usr/bin/sh", &args, &policy, policy.sandbox_profile).unwrap(),
            root.path(),
        );
        assert!(result.status.success(), "bubblewrap: {result:?}");
        assert_eq!(result.stdout, b"credential-boundary-reached");
        if super::super::landlock_available() {
            let mut command = super::super::super::build_std_command::<super::super::Backend>(
                "/usr/bin/sh",
                &args,
                &policy,
                policy.sandbox_profile,
            )
            .unwrap();
            let result = command.current_dir(root.path()).output().unwrap();
            assert!(result.status.success(), "landlock: {result:?}");
            assert_eq!(result.stdout, b"credential-boundary-reached");
        } else {
            eprintln!("[linux-landlock] exercised=0: Landlock unavailable on this host");
            assert_ne!(
                std::env::var("HARN_REQUIRE_LANDLOCK_TESTS").as_deref(),
                Ok("1")
            );
        }
    }
}