sandbox-jail
Run a command in the OS sandbox: Seatbelt (sandbox-exec) on macOS, bubblewrap (bwrap) on Linux. The caller defines the whole policy, and the crate turns it into a Seatbelt profile or bwrap arguments. A backend refuses a policy it cannot enforce before anything runs.
Command line
exec may write the current directory and the temp dirs, and nothing else. Credentials in your home are hidden, signals reach only its own processes, and on macOS it cannot run launchctl or a setuid binary such as sudo. -w adds a writable path, -r a read-only one inside it, --hide an unreadable one. The exit code is the command's, or 2 when the sandbox cannot start.
Library
Import it as sandbox_jail.
use ;
let home = from;
let policy = Policy ;
let sandbox = new?;
let status = sandbox.command.arg.current_dir.status?;
Sandbox::argv() gives the program and arguments that go before the command, for a caller that writes a script instead of spawning, such as one that runs the command in tmux.
Policy
| Field | Meaning | Seatbelt | bwrap |
|---|---|---|---|
writable |
May write, and read inside unreadable |
subpath allow |
--bind, must exist |
read_only |
May not write, even inside writable |
subpath deny, and its parents pinned |
--ro-bind |
pinned |
May not replace or remove the path itself | literal deny |
Refused |
unreadable |
May not read files in it | file-read-data deny |
Empty read-only --tmpfs |
readable |
Exceptions inside unreadable |
file-read-data allow |
--ro-bind |
scratch |
Starts empty and writable, writes go away with the command | Refused | Writable --tmpfs, refused inside a bound path |
hidden |
May not read, list, write or rename, wins over everything | file-read* and file-write* deny, and its parents pinned |
--tmpfs or /dev/null over it |
network |
TCP and UDP | remote ip deny |
--unshare-net |
unix_sockets |
Sockets it may connect to, by directory | remote unix-socket filters, plus the DNS resolver |
Only what unreadable and hidden keep out of reach |
mach_services |
macOS services it may look up | mach-lookup allowlist |
Not applicable |
apple_events |
May send Apple events | appleevent-send deny |
Not applicable |
own_signals |
Signals reach only its own processes | signal same-sandbox |
Always, pid namespace |
own_terminals |
Only pseudo-terminals it opened | com.apple.sandbox.pty extension |
--new-session |
no_exec |
Binaries it may not run | process-exec* deny |
Refused |
overlays |
Writes go to a layer over a directory | Refused | --overlay, check first with overlay_supported() |
sandbox_jail::known has lists a caller can opt into: credential paths, an env filter for credentials, the temp dirs, the Mach services common tools need, the setuid binaries and launchctl. None applies unless the caller puts it in its policy.
On macOS, sandbox_jail::macos::sandboxed() and sandbox_jail::macos::denies() ask the kernel about the sandbox the current process runs in. macOS cannot start a sandbox inside another one, and Backend::detect() returns Error::Nested there.
Development
&& &&
The tests in tests/sandbox.rs run real commands in the sandbox. On Linux they need bubblewrap and unprivileged user namespaces. A container's root is an overlay, which overlayfs will not stack on, so TMPDIR points the tests at a tmpfs. To run them from a Mac:
See docs/DESIGN.md for why it is built this way, and docs/orch.md for the first planned user.