跳到正文
DEV Community· İbrahim Kalaycı·· 9 小時前AI 評分55

rust-sandbox 以純 Rust 構建無需 root 權限的 Linux 沙箱,毋須 Docker 或守護程序

Building a Rootless Linux Sandbox in Pure Rust — Without Docker or a Daemon

AI 導讀

作者介紹 rust-sandbox(rsb)v0.1.2,這是一個單一執行檔及可嵌入的 Rust 程式庫,利用 Linux 內置核心功能隔離程序,無需 root 權限、守護程序或 C 依賴。

正文

İbrahim Kalaycı


If you ever need to run untrusted scripts, test builds, or execute AI-generated code on Linux, the usual options are either spinning up a full Docker container or trusting the process on your host.

I wanted something lighter: a single binary and embeddable Rust library that isolates a process using Linux's built-in kernel features — no root privileges, no daemon, and no C dependencies.

That turned into rust-sandbox (rsb) (v0.1.2).

What it does

  • Folder-level access control: Choose exactly which directories a command can read or write, or run it in a temporary copy-on-write (--cow) mode that leaves your real folder completely untouched and reports what changed on exit.
  • Domain-level network filtering: Keep networking off by default, or allow outbound traffic only to specific domains and ports while blocking private networks and cloud metadata IPs.
  • Resource & timeout limits: Enforce timeouts, output size caps, and optional Cgroups v2 memory/CPU/PID quotas.
  • Auto-generated profiles: Run rsb learn once on a command to trace what it needs and save a reusable sandbox.toml config.
  • Embeddable everywhere: Use it as a standalone CLI, a Rust crate (rsb-core), or via the included C and Python FFI bindings (including a warm Python worker pool for fast repeated runs).

Quick look

# Allow writing only to ./work, with no internet access
rsb run --rw ./work -- python3 untrusted.py

# Run destructively without touching the real folder
rsb ai-exec --cow ./work -- /bin/sh -c 'rm -rf ./work/*; echo test > ./work/a.txt'

# Allow HTTPS only to example.com:443
rsb run --transparent-tap --allow-domain example.com:443 -- curl -sI https://example.com

Scope & Notes

It's a Linux-only tool that shares the host kernel (namespace, Landlock, and Seccomp-BPF isolation — not a virtual machine). All of the architectural decisions and known boundaries are documented in ARCHITECTURE.md.

If you're into Rust, Linux internals, or sandboxing AI agents, feel free to check out the repo and share feedback or test it on your distro:

👉 GitHub: https://github.com/Shura5860/rust-sandbox

來源:DEV Community · dev.to