fix(apple): validate physical slices and dylib platform compatibility
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { MachOSliceArtifactReader } from "./MachOSliceArtifactReader.js";
|
||||
|
||||
describe("Mach-O slice reader with system lipo", () => {
|
||||
it.skipIf(process.platform !== "darwin")(
|
||||
"checks real /bin/ls lipo CPU symbols and ptrauth capabilities against the FAT table",
|
||||
async () => {
|
||||
const reader = new MachOSliceArtifactReader("/bin/ls");
|
||||
const entries = [];
|
||||
for await (const entry of reader.entries()) entries.push(entry);
|
||||
expect(entries.map(({ path }) => path)).toEqual([
|
||||
"slices/x86_64",
|
||||
"slices/arm64e",
|
||||
]);
|
||||
expect(
|
||||
entries.map(({ byteOffset, declaredSize }) => [
|
||||
byteOffset,
|
||||
declaredSize,
|
||||
]),
|
||||
).toEqual([
|
||||
[16384, 48112],
|
||||
[65536, 89088],
|
||||
]);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { createReadStream } from "node:fs";
|
||||
import { stat } from "node:fs/promises";
|
||||
import { open as openFile, stat } from "node:fs/promises";
|
||||
import { Readable } from "node:stream";
|
||||
|
||||
import type { ArtifactCommand } from "../domain/artifactGraph.js";
|
||||
@@ -7,7 +7,12 @@ import {
|
||||
XcrunCommandRunner,
|
||||
type NativeCommandRunner,
|
||||
} from "../native/CommandRunner.js";
|
||||
import { parseLipoArchitectures } from "../native/parsers/lipo.js";
|
||||
import {
|
||||
parseLipoArchitectures,
|
||||
type LipoArchitecture,
|
||||
} from "../native/parsers/lipo.js";
|
||||
import type { MachoSlice } from "../domain/apple/dylibResolution.js";
|
||||
import { readMachoImage, type ReadAt } from "./apple/MachoLoadCommandReader.js";
|
||||
import {
|
||||
ArtifactReaderFailure,
|
||||
type ArtifactEntry,
|
||||
@@ -46,19 +51,56 @@ export class MachOSliceArtifactReader implements ArtifactReader {
|
||||
effects: ["read"],
|
||||
};
|
||||
const fileSize = (await stat(this.path)).size;
|
||||
for (const architecture of parseLipoArchitectures(captured.value.stdout)) {
|
||||
if (architecture.file_offset === null || architecture.size === null)
|
||||
const architectures = parseLipoArchitectures(captured.value.stdout);
|
||||
const structural = await readStructuralSlices(this.path, fileSize);
|
||||
if (structural.status === "malformed")
|
||||
throw new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
`Mach-O structural slice index is malformed: ${structural.reason}`,
|
||||
);
|
||||
if (structural.status === "unsupported")
|
||||
throw new ArtifactReaderFailure(
|
||||
"format",
|
||||
`Mach-O structural slice index is unsupported: ${structural.reason}`,
|
||||
);
|
||||
const slices = structural.status === "parsed" ? structural.slices : null;
|
||||
const matchingSlices = new Map<LipoArchitecture, MachoSlice>();
|
||||
if (slices !== null) {
|
||||
if (slices.length !== architectures.length)
|
||||
throw new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
"lipo architecture count disagrees with the Mach-O FAT table",
|
||||
);
|
||||
const unmatched = [...slices];
|
||||
for (const reported of architectures) {
|
||||
const match = unmatched.findIndex((observed) =>
|
||||
lipoMatchesSlice(reported, observed, slices.length === 1),
|
||||
);
|
||||
if (match < 0)
|
||||
throw new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
`lipo slice metadata disagrees with the Mach-O FAT table: ${reported.name} (cputype=${reported.cpu_type ?? "unknown"}, cpusubtype=${reported.cpu_subtype ?? "unknown"}, offset=${reported.file_offset ?? "unknown"}, size=${reported.size ?? "unknown"}, alignment=${reported.alignment ?? "unknown"})`,
|
||||
);
|
||||
const [observed] = unmatched.splice(match, 1);
|
||||
if (observed !== undefined) matchingSlices.set(reported, observed);
|
||||
}
|
||||
}
|
||||
for (const architecture of architectures) {
|
||||
const structuralSlice = matchingSlices.get(architecture);
|
||||
const offset = architecture.file_offset ?? structuralSlice?.slice_offset;
|
||||
const sliceSize = architecture.size ?? structuralSlice?.slice_size;
|
||||
if (offset === undefined || sliceSize === undefined)
|
||||
throw new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
"lipo omitted a universal slice byte range",
|
||||
);
|
||||
if (
|
||||
!Number.isSafeInteger(architecture.file_offset) ||
|
||||
!Number.isSafeInteger(architecture.size) ||
|
||||
architecture.file_offset < 0 ||
|
||||
architecture.size <= 0 ||
|
||||
!Number.isSafeInteger(architecture.file_offset + architecture.size) ||
|
||||
architecture.file_offset + architecture.size > fileSize
|
||||
!Number.isSafeInteger(offset) ||
|
||||
!Number.isSafeInteger(sliceSize) ||
|
||||
offset < 0 ||
|
||||
sliceSize <= 0 ||
|
||||
!Number.isSafeInteger(offset + sliceSize) ||
|
||||
offset + sliceSize > fileSize
|
||||
)
|
||||
throw new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
@@ -67,15 +109,38 @@ export class MachOSliceArtifactReader implements ArtifactReader {
|
||||
yield {
|
||||
path: `slices/${architecture.name}`,
|
||||
kind: "slice",
|
||||
declaredSize: architecture.size,
|
||||
declaredSize: sliceSize,
|
||||
compressedSize: null,
|
||||
executable: true,
|
||||
encrypted: false,
|
||||
byteOffset: architecture.file_offset,
|
||||
byteOffset: offset,
|
||||
declaredSha256: null,
|
||||
unpacked: false,
|
||||
limitations: [],
|
||||
adapterKey: `${String(architecture.file_offset)}:${String(architecture.size)}`,
|
||||
limitations: [
|
||||
...(structural.status === "not-mach-o"
|
||||
? [
|
||||
"Structural Mach-O facts were unavailable; slice range is based on lipo output.",
|
||||
]
|
||||
: []),
|
||||
...(architecture.file_offset === null || architecture.size === null
|
||||
? [
|
||||
"Slice range was derived from structural Mach-O facts because lipo omitted it.",
|
||||
]
|
||||
: []),
|
||||
...(architecture.cpu_type !== null &&
|
||||
architecture.cpu_type_code === null
|
||||
? [
|
||||
`lipo CPU type is retained as unnormalized source text: ${architecture.cpu_type}`,
|
||||
]
|
||||
: []),
|
||||
...(architecture.cpu_subtype !== null &&
|
||||
architecture.cpu_subtype_code === null
|
||||
? [
|
||||
`lipo CPU subtype is retained as unnormalized source text: ${architecture.cpu_subtype}${architecture.capabilities === null ? "" : `; capabilities ${architecture.capabilities}`}`,
|
||||
]
|
||||
: []),
|
||||
],
|
||||
adapterKey: `${String(offset)}:${String(sliceSize)}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -157,6 +222,53 @@ export class MachOSliceArtifactReader implements ArtifactReader {
|
||||
}
|
||||
}
|
||||
|
||||
/** Compare lipo's independent ranges with structurally parsed slice identity. */
|
||||
const readStructuralSlices = async (
|
||||
path: string,
|
||||
size: number,
|
||||
): Promise<Awaited<ReturnType<typeof readMachoImage>>> => {
|
||||
const file = await openFile(path, "r");
|
||||
try {
|
||||
const readAt: ReadAt = async (offset, length) => {
|
||||
const bytes = Buffer.alloc(length);
|
||||
const { bytesRead } = await file.read(bytes, 0, length, offset);
|
||||
return bytes.subarray(0, bytesRead);
|
||||
};
|
||||
const facts = await readMachoImage(readAt, size);
|
||||
return facts;
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
};
|
||||
|
||||
const lipoMatchesSlice = (
|
||||
reported: LipoArchitecture,
|
||||
observed: MachoSlice,
|
||||
structurallyUnique: boolean,
|
||||
): boolean => {
|
||||
const observedCpuType = observed.fat_cpu_type ?? observed.cpu_type;
|
||||
const observedCpuSubtype = observed.fat_cpu_subtype ?? observed.cpu_subtype;
|
||||
const hasPhysicalIdentity =
|
||||
reported.file_offset !== null ||
|
||||
reported.size !== null ||
|
||||
reported.cpu_type_code !== null ||
|
||||
reported.cpu_subtype_code !== null;
|
||||
return (
|
||||
(hasPhysicalIdentity || structurallyUnique) &&
|
||||
reported.name === observed.architecture &&
|
||||
(reported.file_offset === null ||
|
||||
reported.file_offset === observed.slice_offset) &&
|
||||
(reported.size === null || reported.size === observed.slice_size) &&
|
||||
(reported.cpu_type_code === null ||
|
||||
reported.cpu_type_code === observedCpuType) &&
|
||||
(reported.cpu_subtype_code === null ||
|
||||
reported.cpu_subtype_code === observedCpuSubtype) &&
|
||||
(reported.alignment === null ||
|
||||
observed.fat_alignment_exponent === null ||
|
||||
reported.alignment === 2 ** observed.fat_alignment_exponent)
|
||||
);
|
||||
};
|
||||
|
||||
const parseSliceKeyInteger = (value: string | undefined): number | null => {
|
||||
if (value === undefined || !/^\d+$/u.test(value)) return null;
|
||||
const parsed = Number(value);
|
||||
|
||||
@@ -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