From ef3eb1e46e37836fe91984ec15a8a1d0a352fe5c Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 17:23:52 -0500 Subject: [PATCH] fix(native-exports): verify the declared PE directory size (#13661) --- docs/guides/native/binary-diagnostics.md | 1 + src/domain/native/nativeExportParser.test.ts | 128 ++++++++++--------- src/domain/native/nativeExportParser.ts | 5 + 3 files changed, 74 insertions(+), 60 deletions(-) diff --git a/docs/guides/native/binary-diagnostics.md b/docs/guides/native/binary-diagnostics.md index 4021dfb635..2a61090fb9 100644 --- a/docs/guides/native/binary-diagnostics.md +++ b/docs/guides/native/binary-diagnostics.md @@ -481,6 +481,7 @@ rea inspect-native-exports ./libexample.so --detail summary --evidence_output ./ Unreadable names, out-of-range name ordinals and unreadable forwarders are listed in `issues`, their entries are not reported, and coverage is `partial`. +A declared PE export directory shorter than its 40-byte table is malformed. PE export tables and strings use file-backed headers and declared section content. A name's terminating NUL must also fit within those mapped bytes. PE export names are bounded null-terminated byte strings and may continue diff --git a/src/domain/native/nativeExportParser.test.ts b/src/domain/native/nativeExportParser.test.ts index 6133d044ee..cdb2a4b079 100644 --- a/src/domain/native/nativeExportParser.test.ts +++ b/src/domain/native/nativeExportParser.test.ts @@ -329,29 +329,26 @@ const peExportNameAcrossSections = (overlapAtRva?: number): Buffer => { }; describe("PE export name coverage", () => { - it("reads a bounded export name across adjacent section mappings", () => { - const result = parse(peExportNameAcrossSections()); - expect(result).toMatchObject({ - exports: [ - { - name: "BoundaryName", - ordinal: 1, - address: "0x180002000", - forwarder: null, - }, - ], - coverage: { status: "complete", examined_entries: 1, exports: 1 }, - issues: [], - }); - }); - - it("ignores conflicting mappings after the export name terminator", () => { - const result = parse(peExportNameAcrossSections(0x1409)); - - expect(result.exports).toMatchObject([{ name: "BoundaryName" }]); - expect(result.issues).toEqual([]); - expect(result.coverage.status).toBe("complete"); - }); + it.each([ + ["with no conflicting mapping", undefined], + ["with a conflicting mapping after its NUL", 0x1409], + ] as const)( + "reads an export name across adjacent sections %s", + (_, conflict) => { + expect(parse(peExportNameAcrossSections(conflict))).toMatchObject({ + exports: [ + { + name: "BoundaryName", + ordinal: 1, + address: "0x180002000", + forwarder: null, + }, + ], + coverage: { status: "complete", examined_entries: 1, exports: 1 }, + issues: [], + }); + }, + ); it("rejects conflicting mappings that overlap the export name", () => { const result = parse(peExportNameAcrossSections(0x1404)); @@ -386,46 +383,57 @@ describe("PE export name coverage", () => { }); }); -describe("PE forwarder directory bounds", () => { - it.each([32, 64] as const)( - "requires the complete PE%d forwarder string to remain inside the export directory", - (bits) => { - const bytes = peExportImage({ - bits, - functions: [{ rva: 0x11fc }, { rva: 0x2000 }], - names: [{ name: "Kept", index: 1 }], - }); - // The string starts in .edata's declared export range but its NUL is - // outside it. The adjacent section bytes remain mapped and readable. - bytes.write("DLL.Target\0", 0x5fc, "latin1"); - const result = parse(bytes); +const peExportDirectorySizeOffset = (bits: 32 | 64): number => + 152 + (bits === 64 ? 112 : 96) + 4; - expect(result.exports).toMatchObject([{ name: "Kept", ordinal: 2 }]); - expect(result.issues).toMatchObject([ - { code: "malformed_entry", location: "0x11fc" }, - ]); - expect(result.coverage).toMatchObject({ - status: "partial", - examined_entries: 2, - exports: 1, - }); - }, - ); +it.each([32, 64] as const)( + "bounds a PE%d forwarder by the directory even when adjacent bytes are readable", + (bits) => { + const bytes = peExportImage({ + bits, + functions: [{ rva: 0x11f5 }, { rva: 0x2000 }], + names: [{ name: "Kept", index: 1 }], + }); + bytes.write("DLL.Target\0", 0x5f5, "latin1"); + expect(parse(bytes)).toMatchObject({ + exports: [ + { name: null, ordinal: 1, forwarder: "DLL.Target" }, + { name: "Kept", ordinal: 2 }, + ], + coverage: { status: "complete", examined_entries: 2, exports: 2 }, + issues: [], + }); - it.each([32, 64] as const)( - "accepts a PE%d forwarder whose NUL is the final export-directory byte", - (bits) => { - const bytes = peExportImage({ bits, functions: [{ rva: 0x11f5 }] }); - bytes.write("DLL.Target\0", 0x5f5, "latin1"); + // The same NUL remains readable but now lies one byte outside the directory. + bytes.writeUInt32LE(0x1ff, peExportDirectorySizeOffset(bits)); + expect(parse(bytes)).toMatchObject({ + exports: [{ name: "Kept", ordinal: 2 }], + issues: [{ code: "malformed_entry", location: "0x11f5" }], + coverage: { status: "partial", examined_entries: 2, exports: 1 }, + }); + }, +); - expect(parse(bytes)).toMatchObject({ - exports: [{ name: null, ordinal: 1, forwarder: "DLL.Target" }], - coverage: { status: "complete", exports: 1 }, - issues: [], - }); - }, - ); -}); +it.each([32, 64] as const)( + "requires the fixed PE%d export table to fit in its declared directory", + (bits) => { + const bytes = peExportImage({ + bits, + functions: [{ rva: 0x2000 }], + names: [{ name: "Kept", index: 0 }], + }); + const sizeOffset = peExportDirectorySizeOffset(bits); + bytes.writeUInt32LE(40, sizeOffset); + expect(parse(bytes)).toMatchObject({ + export_directory: { size: 40 }, + exports: [{ name: "Kept", ordinal: 1 }], + coverage: { status: "complete", examined_entries: 1, exports: 1 }, + }); + + bytes.writeUInt32LE(39, sizeOffset); + expect(refusal(bytes)).toMatchObject({ failure: { kind: "malformed" } }); + }, +); it("requires the declared section to contain the fixed PE export name ordinal table", () => { const bytes = peExportImage({ diff --git a/src/domain/native/nativeExportParser.ts b/src/domain/native/nativeExportParser.ts index 86d3dc66fe..3dd8083755 100644 --- a/src/domain/native/nativeExportParser.ts +++ b/src/domain/native/nativeExportParser.ts @@ -196,6 +196,11 @@ const parsePeExports = (bytes: Buffer, artifact: Artifact): NativeExports => { ...PE_LIMITATIONS, ], }; + if (directory.size < PE_EXPORT_DIRECTORY_BYTES) + throw new BinaryFormatFailure( + "malformed", + `PE export directory declares ${String(directory.size)} bytes, below the ${String(PE_EXPORT_DIRECTORY_BYTES)}-byte table`, + ); const reader = new BinaryReader(bytes, true, "pe"); const mappingIndex = createFileMappingIndex( peDeclaredFileMappings(image),