fix(apple): validate physical slices and dylib platform compatibility
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
CPU,
|
||||
fatImage,
|
||||
machoImage,
|
||||
} from "../artifacts/apple/MachoImage.fixture.js";
|
||||
import { parseMachoLayout } from "./AppleMachoSelection.js";
|
||||
|
||||
describe("native Mach-O slice selection", () => {
|
||||
it("accepts a FAT slice whose table identity and embedded header agree", () => {
|
||||
const fat = fatImage([{ cpu: CPU.arm64, bytes: machoImage({}) }]);
|
||||
expect(parseMachoLayout(Buffer.from(fat), "arm64").segments).toEqual([]);
|
||||
});
|
||||
|
||||
it("rejects FAT subtype disagreement and unaligned slice offsets", () => {
|
||||
const original = fatImage([{ cpu: CPU.arm64, bytes: machoImage({}) }]);
|
||||
const mismatched = original.slice();
|
||||
new DataView(mismatched.buffer).setUint32(12, 2, false);
|
||||
expect(() => parseMachoLayout(Buffer.from(mismatched), "arm64")).toThrow(
|
||||
/disagrees/u,
|
||||
);
|
||||
|
||||
const unaligned = original.slice();
|
||||
new DataView(unaligned.buffer).setUint32(16, 4097, false);
|
||||
expect(() => parseMachoLayout(Buffer.from(unaligned), "arm64")).toThrow(
|
||||
/not aligned/u,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,8 @@
|
||||
import type { FixupCommands } from "./AppleMachoFixups.js";
|
||||
import {
|
||||
readFatSliceDeclarations,
|
||||
validateFatSlice,
|
||||
} from "../domain/apple/machoContainer.js";
|
||||
|
||||
/** One mapped Mach-O segment with file-backed VA range. */
|
||||
export interface Segment {
|
||||
@@ -19,6 +23,16 @@ export interface Section {
|
||||
export interface MachoSlice {
|
||||
slice: number;
|
||||
sliceEnd: number;
|
||||
/** FAT table identity, absent for a thin image. */
|
||||
fatDeclaration?: {
|
||||
offset: number;
|
||||
size: number;
|
||||
cpuType: number;
|
||||
cpuSubtype: number;
|
||||
alignmentExponent: number;
|
||||
index: number;
|
||||
tableEnd: number;
|
||||
};
|
||||
}
|
||||
|
||||
/** Parsed little-endian 64-bit Mach-O layout for the selected architecture slice. */
|
||||
@@ -66,9 +80,6 @@ export const selectMachoSlice = (
|
||||
const readUInt32 = littleEndian
|
||||
? (offset: number) => bytes.readUInt32LE(offset)
|
||||
: (offset: number) => bytes.readUInt32BE(offset);
|
||||
const readUInt64 = littleEndian
|
||||
? (offset: number) => bytes.readBigUInt64LE(offset)
|
||||
: (offset: number) => bytes.readBigUInt64BE(offset);
|
||||
const byteOrder = littleEndian ? "little-endian" : "big-endian";
|
||||
const malformed = (reason: string) =>
|
||||
new RangeError(`${reason} (FAT header byte order: ${byteOrder})`);
|
||||
@@ -80,21 +91,27 @@ export const selectMachoSlice = (
|
||||
if (count > 128 || headerEnd > bytes.length)
|
||||
throw malformed("Malformed FAT architecture table");
|
||||
const cpu = architecture === "arm64" ? 0x0100000c : 0x01000007;
|
||||
const declarations = readFatSliceDeclarations(bytes.subarray(8, headerEnd), {
|
||||
count,
|
||||
wide: fat64,
|
||||
littleEndian,
|
||||
});
|
||||
if (declarations.status !== "parsed") throw malformed(declarations.reason);
|
||||
let selected: MachoSlice | undefined;
|
||||
for (let index = 0; index < count; index++) {
|
||||
const offset = 8 + index * stride;
|
||||
if (readUInt32(offset) !== cpu) continue;
|
||||
for (const declaration of declarations.slices) {
|
||||
const issue = validateFatSlice(declaration, bytes.length, headerEnd);
|
||||
if (issue !== null) throw malformed(issue);
|
||||
if (declaration.cpuType !== cpu) continue;
|
||||
if (selected !== undefined)
|
||||
throw invalid("Ambiguous FAT architecture slice");
|
||||
const start = fat64
|
||||
? readUInt64(offset + 8)
|
||||
: BigInt(readUInt32(offset + 8));
|
||||
const size = fat64
|
||||
? readUInt64(offset + 16)
|
||||
: BigInt(readUInt32(offset + 12));
|
||||
if (start < BigInt(headerEnd) || start + size > BigInt(bytes.length))
|
||||
throw malformed("FAT slice exceeds file");
|
||||
selected = { slice: Number(start), sliceEnd: Number(start + size) };
|
||||
selected = {
|
||||
slice: declaration.offset,
|
||||
sliceEnd: declaration.offset + declaration.size,
|
||||
fatDeclaration: {
|
||||
...declaration,
|
||||
tableEnd: headerEnd,
|
||||
},
|
||||
};
|
||||
}
|
||||
if (selected === undefined)
|
||||
throw invalid("Requested FAT architecture is absent");
|
||||
@@ -107,13 +124,22 @@ export const parseMachoLayout = (
|
||||
architecture: string,
|
||||
): MachoLayout => {
|
||||
if (bytes.length < 4) throw new RangeError("Truncated Mach-O header");
|
||||
const { slice, sliceEnd } = selectMachoSlice(bytes, architecture);
|
||||
const selection = selectMachoSlice(bytes, architecture);
|
||||
const { slice, sliceEnd } = selection;
|
||||
if (slice === 0 && sliceEnd === bytes.length && bytes.length < 32)
|
||||
throw new RangeError("Truncated Mach-O header");
|
||||
if (slice + 32 > sliceEnd || bytes.readUInt32LE(slice) !== 0xfeedfacf)
|
||||
throw new TypeError(
|
||||
"Only little-endian 64-bit Mach-O metadata is supported",
|
||||
);
|
||||
const fat = selection.fatDeclaration;
|
||||
if (fat !== undefined) {
|
||||
const issue = validateFatSlice(fat, bytes.length, fat.tableEnd, {
|
||||
cpuType: bytes.readUInt32LE(slice + 4),
|
||||
cpuSubtype: bytes.readUInt32LE(slice + 8),
|
||||
});
|
||||
if (issue !== null) throw new TypeError(issue);
|
||||
}
|
||||
const commands = bytes.readUInt32LE(slice + 16);
|
||||
const commandEnd = slice + 32 + bytes.readUInt32LE(slice + 20);
|
||||
if (commands > 4096 || commandEnd > sliceEnd)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseLipoArchitectures } from "./lipo.js";
|
||||
|
||||
describe("lipo architecture facts", () => {
|
||||
it("normalizes symbolic CPU fields and arm64e capability bits", () => {
|
||||
const architectures = parseLipoArchitectures(`Fat header in: /bin/ls
|
||||
fat_magic 0xcafebabe
|
||||
nfat_arch 2
|
||||
architecture x86_64
|
||||
cputype CPU_TYPE_X86_64
|
||||
cpusubtype CPU_SUBTYPE_X86_64_ALL
|
||||
capabilities 0x0
|
||||
offset 16384
|
||||
size 48112
|
||||
align 2^14 (16384)
|
||||
architecture arm64e
|
||||
cputype CPU_TYPE_ARM64
|
||||
cpusubtype CPU_SUBTYPE_ARM64E
|
||||
capabilities PTR_AUTH_VERSION USERSPACE 0
|
||||
offset 65536
|
||||
size 89088
|
||||
align 2^14 (16384)
|
||||
`);
|
||||
expect(architectures).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "x86_64",
|
||||
cpu_type: "CPU_TYPE_X86_64",
|
||||
cpu_type_code: 0x01000007,
|
||||
cpu_subtype: "CPU_SUBTYPE_X86_64_ALL",
|
||||
cpu_subtype_code: 3,
|
||||
capabilities: "0x0",
|
||||
file_offset: 16384,
|
||||
size: 48112,
|
||||
alignment: 16384,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "arm64e",
|
||||
cpu_type: "CPU_TYPE_ARM64",
|
||||
cpu_type_code: 0x0100000c,
|
||||
cpu_subtype: "CPU_SUBTYPE_ARM64E",
|
||||
cpu_subtype_code: 0x80000002,
|
||||
capabilities: "PTR_AUTH_VERSION USERSPACE 0",
|
||||
file_offset: 65536,
|
||||
size: 89088,
|
||||
alignment: 16384,
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps unknown symbolic CPU values as source text without a code", () => {
|
||||
expect(
|
||||
parseLipoArchitectures(
|
||||
"architecture future\n cputype CPU_TYPE_FUTURE\n cpusubtype CPU_SUBTYPE_FUTURE\n offset 4096\n size 64\n align 2^12\n",
|
||||
),
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_type: "CPU_TYPE_FUTURE",
|
||||
cpu_type_code: null,
|
||||
cpu_subtype: "CPU_SUBTYPE_FUTURE",
|
||||
cpu_subtype_code: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -4,6 +4,9 @@ const architectureSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
cpu_type: z.string().nullable(),
|
||||
cpu_subtype: z.string().nullable(),
|
||||
cpu_type_code: z.number().int().nonnegative().nullable(),
|
||||
cpu_subtype_code: z.number().int().nonnegative().nullable(),
|
||||
capabilities: z.string().nullable(),
|
||||
file_offset: z.number().int().min(0).nullable(),
|
||||
size: z.number().int().min(0).nullable(),
|
||||
alignment: z.number().int().min(0).nullable(),
|
||||
@@ -25,6 +28,9 @@ export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
name: thinArchitecture,
|
||||
cpu_type: null,
|
||||
cpu_subtype: null,
|
||||
cpu_type_code: null,
|
||||
cpu_subtype_code: null,
|
||||
capabilities: null,
|
||||
file_offset: null,
|
||||
size: null,
|
||||
alignment: null,
|
||||
@@ -43,6 +49,12 @@ export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
: name,
|
||||
cpu_type: current.cputype ?? null,
|
||||
cpu_subtype: current.cpusubtype ?? null,
|
||||
cpu_type_code: cpuTypeCode(current.cputype),
|
||||
cpu_subtype_code: cpuSubtypeCode(
|
||||
current.cpusubtype,
|
||||
current.capabilities,
|
||||
),
|
||||
capabilities: current.capabilities ?? null,
|
||||
file_offset: integer(current.offset),
|
||||
size: integer(current.size),
|
||||
alignment: alignment(current.align),
|
||||
@@ -62,7 +74,10 @@ export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
current = { architecture: line.slice("architecture ".length).trim() };
|
||||
continue;
|
||||
}
|
||||
const match = /^(cputype|cpusubtype|offset|size|align)\s+(.+)$/u.exec(line);
|
||||
const match =
|
||||
/^(cputype|cpusubtype|capabilities|offset|size|align)\s+(.+)$/u.exec(
|
||||
line,
|
||||
);
|
||||
if (match?.[1] !== undefined && match[2] !== undefined) {
|
||||
current ??= {};
|
||||
current[match[1]] = match[2].trim();
|
||||
@@ -80,6 +95,72 @@ const integer = (value: string | undefined): number | null => {
|
||||
return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : null;
|
||||
};
|
||||
|
||||
const CPU_TYPES: Readonly<Record<string, number>> = {
|
||||
CPU_TYPE_X86: 7,
|
||||
CPU_TYPE_X86_64: 0x01000007,
|
||||
CPU_TYPE_ARM: 12,
|
||||
CPU_TYPE_ARM64: 0x0100000c,
|
||||
CPU_TYPE_ARM64_32: 0x0200000c,
|
||||
CPU_TYPE_POWERPC: 18,
|
||||
CPU_TYPE_POWERPC64: 0x01000012,
|
||||
};
|
||||
|
||||
const CPU_SUBTYPES: Readonly<Record<string, number>> = {
|
||||
CPU_SUBTYPE_X86_ALL: 3,
|
||||
CPU_SUBTYPE_X86_64_ALL: 3,
|
||||
CPU_SUBTYPE_X86_64_H: 8,
|
||||
CPU_SUBTYPE_ARM64_ALL: 0,
|
||||
CPU_SUBTYPE_ARM64_V8: 1,
|
||||
CPU_SUBTYPE_ARM64E: 2,
|
||||
CPU_SUBTYPE_ARM64_32_ALL: 0,
|
||||
CPU_SUBTYPE_ARM64_32_V8: 1,
|
||||
CPU_SUBTYPE_ARM_V7: 9,
|
||||
CPU_SUBTYPE_ARM_V7S: 11,
|
||||
CPU_SUBTYPE_ARM_V7K: 12,
|
||||
CPU_SUBTYPE_POWERPC_ALL: 0,
|
||||
CPU_SUBTYPE_POWERPC_970: 100,
|
||||
};
|
||||
|
||||
/** Normalize known lipo names while retaining the producer's original text. */
|
||||
const cpuTypeCode = (value: string | undefined): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const numeric = unsigned32(value);
|
||||
return numeric ?? CPU_TYPES[value] ?? null;
|
||||
};
|
||||
|
||||
/** Normalize the subtype and its separately printed ptrauth capability bits. */
|
||||
const cpuSubtypeCode = (
|
||||
value: string | undefined,
|
||||
capabilities: string | undefined,
|
||||
): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const numeric = unsigned32(value);
|
||||
if (numeric !== null) return numeric;
|
||||
const subtype = CPU_SUBTYPES[value];
|
||||
if (subtype === undefined) return null;
|
||||
if (value !== "CPU_SUBTYPE_ARM64E") return subtype;
|
||||
const version = /PTR_AUTH_VERSION\s+USERSPACE\s+(\d+)/u.exec(
|
||||
capabilities ?? "",
|
||||
)?.[1];
|
||||
if (version === undefined) return null;
|
||||
const parsedVersion = Number(version);
|
||||
if (
|
||||
!Number.isInteger(parsedVersion) ||
|
||||
parsedVersion < 0 ||
|
||||
parsedVersion > 15
|
||||
)
|
||||
return null;
|
||||
return (0x80000000 | (parsedVersion << 24) | subtype) >>> 0;
|
||||
};
|
||||
|
||||
const unsigned32 = (value: string): number | null => {
|
||||
if (!/^(?:\d+|0x[\da-f]+)$/iu.test(value)) return null;
|
||||
const parsed = Number(value);
|
||||
return Number.isInteger(parsed) && parsed >= 0 && parsed <= 0xffffffff
|
||||
? parsed >>> 0
|
||||
: null;
|
||||
};
|
||||
|
||||
const alignment = (value: string | undefined): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const exponent = /^2\^(\d+)(?: \((\d+)\))?$/u.exec(value);
|
||||
|
||||
Reference in New Issue
Block a user