Documentation
¶
Overview ¶
Package sandbox is the portable sandbox contract that ext and github.com/webong/workload share (docs/adr-sandboxing.md): the boundary levels, the policy and lifecycle vocabulary, the rules that turn a request and a policy into a grant, the check of a grant against what a runner can enforce, and the report every launch returns.
It is data and rules only. It confines nothing, starts nothing and imports nothing but the standard library, so a runner that must stay on the standard library can depend on it. The mechanisms that enforce a grant live elsewhere: src/cto/internal/confine for one local process, workload's drivers for containers and VMs.
The behaviour is fixed by the fixtures under testdata/v1, which every implementation runs. Where this package and a fixture disagree, the fixture is right.
Index ¶
- Constants
- Variables
- func Accept(accepted []Boundary, b Boundary) error
- func Admit(report Report, accept []Boundary, policy Policy) error
- func AwaitAdmission(input io.Reader, output io.Writer, report Report) error
- func Code(err error) string
- func Grant(request, policy Policy) (Policy, []Refusal, error)
- func Required(grant Policy) ([]string, error)
- func Sandboxed(r Report) bool
- type Access
- type AdmissionRequest
- type AdmissionResult
- type Boundary
- type Capabilities
- type Environment
- type Host
- type Lifecycle
- type Missing
- type Network
- type Outcome
- type Path
- type Policy
- type ProcessPolicy
- type Refusal
- type Report
- type Resources
- type Secret
- type Support
Constants ¶
const ( AdmissionContractName = "cto.admission" AdmissionContractVersion = "v1" // AdmissionRun runs one command under admission. AdmissionRun = "run" )
The contract a consumer calls to have CTO run an evaluator under admission: CTO starts the runner's AdmissionOperation, reads its report, decides with Admit and approves or refuses. The consumer states what it accepts and gets back the outcome; it never reads the report itself before execution. Products reach it only through Ext (docs/adr-sandboxing.md, section 9).
const ( MaxAdmissionArgs = 256 MaxAdmissionEnv = 1024 MaxAdmissionValueBytes = 32 << 10 MaxAdmissionTimeoutMs = 24 * 60 * 60 * 1000 DefaultAdmissionOutput = 4 << 10 MaxAdmissionOutputBytes = 1 << 20 )
Bounds of an AdmissionRequest.
const ( ReasonUnsupported = "unsupported" ReasonPermission = "permission" ReasonToolMissing = "tool_missing" )
The reasons a class is not enforced (docs/adr-architecture.md, rule 2).
const ( MaxPaths = 256 MaxPathBytes = 4096 MaxPorts = 256 MaxHosts = 256 MaxHostBytes = 253 MaxDevices = 64 MaxDeviceBytes = 128 MaxEnvNames = 256 MaxEnvBytes = 32768 MaxNameBytes = 256 MaxRefBytes = 1024 )
The bounds of a policy (docs/adr-architecture.md, rule 4). testdata/v1 states the same numbers under "limits".
const ( // NetworkNone reaches nothing. NetworkNone = "none" // NetworkPorts reaches the listed TCP ports on any address, where a boundary can enforce // ports without hostnames. NetworkPorts = "ports" // NetworkProxy reaches only an egress proxy that allows the listed hosts. NetworkProxy = "proxy" // NetworkOpen reaches anything. A grant with an open network is never sandboxed. NetworkOpen = "open" )
The network modes. An empty mode is None.
const ( // LifecycleFresh starts from new state, deleted afterwards. LifecycleFresh = "fresh" // LifecycleWorkspace keeps a named directory or volume across runs. LifecycleWorkspace = "workspace" // LifecycleSnapshot restores a named snapshot before the run. LifecycleSnapshot = "snapshot" )
The lifecycle modes. An empty mode is Fresh.
const ( // RestoreFilesystem restores files only. RestoreFilesystem = "filesystem" // RestoreMemory restores memory and device state as well, as a VM snapshot does. RestoreMemory = "memory" )
The scopes a snapshot restores.
const AdmissionApproval = "run\n"
AdmissionApproval is the complete approval the consumer writes to the runner.
const AdmissionOperation = "run-verified"
AdmissionOperation is the evaluator adapter operation that gates execution on the report from the prepared runner.
Variables ¶
var ( // ErrInvalid reports a malformed boundary, policy, capability set, class or report. ErrInvalid = errors.New("sandbox: invalid") // ErrDenied reports a boundary outside the set the consumer accepts. ErrDenied = errors.New("sandbox: boundary not accepted") // ErrUnsupported reports a grant that needs a class the runner cannot enforce and that the // caller did not accept unenforced. ErrUnsupported = errors.New("sandbox: class not enforced") )
The errors of the contract. Code maps each to its stable code. Error text never carries a value it was given.
Boundaries lists every level, in the order of docs/adr-sandboxing.md.
var Classes = []string{
"filesystem",
"network.none",
"network.ports",
"network.proxy",
"network.open",
"process",
"devices",
"environment",
"resources.memoryBytes",
"resources.milliCpu",
"resources.pids",
"resources.cpuTimeMs",
"resources.wallTimeMs",
"resources.openFiles",
"resources.writeBytes",
"lifecycle.fresh",
"lifecycle.workspace",
"lifecycle.snapshot.filesystem",
"lifecycle.snapshot.memory",
}
The classes a runner reports it can enforce. A grant needs a fixed set of them (Required): denying the filesystem, process creation and devices, and scrubbing the environment, always; the class of its network mode; the class of its lifecycle; and one class per resource bound.
Functions ¶
func Accept ¶
Accept checks b against the set a consumer accepts. An empty set accepts every level. It returns ErrInvalid when b or a member of the set is not a level, and ErrDenied when b is not in the set.
func Admit ¶
Admit decides whether a prepared runner's report meets a consumer's requirements. The report must be valid (ErrInvalid), its boundary in accept (ErrDenied), sandboxed (ErrDenied), and its grant within policy: granting the report's grant as a request against policy refuses nothing (ErrDenied). Resource bounds in the grant only take access away and never fail it.
func AwaitAdmission ¶
AwaitAdmission sends the prepared runner's report on output and waits for an explicit approval on input. Only a nil result permits guest execution.
func Code ¶
Code returns the stable code of err: "invalid", "denied" or "unsupported", or "" for nil or an error that is not one of this package's.
func Grant ¶
Grant returns the overlap of a request (what a package needs) and a policy (what a consumer allows), and the parts of the request that were refused. A request never grants anything by itself: everything in the grant is allowed by the policy. To grant a policy as it stands, pass it as the request too.
The rules, per class (testdata/v1 "grant"):
- filesystem: each requested path gets the requested access that some policy path at or above it allows; the rest is refused, and a path with no access left is dropped;
- network: a requested mode is granted when the policy has the same mode (ports and hosts are intersected) or an open network; anything else is refused and the grant reaches nothing;
- process: children only when both allow them;
- devices: the requested devices the policy lists;
- resources: the lower of the two where both set a bound, else whichever is set; a request above the policy's bound is refused and the policy's bound is granted;
- environment: the entries and secrets the policy supplies for the requested names (a request's names, and the names of its own entries and secrets, whose values are never used); a name the policy does not supply is refused;
- lifecycle: the policy's, which must name its workspace or snapshot; a requested mode or restore scope that differs is refused.
The grant states the network and lifecycle modes explicitly. Grant returns ErrInvalid when either input fails Validate, or when the policy's workspace or snapshot has no name.
func Required ¶
Required returns the classes a grant needs enforced, in the order of Classes. It returns ErrInvalid when grant fails Validate.
Types ¶
type AdmissionRequest ¶
type AdmissionRequest struct {
Path string `json:"path"`
Args []string `json:"args,omitempty"`
Env []string `json:"env,omitempty"`
Dir string `json:"dir,omitempty"`
Accept []Boundary `json:"accept"`
Policy Policy `json:"policy"`
// TimeoutMs bounds the whole run, preparation and admission included.
TimeoutMs int64 `json:"timeoutMs"`
// MaxOutputBytes bounds what is kept of each of stdout and stderr; zero uses
// DefaultAdmissionOutput. The rest is drained and dropped.
MaxOutputBytes int `json:"maxOutputBytes,omitempty"`
}
AdmissionRequest asks CTO to run a runner's AdmissionOperation. Path, Args, Env and Dir are the command the consumer has already selected and trusted; CTO adds nothing to it. Accept is the set of boundaries the consumer accepts (at least one), and Policy is what the consumer allows: the runner's grant must stay within it.
func (AdmissionRequest) Validate ¶
func (r AdmissionRequest) Validate() error
Validate checks the request's shape and bounds.
type AdmissionResult ¶
type AdmissionResult struct {
Report Report `json:"report"`
ExitCode int `json:"exitCode"`
// Stdout and Stderr are the kept prefixes of the command's output after approval.
Stdout []byte `json:"stdout,omitempty"`
Stderr []byte `json:"stderr,omitempty"`
// TimedOut is set when TimeoutMs ended the run after approval; ExitCode is then -1.
TimedOut bool `json:"timedOut,omitempty"`
}
AdmissionResult is the outcome of an admitted run: the report CTO admitted, and what the command did after approval. A refusal is an error instead, with the code of Admit's error.
type Boundary ¶
type Boundary string
Boundary is where isolation is enforced. The levels are not a ranking: each moves the attack surface somewhere else. A consumer states the set it accepts.
const ( // InProcess shares the host's address space and authority. InProcess Boundary = "in-process" // Process is a separate process with the user's full authority. Process Boundary = "process" // Confined is a separate process on the host kernel, restricted by OS policy. Confined Boundary = "confined" // Runtime has no syscall ABI; only the imports the host supplies. Runtime Boundary = "runtime" // Container is a process in namespaces on the host kernel, managed by a container runtime. Container Boundary = "container" // Kernel has its syscalls served by a separate kernel: a userspace kernel or a guest kernel // behind hardware virtualization. Kernel Boundary = "kernel" )
The boundary levels.
type Capabilities ¶
type Capabilities struct {
Boundary Boundary `json:"boundary"`
Driver string `json:"driver,omitempty"`
Classes map[string]Support `json:"classes,omitempty"`
}
Capabilities is what a runner (one driver on this host) can enforce. A class that is not listed is not enforced, for the reason "unsupported".
func (Capabilities) Validate ¶
func (c Capabilities) Validate() error
Validate checks that the boundary is a level, every class is known, an enforced class has no reason and a class that is not enforced has one of the reasons.
type Environment ¶
type Environment struct {
Names []string `json:"names,omitempty"`
Entries []string `json:"entries,omitempty"`
Secrets []Secret `json:"secrets,omitempty"`
}
Environment is the environment beyond the runner's scrubbed base. A request lists the Names it needs; a policy supplies Entries ("KEY=value") and Secrets.
type Host ¶
Host is one destination an egress proxy allows: an exact lowercase hostname or address and a TCP port.
type Lifecycle ¶
type Lifecycle struct {
Mode string `json:"mode,omitempty"`
Name string `json:"name,omitempty"`
Restore string `json:"restore,omitempty"`
}
Lifecycle is what survives between runs. Only a policy names the workspace or snapshot; the host chooses where state lives.
type Network ¶
type Network struct {
Mode string `json:"mode,omitempty"`
Ports []int `json:"ports,omitempty"`
Hosts []Host `json:"hosts,omitempty"`
}
Network is what the code can reach.
type Outcome ¶
type Outcome struct {
Enforced []string `json:"enforced"`
Missing []Missing `json:"missing,omitempty"`
Unenforced []string `json:"unenforced,omitempty"`
}
Outcome is what Enforce decided: the required classes the runner enforces, the ones it does not, and of those, the ones the caller accepted unenforced.
func Enforce ¶
func Enforce(grant Policy, caps Capabilities, accept []string) (Outcome, error)
Enforce checks a grant against a runner's capabilities. Every class the grant needs must be enforced, or be named in accept, the classes the caller agrees to run without. It fails closed: when a required class is neither, it returns ErrUnsupported with the outcome, whose Missing lists every such class, and the launch must not happen. Classes in accept that the grant does not need are ignored. Enforce returns ErrInvalid for an invalid grant, capability set or unknown class in accept.
type Path ¶
type Path struct {
// Path is absolute and slash-separated: "/" and its descendants, or a drive such as "C:/"
// and its descendants. It has no empty, "." or ".." component and no trailing slash except
// at a root.
Path string `json:"path"`
Access []Access `json:"access"`
}
Path grants or requests access to one absolute path and everything below it.
type Policy ¶
type Policy struct {
Filesystem []Path `json:"filesystem,omitempty"`
Network Network `json:"network,omitzero"`
Process ProcessPolicy `json:"process,omitzero"`
Devices []string `json:"devices,omitempty"`
Resources Resources `json:"resources,omitzero"`
Environment Environment `json:"environment,omitzero"`
Lifecycle Lifecycle `json:"lifecycle,omitzero"`
}
Policy is what code can touch inside a boundary. The same shape serves as a request (what a package needs), a policy (what a consumer allows) and a grant (their overlap). Everything not listed is denied.
type ProcessPolicy ¶
type ProcessPolicy struct {
Children bool `json:"children,omitempty"`
}
ProcessPolicy says whether the code may start other processes. No new privileges is always enforced and is not a field.
type Refusal ¶
type Refusal struct {
Class string `json:"class"`
Path string `json:"path,omitempty"`
Access []Access `json:"access,omitempty"`
Host *Host `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Name string `json:"name,omitempty"`
}
Refusal is one part of a request that the policy did not allow. Class names the policy class (see Classes); the other fields say which item, where the class has items.
type Report ¶
type Report struct {
Boundary Boundary `json:"boundary"`
Driver string `json:"driver,omitempty"`
Grant Policy `json:"grant"`
Refused []Refusal `json:"refused,omitempty"`
Enforced []string `json:"enforced"`
Unenforced []string `json:"unenforced,omitempty"`
}
Report is what every launch returns and what a consumer records: where it ran, what was requested, granted and refused, and what was actually enforced.
func Plan ¶
func Plan(request, policy Policy, accepted []Boundary, caps Capabilities, acceptUnenforced []string) (Report, error)
Plan is the whole decision before a launch: accept the boundary, compute the grant and check it against the runner. It returns the report to hand to the consumer, or the first error: ErrInvalid, ErrDenied for a boundary outside accepted, or ErrUnsupported (with the report's grant and refusals filled in) for a class the runner cannot enforce and the caller did not accept.
func ReadAdmissionReport ¶
ReadAdmissionReport reads one bounded JSON line. Use the same buffered reader for subsequent output from the runner. An empty stream returns io.EOF.
type Resources ¶
type Resources struct {
MemoryBytes *int64 `json:"memoryBytes,omitempty"`
MilliCPU *int64 `json:"milliCpu,omitempty"`
Pids *int64 `json:"pids,omitempty"`
CPUTimeMs *int64 `json:"cpuTimeMs,omitempty"`
WallTimeMs *int64 `json:"wallTimeMs,omitempty"`
OpenFiles *int64 `json:"openFiles,omitempty"`
WriteBytes *int64 `json:"writeBytes,omitempty"`
}
Resources are bounds. A nil field sets no bound; an explicit zero or negative value is invalid.