fix(agent): Windows sandboxed commands see Beanstalk's work folder as a drive of its own #46

Merged
HumanBean merged 1 commit from HumanBean/Beanstalk:fix/win-work-drive into main 2026-11-16 05:51:05 +00:00
Owner

Why

No Windows job gets past building main. The compiler, clang-23.exe, starts in the sandbox and then exits at once with "<its path>: Permission denied", even for --version. That happens in LLVM's own startup code, and I've checked the source:

  • InitLLVM calls GetCommandLineArguments → GetExecutableName (llvm/lib/Support/Windows/Process.inc), which calls GetModuleFileNameW and then GetLongPathNameW on the compiler's own path.
  • When that fails, InitLLVM exits through ExitOnError with the banner argv[0] + ": ", which is the line we see.

#38's probe on futondestroyer's PC (rp_f4df5d54ac932b34, Longshot #799, nightly-83) shows why GetLongPathNameW fails with error 5: the sandbox may not look up any folder above the ones it was given.

Depth Lookup Needs a listing of Result
1 Users C:\ refused
2 <you> C:\Users refused
3–7 AppData, Local, Beanstalk, work, tools the profile's folders, the data folder refused
8–10 llvm-mingw, bin, clang-23.exe the folders the sandbox was given ok

C:\ and C:\Users can't be granted without an administrator, so no grant on the user's own folders alone can fix this.

What changed

The agent gives sandboxed commands its data folder as a drive letter of its own. A sandboxed command then sees the compiler at Q:\tools\llvm-mingw\bin\clang-23.exe. The only folder above the folders it was given is the drive's root, and that folder gets a listing.

  • WinWorkDrive (Core/WindowsNative.cs): maps the data folder with DefineDosDevice for the agent's logon session, as subst does, with no administrator. It reuses a mapping an earlier run left, and removes the mapping when the agent quits (AgentService.StopAsync).
  • WinDrive (Core/WindowsPlan.cs, pure, checked on Linux):
    • Pick takes the letter already mapped to the folder, else the last free one from Z: down.
    • Rewrite turns each path under the data folder into the drive's path, inside arguments and path lists too, regardless of case. A folder whose name only starts the same is left alone.
  • WinAccess.List: the drive's root gets a listing of that folder only (FILE_LIST_DIRECTORY, FILE_READ_ATTRIBUTES, SYNCHRONIZE). It's not inherited, and it gives no access to file data. The names there are the agent's own folders: tools, longshot, bases, jobs, ….
  • WinSandbox.Prepare rewrites each command's program, arguments, working folder and environment to the drive's paths. Grants still go on the real folders.
  • A check before using the drive. Whether a sandboxed process sees the drive at all hasn't been proven. So the first command of each sandbox mode runs cmd.exe /c if exist Q:\ … in the sandbox. If that doesn't find the drive, or no letter is free, or BEANSTALK_NO_WORK_DRIVE=1 is set, commands see the real paths as before. The result is kept per sandbox mode, because the runner account is another Windows user that doesn't see the agent's mapping.
  • Reporting. Every sandboxed command gets BEANSTALK_WORK_DRIVE: the letter, or off: <why>. The temp probe prints it and LONGSHOT_MINGW, so the next report says whether the drive was used. #38's lookup lines then walk the drive's path.

Testing

  • Agent checks: 1701 passed. The new Windows checks cover:
    • the listing's mask, and that a listing isn't inherited;
    • picking the letter: reusing one, taking the last free one, none free;
    • rewriting a path, the folder itself, path lists without regard to case, a lookalike folder, and an argument.
  • dotnet build agent/App -c Release: 0 warnings.
  • Not tested on Windows, because I'm on Linux. Open questions the next Windows job answers:
    • whether the AppContainer sees the drive: BEANSTALK_WORK_DRIVE in the probe lines, and the check's reason when it's off;
    • whether GetLongPathNameW then succeeds: @@probe longpath clang ok, and the spawn lines exiting 0;
    • whether main builds from there.
