diff --git a/docs/desktop-application-capture.md b/docs/desktop-application-capture.md index 5516829280..474bd90313 100644 --- a/docs/desktop-application-capture.md +++ b/docs/desktop-application-capture.md @@ -511,9 +511,11 @@ DT_RUNPATH and DT_RPATH offsets per kind; `string_bytes: 4096` includes the NUL terminator of each decoded string; `table_bytes: 1048576` bounds each program header or dynamic table. Values must be positive safe integers. A budget refusal names the field that needs headroom; partial package facts and unresolved -coverage remain visible. The policy applies during this capture's admission, -without restarting REA, and explicit selections are retained in Evidence. Raising -content retention or a worker heap does not raise these inventory budgets. +coverage remain visible. A table whose declared range extends past the file +is reported as truncated before the table budget is applied. The policy applies +during this capture's admission, without restarting REA, and explicit selections +are retained in Evidence. Raising content retention or a worker heap does not +raise these inventory budgets. Choosing a new capture still launches the selected application; changing these read budgets does not add execution authority or recover an earlier capture's omitted inventory. diff --git a/scripts/verify/baselines/raw-buffer-read.json b/scripts/verify/baselines/raw-buffer-read.json index 1e00667a19..1c91ed130f 100644 --- a/scripts/verify/baselines/raw-buffer-read.json +++ b/scripts/verify/baselines/raw-buffer-read.json @@ -19,7 +19,6 @@ "src/domain/apple/plistScalars.ts": 6, "src/domain/apple/propertyListDecodeBudget.ts": 2, "src/domain/binaryTarget.ts": 10, - "src/domain/desktop/elfDependencies.ts": 4, "src/domain/desktop/openPgpKeys.ts": 2, "src/domain/dosMz.ts": 10, "src/domain/elfDynamicEntry.ts": 4, diff --git a/src/desktop/LibraryPackageInventory.test.ts b/src/desktop/LibraryPackageInventory.test.ts index 4f099c02ba..5453f626a2 100644 --- a/src/desktop/LibraryPackageInventory.test.ts +++ b/src/desktop/LibraryPackageInventory.test.ts @@ -10,6 +10,41 @@ import { inspectLibraryPackages } from "./LibraryPackageInventory.js"; import { elfImage } from "../domain/desktop/elfImage.fixture.js"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; +it("reports truncated ELF dependency tables without suggesting more capture headroom", async () => { + const root = await createTestTempDirectory("rea-library-table-framing-"); + const executable = join(root, "app"); + const needed = Array.from( + { length: 8 }, + (_, index) => `lib${String(index)}.so`, + ); + const image = elfImage({ needed }); + const policy = desktopLibraryPolicy({ table_bytes: 112 }); + const inspect = () => + inspectLibraryPackages( + executable, + "app", + async () => { + throw new Error( + "An unreadable dependency table must stop host queries", + ); + }, + undefined, + policy, + ); + await writeFile(executable, image); + expect((await inspect()).library_packages_reason).toContain( + "ELF dynamic table reached library-policy.table_bytes=112 bytes", + ); + image.writeBigUInt64LE(BigInt(image.length), 64 + 56 + 8); + await writeFile(executable, image); + expect(await inspect()).toEqual({ + library_packages: [], + missing_libraries: [], + unpackaged_libraries: [], + library_packages_reason: `the libraries of ${executable} were not read: ELF dynamic table is truncated`, + }); +}); + it.skipIf(process.platform === "win32").each(["/", "/.", "/.."])( "preserves a dependency's trailing directory requirement: %s", async (suffix) => { diff --git a/src/domain/desktop/elfDependencies.test.ts b/src/domain/desktop/elfDependencies.test.ts index 57e5367c43..84c4cb1455 100644 --- a/src/domain/desktop/elfDependencies.test.ts +++ b/src/domain/desktop/elfDependencies.test.ts @@ -149,6 +149,40 @@ describe("readElfDependencies", () => { }); }); +it.each([ + { table: "program header", allowance: 111, offsetField: 32 }, + { table: "dynamic", allowance: 112, offsetField: 64 + 56 + 8 }, +])( + "validates the declared ELF $table extent before its table budget", + async ({ table, allowance, offsetField }) => { + const needed = Array.from( + { length: 8 }, + (_, index) => `lib${String(index)}.so`, + ); + const image = elfImage({ needed }); + const policy = desktopLibraryPolicy({ table_bytes: allowance }); + const read = () => + readElfDependencies(readImage(image), image.length, policy); + expect(await read()).toMatchObject({ + ok: false, + error: { + reason: expect.stringContaining( + `ELF ${table} table reached library-policy.table_bytes=${String(allowance)} bytes`, + ), + }, + }); + image.writeBigUInt64LE(BigInt(image.length), offsetField); + expect(await read()).toEqual({ + ok: false, + error: { + kind: "unreadable", + identity: { machine: 183, bits: 64, byte_order: "little" }, + reason: `ELF ${table} table is truncated`, + }, + }); + }, +); + describe("ELF filesystem string admission", () => { it.each(["libé.so", "lib选择.so", "\uFEFFlib.so", "lib\uFFFD.so"])( "preserves the filesystem bytes of needed library %s", diff --git a/src/domain/desktop/elfDependencies.ts b/src/domain/desktop/elfDependencies.ts index dc062d0b96..44e339bf2a 100644 --- a/src/domain/desktop/elfDependencies.ts +++ b/src/domain/desktop/elfDependencies.ts @@ -21,6 +21,7 @@ import { readElfProgramHeader, type ElfProgramHeader, } from "../elfProgramHeader.js"; +import { readElfRawTableDeclarations } from "../elfRawTableDeclarations.js"; import { observeElfStringTableEntry } from "../elfStringTable.js"; import { allKeyed, keyedBy, uniqueKeyed } from "../keyedCollection.js"; import { err, ok, type Result } from "../result.js"; @@ -82,49 +83,52 @@ const DEPENDENCY_SINGLETONS = new Set([ DT_RUNPATH, ]); -const readExactly = async ( +const readBoundedExtent = async ( read: ReadAt, - offset: bigint, - length: bigint, - size: number, -): Promise => { - if (offset + length > BigInt(size)) return undefined; + { + offset, + length, + fileSize, + }: { offset: bigint; length: bigint; fileSize: number }, + label: string, + tablePolicy?: DesktopLibraryPolicy, +): Promise> => { + if (offset + length > BigInt(fileSize)) return err(`${label} is truncated`); + if (tablePolicy !== undefined && length > BigInt(tablePolicy.table_bytes)) + return err(desktopLibraryLimit(tablePolicy, "table_bytes", label)); const bytes = await read(Number(offset), Number(length)); - return bytes.byteLength === Number(length) ? Buffer.from(bytes) : undefined; + return bytes.byteLength === Number(length) + ? ok(Buffer.from(bytes)) + : err(`${label} is truncated`); }; const programHeaders = async ( read: ReadAt, header: Buffer, size: number, - bits: 32 | 64, + { bits, byte_order: byteOrder }: ElfLibraryIdentity, policy: DesktopLibraryPolicy, ): Promise> => { - const little = header[5] === 1; - const view = new DataView(header.buffer, header.byteOffset, header.length); - const offset = - bits === 64 - ? view.getBigUint64(32, little) - : BigInt(view.getUint32(28, little)); - const entrySize = view.getUint16(bits === 64 ? 54 : 42, little); - const count = view.getUint16(bits === 64 ? 56 : 44, little); + const little = byteOrder === "little"; + const declaration = readElfRawTableDeclarations(header, { + elfClass: bits, + byteOrder, + }); + if (!declaration.ok) return err(declaration.error.message); + const { offset, width: entrySize, count } = declaration.value.programs; if (entrySize < (bits === 64 ? 56 : 32) || count === 0xffff) return err("unsupported ELF program header table"); - if (entrySize * count > policy.table_bytes) - return err( - desktopLibraryLimit(policy, "table_bytes", "ELF program header table"), - ); - const table = await readExactly( + const table = await readBoundedExtent( read, - offset, - BigInt(entrySize * count), - size, + { offset, length: BigInt(entrySize * count), fileSize: size }, + "ELF program header table", + policy, ); - if (table === undefined) return err("ELF program header table is truncated"); + if (!table.ok) return table; const headers: ElfProgramHeader[] = []; for (let index = 0; index < count; index += 1) { const parsed = readElfProgramHeader( - table.subarray(index * entrySize, (index + 1) * entrySize), + table.value.subarray(index * entrySize, (index + 1) * entrySize), bits, little, ); @@ -239,9 +243,17 @@ const dynamicStrings = async ( BigInt(policy.string_bytes) < file.bytes - offset ? BigInt(policy.string_bytes) : file.bytes - offset; - const window = await readExactly(read, file.at + offset, length, file.size); - if (window === undefined) return err("ELF dynamic string is truncated"); - const observed = observeElfStringTableEntry(window, 0, policy.string_bytes); + const window = await readBoundedExtent( + read, + { offset: file.at + offset, length, fileSize: file.size }, + "ELF dynamic string", + ); + if (!window.ok) return window; + const observed = observeElfStringTableEntry( + window.value, + 0, + policy.string_bytes, + ); if (observed.state !== "terminated") return err( length < file.bytes - offset @@ -325,7 +337,7 @@ const readDependencyTables = async ( > => { const { bits } = identity; const little = identity.byte_order === "little"; - const headers = await programHeaders(read, header, size, bits, policy); + const headers = await programHeaders(read, header, size, identity, policy); if (!headers.ok) return headers; const none = { needed: [], runpath: undefined, rpath: [] }; // The ELF gABI allows one PT_DYNAMIC; a repeat leaves the table ambiguous. @@ -337,11 +349,14 @@ const readDependencyTables = async ( ); const dynamic = dynamicProgram.value; if (dynamic === undefined) return ok(none); - if (dynamic.fileSize > BigInt(policy.table_bytes)) - return err(desktopLibraryLimit(policy, "table_bytes", "ELF dynamic table")); - const table = await readExactly(read, dynamic.offset, dynamic.fileSize, size); - if (table === undefined) return err("ELF dynamic table is truncated"); - const tags = dynamicTags(table, bits, little, policy); + const table = await readBoundedExtent( + read, + { offset: dynamic.offset, length: dynamic.fileSize, fileSize: size }, + "ELF dynamic table", + policy, + ); + if (!table.ok) return table; + const tags = dynamicTags(table.value, bits, little, policy); if (!tags.ok) return tags; const { strtab, strsz, needed, runpath, rpath } = tags.value; if (needed.size + runpath.size + rpath.size === 0) return ok(none);