Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(dotnet): reject unfinished CIL prefix chains (#4416)

Abdul Samad Siddiquica2ca1614623
diff --git a/src/dotnet/ManagedCilDecoding.test.ts b/src/dotnet/ManagedCilDecoding.test.ts
index 8214d0e85..ed49b5f71 100644
--- a/src/dotnet/ManagedCilDecoding.test.ts
+++ b/src/dotnet/ManagedCilDecoding.test.ts
@@ -334,6 +334,90 @@ describe("managed operand extent diagnostics", () => {
);
});
+describe("managed CIL prefix completion", () => {
+ it.each([
+ ["unaligned.", [0xfe, 0x12, 1], 1],
+ ["volatile.", [0xfe, 0x13], 1],
+ ["tail.", [0xfe, 0x14], 1],
+ ["constrained.", [0xfe, 0x16, 1, 0, 0, 2], 1],
+ ["no.", [0xfe, 0x19, 4], 1],
+ ["readonly.", [0xfe, 0x1e], 1],
+ ["unaligned. volatile. chain", [0xfe, 0x12, 1, 0xfe, 0x13], 2],
+ ] as const)(
+ "retains decoded evidence but rejects a terminal %s prefix",
+ (_label, prefix, prefixCount) => {
+ const il = Buffer.from([0x00, ...prefix]);
+ const { body, coverage } = tinyBody([...il]);
+ expect(body).toMatchObject({
+ status: "malformed",
+ il_size: il.length,
+ il_sha256: createHash("sha256").update(il).digest("hex"),
+ normalized_il_sha256: null,
+ decoded_instruction_count: 1 + prefixCount,
+ opcode_counts: { nop: 1 },
+ issue: "CIL prefix chain at IL offset 1 has no following instruction",
+ });
+ expect(coverage.state).toBe("partial");
+ },
+ );
+
+ it.each([
+ [[0xfe], "Two-byte CIL opcode at IL offset 2 leaves method body"],
+ [[0x20], "Instruction ldc.i4 at IL offset 2 leaves method body"],
+ [[0xfe, 0xff], "Unsupported CIL opcode 0xfeff at IL offset 2"],
+ ] as const)(
+ "keeps the original failure after a prefix followed by %j",
+ (suffix, issue) => {
+ const { body } = tinyBody([0xfe, 0x13, ...suffix]);
+ expect(body).toMatchObject({
+ status: "malformed",
+ decoded_instruction_count: 1,
+ normalized_il_sha256: null,
+ issue,
+ });
+ },
+ );
+
+ it("locates a dangling prefix after a completed chain", () => {
+ const { body, coverage } = tinyBody([
+ 0xfe, 0x12, 1, 0xfe, 0x13, 0x4a, 0xfe, 0x13,
+ ]);
+ expect(body).toMatchObject({
+ status: "malformed",
+ decoded_instruction_count: 4,
+ opcode_counts: { "unaligned.": 1, "volatile.": 2, "ldind.i4": 1 },
+ normalized_il_sha256: null,
+ issue: "CIL prefix chain at IL offset 6 has no following instruction",
+ });
+ expect(coverage.state).toBe("partial");
+ });
+
+ it("rejects a fat method ending in a prefix without a normalized fingerprint", () => {
+ expect(fatBody(Buffer.from([0xfe, 0x13]))).toMatchObject({
+ status: "malformed",
+ header_format: "fat",
+ il_size: 2,
+ decoded_instruction_count: 1,
+ opcode_counts: { "volatile.": 1 },
+ normalized_il_sha256: null,
+ issue: "CIL prefix chain at IL offset 0 has no following instruction",
+ });
+ });
+
+ it("keeps a completed prefix chain present with its fingerprint", () => {
+ const { body, coverage } = tinyBody([
+ 0xfe, 0x12, 1, 0xfe, 0x13, 0x4a, 0x2a,
+ ]);
+ expect(body).toMatchObject({
+ status: "present",
+ decoded_instruction_count: 4,
+ normalized_il_sha256: expect.any(String),
+ issue: null,
+ });
+ expect(coverage.state).toBe("complete");
+ });
+});
+
describe("managed complete operand decoding", () => {
// The extent check must also reject nothing that fits, so every multi-byte
// width is exercised at exactly its own size with a known value. Each body
diff --git a/src/dotnet/ManagedMemberInstructionDecoder.ts b/src/dotnet/ManagedMemberInstructionDecoder.ts
index 7fbe8810b..80c3ae908 100644
--- a/src/dotnet/ManagedMemberInstructionDecoder.ts
+++ b/src/dotnet/ManagedMemberInstructionDecoder.ts
@@ -496,6 +496,8 @@ export const decodeInstructions = (
issue =
cause instanceof Error ? cause.message : "Instruction decode failed";
}
+ if (issue === null && prefixStart !== null)
+ issue = `CIL prefix chain at IL offset ${String(prefixStart)} has no following instruction`;
const truncated = offset < il.length && issue === null ? 1 : 0;
return { parsed, instructionStarts, count: parsed.length, truncated, issue };
};
diff --git a/tests/acceptance/managedCliOutput.test.ts b/tests/acceptance/managedCliOutput.test.ts
index 5e164c0cf..ed96e5eab 100644
--- a/tests/acceptance/managedCliOutput.test.ts
+++ b/tests/acceptance/managedCliOutput.test.ts
@@ -7,6 +7,51 @@ import { buildManagedPeFixture } from "../../src/dotnet/ManagedPe.fixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
+cliTest(
+ "managed CLI preserves malformed prefix evidence without a complete CIL fingerprint",
+ async ({ cli }) => {
+ const root = await createTestTempDirectory("rea-managed-cil-prefix-");
+ for (const [name, il, status, coverage] of [
+ ["dangling", [0xfe, 0x13], "malformed", "partial"],
+ ["complete", [0x00, 0x2a], "present", "complete"],
+ ] as const) {
+ const path = join(root, `${name}.dll`);
+ await writeFile(
+ path,
+ buildManagedPeFixture({
+ ilBody: Buffer.from([(il.length << 2) | 2, ...il]),
+ }),
+ );
+ const result = await cli.run({
+ arguments: ["inspect-managed-members", path, "--json"],
+ });
+ expect(result.exitCode).toBe(0);
+ expect(result.json).toMatchObject({
+ operation: "inspect_managed_members",
+ subject: { local_path: path },
+ normalized_result: {
+ coverage: { state: coverage },
+ user_strings: { state: coverage },
+ methods: [
+ expect.objectContaining({
+ body: expect.objectContaining({
+ status,
+ il_sha256: expect.any(String),
+ normalized_il_sha256:
+ status === "malformed" ? null : expect.any(String),
+ issue:
+ status === "malformed"
+ ? "CIL prefix chain at IL offset 0 has no following instruction"
+ : null,
+ }),
+ }),
+ ],
+ },
+ });
+ }
+ },
+);
+
cliTest(
"managed CLI retains metadata and CIL after repeated local constructor owner lookups",
async ({ cli }) => {