mxc icon indicating copy to clipboard operation
mxc copied to clipboard

Process container: `SetCurrentDirectory` into a read-only path fails (FILE_TRAVERSE denied for directory opens)

Open bluecmd opened this issue 1 month ago • 3 comments

Summary

Inside a processcontainer (process security environment, PSEC contract 1.1, Windows 11 25H2 26200.9550 / bfs.sys 10.0.26100.9549), a directory that is granted read-only can be listed and stat'ed but cannot be opened with FILE_TRAVERSE when the open carries FILE_DIRECTORY_FILE. That is exactly the open SetCurrentDirectoryW performs, so cd into a read-only directory fails with ERROR_ACCESS_DENIED (and chdir of every runtime that wraps it: cmd.exe, PowerShell's start location, busybox). The same right is allowed when the open does not carry FILE_DIRECTORY_FILE, and FILE_LIST_DIRECTORY with FILE_DIRECTORY_FILE is allowed, so this is not an intended restriction but an asymmetric access mask. Enumerate-only paths (enumeratePaths) deny FILE_TRAVERSE in every form.

Repro

Any read-only grant, any directory under it, then from inside the container:

NtOpenFile(dir, FILE_TRAVERSE,              FILE_DIRECTORY_FILE)                -> STATUS_ACCESS_DENIED
NtOpenFile(dir, FILE_TRAVERSE | SYNCHRONIZE, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT) -> STATUS_ACCESS_DENIED  (= SetCurrentDirectoryW)
NtOpenFile(dir, FILE_TRAVERSE,              0)                                  -> STATUS_SUCCESS
NtOpenFile(dir, FILE_LIST_DIRECTORY,        FILE_DIRECTORY_FILE)                -> STATUS_SUCCESS
NtOpenFile(dir, FILE_READ_ATTRIBUTES,       FILE_DIRECTORY_FILE)                -> STATUS_SUCCESS
cmd.exe /c "cd /d <read-only dir>"                                              -> "Access is denied."

With a read-write grant all of them succeed.

Where it is

bfs!BfsQueryAccessOnly(int kind, PFLT_CALLBACK_DATA) decides whether an open under a read-only grant is query-only. kind is 2 for opens with FILE_DIRECTORY_FILE, else 1. It tests OriginalDesiredAccess against:

cmp   esi, 2
jnz   file
test  ebx, 0FFE1FF7Eh          ; directory open: allowed 0x001E0081 (LIST, READ_ATTRIBUTES, READ_CONTROL, SYNCHRONIZE)
jmp   done
file:
call  Feature_BfsQueryOnlyAllowExecute…
test  ebx, 0FFE1FF56h          ; file open, feature on: allowed 0x001E00A9 (+ READ_DATA, READ_EA, EXECUTE)
test  ebx, 0FFE1FF76h          ; file open, feature off
setz  al

FILE_EXECUTE and FILE_TRAVERSE share bit 0x20. The BfsQueryOnlyAllowExecute feature added it to the file mask only. Verified with a kernel debugger: breakpoint on the function, our probe's directory open arrives with rcx = 2, OriginalDesiredAccess = 0x20, Create.Options = 0x01000001, the function returns 0 and the caller (BfsCheckAndApplyPolicy+0xa0d, from BfsPreCreateOperation) completes the create with STATUS_ACCESS_DENIED. The enumerate-only path uses a second constant (0xFFEDFF76) with the same omission.

Expected

Traverse grants nothing a listing does not (the contained token even holds SeChangeNotifyPrivilege, which bypasses traverse checks elsewhere), so the directory mask should allow 0x20: 0xFFE1FF5E instead of 0xFFE1FF7E, and likewise for enumerate-only paths. Until then any tool that changes directory into a read-only input tree inside a container fails.

Why it matters

Build sandboxes (Bazel's windows-sandbox on PSEC) grant inputs read-only and run tools with the execroot or the runfiles tree as the working directory. Every test wrapper chdirs into its runfiles directory; the only workaround is to grant such directories read-write, which weakens the sandbox for no security gain.

bluecmd avatar Oct 12 '26 15:10 bluecmd

Thank you for filing this. We expect a fix to start rolling out at the end of November with a Windows update. I'll update this thread with a KB article when we have one available.

daniedud-MSFT avatar Nov 02 '26 22:11 daniedud-MSFT

Same root cause, one more bit: the directory mask 0x001E0081 also omits FILE_READ_EA (0x8), which the file mask allows. The fix proposed above (0xFFE1FF5E) adds only 0x20, so directory opens that request FILE_READ_EA will still be denied.

Bun opens every directory (readdir and its module resolver) with READ_CONTROL | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE | FILE_TRAVERSE | FILE_LIST_DIRECTORY and FILE_DIRECTORY_FILE, so under any read-only grant fs.readdirSync fails with EPERM and module resolution fails with Cannot read directory: EPERM. Node, cmd and PowerShell are unaffected because they request only FILE_LIST_DIRECTORY | SYNCHRONIZE.

Measured with NtCreateFile from inside a container (mxc-sdk 1.0.1, Windows 11 26100), all 31 combinations of LIST / READ_EA / TRAVERSE / READ_ATTRIBUTES / READ_CONTROL (+ SYNCHRONIZE) on a read-only directory:

  • with FILE_DIRECTORY_FILE: every combination containing FILE_READ_EA or FILE_TRAVERSE → STATUS_ACCESS_DENIED; all others succeed
  • without FILE_DIRECTORY_FILE: all succeed

FILE_READ_EA is part of FILE_GENERIC_READ, so the directory mask should allow it too: 0xFFE1FF56 (allowed 0x001E00A9) rather than 0xFFE1FF5E. Bun-side report: oven-sh/bun#48581.

apoc avatar Nov 11 '26 12:11 apoc

One more program this stops, and a measurement that agrees with the comment above about FILE_READ_EA.

Zig's standard library opens every directory with NtCreateFile, FILE_OPEN, STANDARD_RIGHTS_READ | SYNCHRONIZE | FILE_READ_ATTRIBUTES | FILE_READ_EA | FILE_TRAVERSE (plus FILE_LIST_DIRECTORY when iterating) and FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT. Under a read-only grant that is STATUS_ACCESS_DENIED, so zig.exe installed under a read-only path can't open its own lib folder: "unable to find zig installation directory", or with ZIG_LIB_DIR set, "unable to open zig lib directory …: AccessDenied".

Measured on Windows 11 Pro 26H2 build 26300.9974, wxc-exec.exe 1.0.1, processcontainer, readonlyPaths: ["C:\\"], on a directory under it:

Access Create options Status
the mask above with FILE_DIRECTORY_FILE 0xC0000022
the mask above without FILE_DIRECTORY_FILE 0x00000000
the mask less FILE_TRAVERSE with FILE_DIRECTORY_FILE 0xC0000022
the mask less FILE_READ_EA with FILE_DIRECTORY_FILE 0xC0000022

So either bit alone is enough to be refused, and a fix that adds only FILE_TRAVERSE to the directory mask would leave Zig refused, as it would Bun.

A way round until then, for a folder the caller owns: grant ALL APPLICATION PACKAGES read and execute in the folder's own ACL (icacls FOLDER /grant *S-1-15-2-1:(OI)(CI)RX). The same open then succeeds, as it does for C:\Windows.

altohq avatar Nov 13 '26 21:11 altohq