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
- Variables
- func CreateFile(fs FS, c File) error
- func SplitArgs(s string) []string
- type ChanProvider
- type DirectoryMap
- type DirectoryMapOption
- type Dummy
- type DummyOption
- type FS
- type File
- type Isolate
- type IsolateOption
- type OsFS
- type Provider
- type RunConfig
- type Sandbox
- type Status
- type Verdict
Constants ¶
const DummyPattern = "dummy_sandbox*"
DummyPattern is the pattern in which Dummy creates its temporary directory.
Variables ¶
var ErrorSandboxNotInitialized = errors.New("initialize the sandbox first")
ErrorSandboxNotInitialized should returned when a Run is called on a Sandbox without a prior Init call.
var IsolateMetafilePattern = "isolate_metafile*"
IsolateMetafilePattern is the pattern in which metafiles are created.
var IsolateRoot = "/var/local/lib/isolate/"
IsolateRoot is the root directory structure isolate is using.
Functions ¶
func CreateFile ¶ added in v0.4.0
CreateFile is a convenience method for creating a file inside a sandbox with the given content.
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)
type DummyOption ¶ added in v0.4.0
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.
type Isolate ¶
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)
type IsolateOption ¶ added in v0.4.0
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 (OsFS) MakeExecutable ¶ added in v0.4.0
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.