Skip to content

Commit 4b9b4c0

Browse files
committed
Add sanity check that prevents OOM errors.
The `pe_parse_delayed_imports` function didn't take into account that `pe_rva_to_offset` could return -1 when the RVA can't be translated into a file offset.
1 parent adf3dde commit 4b9b4c0

File tree

1 file changed

+22
-17
lines changed
  • libyara/modules/pe

1 file changed

+22
-17
lines changed

libyara/modules/pe/pe.c

+22-17
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,10 @@ uint64_t pe_parse_delay_import_pointer(
12791279
uint64_t rva)
12801280
{
12811281
const int64_t offset = pe_rva_to_offset(pe, rva);
1282+
1283+
if (offset < 0)
1284+
return YR_UNDEFINED;
1285+
12821286
const uint8_t* data = pe->data + offset;
12831287

12841288
if (!fits_in_pe(pe, data, pointerSize))
@@ -1419,17 +1423,8 @@ static void* pe_parse_delayed_imports(PE* pe)
14191423
if (nameAddress == 0 || funcAddress == 0)
14201424
break;
14211425

1422-
IMPORT_FUNCTION* imported_func = (IMPORT_FUNCTION*) yr_malloc(
1423-
sizeof(IMPORT_FUNCTION));
1424-
1425-
if (imported_func == NULL)
1426-
continue;
1427-
1428-
imported_func->name = NULL;
1429-
imported_func->has_ordinal = 0;
1430-
imported_func->ordinal = 0;
1431-
imported_func->rva = 0;
1432-
imported_func->next = NULL;
1426+
char* func_name;
1427+
uint8_t has_ordinal = 0;
14331428

14341429
// Check name address. It could be ordinal, VA or RVA
14351430
if (!(nameAddress & ordinal_mask))
@@ -1441,21 +1436,31 @@ static void* pe_parse_delayed_imports(PE* pe)
14411436

14421437
offset = pe_rva_to_offset(pe, nameAddress + sizeof(uint16_t));
14431438

1444-
imported_func->name = (char*) yr_strndup(
1439+
if (offset < 0)
1440+
continue;
1441+
1442+
func_name = (char*) yr_strndup(
14451443
(char*) (pe->data + offset),
14461444
yr_min(available_space(pe, (char*) (pe->data + offset)), 512));
14471445
}
14481446
else
14491447
{
14501448
// If imported by ordinal. Lookup the ordinal.
1451-
imported_func->name = ord_lookup(dll_name, nameAddress & 0xFFFF);
1452-
1453-
// Also store the ordinal.
1454-
imported_func->ordinal = nameAddress & 0xFFFF;
1455-
imported_func->has_ordinal = 1;
1449+
func_name = ord_lookup(dll_name, nameAddress & 0xFFFF);
1450+
has_ordinal = 1;
14561451
}
14571452

1453+
IMPORT_FUNCTION* imported_func = (IMPORT_FUNCTION*) yr_malloc(
1454+
sizeof(IMPORT_FUNCTION));
1455+
1456+
if (imported_func == NULL)
1457+
continue;
1458+
1459+
imported_func->name = func_name;
14581460
imported_func->rva = func_rva;
1461+
imported_func->has_ordinal = has_ordinal;
1462+
imported_func->ordinal = (has_ordinal) ? nameAddress & 0xFFFF : 0;
1463+
imported_func->next = NULL;
14591464

14601465
num_function_imports++;
14611466
name_rva += pointer_size;

0 commit comments

Comments
 (0)