181 lines
5.6 KiB
Go
181 lines
5.6 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
// The jailed user only reads and traverses the box; it owns nothing.
|
|
boxDirMode = 0o555
|
|
boxFileMode = 0o444
|
|
// ownerOnlyMode lets the supervisor delete the box again.
|
|
ownerOnlyMode = 0o700
|
|
// workRootMode lets nsjail traverse to a box without listing the root:
|
|
// inside its user namespace host root's DAC override does not apply.
|
|
workRootMode = 0o711
|
|
boxPrefix = "run-"
|
|
)
|
|
|
|
// 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 {
|
|
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.root
|
|
}
|
|
|
|
// 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{root: dir}
|
|
if err := box.populate(fileName, content, owner); err != nil {
|
|
return nil, errors.Join(err, box.Remove())
|
|
}
|
|
return box, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
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 {
|
|
if err := os.MkdirAll(root, workRootMode); err != nil {
|
|
return fmt.Errorf("sandbox.emptyWorkRoot - create: %w", err)
|
|
}
|
|
if err := os.Chmod(root, workRootMode); err != nil {
|
|
return fmt.Errorf("sandbox.emptyWorkRoot - chmod: %w", err)
|
|
}
|
|
entries, err := os.ReadDir(root)
|
|
if err != nil {
|
|
return fmt.Errorf("sandbox.emptyWorkRoot - list: %w", err)
|
|
}
|
|
for _, entry := range entries {
|
|
if err := removeTree(filepath.Join(root, entry.Name())); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|