Repository navigation
Resources: bound the parsed tree (self-referencing and overlapping directories) - #33
Merged
hasherezade merged 1 commit intoNov 7, 2026
Conversation
…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.
This was referenced Oct 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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.pygenerates synthetic PE32 files for three shapes (selfloop,chain,overlap).Verification
Release build with gcc on top of
master(c18e939),bearcommander <file>with therslcommand, 20 s limit and a 4 GB address-space limit:mastera5091fc2…eeff, checked)gen_rsrc_poc.py selfloop(100 entries)gen_rsrc_poc.py chain(100 entries)gen_rsrc_poc.py overlap(20000 entries)test.shagainstbearparser_tests: 26 cases print OK.x86/kernel32 : explistprints "Failed", and it fails identically on unmodifiedmaster, so it is not caused by this change. (test.shexits 0 either way.)Blocks
Blocks hasherezade/pe-bear#86: PE-bear takes
bearparseras a submodule, so its #77 stays open until this is merged and the pin is moved.