fix(flutter): reject ambiguous ELF mappings and bound note reads (#2334)
* fix(flutter): reject ambiguous mappings and bound ELF notes Resolve PT_LOAD interpretations once for symbols and dynamic tables, including partial overlaps. Preserve equivalent mappings and leave conflicting or invalid alternate definitions unresolved. Bound distinct nonempty ELF note ranges before retention and reads, deduplicate shared ranges, preserve observed build-ID candidates and report partial coverage on exhaustion. Correct shared verifier workspace ownership and structured Windows path assertions. Addresses the two post-merge review findings on PR #1582. Validation: 147 focused cases across Linux, macOS Sonoma 14.8.9 and non-administrator Windows 11 build 26300.9457; real Gallery CLI/MCP checks on all three hosts; six compiler-produced ELF32/ELF64 hash-layout cases; static, docs, discovery and duplication checks. Full string-pool projections remain explicitly bounded. * test(flutter): parse complete facts through named result schemas --------- Co-authored-by: me <me@medz.org> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
co-authored by
me
morluto
parent
fd65e278be
commit
e3a012d6c9
+48
-44
@@ -1,5 +1,6 @@
|
||||
import { Buffer } from "node:buffer";
|
||||
import { setImmediate as yieldToEventLoop } from "node:timers/promises";
|
||||
import { createElfLoadMap } from "./ElfLoadMap.js";
|
||||
import {
|
||||
SNAPSHOT_HASH_LENGTH,
|
||||
SNAPSHOT_HASH_OFFSET,
|
||||
@@ -205,6 +206,7 @@ export const readElfSymbols = async (
|
||||
vaddr === null ||
|
||||
offset === null ||
|
||||
filesz === null ||
|
||||
!Number.isSafeInteger(vaddr + filesz) ||
|
||||
offset > bytes.length ||
|
||||
filesz > bytes.length - offset
|
||||
)
|
||||
@@ -214,17 +216,7 @@ export const readElfSymbols = async (
|
||||
return failure("Multiple PT_DYNAMIC segments are ambiguous");
|
||||
else dynamicSegment = { offset, filesz };
|
||||
}
|
||||
const toOffset = (vaddr: number, size: number): number | null => {
|
||||
const segment = segments.find(
|
||||
(candidate) =>
|
||||
vaddr >= candidate.vaddr &&
|
||||
vaddr - candidate.vaddr <= candidate.filesz &&
|
||||
size <= candidate.filesz - (vaddr - candidate.vaddr),
|
||||
);
|
||||
return segment === undefined
|
||||
? null
|
||||
: segment.offset + vaddr - segment.vaddr;
|
||||
};
|
||||
const toOffset = await createElfLoadMap(segments, signal);
|
||||
if (segments.length === 0)
|
||||
return failure("ELF image has no PT_LOAD segments");
|
||||
const tables: {
|
||||
@@ -335,26 +327,25 @@ export const readElfSymbols = async (
|
||||
const gnu = tags.get(0x6ffffef5);
|
||||
if (sysv !== undefined) {
|
||||
const at = toOffset(sysv, 8);
|
||||
if (at === null) return failure("DT_HASH header is not file-backed");
|
||||
const buckets = reader.u32(at)!;
|
||||
count = reader.u32(at + 4)!;
|
||||
if (toOffset(sysv, 8 + 4 * (buckets + count)) === null)
|
||||
return failure("DT_HASH table is truncated");
|
||||
if (!at.ok) return failure(`DT_HASH header ${at.error.reason}`);
|
||||
const buckets = reader.u32(at.value)!;
|
||||
count = reader.u32(at.value + 4)!;
|
||||
const table = toOffset(sysv, 8 + 4 * (buckets + count));
|
||||
if (!table.ok) return failure(`DT_HASH table ${table.error.reason}`);
|
||||
} else if (gnu !== undefined) {
|
||||
const at = toOffset(gnu, 16);
|
||||
if (at === null) return failure("DT_GNU_HASH header is not file-backed");
|
||||
const buckets = reader.u32(at)!;
|
||||
const firstSymbol = reader.u32(at + 4)!;
|
||||
const bloomWords = reader.u32(at + 8)!;
|
||||
if (!at.ok) return failure(`DT_GNU_HASH header ${at.error.reason}`);
|
||||
const buckets = reader.u32(at.value)!;
|
||||
const firstSymbol = reader.u32(at.value + 4)!;
|
||||
const bloomWords = reader.u32(at.value + 8)!;
|
||||
const prefixBytes = 16 + bloomWords * (is64 ? 8 : 4) + buckets * 4;
|
||||
if (
|
||||
buckets === 0 ||
|
||||
bloomWords === 0 ||
|
||||
toOffset(gnu, prefixBytes) === null
|
||||
)
|
||||
if (buckets === 0 || bloomWords === 0)
|
||||
return failure("DT_GNU_HASH buckets are malformed");
|
||||
const prefix = toOffset(gnu, prefixBytes);
|
||||
if (!prefix.ok)
|
||||
return failure(`DT_GNU_HASH buckets ${prefix.error.reason}`);
|
||||
let last = 0;
|
||||
const bucketsAt = at + 16 + bloomWords * (is64 ? 8 : 4);
|
||||
const bucketsAt = at.value + 16 + bloomWords * (is64 ? 8 : 4);
|
||||
for (let index = 0; index < buckets; index += 1) {
|
||||
if (index % 4096 === 0) {
|
||||
await yieldToEventLoop();
|
||||
@@ -374,9 +365,10 @@ export const readElfSymbols = async (
|
||||
signal?.throwIfAborted();
|
||||
}
|
||||
const span = prefixBytes + (index - firstSymbol + 1) * 4;
|
||||
if (toOffset(gnu, span) === null)
|
||||
return failure("DT_GNU_HASH chain is unterminated");
|
||||
const chain = reader.u32(at + span - 4)!;
|
||||
const range = toOffset(gnu, span);
|
||||
if (!range.ok)
|
||||
return failure(`DT_GNU_HASH chain ${range.error.reason}`);
|
||||
const chain = reader.u32(at.value + span - 4)!;
|
||||
index += 1;
|
||||
if ((chain & 1) !== 0) {
|
||||
count = index;
|
||||
@@ -388,14 +380,22 @@ export const readElfSymbols = async (
|
||||
return failure("Stripped ELF has no DT_HASH or DT_GNU_HASH symbol count");
|
||||
const offset = toOffset(sym, count * entrySize);
|
||||
const stringOffset = toOffset(str, stringSize);
|
||||
if (offset === null || stringOffset === null)
|
||||
return failure("Dynamic symbol or string table is not file-backed");
|
||||
tables.push({ offset, count, stringOffset, stringSize });
|
||||
if (!offset.ok)
|
||||
return failure(`Dynamic symbol table ${offset.error.reason}`);
|
||||
if (!stringOffset.ok)
|
||||
return failure(`Dynamic string table ${stringOffset.error.reason}`);
|
||||
tables.push({
|
||||
offset: offset.value,
|
||||
count,
|
||||
stringOffset: stringOffset.value,
|
||||
stringSize,
|
||||
});
|
||||
}
|
||||
const wanted = new Set(symbolNames);
|
||||
const symbols = new Map<string, ElfSymbol>();
|
||||
const ambiguous = new Set<string>();
|
||||
const failures: ElfParseFailure[] = [];
|
||||
const unresolved = new Set<string>();
|
||||
const failures = new Set<string>();
|
||||
for (const table of tables) {
|
||||
for (let index = 0; index < table.count; index += 1) {
|
||||
if (index % 4096 === 0) {
|
||||
@@ -419,33 +419,37 @@ export const readElfSymbols = async (
|
||||
const value = is64 ? reader.u64(base + 8) : reader.u32(base + 4);
|
||||
const size = is64 ? reader.u64(base + 16) : reader.u32(base + 8);
|
||||
if (value === null || size === null) {
|
||||
failures.push({ reason: `Symbol ${name} has unrepresentable bounds` });
|
||||
failures.add(`Symbol ${name} has unrepresentable bounds`);
|
||||
unresolved.add(name);
|
||||
continue;
|
||||
}
|
||||
const offset = toOffset(value, size);
|
||||
if (offset === null) {
|
||||
failures.push({ reason: `Symbol ${name} has no file-backed range` });
|
||||
if (!offset.ok) {
|
||||
failures.add(`Symbol ${name} ${offset.error.reason}`);
|
||||
unresolved.add(name);
|
||||
continue;
|
||||
}
|
||||
const previous = symbols.get(name);
|
||||
if (
|
||||
previous !== undefined &&
|
||||
(previous.offset !== offset || previous.size !== size)
|
||||
(previous.offset !== offset.value || previous.size !== size)
|
||||
)
|
||||
ambiguous.add(name);
|
||||
else symbols.set(name, { name, offset, size });
|
||||
else symbols.set(name, { name, offset: offset.value, size });
|
||||
}
|
||||
}
|
||||
for (const name of ambiguous) {
|
||||
symbols.delete(name);
|
||||
failures.push({ reason: `Symbol ${name} has conflicting definitions` });
|
||||
failures.add(`Symbol ${name} has conflicting definitions`);
|
||||
}
|
||||
for (const name of unresolved) symbols.delete(name);
|
||||
for (const name of wanted)
|
||||
if (!symbols.has(name))
|
||||
failures.push({
|
||||
reason: `symbol ${name} is not in the dynamic symbol table`,
|
||||
});
|
||||
return { symbols: [...symbols.values()], failures };
|
||||
if (!symbols.has(name) && !ambiguous.has(name) && !unresolved.has(name))
|
||||
failures.add(`symbol ${name} is not in the dynamic symbol table`);
|
||||
return {
|
||||
symbols: [...symbols.values()],
|
||||
failures: [...failures].map((reason) => ({ reason })),
|
||||
};
|
||||
};
|
||||
|
||||
/** Read one Dart snapshot section header from its file offset. */
|
||||
|
||||
Reference in New Issue
Block a user