fix(dotnet): bound RVA reads by section virtual size
Use the shared resolver for virtual containment, overlap checks, and file-backed extent. Raw alignment padding no longer supplies method bytes or shadows a later virtual section. Preserve malformed body coverage and suppress CIL hashes outside the admitted range. Verify zero, exact-end, truncated-body, padding, and true-overlap cases through compiled CLI and MCP consumers. Synthetic PE fixtures only; no Windows loader or real managed-provider verification. Fixes #1596. Supersedes #1595.
This commit is contained in:
@@ -32,6 +32,7 @@ interface ManagedPeFixtureOptions {
|
||||
readonly targetFramework?: string | null;
|
||||
readonly typeName?: string;
|
||||
readonly typeNamespace?: string;
|
||||
readonly virtualSectionSize?: number;
|
||||
}
|
||||
|
||||
const textEncoder = new TextEncoder();
|
||||
@@ -846,7 +847,8 @@ const buildManagedFixtureImage = (
|
||||
readyToRunOffset === null ? 0 : readyToRunOffset - 0x0200 + 4,
|
||||
) / 0x0200,
|
||||
) * 0x0200;
|
||||
const virtualSectionSize = Math.max(0x1000, rawSectionSize);
|
||||
const virtualSectionSize =
|
||||
options.virtualSectionSize ?? Math.max(0x1000, rawSectionSize);
|
||||
const image = Buffer.alloc(0x0200 + rawSectionSize);
|
||||
image.write("MZ", 0, "ascii");
|
||||
image.writeUInt32LE(0x80, 0x3c);
|
||||
|
||||
@@ -139,14 +139,14 @@ const resolveRva = (
|
||||
scope,
|
||||
`${scope} has an invalid RVA range`,
|
||||
);
|
||||
// VirtualSize owns RVA containment; raw size also includes alignment padding.
|
||||
const selected = sections.find((section) => {
|
||||
const within = rva - section.virtualAddress;
|
||||
const mappedSize = Math.max(section.virtualSize, section.rawSize);
|
||||
return (
|
||||
mappedSize > 0 &&
|
||||
section.virtualSize > 0 &&
|
||||
within >= 0 &&
|
||||
within <= mappedSize &&
|
||||
size <= mappedSize - within
|
||||
within < section.virtualSize &&
|
||||
size <= section.virtualSize - within
|
||||
);
|
||||
});
|
||||
if (selected === undefined)
|
||||
@@ -156,13 +156,8 @@ const resolveRva = (
|
||||
`${scope} RVA is not covered by one PE section`,
|
||||
);
|
||||
for (const section of sections) {
|
||||
if (
|
||||
section === selected ||
|
||||
Math.max(section.virtualSize, section.rawSize) === 0
|
||||
)
|
||||
continue;
|
||||
const end =
|
||||
section.virtualAddress + Math.max(section.virtualSize, section.rawSize);
|
||||
if (section === selected || section.virtualSize === 0) continue;
|
||||
const end = section.virtualAddress + section.virtualSize;
|
||||
const intersects =
|
||||
size === 0
|
||||
? rva >= section.virtualAddress && rva < end
|
||||
@@ -184,6 +179,7 @@ const resolveRva = (
|
||||
const offset = add(selected.rawOffset, within, scope);
|
||||
requireRange(bytes, offset, size, scope);
|
||||
let available = Math.min(
|
||||
selected.virtualSize - within,
|
||||
selected.rawSize - within,
|
||||
bytes.length - offset,
|
||||
0x1_0000_0000 - rva,
|
||||
@@ -193,7 +189,7 @@ const resolveRva = (
|
||||
if (
|
||||
section !== selected &&
|
||||
section.virtualAddress > rva &&
|
||||
Math.max(section.virtualSize, section.rawSize) > 0
|
||||
section.virtualSize > 0
|
||||
)
|
||||
available = Math.min(available, section.virtualAddress - rva);
|
||||
return { offset, available };
|
||||
|
||||
Reference in New Issue
Block a user