fix(go): accept file-backed unmapped Mach-O debug segments

Validate unmapped segment file ranges without adding virtual mappings or fallback metadata addresses. Keep mapped-segment bounds and reject build-info sections declared in unmapped segments.

Validation: real unstripped Go 1.26.5 Darwin arm64 and amd64 artifacts passed CLI/MCP oracle, Evidence, source-range and unchanged-byte checks. Retained malformed-input regressions; removed the weaker synthetic acceptance test. Full cross-platform verifier not run. Fixes #2167.
This commit is contained in:
morluto
2026-10-13 03:47:45 +08:00
parent c9fed50691
commit 7fb097d72c
3 changed files with 102 additions and 20 deletions
+57 -2
View File
@@ -436,6 +436,59 @@ it("does not treat Mach-O __PAGEZERO as a module string header", () => {
expect(() => readGoBinaryImage(fixture.bytes)).toThrowError(/mapped|range/i);
});
it("rejects out-of-file bytes in an unmapped Mach-O segment", () => {
const fixture = createGoBinaryFixture({ format: "macho" });
addLegacyMapping(fixture.bytes, "macho", {
address: 0x20000n,
fileOffset: 3072,
fileSize: 1025,
memorySize: 0,
protection: 0,
});
expect(() => readGoBinaryImage(fixture.bytes)).toThrowError(
/outside the file/i,
);
});
it("rejects oversized Mach-O file contents when initial protection is set", () => {
const fixture = createGoBinaryFixture({ format: "macho" });
addLegacyMapping(fixture.bytes, "macho", {
address: 0x20000n,
fileOffset: 3072,
fileSize: 512,
memorySize: 0,
protection: 3,
});
expect(() => readGoBinaryImage(fixture.bytes)).toThrowError(
/addresses or sizes are invalid/i,
);
});
it("rejects build-info declared inside an unmapped Mach-O segment", () => {
const fixture = createGoBinaryFixture({ format: "macho" });
const command = 32 + 72 + 80;
const size = 72;
fixture.bytes.writeUInt32LE(2, 16);
fixture.bytes.writeUInt32LE(304, 20);
fixture.bytes.writeUInt32LE(0x19, command);
fixture.bytes.writeUInt32LE(size + 80, command + 4);
fixture.bytes.write("__DWARF", command + 8);
fixture.bytes.writeBigUInt64LE(0x10000n, command + 24);
fixture.bytes.writeBigUInt64LE(0n, command + 32);
fixture.bytes.writeBigUInt64LE(3072n, command + 40);
fixture.bytes.writeBigUInt64LE(512n, command + 48);
fixture.bytes.writeUInt32LE(3, command + 56);
fixture.bytes.writeUInt32LE(0, command + 60);
fixture.bytes.writeUInt32LE(1, command + 64);
const section = command + size;
fixture.bytes.write("__go_buildinfo", section);
fixture.bytes.write("__DWARF", section + 16);
fixture.bytes.writeBigUInt64LE(0x10000n, section + 32);
fixture.bytes.writeBigUInt64LE(0n, section + 40);
fixture.bytes.writeUInt32LE(3072, section + 48);
expect(() => readGoBinaryImage(fixture.bytes)).toThrowError(/unmapped/i);
});
it.each(["elf", "pe", "macho"] as const)(
"refuses file aliases overlapping only part of a zero-filled %s module header",
(format) => {
@@ -541,11 +594,13 @@ const addLegacyMapping = (
fileOffset,
fileSize,
memorySize,
protection = 3,
}: {
address: bigint;
fileOffset: number;
fileSize: number;
memorySize: number;
protection?: number;
},
{ bits, little }: { bits: 32 | 64; little: boolean } = {
bits: 64,
@@ -589,7 +644,7 @@ const addLegacyMapping = (
word(entry + (wide ? 32 : 28), BigInt(memorySize));
word(entry + (wide ? 40 : 32), BigInt(fileOffset));
word(entry + (wide ? 48 : 36), BigInt(fileSize));
u32(entry + (wide ? 56 : 40), 3);
u32(entry + (wide ? 60 : 44), 3);
u32(entry + (wide ? 56 : 40), protection);
u32(entry + (wide ? 60 : 44), protection);
}
};