Skip to content

insbx on Windows: a writable root's grant is kept, so a run no longer walks the tree - #85

Merged
blockmandev merged 2 commits into
mainfrom
perf/insbx-kept-grants
Nov 13, 2026
Merged

blockmandev merged 2 commits into
mainfrom
perf/insbx-kept-grants

Conversation

@blockmandev

Copy link
Copy Markdown
Member

Stacked on #84 (the rename). Merge #84 first; this one then retargets to main.

The problem (measured)

The sandbox itself starts in 0.1 s. But each run granted every writable root to a per-run SID and revoked it after, and NTFS writes an inherited ACE into every object under a folder: 2.3 s per walk on this repo (17,752 objects). A network-deny run walked 5–7 times (run SID, offline account, its read of the current folder, each granted and revoked). incode runs one insbx run per shell command, so every tool call paid it.

insbx run -w <repo> -- cmd /c echo ok before after
network allow 4.7 s 0.05 s
network deny 14–20 s 0.1 s
deny, no -w (current folder read) 15.6 s 0.1 s
first run on a folder 35 s 39 s (once per folder)

The fix: kept grants, one SID per folder

As OpenAI Codex's Windows sandbox does (windows-sandbox-rs/src/cap.rs: a capability SID per writable root, persisted):

  • Each root's SID is derived from the SHA-256 of its path (S-1-5-21-1111111111-2222222222-4141414141-a-b-c-d, Windows CNG, no new dependency).
  • The first run grants it (one walk). The grant is kept; later runs check the folder's own ACL (passes_down) and walk nothing.
  • The offline account's ACEs on roots and on a current folder are kept too.
  • The temp directory stays per run (own SID, revoked after, crash sweep unchanged).

What keeps it confined

  • The token carries the run SID plus the SIDs of this run's roots only; the helper refuses any SID of another shape. A kept grant elsewhere names a SID the token doesn't hold. (A confined process cannot add SIDs to its token: CreateRestrictedToken on a restricted token intersects them.)
  • A root grant that fails part-way is taken off again, so the next run re-walks and reports it rather than trusting a half-granted tree.
  • A root holding insbx's own state (%LOCALAPPDATA%\insbx) is refused.

Costs (documented in README and module docs)

New commands

insbx grants (kept folders, and whether each still carries its grant) and insbx revoke <DIR>... | --all.

Tests (Windows, real insbx-offline account, none skipped)

fmt, clippy -D warnings (workspace), sandbox crate 81 unit + 24 escape pass. New or changed:

  • a kept grant is made once, then only read (a file moved in is not re-granted by the second lease, which proves no walk; after revoke_kept the next lease re-grants it)
  • a kept grant is writable only to runs that name the folder (with a control)
  • insbx grants / insbx revoke remove root and account ACEs
  • a part-way failure is not trusted, and the run's own temp grant is revoked
  • every part of both SID prefixes fits in 32 bits. The first prefix (4343434343) did not: Windows stored it as 4294967295, so the ACE named a different SID from the token's. The new tests caught it.

… walks the tree

Measured on this repository (17,752 objects): the sandbox itself starts in
0.1 s, but every run granted each writable root to a per-run SID and revoked
it after, and NTFS writes an inherited ACE into every object beneath a
folder: 2.3 s per walk. `insbx run -w <repo> -- cmd /c echo ok` took 4.7 s,
and 14-20 s under network deny (5-7 walks: the run SID, the offline
account, its read of the current folder, each granted and revoked). incode
runs one `insbx run` per shell command, so every tool call paid it.

Now each writable root has its own SID, from the SHA-256 of its path
(S-1-5-21-1111111111-2222222222-4141414141-a-b-c-d, hashed by Windows' CNG,
no new dependency). The first run on a folder grants it (one walk); the
grant is kept, and later runs read the folder's own ACL to see it is there.
As OpenAI Codex's Windows sandbox does (a capability SID per writable root).
The offline account's ACEs on roots and on a current folder are kept too.

After: the same `echo ok` takes 0.05 s (allow) and 0.1 s (deny); the first
run on a folder still walks it once.

What keeps it confined:
- the command's token carries the run SID (its temp directory, still per
  run and revoked after) and the SIDs of THIS run's roots only; the helper
  refuses any SID not of those shapes. A kept grant on another folder names
  a SID the token does not hold.
- a root grant that fails part-way is taken off again, so the next run
  walks and reports it instead of trusting a half-granted tree.
- a root holding insbx's own state (%LOCALAPPDATA%\insbx) is refused.

What it costs, documented:
- under network deny, a folder an earlier run was given stays READABLE to
  the offline account in later runs (never writable);
- a file moved into a root keeps its old ACL, so the command cannot write
  it (fails closed) until `insbx revoke <ROOT>`.

New: `insbx grants` (kept folders, and whether each still carries its
grant) and `insbx revoke <DIR>... | --all`.

Found by the new tests: a SID sub-authority above u32::MAX (the first
choice, 4343434343) is stored by Windows as 4294967295, so the ACE named a
different SID from the token's; a test now checks every prefix part.

Tests (Windows, real insbx-offline account): 81 unit, 24 escape, none
skipped. New: a kept grant is made once then only read (a file moved in is
not re-granted until revoke), a kept grant is writable only to runs naming
the folder, revoke takes root and account ACEs back, a part-way failure is
not trusted.
… moved-in files need icacls /reset

CI's windows runner showed the moved-file signal proves nothing: a file moved
into a root is not re-ACL'd by any walk (here too, once looked at), so it
could not show whether the second lease walked. The lease now records the
roots whose grant it had to make; the test asserts the first lease walks,
the second does not, and the one after revoke walks again. Docs that said
revoke + the next run reaches moved-in files are corrected: no grant does
(nor did the per-run one); icacls <FILE> /reset does, and the test shows it.
@blockmandev
blockmandev force-pushed the perf/insbx-kept-grants branch from 4221eba to 443b243 Compare November 13, 2026 15:45
@blockmandev
blockmandev changed the base branch from feat/insbx-rename to main November 13, 2026 15:45
@blockmandev
blockmandev merged commit 54d95ed into main Nov 13, 2026
10 checks passed
@blockmandev
blockmandev deleted the perf/insbx-kept-grants branch November 15, 2026 20:29
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