mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 15:05:51 +00:00
fix(bun): validate named ELF graph section backing (#12260)
* fix(bun): validate named ELF graph section backing * fix(bun): validate ELF string table extent before lookup
This commit is contained in:
1 parent
ae8fb57ef0
commit
241ef45c12
5 files changed
+148
-108
No files matched your search
@@ -1,5 +1,10 @@
|
||||
import { LC } from "../../domain/apple/machoLoadCommandCodes.js";
|
||||
import { Budget } from "../../domain/budget.js";
|
||||
import { declaredFileRange } from "../../domain/binaryContainer.js";
|
||||
import { readElfHeaderFacts } from "../../domain/elfFixedHeader.js";
|
||||
import { readElfRawTableDeclarations } from "../../domain/elfRawTableDeclarations.js";
|
||||
import { readElfSectionHeader } from "../../domain/elfSectionHeader.js";
|
||||
import { observeElfStringTableEntry } from "../../domain/elfStringTable.js";
|
||||
import {
|
||||
readFatContainerHeader,
|
||||
readMachoHeaderPrefixFacts,
|
||||
@@ -672,121 +677,112 @@ const peClaims = async (
|
||||
return claims;
|
||||
};
|
||||
|
||||
const ELF_BUN_NAME = Buffer.from(".bun");
|
||||
const ELF_SHT_NULL = 0;
|
||||
const ELF_SHT_STRTAB = 3;
|
||||
const ELF_SHT_NOBITS = 8;
|
||||
const MAX_SAFE_FILE_WORD = BigInt(Number.MAX_SAFE_INTEGER);
|
||||
|
||||
const elfClaims = async (
|
||||
source: BunProbeSource,
|
||||
): Promise<readonly SectionWindow[]> => {
|
||||
const { handle, fileSize, signal, limits } = source;
|
||||
const header = await tryRead(handle, 0, 64, fileSize, signal);
|
||||
if (
|
||||
header === null ||
|
||||
header.length < 16 ||
|
||||
!header.subarray(0, 4).equals(Buffer.from([0x7f, 0x45, 0x4c, 0x46]))
|
||||
)
|
||||
return [];
|
||||
const wide = header[4] === 2;
|
||||
if (header[4] !== 1 && header[4] !== 2) return [];
|
||||
if (header[5] !== 1 && header[5] !== 2) return [];
|
||||
const littleEndian = header[5] === 1;
|
||||
const shentsize = wide ? 64 : 40;
|
||||
const shoffOffset = wide ? 40 : 32;
|
||||
const shnumOffset = wide ? 60 : 48;
|
||||
const shstrndxOffset = wide ? 62 : 50;
|
||||
if (header.length < (wide ? 64 : 52)) return [];
|
||||
const observedShentsize = readU16(header, wide ? 58 : 46, littleEndian);
|
||||
if (observedShentsize !== 0 && observedShentsize !== shentsize) return [];
|
||||
const shoff = wide
|
||||
? readBoundedU64(header, shoffOffset, littleEndian)
|
||||
: readU32(header, shoffOffset, littleEndian);
|
||||
const shnum = readU16(header, shnumOffset, littleEndian);
|
||||
const shstrndx = readU16(header, shstrndxOffset, littleEndian);
|
||||
if (header === null) return [];
|
||||
const format = readElfHeaderFacts(header, fileSize);
|
||||
if (!format.ok) return [];
|
||||
const { elfClass, byteOrder } = format.value;
|
||||
const declarations = readElfRawTableDeclarations(header, format.value);
|
||||
if (!declarations.ok) return [];
|
||||
const { offset, width, count, namesIndex } = declarations.value.sections;
|
||||
const sectionWidth = elfClass === 64 ? 64 : 40;
|
||||
// Unsupported extended numbering does not establish a Bun graph.
|
||||
if (
|
||||
shoff === undefined ||
|
||||
shoff === 0 ||
|
||||
shnum === 0 ||
|
||||
shnum === 0xffff ||
|
||||
shstrndx >= shnum
|
||||
(width !== 0 && width !== sectionWidth) ||
|
||||
offset === 0n ||
|
||||
offset > MAX_SAFE_FILE_WORD ||
|
||||
count === 0 ||
|
||||
count === 0xffff ||
|
||||
namesIndex >= count
|
||||
)
|
||||
return [];
|
||||
if (shnum * shentsize > limits.recognition_table_bytes)
|
||||
const tableBytes = count * sectionWidth;
|
||||
if (tableBytes > limits.recognition_table_bytes)
|
||||
throw artifactReaderLimitFailure(
|
||||
"artifact_limits.bun.recognition_table_bytes",
|
||||
limits.recognition_table_bytes,
|
||||
"bytes",
|
||||
shnum * shentsize,
|
||||
tableBytes,
|
||||
);
|
||||
const table = await tryRead(
|
||||
handle,
|
||||
shoff,
|
||||
shnum * shentsize,
|
||||
Number(offset),
|
||||
tableBytes,
|
||||
fileSize,
|
||||
signal,
|
||||
);
|
||||
if (table === null) return [];
|
||||
const strings = elfSectionBounds(table, shstrndx, wide, littleEndian);
|
||||
if (strings === null) return [];
|
||||
// A section name lookup needs only `.bun` and its terminator. Read the
|
||||
// referenced range, rather than treating a truncated string-table prefix as
|
||||
// evidence that later names are absent. Duplicate offsets share one lookup.
|
||||
const readSection = (index: number) => {
|
||||
const start = index * sectionWidth;
|
||||
const section = readElfSectionHeader(
|
||||
table.subarray(start, start + sectionWidth),
|
||||
elfClass,
|
||||
byteOrder === "little",
|
||||
);
|
||||
return section.ok ? section.value : null;
|
||||
};
|
||||
|
||||
const strings = readSection(namesIndex);
|
||||
if (strings === null || strings.type !== ELF_SHT_STRTAB) return [];
|
||||
const stringRange = declaredFileRange(fileSize, strings.offset, strings.size);
|
||||
if (!stringRange.file_backed) return [];
|
||||
// Only ".bun" and its terminator are needed. Duplicate offsets share one
|
||||
// bounded lookup, even when the referenced string is much longer.
|
||||
const names = new Map<number, boolean>();
|
||||
const claims: SectionWindow[] = [];
|
||||
for (let index = 0; index < shnum; index += 1) {
|
||||
const bounds = elfSectionBounds(table, index, wide, littleEndian);
|
||||
if (bounds === null) continue;
|
||||
const nameOffset = readU32(table, index * shentsize, littleEndian);
|
||||
for (let index = 0; index < count; index += 1) {
|
||||
const section = readSection(index);
|
||||
if (
|
||||
section === null ||
|
||||
section.type === ELF_SHT_NULL ||
|
||||
section.type === ELF_SHT_NOBITS ||
|
||||
section.offset > MAX_SAFE_FILE_WORD ||
|
||||
section.size > MAX_SAFE_FILE_WORD
|
||||
)
|
||||
continue;
|
||||
const { nameOffset } = section;
|
||||
let isBun = names.get(nameOffset);
|
||||
if (isBun === undefined) {
|
||||
const name =
|
||||
nameOffset >= strings.size
|
||||
nameOffset >= stringRange.size
|
||||
? null
|
||||
: await tryRead(
|
||||
handle,
|
||||
strings.offset + nameOffset,
|
||||
Math.min(ELF_BUN_NAME.length + 1, strings.size - nameOffset),
|
||||
stringRange.offset + nameOffset,
|
||||
Math.min(ELF_BUN_NAME.length + 1, stringRange.size - nameOffset),
|
||||
fileSize,
|
||||
signal,
|
||||
);
|
||||
isBun = name !== null && elfNameIsBun(name, 0);
|
||||
isBun =
|
||||
name !== null &&
|
||||
observeElfStringTableEntry(name, 0, name.length).bytes?.equals(
|
||||
ELF_BUN_NAME,
|
||||
) === true;
|
||||
names.set(nameOffset, isBun);
|
||||
}
|
||||
if (!isBun) continue;
|
||||
claims.push(windowFor("elf-section", bounds.offset, bounds.size, fileSize));
|
||||
claims.push(
|
||||
windowFor(
|
||||
"elf-section",
|
||||
Number(section.offset),
|
||||
Number(section.size),
|
||||
fileSize,
|
||||
),
|
||||
);
|
||||
}
|
||||
return claims;
|
||||
};
|
||||
|
||||
const elfSectionBounds = (
|
||||
table: Buffer,
|
||||
index: number,
|
||||
wide: boolean,
|
||||
littleEndian: boolean,
|
||||
): { readonly offset: number; readonly size: number } | null => {
|
||||
const shentsize = wide ? 64 : 40;
|
||||
const start = index * shentsize;
|
||||
if (start + shentsize > table.length) return null;
|
||||
const offset = wide
|
||||
? readBoundedU64(table, start + 24, littleEndian)
|
||||
: readU32(table, start + 16, littleEndian);
|
||||
const size = wide
|
||||
? readBoundedU64(table, start + 32, littleEndian)
|
||||
: readU32(table, start + 20, littleEndian);
|
||||
if (offset === undefined || size === undefined) return null;
|
||||
return { offset, size };
|
||||
};
|
||||
|
||||
const ELF_BUN_NAME = Buffer.from(".bun");
|
||||
|
||||
/**
|
||||
* Compare only `.bun` and its terminator. Many headers may share one long
|
||||
* name, so scanning each name to its NUL would repeat unbounded work.
|
||||
*/
|
||||
const elfNameIsBun = (table: Buffer, offset: number): boolean => {
|
||||
const end = offset + ELF_BUN_NAME.length;
|
||||
if (end > table.length) return false;
|
||||
if (end < table.length && table[end] !== 0) return false;
|
||||
return table.subarray(offset, end).equals(ELF_BUN_NAME);
|
||||
};
|
||||
|
||||
const windowFor = (
|
||||
carrier: SectionWindow["carrier"],
|
||||
fileOffset: number,
|
||||
@@ -819,33 +815,6 @@ const exactName = (bytes: Buffer, text: string): boolean => {
|
||||
return encoded.equals(bytes);
|
||||
};
|
||||
|
||||
const readBoundedU64 = (
|
||||
bytes: Buffer,
|
||||
offset: number,
|
||||
littleEndian: boolean,
|
||||
): number | undefined => {
|
||||
if (offset < 0 || offset + 8 > bytes.length) return undefined;
|
||||
const value = littleEndian
|
||||
? bytes.readBigUInt64LE(offset)
|
||||
: bytes.readBigUInt64BE(offset);
|
||||
if (value > BigInt(Number.MAX_SAFE_INTEGER)) return undefined;
|
||||
return Number(value);
|
||||
};
|
||||
|
||||
const readU32 = (
|
||||
bytes: Buffer,
|
||||
offset: number,
|
||||
littleEndian: boolean,
|
||||
): number =>
|
||||
littleEndian ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset);
|
||||
|
||||
const readU16 = (
|
||||
bytes: Buffer,
|
||||
offset: number,
|
||||
littleEndian: boolean,
|
||||
): number =>
|
||||
littleEndian ? bytes.readUInt16LE(offset) : bytes.readUInt16BE(offset);
|
||||
|
||||
const tryRead = async (
|
||||
handle: FileHandle,
|
||||
position: number,
|
||||
|
||||
Reference in new issue
Block a user