fix(apple): validate cache file kinds and combined VM ranges
This commit is contained in:
@@ -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. */
|
||||
|
||||
@@ -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<CacheFile> {
|
||||
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()));
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user