Skip to content

Commit a4bd301

Browse files
committed
Seal D2 inputs and complete resource checks
1 parent c438462 commit a4bd301

5 files changed

Lines changed: 122 additions & 53 deletions

File tree

‎agents/container/profile.ts‎

Lines changed: 79 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createHash, randomUUID } from 'node:crypto';
2-
import { chmodSync, closeSync, constants, fstatSync, lstatSync, mkdtempSync, openSync, readFileSync,
2+
import { chmodSync, closeSync, constants, fstatSync, lstatSync, mkdtempSync, openSync, readSync,
33
readdirSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
44
import { tmpdir } from 'node:os';
55
import { join } from 'node:path';
@@ -39,18 +39,40 @@ interface FileIdentity {
3939
readonly digest: string;
4040
}
4141
interface ProfileIdentity { readonly inputDirectory: string; readonly schema: FileIdentity; readonly auth?: FileIdentity;
42-
readonly cleanupDirectory?: string; readonly filesystems: TaskFilesystems; readonly clone: InvocationInput['clone'] }
42+
readonly cleanupDirectories: readonly string[]; readonly filesystems: TaskFilesystems;
43+
readonly clone: InvocationInput['clone'] }
4344
type InputIdentity = Pick<ProfileIdentity, 'inputDirectory' | 'schema'>;
45+
interface InputCapture extends InputIdentity { readonly content: Buffer }
4446
const identities = new WeakMap<ContainerProfile, ProfileIdentity>();
47+
const removeOwnedDirectory = (directory: string) => {
48+
if (!lstatSync(directory, { throwIfNoEntry: false })) return;
49+
chmodSync(directory, 0o700);
50+
rmSync(directory, { recursive: true, force: true });
51+
};
52+
const removeOwnedDirectories = (directories: readonly string[]) => {
53+
const failures: unknown[] = [];
54+
for (const directory of directories) {
55+
try { removeOwnedDirectory(directory); } catch (error) { failures.push(error); }
56+
}
57+
if (failures.length) throw new AggregateError(failures, 'Profile snapshot cleanup did not settle.');
58+
};
4559

4660
const readCapturedFile = (path: string, kind: string): { identity: FileIdentity; content: Buffer } => {
4761
let fd: number | undefined;
4862
try {
4963
fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
5064
const before = fstatSync(fd);
51-
if (!before.isFile() || before.nlink !== 1 || before.size > 1024 * 1024)
65+
const maximum = 1024 * 1024;
66+
if (!before.isFile() || before.nlink !== 1 || before.size > maximum)
5267
throw new Error(`${kind} must be a bounded, unlinked regular file.`);
53-
const content = readFileSync(fd);
68+
const bounded = Buffer.allocUnsafe(maximum + 1);
69+
let length = 0, count = 0;
70+
do {
71+
count = readSync(fd, bounded, length, bounded.length - length, null);
72+
length += count;
73+
} while (count > 0 && length < bounded.length);
74+
if (length > maximum) throw new Error(`${kind} exceeds its maximum size.`);
75+
const content = bounded.subarray(0, length);
5476
const after = fstatSync(fd);
5577
if (before.dev !== after.dev || before.ino !== after.ino || before.size !== after.size
5678
|| before.mtimeMs !== after.mtimeMs || before.ctimeMs !== after.ctimeMs)
@@ -65,17 +87,17 @@ const sameFile = (actual: FileIdentity, expected: FileIdentity) => actual.path =
6587
&& actual.dev === expected.dev && actual.ino === expected.ino && actual.mode === expected.mode
6688
&& actual.nlink === expected.nlink && actual.size === expected.size && actual.mtimeMs === expected.mtimeMs
6789
&& actual.digest === expected.digest;
68-
const captureInput = (directory: string): InputIdentity => {
90+
const captureInput = (directory: string): InputCapture => {
6991
const stat = lstatSync(directory);
7092
if (!stat.isDirectory() || stat.isSymbolicLink() || (stat.mode & 0o005) !== 0o005)
7193
throw new Error('Schema input directory must be a container-readable real directory.');
7294
const canonical = mountSource(realpathSync(directory), 'Schema input');
7395
const entries = readdirSync(canonical);
7496
if (entries.length !== 1 || entries[0] !== 'schema.json')
7597
throw new Error('Schema input must contain only one bounded, unlinked regular schema.json file.');
76-
const schema = captureFile(`${canonical}/schema.json`, 'Schema input');
98+
const captured = readCapturedFile(`${canonical}/schema.json`, 'Schema input'), schema = captured.identity;
7799
if ((schema.mode & 0o004) === 0) throw new Error('Schema input must be container-readable.');
78-
return Object.freeze({ inputDirectory: canonical, schema });
100+
return Object.freeze({ inputDirectory: canonical, schema, content: captured.content });
79101
};
80102

81103
/** Internal authenticity and host-file revalidation used at every launch boundary. */
@@ -96,7 +118,7 @@ export function assertContainerProfile(profile: ContainerProfile): void {
96118
export function disposeContainerProfile(profile: ContainerProfile): void {
97119
const identity = identities.get(profile);
98120
if (!identity) return;
99-
if (identity.cleanupDirectory) rmSync(identity.cleanupDirectory, { recursive: true, force: true });
121+
removeOwnedDirectories(identity.cleanupDirectories);
100122
identities.delete(profile);
101123
}
102124

@@ -119,8 +141,7 @@ export function createContainerProfile(options: ProfileOptions): ContainerProfil
119141
throw new Error('Container profile requires the immutable built image ID.');
120142
assertBuiltAgentImage(options.imageId);
121143
assertTaskFilesystems(filesystems, invocation.clone);
122-
const inputIdentity = captureInput(options.inputDirectory);
123-
const inputDirectory = inputIdentity.inputDirectory;
144+
const sourceInput = captureInput(options.inputDirectory);
124145
if (invocation.vendor === 'codex' && (!options.codexAuthFile || options.claudeToken))
125146
throw new Error('Codex requires only its auth file.');
126147
if (invocation.vendor === 'claude' && (!options.claudeToken || options.codexAuthFile))
@@ -132,47 +153,59 @@ export function createContainerProfile(options: ProfileOptions): ContainerProfil
132153
if (options.codexAuthFile && !lstatSync(options.codexAuthFile).isFile())
133154
throw new Error('Codex auth must be a direct regular file, not a link.');
134155
const sourceAuth = options.codexAuthFile ? readCapturedFile(realpathSync(options.codexAuthFile), 'Codex auth') : undefined;
135-
let cleanupDirectory: string | undefined, codexAuthFile: string | undefined, authIdentity: FileIdentity | undefined;
136-
if (sourceAuth) {
137-
cleanupDirectory = mkdtempSync(join(tmpdir(), 'codeboost-auth-'));
138-
try {
156+
const cleanupDirectories: string[] = [];
157+
let codexAuthFile: string | undefined, authIdentity: FileIdentity | undefined;
158+
try {
159+
const inputDirectory = mkdtempSync(join(tmpdir(), 'codeboost-input-'));
160+
cleanupDirectories.push(inputDirectory);
161+
writeFileSync(join(inputDirectory, 'schema.json'), sourceInput.content,
162+
{ mode: 0o400, flag: 'wx' });
163+
chmodSync(join(inputDirectory, 'schema.json'), 0o444);
164+
chmodSync(inputDirectory, 0o555);
165+
const inputIdentity = captureInput(inputDirectory);
166+
if (sourceAuth) {
167+
const cleanupDirectory = mkdtempSync(join(tmpdir(), 'codeboost-auth-'));
168+
cleanupDirectories.push(cleanupDirectory);
139169
const stagedAuth = join(cleanupDirectory, 'auth.json');
140170
writeFileSync(stagedAuth, sourceAuth.content, { mode: 0o400, flag: 'wx' });
141171
chmodSync(stagedAuth, 0o444);
142172
codexAuthFile = mountSource(realpathSync(stagedAuth), 'Codex auth');
143173
authIdentity = captureFile(codexAuthFile, 'Staged Codex auth');
144-
} catch (error) {
145-
rmSync(cleanupDirectory, { recursive: true, force: true });
146-
throw error;
147174
}
175+
const name = `codeboost-agent-${safeName(invocation.attemptId)}`, ownershipId = randomUUID();
176+
const readOnlyWork = ['planning', 'questions', 'review'].includes(invocation.phase);
177+
const args = ['create', '--name', name, '--read-only', '--user', '10001:10001', '--cap-drop=ALL',
178+
'--security-opt=no-new-privileges', '--pids-limit=128', '--memory=512m', '--memory-swap=512m',
179+
'--cpus=1', '--shm-size=16m',
180+
'--network=none', '--env', 'HOME=/home/codeboost', '--env', `CODEBOOST_PHASE=${invocation.phase}`,
181+
'--label', `io.codeboost.invocation=${ownershipId}`,
182+
'--env', `CODEBOOST_VENDOR=${invocation.vendor}`, '--env', 'npm_config_cache=/tmp/npm-cache',
183+
'--env', `CODEBOOST_WORK_BYTES=${filesystems.workBytes}`, '--env', `CODEBOOST_WORK_INODES=${filesystems.workInodes}`,
184+
'--env', `CODEBOOST_METADATA_BYTES=${filesystems.metadataBytes}`, '--env', `CODEBOOST_METADATA_INODES=${filesystems.metadataInodes}`,
185+
'--env', 'XDG_CACHE_HOME=/tmp/xdg-cache',
186+
'--tmpfs', '/tmp:rw,nosuid,nodev,size=33554432,nr_inodes=4096,mode=1777',
187+
'--tmpfs', '/home/codeboost:rw,nosuid,nodev,size=1048576,nr_inodes=128,uid=10001,gid=10001,mode=0700',
188+
'--mount', mount({ type: 'volume', source: filesystems.workVolume, target: '/work', readonly: readOnlyWork }),
189+
'--mount', mount({ type: 'volume', source: filesystems.metadataVolume, target: '/work/.git', readonly: true }),
190+
'--mount', mount({ type: 'bind', source: inputIdentity.inputDirectory, target: '/run/codeboost-input', readonly: true })];
191+
if (invocation.vendor === 'codex') {
192+
args.push('--env', 'CODEX_HOME=/run/codeboost-auth/codex',
193+
'--tmpfs', '/run/codeboost-auth/codex:rw,nosuid,nodev,size=4194304,nr_inodes=256,uid=10001,gid=10001,mode=0700',
194+
'--mount', mount({ type: 'bind', source: codexAuthFile!, target: '/run/codeboost-auth/codex/auth.json', readonly: true }));
195+
} else args.push('--env', 'CLAUDE_CODE_OAUTH_TOKEN');
196+
args.push(options.imageId, ...options.command);
197+
const capturedFilesystems = filesystems;
198+
const profile = Object.freeze({ name, args: Object.freeze(args), expectedImage: options.imageId,
199+
phase: invocation.phase, vendor: invocation.vendor,
200+
filesystems: capturedFilesystems, inputDirectory: inputIdentity.inputDirectory, codexAuthFile,
201+
command: Object.freeze([...options.command]), ownershipId });
202+
identities.set(profile, Object.freeze({ inputDirectory: inputIdentity.inputDirectory, schema: inputIdentity.schema,
203+
auth: authIdentity,
204+
cleanupDirectories: Object.freeze([...cleanupDirectories]), filesystems, clone: invocation.clone }));
205+
return profile;
206+
} catch (error) {
207+
try { removeOwnedDirectories(cleanupDirectories); }
208+
catch (cleanupError) { throw new AggregateError([error, cleanupError], 'Profile creation and cleanup both failed.'); }
209+
throw error;
148210
}
149-
const name = `codeboost-agent-${safeName(invocation.attemptId)}`, ownershipId = randomUUID();
150-
const readOnlyWork = ['planning', 'questions', 'review'].includes(invocation.phase);
151-
const args = ['create', '--name', name, '--read-only', '--user', '10001:10001', '--cap-drop=ALL',
152-
'--security-opt=no-new-privileges', '--pids-limit=128', '--memory=512m', '--cpus=1',
153-
'--network=none', '--env', 'HOME=/home/codeboost', '--env', `CODEBOOST_PHASE=${invocation.phase}`,
154-
'--label', `io.codeboost.invocation=${ownershipId}`,
155-
'--env', `CODEBOOST_VENDOR=${invocation.vendor}`, '--env', 'npm_config_cache=/tmp/npm-cache',
156-
'--env', `CODEBOOST_WORK_BYTES=${filesystems.workBytes}`, '--env', `CODEBOOST_WORK_INODES=${filesystems.workInodes}`,
157-
'--env', `CODEBOOST_METADATA_BYTES=${filesystems.metadataBytes}`, '--env', `CODEBOOST_METADATA_INODES=${filesystems.metadataInodes}`,
158-
'--env', 'XDG_CACHE_HOME=/tmp/xdg-cache',
159-
'--tmpfs', '/tmp:rw,nosuid,nodev,size=33554432,nr_inodes=4096,mode=1777',
160-
'--tmpfs', '/home/codeboost:rw,nosuid,nodev,size=1048576,nr_inodes=128,uid=10001,gid=10001,mode=0700',
161-
'--mount', mount({ type: 'volume', source: filesystems.workVolume, target: '/work', readonly: readOnlyWork }),
162-
'--mount', mount({ type: 'volume', source: filesystems.metadataVolume, target: '/work/.git', readonly: true }),
163-
'--mount', mount({ type: 'bind', source: inputDirectory, target: '/run/codeboost-input', readonly: true })];
164-
if (invocation.vendor === 'codex') {
165-
args.push('--env', 'CODEX_HOME=/run/codeboost-auth/codex',
166-
'--tmpfs', '/run/codeboost-auth/codex:rw,nosuid,nodev,size=4194304,nr_inodes=256,uid=10001,gid=10001,mode=0700',
167-
'--mount', mount({ type: 'bind', source: codexAuthFile!, target: '/run/codeboost-auth/codex/auth.json', readonly: true }));
168-
} else args.push('--env', 'CLAUDE_CODE_OAUTH_TOKEN');
169-
args.push(options.imageId, ...options.command);
170-
const capturedFilesystems = filesystems;
171-
const profile = Object.freeze({ name, args: Object.freeze(args), expectedImage: options.imageId,
172-
phase: invocation.phase, vendor: invocation.vendor,
173-
filesystems: capturedFilesystems, inputDirectory, codexAuthFile,
174-
command: Object.freeze([...options.command]), ownershipId });
175-
identities.set(profile, Object.freeze({ ...inputIdentity, auth: authIdentity, cleanupDirectory,
176-
filesystems, clone: invocation.clone }));
177-
return profile;
178211
}

‎agents/container/run.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ type Inspect = {
7979
HostConfig: { ReadonlyRootfs: boolean; Privileged: boolean; CapDrop: string[] | null; SecurityOpt: string[] | null;
8080
CapAdd: string[] | null;
8181
NetworkMode: string; PidMode: string; IpcMode: string; UTSMode: string; UsernsMode: string; CgroupnsMode: string;
82-
PidsLimit: number; Memory: number; NanoCpus: number;
82+
PidsLimit: number; Memory: number; MemorySwap: number; MemoryReservation: number; MemorySwappiness: number | null;
83+
OomKillDisable: boolean; OomScoreAdj: number; NanoCpus: number; CpuShares: number; CpuPeriod: number; CpuQuota: number;
84+
CpuRealtimePeriod: number; CpuRealtimeRuntime: number; CpusetCpus: string; CpusetMems: string; ShmSize: number;
85+
BlkioWeight: number; BlkioWeightDevice: unknown[]; BlkioDeviceReadBps: unknown[]; BlkioDeviceWriteBps: unknown[];
86+
BlkioDeviceReadIOps: unknown[]; BlkioDeviceWriteIOps: unknown[]; Ulimits: unknown[]; CpuCount: number;
87+
CpuPercent: number; IOMaximumBandwidth: number; IOMaximumIOps: number; DeviceCgroupRules: unknown[] | null;
88+
StorageOpt?: Record<string, string> | null; CgroupParent: string;
8389
Devices: unknown[] | null; DeviceRequests: unknown[] | null; Tmpfs: Record<string, string> | null;
8490
Mounts: Array<{ Type: string; Source: string; Target: string; ReadOnly: boolean }> | null };
8591
Mounts: Array<{ Type: string; Name?: string; Source: string; Destination: string; RW: boolean }>;
@@ -114,7 +120,15 @@ export function validateContainer(container: string, profile: ContainerProfile,
114120
|| host.NetworkMode !== 'none' || host.PidMode !== '' || host.IpcMode !== 'private'
115121
|| host.UTSMode !== '' || host.UsernsMode !== '' || host.CgroupnsMode !== 'private'
116122
|| (host.Devices?.length ?? 0) !== 0 || (host.DeviceRequests?.length ?? 0) !== 0 || host.PidsLimit !== 128
117-
|| host.Memory !== 512 * 1024 * 1024 || host.NanoCpus !== 1_000_000_000)
123+
|| host.Memory !== 512 * 1024 * 1024 || host.MemorySwap !== 512 * 1024 * 1024
124+
|| host.MemoryReservation !== 0 || host.MemorySwappiness !== null || host.OomKillDisable || host.OomScoreAdj !== 0
125+
|| host.NanoCpus !== 1_000_000_000 || host.CpuShares !== 0 || host.CpuPeriod !== 0 || host.CpuQuota !== 0
126+
|| host.CpuRealtimePeriod !== 0 || host.CpuRealtimeRuntime !== 0 || host.CpusetCpus !== '' || host.CpusetMems !== ''
127+
|| host.ShmSize !== 16 * 1024 * 1024 || host.BlkioWeight !== 0
128+
|| host.BlkioWeightDevice.length || host.BlkioDeviceReadBps.length || host.BlkioDeviceWriteBps.length
129+
|| host.BlkioDeviceReadIOps.length || host.BlkioDeviceWriteIOps.length || host.Ulimits.length
130+
|| host.CpuCount !== 0 || host.CpuPercent !== 0 || host.IOMaximumBandwidth !== 0 || host.IOMaximumIOps !== 0
131+
|| host.DeviceCgroupRules !== null || host.StorageOpt != null || host.CgroupParent !== '')
118132
throw new Error('Container daemon configuration is missing required lockdown.');
119133
const tmpfs = host.Tmpfs ?? {};
120134
const expectedTmpfs = new Map([

‎agents/container/storage.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { execFileSync, spawnSync } from 'node:child_process';
22
import { randomUUID } from 'node:crypto';
33
import { lstatSync, realpathSync } from 'node:fs';
44
import type { TaskClone } from '../contract.ts';
5+
import { assertTaskClone } from '../../git/clone.ts';
56
import { assertBuiltAgentImage } from './image.ts';
67

78
export interface TaskFilesystems {
@@ -101,6 +102,7 @@ export function prepareTaskFilesystems(clone: TaskClone, limits: TaskStorageLimi
101102
for (const [name, value] of Object.entries(limits)) validLimit(value, name);
102103
if (!/^sha256:[0-9a-f]{64}$/.test(imageId)) throw new Error('Task filesystems require the immutable built image ID.');
103104
assertBuiltAgentImage(imageId);
105+
assertTaskClone(clone);
104106
const remaining = createDeadline(timeoutMs), staging = realpathSync(clone.directory);
105107
if (/[\n,]/.test(staging)) throw new Error('Staging path cannot be represented as a Docker mount.');
106108
if (!lstatSync(`${staging}/.git`).isDirectory()) throw new Error('Staging clone must contain standalone Git metadata.');

‎git/clone.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { lstatSync, mkdtempSync, opendirSync, realpathSync, rmSync } from 'node:
44
import { isAbsolute, join, relative, resolve } from 'node:path';
55
import type { TaskClone } from '../agents/contract.ts';
66

7+
const trustedClones = new WeakSet<TaskClone>();
8+
9+
export function assertTaskClone(clone: TaskClone): void {
10+
if (!trustedClones.has(clone)) throw new Error('Task clone was not created by the trusted clone builder.');
11+
}
12+
713
/**
814
* Prepare an independent committed snapshot. This is trusted staging, not the
915
* writable execution filesystem: D2 must reserve bounded storage and separate
@@ -79,7 +85,9 @@ export function createTaskClone(options: {
7985
run(directory, 'remote', 'remove', 'origin');
8086
run(directory, 'checkout', '--detach', options.head);
8187
if (run(directory, 'rev-parse', 'HEAD') !== options.head) throw new Error('Task head changed during clone.');
82-
return Object.freeze({ id: randomUUID(), taskId: options.taskId, directory, head: options.head });
88+
const clone = Object.freeze({ id: randomUUID(), taskId: options.taskId, directory, head: options.head });
89+
trustedClones.add(clone);
90+
return clone;
8391
} catch (error) {
8492
rmSync(directory, { recursive: true, force: true });
8593
throw error;

0 commit comments

Comments
 (0)