sandbox

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 11 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 confines a command with what the operating system offers: no network unless asked for, a read-only view of the host, and one writable directory. It knows nothing about tasks. The engine turns a task body into a set of paths and a network flag (a Spec), and a Backend turns that into a command (spec 053).

The only backend is bubblewrap, on Linux. Every other system has none, and says so: a caller never gets a command that looks confined and is not.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound    = errors.New("bubblewrap (bwrap) was not found on the PATH")
	ErrCannotStart = errors.New("bubblewrap could not start a sandbox")
)

The two ways bubblewrap can be unavailable on Linux.

View Source
var ErrUnsupported = errors.New("no sandbox backend")

ErrUnsupported is the reason of a system with no backend.

Functions

func Ancestors

func Ancestors(workspace, path string) []string

Ancestors lists the directories between workspace and path, shallowest first, excluding both. A path outside the workspace has none.

func BwrapArgs

func BwrapArgs(spec Spec, argv []string) []string

BwrapArgs is the argument list that runs argv under spec. The order is the contract: a later mount covers an earlier one, so the workspace is visible again where a hidden directory covered it, a hidden location inside the workspace is mounted after the workspace, and the read-only remounts come after every mount that needs a mount point inside them.

func CheckProtected

func CheckProtected(workspace, path string, links bool) error

CheckProtected reports why path, which lies inside workspace, cannot be protected by a mount: a part of it below the workspace is a symbolic link, or, when links is set, it is a file with more than one name. A path that does not exist has nothing to protect and passes.

func RepoRoot

func RepoRoot(dir string) string

RepoRoot returns the nearest directory at or above dir that holds a .git entry, or dir when there is none. It runs no git: the answer decides what git itself may see.

func Resolve

func Resolve(path string) string

Resolve returns path with the symbolic links of its existing part followed. The part that does not exist yet is kept as written.

func VarRunDir

func VarRunDir() bool

VarRunDir reports whether /var/run is a directory of its own. On current systems it is a link to /run and is hidden with it.

func Within

func Within(parent, path string) bool

Within reports whether path is parent or lies under it. Both are compared as written, so a caller resolves them first.

Types

type Backend

type Backend interface {
	// Name is recorded in the audit log and shown to the operator. A host
	// with no backend has the empty name.
	Name() string
	// Path is the resolved program the backend runs, or "".
	Path() string
	// Probe starts a real sandbox. Nil means the backend can confine.
	Probe(ctx context.Context) error
	// Command returns the command that runs argv under the rules of spec.
	Command(ctx context.Context, spec Spec, argv []string) (*exec.Cmd, error)
}

Backend provides the sandbox on a host.

func Detect

func Detect(goos string, probe []string) Backend

Detect returns the backend of an operating system. probe is the command a Probe runs inside the sandbox: it must exit 0.

type Bubblewrap

type Bubblewrap struct {
	// contains filtered or unexported fields
}

Bubblewrap is the Linux backend: the bwrap program with user namespaces.

func NewBubblewrap

func NewBubblewrap(lookPath func(string) (string, error), probe []string) *Bubblewrap

NewBubblewrap returns the backend. lookPath finds the program, as exec.LookPath does, and probe is the command a Probe runs inside.

func (*Bubblewrap) Command

func (b *Bubblewrap) Command(ctx context.Context, spec Spec, argv []string) (*exec.Cmd, error)

Command returns the command that runs argv confined by spec.

func (*Bubblewrap) Name

func (*Bubblewrap) Name() string

Name is the backend's name in records and messages.

func (*Bubblewrap) Path

func (b *Bubblewrap) Path() string

Path is the program with its links followed, or "" when there is none. It is looked up on every call, so a program installed later is found.

func (*Bubblewrap) Probe

func (b *Bubblewrap) Probe(ctx context.Context) error

Probe starts a sandbox over a temporary directory and runs the probe command in it. The program's version says nothing: on a host that denies user namespaces it prints one and still cannot start.

type LinkedError

type LinkedError struct {
	Path  string
	Count uint64
}

LinkedError reports a protected file that has more than one name. A mount protects a name, and the file could be written through the other one.

func (*LinkedError) Error

func (e *LinkedError) Error() string

type Mode

type Mode string

Mode is the operator's choice for a server.

const (
	ModeEnforce Mode = "enforce"
	ModeOff     Mode = "off"
)

The modes. There is no automatic one: confining where it works and running unconfined elsewhere would be a silent fallback.

func ParseMode

func ParseMode(value string) (Mode, error)

ParseMode reads a mode as the operator wrote it.

type Spec

type Spec struct {
	// Workspace is the directory the command may write, and where it starts.
	Workspace string
	// ReadOnly makes the workspace read-only as well: nothing is writable.
	ReadOnly bool
	// Protected are paths inside the workspace that stay readable and cannot
	// be changed, removed, renamed or replaced.
	Protected []string
	// HiddenDirs and HiddenFiles are seen empty and read-only.
	HiddenDirs, HiddenFiles []string
	// Empty is an empty file that stands in for each hidden file. A tool
	// that insists on reading its configuration then reads nothing, where
	// the null device, which is used when Empty is not set, cannot be opened
	// at all under a mount that allows no devices.
	Empty string
	// Tmp is bound at /tmp. Empty gives an empty read-only /tmp.
	Tmp string
	// Cache is a second writable directory, bound at its own path.
	Cache string
	// Network keeps the host's network. Without it the command has a
	// loopback of its own and nothing else.
	Network bool
	// ResolvConf is the resolver file to bind back for a command with the
	// network, when it lies under a hidden directory.
	ResolvConf string
	// VarRunDir says /var/run is a directory of its own, not a link to /run.
	VarRunDir bool
	// Exe is a program to keep reachable at its own path, wherever it lies.
	Exe string
}

Spec is the rule set for one command. Every path is absolute and resolved, and exists: the caller chooses and classifies them, and nothing here reads a file.

type SymlinkError

type SymlinkError struct {
	Path string
	Link string
}

SymlinkError reports a protected path that is reached through a symbolic link inside the workspace. A mount follows the link, so the link itself stays replaceable.

func (*SymlinkError) Error

func (e *SymlinkError) Error() string

Jump to

Keyboard shortcuts

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