From b1479de3dd954ec35657d695c3c47d85cf88aa6d Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 07:11:01 -0500 Subject: [PATCH] fix(macho): validate command counts before record limits (#12305) --- src/domain/apple/machoCommandNames.ts | 4 +- src/domain/apple/machoLoadCommands.ts | 15 +++++-- src/native/MachoProjectionBudget.test.ts | 51 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/domain/apple/machoCommandNames.ts b/src/domain/apple/machoCommandNames.ts index a0984d4628..c246cf0994 100644 --- a/src/domain/apple/machoCommandNames.ts +++ b/src/domain/apple/machoCommandNames.ts @@ -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 = []; diff --git a/src/domain/apple/machoLoadCommands.ts b/src/domain/apple/machoLoadCommands.ts index 1605f427db..83702f7f7b 100644 --- a/src/domain/apple/machoLoadCommands.ts +++ b/src/domain/apple/machoLoadCommands.ts @@ -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 => { 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 ) diff --git a/src/native/MachoProjectionBudget.test.ts b/src/native/MachoProjectionBudget.test.ts index 5184b204ea..affb33a23c 100644 --- a/src/native/MachoProjectionBudget.test.ts +++ b/src/native/MachoProjectionBudget.test.ts @@ -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);