Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(artifacts): flag truncated Mach-O headers and keep canonicalization denials

Kaoru08229a487a4e0057
diff --git a/src/artifacts/apple/DylibResolutionReader.ts b/src/artifacts/apple/DylibResolutionReader.ts
index b18c369b0..812e14715 100644
--- a/src/artifacts/apple/DylibResolutionReader.ts
+++ b/src/artifacts/apple/DylibResolutionReader.ts
@@ -90,12 +90,15 @@ export const traceDylibResolution = async (options: {
"path",
"roots must be normalized paths relative to the analyzed root",
);
- const root = await realpath(options.rootPath);
- const target = relative(root, await realpath(options.targetPath))
- .split(sep)
- .join("/");
- const view = new FilesystemTreeView(root, options.signal);
+ // Canonicalization is inside the translated region: a revoked permission
+ // keeps its reason and path.
+ let root = options.rootPath;
try {
+ root = await realpath(options.rootPath);
+ const target = relative(root, await realpath(options.targetPath))
+ .split(sep)
+ .join("/");
+ const view = new FilesystemTreeView(root, options.signal);
const { roots: requested, unclassified } =
parsed.data.roots !== undefined
? { roots: parsed.data.roots, unclassified: [] }
@@ -154,7 +157,7 @@ export const traceDylibResolution = async (options: {
if (denied !== undefined)
throw new ArtifactReaderFailure(
"unavailable",
- `Permission denied (${denied.code}) reading ${denied.path === undefined ? "a file in the analyzed root" : relative(root, denied.path) || denied.path}`,
+ `Permission denied (${denied.code}) reading ${deniedPath(root, denied.path)}`,
{ cause },
);
throw cause;
@@ -329,6 +332,13 @@ const cancelled = (signal?: AbortSignal): void => {
);
};
+/** A denied path relative to the analyzed root, or absolute when outside it. */
+const deniedPath = (root: string, path: string | undefined): string => {
+ if (path === undefined) return "a file in the analyzed root";
+ const inside = relative(root, path);
+ return inside === "" || inside.startsWith("..") ? path : inside;
+};
+
/** Host permission denials, kept distinct from malformed or missing files. */
const permissionDenied = (
cause: unknown,
diff --git a/src/artifacts/apple/MachoLoadCommandReader.test.ts b/src/artifacts/apple/MachoLoadCommandReader.test.ts
index 46c4138cc..892312ea0 100644
--- a/src/artifacts/apple/MachoLoadCommandReader.test.ts
+++ b/src/artifacts/apple/MachoLoadCommandReader.test.ts
@@ -183,6 +183,10 @@ describe("Mach-O load command reader inputs", () => {
expect(await read(Uint8Array.from([0xcf, 0xfa]))).toEqual({
status: "not-mach-o",
});
+ // A recognized magic without room for a header is a truncated image.
+ expect(
+ await read(Uint8Array.from([0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00])),
+ ).toEqual({ status: "malformed", reason: "Mach-O header is truncated" });
});
});
diff --git a/src/artifacts/apple/MachoLoadCommandReader.ts b/src/artifacts/apple/MachoLoadCommandReader.ts
index 1eb4fccb7..b78ce7fbc 100644
--- a/src/artifacts/apple/MachoLoadCommandReader.ts
+++ b/src/artifacts/apple/MachoLoadCommandReader.ts
@@ -88,7 +88,11 @@ export const readMachoImage = async (
): Promise<MachoImageFacts> => {
try {
const head = await readAt(0, Math.min(size, 8));
- if (head.byteLength < 8) return { status: "not-mach-o" };
+ if (head.byteLength < 8)
+ // A recognized magic with no room for a header is a truncated image.
+ return hasMachoMagic(head)
+ ? { status: "malformed", reason: "Mach-O header is truncated" }
+ : { status: "not-mach-o" };
const view = viewOf(head);
const magic = view.getUint32(0, false);
if ([FAT_MAGIC, FAT_MAGIC_64, FAT_CIGAM, FAT_CIGAM_64].includes(magic)) {
diff --git a/tests/boundary/mcp/dylibResolutionMcp.test.ts b/tests/boundary/mcp/dylibResolutionMcp.test.ts
index 2a2c18f06..179912a18 100644
--- a/tests/boundary/mcp/dylibResolutionMcp.test.ts
+++ b/tests/boundary/mcp/dylibResolutionMcp.test.ts
@@ -1,4 +1,4 @@
-import { mkdir, writeFile } from "node:fs/promises";
+import { chmod, mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
@@ -159,3 +159,37 @@ it("traces a standalone Mach-O file whose name ends in .app", async () => {
]);
});
});
+
+it.skipIf(process.getuid?.() === 0)(
+ "keeps a permission denied while canonicalizing the opened bundle",
+ async () => {
+ const parent = await createTestTempDirectory("rea-dylib-mcp-revoked-");
+ const app = join(parent, "Revoked.app");
+ await mkdir(join(app, "Contents/MacOS"), { recursive: true });
+ await writeFile(
+ join(app, "Contents/Info.plist"),
+ "<plist><dict><key>CFBundleExecutable</key><string>App</string></dict></plist>",
+ );
+ await writeFile(join(app, "Contents/MacOS/App"), machoImage({}));
+ await withClient(async (client) => {
+ const opened = await client.callTool({
+ name: "open_binary",
+ arguments: { path: app },
+ });
+ expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
+ await chmod(parent, 0o000);
+ try {
+ const called = await client.callTool({
+ name: "trace_dylib_resolution",
+ arguments: {},
+ });
+ expect(called.isError).toBe(true);
+ expect(JSON.stringify(called.structuredContent)).toMatch(
+ /Permission denied \(EACCES\)/u,
+ );
+ } finally {
+ await chmod(parent, 0o755);
+ }
+ });
+ },
+);