CVE-2017-11328: Improper Restriction of Operations within the Bounds of a Memory Buffer (CWE-119)
value: Heap buffer overflow in the yr_object_array_set_item() function in object.c in YARA 3.x allows a denial-of-service attack by scanning a crafted .NET file.
CWE description: The product performs operations on a memory buffer, but it reads from or writes to a memory location outside the buffer's intended boundary. This may result in read or write operations on unexpected memory locations that could be linked to other variables, data structures, or internal program data.
CVSS v3 base score: 5.5
Published: 2017-07-17T13:18Z
Repository: https://github.com/VirusTotal/yara
Language: C
Files changed: libyara/modules/tests.c, libyara/object.c, tests/test-rules.c
Commit date: 2017-07-05 10:01:03 +0200
Commit message: Fix heap overflow (reported by Jurriaan Bremer)

When setting a new array item with yr_object_array_set_item() the array size is doubled if the index for the new item is larger than the already allocated ones. No further checks were made to ensure that the index fits into the array after doubling its capacity. If the array capacity was for example 64, and a new object is assigned to an index larger than 128 the overflow occurs. As yr_object_array_set_item() is usually invoked with indexes that increase monotonically by one, this bug never triggered before. But the new "dotnet" module has the potential to allow the exploitation of this bug by scanning a specially crafted .NET binary.

Diff: diff --git a/libyara/modules/tests.c b/libyara/modules/tests.c
index a1526383..4eacc365 100644
--- a/libyara/modules/tests.c
+++ b/libyara/modules/tests.c
@@ -174,10 +174,11 @@ int module_load(
   set_integer(1, module_object, "struct_array[1].i");
 
   set_integer(0, module_object, "integer_array[%i]", 0);
   set_integer(1, module_object, "integer_array[%i]", 1);
   set_integer(2, module_object, "integer_array[%i]", 2);
+  set_integer(256, module_object, "integer_array[%i]", 256);
 
   set_string("foo", module_object, "string_array[%i]", 0);
   set_string("bar", module_object, "string_array[%i]", 1);
   set_string("baz", module_object, "string_array[%i]", 2);
 
diff --git a/libyara/object.c b/libyara/object.c
index b130278e..1572a131 100644
--- a/libyara/object.c
+++ b/libyara/object.c
@@ -723,11 +723,14 @@ int yr_object_array_set_item(
 
   array = object_as_array(object);
 
   if (array->items == NULL)
   {
-    count = yr_max(64, (index + 1) * 2);
+    count = 64;
+
+    while (count <= index)
+      count *= 2;
 
     array->items = (YR_ARRAY_ITEMS*) yr_malloc(
         sizeof(YR_ARRAY_ITEMS) + count * sizeof(YR_OBJECT*));
 
     if (array->items == NULL)
@@ -738,10 +741,14 @@ int yr_object_array_set_item(
     array->items->count = count;
   }
   else if (index >= array->items->count)
   {
     count = array->items->count * 2;
+
+    while (count <= index)
+      count *= 2;
+
     array->items = (YR_ARRAY_ITEMS*) yr_realloc(
         array->items,
         sizeof(YR_ARRAY_ITEMS) + count * sizeof(YR_OBJECT*));
 
     if (array->items == NULL)
diff --git a/tests/test-rules.c b/tests/test-rules.c
index f03a7b92..1897499d 100644
--- a/tests/test-rules.c
+++ b/tests/test-rules.c
@@ -1393,10 +1393,17 @@ static void test_modules()
        rule test { \
         condition: tests.integer_array[1] == 1 \
       }",
       NULL);
 
+  assert_true_rule(
+      "import \"tests\" \
+       rule test { \
+        condition: tests.integer_array[256] == 256 \
+      }",
+      NULL);
+
   assert_true_rule(
       "import \"tests\" \
        rule test { \
         condition: tests.string_array[0] == \"foo\" \
       }",

Vulnerable code:     count = yr_max(64, (index + 1) * 2);
Fixed code:   set_integer(256, module_object, "integer_array[%i]", 256);
    count = 64;

    while (count <= index)
      count *= 2;

    while (count <= index)
      count *= 2;

  assert_true_rule(
      "import \"tests\" \
       rule test { \
        condition: tests.integer_array[256] == 256 \
      }",
      NULL);