Expand description
§kernex-sandbox
OS-level system protection for AI agent subprocesses.
Uses a blocklist approach: everything is allowed by default, then dangerous system directories and the runtime’s core data are blocked.
- macOS: Apple Seatbelt via
sandbox-exec -p <profile>— denies reads and writes to{data_dir}/data/(memory.db) andconfig.toml; denies writes to/System,/bin,/sbin,/usr/{bin,sbin,lib,libexec},/private/etc,/Library. - Linux: Landlock LSM via
pre_exechook (kernel 5.13+ minimum, 6.12+ for full ABI::V5 enforcement; older kernels apply best-effort protection using only the rights they support) — broad read-only on/with full access to$HOME,/tmp,/var/tmp,/opt,/srv,/run,/media,/mnt; restricted access to{data_dir}/data/andconfig.toml. - Other: Falls back to a plain command with a warning.
protected_command is best-effort and never fails: when the host
cannot apply OS-level enforcement it returns an unsandboxed command
after emitting a once-per-process stderr warning. For deployments where
running unsandboxed is unacceptable, set
SandboxProfile::require_os_enforcement = true (or export
KERNEX_REQUIRE_SANDBOX=1, which applies process-wide without code
changes) and call try_protected_command instead, which surfaces an
std::io::Error when enforcement is unavailable. os_enforcement_available
reports the host’s capability without building a command.
The fail-open fallback is the 0.x default; kernex 1.0 flips the library
default to fail-closed (require_os_enforcement = true). Embedders that
need today’s lenient behaviour at 1.0 will have to opt out explicitly.
§Platform enforcement notes
- macOS: enforcement relies on
/usr/bin/sandbox-exec, which Apple documents as deprecated but still ships and uses in its own tooling. There is no supported third-party replacement API. If a future macOS release removes it,os_enforcement_availablereturnsfalseand the fail-mode policy above applies. - Linux: Landlock enforcement depth depends on the kernel ABI
(5.13 minimum; 6.7+ adds TCP restrictions; 6.12+ for ABI v5). On
kernels between 5.13 and 6.11 some access rights cannot be enforced
and the sandbox degrades to what the kernel supports; the ruleset
status is logged at spawn time but is not observable programmatically
by callers.
os_enforcement_availablereports Landlock presence, not enforcement depth.
§Subprocess network policy
Sandboxed subprocesses are denied network access by default
(SandboxProfile::allow_network = false); tools that genuinely need
egress opt in explicitly. Coverage is asymmetric by platform:
- macOS Seatbelt: full — denies every socket operation (TCP, UDP, and local sockets; DNS resolution is consequently unavailable too).
- Linux Landlock: TCP bind/connect only, and only on kernels 6.7+ (ABI v4). Older kernels cannot restrict the network; the gap is logged at spawn time. UDP and non-TCP sockets are never restricted.
In-process HTTP performed by the runtime itself (e.g. a web_fetch
tool implemented without a subprocess) is not subject to this policy;
it has its own request-level validation.
Also provides is_write_blocked and is_read_blocked for code-level
enforcement in tool executors (protects memory.db and config.toml on all
platforms).
This crate is intentionally standalone with zero internal dependencies, making it usable outside the Kernex ecosystem.
Structs§
- Sandbox
Profile - Configuration for system sandbox restrictions.
Constants§
- BASE_
ENV_ ALLOWLIST - Environment variables preserved when hardening a spawned subprocess.
- REQUIRE_
SANDBOX_ ENV - Environment variable that requires OS-level sandbox enforcement
process-wide, equivalent to setting
SandboxProfile::require_os_enforcementon every profile.
Functions§
- credential_
read_ deny_ dirs - Credential directories/files under
$HOMEwhose READS are denied to sandboxed subprocesses (the credential read-deny list). - enforcement_
required - Returns
truewhen OS-level sandbox enforcement is required, either by the profile flag or by theREQUIRE_SANDBOX_ENVenvironment variable. - hardened_
env - Clear the inherited environment on
cmdand re-apply only the minimal base allowlist (BASE_ENV_ALLOWLISTplus theLC_*family) from the parent process. - is_
read_ blocked - Check if a read from the given path should be blocked.
- is_
write_ blocked - Check if a write to the given path should be blocked.
- os_
enforcement_ available - Returns
truewhen the current host can apply OS-level sandbox enforcement (Seatbelt on macOS or Landlock on Linux). Used bytry_protected_commandto honourrequire_os_enforcement. - protected_
command - Build a
Commandwith OS-level system protection (best-effort). - try_
protected_ command - Strict variant of
protected_command. - write_
allow_ dirs - Directories whose WRITES are allowed under the default lockdown posture
(writes are otherwise denied inside
$HOME).