Repository navigation
Description
Describe the bug
Parser::read_section_content (src/PE/Parser.cpp) copies each section's bytes into its own buffer. The size is clamped to the end of the file, but only per section, and several headers may point at the same bytes. If every section header claims the whole file, the file is held once per header, up to the NB_MAX_SECTIONS limit of 1000.
To Reproduce
The script builds the PE in memory.
import resource
import struct
import sys
import lief
SECTIONS = int(sys.argv[1]) if len(sys.argv) > 1 else 1000
SIZE = int(sys.argv[2]) if len(sys.argv) > 2 else 1 << 20
ALIGNMENT = 0x1000
dos = bytearray(0x40)
dos[0:2] = b"MZ"
struct.pack_into("<I", dos, 0x3C, 0x40)
coff = struct.pack("<HHIIIHH", 0x14C, SECTIONS, 0, 0, 0, 0xE0, 0x0102)
span = (SIZE + ALIGNMENT - 1) // ALIGNMENT * ALIGNMENT
table = b"".join(
struct.pack("<8sIIIIIIHHI", b".s%d" % i, SIZE, ALIGNMENT + i * span, SIZE, 0, 0, 0, 0, 0, 0x40000040)
for i in range(SECTIONS)
)
optional = bytearray(0xE0)
struct.pack_into("<H", optional, 0, 0x10B)
struct.pack_into("<I", optional, 28, 0x400000)
struct.pack_into("<II", optional, 32, ALIGNMENT, 0x200)
struct.pack_into("<I", optional, 56, ALIGNMENT + SECTIONS * span)
struct.pack_into("<I", optional, 60, 0x40 + 4 + len(coff) + len(optional) + len(table))
struct.pack_into("<H", optional, 68, 3)
struct.pack_into("<I", optional, 92, 16)
raw = (bytes(dos) + b"PE\0\0" + coff + bytes(optional) + table).ljust(SIZE, b"\xcc")
before = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
binary = lief.PE.parse(raw)
after = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
held = sum(len(section.content) for section in binary.sections)
print(f"LIEF {lief.__version__}")
print(f"file: {len(raw)} bytes, {SECTIONS} section headers")
print(f"sections parsed: {len(binary.sections)}, content held: {held} bytes ({held / len(raw):.0f}x the file)")
print(f"peak memory growth: {(after - before) >> 10} MiB")$ python repro.py 1000 1048576
LIEF 1.0.0-d05b3499b
file: 1048576 bytes, 1000 section headers
sections parsed: 1000, content held: 1048576000 bytes (1000x the file)
peak memory growth: 1004 MiB
Expected behavior
Memory should not grow with the number of headers that point at the same bytes.
Environment
- System and Version: Ubuntu 24.04 under WSL, Python 3.12.3
- Target format: PE
- LIEF commit version: 1.0.0-d05b3499b from PyPI. Same numbers on the 2.0.0 nightly of 8 Nov 2026.
Additional context
Default ParserConfig. No option skips section content. Possibly related to #58.
I hit this while writing a tool that runs LIEF on untrusted binaries. It now checks the section table before calling LIEF.
Mach-O segments look the same to me. Each LC_SEGMENT_64 is copied by peek_data(segment->data_, ...) in src/MachO/BinaryParser.tcc, without the limit of 1,000. A 256 KiB file with 3,000 segment commands holds 750 MiB. I can post that script too.
An AI assistant helped me write the reproduction scripts and run the measurements. I found the PE problem myself. The Mach-O case turned up while I was looking into it with the assistant.
Activity