mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
refactor(pe): share import descriptor walking (#16225)
This commit is contained in:
1 parent
c9eaf853be
commit
a52d543f73
1 file changed
+32
-39
@@ -471,35 +471,41 @@ export const parsePeImportsFromHandle = async (
|
||||
const importDirectory = image.pe_data_directories?.[PE_IMPORT_DIRECTORY];
|
||||
const delayDirectory = image.pe_data_directories?.[PE_DELAY_IMPORT_DIRECTORY];
|
||||
const headerAnomalies: PeHeaderAnomaly[] = [...image.pe_header_anomalies];
|
||||
const sizeExceeded = new Set<object>();
|
||||
const descriptorReader = (
|
||||
directory: NonNullable<typeof importDirectory>,
|
||||
kind: "import" | "delay-load import",
|
||||
spans: { offset: number; bytes: number }[],
|
||||
) => {
|
||||
const standard = kind === "import";
|
||||
const width = standard ? IMPORT_DESCRIPTOR_BYTES : DELAY_DESCRIPTOR_BYTES;
|
||||
const label = standard ? "Import descriptor" : "Delay-load descriptor";
|
||||
const directoryIndex = standard
|
||||
? PE_IMPORT_DIRECTORY
|
||||
: PE_DELAY_IMPORT_DIRECTORY;
|
||||
// The loader reads to the null terminator even when the declared size ends first.
|
||||
const firstPastSize = Math.floor(directory.size / width);
|
||||
return (index: number, rva: number): number | null => {
|
||||
if (index === firstPastSize)
|
||||
headerAnomalies.push({
|
||||
code: "directory_size_below_structure",
|
||||
message: `Data directory ${String(directoryIndex)} (${kind}) declares Size ${String(directory.size)}, which ends before descriptor ${String(index)}; the loader ignores this Size and reads descriptors to the null terminator, and so did this read.`,
|
||||
});
|
||||
const descriptor = mapDescriptor(rva, width, label);
|
||||
if (descriptor !== null) consume(spans, descriptor, width);
|
||||
return descriptor;
|
||||
};
|
||||
};
|
||||
const imports: PeImportDll[] = [];
|
||||
let importDirectoryResult: PeImports["import_directory"] = null;
|
||||
if (importDirectory !== undefined && importDirectory.address !== 0) {
|
||||
const spans: { offset: number; bytes: number }[] = [];
|
||||
const readDescriptor = descriptorReader(importDirectory, "import", spans);
|
||||
for (let index = 0; ; index++) {
|
||||
if (++entries % ENTRIES_PER_YIELD === 0) await checkpoint();
|
||||
const descriptorRva =
|
||||
importDirectory.address + index * IMPORT_DESCRIPTOR_BYTES;
|
||||
// The loader walks descriptors to the null terminator without
|
||||
// consulting Size, so neither does this walk.
|
||||
if (
|
||||
index * IMPORT_DESCRIPTOR_BYTES + IMPORT_DESCRIPTOR_BYTES >
|
||||
importDirectory.size &&
|
||||
!sizeExceeded.has(importDirectory)
|
||||
) {
|
||||
sizeExceeded.add(importDirectory);
|
||||
headerAnomalies.push({
|
||||
code: "directory_size_below_structure",
|
||||
message: `Data directory ${String(importDirectory === importDirectory ? PE_IMPORT_DIRECTORY : PE_DELAY_IMPORT_DIRECTORY)} (import) declares Size ${String(importDirectory.size)}, which ends before descriptor ${String(index)}; the loader ignores this Size and reads descriptors to the null terminator, and so did this read.`,
|
||||
});
|
||||
}
|
||||
const descriptor = mapDescriptor(
|
||||
descriptorRva,
|
||||
IMPORT_DESCRIPTOR_BYTES,
|
||||
"Import descriptor",
|
||||
);
|
||||
const descriptor = readDescriptor(index, descriptorRva);
|
||||
if (descriptor === null) break;
|
||||
consume(spans, descriptor, IMPORT_DESCRIPTOR_BYTES);
|
||||
const originalFirstThunk = await reader.u32(descriptor);
|
||||
const timeDateStamp = await reader.u32(descriptor + 4);
|
||||
const forwarderChain = await reader.u32(descriptor + 8);
|
||||
@@ -563,30 +569,17 @@ export const parsePeImportsFromHandle = async (
|
||||
let delayDirectoryResult: PeImports["delay_import_directory"] = null;
|
||||
if (delayDirectory !== undefined && delayDirectory.address !== 0) {
|
||||
const spans: { offset: number; bytes: number }[] = [];
|
||||
const readDescriptor = descriptorReader(
|
||||
delayDirectory,
|
||||
"delay-load import",
|
||||
spans,
|
||||
);
|
||||
for (let index = 0; ; index++) {
|
||||
if (++entries % ENTRIES_PER_YIELD === 0) await checkpoint();
|
||||
const descriptorRva =
|
||||
delayDirectory.address + index * DELAY_DESCRIPTOR_BYTES;
|
||||
// The loader walks descriptors to the null terminator without
|
||||
// consulting Size, so neither does this walk.
|
||||
if (
|
||||
index * DELAY_DESCRIPTOR_BYTES + DELAY_DESCRIPTOR_BYTES >
|
||||
delayDirectory.size &&
|
||||
!sizeExceeded.has(delayDirectory)
|
||||
) {
|
||||
sizeExceeded.add(delayDirectory);
|
||||
headerAnomalies.push({
|
||||
code: "directory_size_below_structure",
|
||||
message: `Data directory ${String(delayDirectory === importDirectory ? PE_IMPORT_DIRECTORY : PE_DELAY_IMPORT_DIRECTORY)} (delay-load import) declares Size ${String(delayDirectory.size)}, which ends before descriptor ${String(index)}; the loader ignores this Size and reads descriptors to the null terminator, and so did this read.`,
|
||||
});
|
||||
}
|
||||
const descriptor = mapDescriptor(
|
||||
descriptorRva,
|
||||
DELAY_DESCRIPTOR_BYTES,
|
||||
"Delay-load descriptor",
|
||||
);
|
||||
const descriptor = readDescriptor(index, descriptorRva);
|
||||
if (descriptor === null) break;
|
||||
consume(spans, descriptor, DELAY_DESCRIPTOR_BYTES);
|
||||
const attributes = await reader.u32(descriptor);
|
||||
const nameRva = await reader.u32(descriptor + 4);
|
||||
const moduleHandleRva = await reader.u32(descriptor + 8);
|
||||
|
||||
Reference in new issue
Block a user