Skip to content

Commit 249a6d9

Browse files
Jgratton24claude
andcommitted
fix(desktop): run the WSL install-dir release check as root
ensureWindowsPathReleased spawned the holder scan as the default WSL user. find_holders inspects /proc/<pid>/{cwd,exe,fd,maps}, which the kernel exposes only to the process owner and root, so the unprivileged scan silently got EACCES for any process it did not own. A root- or other-user-owned process holding the install directory was therefore invisible: the check returned RELEASED without ever seeing (let alone killing) it, defeating the guard in exactly the case it exists for. Spawn the probe as `wsl -u root` (WSL grants root with no password, gated by the Windows host) so the scan sees every holder and its kills land on holders it does not own. find_holders is already scoped to processes that reference the install dir, so this cannot reach unrelated processes. Verified on Windows 11 + WSL2 (Ubuntu, mirrored): a root-owned holder with cwd in the install dir that the old code left untouched is now detected and SIGTERM-cleared before the installer handoff. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 0fdb80d commit 249a6d9

2 files changed

Lines changed: 62 additions & 26 deletions

File tree

‎apps/desktop/src/wsl/DesktopWslEnvironment.test.ts‎

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,7 @@ const makeArgvSpawner = (
272272
handler: (argv: readonly string[]) => { readonly stdout?: string; readonly exitCode?: number },
273273
) =>
274274
ChildProcessSpawner.make((command) => {
275-
const argv =
276-
command._tag === "StandardCommand" ? [command.command, ...command.args] : [];
275+
const argv = command._tag === "StandardCommand" ? [command.command, ...command.args] : [];
277276
const result = handler(argv);
278277
return Effect.succeed(
279278
ChildProcessSpawner.makeHandle({
@@ -341,40 +340,29 @@ describe("getNetworkingModeImpl", () => {
341340
Effect.gen(function* () {
342341
expect(yield* getNetworkingModeImpl("Ubuntu")).toEqual("mirrored");
343342
}).pipe(
344-
Effect.provideService(
345-
ChildProcessSpawner.ChildProcessSpawner,
346-
modeSpawner("mirrored\n"),
347-
),
343+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, modeSpawner("mirrored\n")),
348344
),
349345
);
350346

351347
it.effect("parses nat mode", () =>
352348
Effect.gen(function* () {
353349
expect(yield* getNetworkingModeImpl(null)).toEqual("nat");
354-
}).pipe(
355-
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, modeSpawner("nat\n")),
356-
),
350+
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, modeSpawner("nat\n"))),
357351
);
358352

359353
it.effect("reports unknown when wslinfo is missing or fails", () =>
360354
Effect.gen(function* () {
361355
expect(yield* getNetworkingModeImpl(null)).toEqual("unknown");
362356
}).pipe(
363-
Effect.provideService(
364-
ChildProcessSpawner.ChildProcessSpawner,
365-
modeSpawner(undefined, 127),
366-
),
357+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, modeSpawner(undefined, 127)),
367358
),
368359
);
369360

370361
it.effect("reports unknown for unrecognized output", () =>
371362
Effect.gen(function* () {
372363
expect(yield* getNetworkingModeImpl(null)).toEqual("unknown");
373364
}).pipe(
374-
Effect.provideService(
375-
ChildProcessSpawner.ChildProcessSpawner,
376-
modeSpawner("bridged\n"),
377-
),
365+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, modeSpawner("bridged\n")),
378366
),
379367
);
380368
});
@@ -455,6 +443,40 @@ describe("ensureWindowsPathReleasedImpl", () => {
455443
),
456444
);
457445

