Skip to content

feat: per-sandbox network policies (no egress, or an allowlist) - #32

Merged
Wauplin merged 5 commits into
mainfrom
sandbox-network-policies
Nov 18, 2026
Merged

Wauplin merged 5 commits into
mainfrom
sandbox-network-policies

Conversation

@Wauplin

@Wauplin Wauplin commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds per-sandbox network policies: no outbound access, or an allowlist. This is the server side of the plan from this Slack thread (plan, huggingface/huggingface_hub#5261). The client side is huggingface/huggingface_hub#5303, which adds network= to Sandbox.create / SandboxPool.create.

  • Dedicated mode: SBX_NETWORK (none or {"allow": [...]}). The server refuses to start if it cannot parse or enforce it.
  • Host mode: network on POST /v1/sandboxes, so one host can mix isolated and connected sandboxes.
  • The policy is echoed in the create response, GET /v1/sandboxes and authenticated /health, so a client can tell it was applied.
  • The server is now non-dumpable. Root code in a dedicated sandbox cannot read or change its memory (HF Jobs run with Yama ptrace_scope 0).

How it works (src/network.rs)

  • seccomp: unix, netlink and TCP over IPv4/IPv6 only. It refuses UDP, raw sockets, other families, TCP Fast Open, io_uring, ptrace, process_vm_writev and pidfd_getfd.
  • Landlock (ABI 4): refuses every TCP connect() from the sandbox. Bind and listen still work, for the port proxy.
  • Allowlist: the server runs the command through sbx-server --net-supervise, a supervisor started as the command's parent, inside the sandbox. The filter sends the sandbox's IP socket(), connect() and listen() calls to it. It creates each IP socket and installs it as the result of socket() (ADDFD_FLAG_SEND), keeping a handle. On connect(), it checks the destination it read and connects that handle, which is the sandbox's own socket. epoll, dup'd descriptors, socket options and EISCONN behave normally, and there is no time-of-check/time-of-use gap.
  • Hostnames are resolved once by the server and pinned in /etc/hosts.

Decisions

  • Supervisor, not server threads. The first version replaced the sandbox's socket at connect() time and handled each connect in a server thread. The review found that this broke epoll and dup'd descriptors, and that pending connects kept server threads alive past a timeout. The supervisor shares the command's process group and limits, so a timeout or kill ends it. It handles at most 64 pending connects. A blocking connect stops waiting as soon as the caller is gone.
  • No pidfd_getfd. Taking the sandbox's socket would be simpler, but the HF Jobs seccomp profile refuses pidfd_getfd without CAP_SYS_PTRACE (checked on a real job). So the supervisor creates the sockets and holds them until they connect, listen or are closed (swept every second).
  • "none" needs no supervisor. Landlock refuses connects, and listening still works.
  • PROTOCOL is not bumped: the change only adds fields, and the client checks the echoed policy.

Tests

  • Unit tests: policy parsing and matching, BPF assembly, sockaddr decoding.
  • scripts/network-regression.sh (root, Docker) covers both modes. It includes the review's reproductions: epoll registered before connect(), dup'd descriptors with EISCONN, and pending connects after a timeout (no server thread or supervisor left). All other regression suites pass.
  • End to end on HF Jobs: dedicated none with a mounted repo, dedicated allowlist (with epoll), and a pool host with open, none and allowlist sandboxes.

A release is needed before the client can pin this binary.

🤖 Generated with Claude Code

Wauplin and others added 2 commits November 4, 2026 15:42
Commands run under a network policy get a seccomp filter (TCP-only IP
sockets, no UDP/raw/io_uring/TFO/ptrace) and a Landlock ruleset that
refuses every TCP connect. With an allowlist, connect() is routed to the
server, which checks the destination, connects its own socket and installs
it in the sandbox (SECCOMP_IOCTL_NOTIF_ADDFD). Hostnames are resolved by
the server and pinned in /etc/hosts.

Dedicated mode: SBX_NETWORK. Host mode: `network` on POST /v1/sandboxes,
echoed back in the response. The server also marks itself non-dumpable so
sandboxed root code cannot act through it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replacing the sandbox's socket at connect() time (NOTIF_ADDFD SETFD) broke
epoll registrations, duplicated descriptors and EISCONN, and every pending
connect held a privileged server thread past the command's timeout.

With an allowlist, the server now runs the command through a supervisor
(sbx-server --net-supervise) that runs inside the sandbox as its parent.
The supervisor creates the sandbox's IP sockets itself (ADDFD_FLAG_SEND on
socket()) and keeps a handle until they connect, listen or are closed, so
connect() acts on the sandbox's own socket. It shares the command's process
group and limits, bounds pending connects at 64, and stops waiting on a
blocking connect as soon as the caller is gone. pidfd_getfd was not an
option: the Jobs seccomp profile refuses it without CAP_SYS_PTRACE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Wauplin and others added 3 commits November 5, 2026 01:41
In dedicated mode the sandbox runs as root and can replace the server binary at its path on disk.
The supervisor was exec'd by that path and runs without the sandbox's restrictions, so the next
allowlisted command got an unrestricted parent of the sandbox's choosing, with full network access.
/proc/self/exe always resolves to the inode the server was started from.

Adds the reproduction to scripts/network-regression.sh (fails before, passes after).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 256-entry limit was checked on the entries sent, then again by the supervisor of every command on
the resolved rules: a few CDN hostnames passed creation, then made every command exit with code 2.
The limit is now 1024 and also applies to the resolved rules when the sandbox is created (1024 rules
stay well under the 128 KiB argument limit). Also give supervisor workers a 256 KiB stack, so 64 of
them fit under a small memory limit, explain why a connect could not be checked, and document the
limits of the supervisor (held sockets, other users, background processes, pending calls).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With an allowlist, a command the supervisor could not start (a missing program, for example) used to
look like the command's own result: exit code 126/127 and a message in its stderr. Without a policy,
the same mistake is a 400 "failed to spawn". The supervisor now reports its setup on a pipe the
server reads until the command is started, so both cases fail the same way. The pipe is passed to the
supervisor only (close-on-exec everywhere else), and the command does not inherit it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Wauplin
Wauplin marked this pull request as ready for review November 18, 2026 14:24
@Wauplin
Wauplin merged commit ef31b89 into main Nov 18, 2026
4 checks passed
Wauplin added a commit to huggingface/huggingface_hub that referenced this pull request Nov 19, 2026
Built from huggingface/sandbox-server@ef31b89 (v0.8.0), which includes
huggingface/sandbox-server#32.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant