Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(pe): ignore zero-extent sections in resource interval ownership (#4463)

jayinvers9e7e13fb7155
diff --git a/src/native/pe/PeResourceLayout.ts b/src/native/pe/PeResourceLayout.ts
index 833c65194..8484ec4ba 100644
--- a/src/native/pe/PeResourceLayout.ts
+++ b/src/native/pe/PeResourceLayout.ts
@@ -34,6 +34,23 @@ interface Section {
const virtualExtent = (section: Section): number =>
section.virtualSize === 0 ? section.size : section.virtualSize;
+// Intersection normalized with those ownership rules: a section without
+// virtual extent owns no byte and never conflicts, while an empty resource
+// owns exactly its start point, so a positive-extent section starting there
+// conflicts with header-backed ownership of the same RVA.
+const intersectsResource = (
+ section: Section,
+ rva: number,
+ size: number,
+): boolean => {
+ const extent = virtualExtent(section);
+ return (
+ extent > 0 &&
+ section.rva < rva + Math.max(size, 1) &&
+ rva < section.rva + extent
+ );
+};
+
/** Header admission and unambiguous file-backed RVA mapping, independent of section names. */
export const readPeResourceLayout = (bytes: Buffer) => {
requirePeRange(bytes, 0, 64);
@@ -111,11 +128,7 @@ export const readPeResourceLayout = (bytes: Buffer) => {
: containing;
if (rva < headerSize) {
if (
- sections.some(
- (section) =>
- section.rva < rva + size &&
- section.rva + virtualExtent(section) > rva,
- ) ||
+ sections.some((section) => intersectsResource(section, rva, size)) ||
size > headerSize - rva
)
peFailure("Ambiguous resource header RVA mapping.");
@@ -131,13 +144,11 @@ export const readPeResourceLayout = (bytes: Buffer) => {
peFailure("Resource range leaves its section's virtual size.");
if (within > section.size || size > section.size - within)
peFailure("Resource range is not entirely file-backed in one section.");
- // A second section starting inside the requested interval is ambiguous too.
+ // A second section with virtual extent inside the requested interval is
+ // ambiguous too; a zero-extent section owns no byte there.
if (
sections.some(
- (other) =>
- other !== section &&
- other.rva < rva + size &&
- other.rva + virtualExtent(other) > rva,
+ (other) => other !== section && intersectsResource(other, rva, size),
)
)
peFailure("Resource interval intersects overlapping virtual sections.");
diff --git a/src/native/pe/PeResourceParser.test.ts b/src/native/pe/PeResourceParser.test.ts
index dfedb27d2..300c054c1 100644
--- a/src/native/pe/PeResourceParser.test.ts
+++ b/src/native/pe/PeResourceParser.test.ts
@@ -387,3 +387,88 @@ describe("PE resource section virtual extent", () => {
});
});
});
+
+describe("PE resource ownership boundaries", () => {
+ const singleResource = () =>
+ peResourceFixture(false, [
+ { type: 10, name: 1, language: 1033, payload: Buffer.from("DATA") },
+ ]);
+ const secondSection = (
+ fixture: ReturnType<typeof peResourceFixture>,
+ virtualSize: number,
+ rva: number,
+ rawSize: number,
+ ): Buffer => {
+ fixture.bytes.writeUInt16LE(2, 0x86);
+ const section = fixture.sectionAt + 40;
+ fixture.bytes.writeUInt32LE(virtualSize, section + 8);
+ fixture.bytes.writeUInt32LE(rva, section + 12);
+ fixture.bytes.writeUInt32LE(rawSize, section + 16);
+ if (rawSize === 0) return fixture.bytes;
+ fixture.bytes.writeUInt32LE(fixture.bytes.length, section + 20);
+ return Buffer.concat([fixture.bytes, Buffer.alloc(rawSize)]);
+ };
+
+ it("keeps locations and digests unchanged for a zero-extent section inside the resource interval", async () => {
+ const baseline = peResourcesSchema.parse(
+ await parse(singleResource().bytes),
+ );
+ const fixture = singleResource();
+ const bytes = secondSection(fixture, 0, 0x2001, 0);
+
+ const result = peResourcesSchema.parse(await parse(bytes));
+ expect(result.coverage).toMatchObject({ status: "complete" });
+ expect(result.resources[0]?.payload).toEqual(
+ baseline.resources[0]?.payload,
+ );
+ });
+
+ it("still rejects a positive-extent section inside the resource interval", async () => {
+ const fixture = singleResource();
+ const bytes = secondSection(fixture, 4, 0x2001, 0);
+ await expect(parse(bytes)).rejects.toMatchObject({
+ reason: "format",
+ message: "Resource interval intersects overlapping virtual sections.",
+ });
+ });
+
+ it("keeps a header-backed payload whose interval contains a zero-extent section", async () => {
+ const fixture = singleResource();
+ fixture.bytes.writeUInt32LE(0x100, fixture.dataOffsets[0]);
+ fixture.bytes.writeUInt32LE(4, (fixture.dataOffsets[0] ?? 0) + 4);
+ const bytes = secondSection(fixture, 0, 0x102, 0);
+
+ const result = peResourcesSchema.parse(await parse(bytes));
+ expect(result.resources[0]?.payload).toMatchObject({
+ rva: 0x100,
+ location: { offset: 0x100, bytes: 4 },
+ sha256: createHash("sha256")
+ .update(bytes.subarray(0x100, 0x104))
+ .digest("hex"),
+ });
+ });
+
+ it("rejects an empty header payload whose RVA starts a file-backed section", async () => {
+ const fixture = singleResource();
+ fixture.bytes.writeUInt32LE(0x100, fixture.dataOffsets[0]);
+ fixture.bytes.writeUInt32LE(0, (fixture.dataOffsets[0] ?? 0) + 4);
+ const bytes = secondSection(fixture, 4, 0x100, 4);
+ await expect(parse(bytes)).rejects.toMatchObject({
+ reason: "format",
+ message: "Ambiguous resource header RVA mapping.",
+ });
+ });
+
+ it("retains an empty header payload that no section owns", async () => {
+ const fixture = singleResource();
+ fixture.bytes.writeUInt32LE(0x100, fixture.dataOffsets[0]);
+ fixture.bytes.writeUInt32LE(0, (fixture.dataOffsets[0] ?? 0) + 4);
+
+ const result = peResourcesSchema.parse(await parse(fixture.bytes));
+ expect(result.resources[0]?.payload).toMatchObject({
+ rva: 0x100,
+ location: { offset: 0x100, bytes: 0 },
+ sha256: createHash("sha256").digest("hex"),
+ });
+ });
+});