fix(apple): validate physical slices and dylib platform compatibility
This commit is contained in:
@@ -10,6 +10,7 @@ export const LC = {
|
||||
RPATH: 0x8000001c,
|
||||
DYLD_ENVIRONMENT: 0x27,
|
||||
CODE_SIGNATURE: 0x1d,
|
||||
BUILD_VERSION: 0x32,
|
||||
} as const;
|
||||
|
||||
export const CPU = {
|
||||
@@ -75,6 +76,16 @@ export const codeSignatureCommand = (): Uint8Array => {
|
||||
return bytes;
|
||||
};
|
||||
|
||||
/** `build_version_command` carrying the producer's raw platform number. */
|
||||
export const buildVersionCommand = (platform: number): Uint8Array => {
|
||||
const bytes = new Uint8Array(24);
|
||||
const view = new DataView(bytes.buffer);
|
||||
view.setUint32(0, LC.BUILD_VERSION, true);
|
||||
view.setUint32(4, bytes.length, true);
|
||||
view.setUint32(8, platform, true);
|
||||
return bytes;
|
||||
};
|
||||
|
||||
/** A 64-bit (or 32-bit) little-endian Mach-O image made of the given commands. */
|
||||
export const machoImage = (options: {
|
||||
readonly cpu?: { readonly type: number; readonly subtype: number };
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
FILE_TYPE,
|
||||
LC,
|
||||
codeSignatureCommand,
|
||||
buildVersionCommand,
|
||||
dyldEnvironmentCommand,
|
||||
dylibCommand,
|
||||
dylibUseCommand,
|
||||
@@ -22,7 +23,7 @@ import {
|
||||
const read = (bytes: Uint8Array) =>
|
||||
readMachoImage(readerOf(bytes), bytes.length);
|
||||
|
||||
describe("Mach-O load command reader", () => {
|
||||
describe("Mach-O load command reader: decodes thin-image load commands", () => {
|
||||
it("decodes dylib loading commands of a thin image", async () => {
|
||||
const image = machoImage({
|
||||
fileType: FILE_TYPE.dylib,
|
||||
@@ -124,7 +125,9 @@ describe("Mach-O load command reader", () => {
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Mach-O load command reader: reads and validates universal binary slices", () => {
|
||||
it("reads every slice of FAT and FAT_64 containers in either byte order", async () => {
|
||||
for (const [wide, littleEndian] of [
|
||||
[false, false],
|
||||
@@ -163,6 +166,63 @@ describe("Mach-O load command reader", () => {
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
it("retains physical slice identity and declared build platform", async () => {
|
||||
const thin = machoImage({ commands: [buildVersionCommand(7)] });
|
||||
const parsedThin = await read(thin);
|
||||
expect(
|
||||
parsedThin.status === "parsed" && parsedThin.slices[0],
|
||||
).toMatchObject({
|
||||
slice_offset: 0,
|
||||
slice_size: thin.length,
|
||||
cpu_type: CPU.arm64.type,
|
||||
cpu_subtype: CPU.arm64.subtype,
|
||||
fat_cpu_type: null,
|
||||
fat_cpu_subtype: null,
|
||||
platform: 7,
|
||||
});
|
||||
const fat = fatImage([{ cpu: CPU.arm64, bytes: thin }]);
|
||||
const parsedFat = await read(fat);
|
||||
expect(parsedFat.status === "parsed" && parsedFat.slices[0]).toMatchObject({
|
||||
slice_offset: 4096,
|
||||
slice_size: thin.length,
|
||||
fat_cpu_type: CPU.arm64.type,
|
||||
fat_cpu_subtype: CPU.arm64.subtype,
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects FAT table/header identity mismatches and unaligned ranges", async () => {
|
||||
const original = fatImage([{ cpu: CPU.arm64, bytes: machoImage({}) }]);
|
||||
for (const [fieldOffset, value] of [
|
||||
[8, CPU.x86_64.type],
|
||||
[12, 99],
|
||||
] as const) {
|
||||
const mismatch = original.slice();
|
||||
new DataView(mismatch.buffer).setUint32(fieldOffset, value, false);
|
||||
expect(await read(mismatch)).toMatchObject({
|
||||
status: "malformed",
|
||||
reason: expect.stringContaining("disagrees"),
|
||||
});
|
||||
}
|
||||
const unaligned = original.slice();
|
||||
new DataView(unaligned.buffer).setUint32(16, 4097, false);
|
||||
expect(await read(unaligned)).toMatchObject({
|
||||
status: "malformed",
|
||||
reason: expect.stringContaining("not aligned"),
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves unknown CPU table facts when the embedded slice is Mach-O", async () => {
|
||||
const cpu = { type: 0x7fffffff, subtype: 11 };
|
||||
const facts = await read(fatImage([{ cpu, bytes: machoImage({ cpu }) }]));
|
||||
expect(facts.status === "parsed" && facts.slices[0]).toMatchObject({
|
||||
architecture: "cpu-7fffffff",
|
||||
cpu_type: cpu.type,
|
||||
cpu_subtype: cpu.subtype,
|
||||
fat_cpu_type: cpu.type,
|
||||
fat_cpu_subtype: cpu.subtype,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Mach-O load command reader inputs", () => {
|
||||
@@ -314,7 +374,7 @@ describe("Mach-O load command reader failures", () => {
|
||||
const fat = fatImage([{ cpu: CPU.arm64, bytes: machoImage({}) }]);
|
||||
expect(await read(fat.subarray(0, 4100))).toMatchObject({
|
||||
status: "malformed",
|
||||
reason: "FAT architecture 0 extends beyond the file",
|
||||
reason: "FAT architecture 0 slice range extends beyond the file",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,6 +3,11 @@ import type {
|
||||
MachoImageFacts,
|
||||
MachoSlice,
|
||||
} from "../../domain/apple/dylibResolution.js";
|
||||
import {
|
||||
readFatSliceDeclarations,
|
||||
validateFatSlice,
|
||||
type FatSliceDeclaration,
|
||||
} from "../../domain/apple/machoContainer.js";
|
||||
|
||||
/** Read `length` bytes at `offset`; shorter results mean end of file. */
|
||||
export type ReadAt = (offset: number, length: number) => Promise<Uint8Array>;
|
||||
@@ -34,6 +39,11 @@ const LC_LOAD_UPWARD_DYLIB = 0x80000023;
|
||||
const LC_RPATH = 0x8000001c;
|
||||
const LC_DYLD_ENVIRONMENT = 0x27;
|
||||
const LC_CODE_SIGNATURE = 0x1d;
|
||||
const LC_VERSION_MIN_MACOSX = 0x24;
|
||||
const LC_VERSION_MIN_IPHONEOS = 0x25;
|
||||
const LC_VERSION_MIN_TVOS = 0x2f;
|
||||
const LC_VERSION_MIN_WATCHOS = 0x30;
|
||||
const LC_BUILD_VERSION = 0x32;
|
||||
|
||||
/** `mach-o/loader.h` `dylib_use_command`: `nameoff == 28` and this marker. */
|
||||
const DYLIB_USE_MARKER = 0x1a741800;
|
||||
@@ -109,22 +119,55 @@ export const readMachoImage = async (
|
||||
};
|
||||
if (count > MAX_FAT_ARCHITECTURES || 8 + count * (wide ? 32 : 20) > size)
|
||||
return { status: "not-mach-o" };
|
||||
const table = viewOf(
|
||||
await readExact(readAt, 8, count * (wide ? 32 : 20), "FAT table"),
|
||||
const tableBytes = await readExact(
|
||||
readAt,
|
||||
8,
|
||||
count * (wide ? 32 : 20),
|
||||
"FAT table",
|
||||
);
|
||||
const declarations = readFatSliceDeclarations(tableBytes, {
|
||||
count,
|
||||
wide,
|
||||
littleEndian,
|
||||
});
|
||||
if (declarations.status !== "parsed")
|
||||
throw new MachoFormatIssue("malformed", declarations.reason);
|
||||
// A Java class file's version fields read as a FAT count; its "table" names no CPU.
|
||||
const cpuTypes = Array.from({ length: count }, (_, index) =>
|
||||
table.getUint32(index * (wide ? 32 : 20), littleEndian),
|
||||
);
|
||||
if (!cpuTypes.some((type) => KNOWN_CPU_TYPES.includes(type)))
|
||||
return { status: "not-mach-o" };
|
||||
if (
|
||||
!declarations.slices.some(({ cpuType }) =>
|
||||
KNOWN_CPU_TYPES.includes(cpuType),
|
||||
)
|
||||
) {
|
||||
let hasMachOSliceHeader = false;
|
||||
for (const declaration of declarations.slices) {
|
||||
if (
|
||||
validateFatSlice(
|
||||
declaration,
|
||||
size,
|
||||
8 + count * (wide ? 32 : 20),
|
||||
) !== null
|
||||
)
|
||||
continue;
|
||||
const bytes = await readAt(declaration.offset, 4);
|
||||
if (bytes.byteLength < 4) continue;
|
||||
const sliceMagic = viewOf(bytes).getUint32(0, false);
|
||||
if (
|
||||
[MH_MAGIC, MH_MAGIC_64, MH_CIGAM, MH_CIGAM_64].includes(sliceMagic)
|
||||
) {
|
||||
hasMachOSliceHeader = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!hasMachOSliceHeader) return { status: "not-mach-o" };
|
||||
}
|
||||
return {
|
||||
status: "parsed",
|
||||
slices: await readFatSlices(readAt, size, {
|
||||
wide,
|
||||
littleEndian,
|
||||
count,
|
||||
}),
|
||||
slices: await readFatSlices(
|
||||
readAt,
|
||||
size,
|
||||
8 + count * (wide ? 32 : 20),
|
||||
declarations.slices,
|
||||
),
|
||||
};
|
||||
}
|
||||
if (magic === MH_CIGAM || magic === MH_CIGAM_64)
|
||||
@@ -145,32 +188,30 @@ export const readMachoImage = async (
|
||||
const readFatSlices = async (
|
||||
readAt: ReadAt,
|
||||
size: number,
|
||||
table: {
|
||||
readonly wide: boolean;
|
||||
readonly littleEndian: boolean;
|
||||
readonly count: number;
|
||||
},
|
||||
tableEnd: number,
|
||||
declarations: readonly FatSliceDeclaration[],
|
||||
): Promise<MachoSlice[]> => {
|
||||
const { wide, littleEndian, count } = table;
|
||||
const entrySize = wide ? 32 : 20;
|
||||
const view = viewOf(
|
||||
await readExact(readAt, 8, count * entrySize, "FAT table"),
|
||||
);
|
||||
const slices: MachoSlice[] = [];
|
||||
for (let index = 0; index < count; index++) {
|
||||
const base = index * entrySize;
|
||||
const offset = wide
|
||||
? safeNumber(view.getBigUint64(base + 8, littleEndian))
|
||||
: view.getUint32(base + 8, littleEndian);
|
||||
const length = wide
|
||||
? safeNumber(view.getBigUint64(base + 16, littleEndian))
|
||||
: view.getUint32(base + 12, littleEndian);
|
||||
if (offset + length > size)
|
||||
for (const declaration of declarations) {
|
||||
const { index, offset, size: length, cpuType, cpuSubtype } = declaration;
|
||||
const rangeIssue = validateFatSlice(declaration, size, tableEnd);
|
||||
if (rangeIssue !== null)
|
||||
throw new MachoFormatIssue(
|
||||
"malformed",
|
||||
`FAT architecture ${index} extends beyond the file`,
|
||||
rangeIssue.includes("unsupported") ? "unsupported" : "malformed",
|
||||
rangeIssue,
|
||||
);
|
||||
slices.push(await readSlice(readAt, offset, length));
|
||||
const slice = await readSlice(readAt, offset, length, {
|
||||
fatCpuType: cpuType,
|
||||
fatCpuSubtype: cpuSubtype,
|
||||
alignmentExponent: declaration.alignmentExponent,
|
||||
});
|
||||
const identityIssue = validateFatSlice(declaration, size, tableEnd, {
|
||||
cpuType: slice.cpu_type,
|
||||
cpuSubtype: slice.cpu_subtype,
|
||||
});
|
||||
if (identityIssue !== null)
|
||||
throw new MachoFormatIssue("malformed", identityIssue);
|
||||
slices.push(slice);
|
||||
}
|
||||
return slices;
|
||||
};
|
||||
@@ -179,6 +220,11 @@ const readSlice = async (
|
||||
readAt: ReadAt,
|
||||
base: number,
|
||||
length: number,
|
||||
fat: {
|
||||
readonly fatCpuType: number;
|
||||
readonly fatCpuSubtype: number;
|
||||
readonly alignmentExponent: number;
|
||||
} | null = null,
|
||||
): Promise<MachoSlice> => {
|
||||
const header = viewOf(await readExact(readAt, base, 28, "Mach-O header"));
|
||||
const magic = header.getUint32(0, true);
|
||||
@@ -203,13 +249,22 @@ const readSlice = async (
|
||||
commandBytes,
|
||||
"load commands",
|
||||
);
|
||||
return decodeCommands(commands, {
|
||||
const slice = decodeCommands(commands, {
|
||||
wide,
|
||||
cpuType: header.getUint32(4, true),
|
||||
cpuSubtype: header.getUint32(8, true),
|
||||
fileType: header.getUint32(12, true),
|
||||
commandCount,
|
||||
sliceOffset: base,
|
||||
sliceSize: length,
|
||||
fatCpuType: fat?.fatCpuType ?? null,
|
||||
fatCpuSubtype: fat?.fatCpuSubtype ?? null,
|
||||
fatAlignmentExponent: fat?.alignmentExponent ?? null,
|
||||
});
|
||||
const distinctPlatforms = [...new Set(slice.platforms)];
|
||||
slice.platform =
|
||||
distinctPlatforms.length === 1 ? (distinctPlatforms[0] ?? null) : null;
|
||||
return slice;
|
||||
}
|
||||
throw new MachoFormatIssue(
|
||||
magic === swap(MH_MAGIC) || magic === swap(MH_MAGIC_64)
|
||||
@@ -229,6 +284,11 @@ const decodeCommands = (
|
||||
readonly cpuSubtype: number;
|
||||
readonly fileType: number;
|
||||
readonly commandCount: number;
|
||||
readonly sliceOffset: number;
|
||||
readonly sliceSize: number;
|
||||
readonly fatCpuType: number | null;
|
||||
readonly fatCpuSubtype: number | null;
|
||||
readonly fatAlignmentExponent: number | null;
|
||||
},
|
||||
): MachoSlice => {
|
||||
// loader.h: load commands are 8-byte aligned in 64-bit images, 4 in 32-bit.
|
||||
@@ -236,7 +296,17 @@ const decodeCommands = (
|
||||
const view = viewOf(bytes);
|
||||
const slice: MachoSlice = {
|
||||
architecture: architectureName(header.cpuType, header.cpuSubtype),
|
||||
slice_offset: header.sliceOffset,
|
||||
slice_size: header.sliceSize,
|
||||
cpu_type: header.cpuType,
|
||||
cpu_subtype: header.cpuSubtype,
|
||||
fat_cpu_type: header.fatCpuType,
|
||||
fat_cpu_subtype: header.fatCpuSubtype,
|
||||
fat_alignment_exponent: header.fatAlignmentExponent,
|
||||
file_type: fileTypeName(header.fileType),
|
||||
file_type_code: header.fileType,
|
||||
platform: null,
|
||||
platforms: [],
|
||||
install_name: null,
|
||||
dependencies: [],
|
||||
rpaths: [],
|
||||
@@ -292,6 +362,31 @@ const decodeCommand = (
|
||||
slice.code_signature_present = true;
|
||||
return;
|
||||
}
|
||||
if (command === LC_BUILD_VERSION) {
|
||||
if (body.byteLength < 24)
|
||||
throw new MachoFormatIssue(
|
||||
"malformed",
|
||||
`load command ${index} is too short for LC_BUILD_VERSION`,
|
||||
);
|
||||
const platform = viewOf(body).getUint32(8, true);
|
||||
slice.platforms.push(platform);
|
||||
return;
|
||||
}
|
||||
const legacyPlatform = new Map<number, number>([
|
||||
[LC_VERSION_MIN_MACOSX, 1],
|
||||
[LC_VERSION_MIN_IPHONEOS, 2],
|
||||
[LC_VERSION_MIN_TVOS, 3],
|
||||
[LC_VERSION_MIN_WATCHOS, 4],
|
||||
]).get(command);
|
||||
if (legacyPlatform !== undefined) {
|
||||
if (body.byteLength < 16)
|
||||
throw new MachoFormatIssue(
|
||||
"malformed",
|
||||
`load command ${index} is too short for a version-min command`,
|
||||
);
|
||||
slice.platforms.push(legacyPlatform);
|
||||
return;
|
||||
}
|
||||
const name = DEPENDENCY_COMMANDS.get(command);
|
||||
if (name !== undefined)
|
||||
slice.dependencies.push(decodeDependency(name, body, index));
|
||||
@@ -416,11 +511,5 @@ const swap = (value: number): number =>
|
||||
(value >>> 24)) >>>
|
||||
0;
|
||||
|
||||
const safeNumber = (value: bigint): number => {
|
||||
if (value > BigInt(Number.MAX_SAFE_INTEGER))
|
||||
throw new MachoFormatIssue("malformed", "FAT offset exceeds safe range");
|
||||
return Number(value);
|
||||
};
|
||||
|
||||
const viewOf = (bytes: Uint8Array): DataView =>
|
||||
new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
|
||||
Reference in New Issue
Block a user