diff --git a/scripts/verify/architecture-guard-baselines.json b/scripts/verify/architecture-guard-baselines.json index 985bcf9aab..2279db0937 100644 --- a/scripts/verify/architecture-guard-baselines.json +++ b/scripts/verify/architecture-guard-baselines.json @@ -121,7 +121,6 @@ "src/il2cpp/Il2cppMetadataFile.ts": 2, "src/model/ModelHeaderReader.ts": 1, "src/native/AppleDispatchMetadata.ts": 1, - "src/native/AppleMachoSelection.ts": 3, "src/native/LldbCallTracer.ts": 1, "src/native/LldbRetainedObservations.ts": 1, "src/native/MachoCommandNames.ts": 1, @@ -570,12 +569,6 @@ "src/server/createServer.ts": 1, "src/windows/WindowsNativeLoader.ts": 1 }, - "keyed-first-match": { - "src/artifacts/fit/FitStructure.ts": 1, - "src/domain/apkSignature/apkV1SignatureBlock.ts": 1, - "src/native/MachoArchitectureSelection.ts": 2, - "src/native/NativeMachoInspection.ts": 1 - }, "abort-without-reason": {}, "completed-abort-reclassification": { "src/application/EvidenceInputResolver.ts": 1, diff --git a/src/artifacts/fit/FitStructure.test.ts b/src/artifacts/fit/FitStructure.test.ts index 8792258e03..55d0aaa021 100644 --- a/src/artifacts/fit/FitStructure.test.ts +++ b/src/artifacts/fit/FitStructure.test.ts @@ -646,3 +646,25 @@ it.each(["data-offset", "data-position"] as const)( expect(imageBytes(file, 1).equals(data)).toBe(true); }, ); + +it("fails an image node that repeats a property name", () => { + const file = devicetreeFixture({ + name: "", + children: [ + { + name: "images", + children: [ + { + name: "kernel", + properties: { data: FIT_KERNEL, type: dtString("kernel") }, + repeatedProperties: [["type", dtString("ramdisk")]], + }, + ], + }, + ], + }); + expect(failureOf(file)).toMatchObject({ + reason: "format", + message: expect.stringMatching(/duplicate.*property/iu), + }); +}); diff --git a/src/artifacts/fit/FitStructure.ts b/src/artifacts/fit/FitStructure.ts index 06f7e44d20..64f77cdba6 100644 --- a/src/artifacts/fit/FitStructure.ts +++ b/src/artifacts/fit/FitStructure.ts @@ -1,5 +1,10 @@ import { createHash } from "node:crypto"; import { crc32 } from "node:zlib"; +import { + keyedBy, + uniqueKeyed, + type Keyed, +} from "../../domain/keyedCollection.js"; import { artifactExpansionBudgetBytes } from "../../domain/artifactExpansionBudget.js"; import type { @@ -83,8 +88,29 @@ const capacityFailure = (message: string): ArtifactReaderFailure => new ArtifactReaderFailure("limit", message, { boundary: "capacity" }); const nodeName = (node: FitNode): string => node.path.slice(node.path.lastIndexOf("/") + 1); -const propertyOf = (node: FitNode, name: string): FitProperty | undefined => - node.properties.find((property) => property.name === name); +/** + * A node's one property of a name. The devicetree parser already refuses a + * repeated property name in a node as malformed, as dtc does; a repeat here + * is the same malformed structure, not a first-wins choice. + */ +const propertiesByNode = new WeakMap>(); +/** Key a node's properties once; every lookup on the node reuses the view. */ +const keyedProperties = (node: FitNode): Keyed => { + let keyed = propertiesByNode.get(node); + if (keyed === undefined) { + keyed = keyedBy(node.properties, (candidate) => candidate.name); + propertiesByNode.set(node, keyed); + } + return keyed; +}; +const propertyOf = (node: FitNode, name: string): FitProperty | undefined => { + const property = uniqueKeyed(keyedProperties(node), name); + if (!property.ok) + throw formatFailure( + `${node.path} declares property ${name} more than once`, + ); + return property.value; +}; const valueOf = (fdt: Buffer, property: FitProperty): Buffer => fdt.subarray(property.offset, property.offset + property.bytes); diff --git a/src/domain/apkSignature/apkV1SignatureBlock.test.ts b/src/domain/apkSignature/apkV1SignatureBlock.test.ts index 00e41a5c83..3a6979934b 100644 --- a/src/domain/apkSignature/apkV1SignatureBlock.test.ts +++ b/src/domain/apkSignature/apkV1SignatureBlock.test.ts @@ -6,7 +6,9 @@ import { indefinite, oid, withIssuer, + withRepeatedExtensions, withSerial, + withSubjectKeyIdentifiers, LEAF_CERTIFICATE, LEAF_SHA256, OID_ECDSA_SHA256, @@ -106,6 +108,20 @@ describe("v1 signature block (PKCS#7 SignedData)", () => { expect(block.certificates.map(sha256)).toEqual([ROOT_SHA256]); }); + it("names no certificate whose tbsCertificate repeats its extensions", () => { + // RFC 5280 §4.1 allows one extensions [3]; a repeat is not a + // TBSCertificate, so neither copy is chosen to name the signer. + const repeated = withRepeatedExtensions(LEAF_CERTIFICATE); + const block = decoded( + signedDataBlock({ + certificates: [repeated], + signerInfos: [signerInfo({ names: LEAF_CERTIFICATE })], + }), + ); + expect(block.signerInfos[0]?.certificateIndex).toBeNull(); + expect(block.certificates.map(sha256)).toEqual([sha256(repeated)]); + }); + it("accepts BER indefinite lengths on constructed elements", () => { const inner = signedDataBlock({ certificates: [LEAF_CERTIFICATE], @@ -451,3 +467,27 @@ it("preserves the canonical OID byte allowance for a valid unknown digest algori ), ).toEqual({ kind: "budget-exceeded", resource: "oid-bytes", limit: 64 }); }); + +it("names no certificate whose extensions repeat subjectKeyIdentifier", () => { + // RFC 5280 §4.2 allows each extension once; neither identifier is chosen. + const named = (certificate: Uint8Array) => + decoded( + signedDataBlock({ + certificates: [certificate], + signerInfos: [signerInfo({ names: LEAF_CERTIFICATE })], + }), + ); + const single = withSubjectKeyIdentifiers( + LEAF_CERTIFICATE, + Buffer.alloc(20, 1), + ); + expect(named(single).signerInfos[0]?.certificateIndex).toBe(0); + const repeated = withSubjectKeyIdentifiers( + LEAF_CERTIFICATE, + Buffer.alloc(20, 1), + Buffer.alloc(20, 2), + ); + const block = named(repeated); + expect(block.signerInfos[0]?.certificateIndex).toBeNull(); + expect(block.certificates.map(sha256)).toEqual([sha256(repeated)]); +}); diff --git a/src/domain/apkSignature/apkV1SignatureBlock.ts b/src/domain/apkSignature/apkV1SignatureBlock.ts index 5946e8303f..b728e9259f 100644 --- a/src/domain/apkSignature/apkV1SignatureBlock.ts +++ b/src/domain/apkSignature/apkV1SignatureBlock.ts @@ -17,6 +17,7 @@ * conflated with malformed input. */ import { type Result, err, ok } from "../result.js"; +import { keyedBy, uniqueKeyed } from "../keyedCollection.js"; import { decodeAsn1Oid } from "../asn1Oid.js"; import { Asn1BerFailure, Asn1BerReader, type Asn1BerNode } from "../asn1Ber.js"; @@ -290,7 +291,16 @@ const certificateIdentity = ( const serial = expectTag(fields[offset], TAG.integer, "serialNumber"); const issuer = expectTag(fields[offset + 2], TAG.sequence, "issuer"); let subjectKeyIdentifier: Uint8Array | null = null; - const extensions = fields.find((field) => field.tag === TAG.context3); + // RFC 5280 §4.1: TBSCertificate is a SEQUENCE with at most one + // `extensions [3]`, and §4.2 allows each extension at most once. A repeat + // is not a TBSCertificate, so the certificate names no SignerInfo. + const extensionFields = uniqueKeyed( + keyedBy(fields, (field) => String(field.tag)), + String(TAG.context3), + ); + if (!extensionFields.ok) + throw malformed("tbsCertificate declares extensions more than once"); + const extensions = extensionFields.value; if (extensions !== undefined) { const list = expectTag( extensions.children[0], @@ -301,6 +311,10 @@ const certificateIdentity = ( const parts = extension.children; const id = expectTag(parts[0], TAG.oid, "extension id"); if (oidText(id.content) !== OID_SUBJECT_KEY_IDENTIFIER) continue; + if (subjectKeyIdentifier !== null) + throw malformed( + "tbsCertificate declares subjectKeyIdentifier more than once", + ); const value = expectTag(parts.at(-1), TAG.octetString, "extnValue"); const inner = reader.read(value.contentStart, value.contentEnd, 0); subjectKeyIdentifier = expectTag( diff --git a/src/firmware/devicetree/Devicetree.fixture.ts b/src/firmware/devicetree/Devicetree.fixture.ts index 2c7151d03b..8081a676f4 100644 --- a/src/firmware/devicetree/Devicetree.fixture.ts +++ b/src/firmware/devicetree/Devicetree.fixture.ts @@ -2,6 +2,8 @@ export interface FixtureNode { name: string; properties?: Record; + /** Written after `properties`, in order, so a name may repeat. */ + repeatedProperties?: readonly (readonly [string, Buffer])[]; children?: FixtureNode[]; } export const cells = (...values: number[]): Buffer => { @@ -49,7 +51,10 @@ export const devicetreeFixture = ( Buffer.concat([b, Buffer.alloc((4 - (b.length % 4)) % 4)]); const walk = (node: FixtureNode) => { structure.push(cells(1), padded(dtString(node.name))); - for (const [name, value] of Object.entries(node.properties ?? {})) { + for (const [name, value] of [ + ...Object.entries(node.properties ?? {}), + ...(node.repeatedProperties ?? []), + ]) { let offset = names.get(name); if (offset === undefined) { offset = stringBytes; diff --git a/src/il2cpp/Il2cppBinaryImage.ts b/src/il2cpp/Il2cppBinaryImage.ts index e0c0afc42f..a60a6ac8ec 100644 --- a/src/il2cpp/Il2cppBinaryImage.ts +++ b/src/il2cpp/Il2cppBinaryImage.ts @@ -19,6 +19,7 @@ import { requireSelectedNativeImage, type SelectedNativeImage, type SliceSelectionInput, + selectMachoSlice, } from "../native/metadata/MachoSliceSelection.js"; import { parsePointerFixupsSteps, @@ -145,12 +146,24 @@ export function* openImageSteps(image: Buffer): Generator { ); let machoPointerFixups: PointerFixups | undefined; if (container.format === "macho") { - try { - const layout = parseMachoLayout( - image, + const selected = selectMachoSlice(image, { + mode: "admitted", + architecture: binaryMetadataArchitecture(container.architecture) ?? - container.architecture, + container.architecture, + slice: undefined, + }); + // The image is the thin slice already selected; a refusal means the + // container's architecture names no CPU family this reader decodes. + if (!selected.ok) + throw new Il2cppBinaryFormatFailure( + selected.error.kind === "malformed" + ? "malformed-container" + : "unsupported-container", + selected.error.message, ); + try { + const layout = parseMachoLayout(image, selected.value); machoPointerFixups = yield* parsePointerFixupsSteps( image, layout.fixups, @@ -166,13 +179,6 @@ export function* openImageSteps(image: Buffer): Generator { : "malformed-container", failure.message, ); - // Mach-O slice selection reports an inconsistent FAT or CPU identity as - // a TypeError. - if (cause instanceof TypeError) - throw new Il2cppBinaryFormatFailure( - "malformed-container", - cause.message, - ); throw cause; } } diff --git a/src/native/AppleDispatchMetadata.test.ts b/src/native/AppleDispatchMetadata.test.ts index 9a56acca6a..15da9c1ce7 100644 --- a/src/native/AppleDispatchMetadata.test.ts +++ b/src/native/AppleDispatchMetadata.test.ts @@ -3,6 +3,18 @@ import { describe, expect, it } from "vitest"; import { machoWithPropertyAttributes } from "../../tests/fixtures/objcPropertyMacho.js"; import { objcRecordBudgetMacho } from "../../tests/fixtures/objcRecordBudgetMacho.js"; import { decodeAppleDispatchMetadata } from "./AppleDispatchMetadata.fixture.js"; +import { ProviderAdapterError } from "../domain/providerAdapterError.js"; + +/** The slice-selection refusal an admitted decode reports as REA's inconsistency. */ +const refusalOf = (decode: () => unknown) => { + try { + decode(); + } catch (cause) { + expect(cause).toBeInstanceOf(ProviderAdapterError); + return (cause as ProviderAdapterError).diagnostics; + } + throw new Error("expected the decode to be refused"); +}; const base = 0x100000000n; const fixture = ( @@ -2033,10 +2045,20 @@ describe("FAT64 dispatch slice validation", () => { }; const admitted = () => ({ offset: 256, size: fixture().length }); - it("rejects a truncated architecture table", () => { + it("does not select from a table that extends past the file", () => { + // Generic admission reads such a table as not universal, so the bytes + // are read whole and refused as an unsupported header. expect(() => decodeAppleDispatchMetadata(wrapped().subarray(0, 32), 100, provenance), - ).toThrow("Malformed FAT architecture table"); + ).toThrow( + expect.objectContaining({ + kind: "unsupported", + message: "Only little-endian 64-bit Mach-O metadata is supported", + }), + ); + expect( + refusalOf(() => decodeAdmitted(wrapped().subarray(0, 32), admitted())), + ).toMatchObject({ refusal: "admitted_slice_mismatch" }); }); it.each([16, 24])( @@ -2060,14 +2082,18 @@ describe("FAT64 dispatch slice validation", () => { bytes.writeUInt32BE(2, 4); bytes.copy(bytes, 40, 8, 40); expect(() => decodeAdmitted(bytes, admitted())).toThrow( - "Admitted FAT slice is declared more than once", + "Mach-O slices 0 and 1 overlap.", ); }); it("refuses a universal image without the admitted slice", () => { - expect(() => - decodeAppleDispatchMetadata(wrapped(), 100, provenance), - ).toThrow("Universal Mach-O metadata needs the admitted slice"); + expect( + refusalOf(() => decodeAppleDispatchMetadata(wrapped(), 100, provenance)), + ).toEqual({ + target_path: provenance.path, + refusal: "admitted_slice_mismatch", + reason: "Universal Mach-O metadata needs the admitted slice", + }); }); it("decodes the admitted capability variant rather than re-selecting", () => { @@ -2120,9 +2146,12 @@ describe("FAT64 dispatch slice validation", () => { }); it("rejects an admitted slice of another architecture", () => { - expect(() => decodeAdmitted(wrapped(), admitted(), "x86_64")).toThrow( - "Admitted FAT slice does not match the requested architecture", - ); + expect( + refusalOf(() => decodeAdmitted(wrapped(), admitted(), "x86_64")), + ).toMatchObject({ + refusal: "admitted_slice_mismatch", + reason: "Admitted FAT slice does not match the requested architecture", + }); }); }); @@ -2161,20 +2190,15 @@ describe("byte-swapped universal Apple dispatch metadata", () => { }, ); - 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("refuses a truncated byte-swapped FAT header", () => { + expect(() => + decodeAppleDispatchMetadata( + wrapped(false).subarray(0, 6), + 100, + provenance, + ), + ).toThrow("FAT header is truncated"); + }); it("reads the selected thin slice's CPU identity in its own byte order", () => { const bytes = wrapped(false); @@ -2194,7 +2218,7 @@ describe("byte-swapped universal Apple dispatch metadata", () => { bytes.writeUInt32LE(12, 260); bytes.writeUInt32LE(0, 264); expect(() => decodeAdmitted(bytes, admitted(), "arm")).toThrow( - "Unsupported 32-bit Mach-O metadata for requested CPU type arm", + "Unsupported 32-bit Mach-O metadata", ); }); @@ -2203,9 +2227,7 @@ describe("byte-swapped universal Apple dispatch metadata", () => { bytes.writeBigUInt64LE(0x100000000n, 16); expect(() => decodeAdmitted(bytes, { offset: 0x100000000, size: admitted().size }), - ).toThrow( - "FAT architecture 0 slice range extends beyond the file (FAT header byte order: little-endian)", - ); + ).toThrow("FAT architecture 0 slice range extends beyond the file"); }); }); diff --git a/src/native/AppleDispatchMetadata.ts b/src/native/AppleDispatchMetadata.ts index c31afd3c0d..28a316ac4d 100644 --- a/src/native/AppleDispatchMetadata.ts +++ b/src/native/AppleDispatchMetadata.ts @@ -47,10 +47,11 @@ import { EvidenceIntegrityError } from "../domain/evidenceErrors.js"; import { pushDispatchCoverage } from "./AppleDispatchCoverage.js"; import { parsePointerFixupsSteps } from "./AppleMachoFixups.js"; import { BinaryFormatFailure } from "./metadata/BinaryContainer.js"; +import { parseMachoLayout } from "./AppleMachoSelection.js"; import { - parseMachoLayout, + selectMachoSlice, type AdmittedMachoSlice, -} from "./AppleMachoSelection.js"; +} from "./metadata/MachoSliceSelection.js"; import { decodeObjcDispatchFacets } from "./AppleObjcDispatchFacets.js"; import { createSwiftRelativeReader, @@ -75,7 +76,25 @@ export function* decodeAppleDispatchMetadataSteps( objcClassImportAddressesComplete = true, admittedSlice?: AdmittedMachoSlice, ): Generator { - const layout = parseMachoLayout(bytes, architecture, admittedSlice); + const selected = selectMachoSlice(bytes, { + mode: "admitted", + architecture, + slice: admittedSlice, + }); + if (!selected.ok) { + if (selected.error.kind === "malformed") + throw malformedInput("macho", selected.error.message); + // Admission bound this architecture and slice to these exact bytes, so a + // refusal is an inconsistency inside REA, not the caller's selection. + throw new ProviderAdapterError(IDENTITY.id, OPERATION, { + diagnostics: { + target_path: provenance.path, + refusal: selected.error.kind, + reason: selected.error.message, + }, + }); + } + const layout = parseMachoLayout(bytes, selected.value); const { segments, sections, offset } = layout; const fixups = yield* parsePointerFixupsSteps( bytes, diff --git a/src/native/AppleMachoSelection.fixture.ts b/src/native/AppleMachoSelection.fixture.ts new file mode 100644 index 0000000000..33e1c02424 --- /dev/null +++ b/src/native/AppleMachoSelection.fixture.ts @@ -0,0 +1,31 @@ +import { malformedInput } from "../domain/parseFailure.js"; +import { parseMachoLayout, type MachoLayout } from "./AppleMachoSelection.js"; +import { + selectMachoSlice, + type AdmittedMachoSlice, + type MachoSliceSelectionRefusal, +} from "./metadata/MachoSliceSelection.js"; + +/** A refused admitted selection, thrown so a test can assert its kind. */ +export class MachoSliceRefused extends Error { + constructor(readonly refusal: MachoSliceSelectionRefusal) { + super(refusal.message); + } +} + +/** Select the admitted image, then parse its layout, as the metadata readers do. */ +export const layoutFor = ( + bytes: Buffer, + architecture: string, + slice?: AdmittedMachoSlice, +): MachoLayout => { + const selected = selectMachoSlice(bytes, { + mode: "admitted", + architecture, + slice, + }); + if (selected.ok) return parseMachoLayout(bytes, selected.value); + if (selected.error.kind === "malformed") + throw malformedInput("macho", selected.error.message); + throw new MachoSliceRefused(selected.error); +}; diff --git a/src/native/AppleMachoSelection.test.ts b/src/native/AppleMachoSelection.test.ts index 052a4a80e5..a4169f9729 100644 --- a/src/native/AppleMachoSelection.test.ts +++ b/src/native/AppleMachoSelection.test.ts @@ -18,8 +18,19 @@ import { } from "../artifacts/apple/MachoImage.fixture.js"; import { readMachoImage } from "../artifacts/apple/MachoLoadCommandReader.js"; import { readMachoImage as readMachoMetadataImage } from "./metadata/MachoImage.js"; -import { parseMachoLayout, selectMachoSlice } from "./AppleMachoSelection.js"; import { BinaryFormatFailure } from "./metadata/BinaryContainer.js"; +import { layoutFor, MachoSliceRefused } from "./AppleMachoSelection.fixture.js"; +import { + selectMachoSlice, + type AdmittedMachoSlice, +} from "./metadata/MachoSliceSelection.js"; +import { ProviderAdapterError } from "../domain/providerAdapterError.js"; + +const admittedSlice = ( + bytes: Buffer, + architecture: string, + slice?: AdmittedMachoSlice, +) => selectMachoSlice(bytes, { mode: "admitted", architecture, slice }); import { parsePointerFixups } from "./AppleMachoFixups.fixture.js"; import { decodeAppleDispatchMetadata } from "./AppleDispatchMetadata.fixture.js"; @@ -28,18 +39,19 @@ const ARM64E_X1 = { type: 0x0100000c, subtype: 0x8000000c } as const; const X86_64H = { type: 0x01000007, subtype: 8 } as const; it("keeps unknown magic distinct from recognized unsupported Mach-O formats", () => { - expect(() => parseMachoLayout(Buffer.alloc(10), "arm64")).toThrow( + expect(() => layoutFor(Buffer.alloc(10), "arm64")).toThrow( "Truncated Mach-O header", ); const unknown = Buffer.alloc(32); let failure: unknown; try { - parseMachoLayout(unknown, "arm64"); + layoutFor(unknown, "arm64"); } catch (error) { failure = error; } - expect(failure).toBeInstanceOf(TypeError); + expect(failure).toBeInstanceOf(ParseFailureError); + expect(failure).toMatchObject({ kind: "unsupported" }); expect(failure).not.toBeInstanceOf(BinaryFormatFailure); }); @@ -53,7 +65,7 @@ it.each([ ({ magic, headerSize }) => { const bytes = Buffer.alloc(headerSize - 1); bytes.writeUInt32BE(magic); - expect(() => parseMachoLayout(bytes, "unrecognized")).toThrow( + expect(() => layoutFor(bytes, "unrecognized")).toThrow( truncatedInput("macho", "Truncated Mach-O header"), ); }, @@ -68,9 +80,15 @@ const registerThinCpuFamilyControls = (): void => { "rejects a thin image outside the requested $architecture CPU family", ({ cpu, architecture }) => { const bytes = Buffer.from(machoImage({ cpu })); - expect(() => parseMachoLayout(bytes, architecture)).toThrow( - `Mach-O CPU type does not match requested architecture ${architecture}`, - ); + expect(admittedSlice(bytes, architecture)).toEqual({ + ok: false, + error: { + kind: "architecture_not_present", + message: `Mach-O CPU type does not match requested architecture ${architecture}`, + }, + }); + // Admission bound the architecture to these bytes; disagreement is + // REA's inconsistency, not a malformed target. expect(() => decodeAppleDispatchMetadata( bytes, @@ -78,15 +96,13 @@ const registerThinCpuFamilyControls = (): void => { { path: "/thin", sha256: "fixture" }, architecture, ), - ).toThrow( - `Mach-O CPU type does not match requested architecture ${architecture}`, - ); + ).toThrow(ProviderAdapterError); }, ); it("accepts a thin x86_64 image for the requested CPU family", () => { const bytes = Buffer.from(machoImage({ cpu: CPU.x86_64 })); - expect(parseMachoLayout(bytes, "x86_64").selectedImage).toMatchObject({ + expect(layoutFor(bytes, "x86_64").selectedImage).toMatchObject({ container: "thin", cpuType: CPU.x86_64.type, cpuSubtype: CPU.x86_64.subtype, @@ -100,17 +116,16 @@ it.each([false, true])( const thin = machoImage({ cpu: CPU.arm64e }); const fat = Buffer.from(fatImage([{ cpu: CPU.arm64e, bytes: thin }], wide)); if (wide) fat.writeUInt32BE(0x89abcdef, 8 + 28); - expect( - selectMachoSlice(fat, "arm64", fatSliceRange(fat, 0)).fatDeclaration, - ).toEqual({ - index: 0, + const selected = admittedSlice(fat, "arm64", fatSliceRange(fat, 0)); + if (!selected.ok) throw new Error(selected.error.message); + expect(selected.value.selection).toEqual({ + kind: "macho-selected-image-v1", + container: "fat", offset: 4096, - size: thin.byteLength, - cpuType: CPU.arm64e.type, - cpuSubtype: CPU.arm64e.subtype, - alignmentExponent: 12, - reserved: wide ? 0x89abcdef : null, - tableEnd: 8 + (wide ? 32 : 20), + byte_length: thin.byteLength, + cpu_type: CPU.arm64e.type, + cpu_subtype: CPU.arm64e.subtype, + container_table_sha256: expect.stringMatching(/^[a-f0-9]{64}$/u), }); }, ); @@ -171,7 +186,7 @@ it.each([ Buffer.from("__DATA\0\npadding", "latin1").copy(segment, 72 + 16); const bytes = Buffer.from(machoImage({ commands: [segment] })); - expect(parseMachoLayout(bytes, "arm64").sections[0]?.name).toBe(expected); + expect(layoutFor(bytes, "arm64").sections[0]?.name).toBe(expected); expect(readMachoMetadataImage(bytes).sections[0]).toMatchObject({ name: `__DATA,${expected}`, section_name: expected, @@ -209,7 +224,7 @@ describe("Mach-O metadata span mapping", () => { ["VM address overflow", mappingSegment((1n << 64n) - 1n, 0, 2)], ])("rejects %s consistently across metadata consumers", (_name, segment) => { const bytes = mappingImage([segment]); - expect(() => parseMachoLayout(bytes, "arm64")).toThrow( + expect(() => layoutFor(bytes, "arm64")).toThrow( "Mach-O segment addresses or sizes are invalid", ); expect(() => readMachoMetadataImage(bytes)).toThrow( @@ -226,7 +241,7 @@ describe("Mach-O metadata span mapping", () => { segment.writeUInt32LE(0, 60); } else segment.write("__PAGEZERO", 8, "ascii"); const bytes = mappingImage([segment]); - const layout = parseMachoLayout(bytes, "arm64"); + const layout = layoutFor(bytes, "arm64"); // Dyld fixup segment ordinals retain the original command order. expect(layout.segments).toHaveLength(1); expect(layout.baseAddress).toBe(0n); @@ -257,7 +272,7 @@ describe("Mach-O metadata span mapping", () => { dyldInfo.writeUInt32LE(opcodes.length, 20); const bytes = mappingImage([pagezero, unmapped, data, dyldInfo]); opcodes.copy(bytes, 400); - const layout = parseMachoLayout(bytes, "arm64"); + const layout = layoutFor(bytes, "arm64"); expect(layout.offset(0x2008n, 8)).toBe(264); expect(() => layout.offset(8n, 8)).toThrow(ParseFailureError); expect(() => layout.offset(0x1000n, 8)).toThrow(ParseFailureError); @@ -277,7 +292,7 @@ describe("Mach-O metadata span mapping", () => { }); it("rejects a conflicting alias covering only the end of a pointer", () => { - const layout = parseMachoLayout( + const layout = layoutFor( mappingImage([ mappingSegment(0x1000n, 0, 512), mappingSegment(0x1104n, 400, 4), @@ -289,7 +304,7 @@ describe("Mach-O metadata span mapping", () => { }); it("maps adjacent ranges and duplicate aliases with the same file translation", () => { - const layout = parseMachoLayout( + const layout = layoutFor( mappingImage([ mappingSegment(0x1000n, 0, 128), mappingSegment(0x1080n, 128, 128), @@ -301,7 +316,7 @@ describe("Mach-O metadata span mapping", () => { }); it("rejects file aliases into another segment's zero-filled memory", () => { - const layout = parseMachoLayout( + const layout = layoutFor( mappingImage([ mappingSegment(0x1000n, 0, 128, 256), mappingSegment(0x1080n, 256, 8), @@ -314,7 +329,7 @@ describe("Mach-O metadata span mapping", () => { it.each([-1, 0.5, Number.POSITIVE_INFINITY])( "rejects an invalid requested span size %s", (size) => { - const layout = parseMachoLayout( + const layout = layoutFor( mappingImage([mappingSegment(0x1000n, 0, 512)]), "arm64", ); @@ -359,7 +374,7 @@ it("rejects a segment command of the wrong width before claiming metadata absenc expect(() => readMachoMetadataImage(bytes)).toThrow( "Mach-O segment command does not match the header width", ); - expect(() => parseMachoLayout(bytes, "arm64")).toThrow( + expect(() => layoutFor(bytes, "arm64")).toThrow( "Mach-O segment command does not match the header width", ); }); @@ -367,19 +382,14 @@ it("rejects a segment command of the wrong width before claiming metadata absenc describe("native Mach-O slice selection", () => { it("rejects misaligned load commands", () => { expect(() => - parseMachoLayout( - Buffer.from(machoImageWithUnknownCommand(true, 12)), - "arm64", - ), + layoutFor(Buffer.from(machoImageWithUnknownCommand(true, 12)), "arm64"), ).toThrow(/invalid cmdsize/u); }); it("accepts an aligned load command", () => { expect( - parseMachoLayout( - Buffer.from(machoImageWithUnknownCommand(true, 16)), - "arm64", - ).segments, + layoutFor(Buffer.from(machoImageWithUnknownCommand(true, 16)), "arm64") + .segments, ).toEqual([]); }); @@ -392,7 +402,7 @@ describe("native Mach-O slice selection", () => { // the known `__objc_classlist` name from a malformed high-bit byte. Buffer.from("__objc_classlist", "ascii").copy(segment, 72); segment[72] = 0xdf; - const layout = parseMachoLayout( + const layout = layoutFor( Buffer.from(machoImage({ commands: [segment] })), "arm64", ); @@ -403,8 +413,7 @@ describe("native Mach-O slice selection", () => { it("accepts a FAT slice whose table identity and embedded header agree", () => { const fat = fatImage([{ cpu: CPU.arm64, bytes: machoImage({}) }]); expect( - parseMachoLayout(Buffer.from(fat), "arm64", fatSliceRange(fat, 0)) - .segments, + layoutFor(Buffer.from(fat), "arm64", fatSliceRange(fat, 0)).segments, ).toEqual([]); }); @@ -412,7 +421,7 @@ describe("native Mach-O slice selection", () => { it("retains the selected image range and complete raw CPU identity", () => { const thin = Buffer.from(machoImage({ cpu: CPU.arm64e })); - expect(parseMachoLayout(thin, "arm64").selectedImage).toEqual({ + expect(layoutFor(thin, "arm64").selectedImage).toEqual({ container: "thin", offset: 0, byteLength: thin.length, @@ -424,7 +433,7 @@ describe("native Mach-O slice selection", () => { const fat = Buffer.from( fatImage([{ cpu: ARM64E_X1, bytes: machoImage({ cpu: ARM64E_X1 }) }]), ); - const selection = parseMachoLayout( + const selection = layoutFor( fat, "arm64", fatSliceRange(fat, 0), @@ -447,7 +456,7 @@ describe("native Mach-O slice selection", () => { new DataView(fat.buffer).setUint32(sliceOffset + 4, CPU.arm64.type, true); expect(() => - parseMachoLayout(Buffer.from(fat), "x86", fatSliceRange(fat, 0)), + layoutFor(Buffer.from(fat), "x86", fatSliceRange(fat, 0)), ).toThrow( /FAT architecture 0 CPU type\/subtype disagrees with its Mach-O header/u, ); @@ -458,12 +467,12 @@ describe("native Mach-O slice selection", () => { { cpu: CPU.x86_64, bytes: machoImage({ cpu: CPU.x86_64 }) }, ]); const admitted = fatSliceRange(sixtyFour, 0); - expect(() => - parseMachoLayout(Buffer.from(sixtyFour), "x86", admitted), - ).toThrow(/Admitted FAT slice does not match the requested architecture/u); - expect(() => - parseMachoLayout(Buffer.from(sixtyFour), "arm", admitted), - ).toThrow(/Admitted FAT slice does not match the requested architecture/u); + expect(() => layoutFor(Buffer.from(sixtyFour), "x86", admitted)).toThrow( + /Admitted FAT slice does not match the requested architecture/u, + ); + expect(() => layoutFor(Buffer.from(sixtyFour), "arm", admitted)).toThrow( + /Admitted FAT slice does not match the requested architecture/u, + ); const thirtyTwo = fatImage([ { cpu: CPU.i386, @@ -471,12 +480,8 @@ describe("native Mach-O slice selection", () => { }, ]); expect(() => - parseMachoLayout( - Buffer.from(thirtyTwo), - "x86", - fatSliceRange(thirtyTwo, 0), - ), - ).toThrow(/Unsupported 32-bit Mach-O metadata for requested CPU type x86/u); + layoutFor(Buffer.from(thirtyTwo), "x86", fatSliceRange(thirtyTwo, 0)), + ).toThrow(/Unsupported 32-bit Mach-O metadata/u); }); it("rejects FAT subtype disagreement and unaligned slice offsets", () => { @@ -484,21 +489,13 @@ describe("native Mach-O slice selection", () => { const mismatched = original.slice(); new DataView(mismatched.buffer).setUint32(12, 2, false); expect(() => - parseMachoLayout( - Buffer.from(mismatched), - "arm64", - fatSliceRange(mismatched, 0), - ), + layoutFor(Buffer.from(mismatched), "arm64", fatSliceRange(mismatched, 0)), ).toThrow(/disagrees/u); const unaligned = original.slice(); new DataView(unaligned.buffer).setUint32(16, 4097, false); expect(() => - parseMachoLayout( - Buffer.from(unaligned), - "arm64", - fatSliceRange(unaligned, 0), - ), + layoutFor(Buffer.from(unaligned), "arm64", fatSliceRange(unaligned, 0)), ).toThrow(/not aligned/u); }); }); @@ -513,12 +510,14 @@ describe("admitted Mach-O slices", () => { })), ), ); - expect(() => selectMachoSlice(fat, "arm64")).toThrow( - /Universal Mach-O metadata needs the admitted slice/u, - ); - expect(() => parseMachoLayout(fat, "arm64")).toThrow( - /Universal Mach-O metadata needs the admitted slice/u, - ); + expect(admittedSlice(fat, "arm64")).toEqual({ + ok: false, + error: { + kind: "admitted_slice_mismatch", + message: "Universal Mach-O metadata needs the admitted slice", + }, + }); + expect(() => layoutFor(fat, "arm64")).toThrow(MachoSliceRefused); }); it("refuses an admitted range the FAT table does not declare", () => { @@ -526,9 +525,13 @@ describe("admitted Mach-O slices", () => { fatImage([{ cpu: CPU.arm64, bytes: machoImage({}) }]), ); const { offset, size } = fatSliceRange(fat, 0); - expect(() => - selectMachoSlice(fat, "arm64", { offset, size: size + 1 }), - ).toThrow(/Admitted FAT slice is not declared/u); + expect(admittedSlice(fat, "arm64", { offset, size: size + 1 })).toEqual({ + ok: false, + error: { + kind: "admitted_slice_mismatch", + message: "Admitted FAT slice is not declared", + }, + }); }); it.each([ @@ -547,11 +550,15 @@ describe("admitted Mach-O slices", () => { ); for (const [index, cpu] of cpus.entries()) { const admitted = fatSliceRange(fat, index); + expect(admittedSlice(fat, architecture, admitted)).toMatchObject({ + ok: true, + value: { + fileOffset: admitted.offset, + selection: { cpu_subtype: cpu.subtype }, + }, + }); expect( - selectMachoSlice(fat, architecture, admitted).fatDeclaration, - ).toMatchObject({ index, cpuSubtype: cpu.subtype }); - expect( - parseMachoLayout(fat, architecture, admitted).selectedImage, + layoutFor(fat, architecture, admitted).selectedImage, ).toMatchObject({ offset: admitted.offset, cpuSubtype: cpu.subtype }); } }, @@ -575,7 +582,7 @@ describe("admitted Mach-O slices", () => { ]), ); const admitted = fatSliceRange(fat, 1); - expect(parseMachoLayout(fat, "arm64", admitted)).toMatchObject({ + expect(layoutFor(fat, "arm64", admitted)).toMatchObject({ selectedImage: { container: "fat", offset: admitted.offset, @@ -633,7 +640,7 @@ describe("native Mach-O fixup load command boundaries", () => { }); const thrown = (() => { try { - parseMachoLayout(Buffer.from(machoImage({ commands })), "arm64"); + layoutFor(Buffer.from(machoImage({ commands })), "arm64"); return undefined; } catch (cause: unknown) { return cause; @@ -648,7 +655,7 @@ describe("native Mach-O fixup load command boundaries", () => { it("preserves valid library identity and rejects install names outside the command", () => { const original = dylibCommand(LC.LOAD_DYLIB, "/usr/lib/libobjc.A.dylib"); - const layout = parseMachoLayout( + const layout = layoutFor( Buffer.from(machoImage({ commands: [original] })), "arm64", ); @@ -679,10 +686,7 @@ describe("native Mach-O fixup load command boundaries", () => { Buffer.from(dylibCommand(LC.LOAD_DYLIB, "/lib�")), Buffer.from(dylibCommand(LC.LOAD_DYLIB, "\ufeff/libé")), ); - const layout = parseMachoLayout( - Buffer.from(machoImage({ commands })), - "arm64", - ); + const layout = layoutFor(Buffer.from(machoImage({ commands })), "arm64"); expect(layout.fixups.dylibs).toEqual([ { bytes_base64: Buffer.from([0x2f, 0x6c, 0x69, 0x62, 0x80]).toString( @@ -713,7 +717,7 @@ describe("native Mach-O fixup load command boundaries", () => { const nameOffset = command.readUInt32LE(8); command[nameOffset + 4] = 0x80; const bytes = Buffer.from(machoImage({ commands: [command] })); - expect(parseMachoLayout(bytes, "arm64").fixups.dylibs).toEqual([ + expect(layoutFor(bytes, "arm64").fixups.dylibs).toEqual([ { bytes_base64: Buffer.from([0x2f, 0x6c, 0x69, 0x62, 0x80]).toString( "base64", @@ -734,7 +738,7 @@ describe("native Mach-O fixup load command boundaries", () => { }); it.each([0, 129])( - "preserves native FAT selection policy separately from generic identity admission (count %i)", + "admits the FAT table by the same rules as generic identity admission (count %i)", (count) => { const bytes = Buffer.alloc(8); bytes.writeUInt32BE(0xcafebabe); @@ -744,15 +748,18 @@ it.each([0, 129])( ? { state: "malformed", reason: "FAT header declares no architectures" } : { state: "not-fat" }, ); - expect(() => selectMachoSlice(bytes, "arm64")).toThrow( + // A table generic admission rejects as not FAT is not selected from; + // the image is then read whole and its header refused by the layout. + expect(admittedSlice(bytes, "arm64")).toMatchObject( count === 0 - ? new TypeError( - "Universal Mach-O metadata needs the admitted slice (FAT header byte order: big-endian)", - ) - : malformedInput( - "macho", - "Malformed FAT architecture table (FAT header byte order: big-endian)", - ), + ? { + ok: false, + error: { + kind: "malformed", + message: "FAT header declares no architectures", + }, + } + : { ok: true, value: { fileOffset: 0, selection: null } }, ); }, ); diff --git a/src/native/AppleMachoSelection.ts b/src/native/AppleMachoSelection.ts index f0da4b4310..1806e962d5 100644 --- a/src/native/AppleMachoSelection.ts +++ b/src/native/AppleMachoSelection.ts @@ -1,4 +1,8 @@ -import { malformedInput, truncatedInput } from "../domain/parseFailure.js"; +import { + malformedInput, + truncatedInput, + unsupportedInput, +} from "../domain/parseFailure.js"; import { LC } from "../domain/apple/machoLoadCommandCodes.js"; import type { FileMapping, @@ -17,7 +21,6 @@ import { MachoLoadCommandCursor, readMachoLoadCommandDeclarations, } from "../domain/apple/machoLoadCommands.js"; -import { createHash } from "node:crypto"; import { BinaryFormatFailure, createFileMappingIndex, @@ -25,14 +28,7 @@ import { import type { FixupCommands } from "./AppleMachoFixups.js"; import { decodeMachoFixedName, decodeMachoName } from "./AppleMachoNames.js"; -import { MACHO_ARCHITECTURE_POLICIES } from "./MachoArchitectureSelection.js"; -import { - readFatSliceDeclarations, - readFatHeaderPrefix, - validateFatSlice, - validateFatSliceIdentity, - type FatSliceDeclaration, -} from "../domain/apple/machoContainer.js"; +import type { SelectedNativeImage } from "./metadata/MachoSliceSelection.js"; /** One mapped Mach-O segment with file-backed VA range. */ export interface Segment { @@ -49,14 +45,6 @@ export interface Section { size: number; } -/** Selected thin slice bounds inside a Mach-O or FAT container. */ -export interface MachoSlice { - slice: number; - sliceEnd: number; - /** FAT table identity, absent for a thin image. */ - fatDeclaration?: FatSliceDeclaration & { readonly tableEnd: number }; -} - /** Parsed little-endian 64-bit Mach-O layout for the selected architecture slice. */ export interface MachoLayout { segments: Segment[]; @@ -80,82 +68,18 @@ export interface MachoLayout { const hex = (value: bigint) => `0x${value.toString(16)}`; -/** File range admission already chose. Metadata must not rank slices again. */ -export interface AdmittedMachoSlice { - readonly offset: number; - readonly size: number; -} - /** - * Locate the thin Mach-O image to read. A thin image is read whole; a - * universal image only at the slice target admission selected, whose - * declaration and CPU family this reader validates. + * Parse segments/sections of the little-endian 64-bit image `selectMachoSlice` + * selected from `bytes`; the selection owns slice choice and CPU admission. */ -export const selectMachoSlice = ( - bytes: Buffer, - architecture: string, - admitted?: AdmittedMachoSlice, -): MachoSlice => { - const header = readFatHeaderPrefix(bytes); - if (header.state === "unknown") - throw truncatedInput("macho", "Truncated Mach-O header"); - if (header.state === "not-fat") return { slice: 0, sliceEnd: bytes.length }; - const { littleEndian, wide } = header; - const byteOrder = littleEndian ? "little-endian" : "big-endian"; - const malformed = (reason: string) => - malformedInput("macho", `${reason} (FAT header byte order: ${byteOrder})`); - const invalid = (reason: string) => - new TypeError(`${reason} (FAT header byte order: ${byteOrder})`); - if (header.state === "incomplete") - throw malformed("Malformed FAT architecture table"); - const { count, tableEnd: headerEnd } = header; - if (count > 128 || headerEnd > bytes.length) - throw malformed("Malformed FAT architecture table"); - const policy = MACHO_ARCHITECTURE_POLICIES[architecture]; - if (policy === undefined) - throw invalid("Requested FAT architecture is absent"); - const declarations = readFatSliceDeclarations(bytes.subarray(8, headerEnd), { - count, - wide, - littleEndian, - }); - if (declarations.status !== "parsed") throw malformed(declarations.reason); - // Target admission owns slice choice; ranking here again could read a - // different slice than the one the session's Evidence names. - if (admitted === undefined) - throw invalid("Universal Mach-O metadata needs the admitted slice"); - const matches = declarations.slices.filter( - (declaration) => - declaration.offset === admitted.offset && - declaration.size === admitted.size, - ); - const [match] = matches; - if (match === undefined) throw invalid("Admitted FAT slice is not declared"); - if (matches.length > 1) - throw malformed("Admitted FAT slice is declared more than once"); - if (match.cpuType !== policy.cpuType) - throw invalid( - "Admitted FAT slice does not match the requested architecture", - ); - const issue = validateFatSlice(match, bytes.length, headerEnd); - if (issue !== null) throw malformed(issue); - return { - slice: match.offset, - sliceEnd: match.offset + match.size, - fatDeclaration: { ...match, tableEnd: headerEnd }, - }; -}; - -/** Parse segments/sections for a little-endian 64-bit Mach-O architecture slice. */ export const parseMachoLayout = ( bytes: Buffer, - architecture: string, - admitted?: AdmittedMachoSlice, + selected: SelectedNativeImage, ): MachoLayout => { if (bytes.length < 4) throw truncatedInput("macho", "Truncated Mach-O header"); - const selection = selectMachoSlice(bytes, architecture, admitted); - const { slice, sliceEnd } = selection; + const slice = selected.fileOffset; + const sliceEnd = slice + selected.bytes.length; if (slice + 4 > sliceEnd) throw truncatedInput("macho", "Truncated Mach-O header"); const selectedBytes = bytes.subarray(slice, sliceEnd); @@ -168,20 +92,16 @@ export const parseMachoLayout = ( if (declarations.value === null) { if (selectedBytes.length < 32) throw truncatedInput("macho", "Truncated Mach-O header"); - throw new TypeError( + // No Mach-O magic this reader knows: an unsupported header, and not a + // recognized class with a metadata capability limit. + throw unsupportedInput( + "macho", "Only little-endian 64-bit Mach-O metadata is supported", ); } const table = declarations.value; const { cpuType, cpuSubtype } = table.identity; - const fat = selection.fatDeclaration; - if (fat !== undefined) { - const issue = validateFatSliceIdentity(fat, { - cpuType, - cpuSubtype, - }); - if (issue !== null) throw new TypeError(issue); - } + const fat = selected.selection; if (table.wide && !table.littleEndian) { if (cpuType !== 0x01000012) throw new BinaryFormatFailure( @@ -197,16 +117,10 @@ export const parseMachoLayout = ( }, ); } - // A thin image has no FAT declaration to bind the requested CPU family. - // Admit its parsed header before consumers attach the caller's architecture. - if (MACHO_ARCHITECTURE_POLICIES[architecture]?.cpuType !== cpuType) - throw new TypeError( - `Mach-O CPU type does not match requested architecture ${architecture}`, - ); if (!table.wide) throw new BinaryFormatFailure( "unsupported", - `Unsupported 32-bit Mach-O metadata for requested CPU type ${architecture}`, + "Unsupported 32-bit Mach-O metadata", { format: "macho", undecodedContent: true, @@ -270,17 +184,12 @@ export const parseMachoLayout = ( segments, sections, selectedImage: { - container: fat === undefined ? "thin" : "fat", + container: fat === null ? "thin" : "fat", offset: slice, byteLength: sliceEnd - slice, cpuType, cpuSubtype, - containerTableSha256: - fat === undefined - ? null - : createHash("sha256") - .update(bytes.subarray(0, fat.tableEnd)) - .digest("hex"), + containerTableSha256: fat?.container_table_sha256 ?? null, }, offset, fixups: { chained, binds, rebases, dylibs }, diff --git a/src/native/MachoArchitectureSelection.test.ts b/src/native/MachoArchitectureSelection.test.ts new file mode 100644 index 0000000000..4db2de70a3 --- /dev/null +++ b/src/native/MachoArchitectureSelection.test.ts @@ -0,0 +1,85 @@ +import { describe, expect, it } from "vitest"; + +import { selectLipoArchitecture } from "./MachoArchitectureSelection.js"; +import type { LipoArchitecture } from "./parsers/lipo.js"; + +const record = ( + name: string, + file_offset: number | null, + size: number | null = 4096, +): LipoArchitecture => ({ + name, + hidden: false, + cpu_type: null, + cpu_subtype: null, + cpu_type_code: null, + cpu_subtype_code: null, + cpu_subtype_base: null, + capabilities: null, + file_offset, + size, + alignment: null, + fields: { + offset: { kind: "absent" }, + size: { kind: "absent" }, + align: { kind: "absent" }, + }, + limitations: [], +}); + +describe("selectLipoArchitecture", () => { + it("refuses a policy-less name lipo lists twice instead of taking the first", () => { + const available = [record("ppc", 4096), record("ppc", 8192)]; + expect( + selectLipoArchitecture( + { mode: "architecture", architecture: "ppc" }, + available, + ), + ).toMatchObject({ ok: false, error: { kind: "ambiguous_architecture" } }); + expect( + selectLipoArchitecture({ mode: "architecture", architecture: "ppc" }, [ + record("ppc", 4096), + record("i386", 8192), + ]), + ).toMatchObject({ ok: true, value: { toolName: "ppc" } }); + }); + + it("separates an absent caller architecture from an undeclared admitted slice", () => { + const available = [record("arm64", 4096)]; + expect( + selectLipoArchitecture( + { mode: "architecture", architecture: "x86" }, + available, + ), + ).toEqual({ + ok: false, + error: { + kind: "architecture_not_present", + message: "lipo did not list i386; refusing to read another slice", + }, + }); + expect( + selectLipoArchitecture( + { + mode: "admitted", + architecture: "arm64", + slice: { offset: 8192, size: 4096 }, + }, + available, + ), + ).toMatchObject({ ok: false, error: { kind: "admitted_slice_mismatch" } }); + }); + + it("refuses an admitted range lipo lists twice", () => { + expect( + selectLipoArchitecture( + { + mode: "admitted", + architecture: "arm64", + slice: { offset: 4096, size: 4096 }, + }, + [record("arm64", 4096), record("arm64e", 4096, null)], + ), + ).toMatchObject({ ok: false, error: { kind: "ambiguous_architecture" } }); + }); +}); diff --git a/src/native/MachoArchitectureSelection.ts b/src/native/MachoArchitectureSelection.ts index 4f969377b5..52e2fa7c3d 100644 --- a/src/native/MachoArchitectureSelection.ts +++ b/src/native/MachoArchitectureSelection.ts @@ -5,7 +5,13 @@ import { X86_64_HOST_SUBTYPE_PREFERENCE, X86_HOST_SUBTYPE_PREFERENCE, } from "../domain/apple/machoCpuIdentity.js"; +import { keyedBy, uniqueKeyed } from "../domain/keyedCollection.js"; +import { err, ok, type Result } from "../domain/result.js"; import type { LipoArchitecture } from "./parsers/lipo.js"; +import type { + AdmittedMachoSlice, + MachoSliceSelectionRefusal, +} from "./metadata/MachoSliceSelection.js"; /** Apple tool names for the domain-owned host subtype preference. */ const preferredSubtypes = ( @@ -55,6 +61,24 @@ export const MACHO_ARCHITECTURE_POLICIES: Readonly< }, }; +/** + * Each policy's subtypes by Apple tool name. The table is this module's + * static configuration, and every name in it is distinct, so a name lookup + * selects no member of parsed data. + */ +const SUBTYPES_BY_NAME: ReadonlyMap< + MachoArchitecturePolicy, + ReadonlyMap +> = new Map( + Object.values(MACHO_ARCHITECTURE_POLICIES).map((policy) => [ + policy, + new Map(policy.subtypes.map((subtype) => [subtype.name, subtype])), + ]), +); + +const subtypeNamed = (policy: MachoArchitecturePolicy, name: string) => + SUBTYPES_BY_NAME.get(policy)?.get(name); + /** Unlisted subtypes follow the preferred slices in FAT table order. */ const machoSubtypeRank = ( policy: MachoArchitecturePolicy, @@ -112,7 +136,7 @@ export const machoKnownCodeSignSelector = ( }; /** Keep the selected lipo record, including its container coordinates. */ -export const selectMachoArchitecture = ( +const selectPreferredArchitecture = ( policy: MachoArchitecturePolicy, available: readonly LipoArchitecture[], ): LipoArchitecture | undefined => { @@ -124,9 +148,7 @@ export const selectMachoArchitecture = ( architecture.cpu_type_code !== policy.cpuType ) continue; - const namedSubtype = policy.subtypes.find( - ({ name }) => name === architecture.name, - ); + const namedSubtype = subtypeNamed(policy, architecture.name); const recognizedName = namedSubtype !== undefined || policy.variantName?.test(architecture.name); if ( @@ -149,11 +171,11 @@ export const selectMachoArchitecture = ( }; /** lipo may label ARM64 V8 as arm64 even though tools select it as arm64v8. */ -export const machoToolArchitectureName = ( +const machoToolArchitectureName = ( policy: MachoArchitecturePolicy, architecture: LipoArchitecture, ): string => { - const named = policy.subtypes.find(({ name }) => name === architecture.name); + const named = subtypeNamed(policy, architecture.name); const actual = policy.subtypes.find( ({ subtype }) => subtype === architecture.cpu_subtype_base, ); @@ -161,3 +183,89 @@ export const machoToolArchitectureName = ( ? actual.name : architecture.name; }; + +/** + * lipo appends the pointer-auth ABI version to arm64e (`arm64e.v1`). Only + * dyld_info accepts that spelling; the other Apple tools select the slice by + * its CPU-family name. Other variants are distinct subtypes (`arm64e.x1`) + * that the tools address only by their exact name. + */ +export const architectureBaseName = (architecture: string): string => + architecture.replace(/\.v\d+$/u, ""); + +/** The lipo record one request addresses, and the name tools select it by. */ +export interface SelectedLipoArchitecture { + readonly architecture: LipoArchitecture; + readonly toolName: string; +} + +/** + * Select the lipo record slice tools read, in the same modes as + * `selectMachoSlice`: an admitted universal slice by its file range, or a + * caller's architecture by CPU policy (by lipo name when REA has no policy + * for it). A selector that tools would resolve to more than one record is + * refused rather than associated with one record's offset. + */ +export const selectLipoArchitecture = ( + request: + | { + readonly mode: "admitted"; + readonly architecture: string; + readonly slice: AdmittedMachoSlice; + } + | { readonly mode: "architecture"; readonly architecture: string }, + available: readonly LipoArchitecture[], +): Result => { + const policy = MACHO_ARCHITECTURE_POLICIES[request.architecture]; + const toolName = (record: LipoArchitecture): string => + policy === undefined + ? record.name + : machoToolArchitectureName(policy, record); + const spelled = + request.architecture === "x86" ? "i386" : request.architecture; + const absent = ( + kind: "architecture_not_present" | "admitted_slice_mismatch", + ): Result => + err({ + kind, + message: `lipo did not list ${spelled}; refusing to read another slice`, + }); + const ambiguous = ( + selector: string, + ): Result => + err({ + kind: "ambiguous_architecture", + message: `lipo listed multiple slices addressable as ${selector}; refusing to associate a slice offset with an ambiguous tool selection`, + }); + if (request.mode === "admitted") { + const { slice } = request; + const matches = available.filter( + (candidate) => + candidate.file_offset === slice.offset && + (candidate.size === null || candidate.size === slice.size), + ); + const [admitted] = matches; + if (admitted === undefined) return absent("admitted_slice_mismatch"); + if (matches.length > 1) return ambiguous(spelled); + return ok({ architecture: admitted, toolName: toolName(admitted) }); + } + let selected: LipoArchitecture | undefined; + if (policy === undefined) { + const named = uniqueKeyed( + keyedBy(available, ({ name }) => name), + spelled, + ); + if (!named.ok) return ambiguous(spelled); + selected = named.value; + } else selected = selectPreferredArchitecture(policy, available); + if (selected === undefined) return absent("architecture_not_present"); + const name = toolName(selected); + const selector = architectureBaseName(name); + return available.some( + (candidate) => + candidate !== selected && + architectureBaseName(toolName(candidate)) === selector, + ) + ? ambiguous(selector) + : ok({ architecture: selected, toolName: name }); +}; diff --git a/src/native/NativeMachoInspection.test.ts b/src/native/NativeMachoInspection.test.ts index 95fba58825..a252396f9a 100644 --- a/src/native/NativeMachoInspection.test.ts +++ b/src/native/NativeMachoInspection.test.ts @@ -13,7 +13,8 @@ import { AnalysisOutputError, } from "../domain/analysisErrorCore.js"; import { err, ok } from "../domain/result.js"; -import { parseMachoLayout, selectMachoSlice } from "./AppleMachoSelection.js"; +import { layoutFor } from "./AppleMachoSelection.fixture.js"; +import { selectMachoSlice } from "./metadata/MachoSliceSelection.js"; import type { NativeCommandCapture } from "./CommandRunner.js"; import type { MachoSliceEncryption } from "../domain/apple/machoEncryption.js"; import { inspectMachoSchema } from "../domain/native/nativeInspection.js"; @@ -288,13 +289,19 @@ describe("Mach-O tool architecture selection", () => { offset: (index + 1) * 4096, size: slices[index]!.bytes.length, }; - expect(selectMachoSlice(fat, "arm64", admitted).slice).toBe( - admitted.offset, - ); - expect(() => selectMachoSlice(fat, "arm64")).toThrow( - /needs the admitted slice/u, - ); - expect(parseMachoLayout(fat, "arm64", admitted).fixups.dylibs).toEqual([ + const slice = (at?: typeof admitted) => + selectMachoSlice(fat, { + mode: "admitted", + architecture: "arm64", + slice: at, + }); + expect(slice(admitted)).toMatchObject({ + value: { fileOffset: admitted.offset }, + }); + expect(slice()).toMatchObject({ + error: { kind: "admitted_slice_mismatch" }, + }); + expect(layoutFor(fat, "arm64", admitted).fixups.dylibs).toEqual([ `/fixture/${selected}.dylib`, ]); }, diff --git a/src/native/NativeMachoInspection.ts b/src/native/NativeMachoInspection.ts index c10a9690b7..df5f509ffb 100644 --- a/src/native/NativeMachoInspection.ts +++ b/src/native/NativeMachoInspection.ts @@ -15,15 +15,16 @@ import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js"; import { machoRuntimeSymbolName } from "../domain/native/machoSymbolName.js"; import { keyedBy, uniqueKeyed } from "../domain/keyedCollection.js"; import { err, ok, type Result } from "../domain/result.js"; +import type { MachoSliceSelectionRefusal } from "./metadata/MachoSliceSelection.js"; import { NATIVE_COMMAND_OUTPUT_BUDGET_BYTES, type NativeCommandCapture, } from "./CommandRunner.js"; import { chainedImportNames } from "./AppleMachoFixups.js"; import { - MACHO_ARCHITECTURE_POLICIES, - machoToolArchitectureName, - selectMachoArchitecture, + architectureBaseName, + selectLipoArchitecture, + type SelectedLipoArchitecture, } from "./MachoArchitectureSelection.js"; import { parseDyldExportAddressRow, @@ -191,23 +192,13 @@ export const inspectNativeMacho = async ( ); if (!parsedLipo.ok) return parsedLipo; const { format, architectures } = parsedLipo.value; - const selection = selectArchitecture(context.target, architectures); - if (selection.status === "absent") + const lipoSelection = selectArchitecture(context.target, architectures); + if (!lipoSelection.ok) return err( - new AnalysisOutputError( - "inspect_macho", - `lipo did not list ${selection.architecture}; refusing to read another slice`, - ), + new AnalysisOutputError("inspect_macho", lipoSelection.error.message), ); - if (selection.status === "ambiguous") - return err( - new AnalysisOutputError( - "inspect_macho", - `lipo listed multiple slices addressable as ${selection.architecture}; refusing to associate a slice offset with an ambiguous tool selection`, - ), - ); - const selectedArchitecture = - selection.status === "selected" ? selection.toolName : null; + const selection = lipoSelection.value; + const selectedArchitecture = selection?.toolName ?? null; const runSliceCommand = ([tool, prefix]: readonly [ string, readonly string[], @@ -272,7 +263,7 @@ export const inspectNativeMacho = async ( if (nmIndex < 0 || dyldExports === undefined) throw new TypeError("Missing nm or dyld_info export capture"); const selected = - selection.status === "selected" + selection !== null ? selection.architecture : architectures.length === 1 ? architectures[0] @@ -400,7 +391,7 @@ export const inspectNativeMacho = async ( } const encryption = await context.readEncryption(context.signal); const selectedArchitectureName = - (selection.status === "selected" ? selection.architecture.name : null) ?? + selection?.architecture.name ?? (architectures.length === 1 ? (architectures[0]?.name ?? null) : null); const containerOffset = selectedArchitectureName === null ? undefined : offset; @@ -853,15 +844,6 @@ export const architectureLocations = ( return locations; }; -/** - * lipo appends the pointer-auth ABI version to arm64e (`arm64e.v1`). Only - * dyld_info accepts that spelling; the other Apple tools select the slice by - * its CPU-family name. Other variants are distinct subtypes (`arm64e.x1`) - * that the tools address only by their exact name. - */ -const architectureBaseName = (architecture: string): string => - architecture.replace(/\.v\d+$/u, ""); - const toolArchitectureName = (tool: string, architecture: string): string => tool === "dyld_info" ? architecture : architectureBaseName(architecture); @@ -876,68 +858,24 @@ const selectedArchitectureArguments = ( : ["-arch", toolArchitecture]; }; -type ArchitectureSelection = - | { readonly status: "unfiltered" } - | { - readonly status: "selected"; - readonly architecture: LipoArchitecture; - readonly toolName: string; - } - | { readonly status: "ambiguous"; readonly architecture: string } - | { readonly status: "absent"; readonly architecture: string }; - +/** The lipo record slice tools read; null when tools read the file unfiltered. */ const selectArchitecture = ( target: BinaryTarget, available: readonly LipoArchitecture[], -): ArchitectureSelection => { +): Result => // BinaryTarget lists CPU families and lipo lists slices. A universal file // can hold several slices of one family (arm64e and arm64e.x1), and slice // tools without -arch print every slice. - if ( - target.kind !== "executable" || - (target.availableArchitectures.length < 2 && available.length < 2) - ) - return { status: "unfiltered" }; - const normalized = - target.architecture === "x86" ? "i386" : target.architecture; - const policy = MACHO_ARCHITECTURE_POLICIES[target.architecture]; - if (target.format === "mach-o-universal") { - const admitted = available.find( - (candidate) => - candidate.file_offset === target.universalSlice.offset && - (candidate.size === null || - candidate.size === target.universalSlice.size), - ); - if (admitted === undefined) - return { status: "absent", architecture: normalized }; - const toolName = - policy === undefined - ? admitted.name - : machoToolArchitectureName(policy, admitted); - return { status: "selected", architecture: admitted, toolName }; - } - const selected = - policy === undefined - ? available.find(({ name }) => name === normalized) - : selectMachoArchitecture(policy, available); - if (selected !== undefined) { - const toolName = - policy === undefined - ? selected.name - : machoToolArchitectureName(policy, selected); - const selector = architectureBaseName(toolName); - if ( - available.some((candidate) => { - if (candidate === selected) return false; - const candidateName = - policy === undefined - ? candidate.name - : machoToolArchitectureName(policy, candidate); - return architectureBaseName(candidateName) === selector; - }) - ) - return { status: "ambiguous", architecture: selector }; - return { status: "selected", architecture: selected, toolName }; - } - return { status: "absent", architecture: normalized }; -}; + target.kind !== "executable" || + (target.availableArchitectures.length < 2 && available.length < 2) + ? ok(null) + : selectLipoArchitecture( + target.format === "mach-o-universal" + ? { + mode: "admitted", + architecture: target.architecture, + slice: target.universalSlice, + } + : { mode: "architecture", architecture: target.architecture }, + available, + ); diff --git a/src/native/initializers/MachoInitializers.ts b/src/native/initializers/MachoInitializers.ts index ace2914d68..4523c99e4d 100644 --- a/src/native/initializers/MachoInitializers.ts +++ b/src/native/initializers/MachoInitializers.ts @@ -19,6 +19,7 @@ import { type PointerFixups, } from "../AppleMachoFixups.js"; import { parseMachoLayout } from "../AppleMachoSelection.js"; +import { selectMachoSlice } from "../metadata/MachoSliceSelection.js"; import { decodeMachoName } from "../AppleMachoNames.js"; import { BinaryFormatFailure, @@ -102,8 +103,19 @@ const layoutFixups = async ( cpu === 0x0100000c ? "arm64" : cpu === 0x01000007 ? "x86_64" : null; if (bits !== 64 || !little || architecture === null) return { fixups: null, base: null }; + // The architecture is the scanned header's own, so the thin image is + // selected whole; a refusal is noted like any other undecoded fixup. + const selected = selectMachoSlice(scan.bytes, { + mode: "admitted", + architecture, + slice: undefined, + }); + if (!selected.ok) { + note(scan, selected.error.message); + return { fixups: null, base: null }; + } try { - const layout = parseMachoLayout(scan.bytes, architecture); + const layout = parseMachoLayout(scan.bytes, selected.value); const steps = parsePointerFixupsSteps( scan.bytes, layout.fixups, diff --git a/src/native/metadata/MachoSliceSelection.test.ts b/src/native/metadata/MachoSliceSelection.test.ts index 5b8758ef8d..b61341be36 100644 --- a/src/native/metadata/MachoSliceSelection.test.ts +++ b/src/native/metadata/MachoSliceSelection.test.ts @@ -2,7 +2,12 @@ import { describe, expect, it } from "vitest"; import type { MachoSliceArchitecture } from "../../domain/apple/machoTargetSelection.js"; import { machoArm64Initializers } from "../initializers/NativeInitializers.fixture.js"; -import { selectNativeImage } from "./MachoSliceSelection.js"; +import { selectMachoSlice } from "./MachoSliceSelection.js"; + +const selectNativeImage = ( + bytes: Buffer, + architecture: MachoSliceArchitecture | undefined, +) => selectMachoSlice(bytes, { mode: "architecture", architecture }); const CPU_ARM = 12; const CPU_X86_64 = 0x01000007; @@ -104,7 +109,7 @@ describe("selectNativeImage subtype names", () => { const arm64e = select(cpus, "arm64e"); // The refusal is the caller's to correct and names a selector that works, // which the next selection proves. - expect(arm64e.kind).toBe("architecture"); + expect(arm64e.kind).toBe("ambiguous_architecture"); expect(arm64e.offset).toBeUndefined(); expect(arm64e.message).toMatch(/Select architecture arm64\.$/u); const arm64 = select(cpus, "arm64"); @@ -123,7 +128,7 @@ describe("selectNativeImage subtype names", () => { ], "arm64e", ); - expect(ambiguous.kind).toBe("architecture"); + expect(ambiguous.kind).toBe("architecture_not_present"); expect(ambiguous.message).not.toMatch(/Select architecture/u); // A lone family slice is read by its family name, so the refusal names it. const lone: readonly (readonly [number, number])[] = [ @@ -131,7 +136,7 @@ describe("selectNativeImage subtype names", () => { [CPU_ARM64, 0], ]; const refused = select(lone, "arm64e"); - expect(refused.kind).toBe("architecture"); + expect(refused.kind).toBe("architecture_not_present"); expect(refused.message).toMatch(/Select architecture arm64\.$/u); expect(select(lone, "arm64").offset).toBeDefined(); }); diff --git a/src/native/metadata/MachoSliceSelection.ts b/src/native/metadata/MachoSliceSelection.ts index d2fff0dfde..cdceeae8fc 100644 --- a/src/native/metadata/MachoSliceSelection.ts +++ b/src/native/metadata/MachoSliceSelection.ts @@ -4,6 +4,10 @@ import { readUniversalMachoImage, readUniversalMachoSlice, } from "../../domain/apple/machoImageSelection.js"; +import { + readMachoHeaderPrefixFacts, + type FatSliceDeclaration, +} from "../../domain/apple/machoContainer.js"; import { MACHO_CPU_TYPES, machoSliceFamily, @@ -13,6 +17,7 @@ import { } from "../../domain/apple/machoTargetSelection.js"; import { AnalysisInputError } from "../../domain/analysisErrorCore.js"; import { err, ok, type Result } from "../../domain/result.js"; +import { MACHO_ARCHITECTURE_POLICIES } from "../MachoArchitectureSelection.js"; const cpuName = (cpuType: number, cpuSubtype: number): string => { const name = Object.entries(MACHO_CPU_TYPES).find( @@ -59,12 +64,49 @@ export const SHARED_SLICE_SELECTION_INPUT: SliceSelectionInput = { }, }; -/** Why no image could be selected; `architecture` issues are the caller's to correct. */ -export interface MachoSliceSelectionIssue { - readonly kind: "architecture" | "malformed"; +/** File range target admission chose. Readers must not rank slices again. */ +export interface AdmittedMachoSlice { + readonly offset: number; + readonly size: number; +} + +/** + * Which slice a reader asks for. `admitted` reads the image target admission + * bound to the session: a universal image only at the admitted range, which + * must be declared for the architecture's CPU family, and a thin image only + * when its header is of that family. `architecture` resolves the caller's + * selector against the universal table by CPU family and subtype. + */ +export type MachoSliceRequest = + | { + readonly mode: "admitted"; + readonly architecture: string; + readonly slice: AdmittedMachoSlice | undefined; + } + | { + readonly mode: "architecture"; + readonly architecture: MachoSliceArchitecture | undefined; + readonly input?: SliceSelectionInput; + }; + +/** + * A selection the request and the file do not agree on. The file may be + * well formed; the request names no slice in it, more than one slice, or a + * slice admission did not declare. + */ +export interface MachoSliceSelectionRefusal { + readonly kind: + | "architecture_not_present" + | "ambiguous_architecture" + | "admitted_slice_mismatch"; readonly message: string; } +/** Why no image could be selected: a refused request, or a malformed container. */ +export type MachoSliceSelectionFailure = + | MachoSliceSelectionRefusal + | { readonly kind: "malformed"; readonly message: string }; + interface FatSlice { readonly cpuType: number; readonly cpuSubtype: number; @@ -101,29 +143,141 @@ const addressedSlices = ( architecture, ); +type UniversalMachoHeader = NonNullable< + Extract, { ok: true }>["value"] +>; + /** - * Select the thin image to inspect without lipo. A universal Mach-O needs the - * caller's architecture. A family name selects the family's only slice; when - * several slices share the family, the value must name one slice's subtype - * exactly, and no slice is chosen by preference. Any other file is returned - * whole and rejects an architecture rather than ignoring it. + * Select the thin image one reader reads, without lipo, in the request's + * mode. The container is read once here for both modes, so a universal image + * is admitted by the same rules whichever reader asks; a refused request is a + * typed refusal, never a format failure. */ -export const selectNativeImage = ( +export const selectMachoSlice = ( bytes: Buffer, + request: MachoSliceRequest, +): Result => { + const container = readUniversalMachoImage(bytes); + if (!container.ok) return container; + return request.mode === "admitted" + ? selectAdmittedSlice(bytes, container.value, request) + : selectArchitectureSlice( + bytes, + container.value, + request.architecture, + request.input ?? SHARED_SLICE_SELECTION_INPUT, + ); +}; + +const selectAdmittedSlice = ( + bytes: Buffer, + fat: UniversalMachoHeader | null, + { + architecture, + slice: admitted, + }: Extract, +): Result => { + const cpuType = MACHO_ARCHITECTURE_POLICIES[architecture]?.cpuType; + if (fat === null && admitted !== undefined) + return err({ + kind: "admitted_slice_mismatch", + message: + "Admission bound a universal slice, but the file is not a universal Mach-O", + }); + if (fat === null) { + // The layout reader reports a truncated or unrecognized header, and a + // big-endian 64-bit image it decodes for no architecture. + const { identity, format } = readMachoHeaderPrefixFacts(bytes); + if ( + identity !== null && + format !== null && + bytes.length >= format.headerSize && + !(format.wide && !format.littleEndian) && + identity.cpuType !== cpuType + ) + return err({ + kind: "architecture_not_present", + message: `Mach-O CPU type does not match requested architecture ${architecture}`, + }); + return ok({ bytes, fileOffset: 0, selection: null, limitations: [] }); + } + if (cpuType === undefined) + return err({ + kind: "architecture_not_present", + message: `Universal Mach-O metadata does not read architecture ${architecture}`, + }); + // Target admission owns slice choice; ranking here again could read a + // different slice than the one the session's Evidence names. + if (admitted === undefined) + return err({ + kind: "admitted_slice_mismatch", + message: "Universal Mach-O metadata needs the admitted slice", + }); + // The container admits no overlapping ranges, so a range is declared once. + const declared = fat.slices.find( + ({ offset, size }) => offset === admitted.offset && size === admitted.size, + ); + if (declared === undefined) + return err({ + kind: "admitted_slice_mismatch", + message: "Admitted FAT slice is not declared", + }); + if (declared.cpuType !== cpuType) + return err({ + kind: "admitted_slice_mismatch", + message: "Admitted FAT slice does not match the requested architecture", + }); + return selectedFatImage(bytes, fat, declared, []); +}; + +/** Read and validate one declared slice, binding its container identity. */ +const selectedFatImage = ( + bytes: Buffer, + fat: UniversalMachoHeader, + slice: FatSliceDeclaration, + limitations: readonly string[], +): Result => { + const selected = readUniversalMachoSlice(bytes, fat, slice); + if (!selected.ok) return selected; + return ok({ + bytes: selected.value, + fileOffset: slice.offset, + selection: { + kind: "macho-selected-image-v1", + container: "fat", + offset: slice.offset, + byte_length: slice.size, + cpu_type: slice.cpuType, + cpu_subtype: slice.cpuSubtype, + container_table_sha256: createHash("sha256") + .update(bytes.subarray(0, fat.tableEnd)) + .digest("hex"), + }, + limitations, + }); +}; + +/** + * A universal Mach-O needs the caller's architecture. A family name selects + * the family's only slice; when several slices share the family, the value + * must name one slice's subtype exactly, and no slice is chosen by + * preference. Any other file is returned whole and rejects an architecture + * rather than ignoring it. + */ +const selectArchitectureSlice = ( + bytes: Buffer, + fat: UniversalMachoHeader | null, architecture: MachoSliceArchitecture | undefined, - input: SliceSelectionInput = SHARED_SLICE_SELECTION_INPUT, -): Result => { + input: SliceSelectionInput, +): Result => { const field = input.architectureField; const spelled = (value: MachoSliceArchitecture): string => input.spelling[value] ?? value; - const container = readUniversalMachoImage(bytes); - if (!container.ok) return container; - const fat = container.value; if (fat === null) return architecture === undefined ? ok({ bytes, fileOffset: 0, selection: null, limitations: [] }) : err({ - kind: "architecture", + kind: "architecture_not_present", message: `${field} ${spelled(architecture)} was selected, but the file is not a universal Mach-O; omit ${field}.`, }); const available = fat.slices @@ -131,7 +285,7 @@ export const selectNativeImage = ( .join(", "); if (architecture === undefined) return err({ - kind: "architecture", + kind: "ambiguous_architecture", message: `Universal Mach-O holds ${available}; select one architecture.`, }); const sameFamily = fat.slices.filter( @@ -168,7 +322,10 @@ export const selectNativeImage = ( ? "" : ` Select ${field} ${choices.map(spelled).join(" or ")}.`; return err({ - kind: "architecture", + kind: + matches.length > 1 + ? "ambiguous_architecture" + : "architecture_not_present", message: sameFamily.length === 0 ? `Universal Mach-O holds ${available}, not ${spelled(architecture)}.${select}` @@ -177,8 +334,6 @@ export const selectNativeImage = ( : `Universal Mach-O holds several ${spelled(architecture)} slices (${available}); no slice was selected.${select}`, }); } - const selected = readUniversalMachoSlice(bytes, fat, slice); - if (!selected.ok) return selected; const unread = sameFamily .filter((other) => other !== slice) .map((other) => { @@ -193,27 +348,16 @@ export const selectNativeImage = ( ); return `${place} (${selector === undefined ? `no ${field} value selects it` : `${field} ${spelled(selector)} selects it`})`; }); - return ok({ - bytes: selected.value, - fileOffset: slice.offset, - selection: { - kind: "macho-selected-image-v1", - container: "fat", - offset: slice.offset, - byte_length: slice.size, - cpu_type: slice.cpuType, - cpu_subtype: slice.cpuSubtype, - container_table_sha256: createHash("sha256") - .update(bytes.subarray(0, fat.tableEnd)) - .digest("hex"), - }, - limitations: - unread.length === 0 - ? [] - : [ - `Only the selected ${spelled(architecture)} slice (${cpuName(slice.cpuType, slice.cpuSubtype)} at file offset ${String(slice.offset)}) was read; this universal Mach-O also holds ${unread.join(", ")}.`, - ], - }); + return selectedFatImage( + bytes, + fat, + slice, + unread.length === 0 + ? [] + : [ + `Only the selected ${spelled(architecture)} slice (${cpuName(slice.cpuType, slice.cpuSubtype)} at file offset ${String(slice.offset)}) was read; this universal Mach-O also holds ${unread.join(", ")}.`, + ], + ); }; /** @@ -228,11 +372,15 @@ export const requireSelectedNativeImage = ( architecture: MachoSliceArchitecture | undefined, input: SliceSelectionInput = SHARED_SLICE_SELECTION_INPUT, ): SelectedNativeImage => { - const selected = selectNativeImage(bytes, architecture, input); + const selected = selectMachoSlice(bytes, { + mode: "architecture", + architecture, + input, + }); if (selected.ok) return selected.value; const { kind, message } = selected.error; throw new AnalysisInputError(operation, undefined, [ - kind === "architecture" + kind !== "malformed" ? { path: [input.architectureField], reason: "invalid_value", message } : { path: [input.pathField], diff --git a/tests/boundary/providers/native/objcClassMetadataFallback.test.ts b/tests/boundary/providers/native/objcClassMetadataFallback.test.ts index 6f6d5cff50..c14545787e 100644 --- a/tests/boundary/providers/native/objcClassMetadataFallback.test.ts +++ b/tests/boundary/providers/native/objcClassMetadataFallback.test.ts @@ -10,7 +10,7 @@ import type { } from "../../../../src/application/AnalysisProvider.js"; import { createAnalysisExecution } from "../../../../src/application/AnalysisProvider.js"; import { EnhancedTools } from "../../../../src/application/EnhancedTools.js"; -import { parseMachoLayout } from "../../../../src/native/AppleMachoSelection.js"; +import { layoutFor } from "../../../../src/native/AppleMachoSelection.fixture.js"; import { NativeMacOSProvider } from "../../../../src/native/NativeMacOSProvider.js"; import { BinaryFormatFailure } from "../../../../src/native/metadata/BinaryContainer.js"; import { NativeFixtureRunner } from "../../../fixtures/nativeCommands.js"; @@ -114,7 +114,7 @@ describe("native Objective-C class metadata capability fallback", () => { bytes.writeUInt32LE(2, 12); let failure: unknown; try { - parseMachoLayout(bytes, "x86"); + layoutFor(bytes, "x86"); } catch (cause: unknown) { failure = cause; } @@ -160,7 +160,7 @@ describe("native Objective-C class metadata capability fallback", () => { bytes.writeUInt32BE(2, 12); let failure: unknown; try { - parseMachoLayout(bytes, "powerpc"); + layoutFor(bytes, "powerpc"); } catch (cause: unknown) { failure = cause; } @@ -173,7 +173,7 @@ describe("native Objective-C class metadata capability fallback", () => { bytes.writeUInt32BE(0x01000007, 4); let mismatchedCpuFailure: unknown; try { - parseMachoLayout(bytes, "powerpc"); + layoutFor(bytes, "powerpc"); } catch (cause: unknown) { mismatchedCpuFailure = cause; } diff --git a/tests/fixtures/apkSignature/v1SignatureBlock.ts b/tests/fixtures/apkSignature/v1SignatureBlock.ts index f9914522bf..4412b80e0d 100644 --- a/tests/fixtures/apkSignature/v1SignatureBlock.ts +++ b/tests/fixtures/apkSignature/v1SignatureBlock.ts @@ -220,6 +220,55 @@ export const withExtension = ( ); }; +/** The certificate with its `extensions [3]` field declared twice. */ +export const withRepeatedExtensions = (certificate: Uint8Array): Uint8Array => { + const { fields, outer } = certificateParts(certificate); + const part = ({ start, end }: DerSpan) => certificate.subarray(start, end); + return tlv( + 0x30, + tlv( + 0x30, + ...fields.flatMap((field) => + certificate[field.start] === 0xa3 + ? [part(field), part(field)] + : [part(field)], + ), + ), + ...outer.map(part), + ); +}; + +/** + * The certificate with its one `extensions [3]` field replaced by a + * subjectKeyIdentifier extension for each key identifier, in order. + */ +export const withSubjectKeyIdentifiers = ( + certificate: Uint8Array, + ...keyIdentifiers: readonly Uint8Array[] +): Uint8Array => { + const { fields, outer } = certificateParts(certificate); + const part = ({ start, end }: DerSpan) => certificate.subarray(start, end); + const extensions = tlv( + 0xa3, + tlv( + 0x30, + ...keyIdentifiers.map((keyIdentifier) => + tlv(0x30, oid("2.5.29.14"), tlv(0x04, tlv(0x04, keyIdentifier))), + ), + ), + ); + return tlv( + 0x30, + tlv( + 0x30, + ...fields.map((field) => + certificate[field.start] === 0xa3 ? extensions : part(field), + ), + ), + ...outer.map(part), + ); +}; + export interface SignerInfoSpec { /** Certificate whose issuer and serial the SignerInfo names. */ readonly names: Uint8Array;