Skip to content

sbx: network deny natively on Windows (layer 2) — offline account + WFP - #27

Merged
blockmandev merged 4 commits into
mainfrom
claude/laughing-darwin-hyqkjj
Oct 21, 2026
Merged

blockmandev merged 4 commits into
mainfrom
claude/laughing-darwin-hyqkjj

Conversation

@blockmandev

Copy link
Copy Markdown
Member

Closes #24 once CI proves it on real Windows. Draft until the Windows job is green with the gated network tests actually running.

Summary

Each design choice follows OpenAI Codex's codex-rs/windows-sandbox-rs (HEAD d4e03acb); the file named is where it came from.

Setup: sbx setup-windows (elevated, idempotent; --remove undoes it)

  • The account: creates the local account sbx-offline (NetUserAdd, as sandbox_users.rs does).
    • It's in Users, which it needs to log on and read C:\Windows, and never in Administrators.
    • Its password is 256 random bits, never printed and zeroed after use. The setup checks it once with a test logon.
    • It's hidden from the sign-in screen (hide_users.rs).
  • Storing the password: DPAPI with machine scope (dpapi.rs) in %ProgramData%\sbx\offline.conf.
    • The folder's ACL is protected: SYSTEM, Administrators and the user who ran the setup. Users is excluded, so neither the account nor a sandboxed command can read it.
    • If the folder already exists, the setup refuses unless Administrators or SYSTEM own it and it isn't a link.
  • The firewall: WFP filters (FwpmFilterAdd0, one transaction, fixed keys; wfp.rs) that match that account.
    • They block outbound connects, inbound accepts and raw ICMP on IPv4 and IPv6, loopback included.
    • One higher-weight permit allows TCP to 127.0.0.1 on ports 47600–47615 (--proxy-ports), so proxy (the host allowlist) works natively (firewall.rs).
  • --read <dir>: grants the account read access to a toolchain outside system folders, such as ~/.cargo.

Running a command

  • When the policy denies the network, or uses the proxy, and the setup is present: the user's helper makes the job, then starts a second copy of itself as the account with CreateProcessWithLogonW, suspended, and assigns it to the job (runner_client.rs).
  • That second helper builds the existing write-restricted token and per-run SID from its own token, then starts the command.
  • The spec travels in an environment variable that is removed before the command starts (CreateProcessWithLogonW caps the command line at 1024 characters).
  • No profile is loaded for the account.
  • Grants: writable roots get ACEs for both the run's SID and the account. The account's ACE is shared between runs, reference-counted, and covered by the crash sweep.

Reporting

  • enforcement() marks deny and proxy as enforced only when the setup record is readable, the account's SID matches it and the main filter is present. Otherwise it says to run sbx setup-windows as administrator.
  • auto picks native with the setup and WSL without it.
  • sbx doctor has a new "offline user" line.

Behaviour to know (in the crate README)

  • Under deny, the command can only read what the account can: system folders, the run's folders and anything added with --read.
  • Not blocked by the filters:
    • DNS lookups, which the DNS Client service resolves on the command's behalf;
    • \\host\share paths, which the kernel opens.
  • Under deny, loopback is blocked too, so test suites that talk to their own local server fail. Use proxy or allow for those.

CI

  • windows-latest runs sbx setup-windows (the runner is admin) and sbx doctor before cargo test -p incord-sandbox, so the gated tests run there.
  • New gated tests in tests/escape_windows.rs:
    • TCP to a local server and to 1.1.1.1, and UDP, all refused under deny;
    • writes and exit codes under deny;
    • the account's grant during and after a run;
    • two concurrent runs sharing a folder;
    • the proxy allowlist;
    • sbx mcp.
  • Existing tests that assumed deny is never enforced on Windows now branch on whether the setup is present.

Test plan

  • Linux: cargo clippy -p incord-sandbox --all-targets -D warnings, cargo test -p incord-sandbox, and the full scripts/test-all.sh rust sbx
  • cargo clippy -p incord-sandbox --target x86_64-pc-windows-gnu --all-targets -D warnings
  • The 60 Windows lib tests pass under Wine (Wine stubs the account calls, so setup can't run there)
  • Windows CI: setup succeeds, doctor says enforced, and the gated network tests run and pass. Things only real Windows can show:
    • that the user-matching WFP filter also catches the write-restricted token;
    • that loopback is really blocked;
    • that CreateProcessWithLogonW works from the runner (inherited pipes, job assignment);
    • that PowerShell and curl.exe run without a profile;
    • the threads' default DACL.

Generated by Claude Code

…FP cuts off

The way OpenAI Codex's Windows sandbox does it (codex-rs/windows-sandbox-rs:
sandbox_users.rs, firewall.rs, wfp.rs, dpapi.rs, runner_client.rs).

