Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(native): bound metadata projection and compact Mach-O bind runs

morluto5d0a8836059b
diff --git a/src/native/AppleDispatchMetadata.ts b/src/native/AppleDispatchMetadata.ts
index 1ff266807..1b54c42b8 100644
--- a/src/native/AppleDispatchMetadata.ts
+++ b/src/native/AppleDispatchMetadata.ts
@@ -1,13 +1,16 @@
import { createHash } from "node:crypto";
-import { open } from "node:fs/promises";
-import { constants } from "node:fs";
import {
objcSwiftMetadataSchema,
nativeDispatchMetadataResultSchema,
type ObjcSwiftMetadata,
} from "../domain/native/objcSwiftMetadata.js";
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
-import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
+import {
+ AnalysisCancelledError,
+ AnalysisResourceConstraintError,
+} from "../domain/analysisErrorCore.js";
+import { openRegularFile } from "../filesystem/RegularFile.js";
+import { readBoundedFileBytes } from "../process/BoundedFileBytes.js";
import { EvidenceIntegrityError } from "../domain/evidenceErrors.js";
import { pushDispatchCoverage } from "./AppleDispatchCoverage.js";
import { parsePointerFixups } from "./AppleMachoFixups.js";
@@ -173,19 +176,25 @@ export const inspectAppleDispatchMetadata = async (
) => {
if (target.kind !== "executable" || target.format !== "mach-o")
throw new TypeError("Apple dispatch metadata requires a Mach-O target");
- const handle = await open(
- target.path,
- constants.O_RDONLY | constants.O_NOFOLLOW,
- );
+ const maximumBytes = 64 * 1024 * 1024;
+ const capacityError = () =>
+ new AnalysisResourceConstraintError(
+ "inspect_native_dispatch_metadata",
+ "memory",
+ "Apple static metadata input exceeds its 64 MiB snapshot budget",
+ { input_bytes: maximumBytes },
+ );
+ const handle = await openRegularFile(target.path, {
+ symlinks: "reject",
+ signal,
+ });
try {
const before = await handle.stat();
- if (!before.isFile() || before.size > 64 * 1024 * 1024)
- throw new RangeError(
- "Apple metadata target must be a regular file no larger than 64 MiB",
- );
+ if (before.size > maximumBytes) throw capacityError();
if (signal?.aborted)
throw new AnalysisCancelledError("inspect_native_dispatch_metadata");
- const bytes = await handle.readFile({ signal });
+ const bytes = await readBoundedFileBytes(handle, maximumBytes, signal);
+ if (bytes === undefined) throw capacityError();
const digest = createHash("sha256").update(bytes).digest("hex");
if (digest !== target.sha256)
throw new EvidenceIntegrityError(
diff --git a/src/native/AppleMachoFixups.ts b/src/native/AppleMachoFixups.ts
index 1422ff8c7..b8181fc79 100644
--- a/src/native/AppleMachoFixups.ts
+++ b/src/native/AppleMachoFixups.ts
@@ -69,6 +69,13 @@ interface Import {
readonly addend: bigint;
}
+interface BindRun {
+ readonly start: bigint;
+ count: bigint;
+ readonly stride: bigint;
+ readonly binding: Import;
+}
+
/** Library ordinal to install name; 0 and negative ordinals are special lookups. */
const libraryName = (
ordinal: number,
@@ -307,6 +314,7 @@ const sleb = (bytes: Buffer, cursor: { at: number }, end: number): bigint => {
/** Registers of the `LC_DYLD_INFO` bind state machine. */
interface BindState {
ordinal: number;
+ segmentIndex: number;
symbol: string;
weak: boolean;
addend: bigint;
@@ -351,6 +359,7 @@ const setBindRegister = (
const segment = image.segments[immediate];
if (segment === undefined)
throw new RangeError("Bind segment index is out of range");
+ state.segmentIndex = immediate;
state.address = segment.address + uleb(bytes, cursor, end);
return true;
}
@@ -368,7 +377,7 @@ const setBindRegister = (
const readBindStream = (
image: FixupImage,
range: FixupCommands["binds"][number],
- binds: Map<bigint, Import>,
+ binds: BindRun[],
): void => {
const { bytes } = image;
const end = range.offset + range.size;
@@ -377,13 +386,27 @@ const readBindStream = (
const cursor = { at: range.offset };
const state: BindState = {
ordinal: 0,
+ segmentIndex: -1,
symbol: "",
weak: false,
addend: 0n,
address: 0n,
};
- const bind = (skip: bigint): void => {
- binds.set(state.address, {
+ const preflightRun = (count: bigint, skip: bigint): void => {
+ if (count === 0n) return;
+ const segment = image.segments[state.segmentIndex];
+ if (segment === undefined)
+ throw new RangeError("Bind location has no selected segment");
+ const stride = 8n + skip;
+ const end = state.address + (count - 1n) * stride + 8n;
+ if (state.address < segment.address || end > segment.address + segment.size)
+ throw new RangeError("Bind expansion leaves its file-backed segment");
+ };
+ const bind = (count: bigint, skip: bigint): void => {
+ if (count === 0n) return;
+ preflightRun(count, skip);
+ const stride = 8n + skip;
+ const binding: Import = {
symbol: state.symbol,
library:
range.stream === "weak"
@@ -391,8 +414,22 @@ const readBindStream = (
: libraryName(state.ordinal, image.dylibs),
weak: state.weak,
addend: state.addend,
- });
- state.address += 8n + skip;
+ };
+ const previous = binds.at(-1);
+ if (
+ previous !== undefined &&
+ previous.stride === stride &&
+ previous.start + previous.count * stride === state.address &&
+ previous.binding.symbol === binding.symbol &&
+ previous.binding.library === binding.library &&
+ previous.binding.weak === binding.weak &&
+ previous.binding.addend === binding.addend
+ ) {
+ previous.count += count;
+ } else {
+ binds.push({ start: state.address, count, stride, binding });
+ }
+ state.address += count * stride;
};
while (cursor.at < end) {
const byte = bytes[cursor.at++] ?? 0;
@@ -403,18 +440,22 @@ const readBindStream = (
if (range.stream !== "lazy") return;
break;
case 0x90:
- bind(0n);
+ bind(1n, 0n);
break;
case 0xa0:
- bind(uleb(bytes, cursor, end));
+ bind(1n, uleb(bytes, cursor, end));
break;
case 0xb0:
- bind(BigInt(byte & 0x0f) * 8n);
+ bind(1n, BigInt(byte & 0x0f) * 8n);
break;
case 0xc0: {
+ if (range.stream === "lazy")
+ throw new RangeError(
+ "Lazy bind streams do not support repeated bind opcodes",
+ );
const count = uleb(bytes, cursor, end);
const skip = uleb(bytes, cursor, end);
- for (let index = 0n; index < count; index++) bind(skip);
+ bind(count, skip);
break;
}
default:
@@ -451,7 +492,7 @@ export const parsePointerFixups = (
unreadRebaseBytes,
decode: (_address, raw) => ({ kind: "rebase", target: raw }),
};
- const binds = new Map<bigint, Import>();
+ const binds: BindRun[] = [];
const failures: string[] = [];
for (const stream of commands.binds)
try {
@@ -471,10 +512,26 @@ export const parsePointerFixups = (
examined: commands.binds.length,
unreadRebaseBytes,
decode(address, raw) {
- const bound = binds.get(address);
- return bound === undefined
+ let match: BindRun | undefined;
+ for (const run of binds) {
+ const { start, count, stride } = run;
+ const distance = address - start;
+ if (
+ distance >= 0n &&
+ distance % stride === 0n &&
+ distance / stride < count
+ ) {
+ if (match !== undefined)
+ return {
+ kind: "unsupported",
+ reason: `Ambiguous bind records target pointer location 0x${address.toString(16)}`,
+ };
+ match = run;
+ }
+ }
+ return match === undefined
? { kind: "rebase", target: raw }
- : { kind: "bind", ...bound };
+ : { kind: "bind", ...match.binding };
},
};
};
diff --git a/src/native/pe/PeResourceParser.ts b/src/native/pe/PeResourceParser.ts
index 66b48b06c..a72687fc0 100644
--- a/src/native/pe/PeResourceParser.ts
+++ b/src/native/pe/PeResourceParser.ts
@@ -1,6 +1,7 @@
import { createHash } from "node:crypto";
import { setImmediate } from "node:timers/promises";
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
+import { jsonParts } from "../../domain/jsonSerialization.js";
import type {
InspectPeResourcesInput,
PeResourceIdentity,
@@ -20,6 +21,15 @@ const METADATA_BYTES = 8 * 1024 * 1024;
const limit = (message: string): never => {
throw new ArtifactReaderFailure("limit", message);
};
+/** Count bytes through the canonical bounded JSON writer. */
+const jsonBytes = (value: unknown, maximum: number): number => {
+ let size = 0;
+ for (const part of jsonParts(value)) {
+ size += Buffer.byteLength(part, "utf8");
+ if (size > maximum) return size;
+ }
+ return size;
+};
const identityKey = (id: PeResourceIdentity): string =>
id.kind === "id" ? `id:${String(id.id)}` : `name:${id.utf16le_hex}`;
@@ -38,13 +48,27 @@ export const parsePeResources = async (
const names = new Map<number, PeResourceIdentity>();
let examined = 0;
let metadataBytes = 0;
- const account = (value: unknown): void => {
- metadataBytes += Buffer.byteLength(JSON.stringify(value));
- if (metadataBytes > METADATA_BYTES)
+ const ensureCapacity = (projectedBytes: number): void => {
+ if (metadataBytes + projectedBytes > METADATA_BYTES)
limit(
"PE resource metadata exceeds the 8 MiB complete-result allocation budget.",
);
};
+ const accountBytes = (projectedBytes: number): void => {
+ ensureCapacity(projectedBytes);
+ metadataBytes += projectedBytes;
+ };
+ const account = (value: unknown): void => {
+ // Rows are retained in arrays; reserve a separator along with each row.
+ accountBytes(jsonBytes(value, METADATA_BYTES - metadataBytes - 1) + 1);
+ };
+ const reserveDecodedName = (utf16Units: number): void => {
+ // A JSON string can need six ASCII bytes per UTF-16 code unit (for example,
+ // a control character encoded as `\\u0000`). The original spelling and
+ // its hex form are both retained in the result.
+ const projectedBytes = 10 * utf16Units + 256;
+ ensureCapacity(projectedBytes);
+ };
const root = layout.size === 0 ? 0 : layout.map(layout.rva, layout.size);
const relative = (at: number, length: number): number => {
if (at > layout.size - length || at < 0)
@@ -70,7 +94,9 @@ export const parsePeResources = async (
if (previous !== undefined) return previous;
if (at % 2 !== 0) peFailure("Unaligned PE resource name.");
const start = relative(at, 2);
- const length = bytes.readUInt16LE(start) * 2;
+ const utf16Units = bytes.readUInt16LE(start);
+ reserveDecodedName(utf16Units);
+ const length = utf16Units * 2;
relative(at, 2 + length);
const raw = bytes.subarray(start + 2, start + 2 + length);
const value: PeResourceIdentity = {
@@ -80,7 +106,6 @@ export const parsePeResources = async (
location: recordRange(start, 2 + length, "name"),
};
names.set(at, value);
- account(value);
return value;
};
const visit = async (
@@ -110,8 +135,8 @@ export const parsePeResources = async (
named_entries: named,
id_entries: ids,
};
- directories.push(directory);
account(directory);
+ directories.push(directory);
const keys = new Set<string>();
const nested = new Set([...ancestors, at]);
for (let index = 0; index < count; index++) {
@@ -168,8 +193,8 @@ export const parsePeResources = async (
sha256: "0".repeat(64),
},
};
- resources.push(resource);
account(resource);
+ resources.push(resource);
}
}
};
@@ -197,7 +222,7 @@ export const parsePeResources = async (
}
resource.payload.sha256 = digest;
}
- const iconGroups = readIconGroups(bytes, resources, account);
+ const iconGroups = readIconGroups(bytes, resources, account, ensureCapacity);
const report: PeResources = {
artifact,
format: layout.format,
@@ -225,7 +250,7 @@ export const parsePeResources = async (
],
};
signal?.throwIfAborted();
- if (Buffer.byteLength(JSON.stringify(report)) > METADATA_BYTES)
+ if (jsonBytes(report, METADATA_BYTES) > METADATA_BYTES)
limit("PE resource result exceeds the 8 MiB allocation budget.");
return report;
};
@@ -252,6 +277,7 @@ const readIconGroups = (
bytes: Buffer,
resources: PeResources["resources"],
account: (value: unknown) => void,
+ ensureCapacity: (projectedBytes: number) => void,
): PeResources["icon_groups"] => {
const imagesById = new Map<number, PeResources["resources"]>();
for (const resource of resources) {
@@ -289,6 +315,9 @@ const readIconGroups = (
(resource) =>
identityKey(resource.language) === identityKey(group.language),
);
+ // Indices are uint32 values and each needs at most ten decimal digits
+ // plus a comma. Check the expanded candidate list before allocating it.
+ ensureCapacity(302 + candidates.length * 11);
const declaredBytes = bytes.readUInt32LE(at + 8);
const image = {
location: { offset: at, bytes: 14 },
@@ -310,6 +339,7 @@ const readIconGroups = (
account(image);
images.push(image);
}
+ account({ resource_index: group.index, images: [] });
return { resource_index: group.index, images };
});
};