From 3f0f9932336a3cb29aa69a67d9f5994cb835d5b7 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:07:40 -0500 Subject: [PATCH] fix(macho): share lazy dylib linkedit command facts (#12541) --- src/domain/apple/machoLoadCommandReader.ts | 11 ++++--- src/domain/apple/machoLoadCommands.ts | 1 + src/native/MachoBinaryProjection.test.ts | 35 +++++++++++++++++++++- src/native/MachoBinaryProjection.ts | 8 ++--- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/domain/apple/machoLoadCommandReader.ts b/src/domain/apple/machoLoadCommandReader.ts index 4cf84b669b..8a51238177 100644 --- a/src/domain/apple/machoLoadCommandReader.ts +++ b/src/domain/apple/machoLoadCommandReader.ts @@ -794,14 +794,17 @@ const decodeLazyLoadDylibInfoCommand = ( body: Uint8Array, index: number, ): LazyLoadDylibInfoCommand => { - if (body.byteLength < 16) + const data = readMachoLinkeditDataCommand( + LC.LAZY_LOAD_DYLIB_INFO, + body, + true, + ); + if (data === null) throw machoIssue( "malformed", `load command ${index} is too short for LC_LAZY_LOAD_DYLIB_INFO`, ); - const reader = machoWordReader(body, true); - const dataOffset = reader.u32(8); - const dataSize = reader.u32(12); + const { dataOffset, dataSize } = data; if (dataSize < LAZY_INFO_HEADER_BYTES) throw machoIssue( "malformed", diff --git a/src/domain/apple/machoLoadCommands.ts b/src/domain/apple/machoLoadCommands.ts index 78cc6b653b..454a73cb9c 100644 --- a/src/domain/apple/machoLoadCommands.ts +++ b/src/domain/apple/machoLoadCommands.ts @@ -254,6 +254,7 @@ const FIXED_WORD_FIELDS: ReadonlyMap = new Map([ [LC.ATOM_INFO, ["dataoff", "datasize"]], [LC.FUNCTION_VARIANTS, ["dataoff", "datasize"]], [LC.FUNCTION_VARIANT_FIXUPS, ["dataoff", "datasize"]], + [LC.LAZY_LOAD_DYLIB_INFO, ["dataoff", "datasize"]], ]); /** Names of a command's fixed uint32 fields, or undefined for other layouts. */ diff --git a/src/native/MachoBinaryProjection.test.ts b/src/native/MachoBinaryProjection.test.ts index a57979f9b2..ead7084af4 100644 --- a/src/native/MachoBinaryProjection.test.ts +++ b/src/native/MachoBinaryProjection.test.ts @@ -3,6 +3,7 @@ import { projectMachoBinary } from "./MachoBinaryProjection.js"; import type { MachoCommandNames } from "../domain/apple/machoCommandNames.js"; import { readMachoCommandNames } from "../domain/apple/machoCommandNames.js"; import { + lazyLoadDylibInfoCommand, machoImage, readerOf, segmentCommand, @@ -392,6 +393,30 @@ it("distinguishes exact fixed command extents from unmodeled trailing bytes", as ]); }); +it("reports lazy dylib linkedit fields and retains a digest when the command has a tail", async () => { + const exact = lazyLoadDylibInfoCommand(0x340, 56); + const extended = Buffer.concat([exact, Buffer.alloc(8, 0xa5)]); + extended.writeUInt32LE(extended.length, 4); + const image = machoImage({ commands: [exact, extended] }); + const result = projectMachoBinary( + await readMachoCommandNames(readerOf(image), image.length, 1024, 8), + ); + + expect(result.commands[0]?.fields).toMatchObject({ + dataoff: 0x340, + datasize: 56, + }); + expect(result.commands[0]?.fields).not.toHaveProperty("command_sha256"); + expect(result.commands[1]?.fields).toMatchObject({ + dataoff: 0x340, + datasize: 56, + command_sha256: expect.stringMatching(/^[a-f0-9]{64}$/u), + }); + expect(result.dependencyLimitations).toEqual([ + "The linkedit dependency payload is not decoded.", + ]); +}); + it("preserves leading UTF-8 BOM characters in names and dependency paths", () => { const marker = "\uFEFF"; const commands = multilineBinary.commands.map((row) => ({ @@ -449,7 +474,15 @@ it("reports undecoded lazy dependency payloads and unsafe entrypoint offsets", ( flags: 0, }, commands: [ - { index: 0, command: 0x3a, offset: 0, size: 16, rawSha256: null }, + { + index: 0, + command: 0x3a, + offset: 0, + size: 16, + fixedFields: { dataoff: 0x340, datasize: 56 }, + fullyDecoded: true, + rawSha256: null, + }, { index: 1, command: 0x80000028, diff --git a/src/native/MachoBinaryProjection.ts b/src/native/MachoBinaryProjection.ts index 8580ba0c2b..86eeb5c1b9 100644 --- a/src/native/MachoBinaryProjection.ts +++ b/src/native/MachoBinaryProjection.ts @@ -515,13 +515,13 @@ export const projectMachoBinary = ( } if (kind === "LC_THREAD" || kind === "LC_UNIXTHREAD") entrypointLimitations.add("Thread-state entrypoints are not decoded."); - if (kind === "LC_LAZY_LOAD_DYLIB_INFO") - dependencyLimitations.add( - "The linkedit dependency payload is not decoded.", - ); if (kind.startsWith("LC_VERSION_MIN_") || kind === "LC_BUILD_VERSION") buildLimitations.add(`The ${kind} payload is not decoded.`); } + if (kind === "LC_LAZY_LOAD_DYLIB_INFO") + dependencyLimitations.add( + "The linkedit dependency payload is not decoded.", + ); commands.push({ index: row.index, kind, fields }); } return {