Skip to content

Commit e02995f

Browse files
committed
Close D2 profile and validation trust gaps
1 parent 124800b commit e02995f

5 files changed

Lines changed: 123 additions & 23 deletions

File tree

‎agents/container/image.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function buildAgentImage(timeoutMs = 10 * 60_000): string {
2323
const inspect = JSON.parse(execFileSync('docker', ['image', 'inspect', AGENT_IMAGE], {
2424
encoding: 'utf8', timeout: remaining(), stdio: ['ignore', 'pipe', 'pipe'],
2525
}))[0] as { Id?: string; Config?: { User?: string; Labels?: Record<string, string> } };
26+
remaining();
2627
const labels = inspect.Config?.Labels ?? {};
2728
if (!inspect.Id?.startsWith('sha256:') || inspect.Config?.User !== '10001:10001'
2829
|| labels['org.opencontainers.image.base.name'] !== BASE_IMAGE

‎agents/container/probe.sh‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/sh
22
set -eu
3+
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
4+
export PATH
35

46
fail() { printf 'codeboost isolation probe: %s\n' "$1" >&2; exit 78; }
57
mount_options() { findmnt --noheadings --output OPTIONS --target "$1" 2>/dev/null || fail "missing mount: $1"; }

‎agents/container/profile.ts‎

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createHash } from 'node:crypto';
2-
import { lstatSync, readdirSync, realpathSync } from 'node:fs';
2+
import { closeSync, constants, fstatSync, lstatSync, openSync, readFileSync, readdirSync, realpathSync } from 'node:fs';
33
import type { InvocationInput, Phase } from '../contract.ts';
44

55
export interface TaskFilesystems {
@@ -32,6 +32,65 @@ export interface ProfileOptions {
3232
readonly claudeToken?: string;
3333
}
3434

35+
interface FileIdentity {
36+
readonly path: string;
37+
readonly dev: number;
38+
readonly ino: number;
39+
readonly mode: number;
40+
readonly nlink: number;
41+
readonly size: number;
42+
readonly mtimeMs: number;
43+
readonly digest: string;
44+
}
45+
interface ProfileIdentity { readonly inputDirectory: string; readonly schema: FileIdentity; readonly auth?: FileIdentity }
46+
const identities = new WeakMap<ContainerProfile, ProfileIdentity>();
47+
48+
const captureFile = (path: string, kind: string): FileIdentity => {
49+
let fd: number | undefined;
50+
try {
51+
fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
52+
const before = fstatSync(fd);
53+
if (!before.isFile() || before.nlink !== 1 || before.size > 1024 * 1024)
54+
throw new Error(`${kind} must be a bounded, unlinked regular file.`);
55+
const content = readFileSync(fd);
56+
const after = fstatSync(fd);
57+
if (before.dev !== after.dev || before.ino !== after.ino || before.size !== after.size
58+
|| before.mtimeMs !== after.mtimeMs || before.ctimeMs !== after.ctimeMs)
59+
throw new Error(`${kind} changed while its identity was captured.`);
60+
return Object.freeze({ path, dev: after.dev, ino: after.ino, mode: after.mode, nlink: after.nlink,
61+
size: after.size, mtimeMs: after.mtimeMs, digest: createHash('sha256').update(content).digest('hex') });
62+
} finally { if (fd !== undefined) closeSync(fd); }
63+
};
64+
const sameFile = (actual: FileIdentity, expected: FileIdentity) => actual.path === expected.path
65+
&& actual.dev === expected.dev && actual.ino === expected.ino && actual.mode === expected.mode
66+
&& actual.nlink === expected.nlink && actual.size === expected.size && actual.mtimeMs === expected.mtimeMs
67+
&& actual.digest === expected.digest;
68+
const captureInput = (directory: string): ProfileIdentity => {
69+
const stat = lstatSync(directory);
70+
if (!stat.isDirectory() || stat.isSymbolicLink() || (stat.mode & 0o005) !== 0o005)
71+
throw new Error('Schema input directory must be a container-readable real directory.');
72+
const canonical = mountSource(realpathSync(directory), 'Schema input');
73+
const entries = readdirSync(canonical);
74+
if (entries.length !== 1 || entries[0] !== 'schema.json')
75+
throw new Error('Schema input must contain only one bounded, unlinked regular schema.json file.');
76+
const schema = captureFile(`${canonical}/schema.json`, 'Schema input');
77+
if ((schema.mode & 0o004) === 0) throw new Error('Schema input must be container-readable.');
78+
return Object.freeze({ inputDirectory: canonical, schema });
79+
};
80+
81+
/** Internal authenticity and host-file revalidation used at every launch boundary. */
82+
export function assertContainerProfile(profile: ContainerProfile): void {
83+
const expected = identities.get(profile);
84+
if (!expected) throw new Error('Container profile was not created by the trusted profile builder.');
85+
const actual = captureInput(expected.inputDirectory);
86+
if (actual.inputDirectory !== expected.inputDirectory || !sameFile(actual.schema, expected.schema))
87+
throw new Error('Schema input changed after the profile was captured.');
88+
if (expected.auth) {
89+
const auth = captureFile(expected.auth.path, 'Codex auth');
90+
if (!sameFile(auth, expected.auth)) throw new Error('Codex auth changed after the profile was captured.');
91+
}
92+
}
93+
3594
const safeName = (value: string) => {
3695
const prefix = value.replace(/[^a-zA-Z0-9_.-]/g, '-').slice(0, 24);
3796
return `${prefix}-${createHash('sha256').update(value).digest('hex').slice(0, 16)}`;
@@ -49,14 +108,8 @@ export function createContainerProfile(options: ProfileOptions): ContainerProfil
49108
throw new Error('Container command must be a complete literal argv array.');
50109
if (!/^sha256:[0-9a-f]{64}$/.test(options.imageId))
51110
throw new Error('Container profile requires the immutable built image ID.');
52-
const inputStat = options.inputDirectory ? lstatSync(options.inputDirectory) : undefined;
53-
if (!inputStat?.isDirectory() || (inputStat.mode & 0o005) !== 0o005) throw new Error('Schema input directory must be container-readable.');
54-
const inputDirectory = mountSource(realpathSync(options.inputDirectory), 'Schema input');
55-
const entries = readdirSync(inputDirectory);
56-
const schema = entries.length === 1 && entries[0] === 'schema.json' ? lstatSync(`${inputDirectory}/schema.json`) : undefined;
57-
if (!schema?.isFile() || schema.isSymbolicLink() || schema.nlink !== 1 || schema.size > 1024 * 1024
58-
|| (schema.mode & 0o004) === 0)
59-
throw new Error('Schema input must contain only one bounded, unlinked regular schema.json file.');
111+
const inputIdentity = captureInput(options.inputDirectory);
112+
const inputDirectory = inputIdentity.inputDirectory;
60113
if (invocation.vendor === 'codex' && (!options.codexAuthFile || options.claudeToken))
61114
throw new Error('Codex requires only its auth file.');
62115
if (invocation.vendor === 'claude' && (!options.claudeToken || options.codexAuthFile))
@@ -68,10 +121,7 @@ export function createContainerProfile(options: ProfileOptions): ContainerProfil
68121
if (options.codexAuthFile && !lstatSync(options.codexAuthFile).isFile())
69122
throw new Error('Codex auth must be a direct regular file, not a link.');
70123
const codexAuthFile = options.codexAuthFile ? mountSource(realpathSync(options.codexAuthFile), 'Codex auth') : undefined;
71-
if (codexAuthFile) {
72-
const auth = lstatSync(codexAuthFile);
73-
if (!auth.isFile() || auth.isSymbolicLink() || auth.size > 1024 * 1024) throw new Error('Codex auth must be a bounded regular file.');
74-
}
124+
const authIdentity = codexAuthFile ? captureFile(codexAuthFile, 'Codex auth') : undefined;
75125
const name = `codeboost-agent-${safeName(invocation.attemptId)}`;
76126
const readOnlyWork = ['planning', 'questions', 'review'].includes(invocation.phase);
77127
const args = ['create', '--name', name, '--read-only', '--user', '10001:10001', '--cap-drop=ALL',
@@ -93,8 +143,10 @@ export function createContainerProfile(options: ProfileOptions): ContainerProfil
93143
} else args.push('--env', 'CLAUDE_CODE_OAUTH_TOKEN');
94144
args.push(options.imageId, ...options.command);
95145
const capturedFilesystems = Object.freeze({ ...filesystems });
96-
return Object.freeze({ name, args: Object.freeze(args), expectedImage: options.imageId,
146+
const profile = Object.freeze({ name, args: Object.freeze(args), expectedImage: options.imageId,
97147
phase: invocation.phase, vendor: invocation.vendor,
98148
filesystems: capturedFilesystems, inputDirectory, codexAuthFile,
99149
command: Object.freeze([...options.command]) });
150+
identities.set(profile, Object.freeze({ ...inputIdentity, auth: authIdentity }));
151+
return profile;
100152
}

‎agents/container/run.ts‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execFileSync, spawnSync } from 'node:child_process';
22
import { randomUUID } from 'node:crypto';
33
import { lstatSync, realpathSync } from 'node:fs';
4-
import type { ContainerProfile, TaskFilesystems } from './profile.ts';
4+
import { assertContainerProfile, type ContainerProfile, type TaskFilesystems } from './profile.ts';
55
import { BASE_IMAGE, CLAUDE_VERSION, CODEX_VERSION } from './image.ts';
66

77
const dockerEnvironment = (secrets: Readonly<Record<string, string>> = {}) => ({
@@ -30,6 +30,8 @@ const createDeadline = (timeoutMs: number) => {
3030
};
3131
};
3232
const resourceName = (kind: string) => `codeboost-${kind}-${randomUUID()}`;
33+
const exactNoNewPrivileges = (options: string[] | null | undefined) => options?.length === 1
34+
&& (options[0] === 'no-new-privileges' || options[0] === 'no-new-privileges:true');
3335
const canonicalDockerBindSource = (source: string) => {
3436
const desktopHostPath = source.startsWith('/host_mnt/') ? source.slice('/host_mnt'.length) : source;
3537
try { return realpathSync(desktopHostPath); } catch { return source; }
@@ -82,6 +84,7 @@ export function prepareTaskFilesystems(stagingDirectory: string, limits: TaskSto
8284
'--mount', `type=volume,source=${workVolume},target=/work`,
8385
'--mount', `type=volume,source=${metadataVolume},target=/metadata`,
8486
'--entrypoint', 'sh', imageId, '-c', seed], { timeoutMs: remaining() });
87+
remaining();
8588
return Object.freeze({ keeper, workVolume, metadataVolume, ...limits });
8689
} catch (error) {
8790
spawnSync('docker', ['rm', '--force', keeper], { env: dockerEnvironment(), stdio: 'ignore' });
@@ -104,10 +107,11 @@ type Inspect = {
104107
/** Validate daemon-resolved configuration before starting an agent. */
105108
export function validateContainer(container: string, profile: ContainerProfile, timeoutMs = 30_000): void {
106109
const remaining = createDeadline(timeoutMs);
110+
assertContainerProfile(profile);
107111
const inspect = JSON.parse(docker(['container', 'inspect', container], { timeoutMs: remaining() }))[0] as Inspect | undefined;
108112
if (!inspect) throw new Error('Docker did not return the created container.');
109113
const image = JSON.parse(docker(['image', 'inspect', profile.expectedImage], { timeoutMs: remaining() }))[0] as
110-
{ Id?: string; Config?: { User?: string; Entrypoint?: string[]; Labels?: Record<string, string> } } | undefined;
114+
{ Id?: string; Config?: { User?: string; Env?: string[]; Entrypoint?: string[]; Labels?: Record<string, string> } } | undefined;
111115
const imageId = image?.Id, labels = image?.Config?.Labels ?? {};
112116
const host = inspect.HostConfig;
113117
if (!imageId || imageId !== profile.expectedImage || inspect.Image !== profile.expectedImage
@@ -124,7 +128,7 @@ export function validateContainer(container: string, profile: ContainerProfile,
124128
|| JSON.stringify(inspect.Config.Cmd) !== JSON.stringify(profile.command)
125129
|| !host.ReadonlyRootfs || host.Privileged
126130
|| !host.CapDrop?.map(value => value.toUpperCase()).includes('ALL')
127-
|| !host.SecurityOpt?.some(value => value.startsWith('no-new-privileges'))
131+
|| !exactNoNewPrivileges(host.SecurityOpt)
128132
|| host.NetworkMode !== 'none' || host.PidMode !== '' || host.IpcMode !== 'private'
129133
|| (host.Devices?.length ?? 0) !== 0 || (host.DeviceRequests?.length ?? 0) !== 0 || host.PidsLimit !== 128
130134
|| host.Memory !== 512 * 1024 * 1024 || host.NanoCpus !== 1_000_000_000)
@@ -183,7 +187,7 @@ export function validateContainer(container: string, profile: ContainerProfile,
183187
|| keeper.Config?.Labels?.['io.codeboost.task-storage'] !== 'keeper' || !keeper.HostConfig?.ReadonlyRootfs
184188
|| keeper.HostConfig.Privileged || keeper.HostConfig.NetworkMode !== 'none'
185189
|| !keeper.HostConfig.CapDrop?.map(value => value.toUpperCase()).includes('ALL')
186-
|| !keeper.HostConfig.SecurityOpt?.some(value => value.startsWith('no-new-privileges'))
190+
|| !exactNoNewPrivileges(keeper.HostConfig.SecurityOpt)
187191
|| keeperVolumes.get('/work')?.Name !== profile.filesystems.workVolume
188192
|| keeperVolumes.get('/metadata')?.Name !== profile.filesystems.metadataVolume)
189193
throw new Error('Task filesystems must remain owned by their trusted keeper.');
@@ -197,12 +201,14 @@ export function validateContainer(container: string, profile: ContainerProfile,
197201
if (inspect.Config.Env.some(value => value.indexOf('=') < 1)) throw new Error('Container environment is malformed.');
198202
const names = inspect.Config.Env.map(value => value.slice(0, value.indexOf('=')));
199203
const environment = new Map(inspect.Config.Env.map(value => [value.slice(0, value.indexOf('=')), value.slice(value.indexOf('=') + 1)]));
204+
const imageEnvironment = new Map((image?.Config?.Env ?? []).map(value => [value.slice(0, value.indexOf('=')), value.slice(value.indexOf('=') + 1)]));
200205
const allowedEnvironment = new Set(['PATH', 'NODE_VERSION', 'YARN_VERSION', 'HOME', 'CODEBOOST_PHASE', 'CODEBOOST_VENDOR',
201206
'CODEBOOST_WORK_BYTES', 'CODEBOOST_WORK_INODES', 'CODEBOOST_METADATA_BYTES', 'CODEBOOST_METADATA_INODES',
202207
'npm_config_cache', 'XDG_CACHE_HOME', ...(profile.vendor === 'codex' ? ['CODEX_HOME'] : ['CLAUDE_CODE_OAUTH_TOKEN'])]);
203208
if (new Set(names).size !== names.length || names.some(name => !allowedEnvironment.has(name)))
204209
throw new Error('Container includes an unexpected environment variable.');
205-
if (environment.get('HOME') !== '/home/codeboost' || environment.get('CODEBOOST_PHASE') !== profile.phase
210+
if (environment.get('PATH') !== imageEnvironment.get('PATH')
211+
|| environment.get('HOME') !== '/home/codeboost' || environment.get('CODEBOOST_PHASE') !== profile.phase
206212
|| environment.get('CODEBOOST_VENDOR') !== profile.vendor
207213
|| environment.get('CODEBOOST_WORK_BYTES') !== String(profile.filesystems.workBytes)
208214
|| environment.get('CODEBOOST_WORK_INODES') !== String(profile.filesystems.workInodes)
@@ -212,15 +218,20 @@ export function validateContainer(container: string, profile: ContainerProfile,
212218
if (profile.vendor === 'codex' && names.includes('CLAUDE_CODE_OAUTH_TOKEN')) throw new Error('Credential profiles must not be combined.');
213219
if (profile.vendor === 'claude' && (names.includes('CODEX_HOME') || !names.includes('CLAUDE_CODE_OAUTH_TOKEN')))
214220
throw new Error('Credential profiles must not be combined.');
221+
assertContainerProfile(profile);
222+
remaining();
215223
}
216224

217225
export function createValidatedContainer(profile: ContainerProfile, timeoutMs = 30_000,
218226
secrets: Readonly<Record<string, string>> = {}): string {
219227
const remaining = createDeadline(timeoutMs);
220228
validateSecrets(profile, secrets);
229+
assertContainerProfile(profile);
221230
try {
222231
docker(profile.args, { timeoutMs: remaining(), secrets });
223232
validateContainer(profile.name, profile, remaining());
233+
assertContainerProfile(profile);
234+
remaining();
224235
return profile.name;
225236
} catch (error) {
226237
spawnSync('docker', ['rm', '--force', profile.name], { timeout: 30_000, env: dockerEnvironment(), stdio: 'ignore' });
@@ -232,7 +243,12 @@ export function runContainer(profile: ContainerProfile, timeoutMs = 60_000,
232243
secrets: Readonly<Record<string, string>> = {}): string {
233244
const remaining = createDeadline(timeoutMs);
234245
const container = createValidatedContainer(profile, remaining(), secrets);
235-
try { return docker(['start', '--attach', container], { timeoutMs: remaining(), secrets }); }
246+
try {
247+
assertContainerProfile(profile);
248+
const output = docker(['start', '--attach', container], { timeoutMs: remaining(), secrets });
249+
remaining();
250+
return output;
251+
}
236252
finally { spawnSync('docker', ['rm', '--force', container], { timeout: 30_000, env: dockerEnvironment(), stdio: 'ignore' }); }
237253
}
238254

‎test/agent-container.test.ts‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,43 @@ describe('real Docker agent isolation', () => {
167167
const rogue = `codeboost-work-${randomUUID()}`; docker('volume', 'create', rogue);
168168
try {
169169
const rogueArgs = valid.args.map(value => value.replace(data.filesystems.workVolume, rogue));
170-
const rogueProfile = Object.freeze({ ...valid, args: Object.freeze(rogueArgs),
171-
filesystems: Object.freeze({ ...valid.filesystems, workVolume: rogue }) });
172170
docker(...rogueArgs); containers.add(valid.name);
173-
expect(() => validateContainer(valid.name, rogueProfile)).toThrow('bounded tmpfs allocation');
171+
expect(() => validateContainer(valid.name, valid)).toThrow('captured identity');
174172
docker('rm', '--force', valid.name); containers.delete(valid.name);
175173
} finally { spawnSync('docker', ['volume', 'rm', '--force', rogue], { stdio: 'ignore' }); }
176174
}, 60_000);
177175

176+
it('rejects cloned profiles and host inputs changed after capture', () => {
177+
const data = fixture(), valid = profile(data, 'planning', ['true']);
178+
const forged = Object.freeze({ ...valid, inputDirectory: '/',
179+
args: Object.freeze(valid.args.map(value => value.includes(`source=${data.input},`)
180+
? value.replace(`source=${data.input},`, 'source=/,') : value)) });
181+
expect(() => createValidatedContainer(forged)).toThrow('trusted profile builder');
182+
183+
chmodSync(data.input, 0o755); writeFileSync(join(data.input, 'extra.json'), '{}'); chmodSync(data.input, 0o555);
184+
expect(() => createValidatedContainer(valid)).toThrow('only one bounded');
185+
chmodSync(data.input, 0o755); rmSync(join(data.input, 'extra.json')); chmodSync(data.input, 0o555);
186+
187+
writeFileSync(data.fakeAuth, '{"changed":true}');
188+
expect(() => createValidatedContainer(valid)).toThrow('Codex auth changed');
189+
writeFileSync(data.fakeAuth, '{}');
190+
});
191+
192+
it('rejects extra security policies and a PATH that can shadow the startup probe', () => {
193+
const data = fixture(), valid = profile(data, 'planning', ['true']);
194+
const imageIndex = valid.args.indexOf(imageId);
195+
const securityArgs = [...valid.args.slice(0, imageIndex), '--security-opt', 'seccomp=unconfined',
196+
...valid.args.slice(imageIndex)];
197+
docker(...securityArgs); containers.add(valid.name);
198+
expect(() => validateContainer(valid.name, valid)).toThrow('lockdown');
199+
docker('rm', '--force', valid.name); containers.delete(valid.name);
200+
201+
const pathArgs = [...valid.args.slice(0, imageIndex), '--env', 'PATH=/work', ...valid.args.slice(imageIndex)];
202+
docker(...pathArgs); containers.add(valid.name);
203+
expect(() => validateContainer(valid.name, valid)).toThrow(/environment|PATH/);
204+
docker('rm', '--force', valid.name); containers.delete(valid.name);
205+
}, 60_000);
206+
178207
it('rejects a caller-mutated network before the container can start', () => {
179208
const data = fixture(), valid = profile(data, 'planning', ['true']);
180209
const args = valid.args.map(value => value === '--network=none' ? '--network=bridge' : value);

0 commit comments

Comments
 (0)