Skip to content

Resources: bound the parsed tree (self-referencing and overlapping directories) - #33

Merged
hasherezade merged 1 commit into
hasherezade:masterfrom
mkilijanek:fix/resource-tree-bound
Nov 7, 2026
Merged

hasherezade merged 1 commit into
hasherezade:masterfrom
mkilijanek:fix/resource-tree-bound

Conversation

@mkilijanek

@mkilijanek mkilijanek commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Refs hasherezade/pe-bear#77.

A resource directory whose entries point back at itself (or at the top directory), or a chain of distinct directories whose N entries all point at the next one, makes the parsed tree grow as N^MAX_DEPTH. Only the depth (MAX_DEPTH 5) limits it, since the per-directory entry limit was removed. With 100 entries the parser does not return and memory grows until the process is killed.

Change

Two bounds, neither of which limits a well-formed resource tree:

  1. Every directory is expanded at most once per tree (a set of raw offsets held by the top directory). This removes cycles and shared children.
  2. The tree has a budget of getRawSize() / sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY) entries. In a well-formed tree each entry occupies its own bytes, so the budget cannot be exceeded. It is needed because directories of a malformed file may overlap (a directory header placed over the entries of another directory), so distinct offsets can still share the same bytes as entries; the first bound alone does not stop that.

Both are reset by the top directory's wrap(), the only place where a resource tree is rewrapped.

Behaviour change: a directory referenced by more than one entry is expanded only under the first one.

tests/gen_rsrc_poc.py generates synthetic PE32 files for three shapes (selfloop, chain, overlap).

Verification

Release build with gcc on top of master (c18e939), bearcommander <file> with the rsl command, 20 s limit and a 4 GB address-space limit:

Input master with this change
PoC attached to hasherezade/pe-bear#77 (sha256 a5091fc2…eeff, checked) no result in 20 s 0.01 s
gen_rsrc_poc.py selfloop (100 entries) no result in 20 s 0.01 s
gen_rsrc_poc.py chain (100 entries) no result in 20 s 0.01 s
gen_rsrc_poc.py overlap (20000 entries) no result in 20 s 0.03 s

test.sh against bearparser_tests: 26 cases print OK. x86/kernel32 : explist prints "Failed", and it fails identically on unmodified master, so it is not caused by this change. (test.sh exits 0 either way.)

Blocks

Blocks hasherezade/pe-bear#86: PE-bear takes bearparser as a submodule, so its #77 stays open until this is merged and the pin is moved.

…apping directories)

Since "Removed limit for resources to be parsed" the number of entries per
directory is unbounded and only the depth (MAX_DEPTH 5) limits the tree. A
directory whose entries point back at itself (or at the top directory), or a
chain of distinct directories whose N entries all point at the next one, makes
the parsed tree grow as N^MAX_DEPTH. With N=100 the parser does not return and
memory grows until the process is killed (hasherezade/pe-bear#77).

Two bounds, neither of which limits a well-formed resource tree:

1. Every directory is expanded at most once per tree (a set of raw offsets
   held by the top directory). This removes cycles and shared children.
2. The tree has a budget of getRawSize() / sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY)
   entries. In a well-formed tree every entry occupies its own bytes, so the
   budget cannot be exceeded. It is needed because the directories of a
   malformed file may overlap (a directory header placed over the entries of
   another directory), so that distinct offsets still share the same bytes as
   entries; the first bound alone does not stop that.

Both are reset by the top directory's wrap(), the only place where a resource
tree is rewrapped.

Behaviour change: a directory referenced by more than one entry is expanded
only under the first one.

tests/gen_rsrc_poc.py generates synthetic PE32 files for the three shapes
(selfloop, chain, overlap); run bearcommander on them with the `rsl` command.
Without this change all three run past a 20 s limit; with it they finish in
under 0.1 s. The tests_list.txt cases give the same results before and after.
@hasherezade
hasherezade merged commit 56fdd4b into hasherezade:master Nov 7, 2026
2 checks passed
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