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

+22
View File
@@ -0,0 +1,22 @@
# Disk exhaustion: the scratch tmpfs and the file size limit refuse writes
# long before the host is affected.
import os
block = b"x" * (1 << 20)
refused = []
try:
with open("/tmp/one-big-file", "wb") as stream:
for _ in range(64):
stream.write(block)
stream.flush()
except OSError as error:
refused.append("file:" + os.strerror(error.errno))
os.remove("/tmp/one-big-file")
try:
for index in range(64):
with open(f"/tmp/part-{index}", "wb") as stream:
for _ in range(4):
stream.write(block)
except OSError as error:
refused.append("tmpfs:" + os.strerror(error.errno))
print("refused" if len(refused) == 2 else f"wrote everything {refused}")
+13 -5
View File
@@ -1,9 +1,11 @@
# Isolation probe: the root file system and the source are read-only, the
# scratch directory is writable, and runner files are not visible.
# Isolation probe: the root, the source, and the build output are read-only,
# only the scratch directory is writable, and no host file, runner file,
# secret, or process information is visible, neither on disk nor in the
# environment.
import os
writable = []
for path in ("/box/injected", "/usr/injected", "/injected", "/lib/injected"):
for path in ("/box/injected", "/out/injected", "/usr/injected", "/injected", "/lib/injected"):
try:
with open(path, "w") as stream:
stream.write("x")
@@ -12,5 +14,11 @@ for path in ("/box/injected", "/usr/injected", "/injected", "/lib/injected"):
pass
with open("/tmp/scratch", "w") as stream:
stream.write("ok")
visible = [path for path in ("/etc/passwd", "/run/secrets", "/opt/lincodex-runner", "/proc/1") if os.path.exists(path)]
print("isolated" if not writable and not visible else "writable=" + ",".join(writable) + " visible=" + ",".join(visible))
hidden = (
"/etc/passwd", "/etc/shadow", "/etc/hosts", "/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",
)
visible = [path for path in hidden if os.path.lexists(path)]
leaked = [name for name in os.environ if "RUNNER" in name or "TOKEN" in name or "SECRET" in name]
print("isolated" if not writable and not visible and not leaked else f"writable={writable} visible={visible} env={leaked}")
+8
View File
@@ -0,0 +1,8 @@
# Timeout survival: ignores every catchable signal and spins; only SIGKILL
# from the supervisor stops it.
import signal
for number in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGXCPU, signal.SIGALRM, signal.SIGUSR1):
signal.signal(number, signal.SIG_IGN)
while True:
pass
+8
View File
@@ -0,0 +1,8 @@
# Escape attempt: mounting a fresh procfs is killed by the seccomp policy.
import ctypes
import os
os.makedirs("/tmp/proc", exist_ok=True)
libc = ctypes.CDLL(None, use_errno=True)
libc.mount(b"proc", b"/tmp/proc", b"proc", 0, None)
print("mount returned")
+18 -8
View File
@@ -1,13 +1,11 @@
# Isolation probe: every attempt to reach a network must fail.
# Isolation probe: every attempt to reach or sniff a network must fail,
# including raw, packet, and netlink sockets and name resolution.
import socket
attempts = [
(socket.AF_INET, ("1.1.1.1", 53)),
(socket.AF_INET, ("127.0.0.1", 8080)),
(socket.AF_INET6, ("::1", 8080)),
]
attempts = 0
reached = []
for family, address in attempts:
for family, address in ((socket.AF_INET, ("1.1.1.1", 53)), (socket.AF_INET, ("127.0.0.1", 8443)), (socket.AF_INET6, ("::1", 8443))):
attempts += 1
try:
with socket.socket(family, socket.SOCK_STREAM) as connection:
connection.settimeout(1)
@@ -15,4 +13,16 @@ for family, address in attempts:
reached.append(address[0])
except OSError:
pass
print("blocked" if not reached else "reached " + " ".join(reached))
for family, kind, protocol in ((socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP), (getattr(socket, "AF_PACKET", 17), socket.SOCK_RAW, 0), (getattr(socket, "AF_NETLINK", 16), socket.SOCK_RAW, 0)):
attempts += 1
try:
socket.socket(family, kind, protocol).close()
reached.append(f"socket {family}/{kind}")
except OSError:
pass
try:
socket.getaddrinfo("example.com", 443)
reached.append("dns")
except OSError:
pass
print("blocked" if not reached else "reached " + ", ".join(reached))
+13
View File
@@ -0,0 +1,13 @@
# Timeout survival: a detached child in its own session outlives the
# parent; the PID namespace must take it down with the run.
import os
import sys
import time
if os.fork() == 0:
os.setsid()
if os.fork() == 0:
time.sleep(3600)
os._exit(0)
print("parent done")
sys.stdout.flush()
+6
View File
@@ -0,0 +1,6 @@
# Escape attempt: ptrace is killed by the seccomp policy (SIGSYS).
import ctypes
libc = ctypes.CDLL(None, use_errno=True)
libc.ptrace(0, 0, None, None)
print("ptrace returned")
+8
View File
@@ -0,0 +1,8 @@
# Escape attempt: a new user namespace would give capabilities inside it;
# unshare is killed by the seccomp policy.
import ctypes
CLONE_NEWUSER = 0x10000000
libc = ctypes.CDLL(None, use_errno=True)
libc.unshare(CLONE_NEWUSER)
print("unshare returned")