Files
reverse-engineering-anything/src/domain/native/elfSectionNameTable.ts
T
7fd50eb361 fix(zstd): share exact-frame Zstandard decoding across ELF and OTA (#12416)
* fix(zstd): share exact-frame Zstandard decoding across ELF and OTA

Walk RFC 8878 frames once so ELF sections, Android OTA REPLACE_ZSTD, and GPU payloads reject truncated headers, blocks, checksums, and trailing bytes on every supported Node runtime. Bound that walk with the runtime zstd.max_frames setting.

* fix(zstd): keep frame output tight and report GPU frame-cap refusals

Copy each Node 22 frame off its decoder chunk before the next frame is decoded. Map a GPU zstd.max_frames refusal through the resource-limit contract instead of a provider failure.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(zstd): keep frame-cap refusals and bad seeds out of malformed results

Project zstd.max_frames through one resource-limit helper for ELF notes, DWARF, GPU, and artifact reads. An invalid REA_ZSTD_MAX_FRAMES seed stays a configuration failure, and frame-header tests cover content size, window, dictionary id, and RLE blocks.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(zstd): yield GPU frame decoding so cancellation can land

The frame-by-frame fallback awaits a caller pause before each data frame. GPU yields and checks the read signal there, so a many-frame payload does not finish before a disconnect is observed.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(zstd): advertise the frame cap and keep strict GPU decode cancellable

List every operation that enforces zstd.max_frames, including GPU, DWARF, and ELF notes. The strict sequence uses the async decoder and checks the caller signal again after it returns, and a single output buffer is not copied a second time.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(zstd): refresh analysis-setting schemas and GPU frame-cap projection

Regenerate the advertised input summaries for inspect_analysis_settings and update_analysis_settings. GPU inventory now throws the projected zstd.max_frames resource limit from payload decode and #retainFailure, so the adjustable setting survives the public failure.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* style(gpu): format the frame-cap projection throw

oxfmt keeps the zstd.max_frames throw in #retainFailure on one line so CI format:check matches the local formatter.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(zstd): yield after the last frame-by-frame GPU decode

The fallback returned success in the same turn as the final synchronous frame, so a queued abort was still invisible to the caller's signal check. Pause once more before that success, matching the strict sequence.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(zstd): yield ELF, OTA, and DWARF frame decoding on the caller signal

The synchronous frame loop ignored abort signals owned by inspect_elf_notes, inspect_dwarf_units, and Android OTA partition reads. Those callers now use the cooperative decoder and yield before Zstandard work, and 32-frame cancellations cover each path.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(zstd): observe cancellation during structural parsing

* fix(zstd): project section-name frame caps at the ELF directory boundary

readElfDirectoryFromBuffer now requires the caller's operation and throws the adjustable zstd.max_frames resource limit. Hardening, unwind, and the other directory consumers keep that typed refusal instead of an adapter or I/O error.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(zstd): preserve optional ELF evidence when frames are capped

* fix(zip): validate Zstandard members through frame completion (#12461)

fix(zip): require complete Zstandard member frames

* fix(elf): yield hardening section name decoding

* fix(elf): yield section names for unwind and export reads

* fix(zstd): yield ELF directory reads that already own a cancellation signal

Hardening, unwind, initializers, exports, kernel modules, debug link, and Android JNI now read section names through the cooperative directory when their caller has a signal. Readers with no signal stay synchronous.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(elf): share one initializer directory and retain its snapshot

* fix(android): reuse the admitted JNI ELF directory

* fix(elf): retain optional debug candidate evidence through cooperative decoding

* fix(zstd): yield ELF section names for Go, Rust, and IL2CPP

These inspections already own an abort signal. Their ELF directory read now uses the cooperative container decoder so a queued cancellation can land during a many-frame section-name table.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(elf): recognize compressed kernel modules before ELF decoding

The cooperative module reader decoded the bytes as ELF before the container check, so gzip, xz, and zstd modules became malformed images. The compression check now returns the unsupported-container report first.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* refactor(elf): classify each container once before reading it

* fix(il2cpp): preserve admission order during cooperative linking

* fix(elf): reuse section-name decoding in debug-link comparison (#12472)

* style(elf): format updated section-reader test calls

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-19 19:45:26 +00:00

166 lines
4.7 KiB
TypeScript

import { decodeLimit, ParseFailureError } from "../parseFailure.js";
import {
decodeElfCompressedSection,
decodeElfCompressedSectionCooperative,
type ElfCompressedSectionResult,
} from "./elfCompressedSection.js";
import type { ZstandardFramePause } from "../compression/zstandardExactFrame.js";
const SUBJECT = "ELF compressed section-name table";
/** Limit name of the caller's file budget, which also bounds decoded section names. */
const SECTION_NAME_BUDGET_LIMIT = "max_file_bytes";
/** Caller can raise max_file_bytes to admit the decoded section-name table. */
export const elfSectionNameBudgetFailure = (
required: bigint,
selected: number,
): ParseFailureError<"limit"> =>
decodeLimit(
"elf",
`ELF source and decoded section names require ${String(required)} bytes, above the selected max_file_bytes (${String(selected)}).`,
{ name: SECTION_NAME_BUDGET_LIMIT, maximum: selected, required },
);
/**
* The section-name budget stop's limit: the selected max_file_bytes and the
* size the source plus decoded names require.
*/
export const elfSectionNameBudget = (
cause: unknown,
):
| {
readonly selected: number;
readonly required: bigint;
readonly detail: string;
}
| undefined => {
if (
!(cause instanceof ParseFailureError) ||
cause.failure.kind !== "limit" ||
cause.failure.format !== "elf" ||
cause.failure.limit.name !== SECTION_NAME_BUDGET_LIMIT
)
return undefined;
const { maximum, required } = cause.failure.limit;
return required === undefined
? undefined
: {
selected: maximum,
required: BigInt(required),
detail: cause.failure.detail,
};
};
/** One section-name table a directory or note driver asks the owner to decode. */
export interface ElfSectionNameDecodeRequest {
readonly raw: Buffer;
readonly flags: bigint;
readonly bits: 32 | 64;
readonly little: boolean;
readonly maximumDecodedBytes: number;
}
export type CooperativeElfSectionNameDecoder = (
request: ElfSectionNameDecodeRequest,
) => Promise<ElfCompressedSectionResult>;
/** Decode section names through the shared ELF compressed-section owner. */
export const decodeElfSectionNameTable = (
bytes: Buffer,
flags: bigint,
bits: 32 | 64,
little: boolean,
maximumDecodedBytes: number,
): ElfCompressedSectionResult =>
decodeElfCompressedSection(
bytes,
flags,
bits,
little,
maximumDecodedBytes,
SUBJECT,
);
/** Cooperative section-name decode. `pause` observes the caller's abort signal. */
export const decodeElfSectionNameTableCooperative = (
bytes: Buffer,
flags: bigint,
bits: 32 | 64,
little: boolean,
maximumDecodedBytes: number,
pause: ZstandardFramePause,
): Promise<ElfCompressedSectionResult> =>
decodeElfCompressedSectionCooperative(
bytes,
flags,
bits,
little,
maximumDecodedBytes,
SUBJECT,
pause,
);
export const decodeElfSectionNameRequest = (
request: ElfSectionNameDecodeRequest,
): ElfCompressedSectionResult =>
decodeElfSectionNameTable(
request.raw,
request.flags,
request.bits,
request.little,
request.maximumDecodedBytes,
);
export const decodeElfSectionNameRequestCooperative = (
request: ElfSectionNameDecodeRequest,
pause: ZstandardFramePause,
): Promise<ElfCompressedSectionResult> =>
decodeElfSectionNameTableCooperative(
request.raw,
request.flags,
request.bits,
request.little,
request.maximumDecodedBytes,
pause,
);
/** Reuse a decoded table across readers of one borrowed immutable artifact. */
export const sharedElfSectionNameDecoder = (
pause: ZstandardFramePause,
): CooperativeElfSectionNameDecoder => {
let admitted:
| {
request: ElfSectionNameDecodeRequest;
decoded: Extract<
ElfCompressedSectionResult,
{ state: "available" | "unsupported" }
>;
}
| undefined;
return async (request) => {
if (admitted !== undefined) {
const { request: previous, decoded } = admitted;
if (
request.raw.buffer === previous.raw.buffer &&
request.raw.byteOffset === previous.raw.byteOffset &&
request.raw.byteLength === previous.raw.byteLength &&
request.bits === previous.bits &&
request.little === previous.little &&
request.flags === previous.flags &&
(decoded.state === "unsupported" ||
!decoded.compressed ||
decoded.bytes.length <= request.maximumDecodedBytes)
)
return decoded;
}
const decoded = await decodeElfSectionNameRequestCooperative(
request,
pause,
);
if (decoded.state === "available" || decoded.state === "unsupported")
admitted = { request, decoded };
return decoded;
};
};