fix(hopper): bind file mappings to verified source bytes
Read source digests, FAT tables and embedded headers from one opened source stream before translating native offsets. Preserve native mapping observations through changed, missing, nonregular and access-denied source failures; keep filesystem work outside the Hopper API thread. Exercise source drift, relocated slices, missing files, permissions and recovery through real Hopper CLI/MCP verification for thin, FAT32 and FAT64 inputs.
This commit is contained in:
@@ -16,7 +16,7 @@ export const HOPPER_MEMORY_TOOL_DEFINITIONS = [
|
||||
{
|
||||
name: "address_to_file_offset",
|
||||
description:
|
||||
"Map one provider-normalized virtual address to its original nonnegative file offset. Hopper translates FAT Mach-O image-relative offsets using the observed loaded header, preserving its provider coordinate, image base, and source path. Unmapped addresses fail explicitly, and unavailable or ambiguous original-file mappings return typed capability unavailability.",
|
||||
"Map one provider-normalized virtual address to its original nonnegative file offset. Hopper verifies the original bytes against the selected executable digest and translates FAT image-relative offsets using the loaded header. Results preserve the provider coordinate, image base, and source path. Unmapped addresses fail explicitly; changed or unavailable sources return typed errors retaining native mapping facts as partial evidence.",
|
||||
inputSchema: z.object({ address, document }),
|
||||
},
|
||||
] as const;
|
||||
|
||||
@@ -84,9 +84,11 @@ import type {
|
||||
PartialProcessCaptureObservation,
|
||||
ProcessCaptureCleanupReport,
|
||||
} from "./process/processCapture.js";
|
||||
import type { FileOffsetPartialObservation } from "./native/fileOffsetPartialObservation.js";
|
||||
import type { NativeCallPartialObservation } from "./native/nativeCallPartialObservation.js";
|
||||
|
||||
/** Provider-neutral evidence collected before a typed analysis failure. */
|
||||
export type AnalysisPartialObservation =
|
||||
| PartialProcessCaptureObservation
|
||||
| NativeCallPartialObservation;
|
||||
| NativeCallPartialObservation
|
||||
| FileOffsetPartialObservation;
|
||||
|
||||
@@ -136,7 +136,7 @@ export class AnalysisAccessDeniedError extends AnalysisError {
|
||||
readonly operation: string,
|
||||
readonly path: string,
|
||||
readonly systemCode: "EACCES" | "EPERM",
|
||||
options?: ErrorOptions,
|
||||
options?: AnalysisErrorOptions,
|
||||
) {
|
||||
super(
|
||||
`Host filesystem read access denied (${systemCode}) for ${path} during ${operation}`,
|
||||
@@ -152,7 +152,7 @@ export class AnalysisArtifactChangedError extends AnalysisError {
|
||||
readonly operation: string,
|
||||
readonly path: string,
|
||||
readonly reason: string,
|
||||
options?: ErrorOptions,
|
||||
options?: AnalysisErrorOptions,
|
||||
) {
|
||||
super(`Selected artifact changed during acquisition: ${reason}`, options);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { AnalysisError } from "./analysisErrorBase.js";
|
||||
import {
|
||||
AnalysisError,
|
||||
type AnalysisErrorOptions,
|
||||
} from "./analysisErrorBase.js";
|
||||
|
||||
/** Runtime configuration could not be parsed safely. */
|
||||
export class ConfigurationError extends AnalysisError {
|
||||
@@ -16,7 +19,7 @@ export class NoBinaryOpenError extends AnalysisError {
|
||||
}
|
||||
|
||||
/** Additional constraints identified while admitting a filesystem target. */
|
||||
export interface BinaryTargetErrorOptions extends ErrorOptions {
|
||||
export interface BinaryTargetErrorOptions extends AnalysisErrorOptions {
|
||||
readonly constraint?: "directory_requires_file";
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
/** Native mapping facts retained when original-file coordinates cannot be verified. */
|
||||
export interface FileOffsetPartialObservation {
|
||||
readonly operation: "address_to_file_offset";
|
||||
readonly address: string;
|
||||
readonly provider_file_offset: number;
|
||||
readonly provider_source_path: string | null;
|
||||
readonly provider_image_header_hex: string | null;
|
||||
readonly original_file_offset: {
|
||||
readonly available: false;
|
||||
readonly reason: string;
|
||||
};
|
||||
}
|
||||
@@ -363,7 +363,6 @@ const bridgeBootstrapSource = (
|
||||
`REA_TOKEN = ${JSON.stringify(session.token)}`,
|
||||
`REA_RUN_ID = ${JSON.stringify(session.runId)}`,
|
||||
`REA_TARGET_PATH = ${JSON.stringify(loadedPath)}`,
|
||||
`REA_ORIGINAL_EXECUTABLE_PATH = ${options.targetKind === "executable" ? JSON.stringify(options.targetPath) : "None"}`,
|
||||
`REA_OWNS_PROCESS_LIFETIME = ${ownsProcessLifetime ? "True" : "False"}`,
|
||||
`exec(compile(open(${JSON.stringify(options.bridgeScriptPath)}, 'rb').read(), ${JSON.stringify(options.bridgeScriptPath)}, 'exec'))`,
|
||||
"",
|
||||
|
||||
@@ -0,0 +1,322 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { constants } from "node:fs";
|
||||
import { open, type FileHandle } from "node:fs/promises";
|
||||
import { z } from "zod";
|
||||
|
||||
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import {
|
||||
AnalysisAccessDeniedError,
|
||||
AnalysisArtifactChangedError,
|
||||
AnalysisCancelledError,
|
||||
AnalysisCapabilityUnavailableError,
|
||||
AnalysisOutputError,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import { BinaryTargetError } from "../domain/configurationErrors.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import type { FileOffsetPartialObservation } from "../domain/native/fileOffsetPartialObservation.js";
|
||||
import {
|
||||
readFatSliceDeclarations,
|
||||
type FatSliceDeclaration,
|
||||
validateFatSlice,
|
||||
} from "../domain/apple/machoContainer.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
const OPERATION = "address_to_file_offset";
|
||||
const MAX_FAT_ARCHITECTURES = 128;
|
||||
const PREFIX_BYTES = 8 + MAX_FAT_ARCHITECTURES * 32;
|
||||
const observationSchema = z.strictObject({
|
||||
address: z.string().regex(/^0x[0-9a-f]+$/u),
|
||||
provider_file_offset: z
|
||||
.number()
|
||||
.int()
|
||||
.nonnegative()
|
||||
.max(Number.MAX_SAFE_INTEGER),
|
||||
provider_source_path: z.string().nullable(),
|
||||
provider_image_header_hex: z
|
||||
.string()
|
||||
.regex(/^[a-f0-9]{24}$/u)
|
||||
.nullable(),
|
||||
});
|
||||
type Observation = z.infer<typeof observationSchema>;
|
||||
|
||||
/** Bind native image-relative offsets to coordinates in the admitted source bytes. */
|
||||
export async function mapHopperFileOffset(
|
||||
target: BinaryTarget,
|
||||
value: JsonValue,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<JsonValue, AnalysisError>> {
|
||||
const parsed = observationSchema.safeParse(value);
|
||||
if (!parsed.success)
|
||||
return err(
|
||||
new AnalysisOutputError(
|
||||
OPERATION,
|
||||
"Hopper's native file mapping observation is malformed",
|
||||
),
|
||||
);
|
||||
const observation = parsed.data;
|
||||
const partial = (reason: string): FileOffsetPartialObservation => ({
|
||||
operation: OPERATION,
|
||||
...observation,
|
||||
original_file_offset: { available: false, reason },
|
||||
});
|
||||
const unavailable = (reason: string) =>
|
||||
err(
|
||||
new AnalysisCapabilityUnavailableError("hopper", OPERATION, reason, {
|
||||
partialObservation: partial(reason),
|
||||
}),
|
||||
);
|
||||
if (target.kind !== "executable")
|
||||
return unavailable(
|
||||
"The selected database digest does not identify its original executable bytes. Open that executable to verify original-file coordinates; read_bytes still exposes the database's native bytes.",
|
||||
);
|
||||
if (signal?.aborted)
|
||||
return err(
|
||||
new AnalysisCancelledError(OPERATION, {
|
||||
partialObservation: partial("Source verification was cancelled"),
|
||||
}),
|
||||
);
|
||||
try {
|
||||
const captured = await snapshotFromPath(target.path, signal);
|
||||
if (!captured.ok)
|
||||
return err(
|
||||
new AnalysisArtifactChangedError(
|
||||
OPERATION,
|
||||
target.path,
|
||||
captured.error,
|
||||
{
|
||||
partialObservation: partial(captured.error),
|
||||
},
|
||||
),
|
||||
);
|
||||
const snapshot = captured.value;
|
||||
if (signal?.aborted)
|
||||
return err(
|
||||
new AnalysisCancelledError(OPERATION, {
|
||||
partialObservation: partial("Source verification was cancelled"),
|
||||
}),
|
||||
);
|
||||
if (snapshot.sha256 !== target.sha256) {
|
||||
const reason = `Source ${target.path} differs from the admitted artifact: expected SHA-256 ${target.sha256}, observed ${snapshot.sha256}. Reopen the changed source for a new analysis identity.`;
|
||||
return err(
|
||||
new AnalysisArtifactChangedError(OPERATION, target.path, reason, {
|
||||
partialObservation: partial(reason),
|
||||
}),
|
||||
);
|
||||
}
|
||||
const mapped = originalOffset(snapshot, observation);
|
||||
if (!mapped.ok) return unavailable(mapped.error);
|
||||
return ok({
|
||||
address: observation.address,
|
||||
file_offset: mapped.value + observation.provider_file_offset,
|
||||
provider_file_offset: observation.provider_file_offset,
|
||||
image_base_file_offset: mapped.value,
|
||||
source_path: target.path,
|
||||
});
|
||||
} catch (cause: unknown) {
|
||||
if (signal?.aborted)
|
||||
return err(
|
||||
new AnalysisCancelledError(OPERATION, {
|
||||
cause,
|
||||
partialObservation: partial("Source verification was cancelled"),
|
||||
}),
|
||||
);
|
||||
const reason = `Cannot verify original source bytes: ${cause instanceof Error ? cause.message : String(cause)}`;
|
||||
const code =
|
||||
typeof cause === "object" && cause !== null && "code" in cause
|
||||
? cause.code
|
||||
: undefined;
|
||||
if (code === "EACCES" || code === "EPERM")
|
||||
return err(
|
||||
new AnalysisAccessDeniedError(OPERATION, target.path, code, {
|
||||
cause,
|
||||
partialObservation: partial(reason),
|
||||
}),
|
||||
);
|
||||
return err(
|
||||
new BinaryTargetError(target.path, reason, {
|
||||
cause,
|
||||
partialObservation: partial(reason),
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function snapshotFromPath(path: string, signal?: AbortSignal) {
|
||||
const file = await open(path, constants.O_RDONLY | constants.O_NONBLOCK);
|
||||
try {
|
||||
if (!(await file.stat()).isFile())
|
||||
return err(
|
||||
`Source ${path} is no longer a regular file. Restore the admitted executable or reopen a regular source.`,
|
||||
);
|
||||
return ok(await readSourceSnapshot(file, signal));
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
}
|
||||
|
||||
interface FatLayout {
|
||||
readonly slices: readonly FatSliceDeclaration[];
|
||||
readonly tableEnd: number;
|
||||
}
|
||||
|
||||
function fatLayout(prefix: Buffer): Result<FatLayout | null, string> {
|
||||
const magic = prefix.length < 4 ? 0 : prefix.readUInt32BE(0);
|
||||
if (![0xcafebabe, 0xbebafeca, 0xcafebabf, 0xbfbafeca].includes(magic))
|
||||
return ok(null);
|
||||
if (prefix.length < 8) return err("The original FAT header is truncated");
|
||||
const littleEndian = magic === 0xbebafeca || magic === 0xbfbafeca;
|
||||
const wide = magic === 0xcafebabf || magic === 0xbfbafeca;
|
||||
const count = littleEndian ? prefix.readUInt32LE(4) : prefix.readUInt32BE(4);
|
||||
if (count === 0 || count > MAX_FAT_ARCHITECTURES)
|
||||
return err("FAT architecture count is outside the supported 1..128 range");
|
||||
const tableEnd = 8 + count * (wide ? 32 : 20);
|
||||
const table = prefix.subarray(8, tableEnd);
|
||||
const parsed = readFatSliceDeclarations(table, { count, wide, littleEndian });
|
||||
if (parsed.status !== "parsed") return err(parsed.reason);
|
||||
if (wide) {
|
||||
for (const slice of parsed.slices)
|
||||
if (
|
||||
(littleEndian
|
||||
? table.readUInt32LE(slice.index * 32 + 28)
|
||||
: table.readUInt32BE(slice.index * 32 + 28)) !== 0
|
||||
)
|
||||
return err(
|
||||
`FAT64 architecture ${slice.index} has a nonzero reserved field`,
|
||||
);
|
||||
}
|
||||
return ok({ slices: parsed.slices, tableEnd });
|
||||
}
|
||||
|
||||
async function readSourceSnapshot(file: FileHandle, signal?: AbortSignal) {
|
||||
const prefix = Buffer.alloc(PREFIX_BYTES);
|
||||
const hash = createHash("sha256");
|
||||
let size = 0;
|
||||
let layout: Result<FatLayout | null, string> | undefined;
|
||||
const headers = new Map<number, Buffer>();
|
||||
const capture = (bytes: Buffer, position: number) => {
|
||||
if (layout?.ok !== true || layout.value === null) return;
|
||||
for (const slice of layout.value.slices) {
|
||||
const start = Math.max(position, slice.offset);
|
||||
const end = Math.min(position + bytes.length, slice.offset + 12);
|
||||
if (end <= start) continue;
|
||||
let header = headers.get(slice.index);
|
||||
if (header === undefined) {
|
||||
header = Buffer.alloc(12);
|
||||
headers.set(slice.index, header);
|
||||
}
|
||||
bytes.copy(
|
||||
header,
|
||||
start - slice.offset,
|
||||
start - position,
|
||||
end - position,
|
||||
);
|
||||
}
|
||||
};
|
||||
for await (const chunk of file.createReadStream({
|
||||
start: 0,
|
||||
autoClose: false,
|
||||
...(signal === undefined ? {} : { signal }),
|
||||
})) {
|
||||
const bytes = Buffer.from(chunk);
|
||||
hash.update(bytes);
|
||||
if (size < prefix.length)
|
||||
bytes.copy(prefix, size, 0, Math.min(bytes.length, prefix.length - size));
|
||||
if (layout === undefined && size + bytes.length >= prefix.length) {
|
||||
layout = fatLayout(prefix);
|
||||
capture(prefix, 0);
|
||||
}
|
||||
capture(bytes, size);
|
||||
size += bytes.length;
|
||||
}
|
||||
if (layout === undefined) {
|
||||
layout = fatLayout(prefix.subarray(0, size));
|
||||
capture(prefix.subarray(0, size), 0);
|
||||
}
|
||||
return { layout, headers, size, sha256: hash.digest("hex") };
|
||||
}
|
||||
|
||||
type SourceSnapshot = Awaited<ReturnType<typeof readSourceSnapshot>>;
|
||||
|
||||
function originalOffset(
|
||||
snapshot: SourceSnapshot,
|
||||
observation: Observation,
|
||||
): Result<number, string> {
|
||||
const { layout, size, headers } = snapshot;
|
||||
if (!layout.ok) return layout;
|
||||
if (layout.value === null)
|
||||
return observation.provider_file_offset < size
|
||||
? ok(0)
|
||||
: err("Provider mapping lies outside the admitted executable bytes");
|
||||
const header =
|
||||
observation.provider_image_header_hex === null
|
||||
? null
|
||||
: Buffer.from(observation.provider_image_header_hex, "hex");
|
||||
const loadedMagic = header?.readUInt32BE(0);
|
||||
if (
|
||||
header === null ||
|
||||
loadedMagic === undefined ||
|
||||
![0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe].includes(loadedMagic)
|
||||
)
|
||||
return err(
|
||||
"Hopper did not expose the loaded Mach-O header needed to identify its FAT slice",
|
||||
);
|
||||
const little = loadedMagic === 0xcefaedfe || loadedMagic === 0xcffaedfe;
|
||||
const cpu = little ? header.readUInt32LE(4) : header.readUInt32BE(4);
|
||||
const subtype = little ? header.readUInt32LE(8) : header.readUInt32BE(8);
|
||||
const matches = [];
|
||||
const ranges = [...layout.value.slices].sort((a, b) => a.offset - b.offset);
|
||||
for (let index = 1; index < ranges.length; index++) {
|
||||
const previous = ranges[index - 1];
|
||||
const current = ranges[index];
|
||||
if (
|
||||
previous !== undefined &&
|
||||
current !== undefined &&
|
||||
current.offset < previous.offset + previous.size
|
||||
)
|
||||
return err("Original FAT slices overlap");
|
||||
}
|
||||
for (const slice of layout.value.slices) {
|
||||
const reason = validateFatSlice(slice, size, layout.value.tableEnd);
|
||||
if (reason !== null) return err(reason);
|
||||
const embedded = headers.get(slice.index);
|
||||
if (slice.size < 12 || embedded === undefined)
|
||||
return err(
|
||||
`FAT architecture ${slice.index} has a truncated Mach-O header`,
|
||||
);
|
||||
const embeddedMagic = embedded.readUInt32BE(0);
|
||||
if (
|
||||
![0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe].includes(embeddedMagic)
|
||||
)
|
||||
return err(
|
||||
`FAT architecture ${slice.index} does not contain a Mach-O image`,
|
||||
);
|
||||
const embeddedLittle =
|
||||
embeddedMagic === 0xcefaedfe || embeddedMagic === 0xcffaedfe;
|
||||
const identityError = validateFatSlice(slice, size, layout.value.tableEnd, {
|
||||
cpuType: embeddedLittle
|
||||
? embedded.readUInt32LE(4)
|
||||
: embedded.readUInt32BE(4),
|
||||
cpuSubtype: embeddedLittle
|
||||
? embedded.readUInt32LE(8)
|
||||
: embedded.readUInt32BE(8),
|
||||
});
|
||||
if (identityError !== null) return err(identityError);
|
||||
if (slice.cpuType === cpu && slice.cpuSubtype === subtype)
|
||||
matches.push(slice);
|
||||
}
|
||||
const selected = matches.length === 1 ? matches[0] : undefined;
|
||||
if (selected === undefined)
|
||||
return err(
|
||||
`Original FAT slice matching Hopper's loaded header is ${matches.length === 0 ? "missing" : "ambiguous"}`,
|
||||
);
|
||||
if (headers.get(selected.index)?.equals(header) !== true)
|
||||
return err(
|
||||
"Original FAT slice header disagrees with Hopper's loaded header",
|
||||
);
|
||||
if (observation.provider_file_offset >= selected.size)
|
||||
return err("Provider mapping lies outside the loaded FAT slice");
|
||||
if (!Number.isSafeInteger(selected.offset + observation.provider_file_offset))
|
||||
return err("Original file offset exceeds the exact JSON integer range");
|
||||
return ok(selected.offset);
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
hopperLoaderArgsForTarget,
|
||||
resolveHopperAnalysisProfile,
|
||||
} from "./HopperAnalysisProfile.js";
|
||||
import { mapHopperFileOffset } from "./HopperFileOffset.js";
|
||||
import { HopperRegexSearch } from "./HopperRegexSearch.js";
|
||||
import { HopperClient } from "./HopperClient.js";
|
||||
import { hopperMachOImageSchema } from "./HopperMachOImage.js";
|
||||
@@ -239,10 +240,16 @@ export class HopperProvider implements AnalysisProviderCandidate {
|
||||
parameters.mode === "regex"
|
||||
? await regexSearch.execute(operation, parameters, options)
|
||||
: await client.callTool(operation, parameters, options);
|
||||
return result.ok
|
||||
if (!result.ok) return result;
|
||||
const mapped =
|
||||
operation === "address_to_file_offset"
|
||||
? await mapHopperFileOffset(target, result.value, options?.signal)
|
||||
: result;
|
||||
return mapped.ok
|
||||
? {
|
||||
ok: true,
|
||||
value: createAnalysisExecution(result.value, executionProvider, {
|
||||
value: createAnalysisExecution(mapped.value, executionProvider, {
|
||||
rawResult: result.value,
|
||||
...(profile === undefined ? {} : { analysisProfile: profile }),
|
||||
limitations: [
|
||||
...(preparedImage === undefined
|
||||
@@ -266,7 +273,7 @@ export class HopperProvider implements AnalysisProviderCandidate {
|
||||
],
|
||||
}),
|
||||
}
|
||||
: result;
|
||||
: mapped;
|
||||
},
|
||||
runtimeLineageSnapshots: () => {
|
||||
const observation = client.runtimeLineage();
|
||||
|
||||
@@ -79,6 +79,11 @@ export const CAPABILITIES: readonly CapabilityDescriptor[] = Object.freeze(
|
||||
}),
|
||||
limitations: Object.freeze([
|
||||
"Results depend on Hopper's completed static analysis.",
|
||||
...(operation === "address_to_file_offset"
|
||||
? [
|
||||
"Original-file coordinates require source bytes matching the selected executable digest. A changed, unavailable, or database-only source retains native mapping facts in partial error evidence.",
|
||||
]
|
||||
: []),
|
||||
...(operation === "search_strings" || operation === "search_procedures"
|
||||
? [
|
||||
"Regex mode uses ECMAScript Unicode semantics in a supervised worker with a five-second matching deadline. Cancellation or deadline failure stops matching without blocking Hopper; literal mode retains native casefold semantics.",
|
||||
|
||||
Reference in New Issue
Block a user