From bede3e3c60e53d445209096b360a81eaa0c446cc Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Tue, 6 Oct 2026 02:20:57 +0800 Subject: [PATCH] fix(native): decode little-endian fat headers Fixes #592 --- src/native/AppleDispatchMetadata.test.ts | 70 ++++++++++++++++++++++++ src/native/AppleDispatchMetadata.ts | 41 +++++++++----- 2 files changed, 98 insertions(+), 13 deletions(-) diff --git a/src/native/AppleDispatchMetadata.test.ts b/src/native/AppleDispatchMetadata.test.ts index aa3156209..37219831a 100644 --- a/src/native/AppleDispatchMetadata.test.ts +++ b/src/native/AppleDispatchMetadata.test.ts @@ -191,3 +191,73 @@ describe("FAT64 dispatch slice validation", () => { ).toThrow("Requested FAT architecture is absent"); }); }); + +describe("byte-swapped universal Apple dispatch metadata", () => { + const wrapped = (fat64: boolean) => { + const thin = fixture(); + const offset = 256; + const bytes = Buffer.alloc(offset + thin.length); + bytes.writeUInt32LE(fat64 ? 0xcafebabf : 0xcafebabe, 0); + bytes.writeUInt32LE(1, 4); + bytes.writeUInt32LE(0x0100000c, 8); + if (fat64) { + bytes.writeBigUInt64LE(BigInt(offset), 16); + bytes.writeBigUInt64LE(BigInt(thin.length), 24); + } else { + bytes.writeUInt32LE(offset, 16); + bytes.writeUInt32LE(thin.length, 20); + } + thin.copy(bytes, offset); + return bytes; + }; + + it.each([false, true])( + "decodes selected little-endian FAT slices (fat64=%s)", + (fat64) => { + const result = decodeAppleDispatchMetadata( + wrapped(fat64), + 100, + provenance, + ); + expect(result.objc_classes[0]).toMatchObject({ + name: "Fixture", + location: { address: "0x100000200", file_offset: 256 + 512 }, + }); + expect(result.objc_dispatch_implementations[0]).toMatchObject({ + implementation_address: "0x100000700", + location: { file_offset: 256 + 0x488 }, + }); + }, + ); + + it.each([6, 16])( + "states the header byte order when its architecture table is truncated at %i bytes", + (length) => { + expect(() => + decodeAppleDispatchMetadata( + wrapped(false).subarray(0, length), + 100, + provenance, + ), + ).toThrow( + "Malformed FAT architecture table (FAT header byte order: little-endian)", + ); + }, + ); + + it("validates the selected thin slice's own byte order", () => { + const bytes = wrapped(false); + bytes.writeUInt32BE(0xfeedfacf, 256); + expect(() => decodeAppleDispatchMetadata(bytes, 100, provenance)).toThrow( + "Only little-endian 64-bit Mach-O metadata is supported", + ); + }); + + it("checks the full little-endian FAT64 slice extent", () => { + const bytes = wrapped(true); + bytes.writeBigUInt64LE(0x100000000n, 16); + expect(() => decodeAppleDispatchMetadata(bytes, 100, provenance)).toThrow( + "FAT slice exceeds file (FAT header byte order: little-endian)", + ); + }); +}); diff --git a/src/native/AppleDispatchMetadata.ts b/src/native/AppleDispatchMetadata.ts index 814e107bc..91a2aa8e6 100644 --- a/src/native/AppleDispatchMetadata.ts +++ b/src/native/AppleDispatchMetadata.ts @@ -35,8 +35,10 @@ export const decodeAppleDispatchMetadata = ( provenance: { path: string; sha256: string }, architecture = "arm64", ): ObjcSwiftMetadata => { - if (bytes.length < 32) throw new RangeError("Truncated Mach-O header"); + if (bytes.length < 4) throw new RangeError("Truncated Mach-O header"); const { slice, sliceEnd } = selectMachoSlice(bytes, architecture); + if (slice === 0 && sliceEnd === bytes.length && bytes.length < 32) + throw new RangeError("Truncated Mach-O header"); if (slice + 32 > sliceEnd || bytes.readUInt32LE(slice) !== 0xfeedfacf) throw new TypeError( "Only little-endian 64-bit Mach-O metadata is supported", @@ -658,32 +660,45 @@ export const inspectAppleDispatchMetadata = async ( const selectMachoSlice = (bytes: Buffer, architecture: string) => { const magic = bytes.readUInt32BE(0); - if (magic !== 0xcafebabe && magic !== 0xcafebabf) + const littleEndian = magic === 0xbebafeca || magic === 0xbfbafeca; + if (magic !== 0xcafebabe && magic !== 0xcafebabf && !littleEndian) return { slice: 0, sliceEnd: bytes.length }; - const fat64 = magic === 0xcafebabf; + const fat64 = magic === 0xcafebabf || magic === 0xbfbafeca; const stride = fat64 ? 32 : 20; - const count = bytes.readUInt32BE(4); + const readUInt32 = littleEndian + ? (offset: number) => bytes.readUInt32LE(offset) + : (offset: number) => bytes.readUInt32BE(offset); + const readUInt64 = littleEndian + ? (offset: number) => bytes.readBigUInt64LE(offset) + : (offset: number) => bytes.readBigUInt64BE(offset); + const byteOrder = littleEndian ? "little-endian" : "big-endian"; + const malformed = (reason: string) => + new RangeError(`${reason} (FAT header byte order: ${byteOrder})`); + const invalid = (reason: string) => + new TypeError(`${reason} (FAT header byte order: ${byteOrder})`); + if (bytes.length < 8) throw malformed("Malformed FAT architecture table"); + const count = readUInt32(4); const headerEnd = 8 + count * stride; if (count > 128 || headerEnd > bytes.length) - throw new RangeError("Malformed FAT architecture table"); + throw malformed("Malformed FAT architecture table"); const cpu = architecture === "arm64" ? 0x0100000c : 0x01000007; let selected: { slice: number; sliceEnd: number } | undefined; for (let index = 0; index < count; index++) { const offset = 8 + index * stride; - if (bytes.readUInt32BE(offset) !== cpu) continue; + if (readUInt32(offset) !== cpu) continue; if (selected !== undefined) - throw new TypeError("Ambiguous FAT architecture slice"); + throw invalid("Ambiguous FAT architecture slice"); const start = fat64 - ? bytes.readBigUInt64BE(offset + 8) - : BigInt(bytes.readUInt32BE(offset + 8)); + ? readUInt64(offset + 8) + : BigInt(readUInt32(offset + 8)); const size = fat64 - ? bytes.readBigUInt64BE(offset + 16) - : BigInt(bytes.readUInt32BE(offset + 12)); + ? readUInt64(offset + 16) + : BigInt(readUInt32(offset + 12)); if (start < BigInt(headerEnd) || start + size > BigInt(bytes.length)) - throw new RangeError("FAT slice exceeds file"); + throw malformed("FAT slice exceeds file"); selected = { slice: Number(start), sliceEnd: Number(start + size) }; } if (selected === undefined) - throw new TypeError("Requested FAT architecture is absent"); + throw invalid("Requested FAT architecture is absent"); return selected; };