A hardcore Linux sandbox for running untrusted code safely.
Think of it as a bulletproof prison for dangerous programs - they can't escape, can't see your system, and can't use unlimited resources.
A sandbox = isolated environment where code runs trapped in a box.
Real-world example: When you run code on LeetCode or HackerRank, they use sandboxes like this so your code can't:
- Hack their servers
- See other users' solutions
- Mine bitcoin on their machines
- Fork bomb and crash everything
- π Online Code Judges (LeetCode, HackerRank) - Run student code safely
- π§ CI/CD Systems (GitHub Actions) - Execute untrusted build scripts
- π§© Plugin Systems - WordPress plugins, browser extensions, game mods
- π¦ Malware Analysis - Run suspicious files without risk
- βοΈ Cloud Functions (AWS Lambda style) - Multi-tenant code execution
cd tests
./demo.shThis runs 5 tests showing malicious programs trying to attack and getting blocked.
cd tests
# Test 1: Fork bomb (tries to crash system by creating infinite processes)
./manual_test ./fork_bomb
# β Should get killed at 10 processes: Exit code 137
# Test 2: Memory hog (tries to eat all RAM)
./manual_test ./memory_hog
# β Should get OOM killed at 100MB: OOM killed: YES
# Test 3: Privilege escalation (tries to hack with ptrace)
./manual_test ./escape_ptrace
# β Should fail: ptrace failed: Operation not permitted
# Test 4: Check root powers
./manual_test ./check_caps
# β Should show no capabilities: (empty set)7 Layers of Isolation:
- PID namespace - Can't see other processes
- Network namespace - Isolated network (no internet by default)
- Mount namespace - Own filesystem (can't see your files)
- User namespace - Different user ID
- IPC namespace - Can't talk to other programs
- UTS namespace - Different hostname
- Cgroup namespace - Can't see system resources
Resource Limits:
- Max 100MB RAM (configurable) β kills if exceeded
- Max 50% CPU (configurable) β throttles if greedy
- Max 10 processes (configurable) β blocks fork bombs
- Timeout after 10 seconds β kills long-running code
Security:
- Seccomp-BPF - Blocks dangerous syscalls (ptrace, mount, kernel modules)
- Capability dropping - Removes ALL root privileges
- Filesystem isolation - Can't access your files
Your Code
β
Sandbox CLI βββ Supervisor Daemon (root)
β
Creates isolated environment:
- 7 namespace types
- Cgroup limits
- Seccomp filter
- Drops capabilities
β
Sandboxed Executor
β
Untrusted Code (TRAPPED)
Multi-process architecture:
- Supervisor Daemon - Privileged process managing sandbox lifecycle
- Sandbox Manager - Sets up isolation (namespaces, cgroups, mounts)
- Sandboxed Executor - Runs untrusted code in isolated environment
Isolation layers:
- All 7 namespace types (PID, NET, UTS, IPC, MOUNT, USER, CGROUP)
- Seccomp-BPF syscall filtering (3 policy levels: minimal <10 syscalls, standard ~50, permissive)
- Cgroups v2 resource limits (CPU, memory, PIDs, IO)
- Filesystem isolation with pivot_root + minimal rootfs
- Network isolation (none, loopback-only, bridged with veth)
- Complete capability dropping + bounding set clearing
- Resource limits via rlimits
Communication:
- IPC via Unix domain sockets with binary protocol
- File descriptor passing for stdin/stdout/stderr
- Real-time metrics and audit logging
| Attack | How It's Blocked | Result |
|---|---|---|
| Fork bomb (infinite processes) | Cgroup PID limit (max 10) | Killed at process 10 |
| Memory bomb (eat all RAM) | Cgroup memory limit (100MB) | OOM killed |
| CPU hogging (100% CPU forever) | Cgroup CPU quota (50%) | Throttled to 50% |
| ptrace (debug parent process) | Seccomp blocks ptrace syscall | Operation not permitted |
| mount (hack filesystem) | No CAP_SYS_ADMIN capability | Permission denied |
| Kernel module loading | Seccomp blocks init_module | Bad syscall |
| Network access | Network namespace (loopback only) | No route to host |
mkdir build && cd build
cmake ..
make -j4Executables: sandbox_supervisor, sandbox_manager, sandbox_executor, sandbox_cli
# Ubuntu/Debian
sudo apt install libcap-dev libseccomp-dev cmake g++
# Arch
sudo pacman -S libcap libseccomp cmake gccCreate sandbox.conf:
[sandbox]
rootfs_path=/tmp/sandbox_root
work_dir=/sandbox
bind_mounts=/usr/bin:/usr/bin:ro,/lib:/lib:ro,/lib64:/lib64:ro
enable_network=false
seccomp_policy=standard
[resources]
memory_limit=512M
cpu_quota=100000
cpu_period=100000
max_pids=100
max_file_size=100M
max_open_files=1024
[execution]
timeout_seconds=30
capture_output=truesudo ./sandbox_supervisor --config sandbox.conf --socket /var/run/sandbox.sock./sandbox_cli start --command "/bin/python3 script.py" --args "arg1 arg2" --config sandbox.conf./sandbox_cli status --sandbox-id <id>./sandbox_cli metrics --sandbox-id <id>./sandbox_cli kill --sandbox-id <id>./sandbox_cli stopMinimal - Only essential syscalls (<10): read, write, exit, brk, mmap, close
Standard - Common syscalls (~50): + file ops, signals, memory management, basic IO
Permissive - Allow all except dangerous: blocks ptrace, kernel modules, bpf, perf, syslog
- none - No network access
- loopback_only - Only localhost (127.0.0.1)
- bridged - Full network via veth pair to bridge
- CPU quota/period (microseconds)
- Memory limit (bytes)
- Max PIDs
- Max file size
- Max open files
- Execution timeout
- Sandbox creation: <10ms
- Syscall filtering overhead: <1%
- Zero-copy IPC with FD passing
- Can run 1000+ sandboxes simultaneously
# Start supervisor
sudo ./sandbox_supervisor --socket /var/run/sandbox.sock &
# Run Python script with 256MB RAM, 50% CPU, 10s timeout
./sandbox_cli start \
--command "/usr/bin/python3" \
--args "untrusted.py" \
--memory 256M \
--cpu-quota 50000 \
--timeout 10 \
--seccomp standard \
--network none
# Check metrics
./sandbox_cli metrics --sandbox-id sandbox_1234Multi-process architecture:
- Supervisor Daemon - Privileged process managing sandbox lifecycle
- Sandbox Manager - Sets up isolation (namespaces, cgroups, mounts)
- Sandboxed Executor - Runs untrusted code in isolated environment
Isolation layers:
- All 7 namespace types (PID, NET, UTS, IPC, MOUNT, USER, CGROUP)
- Seccomp-BPF syscall filtering (3 policy levels: minimal <10 syscalls, standard ~50, permissive)
- Cgroups v2 resource limits (CPU, memory, PIDs, IO)
- Filesystem isolation with pivot_root + minimal rootfs
- Network isolation (none, loopback-only, bridged with veth)
- Complete capability dropping + bounding set clearing
- Resource limits via rlimits
Communication:
- IPC via Unix domain sockets with binary protocol
- File descriptor passing for stdin/stdout/stderr
- Real-time metrics and audit logging
Need help? Read QUICKSTART.md for detailed explanations for security test results.