Skip to main content

sandlock_core/
lib.rs

1pub mod error;
2pub mod policy;
3pub mod profile;
4pub mod result;
5pub mod sandbox;
6pub(crate) mod sys;
7pub mod landlock;
8pub mod seccomp;
9pub(crate) mod resource;
10pub(crate) mod network;
11pub mod context;
12pub(crate) mod vdso;
13pub(crate) mod random;
14pub(crate) mod time;
15pub(crate) mod cow;
16pub(crate) mod checkpoint;
17pub(crate) mod procfs;
18pub(crate) mod port_remap;
19pub mod pipeline;
20pub mod policy_fn;
21pub mod image;
22pub mod fork;
23pub(crate) mod chroot;
24pub mod dry_run;
25
26pub use error::SandlockError;
27pub use checkpoint::Checkpoint;
28pub use policy::{Policy, PolicyBuilder};
29pub use result::{RunResult, ExitStatus};
30pub use sandbox::Sandbox;
31pub use pipeline::{Stage, Pipeline};
32pub use dry_run::{Change, ChangeKind, DryRunResult};
33
34/// Query the Landlock ABI version supported by the running kernel.
35pub fn landlock_abi_version() -> Result<u32, error::ConfinementError> {
36    landlock::abi_version()
37}
38
39/// Minimum Landlock ABI version required by sandlock.
40pub const MIN_LANDLOCK_ABI: u32 = landlock::MIN_ABI;
41
42/// Confine the calling process with Landlock filesystem restrictions.
43///
44/// This applies `PR_SET_NO_NEW_PRIVS` and Landlock rules from the policy's
45/// `fs_readable`, `fs_writable`, and `fs_denied` fields. The confinement is
46/// **irreversible** — once applied, the process cannot regain access to
47/// restricted paths.
48///
49/// Only filesystem rules from the policy are used. Network, seccomp, resource
50/// limits, and other policy fields are ignored.
51///
52/// This does NOT fork or exec — it confines the current process in-place.
53pub fn confine_current_process(policy: &Policy) -> Result<(), SandlockError> {
54    // Set NO_NEW_PRIVS (required for Landlock)
55    if unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) } != 0 {
56        return Err(SandlockError::Sandbox(
57            error::SandboxError::Confinement(
58                error::ConfinementError::Landlock(format!(
59                    "prctl(PR_SET_NO_NEW_PRIVS): {}",
60                    std::io::Error::last_os_error()
61                ))
62            )
63        ));
64    }
65
66    // Apply Landlock filesystem rules
67    landlock::confine(policy)
68}