Skip to content

insbx on Windows: Git Bash / msys can't start natively (error 5); fix with a private object namespace per run #38

Closed

Description

@blockmandev

Why

Natively on Windows, every msys program (Git Bash, and anything linked against msys-2.0.dll) dies under sbx with CreateFileMapping ..., Win32 error 5 (documented as a known limit in the README and src/windows/mod.rs).

This blocks the main use case: Claude Code's Bash tool runs through Git Bash on Windows. sbx run ... -- claude starts the CLI (node isn't msys), but every Bash tool call fails. Today the only working path is SBX_BACKEND=wsl.

Cause (from msys2-runtime, winsup/cygwin/mm/shared.cc)

  • Every msys process opens a per-user shared section named <user SID>.<USER_VERSION> (user_info::create, name from cygheap->user.get_windows_id). It holds per-user state, including the mount table.
  • It is created with CreateFileMappingW(INVALID_HANDLE_VALUE, &sec_none, PAGE_READWRITE, ...). sec_none has no descriptor, so the section gets the default DACL of whichever process created it first.
  • The user's normal Git Bash usually creates it first: its DACL grants the user SID and SYSTEM, not the run's SID and not Everyone.
  • The sandboxed bash has the same user SID, so it opens the same section by name and asks for write. The write-restricted token's restricted check needs one of its restricting SIDs (run SID, Everyone, logon SID) granted write. None is, so: access denied, error 5.
  • The machine-wide shared.<n> section is created with sec_all_nih (Everyone), so it passes. The per-user section is the culprit.

Do NOT fix it by granting the run SID on the existing section

That would make bash start, but the sandbox could then write the mount table the user's unsandboxed Git Bash reads (e.g. remap /usr/bin to a sandbox-writable folder, so the user's next normal bash runs planted binaries). That is a sandbox escape. The same leak exists in reverse whenever a sandboxed bash creates the section first and an unsandboxed one shares it. The sandbox must never share msys shared sections with processes outside the run.

Proposed fix: a private object namespace per run (BNO isolation)

Start the command with PROC_THREAD_ATTRIBUTE_BNO_ISOLATION, a per-run prefix (e.g. sbx-<rid>). The struct is PROC_THREAD_BNO_ISOLATION_ATTRIBUTE { BOOL IsolationEnabled; WCHAR IsolationPrefix[136]; }. If windows-sys 0.61 doesn't export the constant, it should be 0x00020013 (attribute number 19 | PROC_THREAD_ATTRIBUTE_INPUT); confirm against winbase.h.

Effect, if it behaves as expected:

  • unprefixed and Local\ named objects of the command resolve into a private directory, so the first msys process in the run creates its own user section with the token's default DACL, which already includes the run SID (default_dacl_sddl), and the write succeeds;
  • the run never sees the user's real msys mount table or process table (cygpid.* sections are opened through the same open_shared path), and two runs can't see each other's.

Where

crates/sandbox/src/windows/helper.rs, start(): the command is spawned with CreateProcessAsUserW / CreateProcessW and a plain STARTUPINFOW. Switch to STARTUPINFOEXW + EXTENDED_STARTUPINFO_PRESENT and an attribute list (InitializeProcThreadAttributeList / UpdateProcThreadAttribute) holding the BNO attribute. Keep CREATE_SUSPENDED and the job-object assignment as they are. The offline-account path already reaches this same call in the second helper, so CreateProcessWithLogonW not taking attribute lists doesn't matter.

Derive the prefix from the run's RID so it is unique per run and easy to recognise.

Must be measured before relying on it (not verified yet; nobody has run this)

  1. BNO isolation works for an unprivileged caller spawning under a write-restricted token (and under the offline account). If CreateProcessAsUserW rejects the attribute, record the error and stop.
  2. Children inherit it. The prefix is stored on the token (TokenBnoIsolation), so it should, but bash spawning git, sed, etc. must land in the same namespace.
  3. msys also creates objects through absolute NT paths that isolation does not redirect: \BaseNamedObjects\msys-...-<installation key> and \Sessions\BNOLINKS\<n>\msys-...-<installation key> (get_shared_parent_dir / get_session_parent_dir). Their directories are created with an Everyone descriptor, so they shouldn't fail, but list what lives in them during a run (WinObj or NtQueryDirectoryObject) and confirm nothing writable is shared with unsandboxed msys processes. If something is, report it here.
  4. Pipes and ptys use named pipes (npfs), not BNO. Once the section is fixed they may be the next failure; capture any new error text.

Tests (tests/escape_windows.rs)

