mirror of
https://github.com/morluto/rea.git
synced 2026-12-01 11:01:11 +00:00
* feat(native)!: give the native structural decoding allowance a per-call max_structure_bytes The 16 MiB structural budget on native header, section, load-command and ELF dynamic-directory table reads, and on decoded section names, was a fixed ceiling with no override. inspect_elf_unwind_ranges, inspect_kernel_module, inspect_elf_debug_link, inspect_dwarf_units, inspect_binary_hardening and inspect_native_initializers now accept max_structure_bytes (64 bytes to 512 MiB, default 16 MiB) and report details.limit when it stops them. binaryContainer owns the default, the limit name and the failure (nativeStructureFailure); the directory readers take a NativeDirectoryBudgets object in place of the positional decoded-name budget, and Mach-O, the ELF dynamic directory and the ELF pointer relocation pass take the same budget. Other allowances that shared the "structure" limit name now carry their own: the PE per-name decode, the retained DWARF section bound (derived from the admitted image), and the hardening table-entry count. The ELF dependency provider's structural projection was unreachable and is removed. Part of #14235. BREAKING CHANGE: refusals at the structural budget report reported_limits.boundary "max_structure_bytes" instead of "structure". * fix(native): apply max_structure_bytes to the ELF hardening dynamic table and scope its help inspect_binary_hardening read PT_DYNAMIC under its entry count only, so a sectionless ELF could exceed the selected max_structure_bytes; the dynamic table is now checked first. The shared help no longer names tables a reader does not bound, and the limits reference states hardening's Mach-O and PE coverage. The initializer CLI recovery test asserts a parsed successful report. * fix(native): refuse a partial ELF hardening dynamic table as malformed before its budget
163 lines
5.0 KiB
TypeScript
163 lines
5.0 KiB
TypeScript
import {
|
|
BinaryReader,
|
|
DEFAULT_NATIVE_STRUCTURE_BYTES,
|
|
} from "./binaryContainer.js";
|
|
import { err, ok, type Result } from "./result.js";
|
|
|
|
export const DT_NEEDED = 1n;
|
|
export const DT_HASH = 4n;
|
|
export const DT_STRTAB = 5n;
|
|
export const DT_SYMTAB = 6n;
|
|
export const DT_STRSZ = 10n;
|
|
export const DT_SYMENT = 11n;
|
|
export const DT_RPATH = 15n;
|
|
export const DT_RUNPATH = 29n;
|
|
export const DT_FLAGS = 30n;
|
|
export const DT_GNU_HASH = 0x6ffffef5n;
|
|
export const DT_FLAGS_1 = 0x6ffffffbn;
|
|
|
|
const elfDynamicEntryWidth = (bits: 32 | 64): 8 | 16 => (bits === 64 ? 16 : 8);
|
|
|
|
/** Geometry of a bounded file extent, independent of the logical DT_NULL. */
|
|
export const elfDynamicTableGeometry = (bytes: number, bits: 32 | 64) => {
|
|
const width = elfDynamicEntryWidth(bits);
|
|
const trailingBytes = bytes % width;
|
|
return { width, completeBytes: bytes - trailingBytes, trailingBytes };
|
|
};
|
|
|
|
/** Admit the strict native directory before applying its byte budget. */
|
|
export const admitElfDynamicDirectory = (
|
|
fileBytes: number,
|
|
span: { readonly offset: bigint; readonly size: bigint },
|
|
bits: 32 | 64,
|
|
maximum: number = DEFAULT_NATIVE_STRUCTURE_BYTES,
|
|
): Result<
|
|
{ readonly offset: number; readonly size: number; readonly width: number },
|
|
| { readonly kind: "malformed"; readonly message: string }
|
|
| {
|
|
readonly kind: "capacity";
|
|
readonly message: string;
|
|
readonly size: number;
|
|
readonly maximum: number;
|
|
}
|
|
> => {
|
|
const malformed = () =>
|
|
err({
|
|
kind: "malformed" as const,
|
|
message: "PT_DYNAMIC is truncated or outside the file",
|
|
});
|
|
if (
|
|
span.offset < 0n ||
|
|
span.size < 0n ||
|
|
span.offset + span.size > BigInt(fileBytes)
|
|
)
|
|
return malformed();
|
|
const size = Number(span.size);
|
|
const { width, trailingBytes } = elfDynamicTableGeometry(size, bits);
|
|
if (trailingBytes !== 0) return malformed();
|
|
if (size > maximum)
|
|
return err({
|
|
kind: "capacity",
|
|
message: `ELF dynamic directory requires ${String(size)} bytes, above the ${String(maximum)}-byte structural allowance`,
|
|
size,
|
|
maximum,
|
|
});
|
|
return ok({ offset: Number(span.offset), size, width });
|
|
};
|
|
|
|
const dynamicReader = (
|
|
bytes: Uint8Array,
|
|
littleEndian: boolean,
|
|
): BinaryReader =>
|
|
new BinaryReader(
|
|
Buffer.isBuffer(bytes)
|
|
? bytes
|
|
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
|
|
littleEndian,
|
|
"elf",
|
|
);
|
|
|
|
const readDynamicWords = (
|
|
reader: BinaryReader,
|
|
offset: number,
|
|
bits: 32 | 64,
|
|
) => ({
|
|
tag: bits === 64 ? reader.i64(offset) : BigInt(reader.i32(offset)),
|
|
value: reader.word(offset + bits / 8, bits),
|
|
});
|
|
|
|
/** Fixed Elf32_Dyn/Elf64_Dyn: signed d_tag and unsigned d_un, without tag or value admission. */
|
|
export const readElfDynamicEntry = (
|
|
bytes: Uint8Array,
|
|
bits: 32 | 64,
|
|
littleEndian: boolean,
|
|
) => {
|
|
const width = elfDynamicEntryWidth(bits);
|
|
if (bytes.byteLength < width)
|
|
return err({
|
|
kind: "malformed" as const,
|
|
message: "ELF dynamic entry is truncated",
|
|
});
|
|
const reader = dynamicReader(bytes, littleEndian);
|
|
return ok(readDynamicWords(reader, 0, bits));
|
|
};
|
|
|
|
type DynamicRow = ReturnType<typeof readDynamicWords> & {
|
|
readonly offset: number;
|
|
readonly singleton: boolean;
|
|
};
|
|
|
|
type DynamicTableFailure<E> =
|
|
| {
|
|
readonly kind: "duplicate-singleton";
|
|
readonly tag: bigint;
|
|
readonly firstOffset: number;
|
|
readonly offset: number;
|
|
}
|
|
| { readonly kind: "visitor"; readonly error: E };
|
|
|
|
/**
|
|
* Walk an admitted dynamic-table byte span in order. All offsets are relative
|
|
* to the span. DT_NULL closes the table and its value has no meaning. Report
|
|
* incomplete trailing bytes independently of the terminator. Only tags the
|
|
* caller consumes as singletons are tracked, so ignored extensions and
|
|
* repeated DT_NEEDED do not enlarge the reader's retained state.
|
|
*/
|
|
export const walkElfDynamicTable = <E>(
|
|
bytes: Uint8Array,
|
|
{
|
|
bits,
|
|
littleEndian,
|
|
}: { readonly bits: 32 | 64; readonly littleEndian: boolean },
|
|
singletonTags: ReadonlySet<bigint>,
|
|
visit: (row: DynamicRow) => Result<void, E> | void,
|
|
): Result<
|
|
{
|
|
readonly terminatorOffset: number | null;
|
|
readonly trailingBytes: number;
|
|
},
|
|
DynamicTableFailure<E>
|
|
> => {
|
|
const { width, completeBytes, trailingBytes } = elfDynamicTableGeometry(
|
|
bytes.byteLength,
|
|
bits,
|
|
);
|
|
const reader = dynamicReader(bytes, littleEndian);
|
|
const firstOffsets = new Map<bigint, number>();
|
|
for (let offset = 0; offset < completeBytes; offset += width) {
|
|
const { tag, value } = readDynamicWords(reader, offset, bits);
|
|
if (tag === 0n) return ok({ terminatorOffset: offset, trailingBytes });
|
|
const singleton = singletonTags.has(tag);
|
|
if (singleton) {
|
|
const firstOffset = firstOffsets.get(tag);
|
|
if (firstOffset !== undefined)
|
|
return err({ kind: "duplicate-singleton", tag, firstOffset, offset });
|
|
firstOffsets.set(tag, offset);
|
|
}
|
|
const visited = visit({ tag, value, offset, singleton });
|
|
if (visited !== undefined && !visited.ok)
|
|
return err({ kind: "visitor", error: visited.error });
|
|
}
|
|
return ok({ terminatorOffset: null, trailingBytes });
|
|
};
|