fix(targets): recognize Windows headers after short DOS stubs

Observe MZ header declarations and signature uncertainty at the domain boundary, then reuse those facts in bounded target resolution, inventory, and Go PE parsing. Preserve DOS relocation ownership and fail closed when the relevant bytes remain unexamined.

Validation: 59 domain tests, 130 target/Go boundary tests, and public CLI inspection of a clang/lld-link PE DLL with a short DOS header. Fixes #2172.
This commit is contained in:
morluto
2026-10-13 03:47:45 +08:00
parent f611d8ce72
commit c9fed50691
7 changed files with 314 additions and 57 deletions
+12 -18
View File
@@ -1,4 +1,4 @@
import { mzWindowsHeaderOffset, parseDosMzHeader } from "../domain/dosMz.js";
import { observeMzWindowsHeader, parseDosMzHeader } from "../domain/dosMz.js";
import {
GoBinaryFormatFailure,
GoBinaryReader,
@@ -8,8 +8,8 @@ import {
} from "./GoBinaryContainer.js";
const peHeaderOffset = (reader: GoBinaryReader): number => {
const pe = mzWindowsHeaderOffset(reader.bytes);
if (pe === null) {
const windowsHeader = observeMzWindowsHeader(reader.bytes);
if (windowsHeader.state !== "declared") {
const dos = parseDosMzHeader(reader.bytes);
if (!dos.ok) throw new GoBinaryFormatFailure("malformed", dos.error);
throw new GoBinaryFormatFailure(
@@ -17,18 +17,22 @@ const peHeaderOffset = (reader: GoBinaryReader): number => {
"DOS MZ images are outside the supported ELF, PE and thin Mach-O formats",
);
}
const { offset: pe } = windowsHeader;
if (pe < 64)
throw new GoBinaryFormatFailure(
"malformed",
"PE signature overlaps the DOS header",
);
const signature = reader
.range(pe, 2, "Windows new-header signature")
.toString("latin1");
if (["NE", "LE", "LX"].includes(signature))
reader.range(pe, 24, "PE signature and COFF header");
if (["NE", "LE", "LX"].includes(windowsHeader.signature))
throw new GoBinaryFormatFailure(
"unsupported",
`${signature} images are outside the supported ELF, PE and thin Mach-O formats`,
`${windowsHeader.signature} images are outside the supported ELF, PE and thin Mach-O formats`,
);
if (windowsHeader.signature !== "PE")
throw new GoBinaryFormatFailure(
"malformed",
"PE signature is invalid or overlaps the DOS header",
);
return pe;
};
@@ -51,16 +55,6 @@ const optionalHeaderMagic = (
export const readGoPeImage = (bytes: Buffer): GoBinaryContainer => {
const reader = new GoBinaryReader(bytes, true);
const pe = peHeaderOffset(reader);
if (
!reader
.range(pe, 24, "PE signature and COFF header")
.subarray(0, 4)
.equals(Buffer.from("PE\0\0"))
)
throw new GoBinaryFormatFailure(
"malformed",
"PE signature is invalid or overlaps the DOS header",
);
const optional = pe + 24;
const optionalSize = reader.u16(pe + 20);
const magic = optionalHeaderMagic(reader, optional, optionalSize);