Skip to content

Commit

Permalink
Fix infinite loop while processing PE resource tree
Browse files Browse the repository at this point in the history
Resolve #1115
  • Loading branch information
romainthomas committed Oct 12, 2024
1 parent e98d6e7 commit e45dfb7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/PE/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,14 @@ Parser::parse_resource_node(const details::pe_resource_directory_table& director
uint32_t offset = base_offset + data_rva;
details::pe_resource_data_entry data_entry;

if (!resource_visited_.insert(offset).second) {
if (resource_visited_.size() == 1) {
// Only print once
LIEF_WARN("Infinite loop detected on resources");
}
break;
}

if (auto res_data_entry = stream_->peek<details::pe_resource_data_entry>(offset)) {
data_entry = *res_data_entry;
} else {
Expand Down Expand Up @@ -462,7 +470,10 @@ Parser::parse_resource_node(const details::pe_resource_directory_table& director
const uint32_t directory_rva = data_rva & (~ 0x80000000);
const uint32_t offset = base_offset + directory_rva;
if (!resource_visited_.insert(offset).second) {
LIEF_WARN("Infinite loop detected on resources");
if (resource_visited_.size() == 1) {
// Only print once
LIEF_WARN("Infinite loop detected on resources");
}
break;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/pe/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,11 @@ def test_issue_1049():
def test_xbox_file():
pe = lief.PE.parse(get_sample("PE/backcompat.exe"))
assert pe.header.machine == lief.PE.Header.MACHINE_TYPES.POWERPCBE

def test_issue_1115():
"""
Infinite loop in PE resource tree
"""
pe = lief.PE.parse(get_sample("PE/issue_1115.pe"))
assert pe is not None

0 comments on commit e45dfb7

Please sign in to comment.