libsandbox

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Overview

Package libsandbox confines a spawned foreign agent to "the wall": every path out of its tools and workspace — filesystem, network, environment — is absent by construction. If confinement cannot be built, the agent is not spawned.

Index

Constants

View Source
const (
	// ScrubOff inherits the full parent environment unscrubbed.
	ScrubOff = "off"
	// ScrubDenySecrets passes everything except the control plane and common
	// credential shapes (DefaultEnvDeny).
	ScrubDenySecrets = "deny-secrets"
	// ScrubStrict passes only DefaultEnvAllow plus operator additions.
	ScrubStrict = "strict"
)

Scrub postures for a shell contenox spawns, named in operator configuration.

View Source
const (
	// ModeRO grants read-only access through a filesystem carve-out.
	ModeRO = "ro"
	// ModeRW grants read-write access; use only where read-only demonstrably
	// breaks the agent.
	ModeRW = "rw"
)

Access modes for an FSCarveout.

Variables

View Source
var ErrInvalidCarveout = errors.New("libsandbox: invalid carve-out")

ErrInvalidCarveout is returned when a filesystem or network carve-out is malformed: bad access mode, missing justification, traversal path, or empty host.

View Source
var ErrInvalidSpec = errors.New("libsandbox: invalid spec")

ErrInvalidSpec is returned when the spec itself is unusable: no workspace, no scoped HOME, or no command to confine.

View Source
var ErrIsolation = errors.New("libsandbox: isolation failure")

ErrIsolation is returned when building or enforcing the wall fails. The wall is fail-closed, so the agent is not spawned.

View Source
var ErrLandlockUnsupported = errors.New("libsandbox: landlock unsupported by kernel")

ErrLandlockUnsupported reports that the running kernel exposes no usable Landlock filesystem ABI; it fails closed rather than running unconfined.

Functions

func Command

func Command(ctx context.Context, spec Spec, name string, args ...string) (*exec.Cmd, error)

Command assembles the confined *exec.Cmd for name and args without starting it; errors wrap ErrInvalidSpec or ErrInvalidCarveout.

func ControlPlaneEnvDeny

func ControlPlaneEnvDeny() []string

ControlPlaneEnvDeny is the non-negotiable veto for contenox's own control-plane variables; no Allow may re-permit these.

func DefaultEnvAllow

func DefaultEnvAllow() []string

DefaultEnvAllow is the baseline of POSIX-shell essentials. HOME is absent because scrubEnv always forces the scoped HOME.

func DefaultEnvDeny

func DefaultEnvDeny() []string

DefaultEnvDeny is the union of ControlPlaneEnvDeny and SecretEnvDeny used by DefaultEnvPolicy.

func EnvScrub

func EnvScrub(mode string, extraAllow, extraDeny []string) func(parent []string) []string

EnvScrub returns the scrub function for a posture, or nil when inactive (ScrubOff or an unrecognized mode).

func LoadCarveouts

func LoadCarveouts(r io.Reader) (fs []FSCarveout, net []NetCarveout, err error)

LoadCarveouts decodes and validates a JSON carve-out document into fs and net, rejecting unknown fields and wrapping errors in ErrInvalidCarveout.

func OverlayEnv

func OverlayEnv(env []string, set map[string]string) []string

OverlayEnv returns env with the pairs in set applied on top, de-duplicated by name and sorted. Malformed entries are dropped.

func ParseEnvList

func ParseEnvList(s string) []string

ParseEnvList splits a comma/semicolon/whitespace separated allow/deny list into trimmed entries, returning nil for empty input.

func Preflight

func Preflight() error

Preflight reports whether the Landlock confinement floor can be built on this host without attempting a spawn. A non-nil error wraps ErrIsolation.

func ScrubModeValid

func ScrubModeValid(mode string) bool

ScrubModeValid reports whether mode is a recognized scrub posture.

func SecretEnvDeny

func SecretEnvDeny() []string

SecretEnvDeny is the common credential name-shape veto, letting a loose "pass everything" allow still strip obvious secrets.

func ShimMain

func ShimMain() (handled bool, err error)

ShimMain is the sandbox shim entrypoint, called at the top of main(). It returns (false, nil) when not invoked as the shim; otherwise it does not return except with (true, err) wrapping ErrIsolation, which is fatal.

func SystemExecDirs

func SystemExecDirs() []string

SystemExecDirs returns the confined PATH's stock binary locations in precedence order, each a subtree of the Landlock-granted system runtime.

Types

type EnvPolicy