windows-latest runners have Git Bash at C:\Program Files\Git\bin\bash.exe.

  • Starts: under sbx run -w <tmp>, bash -c "echo ok > x && cat x" exits 0 and prints ok. Run it once with an unsandboxed bash already running (holding the user section open) to reproduce the real-world order. This test should fail with error 5 before the fix.
  • Still confined: under the same run, bash -c "echo x > <outside root>" is refused.
  • Children: bash -c "git --version && sed --version" works.
  • No shared state (the escape): start an unsandboxed bash, have a sandboxed bash add a user mount (mount <tmp-as-windows-path> /sbx-probe; msys keeps user mounts in the per-user section), then confirm the unsandboxed bash's mount output is unchanged.
  • Under setup-windows: the same "starts" test with --net deny.

Done when

  • The tests above pass on windows-latest.
  • README and the src/windows/mod.rs doc comment no longer list Git Bash as a known limit (or say exactly what still fails).
  • sbx mcp on Windows can offer sh/bash as an interpreter when Git Bash is present.

Generated by Claude Code

Activity

  1. blockmandev commented on Oct 22, 2026

    @blockmandev
    MemberAuthor

    Measured on Windows 11 (Git for Windows, msys-2.0.dll dated 2025-10-28): BNO isolation works, but it does not fix Git Bash.

    1. Reproduced

    With an unsandboxed Git Bash running (it holds the user section open):

    sbx run -w . -- "C:\Program Files\Git\bin\bash.exe" -c "echo ok > x && cat x"
    → *** fatal error - CreateFileMapping S-1-5-21-…-1004.1, Win32 error 5.  Terminating.
    

    2. BNO isolation, as proposed

    helper::start used STARTUPINFOEXW with one attribute, PROC_THREAD_ATTRIBUTE_BNO_ISOLATION, under the confining token.

    • The constant. windows-sys 0.61 has no binding, so I derived it with the same n | 0x20000 encoding as PARENT_PROCESS (0x20000) and HANDLE_LIST (0x20002), giving 0x20013.
    • The prefix: sbx-<rid>.
    • The call: CreateProcessAsUserW accepts it for an unprivileged caller with a write-restricted token.

    3. It takes effect: a control

    I held a Local\sbx-bno-probe-… mutex outside and asked for it from inside a run (Mutex.TryOpenExisting):

    who asks sees the outside object
    unconfined child True
    sbx run without the change True
    sbx run with the change False

    So the attribute is applied, and Win32 named objects are isolated per run.

    4. Git Bash still dies the same way

    It's the same error: CreateFileMapping <SID>.1, Win32 error 5.

    5. Why

    • msys does not name its shared sections through the Win32 Local\ / Global\ prefixes, which are what BNO isolation redirects.
    • msys-2.0.dll holds the UTF-16 strings \BaseNamedObjects, \Sessions\ and BNOLINKS, and no Local\ or Global\ at all. My search was checked against kernel32.dll, which it does find.
    • So msys opens its object directories by absolute NT path (get_shared_parent_dir / get_session_parent_dir). That is the case point 3 of this issue warned about. Isolation doesn't redirect these paths, so the per-user <SID>.1 section is still the user's, and the restricted write check still refuses it.

    What this leaves

    I stopped after these two measured attempts rather than guessing further. These are the candidate directions, none tried yet:

    • A private msys runtime per sandbox. msys's "installation key", the msys-<key> in those directory names, comes from where msys-2.0.dll lives. A copy of Git's usr\bin runtime in an sbx-owned folder would get its own key and its own sections, never shared with the user's bash. This costs disk and an update story.
    • An AppContainer (lowbox) token for the command. Its named objects live under AppContainerNamedObjects, but absolute \BaseNamedObjects paths would need checking the same way, and AppContainer changes far more than objects.
    • Keep SBX_BACKEND=wsl for msys programs on Windows, as documented today.

    BNO isolation on its own is still a hardening worth weighing: a confined command can't open the user's Local\ mutexes, events or sections. But it changes what every Windows run can see (for example, a jobserver semaphore handed in from outside), so I haven't proposed it without that decision. The working code is kept locally, not pushed: D:/Projects/_backups/incord-code/sbx-bno-isolation-experiment-2026-10-22.patch.

  2. blockmandev commented on Oct 22, 2026

    @blockmandev
    MemberAuthor

    Second round: the private-runtime idea measured, and the real blocker found in msys's source.

    1. A private msys runtime gets past the shared section

    I copied Git's usr\bin (88 MB) to a scratch folder and ran its bash.exe under sbx run while an ordinary Git Bash was running:

    bash under sbx result
    Git's own CreateFileMapping <SID>.1, Win32 error 5 (as before)
    the private copy gets past it, then: couldn't create signal pipe, Win32 error 5
    the private copy, writing to the profile nothing written (still confined)

    So the installation key does separate the sections. But that was only the first wall.

    2. Named pipes in general are fine

    Under the same sbx run, PowerShell creates a named pipe (NamedPipeServerStream) without trouble. The failure is specific to msys.

    3. The cause, in msys2-runtime (msys2-3.6.11)

    • sigproc.cc:527: the signal pipe is created with sec_user_nih(…, cygheap->user.sid()).
    • sec/helper.cc:547-590 (sec_acl): that DACL grants GENERIC_ALL to the user's SID (plus the saved original SID) and Administrators. It adds no Everyone, no logon SID, nothing else.
    • fhandler/pipe.cc:1149: the write end is then opened with GENERIC_WRITE against that DACL.

    A write-restricted token passes a write check only if a restricting SID (the run SID, Everyone, the logon SID) is also granted. None is, so msys refuses itself. Every msys object built through sec_user has this property, whatever its name. Name isolation (BNO, a private runtime) cannot fix it on its own.

    Directions this leaves

    1. Run as the sbx-offline account, with the account's own SID added to the restricting SIDs.
      • msys then builds its DACLs for that account, and the restricted check passes for exactly what is granted to that account.
      • Your files aren't granted to it, and directories open to BUILTIN\Users (ProgramData and the like) stay closed, because the account SID itself isn't granted there.
      • Its per-user section is <sbx-offline SID>.1, never yours.
      • Costs: it needs sbx setup-windows (admin, once), the same step insbx: a ready-made policy for running Claude Code (and what the Windows offline account must read) #40 needs. Two simultaneous runs share the account, so each could write what the other was granted while both run.
      • Not built; measurable once the account exists.
    2. Plain unrestricted sbx-offline. msys works, but confinement falls back to what the account can write, BUILTIN\Users locations included. Weaker than (1); not recommended.
    3. An upstream msys2-runtime change. When the process token is write-restricted, add its restricting SIDs to sec_acl. That would fix it for every sandbox, but it is in someone else's hands and on their timeline.
    4. AppContainer: ruled out on the same reading. Its access check needs the package SID (or ALL APPLICATION PACKAGES) granted, and sec_acl grants neither.
    5. SBX_BACKEND=wsl, as today.
  3. blockmandev commented on Oct 24, 2026

    @blockmandev
    MemberAuthor

    Decided: run Git Bash as the sbx-offline account, with that account's SID added to the restricting SIDs. Only a policy that asks for it (msys = true) gets this, and such a run is exclusive: it waits until no other offline run is live, and blocks new ones while it runs.

    Why it must be exclusive: today two runs can't write each other's folders, pinned by tests/escape_windows.rs concurrent_deny_runs_share_a_root_and_cannot_write_each_others. That holds because only the per-run SID tells runs apart; the account's ACE is shared (windows/acl.rs Lease::acquire, live_account_grants). Trusting the account SID during overlapping runs would break it.

    Limits of the design:

    • It only applies with network deny or proxy. With allow, the command runs as your user, not the account (lib.rs Sandbox::command).

    One risk to measure before shipping:

    • What: files that earlier offline runs created are owned by sbx-offline, and an owner can rewrite a file's permissions. Once the account SID is trusted, a Git Bash run might be able to change, and so reach, files that runs in other projects left behind.
    • To settle: whether the restricted access check grants owner rights in that case must be measured, then fixed or ruled out.

    Blocked on: sbx setup-windows, run once as admin. No sbx-offline account exists on this machine yet, so neither the fix nor that risk can be measured.

  4. blockmandev commented on Oct 24, 2026

    @blockmandev
    MemberAuthor

    Unblocked: the owner ran \sbx setup-windows\ on this machine (sbx-offline account and 7 firewall filters created). \sbx check --net deny\ now reports network deny as enforced natively. Next, measured first and then built: the owner-rights risk on files the account owns, and Git Bash and Node child processes running as the account with its SID trusted (which also lifts the #68 limit).

  5. changed the title [-]sbx on Windows: Git Bash / msys can't start natively (error 5); fix with a private object namespace per run[/-] [+]insbx on Windows: Git Bash / msys can't start natively (error 5); fix with a private object namespace per run[/+] on Nov 13, 2026
  6. blockmandev commented on Nov 13, 2026

    @blockmandev
    MemberAuthor

    Measured today (Windows 11, insbx-offline set up, Git for Windows bash), on a throwaway build that can add the token's own user SID to the restricting SIDs:

    run, --net deny -w . (runs as insbx-offline) result
    bash -c "echo ok > x && cat x", as today couldn't create signal pipe, Win32 error 5 (as predicted above)
    same, account SID trusted works: ok, x written, whoami = insbx-offline
    cmd writing into another folder an earlier deny run was granted (a kept grant, since #85) refused: Access is denied
    same, account SID trusted written. The escape is real.

    The account SID is what makes msys work, but since #85 keeps the offline account's grants, trusting that SID opens every folder any earlier network-deny run used.

    Decided (owner): a separate account, insbx-msys, used only for runs that need msys:

    • Per-run grants, never kept. Its SID passes the restricted check only on this run's roots and temp directory.
    • One msys run at a time, because the account is shared between runs.
    • An OWNER RIGHTS entry on the roots, so files it created in earlier runs can't be re-permissioned by it.
    • insbx setup-windows creates it, with its own firewall filters. This needs administrator, once.

    Building it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions