sandbox

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package sandbox declares the Sandbox interface which can be used to run untrusted code. Hopefully in a secure way. It has two implementations built-in:

  • Isolate: which uses isolate for sandboxing.
  • Dummy: which is used for testing and not really secure.

Index

Constants

View Source
const DummyPattern = "dummy_sandbox*"

DummyPattern is the pattern in which Dummy creates its temporary directory.

Variables

View Source
var ErrorSandboxNotInitialized = errors.New("initialize the sandbox first")

ErrorSandboxNotInitialized should returned when a Run is called on a Sandbox without a prior Init call.

View Source
var IsolateMetafilePattern = "isolate_metafile*"

IsolateMetafilePattern is the pattern in which metafiles are created.

View Source
var IsolateRoot = "/var/local/lib/isolate/"

IsolateRoot is the root directory structure isolate is using.

Functions

func CreateFile added in v0.4.0

func CreateFile(fs FS, c File) error

CreateFile is a convenience method for creating a file inside a sandbox with the given content.

func SplitArgs added in v0.4.0

func SplitArgs(s string) []string

SplitArgs is used to split a program's arguments for Sandbox.Run It just calls strings.Split, but it's useful for context.

Types

type ChanProvider added in v0.4.0

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

func NewProvider added in v0.4.0

func NewProvider() *ChanProvider

func (*ChanProvider) Get added in v0.4.0

func (sp *ChanProvider) Get() (Sandbox, error)

func (*ChanProvider) Put added in v0.4.0

func (sp *ChanProvider) Put(s Sandbox) Provider

type DirectoryMap added in v0.4.0

type DirectoryMap struct {
	Inside  string
	Outside string
	Options []DirectoryMapOption
}

DirectoryMap is used to map a directory from outside to the inside of a Sandbox.

type DirectoryMapOption added in v0.4.0

type DirectoryMapOption string

DirectoryMapOption is an option for a DirectoryMap. The values are defined as in isolate. You may refer to isolate's help for more information on them.

const (
	AllowSpecial   DirectoryMapOption = "dev"
	MountFS        DirectoryMapOption = "fs"
	Maybe          DirectoryMapOption = "maybe"
	NoExec         DirectoryMapOption = "noexec"
	AllowReadWrite DirectoryMapOption = "rw"
	Temporary      DirectoryMapOption = "tmp"
)

type Dummy

type Dummy struct {
	ID  int
	Dir string

	OsFS

	Logger *slog.Logger
	// contains filtered or unexported fields
}

Dummy is a very straightforward implementation of a Sandbox. It creates a temporary directory and executes the commands without many precautions.

func NewDummy

func NewDummy(opts ...DummyOption) (*Dummy, error)

func (*Dummy) Cleanup

func (d *Dummy) Cleanup(_ context.Context) error

func (*Dummy) Id

func (d *Dummy) Id() string

func (*Dummy) Init

func (d *Dummy) Init(_ context.Context) error

func (*Dummy) Run

func (d *Dummy) Run(ctx context.Context, config RunConfig, command string, commandArgs ...string) (*Status, error)

type DummyOption added in v0.4.0

type DummyOption func(*Dummy) error

func DummyWithLogger added in v0.4.0

func DummyWithLogger(logger *slog.Logger) DummyOption

type FS added in v0.4.0

type FS interface {
	Pwd() string
	Create(name string) (io.WriteCloser, error)
	MakeExecutable(name string) error

	fs.FS
}

FS is a file system abstraction for sandboxes.

type File added in v0.4.0

type File struct {
	Name   string
	Source io.ReadCloser
}

File is a named io.Reader which emulates a file.

func ExtractFile added in v0.4.0

func ExtractFile(s FS, name string) (*File, error)

ExtractFile is a convenience method for getting the content of a file from inside the sandbox.

type Isolate

type Isolate struct {
	ID int

	OsFS

	Logger *slog.Logger
	// contains filtered or unexported fields
}

Isolate is a Sandbox implementation which calls isolate's command line program. It's required that isolate is installed on the system for it to work. This is the preferred way of sandboxing in njudge.

func NewIsolate

func NewIsolate(ID int, opts ...IsolateOption) (*Isolate, error)

func (*Isolate) Cleanup

func (i *Isolate) Cleanup(_ context.Context) error

func (*Isolate) Id

func (i *Isolate) Id() string

func (*Isolate) Init

func (i *Isolate) Init(ctx context.Context) error

func (*Isolate) Run

func (i *Isolate) Run(_ context.Context, config RunConfig, toRun string, toRunArgs ...string) (*Status, error)

type IsolateOption added in v0.4.0

type IsolateOption func(*Isolate) error

func IsolateOptionUseLogger added in v0.4.0

func IsolateOptionUseLogger(logger *slog.Logger) IsolateOption

type OsFS added in v0.4.0

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

OsFS implements an FS with and underlying base path. All of its methods are executed as if the current working directory is this base path. Should be only created via NewOsFS

func NewOsFS added in v0.4.0

func NewOsFS(base string) OsFS

func (OsFS) Create added in v0.4.0

func (o OsFS) Create(name string) (io.WriteCloser, error)

func (OsFS) MakeExecutable added in v0.4.0

func (o OsFS) MakeExecutable(name string) error

func (OsFS) Open added in v0.4.0

func (o OsFS) Open(name string) (fs.File, error)

func (OsFS) Pwd added in v0.4.0

func (o OsFS) Pwd() string

type Provider added in v0.4.0

type Provider interface {
	Get() (Sandbox, error)
	Put(s Sandbox) Provider
}

Provider can be used to store Sandboxes

type RunConfig added in v0.4.0

type RunConfig struct {
	RunID string // RunID is some kind of ID of the run, doesn't need to be set but if set is used to give additional context.

	MaxProcesses     int
	InheritEnv       bool
	Env              []string
	WorkingDirectory string
	DirectoryMaps    []DirectoryMap

	TimeLimit   time.Duration
	MemoryLimit memory.Amount

	Stdin  io.Reader
	Stderr io.Writer
	Stdout io.Writer

	Args []string // Args are given to the underlying sandbox implementation as-is. This should only be used for things that are currently not supported in RunConfig.
}

RunConfig is used to configure the sandbox, setting limits and streams (stdin, stdout, stderr).

type Sandbox added in v0.4.0

type Sandbox interface {
	Id() string // Id should return an unique ID for sandboxes of the same type

	Init(ctx context.Context) error
	FS
	Run(ctx context.Context, config RunConfig, command string, commandArgs ...string) (*Status, error)
	Cleanup(ctx context.Context) error
}

Sandbox is used to Run a command inside a secure sandbox.

type Status added in v0.4.0

type Status struct {
	Verdict  Verdict
	Signal   int
	Time     time.Duration
	Memory   memory.Amount
	ExitCode int
}

Status contains information about how a program ran.

func RunBinary added in v0.4.0

func RunBinary(ctx context.Context, s Sandbox, binary File, stdin io.Reader, stdout io.Writer, tl time.Duration, ml memory.Amount) (*Status, error)

RunBinary is a convenience method for running a binary (which needs no special configuration) inside the given Sandbox.

type Verdict added in v0.4.0

type Verdict int

Verdict is the overall outcome of running a program.

const (
	VerdictOK Verdict = 1 << iota
	VerdictTL
	VerdictML
	VerdictRE
	VerdictXX
	VerdictCE
)

func (Verdict) String added in v0.4.0

func (v Verdict) String() string

Jump to

Keyboard shortcuts

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