fix(apple): hash the dyld cache through the handle its header was read from

main_file_sha256 now comes from the main cache file's open handle, so a path
replaced during inspection cannot attach another file's digest to the parsed
header and image list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822andClaude Opus 5.5 committed 2026-10-07 13:35:58 +00:00
1 parent ed40d2e001
commit 858aa9fd7d
2 files changed
+26 -24

No files matched your search

@@ -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<string> => {
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 }) => ({
@@ -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<string> {
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<void> {
await Promise.allSettled(this.files.map(({ handle }) => handle.close()));
}