mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +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
@@ -31,7 +31,6 @@
|
||||
"src/ghidra/GhidraLoadImageValues.ts": 1,
|
||||
"src/ghidra/extensions/GhidraExtensions.ts": 1,
|
||||
"src/go/GoStringLiterals.ts": 4,
|
||||
"src/hopper/HopperAnalysisProfile.ts": 1,
|
||||
"src/il2cpp/Il2cppBinaryImage.ts": 2,
|
||||
"src/il2cpp/Il2cppBinaryRegistration.ts": 8,
|
||||
"src/il2cpp/Il2cppMetadataFile.ts": 21,
|
||||
|
||||
@@ -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);
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@@ -6,6 +6,12 @@ import { afterEach, describe, expect, it } from "vitest";
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
import { createInspectionResources } from "../../../fixtures/inspectionResources.js";
|
||||
|
||||
import { parseBinaryTarget } from "../../../../src/application/BinaryTargetResolver.js";
|
||||
import {
|
||||
elf,
|
||||
fat,
|
||||
thinMach,
|
||||
} from "../../../../src/domain/binaryTarget.fixture.js";
|
||||
import type { BinaryTarget } from "../../../../src/domain/binaryTargetTypes.js";
|
||||
import {
|
||||
hopperLoaderArgsForTarget,
|
||||
@@ -177,6 +183,61 @@ describe("Hopper analysis profiles", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ admitted: "thin", replacement: "elf" },
|
||||
{ admitted: "thin", replacement: "fat32" },
|
||||
{ admitted: "fat32", replacement: "thin" },
|
||||
] as const)(
|
||||
"refuses a replacement of kind $replacement for an admitted $admitted Mach-O with loader overrides",
|
||||
async ({ admitted, replacement }) => {
|
||||
const root = await createTestTempDirectory("rea-hopper-profile-magic-");
|
||||
const path = join(root, "selected.macho");
|
||||
const launcherPath = join(root, "hopper");
|
||||
const arm64 = 0x0100000c;
|
||||
const thin = thinMach(0xfeedfacf, arm64);
|
||||
thin.writeUInt32BE(2, 12); // MH_EXECUTE
|
||||
const inputs = { thin, fat32: fat([arm64]), elf: elf(2, 1, 183) };
|
||||
await writeFile(path, inputs[admitted]);
|
||||
await writeFile(launcherPath, "Hopper fixture");
|
||||
const selected = await parseBinaryTarget(path, {
|
||||
hostArchitecture: "arm64",
|
||||
});
|
||||
if (!selected.ok) throw selected.error;
|
||||
const options = {
|
||||
launcherPath,
|
||||
loaderArgsOverride: ["-l", "Custom Loader"],
|
||||
provider: HOPPER_PROVIDER_IDENTITY,
|
||||
resourceScope: inspectionResources.owner,
|
||||
};
|
||||
const control = await resolveHopperAnalysisProfile(selected.value, options);
|
||||
expect(control).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
profile: {
|
||||
parameters: {
|
||||
macho_container: admitted,
|
||||
loader: {
|
||||
source: "configured_override",
|
||||
arguments: options.loaderArgsOverride,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await writeFile(path, inputs[replacement]);
|
||||
await expect(
|
||||
resolveHopperAnalysisProfile(selected.value, options),
|
||||
).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
_tag: "BinaryTargetError",
|
||||
message: expect.stringContaining("admitted Mach-O format"),
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
const target = (
|
||||
format: "mach-o" | "elf" | "pe" | "dos-mz",
|
||||
architecture: "x86" | "x86_64" | "arm" | "arm64",
|
||||
|
||||
Reference in new issue
Block a user