package sandbox import ( "crypto/sha256" "encoding/hex" "fmt" "io" "os" ) // Profile describes the isolation applied to every run. Reports record it // next to their results; it holds no secret. type Profile struct { NsjailPath string `json:"nsjail_path"` NsjailSHA256 string `json:"nsjail_sha256"` Arguments []string `json:"nsjail_arguments"` SeccompPolicy string `json:"seccomp_policy"` CPUQuota string `json:"cgroup_cpu_max"` MaxProcesses int `json:"max_processes"` MemoryOverheadBytes int64 `json:"memory_overhead_bytes"` TmpfsBytes int64 `json:"tmpfs_bytes"` FileSizeBytes int64 `json:"file_size_bytes"` OpenFiles int `json:"open_files"` SandboxUID int `json:"sandbox_uid"` ReadOnlyMounts []string `json:"read_only_mounts"` } // Placeholders of the sample command line in a Profile. const ( profileBox = "" profileProgram = "" profilePolicy = "" ) // Profile returns the isolation profile with the nsjail command line of a // sample test run under limits. func (e *Executor) Profile(limits Limits) (Profile, error) { digest, err := fileSHA256(e.cfg.NsjailPath) if err != nil { return Profile{}, err } sample := Spec{ Argv: []string{profileProgram}, Mounts: []Mount{ {Source: profileBox + "/src", Target: BoxPath}, {Source: profileBox + "/out", Target: OutPath}, }, } return Profile{ NsjailPath: e.cfg.NsjailPath, NsjailSHA256: digest, Arguments: nsjailArgs(e.cfg, sample, limits, profilePolicy), SeccompPolicy: e.policy, CPUQuota: cpuQuota, MaxProcesses: e.cfg.MaxProcesses, MemoryOverheadBytes: e.cfg.MemoryOverheadBytes, TmpfsBytes: e.cfg.TmpfsBytes, FileSizeBytes: e.cfg.FileSizeBytes, OpenFiles: e.cfg.OpenFiles, SandboxUID: e.cfg.SandboxUID, ReadOnlyMounts: e.cfg.ReadOnlyMounts, }, nil } func fileSHA256(path string) (string, error) { file, err := os.Open(path) if err != nil { return "", fmt.Errorf("sandbox.fileSHA256 - open: %w", err) } defer func() { _ = file.Close() }() // Read-only. hash := sha256.New() if _, err := io.Copy(hash, file); err != nil { return "", fmt.Errorf("sandbox.fileSHA256 - read: %w", err) } return hex.EncodeToString(hash.Sum(nil)), nil }