fix(pe): preserve biased export ordinals
Accept the format-derived ordinal range for PE export slots.
This commit is contained in:
1 parent
0473a3091c
commit
73d85f6299
5 files changed
+99
-17
No files matched your search
@@ -4,6 +4,9 @@ import { absoluteLocalPathStringSchema } from "../localPath.js";
|
||||
|
||||
const uint = z.number().int().min(0).max(0xffffffff);
|
||||
const address = z.string().regex(/^0x[0-9a-f]+$/u);
|
||||
// PE ordinals are the DWORD OrdinalBase plus an index into the DWORD-sized
|
||||
// export address table. The last valid index is one less than its entry count.
|
||||
const MAX_PE_EXPORT_ORDINAL = 0xffffffff + 0xfffffffe;
|
||||
|
||||
/** Hard ceiling on the caller-selected input read budget. */
|
||||
export const MAX_INSPECT_NATIVE_EXPORTS_FILE_BYTES = 536870912;
|
||||
@@ -33,7 +36,7 @@ export type InspectNativeExportsInput = z.infer<
|
||||
*/
|
||||
const peExportSchema = z.strictObject({
|
||||
name: z.string().min(1).nullable(),
|
||||
ordinal: z.number().int().min(0).max(0xffff),
|
||||
ordinal: z.number().int().min(0).max(MAX_PE_EXPORT_ORDINAL),
|
||||
address: address.nullable(),
|
||||
forwarder: z.string().min(1).nullable(),
|
||||
});
|
||||
|
||||
@@ -115,16 +115,29 @@ describe("PE export directories", () => {
|
||||
expect(result.limitations[0]).toContain("declares no export directory");
|
||||
});
|
||||
|
||||
it("refuses ordinals beyond the 16-bit ordinal space", () => {
|
||||
expect(() =>
|
||||
parseNativeExports(
|
||||
it("preserves biased ordinals beyond WORD and DWORD", () => {
|
||||
const cases = [
|
||||
{ ordinalBase: 0x10000, index: 0, ordinal: 0x10000 },
|
||||
{ ordinalBase: 0xffffffff, index: 0, ordinal: 0xffffffff },
|
||||
{
|
||||
ordinalBase: 0xffffffff,
|
||||
index: 1,
|
||||
ordinal: 0x1_0000_0000,
|
||||
},
|
||||
];
|
||||
|
||||
for (const { ordinalBase, index, ordinal } of cases) {
|
||||
const result = parse(
|
||||
peExportImage({
|
||||
functions: [{ rva: 0x2000 }],
|
||||
ordinalBase: 0xffff + 1,
|
||||
ordinalBase,
|
||||
functions: Array.from({ length: index + 1 }, (_, row) =>
|
||||
row === index ? { rva: 0x2000 } : null,
|
||||
),
|
||||
names: [{ name: "Named", index }],
|
||||
}),
|
||||
artifact,
|
||||
),
|
||||
).toThrow("16-bit ordinals");
|
||||
);
|
||||
expect(result.exports).toMatchObject([{ name: "Named", ordinal }]);
|
||||
}
|
||||
});
|
||||
|
||||
it("ignores a trailing unused slot after the last 16-bit ordinal", () => {
|
||||
|
||||
@@ -188,11 +188,6 @@ const parsePeExports = (bytes: Buffer, artifact: Artifact): NativeExports => {
|
||||
// A zero slot is an unused ordinal, not an export.
|
||||
if (rva === 0) continue;
|
||||
const ordinal = ordinalBase + index;
|
||||
if (ordinal > 0xffff)
|
||||
throw new BinaryFormatFailure(
|
||||
"malformed",
|
||||
"PE exported ordinal exceeds 16-bit ordinals",
|
||||
);
|
||||
let target: Pick<PeExport, "address" | "forwarder">;
|
||||
if (rva >= directory.virtual_address && rva < forwarderEnd) {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user