104 lines
3.4 KiB
Go
104 lines
3.4 KiB
Go
// Package sandbox runs one untrusted command inside nsjail.
|
|
//
|
|
// Every run gets its own cgroup v2 directory with CPU, memory, swap, and
|
|
// process limits. The supervisor creates that cgroup itself and starts nsjail
|
|
// directly inside it (CLONE_INTO_CGROUP), so the limits cover nsjail and every
|
|
// jailed process, the supervisor can read exact OOM, process-limit, and CPU
|
|
// counters, and cgroup.kill removes the whole tree. nsjail adds the namespaces
|
|
// (no network, own PID, mount, IPC, UTS, user, and cgroup namespaces), the
|
|
// read-only file system with a tmpfs work directory, the unprivileged user,
|
|
// rlimits, and the seccomp policy.
|
|
package sandbox
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// Termination names the limit that stopped a run.
|
|
type Termination string
|
|
|
|
const (
|
|
// TerminationNone means that the program stopped by itself.
|
|
TerminationNone Termination = ""
|
|
// TerminationCPUTime means the CPU time limit.
|
|
TerminationCPUTime Termination = "cpu_time"
|
|
// TerminationWallTime means the wall-clock limit.
|
|
TerminationWallTime Termination = "wall_time"
|
|
// TerminationMemory means the cgroup memory limit.
|
|
TerminationMemory Termination = "memory"
|
|
// TerminationOutput means the standard output limit.
|
|
TerminationOutput Termination = "output"
|
|
// TerminationProcesses means the cgroup process limit.
|
|
TerminationProcesses Termination = "processes"
|
|
)
|
|
|
|
// Jail paths seen by the jailed program.
|
|
const (
|
|
// BoxPath is the read-only directory with the files of a run.
|
|
BoxPath = "/box"
|
|
// ScratchPath is the writable tmpfs and the working directory.
|
|
ScratchPath = "/tmp"
|
|
)
|
|
|
|
// ErrUnsupportedPlatform reports that the sandbox needs Linux.
|
|
var ErrUnsupportedPlatform = errors.New("sandbox requires Linux with cgroup v2")
|
|
|
|
// Limits bound one run.
|
|
type Limits struct {
|
|
CPUTime time.Duration
|
|
WallTime time.Duration
|
|
MemoryBytes int64
|
|
OutputBytes int64
|
|
}
|
|
|
|
// Spec is one command to run in the jail.
|
|
type Spec struct {
|
|
// Argv is the command inside the jail; Argv[0] is an absolute path.
|
|
Argv []string
|
|
// Stdin is written to the standard input of the command.
|
|
Stdin []byte
|
|
// BoxDir is the host directory mounted read-only at BoxPath.
|
|
BoxDir string
|
|
}
|
|
|
|
// Result describes a finished run. It never contains host paths.
|
|
type Result struct {
|
|
Stdout []byte
|
|
// Stderr holds at most Config.StderrCaptureBytes of standard error.
|
|
Stderr []byte
|
|
// ExitCode is the exit status of the command, or -1 when a signal stopped it.
|
|
ExitCode int
|
|
// Signal is the signal that stopped the command, or 0.
|
|
Signal int
|
|
CPUTime time.Duration
|
|
WallTime time.Duration
|
|
PeakMemoryKB int64
|
|
Termination Termination
|
|
}
|
|
|
|
// Config holds the host-side sandbox settings shared by every run.
|
|
type Config struct {
|
|
NsjailPath string
|
|
// CgroupRoot is a cgroup v2 directory delegated to the runner.
|
|
CgroupRoot string
|
|
// WorkRoot holds one directory per run; it is emptied at startup.
|
|
WorkRoot string
|
|
SandboxUID int
|
|
SandboxGID int
|
|
// MaxProcesses bounds processes and threads of a run, nsjail excluded.
|
|
MaxProcesses int
|
|
// MemoryOverheadBytes is added to the memory limit for nsjail itself.
|
|
MemoryOverheadBytes int64
|
|
TmpfsBytes int64
|
|
FileSizeBytes int64
|
|
OpenFiles int
|
|
StderrCaptureBytes int
|
|
// ReadOnlyMounts are host paths mounted read-only at the same place.
|
|
ReadOnlyMounts []string
|
|
// Env is the complete environment of the jailed command.
|
|
Env []string
|
|
// PollInterval is how often the supervisor checks the cgroup counters.
|
|
PollInterval time.Duration
|
|
}
|