## Why No Windows job gets past building `main`. The compiler, `clang-23.exe`, starts in the sandbox and then exits at once with "`<its path>`: Permission denied", even for `--version`. That happens in LLVM's own startup code, and I've checked the source: - `InitLLVM` calls `GetCommandLineArguments` → `GetExecutableName` (`llvm/lib/Support/Windows/Process.inc`), which calls `GetModuleFileNameW` and then `GetLongPathNameW` on the compiler's own path. - When that fails, `InitLLVM` exits through `ExitOnError` with the banner `argv[0] + ": "`, which is the line we see. #38's probe on futondestroyer's PC ([rp_f4df5d54ac932b34](https://beanstalk.magicbean.dev/reports/rp_f4df5d54ac932b34), Longshot #799, nightly-83) shows why `GetLongPathNameW` fails with error 5: the sandbox may not look up any folder above the ones it was given. | Depth | Lookup | Needs a listing of | Result | |---|---|---|---| | 1 | `Users` | `C:\` | refused | | 2 | `<you>` | `C:\Users` | refused | | 3–7 | `AppData`, `Local`, `Beanstalk`, `work`, `tools` | the profile's folders, the data folder | refused | | 8–10 | `llvm-mingw`, `bin`, `clang-23.exe` | the folders the sandbox was given | ok | `C:\` and `C:\Users` can't be granted without an administrator, so no grant on the user's own folders alone can fix this. ## What changed The agent gives sandboxed commands its data folder as a drive letter of its own. A sandboxed command then sees the compiler at `Q:\tools\llvm-mingw\bin\clang-23.exe`. The only folder above the folders it was given is the drive's root, and that folder gets a listing. - `WinWorkDrive` (`Core/WindowsNative.cs`): maps the data folder with `DefineDosDevice` for the agent's logon session, as `subst` does, with no administrator. It reuses a mapping an earlier run left, and removes the mapping when the agent quits (`AgentService.StopAsync`). - `WinDrive` (`Core/WindowsPlan.cs`, pure, checked on Linux): - `Pick` takes the letter already mapped to the folder, else the last free one from Z: down. - `Rewrite` turns each path under the data folder into the drive's path, inside arguments and path lists too, regardless of case. A folder whose name only starts the same is left alone. - `WinAccess.List`: the drive's root gets a listing of that folder only (`FILE_LIST_DIRECTORY`, `FILE_READ_ATTRIBUTES`, `SYNCHRONIZE`). It's not inherited, and it gives no access to file data. The names there are the agent's own folders: `tools`, `longshot`, `bases`, `jobs`, …. - `WinSandbox.Prepare` rewrites each command's program, arguments, working folder and environment to the drive's paths. Grants still go on the real folders. - **A check before using the drive.** Whether a sandboxed process sees the drive at all hasn't been proven. So the first command of each sandbox mode runs `cmd.exe /c if exist Q:\ …` in the sandbox. If that doesn't find the drive, or no letter is free, or `BEANSTALK_NO_WORK_DRIVE=1` is set, commands see the real paths as before. The result is kept per sandbox mode, because the runner account is another Windows user that doesn't see the agent's mapping. - **Reporting.** Every sandboxed command gets `BEANSTALK_WORK_DRIVE`: the letter, or `off: <why>`. The temp probe prints it and `LONGSHOT_MINGW`, so the next report says whether the drive was used. #38's lookup lines then walk the drive's path. ## Testing - Agent checks: 1701 passed. The new Windows checks cover: - the listing's mask, and that a listing isn't inherited; - picking the letter: reusing one, taking the last free one, none free; - rewriting a path, the folder itself, path lists without regard to case, a lookalike folder, and an argument. - `dotnet build agent/App -c Release`: 0 warnings. - **Not tested on Windows**, because I'm on Linux. Open questions the next Windows job answers: - whether the AppContainer sees the drive: `BEANSTALK_WORK_DRIVE` in the probe lines, and the check's reason when it's off; - whether `GetLongPathNameW` then succeeds: `@@probe longpath clang ok`, and the spawn lines exiting 0; - whether `main` builds from there.
fix(agent): Windows sandboxed commands see Beanstalk's work folder as a drive of its own
Some checks are pending
CI / Coordinator tests (pull_request) Waiting to run
CI / Agent build and checks (pull_request) Waiting to run
CI / Coordinator image (pull_request) Waiting to run
CI / Conventional Commit title (pull_request) Successful in 3s
CI / Workflow lint (actionlint) (pull_request) Successful in 3s
09406e822f
LLVM's start (InitLLVM -> GetCommandLineArguments -> GetExecutableName) expands
its own path with GetLongPathNameW and exits with "<argv0>: <error>" when that
fails. In the AppContainer every look-up of a folder above the ones it was
given is refused (#38's probe on futondestroyer's PC, Longshot #799: C:\,
C:\Users, the profile, ..., work all refused), so clang-23.exe stopped with
"Permission denied" and no Windows job got past building main.

The agent now maps its data folder to a free drive letter for its logon
session (DefineDosDevice, as subst does: no administrator; a mapping left by
an earlier run is used again; removed when the agent quits) and gives the
sandbox a listing of that folder only. Each sandboxed command's program,
arguments, folder and environment see paths under the data folder through
the drive (Q:\tools\llvm-mingw\...), so the only folders above are the
drive's root and folders it was given.

Whether a sandboxed process sees the drive is checked once per sandbox mode:
cmd.exe started in the sandbox must find the drive's root. If not (or no
letter is free, or BEANSTALK_NO_WORK_DRIVE=1), commands see the real paths
as before. Every sandboxed command is told BEANSTALK_WORK_DRIVE (the letter,
or "off" and why), and the probe prints it.

Assisted-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Beanstalk!46
No description provided.