mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 06:04:03 +00:00
fix(hopper): verify Mach-O profile signature before loader selection (#12485)
fix(hopper): verify the admitted Mach-O profile signature
This commit is contained in:
1 parent
673bd049b7
commit
1b0a118192
4 files changed
+100
-23
No files matched your search
@@ -156,7 +156,7 @@ export type FatHeaderPrefix =
|
||||
));
|
||||
|
||||
/** Interpret a raw FAT magic without asserting container identity or header readiness. */
|
||||
export const readFatMagicFormat = (magic: number): FatHeaderFormat | null => {
|
||||
const readFatMagicFormat = (magic: number): FatHeaderFormat | null => {
|
||||
if (![FAT_MAGIC, FAT_MAGIC_64, FAT_CIGAM, FAT_CIGAM_64].includes(magic))
|
||||
return null;
|
||||
return {
|
||||
@@ -166,6 +166,15 @@ export const readFatMagicFormat = (magic: number): FatHeaderFormat | null => {
|
||||
};
|
||||
};
|
||||
|
||||
/** Classify a Mach-O signature without admitting a thin header or FAT table. */
|
||||
export const machoContainerKindFromMagic = (
|
||||
magic: number,
|
||||
): "thin" | "fat32" | "fat64" | null => {
|
||||
if (thinMachoFormat(magic) !== null) return "thin";
|
||||
const fat = readFatMagicFormat(magic);
|
||||
return fat === null ? null : fat.wide ? "fat64" : "fat32";
|
||||
};
|
||||
|
||||
/** Observe the fixed FAT header without table, count-limit or container-identity admission. */
|
||||
export const readFatHeaderPrefix = (bytes: Uint8Array): FatHeaderPrefix => {
|
||||
if (bytes.byteLength < 4) return { state: "unknown", requiredBytes: 4 };
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
} from "../filesystem/OwnedRegularFile.js";
|
||||
|
||||
import { assertRegularFileActive } from "../filesystem/RegularFile.js";
|
||||
import { readFileHandleRange } from "../filesystem/readFileHandleRange.js";
|
||||
|
||||
import type {
|
||||
AnalysisProfileResolution,
|
||||
@@ -43,7 +44,8 @@ import {
|
||||
type MachoTargetSelection,
|
||||
} from "../domain/apple/machoTargetSelection.js";
|
||||
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import { readFatMagicFormat } from "../domain/apple/machoContainer.js";
|
||||
import { machoContainerKindFromMagic } from "../domain/apple/machoContainer.js";
|
||||
import { BinaryReader } from "../domain/binaryContainer.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
import {
|
||||
@@ -103,9 +105,18 @@ const resolveHopperAnalysisProfileInScope = async (
|
||||
options.signal,
|
||||
);
|
||||
if (!header.ok) return header;
|
||||
const fatFormat = readFatMagicFormat(header.value);
|
||||
machoContainer =
|
||||
fatFormat === null ? "thin" : fatFormat.wide ? "fat64" : "fat32";
|
||||
const observed = machoContainerKindFromMagic(header.value);
|
||||
if (
|
||||
observed === null ||
|
||||
(observed === "thin") !== (target.format === "mach-o")
|
||||
)
|
||||
return err(
|
||||
new BinaryTargetError(
|
||||
target.path,
|
||||
"The selected file's container signature disagrees with its admitted Mach-O format. Open the stable source again.",
|
||||
),
|
||||
);
|
||||
machoContainer = observed;
|
||||
}
|
||||
let preparedImage: HopperMachOImage | undefined;
|
||||
if (
|
||||
@@ -308,24 +319,21 @@ const readMachoMagic = (
|
||||
signal,
|
||||
(options) =>
|
||||
withOwnedRegularFile(path, options, async ({ handle }) => {
|
||||
const bytes = Buffer.alloc(4);
|
||||
let length = 0;
|
||||
while (length < bytes.length) {
|
||||
assertRegularFileActive(signal);
|
||||
const { bytesRead } = await handle.read(
|
||||
bytes,
|
||||
length,
|
||||
bytes.length - length,
|
||||
length,
|
||||
const magicBytes = 4;
|
||||
const bytes = await readFileHandleRange(
|
||||
handle,
|
||||
0,
|
||||
magicBytes,
|
||||
(read) => {
|
||||
if (read < magicBytes) assertRegularFileActive(signal);
|
||||
},
|
||||
);
|
||||
if (bytes.length < magicBytes)
|
||||
throw new BinaryTargetError(
|
||||
path,
|
||||
"Mach-O header became truncated before Hopper loader selection",
|
||||
);
|
||||
if (bytesRead === 0)
|
||||
throw new BinaryTargetError(
|
||||
path,
|
||||
"Mach-O header became truncated before Hopper loader selection",
|
||||
);
|
||||
length += bytesRead;
|
||||
}
|
||||
return bytes.readUInt32BE(0);
|
||||
return new BinaryReader(bytes, false, "Mach-O").u32(0);
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
Reference in new issue
Block a user