From e8095b01c48725524922d307004108dc4480b031 Mon Sep 17 00:00:00 2001 From: TheInfamousToTo <72304665+TheInfamousToTo@users.noreply.github.com> Date: Thu, 10 Sep 2026 14:42:47 +0300 Subject: [PATCH 1/2] Coach: chown of the job directory locked the parent out of it Every Coach run failed instantly on Kubernetes -- "Claude Code native binary ... exists but failed to launch. This usually means the binary does not match this system's libc" -- with a musl binary on a musl host, which the SDK's own --version printed happily one shell command earlier. The message is the Agent SDK's guess at any spawn failure. The real errno was EACCES, and the cause is the job directory rather than the binary: libuv chdir()s into cwd BEFORE dropping to the child's uid, so handing the directory to `coach` locks out the very process that has to enter it first. mkdtemp creates 0700, and `drop: [ALL]` leaves root without CAP_DAC_OVERRIDE to ignore that with -- so the child never starts. cf7e4cc fixed the same class of bug for the fixture provider by skipping the chown when the child stays root. It does not help here: claude and codex do drop, and the chdir still happens before they do. The directory now stays owned by the server and `coach\' reaches it through the group (0770, coach gid). The child writes, the parent still chdirs and still cleans up, nothing else on the container can read it. chmod before chown, while the server is still the owner: CAP_FOWNER is not in the set either, so the other order fails with EPERM. Verified in the running container: chown -> EACCES, group handover -> exit 0, child writes, parent removes. --- api/coach/jobs.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/api/coach/jobs.js b/api/coach/jobs.js index 9bddc27..91bb109 100644 --- a/api/coach/jobs.js +++ b/api/coach/jobs.js @@ -294,7 +294,7 @@ async function execute(job) { const env = cfgStore.jobEnv(jobDir || os.tmpdir(), cfgStore.credentialFor(job.uid)); try { const ids = jobDir && unprivilegedIds(); - if (ids) fs.chownSync(jobDir, ids.uid, ids.gid); + if (ids) shareJobDir(jobDir, ids); const attempt = await runPipeline({ adapter, cfg, kind: job.kind, payload, model: cfgStore.modelFor(cfg), timeoutMs: TIMEOUT_MS, @@ -342,6 +342,25 @@ export function resolvePending(uid, { accepted = [], rejected = [], dismissed = /* ---------- admin test + boot recovery ---------- */ /** A2's "Test the Coach": the real adapter, a trivial round-trip, no user data anywhere near it. */ +/* Give the unprivileged `coach` user its job directory without locking this process out of it. + * + * The obvious move is chown(jobDir, coach) — and it breaks the spawn outright. libuv chdir()s + * into cwd BEFORE it drops to the child's uid, so the parent still has to be able to enter the + * directory it just gave away; mkdtemp creates 0700, and a container started with `drop: [ALL]` + * has no CAP_DAC_OVERRIDE for root to ignore that with. The child never starts and node reports + * EACCES, which the Agent SDK renders as "the native binary failed to launch … does not match + * this system's libc" — a guess, and a misleading one. + * + * So the directory stays owned by this process and `coach` reaches it through the group: 0770 + * with the coach gid. The child can write, the parent can still chdir and still clean up + * afterwards, and nobody else on the container can read it. chmod before chown, while this + * process is still the owner — CAP_FOWNER is not in the capability set either. + */ +function shareJobDir(jobDir, ids) { + fs.chmodSync(jobDir, 0o770); + fs.chownSync(jobDir, process.getuid ? process.getuid() : 0, ids.gid); +} + export async function testRun() { const cfg = cfgStore.load(); const adapter = adapterFor(cfg.provider); @@ -354,7 +373,7 @@ export async function testRun() { // a credential that belongs to a profile. const env = cfgStore.jobEnv(jobDir || os.tmpdir(), cfgStore.credentialFor(cfgStore.boundUidFor(cfg))); const ids = jobDir && unprivilegedIds(); - if (ids) fs.chownSync(jobDir, ids.uid, ids.gid); + if (ids) shareJobDir(jobDir, ids); const check = await adapter.check(cfg, env); if (!check.ok) return { ok: false, error: check.error || 'the provider runtime could not be run' }; const r = await adapter.invoke({ From 1639ffaf9dd18727dff83ab697bf1c1a21d87230 Mon Sep 17 00:00:00 2001 From: TheInfamousToTo <72304665+TheInfamousToTo@users.noreply.github.com> Date: Thu, 10 Sep 2026 14:49:13 +0300 Subject: [PATCH 2/2] 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. --- api/coach/jobs.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/api/coach/jobs.js b/api/coach/jobs.js index 91bb109..663d8f0 100644 --- a/api/coach/jobs.js +++ b/api/coach/jobs.js @@ -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()); } }