Skip to content

Commit b77e4f4

Browse files
committed
Prevent undefined values in offset ranges from corrupting the VM's stack.
1 parent f90cd22 commit b77e4f4

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

libyara/exec.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -1610,11 +1610,20 @@ int yr_execute_code(YR_SCAN_CONTEXT* context)
16101610

16111611
pop(r2); // Offset range end
16121612
pop(r1); // Offset range start
1613+
pop(r3); // First string
16131614

1614-
ensure_defined(r1);
1615-
ensure_defined(r2);
1616-
1617-
pop(r3);
1615+
// If any of the range boundaries are undefined the result is also
1616+
// undefined, be we need to unwind the stack first.
1617+
if (is_undef(r1) || is_undef(r2))
1618+
{
1619+
// Remove all the strings.
1620+
while (!is_undef(r3)) pop(r3);
1621+
// Remove the quantifier at the bottom of the stack.
1622+
pop(r3);
1623+
r1.i = YR_UNDEFINED;
1624+
push(r1);
1625+
break;
1626+
}
16181627

16191628
while (!is_undef(r3))
16201629
{

tests/test-rules.c

+32-20
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,38 @@ static void test_of()
19471947
}",
19481948
"mississippi");
19491949

1950+
// If one of the bounds can not be determined statically it isn't an error.
1951+
assert_true_rule(
1952+
"rule test { \
1953+
strings: \
1954+
$a = \"AXSERS\" \
1955+
condition: \
1956+
true or any of them in (0..filesize-100) \
1957+
}",
1958+
TEXT_1024_BYTES);
1959+
1960+
// Lower bound can not be negative, if it can be determined statically.
1961+
assert_error(
1962+
"rule test { \
1963+
strings: \
1964+
$a = \"AXSERS\" \
1965+
condition: \
1966+
$a in (-1..10) \
1967+
}",
1968+
ERROR_INVALID_VALUE);
1969+
1970+
// Make sure that an undefined range boundary returns an undefined value,
1971+
// which translates to false.
1972+
assert_false_rule(
1973+
"import \"tests\" \
1974+
rule test { \
1975+
strings: \
1976+
$a = \"missi\" \
1977+
condition: \
1978+
any of them in (0..tests.undefined.i) \
1979+
}",
1980+
"mississippi");
1981+
19501982
YR_DEBUG_FPRINTF(1, stderr, "} // %s()\n", __FUNCTION__);
19511983
}
19521984

@@ -2184,26 +2216,6 @@ void test_for()
21842216
}",
21852217
ERROR_INVALID_VALUE);
21862218

2187-
// If one of the bounds can not be determined statically it isn't an error.
2188-
assert_true_rule(
2189-
"rule test { \
2190-
strings: \
2191-
$a = \"AXSERS\" \
2192-
condition: \
2193-
true or any of them in (0..filesize-100) \
2194-
}",
2195-
TEXT_1024_BYTES);
2196-
2197-
// Lower bound can not be negative, if it can be determined statically.
2198-
assert_error(
2199-
"rule test { \
2200-
strings: \
2201-
$a = \"AXSERS\" \
2202-
condition: \
2203-
$a in (-1..10) \
2204-
}",
2205-
ERROR_INVALID_VALUE);
2206-
22072219
// Test case for https://github.com/VirusTotal/yara/issues/1729
22082220
assert_true_rule(
22092221
"rule test { \

0 commit comments

Comments
 (0)