446+
it.effect("runs the holder scan as root so it can see and kill non-user-owned holders", () =>
447+
Effect.gen(function* () {
448+
// /proc/<pid>/{cwd,exe,fd,maps} are readable only by the process owner
449+
// and root, so an unprivileged scan is blind to holders owned by other
450+
// users (e.g. a root process with cwd in the install dir) and would
451+
// wrongly report RELEASED. The probe must run as root.
452+
const scanArgvs: string[][] = [];
453+
const result = yield* ensureWindowsPathReleasedImpl({
454+
distro: "Ubuntu",
455+
windowsPath: "C:\\Users\\test\\AppData\\Local\\Programs\\t3code",
456+
}).pipe(
457+
Effect.provideService(
458+
ChildProcessSpawner.ChildProcessSpawner,
459+
makeArgvSpawner((argv) => {
460+
if (argv.includes("wslpath")) {
461+
return { stdout: "/mnt/c/Users/test/AppData/Local/Programs/t3code\n", exitCode: 0 };
462+
}
463+
scanArgvs.push([...argv]);
464+
return { stdout: "RELEASED\n", exitCode: 0 };
465+
}),
466+
),
467+
);
468+
expect(result).toBe("released");
469+
expect(scanArgvs).toHaveLength(1);
470+
const argv = scanArgvs[0]!;
471+
const separator = argv.indexOf("--");
472+
const userFlag = argv.indexOf("-u");
473+
// `-u root` must appear before the `--` command separator.
474+
expect(userFlag).toBeGreaterThanOrEqual(0);
475+
expect(argv[userFlag + 1]).toBe("root");
476+
expect(userFlag).toBeLessThan(separator);
477+
}),
478+
);
479+
458480
it.effect("reports unknown when the release-check spawn fails against a reachable distro", () =>
459481
Effect.gen(function* () {
460482
// wslpath just succeeded, so the distro was reachable moments ago — a

‎apps/desktop/src/wsl/DesktopWslEnvironment.ts‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -917,17 +917,31 @@ export const ensureWindowsPathReleasedImpl = (input: {
917917
return yield* Effect.scoped(
918918
Effect.gen(function* () {
919919
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
920+
// Run the probe as root (-u root; WSL grants this with no password,
921+
// gated by the Windows host). find_holders reads /proc/<pid>/{cwd,exe,
922+
// fd,maps}, which the kernel exposes only to the process owner and
923+
// root: as the unprivileged distro user the scan gets EACCES for any
924+
// process it does not own, so a root- or other-user-owned holder of
925+
// the install dir is invisible and the check wrongly reports RELEASED.
926+
// Running as root lets the scan SEE every holder and lets its kills
927+
// land on holders it does not own. find_holders is already scoped to
928+
// processes that actually reference the install dir, so this cannot
929+
// reach unrelated processes.
920930
// Script goes via stdin: wsl.exe re-escapes command-line args on the
921931
// way into Linux, which mangles quoted paths with spaces.
922-
const command = ChildProcess.make("wsl.exe", [...buildDistroArgs(input.distro), "--", "sh"], {
923-
stdin: Stream.encodeText(
924-
Stream.make(buildEnsurePathReleasedScript(shellQuote(linuxPath.value))),
925-
),
926-
stdout: "pipe",
927-
stderr: "ignore",
928-
killSignal: "SIGTERM",
929-
forceKillAfter: PROCESS_TERMINATE_GRACE,
930-
});
932+
const command = ChildProcess.make(
933+
"wsl.exe",
934+
[...buildDistroArgs(input.distro), "-u", "root", "--", "sh"],
935+
{
936+
stdin: Stream.encodeText(
937+
Stream.make(buildEnsurePathReleasedScript(shellQuote(linuxPath.value))),
938+
),
939+
stdout: "pipe",
940+
stderr: "ignore",
941+
killSignal: "SIGTERM",
942+
forceKillAfter: PROCESS_TERMINATE_GRACE,
943+
},
944+
);
931945
// A spawn failure here is NOT evidence that WSL is gone: wslpath just
932946
// succeeded against this distro, so the VM was reachable moments
933947
// ago. Fall through to "unknown" (via the catch below) and let the

0 commit comments

Comments
 (0)