Skip to content

Commit 6209630

Browse files
committed
Better sanitation while parsing corrupted resources.
1 parent 80972f0 commit 6209630

File tree

1 file changed

+20
-15
lines changed
  • libyara/modules/pe

1 file changed

+20
-15
lines changed

libyara/modules/pe/pe.c

+20-15
Original file line numberDiff line numberDiff line change
@@ -502,17 +502,20 @@ static int _pe_iterate_resources(
502502

503503
if (struct_fits_in_pe(pe, data_entry, IMAGE_RESOURCE_DATA_ENTRY))
504504
{
505-
if (callback(
506-
data_entry,
507-
*type,
508-
*id,
509-
*language,
510-
type_string,
511-
name_string,
512-
lang_string,
513-
callback_data) == RESOURCE_CALLBACK_ABORT)
505+
if (data_entry->Size > 0 && data_entry->Size < pe->data_size)
514506
{
515-
result = RESOURCE_ITERATOR_ABORTED;
507+
if (callback(
508+
data_entry,
509+
*type,
510+
*id,
511+
*language,
512+
type_string,
513+
name_string,
514+
lang_string,
515+
callback_data) == RESOURCE_CALLBACK_ABORT)
516+
{
517+
result = RESOURCE_ITERATOR_ABORTED;
518+
}
516519
}
517520
}
518521
}
@@ -716,7 +719,8 @@ static void pe_set_resource_string_or_id(
716719
}
717720
else
718721
{
719-
yr_set_integer(rsrc_int, pe->object, int_description, pe->resources);
722+
if (rsrc_int != -1)
723+
yr_set_integer(rsrc_int, pe->object, int_description, pe->resources);
720724
}
721725
}
722726

@@ -731,7 +735,7 @@ static int pe_collect_resources(
731735
PE* pe)
732736
{
733737
// Don't collect too many resources.
734-
if (pe->resources > MAX_RESOURCES)
738+
if (pe->resources >= MAX_RESOURCES)
735739
return RESOURCE_CALLBACK_CONTINUE;
736740

737741
yr_set_integer(
@@ -2007,9 +2011,10 @@ const char* pe_get_section_full_name(
20072011
for (uint64_t len = 0; fits_in_pe(pe, string, len + 1); len++)
20082012
{
20092013
// Prevent sign extension to 32-bits on bytes > 0x7F
2010-
// The result negative integer would cause assert in MSVC debug version of isprint()
2011-
unsigned int one_char = (unsigned char)(string[len]);
2012-
2014+
// The result negative integer would cause assert in MSVC debug version of
2015+
// isprint()
2016+
unsigned int one_char = (unsigned char) (string[len]);
2017+
20132018
// Valid string
20142019
if (one_char == 0)
20152020
{

0 commit comments

Comments
 (0)