type EnvPolicy struct {
	// Allow lists names or globs to pass through; a glob has a single leading
	// or trailing "*".
	Allow []string
	// Deny lists names or globs that are never passed even when Allow matches.
	Deny []string
}

EnvPolicy is the operator-facing whitelist deciding which parent environment variables a confined process may inherit; Deny always wins over Allow.

func DefaultEnvPolicy

func DefaultEnvPolicy() EnvPolicy

DefaultEnvPolicy is the recommended starting point: DefaultEnvAllow gated by DefaultEnvDeny.

func EnvPolicyForMode

func EnvPolicyForMode(mode string, extraAllow, extraDeny []string) (policy EnvPolicy, active bool)

EnvPolicyForMode builds the EnvPolicy for a named scrub posture extended with extra allow/deny entries; active is false for ScrubOff and unknown modes.

func (EnvPolicy) Allowing

func (p EnvPolicy) Allowing(names ...string) EnvPolicy

Allowing returns a copy of p with names appended to Allow.

func (EnvPolicy) Apply

func (p EnvPolicy) Apply(parentEnv []string) []string

Apply is Resolve's counterpart returning surviving "KEY=VALUE" entries rather than names. Unlike scrubEnv it does not force a scoped HOME.

func (EnvPolicy) Denying

func (p EnvPolicy) Denying(names ...string) EnvPolicy

Denying returns a copy of p with names appended to Deny.

func (EnvPolicy) Resolve

func (p EnvPolicy) Resolve(parentEnv []string) []string

Resolve expands the policy against parentEnv into the sorted, de-duplicated names that are present and match Allow but not Deny.

type FSCarveout

type FSCarveout struct {
	// Path is the filesystem path to make reachable (e.g. "~/.claude"); must be non-empty and must not contain a ".." traversal segment.
	Path string `json:"path"`
	// Mode is ModeRO or ModeRW.
	Mode string `json:"mode"`
	// Needs states, in plain words, why the agent breaks without this path.
	Needs string `json:"needs"`
}

FSCarveout is one hole in the filesystem wall (a path, the access granted, and — mandatory — why it's needed), also the on-disk shape of a filesystem necessity-list entry.

type NetCarveout

type NetCarveout struct {
	// Host is the network host to allow egress to (e.g. "registry.npmjs.org").
	Host string `json:"host"`
	// Ports narrows the hole to specific destination ports (empty allows every port, 1-65535 each); a connection to any other port on the same host is refused and logged like a non-carve-out host.
	Ports []int `json:"ports,omitempty"`
	// Needs states why the agent breaks without reaching this host.
	Needs string `json:"needs"`
}

NetCarveout is one hole in the network wall (a host, why it's needed, and optionally which ports), also the on-disk shape of a network necessity-list entry.

type Spec

type Spec struct {
	// WorkspaceRoot is the one writable directory the agent works in
	// (pinned via cmd.Dir).
	WorkspaceRoot string

	// Home is the scoped HOME for this run, so the agent's tools resolve ~/…
	// here instead of the operator's real home; carve-outs bind under it
	// while ~/.ssh, ~/.aws, ~/.npmrc etc. stay unreachable.
	Home string

	// EnvAllow lists names of parent environment variables passed through unchanged; everything else, including any inherited credential, is dropped by default.
	EnvAllow []string

	// EnvSet holds explicit extra variables, overriding same-named EnvAllow values; HOME cannot be set here, it is always forced to Home.
	EnvSet map[string]string

	// FS lists filesystem carve-outs outside the workspace; empty means the workspace is the only reachable path.
	FS []FSCarveout

	// Net lists network carve-outs; empty means no route.
	Net []NetCarveout

	// AllowPrivateEgress opts into dialing carve-out hosts that resolve to non-public addresses (loopback, link-local, RFC1918/ULA, unspecified, multicast); default false is SSRF-safe, and it requires NetworkWall.
	AllowPrivateEgress bool

	// NetworkWall opts into the namespaced network wall (routeless netns plus metered per-host egress for Net carve-outs), built via an unprivileged user namespace; default off, and a spec naming Net or AllowPrivateEgress with it off is rejected.
	NetworkWall bool

	// Tracker observes the wall (Command's assembly lifecycle, and later every blocked bypass attempt); nil is treated as libtracker.NoopTracker.
	Tracker libtracker.ActivityTracker

	// SyscallTap opts into a seccomp user-notify telemetry tap (Linux only, default off) that reports execve/execveat attempts to the Tracker and always allows them to continue — it observes, it does not enforce.
	SyscallTap bool
}

Spec is the confinement spec for one spawned agent. Anything not named here is absent once the wall is enforced.

Jump to

Keyboard shortcuts

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