Coach: the job directory could not be cleaned up either

Same capability, other direction. The child writes as `coach`, so its own
directories come out coach-owned -- and this process cannot unlink inside them
without CAP_DAC_OVERRIDE. fs.rmSync then throws EACCES from a `finally`,
which turns a run that actually succeeded into a failed one:

  EACCES: permission denied, rmdir '/tmp/coach-test-XXXX/.claude/backups'

The child's user now clears its own files (argv array, no shell, paths from
this module's own mkdtemp) and the server removes the directory it still owns.
Best effort on both halves: a leaked temp directory beats a failed job.
This commit is contained in:
TheInfamousToTo committed 2026-09-10 15:05:34 +03:00
1 parent e8095b01c4
commit 1639ffaf9d
1 file changed
+26 -2
+26 -2
View File
@@ -12,6 +12,7 @@
* up, cannot run forever, and cannot lie about what happened when the container restarts.
*/
import fs from 'node:fs';
import { spawnSync } from 'node:child_process';
import os from 'node:os';
import path from 'node:path';
import crypto from 'node:crypto';
@@ -320,7 +321,7 @@ async function execute(job) {
};
return finish(job, { outcome: 'ready', pending });
} finally {
if (jobDir) fs.rmSync(jobDir, { recursive: true, force: true });
if (jobDir) removeJobDir(jobDir, unprivilegedIds());
}
}
@@ -361,6 +362,29 @@ function shareJobDir(jobDir, ids) {
fs.chownSync(jobDir, process.getuid ? process.getuid() : 0, ids.gid);
}
/* Remove a job directory whose contents belong to somebody else.
*
* The child writes as `coach` and its own directories come out 0700/0755 coach-owned — this
* process cannot unlink inside them without CAP_DAC_OVERRIDE, which is the same capability the
* handover above is written to avoid needing. Left to `fs.rmSync` it throws EACCES from a
* `finally`, turning a completed run into a failed one.
*
* So the child's user clears its own files, and this process removes the directory it still
* owns. Best effort throughout: a leaked temp directory is a worse outcome than a failed job
* only in the sense that it is not one.
*/
function removeJobDir(jobDir, ids) {
if (ids) {
try {
const mine = fs.readdirSync(jobDir).map(n => path.join(jobDir, n));
// argv array, no shell — the same rule the provider spawn follows, and these paths are
// this module's own mkdtemp output rather than anything a user chose.
if (mine.length) spawnSync('/bin/rm', ['-rf', ...mine], { uid: ids.uid, gid: ids.gid, stdio: 'ignore' });
} catch { /* fall through: the removal below is still worth attempting */ }
}
try { fs.rmSync(jobDir, { recursive: true, force: true }); } catch { /* leaked, not fatal */ }
}
export async function testRun() {
const cfg = cfgStore.load();
const adapter = adapterFor(cfg.provider);
@@ -391,7 +415,7 @@ export async function testRun() {
}
return { ok: true, version: check.version };
} finally {
if (jobDir) fs.rmSync(jobDir, { recursive: true, force: true });
if (jobDir) removeJobDir(jobDir, unprivilegedIds());
}
}