sandbox

package
v0.206.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sandbox builds the P4 OS-level cage for running an untrusted command (a future broad-shell/exec tool) confined so it CANNOT bypass the agent's app-level cages. On Linux it self-restricts a re-exec'd worker with: a fresh UNPRIVILEGED user+network namespace (severs ALL egress, including the loopback model/memory services), Landlock filesystem confinement (RO system + RO worktree + RW scratch, inherited by every child and irreversible), and a seccomp denylist + no_new_privs. The cage is built and adversarially PROVEN before any shell capability is granted (cage-before-capability). Non-Linux builds get a stub that reports unavailable and refuses to run (fail-closed).

The whole path is rootless: an unprivileged user namespace grants the namespaced privilege to create the net namespace; Landlock and seccomp are unprivileged by design. No sudo, no firewall, no package installs.

Index

Constants

View Source
const NetABIFloor = 4

NetABIFloor is the Landlock ABI the cage's NETWORK guarantee needs: the rule set is landlock.V4 (paths + TCP bind/connect handling), and with FS rules but no ConnectTCP/BindTCP rule every TCP bind and connect is denied — ONLY on a kernel whose Landlock ABI is >= 4 (Linux 6.7). Below that, BestEffort() applies the path rules and silently drops the network ones (register H-27: the shape GHSA-vv6c-69r6-chg9 made worse in go-landlock v0.9.0, fixed upstream in v0.10.0 which this repo took in 0.104.0). The callers used to ask for ABI floor 1, which let a 6.2-era kernel run the cage with the network half of a shipped guarantee missing. Run raises any lower request to this floor; a caller that truly wants a path-only cage has no such mode — the guarantee is one thing, not two.

Variables

This section is empty.

Functions

func Available

func Available() (bool, string)

Available reports whether the OS cage can be enforced on this host: an architecture the cage can guard, Landlock present (ABI >= 1), seccomp supported, and unprivileged user namespaces enabled. Used as the capability gate — a future shell tool is granted ONLY when this is true (fail-closed).

func EffectiveABIFloor added in v0.206.0

func EffectiveABIFloor(requested int) int

EffectiveABIFloor is the floor Run enforces for a requested one: never below NetABIFloor.

func EnvAllowlist added in v0.206.0

func EnvAllowlist() []string

EnvAllowlist is every variable name a caged child can receive on this platform. Exported so the guarantee's test can assert the CLOSED set instead of restating it — a test that re-declares the list proves only that it can copy the list.

func IsWorker

func IsWorker() bool

IsWorker reports whether this process is a re-exec'd sandbox worker.

func RunWorkerFromEnv

func RunWorkerFromEnv()

RunWorkerFromEnv, when this process IS the worker, applies the cage and execs Spec.Argv — it never returns on success. Call it at the very top of main() (and TestMain) so the worker path runs before any normal startup. A no-op when this is not a worker.

Types

type Result

type Result struct {
	Stdout   string
	Stderr   string
	ExitCode int
	Signal   int  // terminating signal number if the command was killed (e.g. SIGSYS=31 from a seccomp KILL), else 0
	Refused  bool // true if the CAGE refused to run (setup failed) — distinct from the command running and exiting non-zero
}

Result is the outcome of a caged run.

Stderr is the command's own stderr, behind the cage's notes when it has any: lines that start with "sandbox " and say what the cage could not do for this run. They lead the stream, so a caller that shows Stderr shows them and a caller that wants to react finds them first. The command is not stopped by them (failing closed is a separate decision), and a run with nothing to report has none. A run that returns an error carries the same notes in the error text.

ExitCode is the command's exit status, or noExitStatus when there is none to report: the command was ended by a signal (Signal says which; Linux), or it ran and the cage could not read how it ended (Windows; a note says why). It is never 0 unless the command exited 0, because every caller reads 0 as success.

func Run

func Run(ctx context.Context, spec Spec) (Result, error)

Run launches cmd (spec.Argv) inside the OS cage: a fresh UNPRIVILEGED user+network+pid+ipc+uts namespace (the user namespace grants the namespaced privilege to make the others; the empty net namespace severs ALL egress, including the loopback model/memory services), then — in the re-exec'd worker — seccomp + Landlock. Returns the command's captured output and exit code.

type Spec

type Spec struct {
	Argv             []string `json:"argv"`              // command to run INSIDE the cage, e.g. ["/bin/sh","-c",cmd]
	Worktree         string   `json:"worktree"`          // the worker's working directory; read-only UNLESS WorktreeWritable
	WorktreeWritable bool     `json:"worktree_writable"` // grant RW (not RO) on Worktree — for a shell that must build/edit in place
	Scratch          string   `json:"scratch"`           // a writable scratch dir (always RW); also HOME/TMPDIR inside the cage
	ReadDirs         []string `json:"read_dirs"`         // extra read-only dirs (the system dirs below are always added)
	ReadFiles        []string `json:"read_files"`        // specific read-only (+executable) files to grant, e.g. a static helper binary
	ABIFloor         int      `json:"abi_floor"`         // minimum achieved Landlock ABI required, else fail-closed; raised to NetABIFloor by Run (0.115.22, H-27)

	// AllowedExecutables is the base-name allowlist of programs the caged command
	// may launch. WINDOWS ENFORCES it: before spawning, the resolved program's base
	// name (case-insensitive, trailing ".exe" dropped) must be in this list, else
	// Run returns Result{Refused:true} WITHOUT launching. The LINUX cage IGNORES it
	// — there the OS-level containment (namespaces + Landlock + seccomp) is the
	// boundary, not an executable name filter. An empty list on Windows refuses
	// every command (fail-closed).
	AllowedExecutables []string `json:"allowed_executables"`
}

Spec is the cage configuration the parent hands to the re-exec'd worker (serialized into workerEnv). All paths must be absolute.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL