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
impl Sandbox
Sourcepub fn protect_system(self, level: ProtectSystem) -> Self
pub fn protect_system(self, level: ProtectSystem) -> Self
Set system directory protection level (systemd: ProtectSystem=)
No: No protectionYes: /usr, /boot, /efi read-onlyFull: + /etc read-onlyStrict: Entire filesystem read-only except API filesystems
Sourcepub fn protect_home(self, level: ProtectHome) -> Self
pub fn protect_home(self, level: ProtectHome) -> Self
Set home directory protection level (systemd: ProtectHome=)
No: No protectionYes: /home, /root, /run/user inaccessibleReadOnly: /home, /root, /run/user read-only
Sourcepub fn read_only_paths<I, P>(self, paths: I) -> Self
pub fn read_only_paths<I, P>(self, paths: I) -> Self
Make paths read-only (systemd: ReadOnlyPaths=)
Writing to these paths will be denied.
Sourcepub fn read_write_paths<I, P>(self, paths: I) -> Self
pub fn read_write_paths<I, P>(self, paths: I) -> Self
Allow read-write access to paths (systemd: ReadWritePaths=)
Use this to create exceptions within ProtectSystem=strict.
Sourcepub fn inaccessible_paths<I, P>(self, paths: I) -> Self
pub fn inaccessible_paths<I, P>(self, paths: I) -> Self
Make paths completely inaccessible (systemd: InaccessiblePaths=)
Neither reading nor writing will be possible.
Sourcepub fn exec_paths<I, P>(self, paths: I) -> Self
pub fn exec_paths<I, P>(self, paths: I) -> Self
Allow execution only from these paths (systemd: ExecPaths=)
Binaries outside these paths cannot be executed.
Sourcepub fn no_exec_paths<I, P>(self, paths: I) -> Self
pub fn no_exec_paths<I, P>(self, paths: I) -> Self
Deny execution from these paths (systemd: NoExecPaths=)
Binaries in these paths cannot be executed.
Sourcepub fn private_network(self, enable: bool) -> Self
pub fn private_network(self, enable: bool) -> Self
Isolate network access (systemd: PrivateNetwork=)
When enabled, all network access is blocked.
Sourcepub fn no_new_privileges(self, enable: bool) -> Self
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.
Sourcepub fn limit_nofile(self, limit: u64) -> Self
pub fn limit_nofile(self, limit: u64) -> Self
Max open file descriptors (systemd: LimitNOFILE=)
Sourcepub fn limit_nproc(self, limit: u64) -> Self
pub fn limit_nproc(self, limit: u64) -> Self
Max number of processes (systemd: LimitNPROC=)
Sourcepub fn limit_core(self, limit: u64) -> Self
pub fn limit_core(self, limit: u64) -> Self
Max core dump size in bytes (systemd: LimitCORE=)
Set to 0 to disable core dumps.
Sourcepub fn limit_fsize(self, limit: u64) -> Self
pub fn limit_fsize(self, limit: u64) -> Self
Max file size in bytes (systemd: LimitFSIZE=)
Sourcepub fn limit_data(self, limit: u64) -> Self
pub fn limit_data(self, limit: u64) -> Self
Max data segment size in bytes (systemd: LimitDATA=)
Sourcepub fn limit_stack(self, limit: u64) -> Self
pub fn limit_stack(self, limit: u64) -> Self
Max stack size in bytes (systemd: LimitSTACK=)
Sourcepub fn limit_memlock(self, limit: u64) -> Self
pub fn limit_memlock(self, limit: u64) -> Self
Max locked memory in bytes (systemd: LimitMEMLOCK=)
Sourcepub fn build(self) -> Result<SandboxPolicy, SandboxError>
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.