Skip to content

Commit

Permalink
Fix SIGFPE when dividing INT64_MIN by -1.
Browse files Browse the repository at this point in the history
Submitted by @vthib
  • Loading branch information
plusvic committed Dec 5, 2022
1 parent a3aa05b commit c2557fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
16 changes: 10 additions & 6 deletions libyara/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1049,10 +1049,12 @@ int yr_execute_code(YR_SCAN_CONTEXT* context)
pop(r1);
ensure_defined(r2);
ensure_defined(r1);
if (r2.i != 0)
r1.i = r1.i % r2.i;
else
// If divisor is zero the result is undefined. It's also undefined
// when dividing INT64_MIN by -1.
if (r2.i == 0 || (r1.i == INT64_MIN && r2.i == -1))
r1.i = YR_UNDEFINED;
else
r1.i = r1.i % r2.i;
push(r1);
break;

Expand Down Expand Up @@ -2099,10 +2101,12 @@ int yr_execute_code(YR_SCAN_CONTEXT* context)
pop(r1);
ensure_defined(r2);
ensure_defined(r1);
if (r2.i != 0)
r1.i = r1.i / r2.i;
else
// If divisor is zero the result is undefined. It's also undefined
// when dividing INT64_MIN by -1.
if (r2.i == 0 || (r1.i == INT64_MIN && r2.i == -1))
r1.i = YR_UNDEFINED;
else
r1.i = r1.i / r2.i;
push(r1);
break;

Expand Down
14 changes: 14 additions & 0 deletions tests/test-rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -3727,6 +3727,20 @@ void test_defined()
not defined ($a at pe.number_of_resources) \
}",
NULL);

// Test that operations that would trigger a SIGFPE are detected and
// returns undefined
assert_true_rule(
"rule t { \
strings: \
$a = /aaa/ \
condition: \
(not defined (1 \\ #a)) and \
(not defined (1 % #a)) and \
(not defined ((#a + -0x7FFFFFFFFFFFFFFF - 1) \\ -1)) and \
(not defined ((#a + -0x7FFFFFFFFFFFFFFF - 1) % -1)) \
}",
NULL);
}

static void test_pass(int pass)
Expand Down

0 comments on commit c2557fc

Please sign in to comment.