Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(native): preserve Mach-O operand boundaries and integer limits
morlutofb51960d7daa
diff --git a/src/native/AppleDispatchMetadata.test.ts b/src/native/AppleDispatchMetadata.test.tsindex 87284200b..d68b1219a 100644--- a/src/native/AppleDispatchMetadata.test.ts+++ b/src/native/AppleDispatchMetadata.test.ts@@ -163,37 +163,47 @@ const partialSiblingsFixture = (};describe("Apple dispatch pointer fixup metadata", () => {- it("keeps decoded bindings and their coverage when an independent stream fails", () => {- const bytes = fixture();- bytes.writeUInt32LE(2, 16);- bytes.writeUInt32LE(200, 20);- bytes.writeUInt32LE(0x80000022, 184);- bytes.writeUInt32LE(48, 188);- const bindings = Buffer.from([- 0x40,- ...Buffer.from("_OBJC_CLASS_$_NSObject\0"),- 0x70,- 0x88,- 0x04,- 0x90,- 0x00,- ]);- bindings.copy(bytes, 0xc00);- bytes[0xc80] = 0xd0;- bytes.writeUInt32LE(0xc00, 200);- bytes.writeUInt32LE(bindings.length, 204);- bytes.writeUInt32LE(0xc80, 208);- bytes.writeUInt32LE(1, 212);+ it.each([+ { name: "unsupported opcode", operand: [0xd0] },+ {+ name: "oversized addend whose tail resembles DO_BIND",+ operand: [0x60, ...Array<number>(10).fill(0x80), 0x90, 0x00],+ },+ ])(+ "keeps decoded bindings when an independent stream has $name",+ ({ operand }) => {+ const bytes = fixture();+ bytes.writeUInt32LE(2, 16);+ bytes.writeUInt32LE(200, 20);+ bytes.writeUInt32LE(0x80000022, 184);+ bytes.writeUInt32LE(48, 188);+ const bindings = Buffer.from([+ 0x40,+ ...Buffer.from("_OBJC_CLASS_$_NSObject\0"),+ 0x70,+ 0x88,+ 0x04,+ 0x90,+ 0x00,+ ]);+ bindings.copy(bytes, 0xc00);+ const invalid = Buffer.from([...bindings.subarray(0, -2), ...operand]);+ invalid.copy(bytes, 0xc80);+ bytes.writeUInt32LE(0xc00, 200);+ bytes.writeUInt32LE(bindings.length, 204);+ bytes.writeUInt32LE(0xc80, 208);+ bytes.writeUInt32LE(invalid.length, 212);- const result = decodeAppleDispatchMetadata(bytes, 100, provenance);- expect(result.objc_classes[0]).toMatchObject({- name: "Fixture",- super_class: "NSObject",- });- expect(- result.coverage.find(({ facet }) => facet === "pointer_fixups"),- ).toMatchObject({ status: "partial", examined: 2, decoded: 1 });- });+ const result = decodeAppleDispatchMetadata(bytes, 100, provenance);+ expect(result.objc_classes[0]).toMatchObject({+ name: "Fixture",+ super_class: "NSObject",+ });+ expect(+ result.coverage.find(({ facet }) => facet === "pointer_fixups"),+ ).toMatchObject({ status: "partial", examined: 2, decoded: 1 });+ },+ );it("keeps absolute metadata pointers between declared chained fixups", () => {const bytes = fixture();diff --git a/src/native/AppleMachoFixups.test.ts b/src/native/AppleMachoFixups.test.tsindex f3f84a76c..4b6af1dd4 100644--- a/src/native/AppleMachoFixups.test.ts+++ b/src/native/AppleMachoFixups.test.ts@@ -950,6 +950,158 @@ describe("legacy pointer fixup streams", () => {});});+describe("legacy bind operand decoding", () => {+ const addendStream = (addend: readonly number[]) =>+ Buffer.from([+ 0x11, // SET_DYLIB_ORDINAL_IMM 1+ 0x40,+ ...Buffer.from("_x\0"),+ 0x71,+ 0x08, // SET_SEGMENT_AND_OFFSET segment 1 + 8+ 0x60,+ ...addend, // SET_ADDEND_SLEB+ 0x00,+ ]);+ const parseBinds = (opcodes: Buffer) =>+ parsePointerFixups(+ opcodes,+ {+ chained: null,+ binds: [{ offset: 0, size: opcodes.length, stream: "bind" }],+ rebases: [],+ dylibs: ["/usr/lib/libSystem.B.dylib"],+ },+ SEGMENTS,+ BASE,+ );++ it.each([+ { addend: 0n, sleb: [0x00] },+ { addend: -1n, sleb: [0x7f] },+ { addend: -8n, sleb: [0x78] },+ { addend: 128n, sleb: [0x80, 0x01] },+ { addend: -129n, sleb: [0xff, 0x7e] },+ // Ten-byte operands at the signed 64-bit limits are still valid.+ { addend: -(2n ** 63n), sleb: [...Array<number>(9).fill(0x80), 0x7f] },+ { addend: 2n ** 63n - 1n, sleb: [...Array<number>(9).fill(0xff), 0x00] },+ // Redundant sign extension still belongs to the operand, not the opcode stream.+ { addend: 0n, sleb: [...Array<number>(11).fill(0x80), 0x00] },+ { addend: -1n, sleb: [...Array<number>(11).fill(0xff), 0x7f] },+ ])(+ "applies a SET_ADDEND_SLEB addend of $addend to the following bind",+ ({ addend, sleb }) => {+ const opcodes = addendStream(sleb);+ opcodes[opcodes.length - 1] = 0x90; // DO_BIND+ const fixups = parseBinds(Buffer.concat([opcodes, Buffer.from([0x00])]));+ expect(fixups.failures).toEqual([]);+ expect(fixups.decode(BASE + 0x4008n, 0n)).toMatchObject({+ kind: "bind",+ symbol: "_x",+ addend,+ });+ },+ );++ it.each([+ // Ten continuation bytes, then 0x90. dyld keeps reading 0x90 as part of+ // the operand (bit 7 is set) and binds nothing; it must not become DO_BIND.+ {+ name: "opcode-like tail",+ operand: [...Array<number>(10).fill(0x80), 0x90, 0x00],+ },+ {+ name: "positive signed overflow",+ operand: [...Array<number>(9).fill(0x80), 0x01],+ },+ {+ name: "negative signed overflow",+ operand: [...Array<number>(9).fill(0xff), 0x7e],+ },+ {+ name: "nonzero zero-extension",+ operand: [...Array<number>(10).fill(0x80), 0x01],+ },+ {+ name: "nonnegative sign-extension",+ operand: [...Array<number>(10).fill(0xff), 0x00],+ },+ ])("rejects an oversized SET_ADDEND_SLEB with $name", ({ operand }) => {+ const opcodes = addendStream(operand);+ const fixups = parseBinds(opcodes);+ expect(fixups).toMatchObject({ examined: 1, decoded: 0 });+ expect(fixups.failures.join("\n")).toContain(+ "Oversized bind opcode operand",+ );+ expect(fixups.decode(BASE + 0x4008n, 0x99n)).not.toMatchObject({+ kind: "bind",+ });+ });++ it("does not read beyond the declared stream to terminate an addend", () => {+ const opcodes = addendStream([0x80]);+ const fixups = parsePointerFixups(+ opcodes,+ {+ chained: null,+ binds: [{ offset: 0, size: opcodes.length - 1, stream: "bind" }],+ rebases: [],+ dylibs: [],+ },+ SEGMENTS,+ BASE,+ );+ expect(fixups).toMatchObject({ examined: 1, decoded: 0 });+ expect(fixups.failures).toEqual([+ "bind bind opcodes: Truncated bind opcode operand",+ ]);+ });++ const addressStream = (operand: readonly number[]) =>+ Buffer.from([+ 0x40,+ ...Buffer.from("_x\0"),+ 0x71,+ 0x08, // SET_SEGMENT_AND_OFFSET segment 1 + 8+ 0x80,+ ...operand, // ADD_ADDR_ULEB+ 0x90,+ 0x00, // DO_BIND, DONE+ ]);++ it.each([+ { operand: [...Array<number>(9).fill(0xff), 0x01], offset: 0x4007n },+ { operand: [...Array<number>(11).fill(0x80), 0x00], offset: 0x4008n },+ ])(+ "preserves unsigned 64-bit address deltas and zero padding: $offset",+ ({ operand, offset }) => {+ const fixups = parseBinds(addressStream(operand));+ expect(fixups.failures).toEqual([]);+ expect(fixups.decode(BASE + offset, 0n)).toMatchObject({+ kind: "bind",+ symbol: "_x",+ });+ },+ );++ it.each([+ {+ name: "tenth-byte overflow",+ operand: [...Array<number>(9).fill(0x80), 0x02],+ },+ {+ name: "nonzero zero-extension",+ operand: [...Array<number>(10).fill(0x80), 0x01],+ },+ ])("rejects unsigned address deltas with $name", ({ operand }) => {+ const fixups = parseBinds(addressStream(operand));+ expect(fixups).toMatchObject({ examined: 1, decoded: 0 });+ expect(fixups.failures).toEqual([+ "bind bind opcodes: Oversized bind opcode operand",+ ]);+ expect(fixups.decode(BASE + 0x4008n, 0n).kind).not.toBe("bind");+ });+});+describe("Objective-C property attributes", () => {it("parses type, ownership, accessors and quoted class names", () => {expect(diff --git a/src/native/AppleMachoFixups.ts b/src/native/AppleMachoFixups.tsindex bafb676f8..f7eaa9fb2 100644--- a/src/native/AppleMachoFixups.ts+++ b/src/native/AppleMachoFixups.ts@@ -510,28 +510,40 @@ const chainedFixups = (const uleb = (bytes: Buffer, cursor: { at: number }, end: number): bigint => {let value = 0n;- let shift = 0n;+ let shift = 0;for (;;) {if (cursor.at >= end) throw new RangeError("Truncated bind opcode operand");const byte = bytes[cursor.at++] ?? 0;- value |= BigInt(byte & 0x7f) << shift;- shift += 7n;+ const slice = byte & 0x7f;+ if ((shift === 63 && slice > 1) || (shift === 64 && slice !== 0))+ throw new RangeError("Oversized bind opcode operand");+ if (shift < 64) value |= BigInt(slice) << BigInt(shift);+ // Padding may extend the encoding, but never the represented integer.+ shift = Math.min(64, shift + 7);if ((byte & 0x80) === 0) return value;- if (shift > 63n) throw new RangeError("Oversized bind opcode operand");}};const sleb = (bytes: Buffer, cursor: { at: number }, end: number): bigint => {let value = 0n;- let shift = 0n;+ let shift = 0;let byte = 0;do {if (cursor.at >= end) throw new RangeError("Truncated bind opcode operand");byte = bytes[cursor.at++] ?? 0;- value |= BigInt(byte & 0x7f) << shift;- shift += 7n;- } while ((byte & 0x80) !== 0 && shift <= 63n);- return (byte & 0x40) !== 0 ? value - (1n << shift) : value;+ const slice = byte & 0x7f;+ if (+ (shift === 63 && slice !== 0 && slice !== 0x7f) ||+ (shift === 64 && slice !== (value < 0n ? 0x7f : 0))+ )+ throw new RangeError("Oversized bind opcode operand");+ if (shift < 64)+ value = BigInt.asIntN(64, value | (BigInt(slice) << BigInt(shift)));+ shift = Math.min(64, shift + 7);+ } while ((byte & 0x80) !== 0);+ return shift < 64 && (byte & 0x40) !== 0+ ? value - (1n << BigInt(shift))+ : value;};/** Registers of the `LC_DYLD_INFO` bind state machine. */