Skip to content

PE: Back the full image declared by the section table - #790

Open
zardus wants to merge 2 commits into
masterfrom
feature/cle-pe-reloc
Open

zardus wants to merge 2 commits into
masterfrom
feature/cle-pe-reloc

Conversation

@zardus

@zardus zardus commented Aug 26, 2026 •

Copy link
Copy Markdown
Member

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Problem

cle maps fewer bytes than a PE object declares. In binaries/tests/i386/windows/rain32.upx, .rsrc declares memsize=0x1000 over filesize=0x600:

<PE Object rain32.upx, maps [0x400000:0x40afff]>
declares                      : 0x400000 .. 0x40afff  (0xb000 bytes)
mapped image the backend built: 0xa600 bytes

ld.memory.load(0x40afff, 1)  -> KeyError: 4239359    # last address the object declares
ld.memory.load(0x40a600, 1)  -> KeyError: 4236800    # the .rsrc tail

Every PE loses the last byte of its image this way. A section also loses its tail when its virtual size exceeds its raw data. All 106 PEs tracked by angr/binaries at fc07821c back fewer bytes than they declare.

A base relocation can place its field in the missing range. The relocation then gets a short buffer. IMAGE_REL_BASED_HIGHLOW raises struct.error, and Loader rejects the object.

Root cause

cle/backends/pe/pe.py has two separate range errors.

Backend.max_addr is the last covered address. It is not the first address after the object. The clamp used self.max_addr - self.min_addr, so it removed the last byte of the declared range.

_get_memory_mapped_image() also stops at the last section's raw data. It emits the zero padding for a section before the next section. The last section gets no padding. In rain32.upx, .rsrc is last, so its final 0xa00 bytes have no backing.

Fix

The clamp now uses max_addr - min_addr + 1. The image now gets zero padding through the highest virtual address + Misc_VirtualSize. This padding applies only when the file contains all of the section's raw data.

declares                      : 0x400000 .. 0x40afff  (0xb000 bytes)
mapped image the backend built: 0xb000 bytes

ld.memory.load(0x40afff, 1)  -> 00
ld.memory.load(0x40a600, 1)  -> 00

A section keeps an unbacked tail when its raw data extends beyond the file. Those bytes are unknown, so this change does not fill them with zeroes.

angr/angr#6943 treats an unbacked byte differently from a real zero byte. The merge order can therefore change the recovered string set. The first coupling note explains this interaction.

cle#835 changes the same PE code. The two changes are independent, and neither loses a byte. The second coupling note records that interaction.

session: sharpen

@zardus

zardus commented Aug 26, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

This evidence applies to head 0015420b2920a8faebb8920967882071787f4c0f and baseline 0e77ade3c39a3cee05f65051e57955675e1ac21b.

On the baseline, a load through max_addr returns one byte fewer than the object declares. The baseline also leaves the last 0xa00 bytes of .rsrc unbacked in tests/i386/windows/rain32.upx. A load from 0x40a600 raises KeyError: 4236800. This head backs both ranges.

At angr/binaries revision fc07821c, all 106 PE files have a mapping shortfall on the baseline. This head reduces that count to 1 of 106. The remaining file is tests/i386/windows/a94bbeed0ef51db3d3964bb0cc2cbed0adab0e47997d88f34daa92faa1a91e8a. Its section data extends beyond the file, so it remains short by 10377 bytes. cle#794 tracks that case. Neither arm has a load error in this set.

Master changed PE.__init__ in #808. The PE.gopclntab value and symbol count are identical between the baseline and this head for all 106 files. Eleven files contain a Go pclntab. Both revisions register the same symbols for all eleven files.

cle#835 changes the same PE code. A tree with both changes still has the same single short file out of 106. The separate coupling note gives the mapping details.

An earlier paired census loaded 493 objects on each arm. The set contained 92 PE files from angr/binaries, 24 from DecBench, and 377 from a local collection. Forty-five public fixtures were byte-identical to objects in the local collection, so the union contained 422 distinct binaries. Every load produced a record.

No mapped image became shorter. No object changed min_addr or max_addr. No object gained a shortfall. The 480 objects with unchanged image lengths also had identical image bytes. Twelve shortfalls closed, and three remained. All three remaining shortfalls involved section data beyond the file.

The change adds 72,615 zero bytes across 13 of the 493 loads. It adds no bytes to the other 480 loads. The largest increase is 38,511 bytes. The largest increase in angr/binaries is 3,584 bytes in tests/x86/windows/packed_pe32.exe.

CFGFast(normalize=True) produced the same function and node address sets before and after this change for rain32.upx, packed_pe32.exe, 9f2ef84bde1e..., a94bbeed0ef5..., and simple_windows.exe. Each file gained 0 addresses and lost 0 addresses.

angr/binaries has no .NET assembly and no PE with a base relocation in the final bytes of its image. No committed fixture reproduces the loader failure from start to finish. Third-party samples do reproduce it. The samples cannot be shared.

