Skip to main content

sandlock_core/
lib.rs

1pub mod error;
2pub mod http;
3pub mod sandbox;     // formerly `policy`; contains Sandbox + SandboxBuilder + Confinement
4pub mod profile;
5pub mod result;
6pub(crate) mod arch;
7pub(crate) mod sys;
8pub mod landlock;
9pub mod protection;
10pub mod seccomp;
11pub(crate) mod resource;
12pub(crate) mod network;
13pub mod context;
14pub(crate) mod resolved;
15pub(crate) mod seccomp_plan;
16pub(crate) mod vdso;
17pub(crate) mod random;
18pub(crate) mod time;
19pub(crate) mod cow;
20pub(crate) mod checkpoint;
21pub(crate) mod freeze;
22pub mod netlink;
23pub(crate) mod procfs;
24pub(crate) mod port_remap;
25pub mod pipeline;
26pub mod policy_fn;
27pub mod image;
28pub mod fork;
29pub(crate) mod ca_inject;
30pub(crate) mod chroot;
31pub mod dry_run;
32mod transparent_proxy;
33
34pub use error::SandlockError;
35pub use sys::structs::{SeccompData, SeccompNotif};
36pub use checkpoint::Checkpoint;
37pub use protection::{Protection, ProtectionState, ProtectionPolicy, ProtectionStatus};
38pub use sandbox::{Confinement, ConfinementBuilder, Sandbox, SandboxBuilder};
39pub use result::{RunResult, ExitStatus};
40pub use pipeline::{Stage, Pipeline, Gather};
41pub use dry_run::{Change, ChangeKind, DryRunResult};
42// Sectioned-profile parsing types: ProfileInput is the top-level deserialization
43// target; ProgramSpec carries [program].exec/args (not a Sandbox field).
44pub use crate::profile::{ProfileInput, ProgramSpec};
45
46// Public extension API — see docs/extension-handlers.md.
47pub use seccomp::dispatch::{Handler, HandlerCtx, HandlerError};
48pub use seccomp::syscall::{Syscall, SyscallError};
49
50/// Query the Landlock ABI version supported by the running kernel.
51pub fn landlock_abi_version() -> Result<u32, error::ConfinementError> {
52    landlock::abi_version()
53}
54
55/// Minimum Landlock ABI version required by sandlock.
56pub const MIN_LANDLOCK_ABI: u32 = landlock::MIN_ABI;
57
58/// Confine the calling process with Landlock restrictions.
59///
60/// This applies `PR_SET_NO_NEW_PRIVS` and Landlock rules from the sandbox's
61/// filesystem fields. IPC and signal isolation are always enabled. The
62/// confinement is **irreversible**.
63///
64/// This does NOT fork or exec — it confines the current process in-place.
65pub fn confine(confinement: &Confinement) -> Result<(), SandlockError> {
66    // Set NO_NEW_PRIVS (required for Landlock)
67    if unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) } != 0 {
68        return Err(SandlockError::Runtime(
69            error::SandboxRuntimeError::Confinement(
70                error::ConfinementError::Landlock(format!(
71                    "prctl(PR_SET_NO_NEW_PRIVS): {}",
72                    std::io::Error::last_os_error()
73                ))
74            )
75        ));
76    }
77
78    let mut builder = Sandbox::builder();
79    for path in &confinement.fs_readable {
80        builder = builder.fs_read(path.clone());
81    }
82    for path in &confinement.fs_writable {
83        builder = builder.fs_write(path.clone());
84    }
85    let stripped = builder.build()?;
86
87    // Apply Landlock filesystem rules.
88    landlock::confine_filesystem(&stripped)
89}