mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
fix(elf): bound dynamic version tables to mapped bytes (#13446)
This commit is contained in:
1 parent
e193596a60
commit
2e1144b0ca
5 files changed
+161
-49
No files matched your search
@@ -61,6 +61,11 @@ that share one unambiguous file translation. It stops at the first slot that
|
||||
cannot be admitted, even if later slots map independently. Later import
|
||||
versions remain unexamined and coverage is `partial`.
|
||||
|
||||
When a GNU version requirement or definition is located through its dynamic
|
||||
address alone, decoding stays inside the starting load's contiguous
|
||||
file-mapped prefix. A conflict later in that span leaves earlier complete
|
||||
records available and limits the reported range to the admitted bytes.
|
||||
|
||||
A missing `PT_DYNAMIC` is an observed empty load-time dependency inventory. It
|
||||
is not proof another overlay or container carried dynamic linking metadata.
|
||||
`DT_*` entries are the load-time facts. A section header can locate a table
|
||||
|
||||
@@ -653,6 +653,31 @@ export const createFileMappingIndex = (
|
||||
);
|
||||
return Number(address + translation);
|
||||
};
|
||||
const prefix = (
|
||||
address: bigint,
|
||||
maximumBytes: number,
|
||||
): FileMapping | null => {
|
||||
if (!Number.isSafeInteger(maximumBytes) || maximumBytes < 1) return null;
|
||||
const interval = lastAtOrBelow(boundaries, address);
|
||||
const translation = translations[interval];
|
||||
if (typeof translation !== "bigint") return null;
|
||||
const zeroFill = lastAtOrBelow(zeroFilled.starts, address);
|
||||
if (zeroFill >= 0 && (zeroFilled.reach[zeroFill] as bigint) > address)
|
||||
return null;
|
||||
const mappingEnd = runEnd[interval] as bigint;
|
||||
const zeroFillStart = zeroFilled.starts[zeroFill + 1];
|
||||
const backingEnd =
|
||||
zeroFillStart !== undefined && zeroFillStart < mappingEnd
|
||||
? zeroFillStart
|
||||
: mappingEnd;
|
||||
const limit = address + BigInt(maximumBytes);
|
||||
const end = backingEnd < limit ? backingEnd : limit;
|
||||
return {
|
||||
address,
|
||||
offset: Number(address + translation),
|
||||
size: Number(end - address),
|
||||
};
|
||||
};
|
||||
const access: MappedStringAccess = {
|
||||
offset,
|
||||
mappingAt: (address) => {
|
||||
@@ -667,6 +692,7 @@ export const createFileMappingIndex = (
|
||||
};
|
||||
return {
|
||||
offset,
|
||||
prefix,
|
||||
readCString: (bytes, address, maximumBytes) =>
|
||||
readMappedString(bytes, access, address, maximumBytes),
|
||||
};
|
||||
@@ -675,6 +701,11 @@ export const createFileMappingIndex = (
|
||||
/** One immutable image's indexed mappings and bounded string access. */
|
||||
export interface FileMappingIndex {
|
||||
readonly offset: (address: bigint, size: number) => number;
|
||||
/** Longest file-backed prefix up to the requested length with one translation. */
|
||||
readonly prefix: (
|
||||
address: bigint,
|
||||
maximumBytes: number,
|
||||
) => FileMapping | null;
|
||||
readonly readCString: (
|
||||
bytes: Buffer,
|
||||
address: bigint,
|
||||
|
||||
@@ -454,6 +454,66 @@ it("falls back to PT_DYNAMIC and DT_* virtual addresses when section headers are
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ table: "version_requirements", tag: 0x6ffffffen, leading: 48 },
|
||||
{ table: "version_definitions", tag: 0x6ffffffcn, leading: 28 },
|
||||
] as const)(
|
||||
"keeps only the mapped $table prefix when a later load translates differently",
|
||||
({ table, tag, leading }) => {
|
||||
const original = elfDependencyFixture({
|
||||
stripped: true,
|
||||
verdef: [
|
||||
{ name: "libdemo.so.1", index: 1, flags: 1 },
|
||||
{ name: "DEMO_1.0", index: 5 },
|
||||
],
|
||||
});
|
||||
const headers = elf64ProgramHeaders(original);
|
||||
const start = elf64DynamicTags(original, headers.programHeaders).get(tag);
|
||||
if (start === undefined)
|
||||
throw new Error("Expected the selected dynamic version table");
|
||||
const { bytes, alias } = appendElf64ZeroFill(original, headers);
|
||||
const boundary = start + BigInt(leading);
|
||||
for (const field of [16, 24])
|
||||
bytes.writeBigUInt64LE(boundary, alias + field);
|
||||
bytes.writeBigUInt64LE(1n, alias + 32);
|
||||
bytes.writeBigUInt64LE(boundary, alias + 8);
|
||||
const intact = parse(bytes);
|
||||
expect(intact.coverage.status).toBe("complete");
|
||||
|
||||
// Readable bytes in the original file no longer establish this virtual address.
|
||||
bytes.writeBigUInt64LE(0n, alias + 8);
|
||||
const blocked = parse(bytes);
|
||||
const expectedTable = intact[table];
|
||||
expect(blocked[table]).toMatchObject({
|
||||
status: "unreadable",
|
||||
location: { offset: Number(start), bytes: leading },
|
||||
...("files" in expectedTable
|
||||
? { files: expectedTable.files.slice(0, 1) }
|
||||
: { names: expectedTable.names.slice(0, 1) }),
|
||||
});
|
||||
expect(blocked.coverage.status).toBe("partial");
|
||||
expect(blocked.needed).toEqual(intact.needed);
|
||||
expect(blocked.imports).toEqual(
|
||||
table === "version_definitions"
|
||||
? intact.imports
|
||||
: intact.imports.map((row) =>
|
||||
row.name === "_ZSt4cout"
|
||||
? {
|
||||
...row,
|
||||
version: {
|
||||
index: 4,
|
||||
hidden: false,
|
||||
name: null,
|
||||
file: null,
|
||||
status: "unknown",
|
||||
},
|
||||
}
|
||||
: row,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it("reads a stripped DT_HASH that crosses two adjacent PT_LOAD mappings", () => {
|
||||
const report = parse(
|
||||
elfDependencyFixture({ stripped: true, splitLoadAtHash: true }),
|
||||
|
||||
@@ -65,6 +65,7 @@ import {
|
||||
BinaryReader,
|
||||
createFileMappingIndex,
|
||||
mappedFileOffset,
|
||||
type FileMappingIndex,
|
||||
} from "../../domain/binaryContainer.js";
|
||||
import {
|
||||
resolveMappedFileOffset,
|
||||
@@ -140,6 +141,10 @@ const STATIC_LIMITATIONS = [
|
||||
type Artifact = ElfDependencies["artifact"];
|
||||
type Range = { readonly offset: number; readonly bytes: number };
|
||||
|
||||
interface IndexedElfLoadMappings extends ElfLoadMappings {
|
||||
readonly index: () => FileMappingIndex;
|
||||
}
|
||||
|
||||
interface LocatedTable {
|
||||
readonly offset: number;
|
||||
readonly size: number;
|
||||
@@ -419,7 +424,7 @@ const mapAddress = (
|
||||
};
|
||||
|
||||
/** File-offset end of the PT_LOAD mapping that contains a virtual address. */
|
||||
const mappedExtentEnd = (
|
||||
const startingLoadFileEnd = (
|
||||
mappings: ElfLoadMappings,
|
||||
address: bigint,
|
||||
): number | null => {
|
||||
@@ -428,11 +433,7 @@ const mappedExtentEnd = (
|
||||
address >= range.address && address < range.address + BigInt(range.size),
|
||||
);
|
||||
if (mapping === undefined) return null;
|
||||
let end = mapping.address + BigInt(mapping.size);
|
||||
for (const zeroFill of mappings.zeroFills)
|
||||
if (zeroFill.address > address && zeroFill.address < end)
|
||||
end = zeroFill.address;
|
||||
return mapping.offset + Number(end - mapping.address);
|
||||
return mapping.offset + mapping.size;
|
||||
};
|
||||
|
||||
interface SectionAdmission {
|
||||
@@ -531,7 +532,7 @@ const GNU_VERSYM_SLOT_BYTES = 2;
|
||||
|
||||
/** Keep complete leading slots in one proven file translation. */
|
||||
const tableFromMappedVersym = (
|
||||
mappings: ElfLoadMappings,
|
||||
mappings: IndexedElfLoadMappings,
|
||||
address: bigint | undefined,
|
||||
count: number,
|
||||
): LocatedTable | null => {
|
||||
@@ -543,27 +544,15 @@ const tableFromMappedVersym = (
|
||||
);
|
||||
if (complete !== null || count <= 1) return complete;
|
||||
|
||||
const mapping = createFileMappingIndex(
|
||||
mappings.fileBacked,
|
||||
mappings.zeroFills,
|
||||
);
|
||||
let low = 0;
|
||||
let high = count - 1;
|
||||
let offset: number | null = null;
|
||||
// A mapped prefix proves every shorter span at the same start.
|
||||
while (low < high) {
|
||||
const slots = Math.ceil((low + high) / 2);
|
||||
try {
|
||||
offset = mapping.offset(address, slots * GNU_VERSYM_SLOT_BYTES);
|
||||
low = slots;
|
||||
} catch (cause) {
|
||||
if (!(cause instanceof BinaryMappingFailure)) throw cause;
|
||||
high = slots - 1;
|
||||
}
|
||||
}
|
||||
if (offset === null) return null;
|
||||
const size = low * GNU_VERSYM_SLOT_BYTES;
|
||||
return { offset, size, location: { offset, bytes: size } };
|
||||
const prefix = mappings
|
||||
.index()
|
||||
.prefix(address, count * GNU_VERSYM_SLOT_BYTES);
|
||||
if (prefix === null) return null;
|
||||
const { offset } = prefix;
|
||||
const size = prefix.size - (prefix.size % GNU_VERSYM_SLOT_BYTES);
|
||||
return size === 0
|
||||
? null
|
||||
: { offset, size, location: { offset, bytes: size } };
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -595,11 +584,11 @@ const sysvSymbolCountAt = (
|
||||
*/
|
||||
const mappedHashWords = (
|
||||
reader: BinaryReader,
|
||||
mappings: ElfLoadMappings,
|
||||
mappings: IndexedElfLoadMappings,
|
||||
address: bigint,
|
||||
coverage: "word" | "prefix" = "word",
|
||||
): ElfHashWordReader => {
|
||||
const index = createFileMappingIndex(mappings.fileBacked, mappings.zeroFills);
|
||||
const index = mappings.index();
|
||||
return (offset) => {
|
||||
if (offset < 0) return err("precedes its table");
|
||||
const start = coverage === "prefix" ? 0 : offset;
|
||||
@@ -645,7 +634,7 @@ const gnuSymbolCount = function* (
|
||||
};
|
||||
|
||||
const locateVersionTable = (
|
||||
mappings: ElfLoadMappings,
|
||||
mappings: IndexedElfLoadMappings,
|
||||
fileBytes: number,
|
||||
section: SectionFacts | undefined,
|
||||
address: bigint | undefined,
|
||||
@@ -659,14 +648,14 @@ const locateVersionTable = (
|
||||
if (offset === null) return null;
|
||||
// Without a section header the table extent is unknown; keep every later
|
||||
// record read inside the PT_LOAD mapping that admitted the table start.
|
||||
const end = mappedExtentEnd(mappings, address);
|
||||
const end = startingLoadFileEnd(mappings, address);
|
||||
if (end === null) return null;
|
||||
const size = Math.min(end, fileBytes) - offset;
|
||||
return {
|
||||
offset,
|
||||
size,
|
||||
location: { offset, bytes: size },
|
||||
};
|
||||
const prefix = mappings
|
||||
.index()
|
||||
.prefix(address, Math.min(end, fileBytes) - offset);
|
||||
if (prefix === null) return null;
|
||||
const { size } = prefix;
|
||||
return { offset, size, location: { offset, bytes: size } };
|
||||
};
|
||||
|
||||
const emptyInventory = (
|
||||
@@ -762,7 +751,6 @@ export const readElfDependenciesSteps = function* (
|
||||
reader.range(phoff, phnum * phentsize, "ELF program headers");
|
||||
const fileBacked: FileMapping[] = [];
|
||||
const zeroFills: ZeroFillMapping[] = [];
|
||||
const mappings: ElfLoadMappings = { fileBacked, zeroFills };
|
||||
let interp: Range | null = null;
|
||||
let dynamic: Range | null = null;
|
||||
for (let index = 0; index < phnum; index += 1) {
|
||||
@@ -795,6 +783,13 @@ export const readElfDependenciesSteps = function* (
|
||||
if (spans.fileBacked !== undefined) fileBacked.push(spans.fileBacked);
|
||||
if (spans.zeroFill !== undefined) zeroFills.push(spans.zeroFill);
|
||||
}
|
||||
let mappingIndex: FileMappingIndex | undefined;
|
||||
const mappings: IndexedElfLoadMappings = {
|
||||
fileBacked,
|
||||
zeroFills,
|
||||
index: () =>
|
||||
(mappingIndex ??= createFileMappingIndex(fileBacked, zeroFills)),
|
||||
};
|
||||
const issues: ElfDependencyIssue[] = [];
|
||||
const interpreter = parseInterpreter(reader, interp, issues);
|
||||
if (dynamic === null)
|
||||
@@ -1298,7 +1293,7 @@ const readSections = (
|
||||
|
||||
const locateSymbolCount = function* (
|
||||
reader: BinaryReader,
|
||||
mappings: ElfLoadMappings,
|
||||
mappings: IndexedElfLoadMappings,
|
||||
dynsym: SectionFacts | undefined,
|
||||
hash: SectionFacts | undefined,
|
||||
gnuHash: SectionFacts | undefined,
|
||||
@@ -1389,7 +1384,7 @@ const followVersionLink = (
|
||||
const parseVerneed = (
|
||||
reader: BinaryReader,
|
||||
bytes: Buffer,
|
||||
mappings: ElfLoadMappings,
|
||||
mappings: IndexedElfLoadMappings,
|
||||
dynstr: DynamicStrings,
|
||||
section: SectionFacts | undefined,
|
||||
address: bigint | undefined,
|
||||
@@ -1533,7 +1528,7 @@ const parseVerneed = (
|
||||
const parseVerdef = (
|
||||
reader: BinaryReader,
|
||||
bytes: Buffer,
|
||||
mappings: ElfLoadMappings,
|
||||
mappings: IndexedElfLoadMappings,
|
||||
dynstr: DynamicStrings,
|
||||
section: SectionFacts | undefined,
|
||||
address: bigint | undefined,
|
||||
|
||||
@@ -233,21 +233,38 @@ it("indexes exactly the spans mappedFileOffset accepts, rejects and translates",
|
||||
});
|
||||
|
||||
it("maps adjacent ranges sharing one file translation", () => {
|
||||
const lookup = createFileMappingIndex([
|
||||
const { offset: lookup, prefix } = createFileMappingIndex([
|
||||
{ address: 0x1000n, offset: 0x400, size: 0x10 },
|
||||
{ address: 0x1010n, offset: 0x410, size: 0x10 },
|
||||
]).offset;
|
||||
]);
|
||||
expect(lookup(0x1008n, 8)).toBe(0x408);
|
||||
expect(lookup(0x100cn, 8)).toBe(0x40c);
|
||||
expect(prefix(0x100cn, 32)).toEqual({
|
||||
address: 0x100cn,
|
||||
offset: 0x40c,
|
||||
size: 20,
|
||||
});
|
||||
expect(prefix(0x100cn, 5)).toEqual({
|
||||
address: 0x100cn,
|
||||
offset: 0x40c,
|
||||
size: 5,
|
||||
});
|
||||
expect(prefix(0xfffn, 32)).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects partially overlapping mappings with different file translations", () => {
|
||||
const lookup = createFileMappingIndex([
|
||||
const { offset: lookup, prefix } = createFileMappingIndex([
|
||||
{ address: 0x1000n, offset: 0x400, size: 0x20 },
|
||||
{ address: 0x1018n, offset: 0x900, size: 0x10 },
|
||||
]).offset;
|
||||
]);
|
||||
expect(lookup(0x1000n, 0x10)).toBe(0x400);
|
||||
expect(() => lookup(0x1010n, 0x10)).toThrow(/ambiguous/u);
|
||||
expect(prefix(0x1010n, 0x10)).toEqual({
|
||||
address: 0x1010n,
|
||||
offset: 0x410,
|
||||
size: 8,
|
||||
});
|
||||
expect(prefix(0x1018n, 0x10)).toBeNull();
|
||||
});
|
||||
|
||||
const mappings = [
|
||||
@@ -273,10 +290,14 @@ it.each(
|
||||
},
|
||||
);
|
||||
it("refuses a zero-fill alias inside a span spanning multiple mappings", () => {
|
||||
expect(() =>
|
||||
mappedFileOffset(mappings, 102n, 4, [{ address: 104n, size: 1n }]),
|
||||
).toThrow(/zero-fill/);
|
||||
const fills = [{ address: 104n, size: 1n }];
|
||||
expect(() => mappedFileOffset(mappings, 102n, 4, fills)).toThrow(/zero-fill/);
|
||||
const { prefix } = createFileMappingIndex(mappings, fills);
|
||||
expect(prefix(102n, 4)).toEqual({ address: 102n, offset: 2, size: 2 });
|
||||
expect(prefix(104n, 4)).toBeNull();
|
||||
expect(prefix(105n, 4)).toEqual({ address: 105n, offset: 5, size: 3 });
|
||||
});
|
||||
|
||||
it.each(["scan", "index"] as const)(
|
||||
"reads strings across discontiguous source ranges without examining bytes after NUL (%s)",
|
||||
(mode) => {
|
||||
|
||||
Reference in new issue
Block a user