`sbx setup-windows` (elevated; refuses clearly without it; idempotent):
- creates the local account sbx-offline (Users, never Administrators),
  random 256-bit password never shown, hidden from the sign-in screen;
- stores the password DPAPI machine-scope in %ProgramData%\sbx\offline.conf
  under a protected ACL: SYSTEM, Administrators, the installing user;
- adds persistent WFP filters (FwpmFilterAdd0, one transaction, fixed keys)
  on FWPM_CONDITION_ALE_USER_ID: connect, accept and raw ICMP blocked, v4 and
  v6, loopback included; one higher-weight permit for TCP to 127.0.0.1 on a
  fixed proxy port range (47600-47615, --proxy-ports);
- --read <DIR> grants the account read on a toolchain; --remove undoes all.

At run time, deny/proxy start a second helper as the account with
CreateProcessWithLogonW (no profile), which builds the write-restricted
token from its own token and starts the command; the first helper's job
holds both. Roots are granted to the run SID and to the account (whose
shared ACE is only revoked when no other live run needs it); the current
folder and the helper program are granted for reading. The proxy listens
in the permitted range. enforcement() reports deny/proxy enforced when the
setup is found (record readable, account SID matches, filter present),
else NotEnforced naming `sbx setup-windows`; auto then runs natively.
`sbx doctor` reports it.

Tests: pure parts unit-tested everywhere (record format, password,
SDDL, filter specs, setup args, status, shared-ACE logic, spec transport);
tests/escape_windows.rs gains gated network tests (TCP local/outside, UDP,
writes and exit codes under deny, the account's grant, concurrent runs,
proxy allowlist, mcp), SKIPPED without the setup. CI runs setup-windows on
windows-latest before the sandbox tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011DiH8n52ifWoCd5Kx2aHwT
As the offline account, PowerShell started at C:\ instead of the run's
folder, so relative paths (Set-Content started.txt) went to the drive root.
Its FileSystem provider sets the first location by looking the current
folder up in its parent (NormalizeRelativePath: Directory.EnumerateFiles /
EnumerateDirectories(parent, leaf)), which needs FILE_LIST_DIRECTORY on the
parent; the account has none on a folder under the user's profile, startup
swallows the UnauthorizedAccessException and leaves the drive root.

Each deny run now lets the account list the current folder's parent (unless
it is a drive or share root, or a root or read folder of the run holds it):
one allow ACE, FILE_LIST_DIRECTORY | FILE_TRAVERSE | FILE_READ_ATTRIBUTES |
SYNCHRONIZE, no inheritance flags, written to that folder alone through its
handle with SetKernelObjectSecurity (no propagation walk over the children).
It is recorded in the grant record (`parent` lines; older records parse with
none), kept while another live run lists the same folder, removed as that
exact ACE only, and swept after a crash. An account grant on a folder that
carries another run's listing ACE takes it off and puts it back, so
SetEntriesInAclW cannot merge the inheritable grant into it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011DiH8n52ifWoCd5Kx2aHwT
The list ACE on the current folder's parent did not change it (CI: Get-Location
still C:\). The test now prints what an explicit Set-Location to the current
directory throws, and each folder's ACL up the chain, so the next run names
the call that fails instead of a guess.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011DiH8n52ifWoCd5Kx2aHwT
CI showed the list ACE on the immediate parent was there and an explicit
Set-Location to the current folder still failed "Access is denied": Windows
PowerShell looks every component up in the folder above. The account now
gets the same list-only, non-inherited ACE on each folder from the current
folder's parent up to (not including) a drive or share root or the profiles
folder (C:\Users, listable by every user already, and never re-ACLed by an
elevated run), less what a root or read folder already holds. Bookkeeping,
release and sweep are the existing per-folder ones. README says what the
command can then see (names, no contents) on the way to the project.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011DiH8n52ifWoCd5Kx2aHwT
@blockmandev
blockmandev marked this pull request as ready for review October 21, 2026 00:10
@blockmandev
blockmandev merged commit bf08929 into main Oct 21, 2026
7 checks passed
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.

sbx on Windows, layer 2: block the network natively (offline user + firewall)

2 participants