mirror of
https://github.com/morluto/rea.git
synced 2026-12-01 11:01:11 +00:00
* fix(native)!: report details.limit for native reader per-call budgets Devicetree, Windows shortcut, kernel build config, firmware staging and byte-entropy refusals of caller-selected budgets named their control only in remediation prose. Each now sets details.limit from a domain-owned budget object that also builds the schema (src/domain/parameterBudget.ts), so the reported range is the one the next call accepts. inspect_devicetree's fixed 32 MiB decoded representation allowance becomes the per-call max_decoded_bytes (default 32 MiB, 1 KiB to 256 MiB); its refusal reports boundary max_decoded_bytes instead of decoded_bytes. The pwntools allocation failure declares measuredExhaustion, the firmware inherited RLIMIT_FSIZE refusal declares measuredExhaustion, and the LLDB combined-result ceiling is a registered representation bound. Part of #14234 and #14235. BREAKING CHANGE: inspect_devicetree's decoded-representation refusal reports reported_limits.boundary "max_decoded_bytes" with details.limit, instead of "decoded_bytes". * fix(native): keep FIT decoded-budget boundary, LLDB narrowing and entropy length range exact The FIT reader compared the renamed devicetree limit against decoded_bytes, so a decoded-budget refusal became capacity; it now checks max_decoded_bytes, with a test. The LLDB representation ceiling keeps a short name and carries its recovery as narrowing. The byte-entropy length maximum is the window the selected block_size admits. * fix(cli): describe inspect-devicetree --max-decoded-bytes * refactor(native): let the generated adjustable-budget recovery name the native reader controls With #14625 the refusal owns per-call recovery, so the devicetree, Windows shortcut, kernel build config, firmware staging and byte-entropy refusals drop their authored sentences. inspect_byte_entropy reports the smallest admitting block_size as required_value.
572 lines
19 KiB
TypeScript
572 lines
19 KiB
TypeScript
import { safeParseUtf8Json } from "../domain/safeJson.js";
|
|
import { createHash } from "node:crypto";
|
|
import { createWriteStream, type Stats } from "node:fs";
|
|
import { chmod, lstat, opendir, readlink } from "node:fs/promises";
|
|
import { isAbsolute, join, relative, sep } from "node:path";
|
|
import { Readable, Transform } from "node:stream";
|
|
import { pipeline } from "node:stream/promises";
|
|
import {
|
|
isAborted,
|
|
AnalysisAccessDeniedError,
|
|
AnalysisResourceConstraintError,
|
|
AnalysisInputError,
|
|
AnalysisOutputError,
|
|
} from "../domain/analysisErrorCore.js";
|
|
import type { FirmwareRequest } from "../domain/firmware/firmwareAnalysis.js";
|
|
import { firmwareStagingBudgetLimit } from "../domain/firmware/firmwareAnalysis.js";
|
|
import { absoluteCallerPath } from "../filesystem/callerPath.js";
|
|
import { readLinuxResourceLimits } from "../process/LinuxResourceLimits.js";
|
|
import { readBoundedFileBytes } from "../process/BoundedFileBytes.js";
|
|
import { FIRMWARE_LIMITS } from "./FirmwareRelease.js";
|
|
import {
|
|
firmwareLimitRecovery,
|
|
firmwareSignalInterruption,
|
|
} from "./FirmwareFailures.js";
|
|
import type { FirmwareDeadline } from "./FirmwareDeadline.js";
|
|
import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js";
|
|
import {
|
|
type SafeOutputTree,
|
|
readArtifactFileChunks,
|
|
ArtifactResourceScope,
|
|
withArtifactRegularFile,
|
|
assertArtifactReadActive,
|
|
mapArtifactFileFailure,
|
|
artifactSelectedPathErrno,
|
|
} from "../resources/index.js";
|
|
import { decodeDirectoryEntryName } from "../filesystem/decodeDirectoryEntryName.js";
|
|
import { NonRegularFileReadError } from "../filesystem/RegularFile.js";
|
|
import { observedSymlinkTarget } from "../domain/symlinkTarget.js";
|
|
|
|
/** One regular file or unpublished filesystem entry in a private extraction. */
|
|
export interface FirmwareEntry {
|
|
readonly relativePath: string;
|
|
readonly path: string;
|
|
readonly size: number;
|
|
readonly kind: "file" | "symlink" | "special";
|
|
readonly linkTarget: string | null;
|
|
readonly linkTargetHex?: string;
|
|
}
|
|
|
|
/** An aborted inventory reports its phase deadline's timeout or its caller's cancellation. */
|
|
export const throwIfFirmwareInventoryCancelled = (
|
|
signal: AbortSignal | undefined,
|
|
operation: string,
|
|
deadline: FirmwareDeadline | undefined,
|
|
): void => {
|
|
if (isAborted(signal))
|
|
throw firmwareSignalInterruption(operation, signal, deadline);
|
|
};
|
|
|
|
/** Own all firmware reads, including rejected admission and final descriptor cleanup. */
|
|
export class FirmwareFileAccess {
|
|
readonly resources = new ArtifactResourceScope();
|
|
|
|
/** Retry retained descriptors without closing admission for future operations. */
|
|
retryCleanup(): Promise<void> {
|
|
return this.resources.retryCleanup();
|
|
}
|
|
|
|
/** Wait for admitted operations and retry every retained descriptor. */
|
|
close(): Promise<void> {
|
|
return this.resources.close();
|
|
}
|
|
|
|
/** Read inherited file policy through this same retryable descriptor owner. */
|
|
async fileSizeCeiling(signal?: AbortSignal): Promise<number> {
|
|
return this.resources.run(async () => {
|
|
assertArtifactReadActive(signal);
|
|
const limits = await readLinuxResourceLimits(
|
|
{
|
|
address_space_bytes: FIRMWARE_LIMITS.addressSpaceBytes,
|
|
cpu_seconds: FIRMWARE_LIMITS.cpuSeconds,
|
|
file_bytes: Number.MAX_SAFE_INTEGER,
|
|
},
|
|
this.resources,
|
|
);
|
|
assertArtifactReadActive(signal);
|
|
return limits.file_bytes;
|
|
});
|
|
}
|
|
|
|
/** Hash one admitted regular file without retaining its bytes in memory. */
|
|
hash(path: string, signal?: AbortSignal): Promise<string> {
|
|
return this.resources.run(() => this.#hash(path, signal));
|
|
}
|
|
|
|
async #hash(path: string, signal?: AbortSignal): Promise<string> {
|
|
return withArtifactRegularFile(
|
|
path,
|
|
this.resources,
|
|
signal,
|
|
async ({ handle }) => {
|
|
const hash = createHash("sha256");
|
|
for await (const chunk of readArtifactFileChunks(handle, { signal })) {
|
|
assertArtifactReadActive(signal);
|
|
hash.update(chunk);
|
|
}
|
|
assertArtifactReadActive(signal);
|
|
return hash.digest("hex");
|
|
},
|
|
);
|
|
}
|
|
|
|
/** Copy and identify the exact bytes consumed by firmware providers. */
|
|
snapshot(request: FirmwareRequest, root: string, signal: AbortSignal) {
|
|
return this.resources.run(() => this.#snapshot(request, root, signal));
|
|
}
|
|
|
|
async #snapshot(request: FirmwareRequest, root: string, signal: AbortSignal) {
|
|
const sourcePath = absoluteCallerPath(request.input.path);
|
|
let sourceAdmitted = false;
|
|
const fullPath = join(root, "firmware.bin");
|
|
const copied = await withArtifactRegularFile(
|
|
sourcePath,
|
|
this.resources,
|
|
signal,
|
|
async ({ handle, initial: stat }) => {
|
|
sourceAdmitted = true;
|
|
if (stat.size === 0 || stat.size > FIRMWARE_LIMITS.inputBytes)
|
|
throw firmwareInputFailure(request.operation);
|
|
const hash = createHash("sha256");
|
|
let size = 0;
|
|
const meter = new Transform({
|
|
transform(chunk: Buffer, _encoding, callback) {
|
|
size += chunk.length;
|
|
if (size > FIRMWARE_LIMITS.inputBytes) {
|
|
callback(firmwareInputFailure(request.operation));
|
|
return;
|
|
}
|
|
hash.update(chunk);
|
|
callback(null, chunk);
|
|
},
|
|
});
|
|
await pipeline(
|
|
readArtifactFileChunks(handle, { signal }),
|
|
meter,
|
|
createWriteStream(fullPath, { flags: "wx", mode: 0o600 }),
|
|
);
|
|
return { size, sha256: hash.digest("hex") };
|
|
},
|
|
"follow",
|
|
).catch((cause: unknown) => {
|
|
// Destination writes and reads after admission retain their own failure.
|
|
throw sourceAdmitted
|
|
? cause
|
|
: firmwareSourceAdmissionFailure(request.operation, sourcePath, cause);
|
|
});
|
|
const { size, sha256 } = copied;
|
|
await chmod(fullPath, 0o400);
|
|
const selected =
|
|
request.operation === "extract_firmware"
|
|
? request.input.range
|
|
: undefined;
|
|
const offset = selected?.offset ?? 0;
|
|
const length = selected?.length ?? size;
|
|
if (length === 0 || offset > size || length > size - offset)
|
|
throw new AnalysisInputError(request.operation, undefined, [
|
|
{
|
|
path: ["range"],
|
|
reason: "out_of_range",
|
|
message: `Selected range must be within the ${size}-byte firmware file`,
|
|
},
|
|
]);
|
|
let inputPath = fullPath;
|
|
let selectionSha256 = sha256;
|
|
if (selected !== undefined) {
|
|
inputPath = join(root, "selected.bin");
|
|
selectionSha256 = await withArtifactRegularFile(
|
|
fullPath,
|
|
this.resources,
|
|
signal,
|
|
async ({ handle }) => {
|
|
const selectionHash = createHash("sha256");
|
|
const hasher = new Transform({
|
|
transform(chunk: Buffer, _encoding, callback) {
|
|
selectionHash.update(chunk);
|
|
callback(null, chunk);
|
|
},
|
|
});
|
|
await pipeline(
|
|
readArtifactFileChunks(handle, {
|
|
start: offset,
|
|
end: offset + length - 1,
|
|
signal,
|
|
}),
|
|
hasher,
|
|
createWriteStream(inputPath, { flags: "wx", mode: 0o600 }),
|
|
);
|
|
return selectionHash.digest("hex");
|
|
},
|
|
);
|
|
await chmod(inputPath, 0o400);
|
|
}
|
|
return {
|
|
subject: { path: sourcePath, sha256, format: "file" as const },
|
|
size,
|
|
inputPath,
|
|
selection: {
|
|
offset,
|
|
length,
|
|
sha256: selectionSha256,
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Own the exact source descriptor consumed by publication through its final close. */
|
|
publish(
|
|
entry: FirmwareEntry,
|
|
sha256: string | null | undefined,
|
|
tree: SafeOutputTree,
|
|
signal: AbortSignal,
|
|
) {
|
|
return this.resources.run(() =>
|
|
withArtifactRegularFile(
|
|
entry.path,
|
|
this.resources,
|
|
signal,
|
|
({ handle }) =>
|
|
tree.write(
|
|
entry.relativePath,
|
|
// SafeOutputTree awaits destination setup before consuming. Defer
|
|
// the abort-bound descriptor stream until it has a consumer.
|
|
Readable.from(
|
|
(async function* () {
|
|
assertArtifactReadActive(signal);
|
|
yield* readArtifactFileChunks(handle, { signal });
|
|
})(),
|
|
),
|
|
{
|
|
bytes: entry.size,
|
|
...(sha256 === undefined || sha256 === null ? {} : { sha256 }),
|
|
},
|
|
signal,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Read a complete bounded report; oversized data is never reported as complete.
|
|
* Each refusal records its observed report state, so an absent or empty report
|
|
* is never conflated with an empty scan or a malformed one.
|
|
*/
|
|
async report(
|
|
path: string,
|
|
operation: string,
|
|
signal?: AbortSignal,
|
|
): Promise<unknown> {
|
|
return this.resources.run(() =>
|
|
withArtifactRegularFile(
|
|
path,
|
|
this.resources,
|
|
signal,
|
|
async ({ handle }) => {
|
|
const bytes = await readBoundedFileBytes(
|
|
handle,
|
|
FIRMWARE_LIMITS.reportBytes,
|
|
);
|
|
if (bytes === undefined)
|
|
throw firmwareReportFailure(
|
|
operation,
|
|
"oversized",
|
|
`Report exceeded ${FIRMWARE_LIMITS.reportBytes} bytes`,
|
|
);
|
|
assertArtifactReadActive(signal);
|
|
if (bytes.length === 0)
|
|
throw firmwareReportFailure(operation, "empty", "Report is empty");
|
|
try {
|
|
const decoded = safeParseUtf8Json(bytes);
|
|
if (!decoded.ok) throw decoded.cause;
|
|
return decoded.value;
|
|
} catch (cause: unknown) {
|
|
throw firmwareReportFailure(
|
|
operation,
|
|
"invalid_json",
|
|
"Tool report is not valid complete JSON",
|
|
cause,
|
|
);
|
|
}
|
|
},
|
|
).catch((cause: unknown) => {
|
|
throw mapArtifactFileFailure(cause, (primary) =>
|
|
primary instanceof NonRegularFileReadError
|
|
? firmwareReportFailure(
|
|
operation,
|
|
"not_regular_file",
|
|
"Report is not a regular file",
|
|
primary,
|
|
)
|
|
: primary instanceof Error &&
|
|
"code" in primary &&
|
|
primary.code === "ENOENT"
|
|
? firmwareReportFailure(
|
|
operation,
|
|
"missing",
|
|
"Report was not created",
|
|
primary,
|
|
)
|
|
: primary,
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
/** Observed state of a producer report that cannot supply analysis Evidence. */
|
|
type FirmwareReportState =
|
|
| "missing"
|
|
| "empty"
|
|
| "oversized"
|
|
| "invalid_json"
|
|
| "not_regular_file";
|
|
|
|
const firmwareReportFailure = (
|
|
operation: string,
|
|
state: FirmwareReportState,
|
|
reason: string,
|
|
cause?: unknown,
|
|
) =>
|
|
new AnalysisOutputError(operation, reason, {
|
|
cause,
|
|
diagnostics: { report_state: state },
|
|
});
|
|
|
|
/** Attribute only failures observed while admitting the caller-selected source. */
|
|
const firmwareSourceAdmissionFailure = (
|
|
operation: string,
|
|
path: string,
|
|
cause: unknown,
|
|
): unknown =>
|
|
mapArtifactFileFailure(cause, (primary) => {
|
|
if (primary instanceof NonRegularFileReadError)
|
|
return firmwareInputFailure(operation, primary);
|
|
if (
|
|
primary instanceof Error &&
|
|
"code" in primary &&
|
|
(primary.code === "EACCES" || primary.code === "EPERM")
|
|
)
|
|
return new AnalysisAccessDeniedError(operation, path, primary.code, {
|
|
cause: primary,
|
|
});
|
|
if (artifactSelectedPathErrno(primary) !== undefined)
|
|
return new AnalysisInputError(operation, { cause: primary }, [
|
|
{
|
|
path: ["path"],
|
|
reason: "invalid_value",
|
|
message: "Firmware source must resolve to an accessible regular file",
|
|
},
|
|
]);
|
|
return primary;
|
|
});
|
|
|
|
const firmwareInputFailure = (operation: string, cause?: unknown) => {
|
|
const message = `Firmware must be a nonempty regular file no larger than ${String(FIRMWARE_LIMITS.inputBytes)} bytes. Select a stable copy within this allowance. ${firmwareLimitRecovery(`The firmware input allowance is ${String(FIRMWARE_LIMITS.inputBytes)} bytes`)}`;
|
|
return new AnalysisInputError(operation, { cause, userMessage: message }, [
|
|
{
|
|
path: ["path"],
|
|
reason: "out_of_range",
|
|
message,
|
|
},
|
|
]);
|
|
};
|
|
|
|
/**
|
|
* Inventory private output without following links, enforcing aggregate staging
|
|
* budgets and the producer's per-file limit.
|
|
*/
|
|
export const inventoryFirmwareOutput = async (
|
|
root: string,
|
|
maxBytes: number,
|
|
maxEntries: number,
|
|
fileSizeBytes: number,
|
|
operation: string,
|
|
signal?: AbortSignal,
|
|
/** The phase deadline that owns an expiry of `signal`. */
|
|
deadline?: FirmwareDeadline,
|
|
): Promise<FirmwareEntry[]> => {
|
|
const entries: FirmwareEntry[] = [];
|
|
const pending = [root];
|
|
let bytes = 0;
|
|
let count = 0;
|
|
while (pending.length > 0) {
|
|
throwIfFirmwareInventoryCancelled(signal, operation, deadline);
|
|
const directory = pending.pop();
|
|
if (directory === undefined) break;
|
|
const directoryStat = await lstat(directory).catch((cause: unknown) => {
|
|
if (cause instanceof Error && "code" in cause && cause.code === "ENOENT")
|
|
return undefined;
|
|
throw cause;
|
|
});
|
|
if (
|
|
directoryStat === undefined ||
|
|
!directoryStat.isDirectory() ||
|
|
directoryStat.isSymbolicLink()
|
|
)
|
|
continue;
|
|
const handle = await opendir(directory, {
|
|
encoding: "buffer" as BufferEncoding,
|
|
}).catch((cause: unknown) => {
|
|
if (cause instanceof Error && "code" in cause && cause.code === "ENOENT")
|
|
return undefined;
|
|
throw cause;
|
|
});
|
|
if (handle === undefined) continue;
|
|
const names: string[] = [];
|
|
for await (const entry of handle) {
|
|
throwIfFirmwareInventoryCancelled(signal, operation, deadline);
|
|
count++;
|
|
if (count > maxEntries)
|
|
throw firmwareStagingLimit(operation, "max_output_files", maxEntries);
|
|
const decoded = decodeDirectoryEntryName(entry.name);
|
|
if (decoded.kind === "unrepresentable")
|
|
throw new AnalysisOutputError(
|
|
operation,
|
|
"Extracted filename cannot be represented losslessly as UTF-8",
|
|
{
|
|
diagnostics: {
|
|
directory: relative(root, directory),
|
|
raw_name_hex: decoded.rawHex,
|
|
},
|
|
},
|
|
);
|
|
names.push(decoded.name);
|
|
}
|
|
const sortedNames = names.sort();
|
|
const inspected = new Array<
|
|
| {
|
|
readonly path: string;
|
|
readonly stat: Stats;
|
|
readonly linkTarget:
|
|
| ReturnType<typeof observedSymlinkTarget>
|
|
| undefined;
|
|
}
|
|
| undefined
|
|
>(sortedNames.length);
|
|
let cursor = 0;
|
|
let firstFailure: unknown;
|
|
const inspectWorker = async (): Promise<void> => {
|
|
while (firstFailure === undefined) {
|
|
const index = cursor++;
|
|
const name = sortedNames[index];
|
|
if (name === undefined) return;
|
|
try {
|
|
throwIfFirmwareInventoryCancelled(signal, operation, deadline);
|
|
const path = join(directory, name);
|
|
const stat = await lstat(path).catch((cause: unknown) => {
|
|
if (
|
|
cause instanceof Error &&
|
|
"code" in cause &&
|
|
cause.code === "ENOENT"
|
|
)
|
|
return undefined;
|
|
throw cause;
|
|
});
|
|
if (stat === undefined) continue;
|
|
const linkTarget = stat.isSymbolicLink()
|
|
? observedSymlinkTarget(
|
|
await readlink(path, { encoding: "buffer" }),
|
|
)
|
|
: undefined;
|
|
inspected[index] = { path, stat, linkTarget };
|
|
} catch (cause: unknown) {
|
|
firstFailure ??= cause;
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
const workerCount = Math.min(16, sortedNames.length);
|
|
await Promise.allSettled(
|
|
Array.from({ length: workerCount }, () => inspectWorker()),
|
|
);
|
|
if (firstFailure !== undefined) throw firstFailure;
|
|
|
|
for (const item of inspected) {
|
|
if (item === undefined) continue;
|
|
const { path, stat, linkTarget } = item;
|
|
if (stat.isDirectory()) {
|
|
pending.push(path);
|
|
continue;
|
|
}
|
|
if (stat.isFile()) bytes += stat.size;
|
|
if (bytes > maxBytes)
|
|
throw firmwareStagingLimit(operation, "max_output_bytes", maxBytes);
|
|
if (stat.isFile() && stat.size >= fileSizeBytes)
|
|
throw firmwareFileSizeLimitReached(operation, fileSizeBytes);
|
|
entries.push({
|
|
path,
|
|
relativePath: relative(root, path).split(sep).join("/"),
|
|
size: stat.size,
|
|
kind: stat.isSymbolicLink()
|
|
? "symlink"
|
|
: stat.isFile()
|
|
? "file"
|
|
: "special",
|
|
linkTarget: linkTarget?.text ?? null,
|
|
...(linkTarget === undefined ? {} : { linkTargetHex: linkTarget.hex }),
|
|
});
|
|
}
|
|
}
|
|
return entries.sort((a, b) =>
|
|
compareUnicodeCodePoints(a.relativePath, b.relativePath),
|
|
);
|
|
};
|
|
|
|
/** The same selected aggregate budget is enforced during and after extraction. */
|
|
export const firmwareStagingLimit = (
|
|
operation: string,
|
|
field: "max_output_files" | "max_output_bytes",
|
|
maximum: number,
|
|
) =>
|
|
new AnalysisResourceConstraintError(
|
|
operation,
|
|
"capacity",
|
|
`Firmware staging exceeded ${field} (${maximum})`,
|
|
{ [field]: maximum },
|
|
{
|
|
retryable: false,
|
|
limit: firmwareStagingBudgetLimit(field, maximum),
|
|
},
|
|
);
|
|
|
|
/**
|
|
* The kernel stops a write at RLIMIT_FSIZE without extending the file, so a
|
|
* staged file that reaches the producer's limit may be truncated. That limit
|
|
* sits one byte above every budget unless an inherited ceiling removed the byte.
|
|
*/
|
|
export const firmwareFileSizeLimitReached = (
|
|
operation: string,
|
|
fileSizeBytes: number,
|
|
) =>
|
|
new AnalysisResourceConstraintError(
|
|
operation,
|
|
"file-size",
|
|
`A staged firmware file reached the ${String(fileSizeBytes)}-byte per-file limit and may be truncated`,
|
|
{ file_size_bytes: fileSizeBytes },
|
|
{
|
|
retryable: false,
|
|
measuredExhaustion: true,
|
|
remediationAction: `REA inherited a ${String(fileSizeBytes)}-byte file-size limit (RLIMIT_FSIZE), so it could not keep one byte of headroom above max_output_bytes and the report and diagnostic allowances. A staged file that reaches the limit cannot be told apart from one the kernel cut short. Raise the file-size limit REA inherits (for example ulimit -f in the launching shell or service) above the largest file the extraction needs. analysis_timeout_ms does not raise it, and repeating the unchanged request fails the same way.`,
|
|
},
|
|
);
|
|
|
|
/** Bind producer paths to the selected input or private extraction tree. */
|
|
export const firmwareLogicalPath = (
|
|
path: string,
|
|
inputPath: string,
|
|
outputRoot: string,
|
|
): string => {
|
|
if (path === inputPath) return "$input";
|
|
const rel = relative(outputRoot, path);
|
|
if (
|
|
!isAbsolute(path) ||
|
|
rel === ".." ||
|
|
rel.startsWith(`..${sep}`) ||
|
|
isAbsolute(rel)
|
|
)
|
|
throw new AnalysisOutputError(
|
|
"extract_firmware",
|
|
`Provider reported a path outside its owned output: ${path}`,
|
|
);
|
|
return rel === "" ? "$output" : `$output/${rel.split(sep).join("/")}`;
|
|
};
|