Skip to main content

sandlock_core/
lib.rs

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