Skip to content

Commit e7485d5

Browse files
committed
fix(daemon-contracts): check and repair state modes on one descriptor
The ownership check and the chmod resolved the state dir path separately, so a path swapped in between could be the one changed. Both now act on the same open descriptor. Also keeps the launch doc on launchDaemonProcess. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016qAu53rMak1bXMuR5U2gF4
1 parent 4a275d7 commit e7485d5

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

‎packages/daemon-contracts/src/daemon-registration-owner.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,16 @@ export function createOwnedReplayStateDir(): OwnedReplayStateDir {
276276
return owned;
277277
}
278278

279-
/** Launches and monitors the actual child before recording its private-directory authority. */
280279
/**
281-
* Whether this process may repair the mode of a state path: only its owner can chmod it, and a
282-
* state dir another user set up keeps the mode that user chose. Windows has no POSIX owner.
280+
* Repairs the mode of an open state path only when this process owns it: only its owner can chmod
281+
* it, and a state dir another user set up keeps the mode that user chose. Checking and changing the
282+
* same descriptor means a path swapped in between is never the one changed.
283283
*/
284-
function isOwnedByCurrentUser(stat: fs.Stats): boolean {
285-
return process.getuid !== undefined && stat.uid === process.getuid();
284+
function repairOwnedMode(fd: number, mode: number): void {
285+
if (fs.fstatSync(fd).uid === process.getuid?.()) fs.fchmodSync(fd, mode);
286286
}
287287

288+
/** Launches and monitors the actual child before recording its private-directory authority. */
288289
export function launchDaemonProcess(
289290
input: Readonly<{
290291
paths: DaemonPaths;
@@ -304,12 +305,18 @@ export function launchDaemonProcess(
304305
});
305306
fs.mkdirSync(input.paths.baseDir, { recursive: true, mode: 0o700 });
306307
// A mode applies only at creation: an older launcher left these at the umask default.
307-
if (isOwnedByCurrentUser(fs.statSync(input.paths.baseDir))) {
308-
fs.chmodSync(input.paths.baseDir, 0o700);
308+
// Windows has no POSIX owner and cannot open a directory, so it keeps the creation mode alone.
309+
if (process.getuid) {
310+
const dirFd = fs.openSync(input.paths.baseDir, 'r');
311+
try {
312+
repairOwnedMode(dirFd, 0o700);
313+
} finally {
314+
fs.closeSync(dirFd);
315+
}
309316
}
310317
const logFd = fs.openSync(input.paths.logPath, 'a', 0o600);
311318
try {
312-
if (isOwnedByCurrentUser(fs.fstatSync(logFd))) fs.fchmodSync(logFd, 0o600);
319+
repairOwnedMode(logFd, 0o600);
313320
const monitored = runCmdDetachedMonitored(process.execPath, input.args, {
314321
cwd: input.cwd,
315322
env: {

0 commit comments

Comments
 (0)