Skip to content

Commit

Permalink
Fix issue #658
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed May 14, 2017
1 parent c2ed7a4 commit 053e67e
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libyara/arena.c
Expand Up @@ -316,7 +316,7 @@ void yr_arena_destroy(
// YR_ARENA* arena - Pointer to the arena.
//
// Returns:
// A pointer to the arena's data. NULL if the no data has been written to
// A pointer to the arena's data. NULL if no data has been written to
// the arena yet.
//

Expand Down
44 changes: 36 additions & 8 deletions libyara/exec.c
Expand Up @@ -34,6 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <time.h>
#include <math.h>

#include <yara/arena.h>
#include <yara/endian.h>
#include <yara/exec.h>
#include <yara/limits.h>
Expand Down Expand Up @@ -177,6 +178,8 @@ int yr_execute_code(
YR_RULE* rule;
YR_MATCH* match;
YR_OBJECT_FUNCTION* function;
YR_OBJECT** obj_ptr;
YR_ARENA* obj_arena;

char* identifier;
char* args_fmt;
Expand All @@ -201,6 +204,10 @@ int yr_execute_code(
if (stack == NULL)
return ERROR_INSUFFICIENT_MEMORY;

FAIL_ON_ERROR_WITH_CLEANUP(
yr_arena_create(1024, 0, &obj_arena),
yr_free(stack));

while(!stop)
{
switch(*ip)
Expand Down Expand Up @@ -437,6 +444,8 @@ int yr_execute_code(
rule->clock_ticks += clock() - start;
start = clock();
#endif

assert(sp == 0);
break;

case OP_OBJ_LOAD:
Expand Down Expand Up @@ -577,18 +586,26 @@ int yr_execute_code(
}
}

// if i == MAX_OVERLOADED_FUNCTIONS at this point no matching
// prototype was found, but this shouldn't happen.

assert(i < MAX_OVERLOADED_FUNCTIONS);

// make a copy of the returned object and push the copy into the stack
// function->return_obj can't be pushed because it can change in
// subsequent calls to the same function.

if (result == ERROR_SUCCESS)
{
r1.o = function->return_obj;
push(r1);
}
else
{
stop = TRUE;
}
result = yr_object_copy(function->return_obj, &r1.o);

// a pointer to the copied object is stored in a arena in order to
// free the object before exiting yr_execute_code

if (result == ERROR_SUCCESS)
result = yr_arena_write_data(obj_arena, &r1.o, sizeof(r1.o), NULL);

stop = (result != ERROR_SUCCESS);
push(r1);
break;

case OP_FOUND:
Expand Down Expand Up @@ -1146,6 +1163,17 @@ int yr_execute_code(
ip++;
}

obj_ptr = (YR_OBJECT**) yr_arena_base_address(obj_arena);

while (obj_ptr != NULL)
{
yr_object_destroy(*obj_ptr);

obj_ptr = (YR_OBJECT**) yr_arena_next_address(
obj_arena, obj_ptr, sizeof(YR_OBJECT*));
}

yr_arena_destroy(obj_arena);
yr_modules_unload_all(context);
yr_free(stack);

Expand Down
5 changes: 5 additions & 0 deletions libyara/include/yara/object.h
Expand Up @@ -86,6 +86,11 @@ void yr_object_destroy(
YR_OBJECT* object);


int yr_object_copy(
YR_OBJECT* object,
YR_OBJECT** object_copy);


YR_OBJECT* yr_object_lookup_field(
YR_OBJECT* object,
const char* field_name);
Expand Down
10 changes: 7 additions & 3 deletions libyara/include/yara/sizedstr.h
Expand Up @@ -51,7 +51,7 @@ typedef struct _SIZED_STRING
{
uint32_t length;
uint32_t flags;

char c_string[1];

} SIZED_STRING;
Expand All @@ -60,7 +60,11 @@ typedef struct _SIZED_STRING


int sized_string_cmp(
SIZED_STRING* s1,
SIZED_STRING* s2);
SIZED_STRING* s1,
SIZED_STRING* s2);


SIZED_STRING* sized_string_dup(
SIZED_STRING* s);

#endif
18 changes: 18 additions & 0 deletions libyara/modules/tests.c
Expand Up @@ -88,6 +88,23 @@ define_function(match)
}


define_function(foobar)
{
int64_t arg = integer_argument(1);

switch (arg)
{
case 1:
return_string("foo");
break;
case 2:
return_string("bar");
break;
}

return_string("oops")
}

begin_declarations;

begin_struct("constants");
Expand Down Expand Up @@ -125,6 +142,7 @@ begin_declarations;
declare_function("fsum", "fff", "f", fsum_3);
declare_function("length", "s", "i", length);
declare_function("empty", "", "s", empty);
declare_function("foobar", "i", "s", foobar);

end_declarations;

Expand Down
16 changes: 14 additions & 2 deletions libyara/object.c
Expand Up @@ -573,11 +573,23 @@ int yr_object_copy(
switch(object->type)
{
case OBJECT_TYPE_INTEGER:
((YR_OBJECT_INTEGER*) copy)->value = UNDEFINED;
((YR_OBJECT_INTEGER*) copy)->value = ((YR_OBJECT_INTEGER*) object)->value;
break;

case OBJECT_TYPE_STRING:
((YR_OBJECT_STRING*) copy)->value = NULL;
if (((YR_OBJECT_STRING*) object)->value != NULL)
{
((YR_OBJECT_STRING*) copy)->value = sized_string_dup(
((YR_OBJECT_STRING*) object)->value);
}
else
{
((YR_OBJECT_STRING*) copy)->value = NULL;
}
break;

case OBJECT_TYPE_FLOAT:
((YR_OBJECT_DOUBLE*) copy)->value = ((YR_OBJECT_DOUBLE*) object)->value;
break;

case OBJECT_TYPE_FUNCTION:
Expand Down
20 changes: 20 additions & 0 deletions libyara/sizedstr.c
Expand Up @@ -27,6 +27,8 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <string.h>
#include <yara/mem.h>
#include <yara/sizedstr.h>


Expand Down Expand Up @@ -54,3 +56,21 @@ int sized_string_cmp(
else
return 1;
}


SIZED_STRING* sized_string_dup(
SIZED_STRING* s)
{
SIZED_STRING* result = (SIZED_STRING*) yr_malloc(
sizeof(SIZED_STRING) + s->length);

if (result == NULL)
return NULL;

result->length = s->length;
result->flags = s->flags;

strncpy(result->c_string, s->c_string, s->length + 1);

return result;
}
14 changes: 14 additions & 0 deletions tests/test-rules.c
Expand Up @@ -1439,6 +1439,20 @@ static void test_modules()
}",
NULL);

assert_true_rule(
"import \"tests\" \
rule test { \
condition: tests.foobar(1) == tests.foobar(1) \
}",
NULL);

assert_true_rule(
"import \"tests\" \
rule test { \
condition: tests.foobar(1) != tests.foobar(2) \
}",
NULL);

assert_true_rule(
"import \"tests\" \
rule test { \
Expand Down

0 comments on commit 053e67e

Please sign in to comment.