Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(ghidra): reject byte matches outside searched coverage (#4531)

ShuaixinHuanga04053347034
diff --git a/src/domain/native/bytePattern.test.ts b/src/domain/native/bytePattern.test.ts
index cc3b6dd9a..9d2cc92c1 100644
--- a/src/domain/native/bytePattern.test.ts
+++ b/src/domain/native/bytePattern.test.ts
@@ -1,5 +1,55 @@
import { describe, expect, it } from "vitest";
-import { parseBytePattern } from "./bytePattern.js";
+import { parseBytePattern, byteSearchResultSchema } from "./bytePattern.js";
+
+it.each(["0x20", "overlay:0x10", "invalid"])(
+ "rejects a match outside reported searched coverage: %s",
+ (address) => {
+ const page = {
+ matches: [
+ { address, segment: ".text", file_offset: null, procedure: null },
+ ],
+ searched: {
+ blocks: [{ segment: ".text", start: "0x10", end: "0x1f", bytes: 16 }],
+ bytes: 16,
+ skipped_uninitialized: [],
+ skipped_not_loaded: [],
+ },
+ complete: true,
+ next_cursor: null,
+ };
+ expect(byteSearchResultSchema.safeParse(page).success).toBe(false);
+ page.matches[0]!.address = "0x10";
+ expect(byteSearchResultSchema.safeParse(page).success).toBe(true);
+ page.matches[0]!.segment = ".bss";
+ expect(byteSearchResultSchema.safeParse(page).success).toBe(false);
+ },
+);
+
+it.each(["", "overlay:"])(
+ "accepts matches at both inclusive coverage boundaries in address space %s",
+ (space) => {
+ const start = `${space}0x10`;
+ const end = `${space}0x1f`;
+ expect(
+ byteSearchResultSchema.safeParse({
+ matches: [start, end].map((address) => ({
+ address,
+ segment: ".text",
+ file_offset: null,
+ procedure: null,
+ })),
+ searched: {
+ blocks: [{ segment: ".text", start, end, bytes: 16 }],
+ bytes: 16,
+ skipped_uninitialized: [],
+ skipped_not_loaded: [],
+ },
+ complete: true,
+ next_cursor: null,
+ }).success,
+ ).toBe(true);
+ },
+);
describe("masked byte patterns", () => {
it("preserves whole-byte wildcards and combines explicit bit masks", () => {
diff --git a/src/domain/native/bytePattern.ts b/src/domain/native/bytePattern.ts
index add5bb7ef..118102dcf 100644
--- a/src/domain/native/bytePattern.ts
+++ b/src/domain/native/bytePattern.ts
@@ -122,4 +122,31 @@ export const byteSearchResultSchema = z
code: "custom",
message: "Byte search coverage and continuation disagree",
});
+ const ranges = value.searched.blocks.map((block) => ({
+ segment: block.segment,
+ start: addressCoordinate(block.start),
+ end: addressCoordinate(block.end),
+ }));
+ for (const [index, match] of value.matches.entries()) {
+ const address = addressCoordinate(match.address);
+ if (
+ address === null ||
+ !ranges.some(
+ (range) =>
+ range.segment === match.segment &&
+ range.start !== null &&
+ range.end !== null &&
+ range.start.space === address.space &&
+ range.end.space === address.space &&
+ range.start.offset <= address.offset &&
+ address.offset <= range.end.offset,
+ )
+ )
+ context.addIssue({
+ code: "custom",
+ path: ["matches", index, "address"],
+ message:
+ "Byte match must lie within reported searched coverage in the same segment and address space",
+ });
+ }
});
diff --git a/tests/boundary/mcp/ghidraInventoryMcp.test.ts b/tests/boundary/mcp/ghidraInventoryMcp.test.ts
index 14894ba7d..b269087ac 100644
--- a/tests/boundary/mcp/ghidraInventoryMcp.test.ts
+++ b/tests/boundary/mcp/ghidraInventoryMcp.test.ts
@@ -106,6 +106,24 @@ it("retains byte-search coverage and rejects malformed pages before creating Evi
});
expect(invalidCoverage.structuredContent).toBeUndefined();
}
+ for (const [address, segment] of [
+ ["0x402000", ".bss"],
+ ["overlay:0x401000", ".text"],
+ ["invalid", ".text"],
+ ]) {
+ output = jsonValueSchema.parse({
+ ...page,
+ matches: [{ ...page.matches[0], address, segment }],
+ });
+ const outsideCoverage = await harness.mcp.callTool({
+ name: "search_bytes",
+ arguments: { pattern: "48 ??" },
+ });
+ expect(parseMcpToolError(outsideCoverage)).toMatchObject({
+ error: { code: "unreadable_output" },
+ });
+ expect(outsideCoverage.structuredContent).toBeUndefined();
+ }
const calls = harness.calls.length;
for (const args of [{ pattern: "??" }, { pattern: "48", mask: "ffff" }]) {
const invalid = await harness.mcp.callTool({