From b23f8288b4374d06e6900bf5fb6ac70ac74fab17 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 8 Oct 2026 20:52:20 +0800 Subject: [PATCH] fix(apple): validate cache file kinds and combined VM ranges --- .../apple/DyldSharedCacheService.ts | 14 ++++++- src/artifacts/apple/DyldSharedCacheReader.ts | 37 +++++++++++++++++-- .../filesystem/dyldSharedCache.test.ts | 24 ++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/application/apple/DyldSharedCacheService.ts b/src/application/apple/DyldSharedCacheService.ts index ad312b6db..086c1927a 100644 --- a/src/application/apple/DyldSharedCacheService.ts +++ b/src/application/apple/DyldSharedCacheService.ts @@ -68,6 +68,13 @@ const failure = (cause: unknown): AnalysisError => { cause.message, ); if (cause instanceof Error && "code" in cause) { + if (cause.code === "EISDIR") + return new ArtifactOperationError( + OPERATION, + "path", + undefined, + cause.message, + ); if (cause.code === "ENOENT" || cause.code === "ENOTDIR") return new ArtifactOperationError( OPERATION, @@ -85,7 +92,12 @@ const failure = (cause: unknown): AnalysisError => { } if (cause instanceof Error && cause.name === "AbortError") return new ArtifactOperationError(OPERATION, "cancelled"); - return new ArtifactOperationError(OPERATION, "io"); + return new ArtifactOperationError( + OPERATION, + "io", + undefined, + cause instanceof Error ? cause.message : String(cause), + ); }; /** Inspect a dyld shared cache file set selected by path. */ diff --git a/src/artifacts/apple/DyldSharedCacheReader.ts b/src/artifacts/apple/DyldSharedCacheReader.ts index 195bf6354..c46d54af7 100644 --- a/src/artifacts/apple/DyldSharedCacheReader.ts +++ b/src/artifacts/apple/DyldSharedCacheReader.ts @@ -1,4 +1,5 @@ import { createHash } from "node:crypto"; +import { constants } from "node:fs"; import { open, realpath, type FileHandle } from "node:fs/promises"; import { applePlatform } from "../../domain/apple/applePlatforms.js"; @@ -75,9 +76,17 @@ class CacheFile { ) {} static async open(path: string, suffix: string): Promise { - const handle = await open(path, "r"); + const handle = await open( + await realpath(path), + constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK, + ); try { const metadata = await handle.stat({ bigint: true }); + if (!metadata.isFile()) + throw new ArtifactReaderFailure( + "path", + `Dyld cache path is not a regular file: ${path}`, + ); return new CacheFile( suffix, handle, @@ -372,21 +381,43 @@ export class DyldSharedCache { "mapping file offset", ); const size = safeNumber(mapping.size, "mapping size"); + const address = safeNumber(mapping.address, "mapping address"); // A truncated file that declares mappings beyond its bytes cannot // map those addresses; drop the unverified extent. - if (fileOffset < 0 || size < 0 || fileOffset + size > file.size) + if ( + fileOffset < 0 || + size <= 0 || + fileOffset + size > file.size || + !Number.isSafeInteger(address + size) + ) throw new ArtifactReaderFailure( "format", `dyld cache mapping in ${file.suffix === "" ? "the main file" : `subcache ${file.suffix}`} extends beyond its file`, ); return { file, - address: safeNumber(mapping.address, "mapping address"), + address, size, fileOffset, }; }), ); + const ordered = regions.toSorted( + (left, right) => left.address - right.address, + ); + for (let index = 1; index < ordered.length; index++) { + const previous = ordered[index - 1]; + const current = ordered[index]; + if ( + previous !== undefined && + current !== undefined && + current.address < previous.address + previous.size + ) + throw new ArtifactReaderFailure( + "format", + `Dyld cache VM mappings overlap at ${hex(current.address)} between ${previous.file.suffix || "main"} and ${current.file.suffix || "main"}`, + ); + } return new DyldSharedCache(header, images, files, regions); } catch (cause: unknown) { await Promise.allSettled(files.map(({ handle }) => handle.close())); diff --git a/tests/boundary/filesystem/dyldSharedCache.test.ts b/tests/boundary/filesystem/dyldSharedCache.test.ts index 88a629660..196d4ef04 100644 --- a/tests/boundary/filesystem/dyldSharedCache.test.ts +++ b/tests/boundary/filesystem/dyldSharedCache.test.ts @@ -24,6 +24,30 @@ import { dyldSharedCacheResultSchema } from "../../../src/domain/apple/dyldShare import { traceDylibResolution } from "../../../src/artifacts/apple/DylibResolutionReader.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +it("rejects a directory and overlapping combined VM mappings with specific reasons", async () => { + const directory = await createTestTempDirectory("rea-cache-invalid-regions-"); + expect(await inspectDyldSharedCache({ cache_path: directory })).toMatchObject( + { ok: false, error: { reason: "path" } }, + ); + const fixture = dyldCacheFixture(IMAGES); + const original = fixture.subcaches[0]; + if (original === undefined) throw new Error("missing subcache fixture"); + const subcache = Buffer.from(original.bytes); + const main = Buffer.from(fixture.main); + const mainTable = main.readUInt32LE(0x10); + const subTable = subcache.readUInt32LE(0x10); + subcache.writeBigUInt64LE(main.readBigUInt64LE(mainTable), subTable); + const path = await writeCache({ + ...fixture, + subcaches: [{ ...original, bytes: subcache }], + }); + const result = await inspectDyldSharedCache({ cache_path: path }); + expect(result).toMatchObject({ + ok: false, + error: { reason: "format", detail: expect.stringContaining("overlap") }, + }); +}); + const dylib = (installName: string, dependencies: readonly Uint8Array[] = []) => machoImage({ fileType: FILE_TYPE.dylib,