test(runner): verify sandbox isolation with a hostile program suite

This commit is contained in:
shira committed 2026-10-11 08:59:59 +03:00
1 parent afee564bf4
commit e858280d91
38 files changed
+1359 -168

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
// Disk exhaustion: the file size limit and the scratch tmpfs refuse writes.
package main
import (
"fmt"
"os"
)
func main() {
block := make([]byte, 1<<20)
refused := 0
if file, err := os.Create("/tmp/one-big-file"); err == nil {
for range 64 {
if _, err := file.Write(block); err != nil {
refused++
break
}
}
_ = file.Close()
_ = os.Remove("/tmp/one-big-file")
}
for index := range 64 {
file, err := os.Create(fmt.Sprintf("/tmp/part-%d", index))
if err != nil {
refused++
break
}
failed := false
for range 4 {
if _, err := file.Write(block); err != nil {
failed = true
break
}
}
_ = file.Close()
if failed {
refused++
break
}
}
if refused == 2 {
fmt.Println("refused")
return
}
fmt.Println("wrote everything")
}
+43
View File
@@ -0,0 +1,43 @@
// Isolation probe: read-only root, source, and build output; a writable
// scratch directory only; no host, runner, secret, or process files; no
// secret in the environment.
package main
import (
"fmt"
"os"
"strings"
)
func main() {
var writable, visible, leaked []string
for _, path := range []string{"/box/injected", "/out/injected", "/usr/injected", "/injected", "/lib/injected"} {
if err := os.WriteFile(path, []byte("x"), 0o600); err == nil {
writable = append(writable, path)
}
}
if err := os.WriteFile("/tmp/scratch", []byte("ok"), 0o600); err != nil {
fmt.Println("scratch not writable:", err)
return
}
for _, path := range []string{
"/etc/passwd", "/etc/shadow", "/run/secrets", "/run/secrets/runner_api_token", "/opt/lincodex-runner",
"/var/lib/lincodex-runner", "/proc/1", "/proc/self/environ", "/proc/self/mounts", "/sys/fs/cgroup",
"/dev/mem", "/dev/kmsg", "/root", "/home",
} {
if _, err := os.Lstat(path); err == nil {
visible = append(visible, path)
}
}
for _, variable := range os.Environ() {
name, _, _ := strings.Cut(variable, "=")
if strings.Contains(name, "RUNNER") || strings.Contains(name, "TOKEN") || strings.Contains(name, "SECRET") {
leaked = append(leaked, name)
}
}
if len(writable)+len(visible)+len(leaked) == 0 {
fmt.Println("isolated")
return
}
fmt.Println("writable", writable, "visible", visible, "env", leaked)
}
+14
View File
@@ -0,0 +1,14 @@
// Process bomb: every copy starts more copies and ignores failures; the
// cgroup process limit must stop it.
package main
import (
"os"
"os/exec"
)
func main() {
for {
_ = exec.Command(os.Args[0]).Start()
}
}
+14
View File
@@ -0,0 +1,14 @@
// Endless standard output: the output limit must stop it.
package main
import (
"bytes"
"os"
)
func main() {
block := bytes.Repeat([]byte("x"), 65536)
for {
_, _ = os.Stdout.Write(block)
}
}
+14
View File
@@ -0,0 +1,14 @@
// Timeout survival: ignores every catchable signal and spins; only SIGKILL
// from the supervisor stops it.
package main
import (
"os/signal"
"syscall"
)
func main() {
signal.Ignore(syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP, syscall.SIGXCPU, syscall.SIGALRM, syscall.SIGUSR1)
for {
}
}
+13
View File
@@ -0,0 +1,13 @@
// Unbounded allocation of touched memory: the cgroup memory limit must stop it.
package main
func main() {
var chunks [][]byte
for {
chunk := make([]byte, 1<<20)
for index := range chunk {
chunk[index] = 1
}
chunks = append(chunks, chunk)
}
}
+14
View File
@@ -0,0 +1,14 @@
// Escape attempt: mounting a fresh procfs is killed by the seccomp policy.
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
_ = os.MkdirAll("/tmp/proc", 0o700)
err := syscall.Mount("proc", "/tmp/proc", "proc", 0, "")
fmt.Println("mount returned", err)
}
+52
View File
@@ -0,0 +1,52 @@
// Isolation probe: TCP, UDP, listening, name resolution, and raw, packet,
// and netlink sockets must all fail.
package main
import (
"context"
"fmt"
"net"
"strings"
"syscall"
"time"
)
func main() {
var reached []string
for _, target := range []struct{ network, address string }{
{"tcp", "1.1.1.1:53"}, {"tcp", "127.0.0.1:8443"}, {"tcp6", "[::1]:8443"}, {"udp", "8.8.8.8:53"},
} {
connection, err := net.DialTimeout(target.network, target.address, time.Second)
if err == nil {
// UDP "connects" without traffic; a write proves the route.
if _, writeErr := connection.Write([]byte("x")); writeErr == nil {
reached = append(reached, target.address)
}
_ = connection.Close()
}
}
if listener, err := net.Listen("tcp", "127.0.0.1:0"); err == nil {
reached = append(reached, "listen")
_ = listener.Close()
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if _, err := net.DefaultResolver.LookupHost(ctx, "example.com"); err == nil {
reached = append(reached, "dns")
}
for _, socket := range []struct{ domain, kind, protocol int }{
{syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP},
{syscall.AF_PACKET, syscall.SOCK_RAW, 0},
{syscall.AF_NETLINK, syscall.SOCK_RAW, 0},
} {
if fd, err := syscall.Socket(socket.domain, socket.kind, socket.protocol); err == nil {
reached = append(reached, fmt.Sprintf("socket %d", socket.domain))
_ = syscall.Close(fd)
}
}
if len(reached) == 0 {
fmt.Println("blocked")
return
}
fmt.Println("reached " + strings.Join(reached, ", "))
}
+25
View File
@@ -0,0 +1,25 @@
// Timeout survival: a child in its own session outlives the parent; the PID
// namespace must take it down with the run.
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
"time"
)
func main() {
if len(os.Args) > 1 {
time.Sleep(time.Hour)
return
}
child := exec.Command(os.Args[0], "daemon")
child.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
if err := child.Start(); err != nil {
fmt.Println("start failed:", err)
return
}
fmt.Println("parent done")
}
+24
View File
@@ -0,0 +1,24 @@
// Isolation probe: the program is not root and cannot become root, change
// ownership to root, or raise its limits.
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
ok := os.Getuid() != 0 && os.Geteuid() != 0 && os.Getgid() != 0 && os.Getegid() != 0
ok = ok && syscall.Setuid(0) != nil && syscall.Setgid(0) != nil
if err := os.WriteFile("/tmp/owned", nil, 0o600); err == nil {
ok = ok && os.Chown("/tmp/owned", 0, 0) != nil
}
limit := syscall.Rlimit{Cur: 1 << 40, Max: 1 << 40}
ok = ok && syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit) != nil
if ok {
fmt.Println("unprivileged")
return
}
fmt.Println("privileged")
}
+12
View File
@@ -0,0 +1,12 @@
// Escape attempt: ptrace is killed by the seccomp policy (SIGSYS).
package main
import (
"fmt"
"syscall"
)
func main() {
err := syscall.PtraceAttach(1)
fmt.Println("ptrace returned", err)
}
+8
View File
@@ -0,0 +1,8 @@
// Idle wait: it uses no CPU, so only the wall-clock limit can stop it.
package main
import "time"
func main() {
time.Sleep(time.Hour)
}
+23
View File
@@ -0,0 +1,23 @@
// Correct solution: prints the sum of the integers on standard input.
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
total := 0
for scanner.Scan() {
value, err := strconv.Atoi(scanner.Text())
if err != nil {
os.Exit(2)
}
total += value
}
fmt.Println(total)
}
+21
View File
@@ -0,0 +1,21 @@
// Thread bomb: goroutines blocked in system calls force the runtime to
// create threads; the cgroup process limit must stop it.
package main
import (
"os"
"syscall"
)
func main() {
var fds [2]int
if err := syscall.Pipe(fds[:]); err != nil {
os.Exit(2)
}
for {
go func() {
buffer := make([]byte, 1)
_, _ = syscall.Read(fds[0], buffer)
}()
}
}
+13
View File
@@ -0,0 +1,13 @@
// Escape attempt: unshare into a new user namespace is killed by the
// seccomp policy.
package main
import (
"fmt"
"syscall"
)
func main() {
err := syscall.Unshare(syscall.CLONE_NEWUSER)
fmt.Println("unshare returned", err)
}