diff --git a/src/application/apple/DyldSharedCacheService.ts b/src/application/apple/DyldSharedCacheService.ts index 5cbe92390b..1216d01908 100644 --- a/src/application/apple/DyldSharedCacheService.ts +++ b/src/application/apple/DyldSharedCacheService.ts @@ -1,5 +1,3 @@ -import { createHash } from "node:crypto"; -import { open } from "node:fs/promises"; import { resolve } from "node:path"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; @@ -20,27 +18,6 @@ import { err, ok, type Result } from "../../domain/result.js"; import { APPLE_APPLICATION_PROVIDER } from "../InvestigationProviders.js"; const OPERATION = "inspect_dyld_shared_cache" as const; -const HASH_CHUNK_BYTES = 1024 * 1024; - -const sha256File = async ( - path: string, - signal?: AbortSignal, -): Promise => { - const handle = await open(path, "r"); - try { - const hash = createHash("sha256"); - const buffer = new Uint8Array(HASH_CHUNK_BYTES); - for (;;) { - signal?.throwIfAborted(); - const { bytesRead } = await handle.read(buffer, 0, buffer.length, null); - if (bytesRead === 0) break; - hash.update(buffer.subarray(0, bytesRead)); - } - return hash.digest("hex"); - } finally { - await handle.close(); - } -}; /** Read one requested image through the cache, keeping absence explicit. */ const inspectImage = async ( @@ -134,7 +111,8 @@ export const inspectDyldSharedCache = async ( return ok( dyldSharedCacheResultSchema.parse({ cache_path: cachePath, - main_file_sha256: await sha256File(cachePath, signal), + // The header and image list were read through this same handle. + main_file_sha256: await cache.mainSha256(signal), ...cache.header, images_total: cache.images.length, images: cache.images.map(({ path, address }) => ({ diff --git a/src/artifacts/apple/DyldSharedCacheReader.ts b/src/artifacts/apple/DyldSharedCacheReader.ts index 2b6cb119ec..93cb7215fb 100644 --- a/src/artifacts/apple/DyldSharedCacheReader.ts +++ b/src/artifacts/apple/DyldSharedCacheReader.ts @@ -1,3 +1,4 @@ +import { createHash } from "node:crypto"; import { open, type FileHandle } from "node:fs/promises"; import { applePlatform } from "../../domain/apple/applePlatforms.js"; @@ -19,6 +20,7 @@ const MAX_MAPPINGS = 64; const MAX_SUBCACHES = 256; const MAX_PATH_BYTES = 4096; const BLOCK_BYTES = 64 * 1024; +const HASH_CHUNK_BYTES = 1024 * 1024; /** Byte offsets of `dyld_cache_header` fields in Apple's open-source dyld. */ const FIELD = { @@ -465,6 +467,28 @@ export class DyldSharedCache { return { file: region.file.suffix, facts }; } + /** Digest of the main file through the handle its header was parsed from. */ + async mainSha256(signal?: AbortSignal): Promise { + const main = this.files[0]; + if (main === undefined) + throw new ArtifactReaderFailure("format", "dyld cache has no main file"); + const hash = createHash("sha256"); + const buffer = Buffer.alloc(HASH_CHUNK_BYTES); + for (let position = 0; position < main.size;) { + signal?.throwIfAborted(); + const { bytesRead } = await main.handle.read( + buffer, + 0, + Math.min(buffer.length, main.size - position), + position, + ); + if (bytesRead === 0) break; + hash.update(buffer.subarray(0, bytesRead)); + position += bytesRead; + } + return hash.digest("hex"); + } + async close(): Promise { await Promise.allSettled(this.files.map(({ handle }) => handle.close())); }