fix(artifacts): verify FAT identities before Bun and Node SEA claims (#12288)

* fix(artifacts): verify FAT identities before Bun and Node SEA claims

* fix(node-sea): bound thin Mach-O header reads to their slice

* fix(bun): exclude rejected FAT slices from trailer fallback
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 11:51:55 +00:00
1 parent c27f6fff84
commit 1e5956a587
5 files changed
+395 -53

No files matched your search

+83 -35
View File
@@ -18,6 +18,8 @@ import { observeElfStringTableEntry } from "../../domain/elfStringTable.js";
import {
readFatContainerHeader,
readMachoHeaderPrefixFacts,
validateFatSliceIdentity,
type FatSliceDeclaration,
} from "../../domain/apple/machoContainer.js";
import {
readMachoLoadCommandTableHeader,
@@ -146,6 +148,15 @@ interface SectionWindow {
type SectionClaim = SectionWindow | { readonly failure: ArtifactReaderFailure };
/** A rejected slice has no authority over any embedded graph at its offsets. */
interface RejectedFatSlice {
readonly rejectedSlice: FatSliceDeclaration;
/** Only set when the bounded command metadata actually names a Bun slot. */
readonly namedFailure?: ArtifactReaderFailure;
}
type SectionObservation = SectionClaim | RejectedFatSlice;
/**
* Locate at most one graph claim with bounded reads of headers, length
* prefixes, and suffixes. Payload data is not read. Multiple real claims are
@@ -159,7 +170,7 @@ export const probeBunStandaloneGraph = async (
): Promise<BunStandaloneProbe> => {
abortIfNeeded(signal);
const source = { handle, fileSize, signal, limits };
let claims: readonly SectionClaim[];
let claims: readonly SectionObservation[];
try {
claims = [
...(await machOClaims(source, 0, fileSize)),
@@ -172,8 +183,15 @@ export const probeBunStandaloneGraph = async (
return { status: "limited", failure: cause };
}
const real: BunStandaloneProbe[] = [];
const rejectedSlices: FatSliceDeclaration[] = [];
let fatIdentityFailure: ArtifactReaderFailure | undefined;
for (const claim of claims) {
abortIfNeeded(signal);
if ("rejectedSlice" in claim) {
rejectedSlices.push(claim.rejectedSlice);
fatIdentityFailure ??= claim.namedFailure;
continue;
}
const evaluated =
"failure" in claim
? presentFailure(claim.failure)
@@ -191,7 +209,10 @@ export const probeBunStandaloneGraph = async (
}
const only = real[0];
if (only !== undefined) return only;
return evaluateAppendedTrailer(source);
const appended = await evaluateAppendedTrailer(source, rejectedSlices);
return appended.status === "absent" && fatIdentityFailure !== undefined
? presentFailure(fatIdentityFailure)
: appended;
};
/** Read a probed payload once and validate its suffix against the claim. */
@@ -423,26 +444,25 @@ const evaluateSection = async (
*/
const evaluateAppendedTrailer = async (
source: BunProbeSource,
rejectedSlices: readonly FatSliceDeclaration[],
): Promise<BunStandaloneProbe> => {
const { handle, fileSize, signal, limits } = source;
const outsideRejectedSlices = (start: number, end: number): boolean =>
!rejectedSlices.some(
(slice) => start < slice.offset + slice.size && slice.offset < end,
);
const readTrailer = async (end: number): Promise<Buffer | null> =>
outsideRejectedSlices(end - BUN_GRAPH_SUFFIX_BYTES, fileSize)
? readExact(handle, end - BUN_TRAILER_BYTES, BUN_TRAILER_BYTES, signal)
: null;
if (fileSize < BUN_GRAPH_SUFFIX_BYTES) return { status: "absent" };
let graphEnd = fileSize;
let trailer = await readExact(
handle,
graphEnd - BUN_TRAILER_BYTES,
BUN_TRAILER_BYTES,
signal,
);
if (!trailer.equals(BUN_TRAILER)) {
let trailer = await readTrailer(graphEnd);
if (!trailer?.equals(BUN_TRAILER)) {
if (fileSize < BUN_GRAPH_SUFFIX_BYTES + 8) return { status: "absent" };
graphEnd -= 8;
trailer = await readExact(
handle,
graphEnd - BUN_TRAILER_BYTES,
BUN_TRAILER_BYTES,
signal,
);
if (!trailer.equals(BUN_TRAILER)) return { status: "absent" };
trailer = await readTrailer(graphEnd);
if (!trailer?.equals(BUN_TRAILER)) return { status: "absent" };
const length = await readExact(handle, graphEnd, 8, signal);
if (length.readBigUInt64LE() !== BigInt(fileSize))
return presentFailure(
@@ -455,21 +475,26 @@ const evaluateAppendedTrailer = async (
BUN_OFFSETS_BYTES,
signal,
);
const byteCount = offsets.readBigUInt64LE(0);
if (byteCount + BigInt(BUN_GRAPH_SUFFIX_BYTES) > BigInt(limits.payload_bytes))
return presentFailure(
bunPayloadBytesLimit(byteCount + BigInt(BUN_GRAPH_SUFFIX_BYTES), limits),
);
const payloadLength = byteCount + BigInt(BUN_GRAPH_SUFFIX_BYTES);
if (payloadLength > BigInt(graphEnd))
const payloadLength =
offsets.readBigUInt64LE(0) + BigInt(BUN_GRAPH_SUFFIX_BYTES);
const payloadStart = BigInt(graphEnd) - payloadLength;
if (
!outsideRejectedSlices(
payloadStart < 0n ? 0 : Number(payloadStart),
graphEnd,
)
)
return { status: "absent" };
if (payloadLength > BigInt(limits.payload_bytes))
return presentFailure(bunPayloadBytesLimit(payloadLength, limits));
if (payloadStart < 0n)
return presentFailure(bunFormatFailure("trailer is truncated"));
const length = Number(payloadLength);
return {
status: "present",
claim: {
carrier: "appended-trailer",
payloadOffset: graphEnd - length,
payloadLength: length,
payloadOffset: Number(payloadStart),
payloadLength: Number(payloadLength),
},
};
};
@@ -504,7 +529,8 @@ const machOClaims = async (
source: BunProbeSource,
imageOffset: number,
imageLimit: number,
): Promise<readonly SectionClaim[]> => {
fat?: FatSliceDeclaration,
): Promise<readonly SectionObservation[]> => {
const { handle, fileSize, signal, limits } = source;
if (imageOffset < 0 || imageOffset >= fileSize) return [];
const imageSize = Math.min(fileSize - imageOffset, imageLimit);
@@ -514,6 +540,10 @@ const machOClaims = async (
const facts = readMachoHeaderPrefixFacts(header);
if (facts.identity === null)
return imageOffset === 0 ? fatClaims(source, header) : [];
const identityIssue =
fat === undefined ? null : validateFatSliceIdentity(fat, facts.identity);
const rejected: readonly RejectedFatSlice[] =
fat !== undefined && identityIssue !== null ? [{ rejectedSlice: fat }] : [];
const full = await tryRead(
handle,
imageOffset,
@@ -521,22 +551,24 @@ const machOClaims = async (
imageEnd,
signal,
);
if (full === null) return [];
if (full === null) return rejected;
const parsed = readMachoLoadCommandTableHeader(full, imageSize);
if (!parsed.ok || parsed.value === null) return [];
if (!parsed.ok || parsed.value === null) return rejected;
const table = parsed.value;
const {
littleEndian,
headerSize: headerBytes,
commandBytes: sizeofcmds,
} = table;
if (sizeofcmds > limits.recognition_load_command_bytes)
if (sizeofcmds > limits.recognition_load_command_bytes) {
if (identityIssue !== null) return rejected;
throw artifactReaderLimitFailure(
"artifact_limits.bun.recognition_load_command_bytes",
limits.recognition_load_command_bytes,
"bytes",
sizeofcmds,
);
}
const commands = await tryRead(
handle,
imageOffset + headerBytes,
@@ -544,7 +576,7 @@ const machOClaims = async (
imageEnd,
signal,
);
if (commands === null || commands.length !== sizeofcmds) return [];
if (commands === null || commands.length !== sizeofcmds) return rejected;
const claims: SectionClaim[] = [];
const cursor = new MachoLoadCommandCursor(table);
let framingFailure: string | undefined;
@@ -563,9 +595,20 @@ const machOClaims = async (
cmd === LC.SEGMENT_64,
imageOffset,
imageSize,
))
)) {
// A mismatched slice can identify a named declaration from bounded
// metadata, but cannot contribute graph bytes or veto another slice.
if (fat !== undefined && identityIssue !== null)
return [
{
rejectedSlice: fat,
namedFailure: bunFormatFailure(identityIssue),
},
];
claims.push(claim);
}
}
if (rejected.length !== 0) return rejected;
const complete = cursor.finish();
if (!complete.ok) framingFailure ??= complete.error;
return framingFailure === undefined
@@ -636,7 +679,7 @@ const segmentClaims = (
const fatClaims = async (
source: BunProbeSource,
prefix: Buffer,
): Promise<readonly SectionClaim[]> => {
): Promise<readonly SectionObservation[]> => {
const { handle, fileSize, signal } = source;
let parsed = readFatContainerHeader(prefix, fileSize);
if (parsed.state === "needs-data") {
@@ -651,11 +694,16 @@ const fatClaims = async (
parsed = readFatContainerHeader(table, fileSize);
}
if (parsed.state !== "parsed") return [];
const claims: SectionClaim[] = [];
const claims: SectionObservation[] = [];
for (const slice of parsed.slices) {
abortIfNeeded(signal);
if (parsed.rangeIssues[slice.index] !== null) continue;
for (const claim of await machOClaims(source, slice.offset, slice.size))
for (const claim of await machOClaims(
source,
slice.offset,
slice.size,
slice,
))
claims.push(claim);
}
return claims;