The branch was rebased from head 9eab3c507a539bbd785e9661b919185fe0b0fb20 on base eac0e5540516b9199dd6a91933e80dc774ea3eac to the current head and baseline. git range-diff eac0e5540516b9199dd6a91933e80dc774ea3eac..9eab3c507a539bbd785e9661b919185fe0b0fb20 0e77ade3c39a3cee05f65051e57955675e1ac21b..0015420b2920a8faebb8920967882071787f4c0f marks both commits as unchanged. tests/test_pe.py has the same blob at both heads.

cle/backends/pe/pe.py has a different blob because master commit b6ff025b from #808 added four lines. It added one import. It also added a comment, an assignment, and a blank line in PE.__init__. This PR also changes that function.

The two commit messages keep their original text so the rebase remains easy to compare. One message says "88 of the 92 by exactly one byte." That figure used angr/binaries revision 8646be4e. The current count is 106 of 106 on the baseline.

@angr-bot

Copy link
Copy Markdown
Member

Corpus decompilation diffs can be found at angr/dec-snapshots@master...angr/cle_790

@zardus zardus changed the title PE: Back the last byte of the image the section table declares PE: Back the whole image the section table declares Aug 26, 2026
@zardus

zardus commented Aug 28, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

This change affects string recovery in angr.

Zero-filling a PE section's unmapped tail makes CFGFast._load_a_byte_as_int return zero for those addresses instead of None. CFGFast._scan_for_printable_strings handles these values differently. It uses if val is None: break for an unbacked byte. It uses if val == 0: break for a real zero byte.

angr/angr#6943 adds is_sz = False to the None branch. That branch then needs 13 printable bytes instead of 3. After this PR, PE runs take the zero-byte branch and qualify at 3.

In binaries/tests/i386/windows/rain32.upx, .rsrc has memsize=0x1000 and filesize=0x600. On current cle, ld.memory.load(0x40a600, 1) raises KeyError.

The two PRs can therefore produce different recovered string sets. The result depends on their merge order.

@zardus

zardus commented Aug 28, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

This output compares tests/i386/windows/rain32.upx before and after the change. Both runs use cle.Loader(path, auto_load_libs=False) and the two shown ld.memory.load calls. The final section is .rsrc. It declares memsize=0x1000 over filesize=0x600.

Before the change, the backend builds 0xa600 bytes for an object that declares 0xb000 bytes. The object does not back its last address or the uninitialized .rsrc tail.

cle master (d2ecea0)
binary                       : tests/i386/windows/rain32.upx
cle                          : master d2ecea0
object                       : <PE Object rain32.upx, maps [0x400000:0x40afff]>
declares                     : 0x400000 .. 0x40afff  (0xb000 bytes)
mapped image the backend built: 0xa600 bytes

section table:
  UPX0   vaddr=0x00401000 memsize=0x07000 filesize=0x00000
  UPX1   vaddr=0x00408000 memsize=0x02000 filesize=0x01800
  .rsrc  vaddr=0x0040a000 memsize=0x01000 filesize=0x00600

ld.memory.load(0x40afff, 1)  -> KeyError: 4239359    # last address the object declares
ld.memory.load(0x40a600, 1)  -> KeyError: 4236800    # .rsrc tail: memsize 0x1000 over filesize 0x600

After the change, the image covers the declared range. The .rsrc tail reads as zeroes.

with this change (6ae4329)
binary                       : tests/i386/windows/rain32.upx
cle                          : this change 6ae4329
object                       : <PE Object rain32.upx, maps [0x400000:0x40afff]>
declares                     : 0x400000 .. 0x40afff  (0xb000 bytes)
mapped image the backend built: 0xb000 bytes

section table:
  UPX0   vaddr=0x00401000 memsize=0x07000 filesize=0x00000
  UPX1   vaddr=0x00408000 memsize=0x02000 filesize=0x01800
  .rsrc  vaddr=0x0040a000 memsize=0x01000 filesize=0x00600

ld.memory.load(0x40afff, 1)  -> 00    # last address the object declares
ld.memory.load(0x40a600, 1)  -> 00    # .rsrc tail: memsize 0x1000 over filesize 0x600

@zardus
zardus force-pushed the feature/cle-pe-reloc branch from 6ae4329 to d34beb5 Compare August 29, 2026 06:47
@zardus

zardus commented Aug 29, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

The branch was rebased onto cle master eac0e554 after #800 made it conflict. The new head was 9eab3c50.

The conflict was in tests/test_pe.py. #800 added test_uefi_image_is_not_windows at the same location where this branch adds test_mapped_image_covers_max_addr. The rebased branch keeps both tests. The pe.py changes stayed unchanged.

The branch diff has the same added and removed lines before and after the rebase.

@zardus
zardus force-pushed the feature/cle-pe-reloc branch from d34beb5 to 9eab3c5 Compare August 29, 2026 19:15
@zardus

zardus commented Aug 29, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

This corpus result uses a separate denominator from the PR evidence.

