fix(macho): validate command counts before record limits (#12305)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 12:11:01 +00:00
1 parent a752cf99c2
commit b1479de3dd
3 files changed
+65 -5

No files matched your search

+2 -2
View File
@@ -524,13 +524,13 @@ export const readMachoCommandNames = async (
countBytes.byteOffset,
countBytes.byteLength,
).getUint32(0, table.littleEndian);
if (count > maximumRecords - records)
throw loadCommandLimit(maximumRecords, "records");
if (count > frame.size - 12)
throw malformedInput(
"macho",
"Mach-O linker option table is truncated",
);
if (count > maximumRecords - records)
throw loadCommandLimit(maximumRecords, "records");
chargeProjection(count, PROJECTED_CHILD_BYTES);
records += count;
linkerOptions = [];
+12 -3
View File
@@ -5,6 +5,8 @@ import {
readMachoHeaderPrefixFacts,
} from "./machoContainer.js";
const MACHO_LOAD_COMMAND_HEADER_BYTES = 8;
/** Header commitments needed to walk commands without retaining their payloads. */
export interface MachoLoadCommandTableHeader {
readonly headerSize: 28 | 32;
@@ -71,11 +73,15 @@ export const readMachoLoadCommandTableHeader = (
): Result<MachoHeaderDeclarations | null, string> => {
const parsed = readMachoLoadCommandDeclarations(bytes, fileSize);
if (!parsed.ok || parsed.value === null) return parsed;
const { commandBytes, headerSize } = parsed.value;
const { commandBytes, commandCount, headerSize } = parsed.value;
if (commandBytes > fileSize - headerSize)
return err(
`truncated Mach-O load commands: the header declares ${commandBytes} bytes after its ${headerSize}-byte header; the file has ${fileSize}`,
);
if (commandCount * MACHO_LOAD_COMMAND_HEADER_BYTES > commandBytes)
return err(
`Mach-O load command count ${commandCount} cannot fit in its ${commandBytes}-byte command table`,
);
return parsed;
};
@@ -107,12 +113,15 @@ export class MachoLoadCommandCursor {
string
> {
const { commandBytes, littleEndian, wide } = this.table;
if (this.#offset + 8 > commandBytes || bytes.byteLength < 8)
if (
this.#offset + MACHO_LOAD_COMMAND_HEADER_BYTES > commandBytes ||
bytes.byteLength < MACHO_LOAD_COMMAND_HEADER_BYTES
)
return err(`load command ${this.#index} starts beyond sizeofcmds`);
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const size = view.getUint32(4, littleEndian);
if (
size < 8 ||
size < MACHO_LOAD_COMMAND_HEADER_BYTES ||
size % machoLoadCommandAlignment(wide) !== 0 ||
size > commandBytes - this.#offset
)
+51
View File
@@ -113,6 +113,57 @@ it("reserves the projection envelope for an empty command table", async () => {
}
});
it.each([
{
field: "load command",
countAt: 16,
valid: 2,
invalid: 3,
maximumRecords: 1,
},
{
field: "linker option",
countAt: 40,
valid: 2,
invalid: 5,
maximumRecords: 2,
},
])(
"distinguishes an impossible $field count from a framed over-budget declaration",
async ({ field, countAt, valid, invalid, maximumRecords }) => {
const bytes = Buffer.alloc(48);
bytes.writeUInt32LE(0xfeedfacf, 0);
bytes.writeUInt32LE(16, 20);
if (field === "linker option") {
bytes.writeUInt32LE(1, 16);
bytes.writeUInt32LE(0x2d, 32);
bytes.writeUInt32LE(16, 36);
bytes.write("a\0b\0", 44);
} else {
// Two header-only commands, each with a complete eight-byte frame.
for (const offset of [32, 40]) {
bytes.writeUInt32LE(0x0a, offset);
bytes.writeUInt32LE(8, offset + 4);
}
}
const inspect = () =>
readMachoCommandNames(
async (offset, size) => bytes.subarray(offset, offset + size),
bytes.length,
128,
maximumRecords,
);
bytes.writeUInt32LE(valid, countAt);
await expect(inspect()).rejects.toMatchObject({
failure: { kind: "limit", limit: { name: "macho_load_command_records" } },
});
bytes.writeUInt32LE(invalid, countAt);
await expect(inspect()).rejects.toMatchObject({
failure: { kind: "malformed", detail: expect.stringContaining(field) },
});
},
);
it("bounds a growing linker option before reading its remaining payload", async () => {
const commandSize = 8208;
const image = Buffer.alloc(32 + commandSize);