Repository navigation
feat: per-sandbox network policies (no egress, or an allowlist) - #32
Merged
Merged
Conversation
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>
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
marked this pull request as ready for review
November 18, 2026 14:24
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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=toSandbox.create/SandboxPool.create.SBX_NETWORK(noneor{"allow": [...]}). The server refuses to start if it cannot parse or enforce it.networkonPOST /v1/sandboxes, so one host can mix isolated and connected sandboxes.GET /v1/sandboxesand authenticated/health, so a client can tell it was applied.ptrace_scope0).How it works (
src/network.rs)ptrace,process_vm_writevandpidfd_getfd.connect()from the sandbox. Bind and listen still work, for the port proxy.sbx-server --net-supervise, a supervisor started as the command's parent, inside the sandbox. The filter sends the sandbox's IPsocket(),connect()andlisten()calls to it. It creates each IP socket and installs it as the result ofsocket()(ADDFD_FLAG_SEND), keeping a handle. Onconnect(), 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./etc/hosts.Decisions
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.pidfd_getfd. Taking the sandbox's socket would be simpler, but the HF Jobs seccomp profile refusespidfd_getfdwithoutCAP_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.PROTOCOLis not bumped: the change only adds fields, and the client checks the echoed policy.Tests
scripts/network-regression.sh(root, Docker) covers both modes. It includes the review's reproductions: epoll registered beforeconnect(), dup'd descriptors with EISCONN, and pending connects after a timeout (no server thread or supervisor left). All other regression suites pass.nonewith a mounted repo, dedicated allowlist (with epoll), and a pool host with open,noneand allowlist sandboxes.A release is needed before the client can pin this binary.
🤖 Generated with Claude Code