feat(runner): compile Go and check answers per test group
This commit is contained in:
1 parent
e95c681165
commit
afee564bf4
61 files changed
+2592
-143
No files matched your search
@@ -75,9 +75,10 @@ func validateConfig(cfg Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewBox creates the read-only file directory of a run.
|
||||
// NewBox creates the directory of one program; the sandbox user owns its
|
||||
// build output.
|
||||
func (e *Executor) NewBox(fileName string, content []byte) (*Box, error) {
|
||||
return CreateBox(e.cfg.WorkRoot, fileName, content)
|
||||
return CreateBox(e.cfg.WorkRoot, fileName, content, Owner{UID: e.cfg.SandboxUID, GID: e.cfg.SandboxGID})
|
||||
}
|
||||
|
||||
// Leftovers lists boxes and run cgroups that still exist. After every run
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"math"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
@@ -34,7 +35,7 @@ func nsjailArgs(cfg Config, spec Spec, limits Limits, seccompPolicy string) []st
|
||||
"--mode", "o",
|
||||
"--log_fd", strconv.Itoa(nsjailLogFD),
|
||||
"--hostname", sandboxHostname,
|
||||
"--cwd", ScratchPath,
|
||||
"--cwd", workDir(spec),
|
||||
// Inside and outside the user namespace the program is the same
|
||||
// unprivileged ID, never root.
|
||||
"--user", idMapping(cfg.SandboxUID),
|
||||
@@ -42,7 +43,7 @@ func nsjailArgs(cfg Config, spec Spec, limits Limits, seccompPolicy string) []st
|
||||
"--time_limit", strconv.Itoa(ceilSeconds(limits.WallTime + nsjailTimeBackstop)),
|
||||
"--rlimit_as", strconv.Itoa(addressSpaceLimitMB),
|
||||
"--rlimit_cpu", strconv.Itoa(ceilSeconds(limits.CPUTime + cpuRlimitBackstop)),
|
||||
"--rlimit_fsize", strconv.Itoa(ceilMB(cfg.FileSizeBytes)),
|
||||
"--rlimit_fsize", strconv.Itoa(ceilMB(fileSizeLimit(cfg, limits))),
|
||||
"--rlimit_nofile", strconv.Itoa(cfg.OpenFiles),
|
||||
"--rlimit_core", "0",
|
||||
"--disable_proc",
|
||||
@@ -54,19 +55,39 @@ func nsjailArgs(cfg Config, spec Spec, limits Limits, seccompPolicy string) []st
|
||||
for _, mount := range cfg.ReadOnlyMounts {
|
||||
args = append(args, "--bindmount_ro", mount)
|
||||
}
|
||||
for _, mount := range spec.Mounts {
|
||||
flag := "--bindmount_ro"
|
||||
if mount.Writable {
|
||||
flag = "--bindmount"
|
||||
}
|
||||
args = append(args, flag, mount.Source+":"+mount.Target)
|
||||
}
|
||||
args = append(args,
|
||||
"--bindmount_ro", spec.BoxDir+":"+BoxPath,
|
||||
"--bindmount", "/dev/null",
|
||||
"--bindmount_ro", "/dev/urandom",
|
||||
"--mount", "none:"+ScratchPath+":tmpfs:size="+strconv.FormatInt(cfg.TmpfsBytes, 10),
|
||||
)
|
||||
for _, variable := range cfg.Env {
|
||||
for _, variable := range append(slices.Clone(cfg.Env), spec.Env...) {
|
||||
args = append(args, "--env", variable)
|
||||
}
|
||||
args = append(args, "--")
|
||||
return append(args, spec.Argv...)
|
||||
}
|
||||
|
||||
func workDir(spec Spec) string {
|
||||
if spec.WorkDir != "" {
|
||||
return spec.WorkDir
|
||||
}
|
||||
return ScratchPath
|
||||
}
|
||||
|
||||
func fileSizeLimit(cfg Config, limits Limits) int64 {
|
||||
if limits.FileSizeBytes > 0 {
|
||||
return limits.FileSizeBytes
|
||||
}
|
||||
return cfg.FileSizeBytes
|
||||
}
|
||||
|
||||
func idMapping(id int) string {
|
||||
value := strconv.Itoa(id)
|
||||
return value + ":" + value + ":1"
|
||||
|
||||
@@ -35,9 +35,14 @@ const (
|
||||
|
||||
// Jail paths seen by the jailed program.
|
||||
const (
|
||||
// BoxPath is the read-only directory with the files of a run.
|
||||
// BoxPath is the read-only directory with the source of a program.
|
||||
BoxPath = "/box"
|
||||
// ScratchPath is the writable tmpfs and the working directory.
|
||||
// OutPath holds build output: writable for compile steps, read-only
|
||||
// for test runs.
|
||||
OutPath = "/out"
|
||||
// CheckPath holds the files a custom checker compares, read-only.
|
||||
CheckPath = "/check"
|
||||
// ScratchPath is the writable tmpfs and the default working directory.
|
||||
ScratchPath = "/tmp"
|
||||
)
|
||||
|
||||
@@ -50,6 +55,16 @@ type Limits struct {
|
||||
WallTime time.Duration
|
||||
MemoryBytes int64
|
||||
OutputBytes int64
|
||||
// FileSizeBytes overrides Config.FileSizeBytes when positive, for
|
||||
// compile steps that write binaries.
|
||||
FileSizeBytes int64
|
||||
}
|
||||
|
||||
// Mount is a host directory bound into the jail.
|
||||
type Mount struct {
|
||||
Source string
|
||||
Target string
|
||||
Writable bool
|
||||
}
|
||||
|
||||
// Spec is one command to run in the jail.
|
||||
@@ -58,8 +73,12 @@ type Spec struct {
|
||||
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
|
||||
// Mounts are the run's directories, for example a Box.
|
||||
Mounts []Mount
|
||||
// Env is added to Config.Env.
|
||||
Env []string
|
||||
// WorkDir is the working directory in the jail; empty means ScratchPath.
|
||||
WorkDir string
|
||||
}
|
||||
|
||||
// Result describes a finished run. It never contains host paths.
|
||||
|
||||
@@ -39,7 +39,11 @@ func flagValue(t *testing.T, args []string, flag string) string {
|
||||
func TestNsjailArgsKeepEveryIsolationSwitch(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
spec := Spec{Argv: []string{"/usr/local/bin/python3", "-I", "/box/main.py"}, BoxDir: "/work/run-1"}
|
||||
spec := Spec{
|
||||
Argv: []string{"/usr/local/bin/python3", "-I", "/box/main.py"},
|
||||
Mounts: []Mount{{Source: "/work/run-1/src", Target: BoxPath}, {Source: "/work/run-1/out", Target: OutPath}},
|
||||
Env: []string{"GOMAXPROCS=1"},
|
||||
}
|
||||
limits := Limits{CPUTime: 1500 * time.Millisecond, WallTime: 3500 * time.Millisecond, MemoryBytes: 64 << 20, OutputBytes: 1 << 16}
|
||||
args := nsjailArgs(testConfig(), spec, limits, "POLICY")
|
||||
|
||||
@@ -74,17 +78,32 @@ func TestNsjailArgsKeepEveryIsolationSwitch(t *testing.T) {
|
||||
t.Errorf("args miss %s", required)
|
||||
}
|
||||
}
|
||||
if !slices.Contains(args, "/work/run-1:/box") {
|
||||
t.Errorf("the box is not mounted at /box: %q", args)
|
||||
if !slices.Contains(args, "/work/run-1/src:/box") || !slices.Contains(args, "/work/run-1/out:/out") {
|
||||
t.Errorf("the box is not mounted read-only: %q", args)
|
||||
}
|
||||
if !slices.Contains(args, "GOMAXPROCS=1") || !slices.Contains(args, "LANG=C.UTF-8") {
|
||||
t.Errorf("args miss the environment: %q", args)
|
||||
}
|
||||
|
||||
compile := spec
|
||||
compile.WorkDir = BoxPath
|
||||
compile.Mounts = []Mount{{Source: "/work/run-1/out", Target: OutPath, Writable: true}}
|
||||
compileArgs := nsjailArgs(testConfig(), compile, Limits{CPUTime: time.Second, WallTime: time.Second, FileSizeBytes: 64 << 20}, "POLICY")
|
||||
if got := flagValue(t, compileArgs, "--cwd"); got != BoxPath {
|
||||
t.Errorf("compile --cwd = %q", got)
|
||||
}
|
||||
if got := flagValue(t, compileArgs, "--rlimit_fsize"); got != "64" {
|
||||
t.Errorf("compile --rlimit_fsize = %q", got)
|
||||
}
|
||||
if got := writableMounts(compileArgs); !slices.Equal(got, []string{"/work/run-1/out:/out", "/dev/null"}) {
|
||||
t.Errorf("compile writable mounts = %q", got)
|
||||
}
|
||||
separator := slices.Index(args, "--")
|
||||
if separator < 0 || !slices.Equal(args[separator+1:], spec.Argv) {
|
||||
t.Errorf("command after -- = %q, want %q", args[separator+1:], spec.Argv)
|
||||
}
|
||||
for _, writable := range writableMounts(args) {
|
||||
if writable != "/dev/null" {
|
||||
t.Errorf("unexpected writable bind mount %q", writable)
|
||||
}
|
||||
if got := writableMounts(args); !slices.Equal(got, []string{"/dev/null"}) {
|
||||
t.Errorf("unexpected writable bind mounts %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,13 +209,27 @@ func TestBoxIsReadOnlyAndRemovable(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
root := t.TempDir()
|
||||
box, err := CreateBox(root, "main.py", []byte("print(1)"))
|
||||
box, err := CreateBox(root, "main.py", []byte("print(1)"), Owner{UID: -1, GID: -1})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateBox: %v", err)
|
||||
}
|
||||
info, err := os.Stat(filepath.Join(box.Dir(), "main.py"))
|
||||
info, err := os.Stat(filepath.Join(box.Dir(), "src", "main.py"))
|
||||
if err != nil || info.Mode().Perm() != boxFileMode {
|
||||
t.Fatalf("source file mode = %v, %v", info.Mode(), err)
|
||||
t.Fatalf("source file mode = %v, %v", info, err)
|
||||
}
|
||||
mounts := box.Mounts(false)
|
||||
if mounts[0].Target != BoxPath || mounts[0].Writable || mounts[1].Target != OutPath || mounts[1].Writable {
|
||||
t.Fatalf("mounts = %+v", mounts)
|
||||
}
|
||||
check, remove, err := box.AddCheckFiles(map[string][]byte{"actual": []byte("1")})
|
||||
if err != nil || check.Target != CheckPath || check.Writable {
|
||||
t.Fatalf("AddCheckFiles = %+v, %v", check, err)
|
||||
}
|
||||
if err := remove(); err != nil {
|
||||
t.Fatalf("remove check files: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(check.Source); !os.IsNotExist(err) {
|
||||
t.Fatalf("check files remain: %v", err)
|
||||
}
|
||||
if err := box.Remove(); err != nil {
|
||||
t.Fatalf("Remove: %v", err)
|
||||
@@ -205,7 +238,7 @@ func TestBoxIsReadOnlyAndRemovable(t *testing.T) {
|
||||
t.Fatalf("root after Remove = %v, %v", entries, err)
|
||||
}
|
||||
for _, name := range []string{"", "../main.py", ".hidden", "dir/main.py"} {
|
||||
if _, err := CreateBox(root, name, nil); err == nil {
|
||||
if _, err := CreateBox(root, name, nil, Owner{UID: -1, GID: -1}); err == nil {
|
||||
t.Errorf("CreateBox accepted file name %q", name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package sandbox
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -20,47 +21,143 @@ const (
|
||||
boxPrefix = "run-"
|
||||
)
|
||||
|
||||
// Box is the host directory with the files of one run. It is mounted
|
||||
// read-only into every jail of the run and removed with Remove.
|
||||
// Box is the host directory of one program: its source, mounted read-only
|
||||
// at BoxPath, and its build output, mounted at OutPath, writable only for
|
||||
// compile steps. Remove deletes it.
|
||||
type Box struct {
|
||||
dir string
|
||||
root string
|
||||
}
|
||||
|
||||
// Owner is the user that may write a box's build output. A negative ID
|
||||
// leaves the owner unchanged, which unit tests use.
|
||||
type Owner struct {
|
||||
UID int
|
||||
GID int
|
||||
}
|
||||
|
||||
const (
|
||||
boxSourceDir = "src"
|
||||
boxOutputDir = "out"
|
||||
checkPrefix = "check-"
|
||||
)
|
||||
|
||||
// Dir returns the host path of the box.
|
||||
func (b *Box) Dir() string {
|
||||
return b.dir
|
||||
return b.root
|
||||
}
|
||||
|
||||
// CreateBox creates a box under root with one file.
|
||||
func CreateBox(root, fileName string, content []byte) (*Box, error) {
|
||||
if fileName == "" || fileName != filepath.Base(fileName) || strings.HasPrefix(fileName, ".") {
|
||||
// CreateBox creates a box under root with one source file.
|
||||
func CreateBox(root, fileName string, content []byte, owner Owner) (*Box, error) {
|
||||
if !validFileName(fileName) {
|
||||
return nil, fmt.Errorf("sandbox.CreateBox - invalid file name %q", fileName)
|
||||
}
|
||||
dir, err := os.MkdirTemp(root, boxPrefix)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sandbox.CreateBox - create: %w", err)
|
||||
}
|
||||
box := &Box{dir: dir}
|
||||
if err := os.WriteFile(filepath.Join(dir, fileName), content, boxFileMode); err != nil {
|
||||
return nil, errors.Join(fmt.Errorf("sandbox.CreateBox - write: %w", err), box.Remove())
|
||||
}
|
||||
if err := os.Chmod(dir, boxDirMode); err != nil {
|
||||
return nil, errors.Join(fmt.Errorf("sandbox.CreateBox - chmod: %w", err), box.Remove())
|
||||
box := &Box{root: dir}
|
||||
if err := box.populate(fileName, content, owner); err != nil {
|
||||
return nil, errors.Join(err, box.Remove())
|
||||
}
|
||||
return box, nil
|
||||
}
|
||||
|
||||
// Remove deletes the box and its files.
|
||||
func (b *Box) Remove() error {
|
||||
if err := os.Chmod(b.dir, ownerOnlyMode); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("sandbox.Box.Remove - chmod: %w", err)
|
||||
func (b *Box) populate(fileName string, content []byte, owner Owner) error {
|
||||
source := filepath.Join(b.root, boxSourceDir)
|
||||
if err := writeReadOnlyDir(source, map[string][]byte{fileName: content}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.RemoveAll(b.dir); err != nil {
|
||||
return fmt.Errorf("sandbox.Box.Remove - remove: %w", err)
|
||||
output := filepath.Join(b.root, boxOutputDir)
|
||||
if err := os.Mkdir(output, ownerOnlyMode); err != nil {
|
||||
return fmt.Errorf("sandbox.Box.populate - create output: %w", err)
|
||||
}
|
||||
if err := os.Chown(output, owner.UID, owner.GID); err != nil {
|
||||
return fmt.Errorf("sandbox.Box.populate - chown output: %w", err)
|
||||
}
|
||||
if err := os.Chmod(b.root, workRootMode); err != nil {
|
||||
return fmt.Errorf("sandbox.Box.populate - chmod: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Mounts returns the source and output mounts of the box.
|
||||
func (b *Box) Mounts(outputWritable bool) []Mount {
|
||||
return []Mount{
|
||||
{Source: filepath.Join(b.root, boxSourceDir), Target: BoxPath},
|
||||
{Source: filepath.Join(b.root, boxOutputDir), Target: OutPath, Writable: outputWritable},
|
||||
}
|
||||
}
|
||||
|
||||
// AddCheckFiles writes read-only files for one custom checker call and
|
||||
// returns their mount at CheckPath and a function that deletes them.
|
||||
func (b *Box) AddCheckFiles(files map[string][]byte) (Mount, func() error, error) {
|
||||
for name := range files {
|
||||
if !validFileName(name) {
|
||||
return Mount{}, nil, fmt.Errorf("sandbox.Box.AddCheckFiles - invalid file name %q", name)
|
||||
}
|
||||
}
|
||||
dir, err := os.MkdirTemp(b.root, checkPrefix)
|
||||
if err != nil {
|
||||
return Mount{}, nil, fmt.Errorf("sandbox.Box.AddCheckFiles - create: %w", err)
|
||||
}
|
||||
remove := func() error { return removeTree(dir) }
|
||||
if err := fillReadOnlyDir(dir, files); err != nil {
|
||||
return Mount{}, nil, errors.Join(err, remove())
|
||||
}
|
||||
return Mount{Source: dir, Target: CheckPath}, remove, nil
|
||||
}
|
||||
|
||||
// Remove deletes the box and its files.
|
||||
func (b *Box) Remove() error {
|
||||
return removeTree(b.root)
|
||||
}
|
||||
|
||||
// writeReadOnlyDir creates dir with read-only files that the jailed user
|
||||
// can read but not change.
|
||||
func writeReadOnlyDir(dir string, files map[string][]byte) error {
|
||||
if err := os.Mkdir(dir, ownerOnlyMode); err != nil {
|
||||
return fmt.Errorf("sandbox.writeReadOnlyDir - create: %w", err)
|
||||
}
|
||||
return fillReadOnlyDir(dir, files)
|
||||
}
|
||||
|
||||
// fillReadOnlyDir writes files into an existing directory and makes both
|
||||
// read-only.
|
||||
func fillReadOnlyDir(dir string, files map[string][]byte) error {
|
||||
for name, content := range files {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), content, boxFileMode); err != nil {
|
||||
return fmt.Errorf("sandbox.fillReadOnlyDir - write: %w", err)
|
||||
}
|
||||
}
|
||||
if err := os.Chmod(dir, boxDirMode); err != nil {
|
||||
return fmt.Errorf("sandbox.fillReadOnlyDir - chmod: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// removeTree makes read-only directories writable again and removes path.
|
||||
// The sandbox user owns build output and may have left symlinks there, so
|
||||
// the walk goes through an os.Root, which never follows a link out of path.
|
||||
func removeTree(path string) error {
|
||||
if root, err := os.OpenRoot(path); err == nil {
|
||||
_ = fs.WalkDir(root.FS(), ".", func(name string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr == nil && entry.IsDir() {
|
||||
_ = root.Chmod(name, ownerOnlyMode) // RemoveAll reports what remains.
|
||||
}
|
||||
return nil
|
||||
})
|
||||
_ = root.Close() // Only the walk used it.
|
||||
}
|
||||
if err := os.RemoveAll(path); err != nil {
|
||||
return fmt.Errorf("sandbox.removeTree - remove: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validFileName(name string) bool {
|
||||
return name != "" && name == filepath.Base(name) && !strings.HasPrefix(name, ".")
|
||||
}
|
||||
|
||||
// emptyWorkRoot removes every entry of root, for example boxes of a process
|
||||
// that crashed, and creates root when it is missing.
|
||||
func emptyWorkRoot(root string) error {
|
||||
@@ -75,8 +172,7 @@ func emptyWorkRoot(root string) error {
|
||||
return fmt.Errorf("sandbox.emptyWorkRoot - list: %w", err)
|
||||
}
|
||||
for _, entry := range entries {
|
||||
box := &Box{dir: filepath.Join(root, entry.Name())}
|
||||
if err := box.Remove(); err != nil {
|
||||
if err := removeTree(filepath.Join(root, entry.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user