diff --git a/docs/elf-dependencies.md b/docs/elf-dependencies.md index 931c11c082..9a51fb0f65 100644 --- a/docs/elf-dependencies.md +++ b/docs/elf-dependencies.md @@ -92,9 +92,11 @@ path, SHA-256, and byte count as a partial observation. Cyclic `vn_next` / unsupported ELF extended numbering return no complete inventory. Each dynamic string and `PT_INTERP` path is scanned for its terminator across at -most 65536 bytes. Unreadable dynamic strings, `PT_INTERP` paths that are -unterminated or end past that limit, and tables that cannot be located from -section headers or `DT_*` tags are reported as partial coverage with `issues`. +most 65536 bytes. A scan-limit issue means the admitted extent continues beyond +the checked bytes. If the scan reaches the end of the extent without a +terminator, the issue identifies an unterminated string. Unreadable strings and +tables that cannot be located from section headers or `DT_*` tags yield partial +coverage with `issues`. When the symbol count from `DT_HASH` or `DT_GNU_HASH` exceeds the located `.dynsym` table, only the symbols inside that table are read and coverage is partial. A GNU version chain whose links and declared count diff --git a/src/domain/elfStringTable.ts b/src/domain/elfStringTable.ts index bfed8675c5..b57eb6afe9 100644 --- a/src/domain/elfStringTable.ts +++ b/src/domain/elfStringTable.ts @@ -15,6 +15,8 @@ export type ElfStringTableObservation = | { readonly state: "truncated"; readonly bytes: null; + /** More bytes of this table remain outside the checked window. */ + readonly scanLimited: boolean; readonly examinedBytes: number; readonly contentBytes: number; }; @@ -33,6 +35,7 @@ export const observeElfStringTableEntry = ( return { state: "truncated", bytes: null, + scanLimited: window.length < table.length - offset, examinedBytes: window.length, contentBytes: window.length, }; diff --git a/src/native/elf/ElfDependencyParser.ts b/src/native/elf/ElfDependencyParser.ts index c6ae4f7d81..14bdb07c54 100644 --- a/src/native/elf/ElfDependencyParser.ts +++ b/src/native/elf/ElfDependencyParser.ts @@ -351,10 +351,7 @@ class DynamicStrings { entry = { state: "outside", name: null, location: null }; else if (observed.state === "truncated") entry = { - state: - observed.examinedBytes === ELF_STRING_SCAN_BYTES - ? "scan-limit" - : "unterminated", + state: observed.scanLimited ? "scan-limit" : "unterminated", name: null, location: this.table.offset + offset, }; @@ -1172,15 +1169,15 @@ const parseInterpreter = ( try { const body = reader.range(interp.offset, interp.bytes, "PT_INTERP"); const observed = observeElfStringTableEntry(body, 0, ELF_STRING_SCAN_BYTES); - if (observed.state !== "terminated" || observed.bytes === null) { + if (observed.state !== "terminated") { issues.push({ code: "name_unreadable", location: hex(interp.offset), - message: `PT_INTERP path ${unreadString(interp.bytes > ELF_STRING_SCAN_BYTES)}`, + message: `PT_INTERP path ${unreadString(observed.state === "truncated" && observed.scanLimited)}`, }); return { status: "unreadable", path: null, location: interp }; } - const path = observed.bytes.toString("latin1"); + const path = elfStringTableText(observed); if (path.length === 0) { issues.push({ code: "name_unreadable", diff --git a/tests/acceptance/elfDependenciesCli.test.ts b/tests/acceptance/elfDependenciesCli.test.ts index 9cb04cf3e8..eb12d34daa 100644 --- a/tests/acceptance/elfDependenciesCli.test.ts +++ b/tests/acceptance/elfDependenciesCli.test.ts @@ -63,6 +63,76 @@ cliTest( }, ); +cliTest( + "distinguishes the ELF string scan cap from an observed unterminated table end", + async ({ cli }) => { + const root = await createTestTempDirectory( + "rea-elf-dependencies-string-end-", + ); + for (const { kind, length, truncate, message } of [ + { + kind: "unterminated-at-cap", + length: 65536, + truncate: true, + message: "DT_SONAME string is unterminated in the dynamic string table", + }, + { + kind: "terminator-beyond-cap", + length: 65536, + truncate: false, + message: + "DT_SONAME string has no terminator within the 65536-byte string scan limit in the dynamic string table", + }, + { + kind: "terminator-at-cap", + length: 65535, + truncate: false, + message: null, + }, + ]) { + const path = join(root, kind); + // Place SONAME last, with a nonzero string-table index and no neighboring + // string that could supply a terminator after the selected bytes. + await writeFile( + path, + elfDependencyFixture({ + soname: "s".repeat(length), + runpath: null, + imports: [], + verneed: [], + verdef: [], + truncateDynstr: truncate, + }), + ); + const result = await cli.run({ + arguments: ["inspect-elf-dependencies", path, "--json"], + }); + expect(result.exitCode, result.stderr).toBe(0); + const report = dependenciesContract.outputSchema.parse( + result.json, + ).normalized_result; + if (!("dynamic" in report)) + throw new Error("Expected an inline ELF dependency inventory"); + expect( + report.needed.map(({ name }) => name), + kind, + ).toEqual(["libstdc++.so.6", "libm.so.6", "libc.so.6"]); + const issue = report.issues.find( + ({ location }) => location === "DT_SONAME", + ); + if (message === null) { + expect(issue, kind).toBeUndefined(); + expect(report.dynamic.soname?.name?.length, kind).toBe(length); + expect(report.coverage.status, kind).toBe("complete"); + } else { + expect(issue, kind).toMatchObject({ code: "name_unreadable", message }); + expect(report.dynamic.soname?.name, kind).toBeNull(); + expect(report.coverage.status, kind).toBe("partial"); + } + } + }, +); + cliTest( "resolves a relative ELF path against the working directory", async ({ cli }) => {