diff --git a/src/artifacts/ArtifactProvider.ts b/src/artifacts/ArtifactProvider.ts index 050ff1b7c..2041a89e9 100644 --- a/src/artifacts/ArtifactProvider.ts +++ b/src/artifacts/ArtifactProvider.ts @@ -246,9 +246,12 @@ class ArtifactClient implements AnalysisClient { "unavailable", "trace_dylib_resolution requires an active Mach-O or .app bundle target", ); - const bundle = this.target.sourcePath?.toLowerCase().endsWith(".app") - ? this.target.sourcePath - : undefined; + // Only a target opened from an app bundle directory carries its Info.plist; + // a regular file whose name ends in .app is a standalone image. + const bundle = + this.target.bundleInfoPlist === undefined + ? undefined + : this.target.sourcePath; const result = await traceDylibResolution({ rootPath: bundle ?? dirname(this.target.path), targetPath: this.target.path, diff --git a/src/artifacts/MachoLoadCommandReader.test.ts b/src/artifacts/MachoLoadCommandReader.test.ts index e215e9fd6..c3afb780b 100644 --- a/src/artifacts/MachoLoadCommandReader.test.ts +++ b/src/artifacts/MachoLoadCommandReader.test.ts @@ -237,6 +237,15 @@ describe("Mach-O load command reader failures", () => { })); const facts = await read(fatImage(slices)); expect(facts.status === "parsed" && facts.slices).toHaveLength(24); + // An empty table is not a Java class file, whose major version is nonzero. + for (const magic of [ + [0xca, 0xfe, 0xba, 0xbe], + [0xbf, 0xba, 0xfe, 0xca], + ]) + expect(await read(Uint8Array.from([...magic, 0, 0, 0, 0]))).toEqual({ + status: "malformed", + reason: "FAT header declares no architectures", + }); }); it("rejects string commands too short to hold their string offset", async () => { diff --git a/src/artifacts/MachoLoadCommandReader.ts b/src/artifacts/MachoLoadCommandReader.ts index d5c79bd23..a7ce1fd02 100644 --- a/src/artifacts/MachoLoadCommandReader.ts +++ b/src/artifacts/MachoLoadCommandReader.ts @@ -96,11 +96,14 @@ export const readMachoImage = async ( const littleEndian = magic === FAT_CIGAM || magic === FAT_CIGAM_64; const count = view.getUint32(4, littleEndian); const wide = magic === FAT_MAGIC_64 || magic === FAT_CIGAM_64; - if ( - count === 0 || - count > MAX_FAT_ARCHITECTURES || - 8 + count * (wide ? 32 : 20) > size - ) + // A Java class file's nonzero major version reads as the count, so an + // empty architecture table can only be a malformed universal header. + if (count === 0) + return { + status: "malformed", + reason: "FAT header declares no architectures", + }; + if (count > MAX_FAT_ARCHITECTURES || 8 + count * (wide ? 32 : 20) > size) return { status: "not-mach-o" }; const table = viewOf( await readExact(readAt, 8, count * (wide ? 32 : 20), "FAT table"), diff --git a/src/domain/dylibResolutionFindings.ts b/src/domain/dylibResolutionFindings.ts index f6972232e..d6b8dd81d 100644 --- a/src/domain/dylibResolutionFindings.ts +++ b/src/domain/dylibResolutionFindings.ts @@ -84,4 +84,5 @@ export const DYLIB_RESOLUTION_LIMITATIONS = [ "Load order follows dyld's dependents-first traversal in load-command order. An image reached through several chains is resolved once per process, with the rpath stack of the first chain; a later request whose install name matches an already loaded image reuses it.", "@loader_path uses each image's symlink-resolved path within the analyzed root.", "Code-signing checks that can reject a found image, such as library validation and the hardened runtime, are not evaluated.", + "Dylib compatibility versions are not compared. Apple's linker documentation describes a load-time rejection when an image's compatibility version is older than the loader requires, but dyld on macOS 26 was observed to load such an image.", ]; diff --git a/tests/boundary/mcp/dylibResolutionMcp.test.ts b/tests/boundary/mcp/dylibResolutionMcp.test.ts index a4bd9fdda..b0639047e 100644 --- a/tests/boundary/mcp/dylibResolutionMcp.test.ts +++ b/tests/boundary/mcp/dylibResolutionMcp.test.ts @@ -123,3 +123,39 @@ it("rejects targets that are not Mach-O", async () => { expect(JSON.stringify(called.structuredContent)).toContain("unavailable"); }); }); + +it("traces a standalone Mach-O file whose name ends in .app", async () => { + const directory = await createTestTempDirectory("rea-dylib-mcp-file-"); + await writeFile( + join(directory, "Tool.app"), + machoImage({ + commands: [dylibCommand(LC.LOAD_DYLIB, "@loader_path/libcore.dylib")], + }), + ); + await writeFile( + join(directory, "libcore.dylib"), + machoImage({ fileType: FILE_TYPE.dylib }), + ); + await withClient(async (client) => { + const opened = await client.callTool({ + name: "open_binary", + arguments: { path: join(directory, "Tool.app") }, + }); + expect(opened.isError, JSON.stringify(opened)).not.toBe(true); + const called = await client.callTool({ + name: "trace_dylib_resolution", + arguments: {}, + }); + expect(called.isError, JSON.stringify(called.structuredContent)).not.toBe( + true, + ); + const trace = dylibResolutionResultSchema.parse( + structuredResult(called.structuredContent), + ); + expect(trace.edges).toEqual([ + expect.objectContaining({ + resolution: { status: "resolved", image: "libcore.dylib" }, + }), + ]); + }); +});