fix(macho): require exact legacy minimum-version command size (#12666)

This commit is contained in:
morluto authored and GitHub committed 2026-11-20 01:47:07 +00:00
1 parent 308a5b2f06
commit 46943787cb
5 files changed
+112 -17

No files matched your search

+13 -10
View File
@@ -13,7 +13,7 @@ import {
readMachoLoadCommandTableHeader,
} from "./machoLoadCommands.js";
import {
MACHO_VERSION_MIN_PLATFORMS,
classifyMachoVersionMinCommand,
type MachoVersionMinPlatform,
} from "./machoPlatform.js";
import {
@@ -508,17 +508,20 @@ export const readMachoCommandNames = async (
throw malformedInput("macho", "Mach-O linker option is unterminated");
}
let legacyBuild: MachoCommandNames["commands"][number]["legacyBuild"];
const legacyPlatform = MACHO_VERSION_MIN_PLATFORMS.get(frame.command);
if (legacyPlatform !== undefined) {
if (frame.size < 16)
throw malformedInput(
"macho",
"Mach-O minimum version command is truncated",
);
const versionMin = classifyMachoVersionMinCommand(
frame.command,
frame.size,
);
if (versionMin?.kind === "truncated")
throw malformedInput(
"macho",
"Mach-O minimum version command is truncated",
);
if (versionMin?.kind === "exact") {
const bytes = await exact(at + 8, 8);
const view = machoWordReader(bytes, table.littleEndian);
legacyBuild = {
platform: legacyPlatform,
platform: versionMin.platform,
version: view.u32(0),
sdk: view.u32(4),
};
@@ -627,7 +630,7 @@ export const readMachoCommandNames = async (
(fixedNames !== undefined && frame.size === 8 + fixedNames.length * 4) ||
(uuid !== undefined && frame.size === 24) ||
(entrypoint !== undefined && frame.size === 24) ||
(legacyBuild !== undefined && frame.size === 16) ||
legacyBuild !== undefined ||
(segmentWide !== null &&
sections !== undefined &&
frame.size ===
+10 -6
View File
@@ -29,7 +29,7 @@ import type {
} from "./dylibResolution.js";
import { machoArchitectureName } from "./machoCpuIdentity.js";
import {
MACHO_VERSION_MIN_PLATFORMS,
classifyMachoVersionMinCommand,
legacyMachoPlatform,
} from "./machoPlatform.js";
import {
@@ -765,14 +765,18 @@ const decodeCommand = (
slice.platforms.push(platform);
return;
}
const legacyPlatform = MACHO_VERSION_MIN_PLATFORMS.get(command);
if (legacyPlatform !== undefined) {
if (body.byteLength < 16)
const versionMin = classifyMachoVersionMinCommand(command, body.byteLength);
if (versionMin !== null) {
if (versionMin.kind !== "exact")
throw machoIssue(
"malformed",
`load command ${index} is too short for a version-min command`,
versionMin.kind === "truncated"
? `load command ${index} is too short for a version-min command`
: `load command ${index} is too long for a version-min command`,
);
slice.platforms.push(legacyMachoPlatform(legacyPlatform, slice.cpu_type));
slice.platforms.push(
legacyMachoPlatform(versionMin.platform, slice.cpu_type),
);
return;
}
if (command === LC.LAZY_LOAD_DYLIB_INFO)
+18 -1
View File
@@ -4,7 +4,7 @@ import { LC } from "./machoLoadCommandCodes.js";
export type MachoVersionMinPlatform = 1 | 2 | 3 | 4;
/** loader.h LC_VERSION_MIN_* command codes and their device platforms. */
export const MACHO_VERSION_MIN_PLATFORMS: ReadonlyMap<
const MACHO_VERSION_MIN_PLATFORMS: ReadonlyMap<
number,
MachoVersionMinPlatform
> = new Map([
@@ -14,6 +14,23 @@ export const MACHO_VERSION_MIN_PLATFORMS: ReadonlyMap<
[LC.VERSION_MIN_WATCHOS, 4],
]);
/**
* Legacy minimum-version commands contain exactly the four words in
* `version_min_command`. Keep framing separate from the CPU-dependent platform.
*/
export const classifyMachoVersionMinCommand = (
command: number,
size: number,
):
| { readonly kind: "exact"; readonly platform: MachoVersionMinPlatform }
| { readonly kind: "truncated" | "oversized" }
| null => {
const platform = MACHO_VERSION_MIN_PLATFORMS.get(command);
if (platform === undefined) return null;
if (size === 16) return { kind: "exact", platform };
return { kind: size < 16 ? "truncated" : "oversized" };
};
/**
* dyld derives legacy simulator platforms from the CPU type. tvOS uses
* x86_64 only; iOS and watchOS also recognize i386. Explicit LC_BUILD_VERSION
+30
View File
@@ -1,12 +1,15 @@
import { createHash } from "node:crypto";
import { expect, it } from "vitest";
import { projectMachoBinary } from "./MachoBinaryProjection.js";
import type { MachoCommandNames } from "../domain/apple/machoCommandNames.js";
import { readMachoCommandNames } from "../domain/apple/machoCommandNames.js";
import {
LC,
lazyLoadDylibInfoCommand,
machoImage,
readerOf,
segmentCommand,
versionMinCommand,
} from "../artifacts/apple/MachoImage.fixture.js";
const multilineBinary: MachoCommandNames = {
@@ -366,6 +369,33 @@ it("retains legacy command metadata and derives simulator platform from CPU", ()
});
});
it("preserves an oversized minimum-version command as raw evidence", async () => {
const oversized = Buffer.concat([
versionMinCommand(LC.VERSION_MIN_MACOSX),
Buffer.alloc(8),
]);
oversized.writeUInt32LE(oversized.length, 4);
const image = machoImage({ commands: [oversized] });
const projected = projectMachoBinary(
await readMachoCommandNames(readerOf(image), image.length, 1024, 8),
);
expect(projected.builds).toEqual([]);
expect(projected.commands).toMatchObject([
{
kind: "LC_VERSION_MIN_MACOSX",
fields: {
command_offset: 32,
command_sha256: createHash("sha256").update(oversized).digest("hex"),
},
},
]);
expect(projected.commands[0]?.fields).not.toHaveProperty("version");
expect(projected.buildLimitations).toContain(
"The LC_VERSION_MIN_MACOSX payload is not decoded.",
);
});
it("distinguishes exact fixed command extents from unmodeled trailing bytes", async () => {
const command = (size: number) => {
const bytes = Buffer.alloc(size);
@@ -13,6 +13,7 @@ import {
fatImage,
machoImage,
rpathCommand,
versionMinCommand,
} from "../../../src/artifacts/apple/MachoImage.fixture.js";
import { dylibResolutionResultSchema } from "../../../src/domain/apple/dylibResolution.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
@@ -713,6 +714,46 @@ cliTest(
},
);
cliTest(
"rejects oversized legacy minimum-version commands before admitting platform",
async ({ cli }) => {
const app = await fixtureApp();
const exact = versionMinCommand(LC.VERSION_MIN_IPHONEOS);
const oversized = Buffer.concat([exact, Buffer.alloc(8)]);
oversized.writeUInt32LE(oversized.length, 4);
const validPath = "Contents/Helpers/minimum-version";
const invalidPath = "Contents/Helpers/oversized-minimum-version";
await writeFiles(app, {
[validPath]: machoImage({ commands: [exact] }),
[invalidPath]: machoImage({ commands: [oversized] }),
});
const result = await cli.run({
arguments: ["trace-dylib-resolution", app, "--json"],
environment: ENVIRONMENT,
});
expect(result.exitCode, JSON.stringify(result.json)).toBe(0);
const trace = dylibResolutionResultSchema.parse(
(result.json as { normalized_result: unknown }).normalized_result,
);
expect(trace.images.find(({ path }) => path === validPath)).toMatchObject({
parse_status: "parsed",
slices: [{ platforms: [2] }],
});
expect(trace.images.find(({ path }) => path === invalidPath)).toMatchObject(
{
parse_status: "malformed",
reason: expect.stringContaining("version-min"),
slices: [],
},
);
expect(trace.coverage).toMatchObject({
status: "partial",
unparsed_images: expect.arrayContaining([invalidPath]),
});
},
);
cliTest(
"retains directory requirements when resolving dylib install names",
async ({ cli }) => {