fix(native): refuse truncated executable headers at target admission

Target admission accepted a thin Mach-O after reading only its magic and
CPU type, so a 12-byte prefix opened as a valid arm64 executable and the
first provider query failed as a generic execution failure. ELF headers
were likewise accepted after 20 bytes, and a FAT file whose selected slice
lies beyond its end was admitted.

Require the complete fixed header (Mach-O 28 or 32 bytes, ELF 52 or 64),
Mach-O load commands that fit in the file, and a selected FAT slice within
the file, checking commitments against the file size rather than the
probed prefix. Each refusal names the failed constraint.

Fixes #1189

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-09 03:51:37 +00:00
co-authored by Claude Opus 5.5
parent e1afbc1914
commit 0b04d384cc
6 changed files with 137 additions and 20 deletions
@@ -325,6 +325,32 @@ describe("binary target I/O: explicit kinds and executable headers", () => {
architecture: "x86_64",
});
});
it("refuses a truncated Mach-O header before any provider sees it", async () => {
const directory = await createTestTempDirectory("rea-target-");
const path = join(directory, "truncated");
await writeFile(path, thinMach(0xcffaedfe, 0x0100000c).subarray(0, 12));
const result = await parseBinaryTarget(path, directory, "arm64");
expect(result.ok).toBe(false);
expect(result.ok ? undefined : result.error).toMatchObject({
path,
reason:
"truncated Mach-O header: a 64-bit header needs 32 bytes; the file has 12",
});
});
it("checks Mach-O load commands against the whole file beyond the probe", async () => {
const directory = await createTestTempDirectory("rea-target-");
const path = join(directory, "large-commands");
const header = thinMach(0xcffaedfe, 0x0100000c);
header.writeUInt32LE(8192, 20);
await writeFile(path, Buffer.concat([header, Buffer.alloc(8192)]));
const result = await parseBinaryTarget(path, directory, "arm64");
expect(result.ok && result.value).toMatchObject({
format: "mach-o",
architecture: "arm64",
});
});
});
describe("app executable filename fidelity", () => {