Skip to main content

Sandbox

Struct Sandbox 

Source
pub struct Sandbox { /* private fields */ }
Expand description

Builder for creating sandbox policies mirroring systemd.exec options.

§Example

use std::path::Path;
use sandbox_run::{Sandbox, ProtectSystem, ProtectHome};

let sandbox = Sandbox::new()
    .protect_system(ProtectSystem::Strict)
    .protect_home(ProtectHome::Yes)
    .read_write_paths([Path::new("/tmp/myapp")])
    .no_new_privileges(true)
    .limit_nofile(256)
    .build()
    .unwrap();

Implementations§

Source§

impl Sandbox

Source

pub fn new() -> Self

Create a new sandbox builder with default settings (no restrictions).

Source

pub fn protect_system(self, level: ProtectSystem) -> Self

Set system directory protection level (systemd: ProtectSystem=)

  • No: No protection
  • Yes: /usr, /boot, /efi read-only
  • Full: + /etc read-only
  • Strict: Entire filesystem read-only except API filesystems
Source

pub fn protect_home(self, level: ProtectHome) -> Self

Set home directory protection level (systemd: ProtectHome=)

  • No: No protection
  • Yes: /home, /root, /run/user inaccessible
  • ReadOnly: /home, /root, /run/user read-only
Source

pub fn read_only_paths<I, P>(self, paths: I) -> Self
where I: IntoIterator<Item = P>, P: AsRef<Path>,

Make paths read-only (systemd: ReadOnlyPaths=)

Writing to these paths will be denied.

Source

pub fn read_write_paths<I, P>(self, paths: I) -> Self
where I: IntoIterator<Item = P>, P: AsRef<Path>,

Allow read-write access to paths (systemd: ReadWritePaths=)

Use this to create exceptions within ProtectSystem=strict.

Source

pub fn inaccessible_paths<I, P>(self, paths: I) -> Self
where I: IntoIterator<Item = P>, P: AsRef<Path>,

Make paths completely inaccessible (systemd: InaccessiblePaths=)

Neither reading nor writing will be possible.

Source

pub fn exec_paths<I, P>(self, paths: I) -> Self
where I: IntoIterator<Item = P>, P: AsRef<Path>,

Allow execution only from these paths (systemd: ExecPaths=)

Binaries outside these paths cannot be executed.

Source

pub fn no_exec_paths<I, P>(self, paths: I) -> Self
where I: IntoIterator<Item = P>, P: AsRef<Path>,

Deny execution from these paths (systemd: NoExecPaths=)

Binaries in these paths cannot be executed.

Source

pub fn private_network(self, enable: bool) -> Self

Isolate network access (systemd: PrivateNetwork=)

When enabled, all network access is blocked.

Source

pub fn no_new_privileges(self, enable: bool) -> Self

Prevent privilege escalation (systemd: NoNewPrivileges=)

When enabled, the process cannot gain new privileges through setuid/setgid bits or file capabilities.

Source

pub fn limit_nofile(self, limit: u64) -> Self

Max open file descriptors (systemd: LimitNOFILE=)

Source

pub fn limit_nproc(self, limit: u64) -> Self

Max number of processes (systemd: LimitNPROC=)

Source

pub fn limit_core(self, limit: u64) -> Self

Max core dump size in bytes (systemd: LimitCORE=)

Set to 0 to disable core dumps.

Source

pub fn limit_fsize(self, limit: u64) -> Self

Max file size in bytes (systemd: LimitFSIZE=)

Source

pub fn limit_data(self, limit: u64) -> Self

Max data segment size in bytes (systemd: LimitDATA=)

Source

pub fn limit_stack(self, limit: u64) -> Self

Max stack size in bytes (systemd: LimitSTACK=)

Source

pub fn limit_cpu(self, limit: u64) -> Self

Max CPU time in seconds (systemd: LimitCPU=)

Source

pub fn limit_memlock(self, limit: u64) -> Self

Max locked memory in bytes (systemd: LimitMEMLOCK=)

Source

pub fn build(self) -> Result<SandboxPolicy, SandboxError>

Build the sandbox policy.

This validates the configuration and prepares platform-specific data needed to apply the sandbox.

Trait Implementations§

Source§

impl Clone for Sandbox

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Sandbox

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Sandbox

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.