The change fixes this load failure:
struct.error: unpack requires a buffer of 4 bytes from
cle/backends/pe/relocation/generic.py:67 in IMAGE_REL_BASED_HIGHLOW.value.

The clamp in PE.__init__ uses self.max_addr - self.min_addr. Region.max_addr is the last covered address, so the backer ends one byte before the declared image ends. Clemory.load returns the available bytes and stops at an unallocated region. It does not raise an error there. A four-byte base relocation can therefore receive only three bytes. struct.unpack then raises.

One affected object has a relocation at RVA 0xa200c. Its declared image ends at 0xa2010. The mapped image contained the byte at 0xa200f, but the clamp removed it.

Both arms use the same objects, matched by SHA-256. Each arm loads them with cle.Loader(path, auto_load_libs=False). On cle master c7e0d4db, all 5 affected objects raise at generic.py:67. On this branch at 9eab3c50, all 5 load.

The collection contains 385 objects. On master, 375 objects load. This branch does not change the result class for any of those 375 objects. The five fixed objects are the only regression-related difference.

Six other pull requests and the current master head fix 0 of the 5 objects. Those changes affect ELF, COFF, DWARF, or the PEReloc base that this relocation class overrides.

The five objects are 1.30% of this 385-object collection. The Wilson 95% interval is 0.56–3.00%. A separate uniform sample contains 3,422 PE objects in a set of 11,989 objects. None of those PE objects hit this crash, so that sample gives a rate below 0.12% at 95%.

All five affected objects are 32-bit x86 PE files between 620 and 660 KB. Each has one symbol and three relocations. The relocations are one DllImport, one IMAGE_REL_BASED_ABSOLUTE, and one IMAGE_REL_BASED_HIGHLOW that fails.

The five samples cannot be shared. None is byte-identical to a fixture in the binaries repository. None of the 95 PE fixtures there has a relocation on the lost byte. No public sample reproduces this crash.

session: sharpen

zardus and others added 2 commits September 8, 2026 17:44
Backend.max_addr is the last address an object covers, not one past it, so the
mapped image spans max_addr - min_addr + 1 bytes. The backend clamped it to the
difference alone and dropped the final byte of the last section. The clamp is
not a rare path: _get_memory_mapped_image() builds from raw section data and
pads between sections, so it usually runs past the last section's virtual end
and the branch is taken. Every PE in angr/binaries ends up with part of its
declared range unbacked, 88 of the 92 by exactly one byte.

Clemory.load returns up to the length it is asked for, so a base relocation
whose field ends on the missing byte reads short and IMAGE_REL_BASED_HIGHLOW
raises struct.error out of Backend.relocate, through Loader, and out of
Project: the binary does not load at all. Five PE objects in a corpus sweep
failed this way, each one a .NET assembly whose single fixup patches the
operand of the jmp [_CorExeMain] stub in a sixteen-byte final section.

Loader already sizes an object as max_addr - min_addr + 1 and hands the next
one the space above max_addr, so the byte was already this object's own.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
A section is mapped over its whole virtual size, and the bytes past the raw
data the file holds for it are zero. That padding is emitted ahead of the
next section, so the last section of all has nothing to back its own, and
the image ends at its raw data while max_addr covers its virtual size.

A section the file cuts short is excluded: those bytes are unknown rather
than zero, which is what PE.Preserve incomplete sections already decided.
@zardus
zardus force-pushed the feature/cle-pe-reloc branch from 9eab3c5 to 0015420 Compare September 8, 2026 19:04
@zardus

zardus commented Sep 8, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

cle#835 changes PESection.memsize to the larger of Misc_VirtualSize and the section's raw size. It bounds that value by the file and SizeOfImage. Its description says this PR then makes image_end too small. image_end is smaller, but the mapped image loses no bytes.

This PR computes image_end as the maximum of va_adj + Misc_VirtualSize. After #835 raises memsize above Misc_VirtualSize, image_end can sit below the top of the image. This happens for 98 of the 106 PE files tracked by angr/binaries at fc07821c when both changes are present. On this PR alone, it happens for 1 of 106 files.

image_end only controls the zero padding after a section's raw data. The larger memsize does not change the final mapped image.

cle.Loader(path, auto_load_libs=False) gives these mapping counts:

cle master 0e77ade3                    106 of 106 short   (102 of them by exactly 1 byte)
master + this PR                         1 of 106 short   (the cle#794 case, short by 10377)
master + this PR + cle#835               1 of 106 short   (same file, same amount)

All three arms have 0 load errors.

The alternative formula is image_end = max(image_end, va_adj + max(Misc_VirtualSize, size)). Here, size is the file-bounded raw size already computed by the loop. This formula changes the mapped image length for 0 of the 106 files. This PR therefore keeps Misc_VirtualSize.

The two changes are independent. They can land in either order.

session: sharpen

@zardus zardus changed the title PE: Back the whole image the section table declares PE: Back the full image declared by the section table Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants