Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(apple): preserve 128-bit keyed archive integers (#4324)

Polly Labs44c9ff7b75d0
diff --git a/src/artifacts/apple/InterfaceBuilderAnalysis.ts b/src/artifacts/apple/InterfaceBuilderAnalysis.ts
index b527c14e0..86e8941c9 100644
--- a/src/artifacts/apple/InterfaceBuilderAnalysis.ts
+++ b/src/artifacts/apple/InterfaceBuilderAnalysis.ts
@@ -37,7 +37,8 @@ import { compareUnicodeCodePoints } from "../../domain/unicodeCodePointOrder.js"
import { decodeXmlPlistText } from "../../domain/propertyListXmlText.js";
import {
- binaryPlistNumberLiterals,
+ decodeBinaryPlistNumbers,
+ scanBinaryPlistNumberLiterals,
xmlPlistNumberLiterals,
} from "./PlistNumberLiterals.js";
import type { PlistNumberLiterals } from "../../domain/apple/plistNumbers.js";
@@ -749,9 +750,14 @@ const decodePlist = (
readonly numberLiterals: PlistNumberLiterals;
} => {
const binary = hasBinaryPlistSignature(bytes);
+ const binaryNumberMetadata = binary
+ ? scanBinaryPlistNumberLiterals(bytes)
+ : undefined;
const { value, omittedPrototypeKeys } = binary
? {
- value: parseBinary(bytes),
+ value: parseBinary(
+ decodeBinaryPlistNumbers(bytes, binaryNumberMetadata!),
+ ),
omittedPrototypeKeys: binaryPrototypeKeyObserved ? null : 0,
}
: parseXmlWithinDecodeBudget(xmlText ?? decodeXmlPlistText(bytes));
@@ -761,7 +767,7 @@ const decodePlist = (
omittedPrototypeKeys,
unknownDateCount: projected.unknownDateCount,
numberLiterals: binary
- ? binaryPlistNumberLiterals(bytes)
+ ? binaryNumberMetadata!.literals
: xmlPlistNumberLiterals(xmlText ?? decodeXmlPlistText(bytes)),
};
};
diff --git a/src/artifacts/apple/InterfaceBuilderDecodeBudget.test.ts b/src/artifacts/apple/InterfaceBuilderDecodeBudget.test.ts
index 0ec14669b..61492d69d 100644
--- a/src/artifacts/apple/InterfaceBuilderDecodeBudget.test.ts
+++ b/src/artifacts/apple/InterfaceBuilderDecodeBudget.test.ts
@@ -27,6 +27,14 @@ it.each([
bytes,
1024 * 1024,
).estimatedBytes;
+ const projected = preflightPropertyListDecode(
+ bytes,
+ 1024 * 1024,
+ undefined,
+ { numberProjection: true },
+ ).estimatedBytes;
+ if (_format === "binary")
+ expect(projected).toBeGreaterThanOrEqual(ordinary + bytes.length);
expect(() =>
preflightPropertyListDecode(bytes, ordinary, undefined, {
numberProjection: true,
diff --git a/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts b/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
index d6292c309..fe813e8a9 100644
--- a/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
+++ b/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
@@ -277,7 +277,10 @@ export const estimateBinaryPlistExpansion = (
// Metadata covers the full object table; projected values may repeat through references.
const objectBytes = numberProjection ? 144 : 16;
const expandedNodeBytes = numberProjection ? 224 : 96;
- let estimate = bytes.length * 2 + objectCount * objectBytes;
+ // Number projection may require a same-sized parser-compatible input copy
+ // for wide integers. Reserve for it before the caller allocates that copy.
+ let estimate =
+ bytes.length * (numberProjection ? 3 : 2) + objectCount * objectBytes;
let binaryPrototypeKeyObserved = false;
if (estimate > maxBytes)
throw fail("expansion", `binary plist object table exceeds ${budget}`);
diff --git a/src/artifacts/apple/KeyedArchiveReader.ts b/src/artifacts/apple/KeyedArchiveReader.ts
index ff1bdf99c..0da53002f 100644
--- a/src/artifacts/apple/KeyedArchiveReader.ts
+++ b/src/artifacts/apple/KeyedArchiveReader.ts
@@ -12,7 +12,8 @@ import {
} from "../../domain/apple/plistValue.js";
import { createPlistNumberProjection } from "../../domain/apple/plistNumbers.js";
import {
- binaryPlistNumberLiterals,
+ decodeBinaryPlistNumbers,
+ scanBinaryPlistNumberLiterals,
xmlPlistNumberLiterals,
} from "./PlistNumberLiterals.js";
import {
@@ -67,12 +68,18 @@ export const decodeKeyedArchiveBytes = (
)
: new RangeError(message),
bounds: "decoder",
+ numberProjection: true,
})
: undefined;
+ const binaryNumberMetadata = binary
+ ? scanBinaryPlistNumberLiterals(bytes)
+ : undefined;
const parsed =
binaryExpansion !== undefined
? {
- value: parseBinary(bytes),
+ value: parseBinary(
+ decodeBinaryPlistNumbers(bytes, binaryNumberMetadata!),
+ ),
omittedPrototypeKeys: binaryExpansion.binaryPrototypeKeyObserved
? null
: 0,
@@ -86,7 +93,7 @@ export const decodeKeyedArchiveBytes = (
const graph = projectKeyedArchive(plistValue, selection);
const numbers = createPlistNumberProjection(
binary
- ? binaryPlistNumberLiterals(bytes)
+ ? binaryNumberMetadata!.literals
: xmlPlistNumberLiterals(xmlText ?? ""),
);
// Classify the serialized graph before projecting values to typed decimals.
diff --git a/src/artifacts/apple/PlistNumberLiterals.test.ts b/src/artifacts/apple/PlistNumberLiterals.test.ts
new file mode 100644
index 000000000..103f28bed
--- /dev/null
+++ b/src/artifacts/apple/PlistNumberLiterals.test.ts
@@ -0,0 +1,40 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ decodeBinaryPlistNumbers,
+ scanBinaryPlistNumberLiterals,
+} from "./PlistNumberLiterals.js";
+
+const dataObjectWithUnusedOverlappingInteger = (): Buffer => {
+ const bytes = Buffer.alloc(66);
+ Buffer.from("bplist00", "ascii").copy(bytes);
+ bytes[8] = 0xa1; // One-element array referencing data object 1.
+ bytes[9] = 1;
+ bytes[10] = 0x4f; // Data object with an extended length of 18 bytes.
+ bytes[11] = 0x10;
+ bytes[12] = 18;
+ bytes[13] = 0x14; // An unused object-table entry points into this data.
+ bytes.fill(0xff, 14, 30);
+ bytes[30] = 0;
+ bytes.set([8, 10, 13], 31); // object offsets, including the overlapping entry
+ bytes[40] = 1; // offset width
+ bytes[41] = 1; // reference width
+ bytes.writeBigUInt64BE(3n, 42);
+ bytes.writeBigUInt64BE(0n, 50);
+ bytes.writeBigUInt64BE(31n, 58);
+ return bytes;
+};
+
+describe("binary plist number rewriting", () => {
+ it("does not rewrite an integer marker inside another object's data", () => {
+ const bytes = dataObjectWithUnusedOverlappingInteger();
+ const metadata = scanBinaryPlistNumberLiterals(bytes);
+
+ expect(metadata.literals.incomplete).toBe(true);
+ expect(metadata.wideIntegers).toEqual([]);
+ expect(decodeBinaryPlistNumbers(bytes, metadata)).toBe(bytes);
+ expect(bytes.subarray(13, 30)).toEqual(
+ Buffer.from([0x14, ...Array<number>(16).fill(0xff)]),
+ );
+ });
+});
diff --git a/src/artifacts/apple/PlistNumberLiterals.ts b/src/artifacts/apple/PlistNumberLiterals.ts
index 6fec67c74..b5293a0e0 100644
--- a/src/artifacts/apple/PlistNumberLiterals.ts
+++ b/src/artifacts/apple/PlistNumberLiterals.ts
@@ -120,27 +120,89 @@ export const xmlPlistNumberLiterals = (xml: string): PlistNumberLiterals => {
return output.result();
};
+export interface BinaryPlistNumberMetadata {
+ readonly literals: PlistNumberLiterals;
+ /** 16-byte integers the pinned plist decoder cannot decode correctly. */
+ readonly wideIntegers: readonly { offset: number; value: bigint }[];
+}
+
+const binaryObjectEnd = (
+ bytes: Buffer,
+ offset: number,
+ objectEnd: number,
+ referenceWidth: number,
+): number | undefined => {
+ const marker = bytes[offset];
+ if (marker === undefined) return undefined;
+ const type = marker >> 4;
+ let length = marker & 0x0f;
+ let cursor = offset + 1;
+ if (length === 0x0f && type !== 0 && type !== 8) {
+ const extendedMarker = bytes[cursor];
+ if (extendedMarker === undefined || extendedMarker >> 4 !== 1)
+ return undefined;
+ const width = 1 << (extendedMarker & 0x0f);
+ if (width > 8 || cursor + 1 + width > objectEnd) return undefined;
+ cursor += 1;
+ let extendedLength = 0n;
+ for (let index = 0; index < width; index += 1)
+ extendedLength =
+ (extendedLength << 8n) | BigInt(bytes[cursor + index] ?? 0);
+ if (extendedLength > BigInt(Number.MAX_SAFE_INTEGER)) return undefined;
+ length = Number(extendedLength);
+ cursor += width;
+ }
+ let payloadLength: number;
+ if (type === 0) payloadLength = 0;
+ else if (type === 1 || type === 2) {
+ if ((marker & 0x0f) > 4) return undefined;
+ payloadLength = 1 << (marker & 0x0f);
+ } else if (type === 3) {
+ if (marker !== 0x33) return undefined;
+ payloadLength = 8;
+ } else if (type === 4 || type === 5 || type === 7) {
+ payloadLength = length;
+ } else if (type === 6) {
+ payloadLength = length * 2;
+ } else if (type === 8) {
+ payloadLength = (marker & 0x0f) + 1;
+ } else if (type === 10 || type === 11 || type === 12) {
+ payloadLength = length * referenceWidth;
+ } else if (type === 13) {
+ payloadLength = length * 2 * referenceWidth;
+ } else return undefined;
+ const end = cursor + payloadLength;
+ return Number.isSafeInteger(end) && end <= objectEnd ? end : undefined;
+};
+
/** Observe bounded binary number markers without changing primary decode acceptance. */
-export const binaryPlistNumberLiterals = (
+export const scanBinaryPlistNumberLiterals = (
bytes: Buffer,
-): PlistNumberLiterals => {
+): BinaryPlistNumberMetadata => {
const output = collector();
+ const wideIntegers: { offset: number; value: bigint }[] = [];
if (bytes.length < 40 || !hasBinaryPlistSignature(bytes))
- return output.result(true);
+ return { literals: output.result(true), wideIntegers };
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const trailer = bytes.length - 32;
const offsetWidth = bytes[trailer + 6] ?? 0;
- if (![1, 2, 4, 8].includes(offsetWidth)) return output.result(true);
+ if (![1, 2, 4, 8].includes(offsetWidth))
+ return { literals: output.result(true), wideIntegers };
const countRaw = view.getBigUint64(trailer + 8);
const tableRaw = view.getBigUint64(trailer + 24);
const safeMaximum = BigInt(Number.MAX_SAFE_INTEGER);
if (countRaw === 0n || countRaw > safeMaximum || tableRaw > safeMaximum)
- return output.result(true);
- if (tableRaw < 8n || tableRaw >= BigInt(trailer)) return output.result(true);
+ return { literals: output.result(true), wideIntegers };
+ if (tableRaw < 8n || tableRaw >= BigInt(trailer))
+ return { literals: output.result(true), wideIntegers };
const offsetTable = Number(tableRaw);
const capacity = Math.floor((trailer - offsetTable) / offsetWidth);
- if (countRaw > BigInt(capacity)) return output.result(true);
+ if (countRaw > BigInt(capacity))
+ return { literals: output.result(true), wideIntegers };
const count = Number(countRaw);
+ const referenceWidth = bytes[trailer + 7] ?? 0;
+ if (![1, 2, 4, 8].includes(referenceWidth))
+ return { literals: output.result(true), wideIntegers };
const unsigned = (offset: number): number | undefined => {
if (offset < offsetTable || offset + offsetWidth > trailer)
return undefined;
@@ -150,23 +212,62 @@ export const binaryPlistNumberLiterals = (
const value = view.getBigUint64(offset);
return value > safeMaximum ? undefined : Number(value);
};
+ const objectOffsets: number[] = [];
for (let index = 0; index < count; index += 1) {
const offset = unsigned(offsetTable + index * offsetWidth);
if (offset === undefined || offset < 8 || offset >= offsetTable)
- return output.result(true);
+ return { literals: output.result(true), wideIntegers };
+ objectOffsets.push(offset);
+ }
+ const sortedOffsets = [...new Set(objectOffsets)].sort((a, b) => a - b);
+ for (let index = 0; index < sortedOffsets.length; index += 1) {
+ const offset = sortedOffsets[index];
+ if (offset === undefined) continue;
+ const nextOffset = sortedOffsets[index + 1] ?? offsetTable;
+ if (
+ binaryObjectEnd(bytes, offset, nextOffset, referenceWidth) === undefined
+ )
+ return { literals: output.result(true), wideIntegers: [] };
+ }
+ for (const offset of objectOffsets) {
const marker = bytes[offset] ?? 0;
const type = marker >> 4;
if (type !== 1 && type !== 2) continue;
const width = 2 ** (marker & 0xf);
- if (offset + 1 + width > offsetTable) return output.result(true);
+ if (offset + 1 + width > offsetTable)
+ return { literals: output.result(true), wideIntegers };
if (type === 1 && width === 8)
output.integer(view.getBigInt64(offset + 1).toString());
- else if (type === 1 && width > 4) return output.result(true);
+ else if (type === 1 && width === 16) {
+ const value = BigInt.asIntN(
+ 128,
+ (view.getBigUint64(offset + 1) << 64n) | view.getBigUint64(offset + 9),
+ );
+ output.integer(value.toString());
+ wideIntegers.push({ offset, value });
+ } else if (type === 1 && width > 4)
+ return { literals: output.result(true), wideIntegers };
else if (type === 2 && width === 4)
output.real(view.getFloat32(offset + 1));
else if (type === 2 && width === 8)
output.real(view.getFloat64(offset + 1));
- else if (type === 2) return output.result(true);
+ else if (type === 2) return { literals: output.result(true), wideIntegers };
}
- return output.result();
+ return { literals: output.result(), wideIntegers };
+};
+
+/** Preserve 16-byte integers through plist@5's Number-only parser. */
+export const decodeBinaryPlistNumbers = (
+ bytes: Buffer,
+ metadata: BinaryPlistNumberMetadata,
+): Buffer => {
+ if (metadata.wideIntegers.length === 0) return bytes;
+ const compatible = Buffer.from(bytes);
+ for (const { offset, value } of metadata.wideIntegers) {
+ // Convert only the decoder's input copy to a double. Exact integer metadata
+ // from the original bytes restores decimal text after the structural parse.
+ compatible[offset] = 0x23;
+ compatible.writeDoubleBE(Number(value), offset + 1);
+ }
+ return compatible;
};
diff --git a/tests/boundary/filesystem/keyedArchiveNumberMetadata.test.ts b/tests/boundary/filesystem/keyedArchiveNumberMetadata.test.ts
index 14034f8b3..54f54a4cf 100644
--- a/tests/boundary/filesystem/keyedArchiveNumberMetadata.test.ts
+++ b/tests/boundary/filesystem/keyedArchiveNumberMetadata.test.ts
@@ -1,7 +1,8 @@
-import { it } from "vitest";
+import { expect, it } from "vitest";
import { inspectBundleKeyedArchive } from "../../../src/artifacts/apple/KeyedArchiveReader.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
+import { keyedArchiveWideIntegerFixture } from "../../fixtures/keyedArchiveWideIntegers.js";
import {
archiveNumberMetadataCases,
expectKeyedArchiveNumberMetadata,
@@ -9,6 +10,71 @@ import {
} from "../../fixtures/keyedArchiveNumberMetadata.js";
import { cliTest } from "../../support/cli/cliFixture.js";
+it("preserves 16-byte binary integers and adjacent 64-bit integer metadata", async () => {
+ const fixture = await keyedArchiveWideIntegerFixture();
+ const result = await inspectBundleKeyedArchive({
+ bundlePath: fixture.root,
+ targetSha256: fixture.digest,
+ parameters: { path: "archive.bin" },
+ });
+ expect(result.archive_sha256).toBe(fixture.digest);
+ expect(result.objects).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ id: 1,
+ kind: "scalar",
+ status: "observed",
+ value: {
+ $plist_type: "integer",
+ decimal: "18446744073709551615",
+ },
+ }),
+ expect.objectContaining({
+ id: 2,
+ kind: "scalar",
+ status: "observed",
+ value: {
+ $plist_type: "integer",
+ decimal: "9223372036854775807",
+ },
+ }),
+ ]),
+ );
+ expect(result.limitations).not.toContainEqual(
+ expect.stringMatching(/metadata could not be completely observed/u),
+ );
+});
+
+cliTest(
+ "compiled CLI preserves 16-byte binary archive integers",
+ async ({ cli }) => {
+ const fixture = await keyedArchiveWideIntegerFixture();
+ const output = await cli.run({
+ arguments: ["inspect-keyed-archive", fixture.path, "--json"],
+ environment: { REA_LOG_LEVEL: "silent", REA_ANALYSIS_PROVIDER: "auto" },
+ });
+ expect(output.exitCode, output.stderr).toBe(0);
+ expect(parseEvidence(output.json).normalized_result).toMatchObject({
+ objects: expect.arrayContaining([
+ expect.objectContaining({
+ id: 1,
+ value: {
+ $plist_type: "integer",
+ decimal: "18446744073709551615",
+ },
+ }),
+ expect.objectContaining({
+ id: 2,
+ value: {
+ $plist_type: "integer",
+ decimal: "9223372036854775807",
+ },
+ }),
+ ]),
+ });
+ },
+);
+
it.each(archiveNumberMetadataCases)(
"filesystem preserves numeric meaning for $name",
async (item) => {
diff --git a/tests/fixtures/keyedArchiveWideIntegers.ts b/tests/fixtures/keyedArchiveWideIntegers.ts
new file mode 100644
index 000000000..4bf436248
--- /dev/null
+++ b/tests/fixtures/keyedArchiveWideIntegers.ts
@@ -0,0 +1,74 @@
+import { createHash } from "node:crypto";
+import { writeFile } from "node:fs/promises";
+import { join } from "node:path";
+
+import { createTestTempDirectory } from "./temporaryDirectory.js";
+
+const ascii = (value: string): Buffer => {
+ const bytes = Buffer.from(value, "ascii");
+ return bytes.length < 15
+ ? Buffer.concat([Buffer.from([0x50 | bytes.length]), bytes])
+ : Buffer.concat([Buffer.from([0x5f, 0x10, bytes.length]), bytes]);
+};
+
+const integer128 = (value: bigint): Buffer => {
+ const bytes = Buffer.alloc(17);
+ bytes[0] = 0x14;
+ const encoded = BigInt.asUintN(128, value);
+ bytes.writeBigUInt64BE(encoded >> 64n, 1);
+ bytes.writeBigUInt64BE(BigInt.asUintN(64, encoded), 9);
+ return bytes;
+};
+
+const integer64 = (value: bigint): Buffer => {
+ const bytes = Buffer.alloc(9);
+ bytes[0] = 0x13;
+ bytes.writeBigInt64BE(value, 1);
+ return bytes;
+};
+
+/** Minimal valid NSKeyedArchiver with a 16-byte integer and an Int64 control. */
+const archiveBytes = (): Buffer => {
+ const objects = [
+ Buffer.from([0xd3, 1, 3, 4, 2, 8, 5]), // root dict: three key/value pairs
+ ascii("$archiver"),
+ ascii("NSKeyedArchiver"),
+ ascii("$objects"),
+ ascii("$top"),
+ Buffer.from([0xd1, 6, 7]), // $top.root -> UID 1
+ ascii("root"),
+ Buffer.from([0x80, 1]), // UID 1
+ Buffer.from([0xa3, 9, 10, 11]), // $objects refs
+ ascii("$null"),
+ integer128(18_446_744_073_709_551_615n),
+ integer64(9_223_372_036_854_775_807n),
+ ];
+ const header = Buffer.from("bplist00", "ascii");
+ const offsets: number[] = [];
+ let position = header.length;
+ for (const object of objects) {
+ offsets.push(position);
+ position += object.length;
+ }
+ const offsetTable = Buffer.from(offsets);
+ const trailer = Buffer.alloc(32);
+ trailer[6] = 1;
+ trailer[7] = 1;
+ trailer.writeBigUInt64BE(BigInt(objects.length), 8);
+ trailer.writeBigUInt64BE(0n, 16);
+ trailer.writeBigUInt64BE(BigInt(position), 24);
+ return Buffer.concat([header, ...objects, offsetTable, trailer]);
+};
+
+export const keyedArchiveWideIntegerFixture = async () => {
+ const root = await createTestTempDirectory("rea-keyed-wide-integers-");
+ const path = join(root, "archive.bin");
+ const bytes = archiveBytes();
+ await writeFile(path, bytes);
+ return {
+ root,
+ path,
+ bytes,
+ digest: createHash("sha256").update(bytes).digest("hex"),
+ };
+};