Repository navigation
Expand file tree
/
Copy pathelf-runtime.test.js
More file actions
524 lines (494 loc) · 16.4 KB
/
Copy pathelf-runtime.test.js
File metadata and controls
524 lines (494 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
"use strict";
const assert = require("node:assert/strict");
const { execFileSync } = require("node:child_process");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const test = require("node:test");
const {
auditExecutableShebangs,
auditFixedTree,
buildPatchelfInvocations,
inventoryTree,
loadManifest,
parseElf,
planFixups,
validateUpstreamInventory,
} = require("../../nix/elf-runtime.cjs");
const ELF_HEADER_SIZE = 64;
const PROGRAM_HEADER_SIZE = 56;
const PROGRAM_HEADER_OFFSET = ELF_HEADER_SIZE;
const PT_LOAD = 1;
const PT_DYNAMIC = 2;
const PT_INTERP = 3;
const DT_NULL = 0n;
const DT_NEEDED = 1n;
const DT_STRTAB = 5n;
const DT_STRSZ = 10n;
const DT_RUNPATH = 29n;
const DT_FLAGS_1 = 0x6ffffffbn;
const DF_1_PIE = 0x08000000n;
function writeProgram(buffer, index, type, offset, fileSize, vaddr = offset) {
const header = PROGRAM_HEADER_OFFSET + index * PROGRAM_HEADER_SIZE;
buffer.writeUInt32LE(type, header);
buffer.writeUInt32LE(4, header + 4);
buffer.writeBigUInt64LE(BigInt(offset), header + 8);
buffer.writeBigUInt64LE(BigInt(vaddr), header + 16);
buffer.writeBigUInt64LE(BigInt(vaddr), header + 24);
buffer.writeBigUInt64LE(BigInt(fileSize), header + 32);
buffer.writeBigUInt64LE(BigInt(fileSize), header + 40);
buffer.writeBigUInt64LE(8n, header + 48);
}
function writeDynamic(buffer, index, tag, value) {
const offset = 0x380 + index * 16;
buffer.writeBigInt64LE(tag, offset);
buffer.writeBigUInt64LE(BigInt(value), offset + 8);
}
function dynamicElf({
machine = 62,
interpreter = "/lib64/ld-linux-x86-64.so.2",
needed = "libc.so.6",
runpath = null,
flags1 = 0,
relativeSectionTableOffset = 0xffffffffffffn,
} = {}) {
const buffer = Buffer.alloc(0x800);
const hasInterpreter = interpreter !== null;
const strings = Buffer.from(
`\0${needed || ""}\0${runpath || ""}\0`,
);
const stringOffset = 0x300;
const neededOffset = 1;
const runpathOffset = neededOffset + (needed || "").length + 1;
const dynamicEntries = [
[DT_STRTAB, stringOffset],
[DT_STRSZ, strings.length],
];
if (needed) dynamicEntries.push([DT_NEEDED, neededOffset]);
if (runpath) dynamicEntries.push([DT_RUNPATH, runpathOffset]);
if (flags1) dynamicEntries.push([DT_FLAGS_1, flags1]);
dynamicEntries.push([DT_NULL, 0]);
buffer.writeUInt32BE(0x7f454c46, 0);
buffer[4] = 2;
buffer[5] = 1;
buffer[6] = 1;
buffer.writeUInt16LE(3, 16);
buffer.writeUInt16LE(machine, 18);
buffer.writeUInt32LE(1, 20);
buffer.writeBigUInt64LE(BigInt(PROGRAM_HEADER_OFFSET), 32);
// Deliberately invalid: the runtime parser must not read section headers.
buffer.writeBigUInt64LE(BigInt(relativeSectionTableOffset), 40);
buffer.writeUInt16LE(ELF_HEADER_SIZE, 52);
buffer.writeUInt16LE(PROGRAM_HEADER_SIZE, 54);
buffer.writeUInt16LE(hasInterpreter ? 3 : 2, 56);
buffer.writeUInt16LE(64, 58);
buffer.writeUInt16LE(99, 60);
buffer.writeUInt16LE(98, 62);
writeProgram(buffer, 0, PT_LOAD, 0, buffer.length, 0);
if (hasInterpreter) {
const interpreterBytes = Buffer.from(`${interpreter}\0`);
interpreterBytes.copy(buffer, 0x280);
writeProgram(buffer, 1, PT_INTERP, 0x280, interpreterBytes.length);
}
writeProgram(
buffer,
hasInterpreter ? 2 : 1,
PT_DYNAMIC,
0x380,
dynamicEntries.length * 16,
);
strings.copy(buffer, stringOffset);
dynamicEntries.forEach(([tag, value], index) =>
writeDynamic(buffer, index, tag, value),
);
return buffer;
}
function staticElf(machine = 62) {
const buffer = Buffer.alloc(0x200);
buffer.writeUInt32BE(0x7f454c46, 0);
buffer[4] = 2;
buffer[5] = 1;
buffer[6] = 1;
buffer.writeUInt16LE(2, 16);
buffer.writeUInt16LE(machine, 18);
buffer.writeUInt32LE(1, 20);
buffer.writeBigUInt64LE(BigInt(PROGRAM_HEADER_OFFSET), 32);
buffer.writeBigUInt64LE(0xffffffffffffn, 40);
buffer.writeUInt16LE(ELF_HEADER_SIZE, 52);
buffer.writeUInt16LE(PROGRAM_HEADER_SIZE, 54);
buffer.writeUInt16LE(1, 56);
writeProgram(buffer, 0, PT_LOAD, 0, buffer.length, 0);
return buffer;
}
function splitDynamicStringsAcrossLoads(buffer, discontinuous = false) {
const interpreterBytes = Buffer.from("/lib64/ld-linux-x86-64.so.2\0");
buffer.writeUInt16LE(4, 56);
writeProgram(buffer, 0, PT_LOAD, 0, 0x305, 0);
writeProgram(
buffer,
1,
PT_LOAD,
discontinuous ? 0x306 : 0x305,
buffer.length - 0x306,
0x305,
);
writeProgram(buffer, 2, PT_INTERP, 0x280, interpreterBytes.length);
writeProgram(buffer, 3, PT_DYNAMIC, 0x380, 5 * 16);
return buffer;
}
function withTemporaryDirectory(callback) {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "elf-runtime-test-"));
try {
return callback(directory);
} finally {
fs.rmSync(directory, { recursive: true, force: true });
}
}
function writeFixture(root, relativePath, contents, mode = 0o755) {
const absolutePath = path.join(root, relativePath);
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
fs.writeFileSync(absolutePath, contents, { mode });
}
test("parses PT_INTERP and PT_DYNAMIC without consulting section headers", () => {
const parsed = parseElf(
dynamicElf({ runpath: "/nix/store/runtime/lib" }),
"ChatGPT",
);
assert.deepEqual(
{
architecture: parsed.architecture,
platform: parsed.platform,
linkage: parsed.linkage,
interpreter: parsed.interpreter,
needed: parsed.needed,
runpath: parsed.runpath,
},
{
architecture: "amd64",
platform: "glibc",
linkage: "dynamic-executable",
interpreter: "/lib64/ld-linux-x86-64.so.2",
needed: ["libc.so.6"],
runpath: ["/nix/store/runtime/lib"],
},
);
});
test("parses a dynamic string table spanning adjacent patchelf PT_LOADs", () => {
const parsed = parseElf(
splitDynamicStringsAcrossLoads(
dynamicElf({ runpath: "/nix/store/runtime/lib" }),
),
"patchelf-output",
);
assert.deepEqual(parsed.needed, ["libc.so.6"]);
assert.deepEqual(parsed.runpath, ["/nix/store/runtime/lib"]);
});
test("rejects a dynamic string table spanning discontinuous file mappings", () => {
assert.throws(
() =>
parseElf(
splitDynamicStringsAcrossLoads(dynamicElf(), true),
"invalid-patchelf-output",
),
/not contiguously file-backed by PT_LOAD/,
);
});
test("classifies target, foreign, static, static PIE, musl, and Android ELF", () => {
assert.equal(parseElf(dynamicElf({ machine: 183 }), "arm").architecture, "arm64");
assert.equal(parseElf(dynamicElf({ machine: 40 }), "foreign").architecture, "machine-40");
assert.equal(parseElf(staticElf(), "static").linkage, "static");
assert.equal(
parseElf(
dynamicElf({ interpreter: null, needed: null, flags1: DF_1_PIE }),
"static-pie",
).linkage,
"static-pie",
);
assert.equal(
parseElf(
dynamicElf({ interpreter: "/lib/ld-musl-x86_64.so.1" }),
"linux-x64-musl/tool",
).platform,
"musl",
);
assert.equal(
parseElf(dynamicElf({ interpreter: "/system/bin/linker64" }), "android/tool")
.platform,
"android",
);
assert.equal(parseElf(Buffer.from("Mach-O"), "foreign"), null);
});
test("fails closed for malformed program and dynamic tables", () => {
const truncatedProgram = dynamicElf();
truncatedProgram.writeBigUInt64LE(0x1000n, PROGRAM_HEADER_OFFSET + 32);
assert.throws(
() => parseElf(truncatedProgram, "truncated"),
/program header 0 is outside/,
);
const missingTerminator = dynamicElf();
for (let offset = 0x380; offset < 0x380 + 5 * 16; offset += 16)
missingTerminator.writeBigInt64LE(DT_NEEDED, offset);
assert.throws(
() => parseElf(missingTerminator, "unterminated"),
/PT_DYNAMIC is not DT_NULL-terminated/,
);
});
for (const architecture of ["amd64", "arm64"]) {
test(`validates the exact ${architecture} upstream executable inventory`, () => {
withTemporaryDirectory((root) => {
const manifest = loadManifest();
delete manifest.architectures[architecture].targetInventoryCount;
delete manifest.architectures[architecture].targetInventorySha256;
const contract = manifest.architectures[architecture];
assert.equal(
contract.requiredDynamicExecutables.includes(
`resources/cua_node/lib/node_modules/@oai/cua/bin/linux/sky_linux_${
architecture === "amd64" ? "x64" : "arm64"
}`,
),
true,
);
assert.equal(
contract.requiredDynamicExecutables.includes("resources/node"),
true,
);
const interpreter =
architecture === "amd64"
? "/lib64/ld-linux-x86-64.so.2"
: "/lib/ld-linux-aarch64.so.1";
for (const relativePath of contract.requiredDynamicExecutables) {
writeFixture(
root,
relativePath,
dynamicElf({ machine: contract.machine, interpreter }),
);
}
writeFixture(root, "resources/static-helper", staticElf(contract.machine));
writeFixture(
root,
"resources/prebuilds/linux-x64-musl/addon.node",
dynamicElf({ interpreter: "/lib/ld-musl-x86_64.so.1" }),
);
writeFixture(root, "resources/foreign-helper", dynamicElf({ machine: 40 }));
const inventory = validateUpstreamInventory(root, architecture, manifest);
assert.equal(
inventory.filter(
(item) =>
item.target &&
item.platform === "glibc" &&
item.linkage === "dynamic-executable",
)
.length,
contract.requiredDynamicExecutables.length,
);
const actions = planFixups(root, architecture, manifest);
assert.equal(
actions.find(({ path: itemPath }) => itemPath === "ChatGPT").strategy,
"relocate-within-detect-libc-range",
);
assert.equal(
actions.find(({ path: itemPath }) => itemPath === "resources/node")
.strategy,
"patchelf",
);
if (architecture === "amd64") {
assert.equal(
actions.find(
({ path: itemPath }) =>
itemPath === "resources/cua_node/bin/node",
).strategy,
"patchelf-rpath-first",
);
}
});
});
}
test("rejects missing, unexpected, and obsolete upstream executables", () => {
withTemporaryDirectory((root) => {
const manifest = loadManifest();
const contract = manifest.architectures.amd64;
delete contract.targetInventoryCount;
delete contract.targetInventorySha256;
for (const relativePath of contract.requiredDynamicExecutables) {
writeFixture(root, relativePath, dynamicElf());
}
fs.unlinkSync(path.join(root, "browser_crashpad_handler"));
writeFixture(root, "unexpected-helper", dynamicElf());
assert.throws(
() => validateUpstreamInventory(root, "amd64", manifest),
/missing=\[browser_crashpad_handler\].*unexpected=\[unexpected-helper\]/,
);
writeFixture(root, "chrome_crashpad_handler", dynamicElf());
assert.throws(
() => validateUpstreamInventory(root, "amd64", manifest),
/obsolete upstream path/,
);
fs.unlinkSync(path.join(root, "chrome_crashpad_handler"));
});
});
const retiredTectonicPaths = [
"resources/tectonic/tectonic",
"resources/plugins/openai-bundled/plugins/latex/bin/tectonic",
];
test("Nix runtime manifest does not retain a retired bundled Tectonic binary", () => {
const manifest = loadManifest();
const manifestPaths = [
...(manifest.forbiddenPaths || []),
...Object.values(manifest.architectures).flatMap((contract) => [
...(contract.requiredDynamicExecutables || []),
...Object.keys(contract.interpreterStrategies || {}),
]),
];
for (const retiredPath of retiredTectonicPaths) {
assert.ok(
!manifestPaths.includes(retiredPath),
`ELF runtime manifest retained retired path: ${retiredPath}`,
);
}
});
test("signed amd64 payload replaces retired Tectonic binaries with its compiler helper", {
skip: process.env.CODEX_SIGNED_AMD64_LINUX_PACKAGE
? false
: "CODEX_SIGNED_AMD64_LINUX_PACKAGE is not set",
}, () => {
const packagePath = path.resolve(
process.env.CODEX_SIGNED_AMD64_LINUX_PACKAGE,
);
const expectedSha256 = process.env.CODEX_EXPECTED_SIGNED_AMD64_SHA256;
assert.match(
expectedSha256 || "",
/^[0-9a-f]{64}$/,
"CODEX_EXPECTED_SIGNED_AMD64_SHA256 must bind the payload to its signed campaign",
);
const [actualSha256] = execFileSync("sha256sum", [packagePath], {
encoding: "utf8",
}).split(/\s+/, 1);
assert.equal(actualSha256, expectedSha256, "signed campaign package hash drifted");
assert.equal(
execFileSync("dpkg-deb", ["--field", packagePath, "Architecture"], {
encoding: "utf8",
}).trim(),
"amd64",
"Tectonic retirement regression requires the signed amd64 payload",
);
const payloadEntries = execFileSync("dpkg-deb", ["--contents", packagePath], {
encoding: "utf8",
maxBuffer: 16 * 1024 * 1024,
})
.split("\n")
.map((line) => line.match(/ \.\/usr\/lib\/chatgpt\/(.+)$/)?.[1])
.filter(Boolean);
for (const retiredPath of retiredTectonicPaths) {
assert.equal(
payloadEntries.includes(retiredPath),
false,
`signed payload retained retired path: ${retiredPath}`,
);
}
assert.ok(
payloadEntries.includes(
"resources/tectonic/compiler/compile_desktop.py",
),
"signed payload is missing the Tectonic compiler helper used by the current ASAR",
);
});
test("the amd64 CUA Node fixup adds RUNPATH before changing PT_INTERP", () => {
assert.deepEqual(
buildPatchelfInvocations(
{
strategy: "patchelf-rpath-first",
setInterpreter: true,
addRpath: true,
},
"/nix/store/glibc/lib/ld-linux-x86-64.so.2",
"/nix/store/runtime/lib",
),
[
["--add-rpath", "/nix/store/runtime/lib"],
[
"--set-interpreter",
"/nix/store/glibc/lib/ld-linux-x86-64.so.2",
],
],
);
});
test("post-fix audit enforces the Nix interpreter and runtime search path", () => {
withTemporaryDirectory((root) => {
const manifest = loadManifest();
const contract = manifest.architectures.amd64;
const dynamicLinker = "/nix/store/glibc/lib/ld-linux-x86-64.so.2";
const runtimeLibraryPath =
"/run/opengl-driver/lib:/nix/store/runtime/lib";
for (const relativePath of contract.requiredDynamicExecutables) {
writeFixture(
root,
relativePath,
dynamicElf({ interpreter: dynamicLinker, runpath: runtimeLibraryPath }),
);
}
writeFixture(
root,
"resources/native/addon.node",
dynamicElf({ interpreter: null, runpath: runtimeLibraryPath }),
);
writeFixture(
root,
"resources/cua_node/lib/node_modules/@img/sharp-libvips-linux-x64/lib/libvips-cpp.so.8.18.7",
dynamicElf({ interpreter: null, runpath: "$ORIGIN/" }),
);
assert.doesNotThrow(() =>
auditFixedTree({
root,
architecture: "amd64",
dynamicLinker,
runtimeLibraryPath,
manifest,
checkShebangs: false,
}),
);
writeFixture(root, "browser_crashpad_handler", dynamicElf());
assert.throws(
() =>
auditFixedTree({
root,
architecture: "amd64",
dynamicLinker,
runtimeLibraryPath,
manifest,
checkShebangs: false,
}),
/interpreter was not fixed/,
);
});
});
test("executable shebang audit accepts Nix-safe scripts and rejects FHS interpreters", () => {
withTemporaryDirectory((root) => {
writeFixture(root, "env-script", "#!/usr/bin/env bash\nexit 0\n");
assert.doesNotThrow(() => auditExecutableShebangs(root));
writeFixture(root, "fhs-script", "#!/bin/bash\nexit 0\n");
assert.throws(
() => auditExecutableShebangs(root),
/executable retains an FHS shebang: \/bin\/bash/,
);
fs.unlinkSync(path.join(root, "fhs-script"));
writeFixture(root, "relative-script", "#!bash\nexit 0\n");
assert.throws(
() => auditExecutableShebangs(root),
/shebang interpreter is not absolute: bash/,
);
});
});
test("inventoryTree reports native and foreign machines without following names", () => {
withTemporaryDirectory((root) => {
writeFixture(root, "native", dynamicElf());
writeFixture(root, "arm64", dynamicElf({ machine: 183 }));
const inventory = inventoryTree(root, "amd64");
assert.deepEqual(
inventory.map(({ path: itemPath, target }) => [itemPath, target]),
[
["arm64", false],
["native", true],
],
);
});
});