feat(native): admit raw binaries with caller-supplied architecture and entry (#1868)
* feat(native): admit raw binaries with caller-supplied architecture and entry * fix(native): honor raw-binary preparation and entry identity * fix(ghidra): stream raw snapshot attestation with cancellation * fix(native): preserve and validate raw lifecycle entries --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
+96
-74
@@ -1,4 +1,6 @@
|
||||
import { randomBytes, randomUUID } from "node:crypto";
|
||||
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
||||
import type { Stats } from "node:fs";
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
import type { Socket } from "node:net";
|
||||
import type { ProgressReporter } from "../application/ProgressReporter.js";
|
||||
|
||||
@@ -43,11 +45,12 @@ import {
|
||||
type GhidraSessionInfo,
|
||||
} from "./GhidraSessionValues.js";
|
||||
import { createGhidraTargetSnapshot } from "./GhidraTargetSnapshot.js";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { readFileHandleChunks } from "../filesystem/readFileHandleChunks.js";
|
||||
import { readBoundedFileBytes } from "../process/BoundedFileBytes.js";
|
||||
import {
|
||||
openRegularFile,
|
||||
RegularFileChangedError,
|
||||
RegularFileAdmissionFailure,
|
||||
sameRegularFileState,
|
||||
} from "../filesystem/RegularFile.js";
|
||||
import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
|
||||
@@ -286,12 +289,66 @@ export class GhidraClient {
|
||||
);
|
||||
}
|
||||
|
||||
/** Read the private immutable source for independent import verification. */
|
||||
async readTargetSnapshot(
|
||||
/** Read the private immutable source for format parsers that require captured bytes. */
|
||||
readTargetSnapshot(
|
||||
maximumBytes?: number,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<GhidraTargetSnapshot, GhidraSessionError>> {
|
||||
if (this.#snapshotPath === undefined)
|
||||
return this.#withTargetSnapshot(async (handle, initial, path) => {
|
||||
if (
|
||||
maximumBytes !== undefined &&
|
||||
(!Number.isSafeInteger(maximumBytes) || maximumBytes < 0)
|
||||
)
|
||||
throw new RangeError("Snapshot byte limit must be a safe integer");
|
||||
if (maximumBytes !== undefined && initial.size > maximumBytes)
|
||||
return {
|
||||
kind: "over-capacity",
|
||||
sourceBytesAtLeast: initial.size,
|
||||
maximumBytes,
|
||||
};
|
||||
const bytes = await readBoundedFileBytes(
|
||||
handle,
|
||||
maximumBytes ?? initial.size,
|
||||
signal,
|
||||
initial.size,
|
||||
);
|
||||
if (bytes === undefined || bytes.length !== initial.size)
|
||||
throw new RegularFileChangedError(path);
|
||||
return { kind: "captured", bytes };
|
||||
}, signal);
|
||||
}
|
||||
|
||||
/** Measure the complete snapshot identity without retaining its bytes in memory. */
|
||||
hashTargetSnapshot(
|
||||
signal?: AbortSignal,
|
||||
): Promise<
|
||||
Result<
|
||||
{ readonly sha256: string; readonly bytes: number },
|
||||
GhidraSessionError
|
||||
>
|
||||
> {
|
||||
return this.#withTargetSnapshot(async (handle, initial, path) => {
|
||||
const hash = createHash("sha256");
|
||||
let bytes = 0;
|
||||
for await (const chunk of readFileHandleChunks(handle, {
|
||||
end: initial.size,
|
||||
signal,
|
||||
})) {
|
||||
bytes += chunk.length;
|
||||
if (bytes > initial.size) throw new RegularFileChangedError(path);
|
||||
hash.update(chunk);
|
||||
}
|
||||
if (bytes !== initial.size) throw new RegularFileChangedError(path);
|
||||
return { sha256: hash.digest("hex"), bytes };
|
||||
}, signal);
|
||||
}
|
||||
|
||||
async #withTargetSnapshot<T>(
|
||||
read: (handle: FileHandle, initial: Stats, path: string) => Promise<T>,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<T, GhidraSessionError>> {
|
||||
const path = this.#snapshotPath;
|
||||
if (path === undefined)
|
||||
return err(
|
||||
this.#failure(
|
||||
"protocol",
|
||||
@@ -299,77 +356,42 @@ export class GhidraClient {
|
||||
),
|
||||
);
|
||||
try {
|
||||
if (maximumBytes !== undefined) {
|
||||
signal?.throwIfAborted();
|
||||
if (!Number.isSafeInteger(maximumBytes) || maximumBytes < 0)
|
||||
throw new RangeError("Snapshot byte limit must be a safe integer");
|
||||
const handle = await openRegularFile(this.#snapshotPath, {
|
||||
symlinks: "reject",
|
||||
});
|
||||
const owner = new OwnedFileHandle(handle);
|
||||
this.#snapshotOwners.add(owner);
|
||||
let result: GhidraTargetSnapshot;
|
||||
let readFailure: unknown;
|
||||
try {
|
||||
const metadata = await handle.stat();
|
||||
signal?.throwIfAborted();
|
||||
if (metadata.size > maximumBytes)
|
||||
result = {
|
||||
kind: "over-capacity",
|
||||
sourceBytesAtLeast: metadata.size,
|
||||
maximumBytes,
|
||||
};
|
||||
else {
|
||||
const bytes = await readBoundedFileBytes(
|
||||
handle,
|
||||
maximumBytes,
|
||||
signal,
|
||||
metadata.size,
|
||||
);
|
||||
signal?.throwIfAborted();
|
||||
const after = await handle.stat();
|
||||
if (
|
||||
bytes !== undefined &&
|
||||
bytes.length === metadata.size &&
|
||||
sameRegularFileState(metadata, after)
|
||||
) {
|
||||
result = { kind: "captured", bytes };
|
||||
} else if (
|
||||
bytes === undefined &&
|
||||
after.isFile() &&
|
||||
after.size > maximumBytes
|
||||
) {
|
||||
result = {
|
||||
kind: "over-capacity",
|
||||
sourceBytesAtLeast: after.size,
|
||||
maximumBytes,
|
||||
};
|
||||
} else {
|
||||
throw new RegularFileChangedError(this.#snapshotPath);
|
||||
}
|
||||
}
|
||||
} catch (cause: unknown) {
|
||||
readFailure = cause;
|
||||
}
|
||||
try {
|
||||
await owner.close();
|
||||
this.#snapshotOwners.delete(owner);
|
||||
} catch (closeFailure: unknown) {
|
||||
throw new AggregateError(
|
||||
readFailure === undefined
|
||||
? [closeFailure]
|
||||
: [readFailure, closeFailure],
|
||||
"Ghidra target snapshot read or descriptor cleanup failed",
|
||||
);
|
||||
}
|
||||
if (readFailure !== undefined) throw readFailure;
|
||||
return ok(result!);
|
||||
}
|
||||
return ok({
|
||||
kind: "captured",
|
||||
bytes: await readFile(this.#snapshotPath),
|
||||
const handle = await openRegularFile(path, {
|
||||
symlinks: "reject",
|
||||
signal,
|
||||
});
|
||||
const owner = new OwnedFileHandle(handle);
|
||||
this.#snapshotOwners.add(owner);
|
||||
let result: Result<T, unknown>;
|
||||
try {
|
||||
const initial = await handle.stat();
|
||||
signal?.throwIfAborted();
|
||||
if (!Number.isSafeInteger(initial.size) || initial.size < 0)
|
||||
throw new RangeError("Snapshot size must be a safe integer");
|
||||
const value = await read(handle, initial, path);
|
||||
signal?.throwIfAborted();
|
||||
const after = await handle.stat();
|
||||
signal?.throwIfAborted();
|
||||
if (!sameRegularFileState(initial, after))
|
||||
throw new RegularFileChangedError(path);
|
||||
result = ok(value);
|
||||
} catch (cause: unknown) {
|
||||
result = err(cause);
|
||||
}
|
||||
try {
|
||||
await owner.close();
|
||||
this.#snapshotOwners.delete(owner);
|
||||
} catch (closeFailure: unknown) {
|
||||
throw new AggregateError(
|
||||
result.ok ? [closeFailure] : [result.error, closeFailure],
|
||||
"Ghidra target snapshot read or descriptor cleanup failed",
|
||||
);
|
||||
}
|
||||
if (!result.ok) throw result.error;
|
||||
return result;
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof RegularFileAdmissionFailure)
|
||||
this.#snapshotOwners.add(cause.owner);
|
||||
return err(
|
||||
this.#failure(
|
||||
signal?.aborted === true ? "cancelled" : "protocol",
|
||||
|
||||
Reference in New Issue
Block a user