192 Commits
Author SHA1 Message Date
Victor M. Alvarez cc02b4fe85 Fix issues while merging cherry-picked commits from master. 2022-08-08 12:53:02 +02:00
Victor M. Alvarez 11138994d9 Prevent undefined values in offset ranges from corrupting the VM's stack. 2022-08-08 12:37:08 +02:00
Victor M. Alvarez 7a99e6dd12 Fix issue #1757. 2022-08-04 13:46:03 +02:00
Wesley Shields 136794355c Fix #1660. (#1661)
There is an issue where the OP_OF_FOUND_IN opcode is broken when using "all of".
This is because the UNDEF that is pushed on the stack to indicate "all" is
compared to the number of matched strings as signed integers. If there are no
matched strings the condition still evaluates to true because 0 > (signed)
UNDEF.

Fix it by checking if we are using an UNDEF and compare it to the number of
strings checked if that is the case.
2022-03-09 17:17:41 +01:00
Victor M. Alvarez 074f246791 Fix issue #1635 2022-03-09 10:09:48 +01:00
Dominika Regéciová 0bc04c34ac add missing semicolon (#1645) 2022-02-07 10:17:50 +01:00
Wesley Shields 7fabd955ab Implement rule sets. (#1597)
This adds the ability to use rule sets just like we do with string sets.
Specifically, it enables conditions like the following:

5 of (a, b)
50% of (a*)

where "a" is an existing rule and "b*" is a wildcard rule identifier, which is
expanded out precisely as wildcard string identifiers are.

I chose to make OP_OF and OP_OF_PERCENT emit an argument which is the "type" of
set it is expected to match on. The two acceptable values are OF_STRING_SET and
OF_RULE_SET. When OF_STRING_SET is used then the references on the stack are
expected to be strings and OF_RULE_SET will check if the rules on the stack
match.

I added a yr_parser_emit_pushes_for_rules() which behaves similarly to the
string counterpart. The one big exception is that I can't use yr_rules_foreach
macro since that needs the terminating "null" rule which is not yet added, so
instead I have to iterate the rules manually using yr_arena_get_ptr() to walk
all the currently compiled rules.

* Stricter ordering of rule set in compiler.

Make sure that when we compile a new rule that any existing rule sets do not
match the new rule. Specifically we are making the following a compiler error:

rule a1 { condition: true }
rule b { condition: 1 of (a*) }
rule a2 { condition: true }

This is a compiler error because "a2" is defined after the usage of (a*) in rule
b.

There is still an edge case here. These rules compile:

rule a { condition 1 of (a) }
rule b { condition: 1 of (b*) }

In both cases those rules evaluate to false, which is in line with the behavior
of this rule:

rule c { condition: c }

Given that the behavior around rule sets matches the current behavior I think
this is acceptable.

* Add docs for rule sets.

* Implement hash table iterators.

* Remove old comment about linked list.

* Move variable declaration.

* Iterate over rules better.
2021-12-21 12:46:33 +01:00
Victor M. Alvarez a36b497926 Fix issue #1616 (#1621)
This is a more comprehensive fix than #1617, it adds new functions to the API for getting/setting uint32 and uint64 settings.  Using these functions is preferable over calling `yr_(get|set)_configuration` directly.
2021-12-20 10:31:12 +01:00
Victor M. Alvarez e2f3e544ce Fix issue causing wrong assertion.
The loop variable i is used in the assertion statement after the loop. Don't redeclare i in the loop, as it hides the i in the outer scope.
2021-12-07 11:50:28 +01:00
xbabka01 dc4e0096c2 Added unary operation defined (#1529)
YARA didn't support any expression which would check value against YR_UNDEFINED constant so checking for non-existence of something can be very problematic. But we run multiple times into issue of people asking us for checking of absence of something.

Because of this we propose operator defined which would be unary operator (like not) which works like this:

defined x evaluates to

true - whenever x != YR_UNDEFINED
false - whenever x == YR_UNDEFINED

Related to issue #835
2021-10-21 10:41:40 +02:00
Wesley Shields e71801644f Implement OP_COUNT_IN. (#1565)
* Implement OP_COUNT_IN.

Add a new instruction (OP_COUNT_IN) which is used to check the number of
occurrences of a string in a range. You can use this with the following syntax:

 #a in (0..100) == 2

This will check that the $a string matches exactly 2 times in the range
provided.

* Add docs for counting strings in a range.
2021-09-16 15:16:33 +02:00
Victor M. AlvarezandWesley Shields 32c969f1e4 Implement percent "%" operator for string sets. (#1434)
This commit implements the "%" operator and extends the grammar to
recognize it for string sets.

The grammar now recognizes "50% of them" or variations on that. The
percentage number must be between 1 and 100 (inclusive) or the compiler will
return an error.

The implementation uses a new opcode (OP_OF_PERCENT) and this is because the
stack for these looks different from a typical "for_expression _OF_ string set"
rule. In the "for_expression" case (which uses OP_OF) the stack looks like this
(assuming two strings $a and $b):

$a
$b
undefined <-- end of list
2 <-- for_expression

The OP_OF operation checks each of the strings until it hits the end of list and
then compares the number found to whatever is left on the stack (2 in this
case).

The new grammar causes a stack at runtime which looks like this:

50 <-- percent number
$b
$a
undefined <-- end of list

The OP_OF operator won't work in this case because the percent number is sitting
on the top of the stack compared to the for_expression sitting at the bottom of
the stack.

I also cleaned up a minor oversight in compiler.c when printing the
ERROR_INVALID_MODIFIER message. It is the same as the other compiler errors so I
just moved it into the same set of fallthrough case statements.

Lastly, add some test cases to make sure this is working as intended.

Co-authored-by: Wesley Shields <[email protected]>
2021-09-01 01:28:24 +02:00
Wesley Shields 24a5ad1383 Implement the OP_OF_FOUND_IN opcode. (#1554)
This is a combination of "OP_OF" and "OP_FOUND_IN" that allows you to express logic like "all of them in (0..100)". This came out of the discussion in #1483 and likely makes part of that pull request redundant.
2021-08-31 19:34:16 +02:00
Marek Milkovič 66c1c7176b Added iequals operator for case-insensitive string comparison (#1536) 2021-08-11 09:40:54 +02:00
Victor M. Alvarez a1112bb34f Add test case for change in 4c53300454 and fix the same issue for dictionaries. 2021-06-02 09:33:29 +02:00
meme 4c53300454 Check if iterator is YR_UNDEFINED before accessing (#1508) 2021-06-02 09:06:08 +02:00
Victor M. Alvarez 5a9efc4716 Revert changes introduced in b7da5d2835.
See #1385 for a discussion about why this has been reverted back.
2021-01-07 15:18:19 +01:00
Victor M. Alvarez 1491f1a562 Fix issue #1435. 2021-01-07 11:26:03 +01:00
Victor M. Alvarez 3533e9dd63 Fix some warnings. 2021-01-04 13:34:47 +01:00
Victor M. Alvarez d37b3dfc83 Fix warnings. 2020-12-24 12:06:10 +01:00
Victor M. AlvarezandSimon Hardy-Francis cba6932759 Implement non-blocking API (#1428)
With this change the yr_scanner_scan_mem_blocks function can return ERROR_BLOCK_NOT_READY when the YR_MEMORY_BLOCK_ITERATOR that provides memory blocks to the function is not ready to return the next block. After this error the caller can retry the call to yr_scanner_scan_mem_blocks multiple times until the function succeeds or fails with some other error code.

Co-authored-by: Simon Hardy-Francis <[email protected]>
2020-12-24 10:46:09 +01:00
Victor M. Alvarez 7bb8ecd742 Rename rules_list_head, strings_list_head and externals_list_head.
The new names are rules_table, strings_table and ext_vars_table respectively. Now that those fields are not linked list the previous names were somewhat confusing. Document the fields in YR_RULES a little bit better.
2020-12-03 13:15:50 +01:00
Victor M. Alvarez ff29f519dd Mitigation for possible security issue with hand-crafted compiled rules. (#1399)
Closes #1389.
2020-11-26 20:59:00 +01:00
Victor M. Alvarez fa5682a98d Homogenize function comments and remove empty lines. 2020-10-19 12:00:53 +02:00
Victor M. Alvarez 0bce4bd74d Start using clang-format for formatting the code automatically. 2020-10-07 16:27:30 +02:00
Victor M. Alvarez 41a32691d4 Implement string operators: icontains, startswith, endswith, istartswith and iendswith. 2020-09-28 09:59:13 +02:00
Hilko Bengen 4d7b4d10ac Make compiled rules smaller by introducing shorter PUSH variants
This cuts the condition evaluation program generated from the
signature-base[1] ruleset from 429053 to 329558 bytes, saving 23.1%

[1] https://github.com/Neo23x0/signature-base
2020-07-13 14:03:07 +02:00
Victor M. Alvarez 1c17965e72 Fix warning. 2020-06-30 10:55:48 +02:00
Victor M. Alvarez fc100a8670 Fix integer overflows in timeout logic when compiling in 32-bits. (#1301)
Closes #1299
2020-06-12 19:58:19 +02:00
Victor M. Alvarez ea73608a77 Return an error instead of aborting the program when looking for field name that doesn't exist during the scan.
This does't occur often, as this kind of errors are catch during compilation. However, this may occur when you try to use rules that were compiled with a version of YARA that has support for OpenSSL with another version that doesn't have it. That's because YARA with OpenSSL supports some fields in the "pe" module that are not allowed by YARA without OpenSSL. The compiled rules are valid for YARA with OpenSSL, but the fields won't be found when they are used without OpenSSL support.

Closes #1288
2020-06-03 12:34:32 +02:00
Victor M. Alvarez cd5820f4ae Add additional integrity checks while in paranoid mode. 2020-05-21 13:24:53 +02:00
Hilko Bengen e1654ae49c Fix unaligned data accesses on ARM
Some unaligned data access patterns are not entirely OK on Linux/ARM.
Depending on kernel configuration, it may cause bus errors or "only"
lead to massive slowdowns as the kernel works around faulting
instructions and perhaps even emits log messages. (This option does
not seem to exist for 32 bit code running on an ARM64 kernel, though.)

Most of this patch which was originally developed by Steve Langasek
has been part of the Debian package for a while.
2020-05-01 09:37:07 +02:00
Victor M. Alvarez b7da5d2835 Fix issue #1180.
This changes the way in which comparison operators, and also the "contains" and "matches" operators handle undefined operands. Before this change the result of such operations was undefined, after this change the result is false.
2020-03-26 13:50:49 +01:00
Victor M. Alvarez 8f3120932c Fix issue with loops over integer ranges where the upper or lower bound was undefined.
For loops like "for all i in (1..some_undefined_value)" were returning true in all cases. If the loop was like "for all i in (some_undefined_value..100)", it was it was executed too many times.
2020-03-24 18:37:34 +01:00
plusvic 50a71ca7c7 Make profiling faster.
Instead of measuring the time spent in verifying all atom matches, do it only for a fraction of them. This way the cost of measuring the time is a smaller percentage of the time spent doing the actual verification.
2020-03-23 13:05:27 +01:00
plusvic 13de6710a9 Make profiling faster.
Instead of measuring the time spent in verifying all atom matches, do it only for a fraction of them. This way the cost of measuring the time is a smaller percentage of the time spent doing the actual verification.
2020-03-23 12:40:10 +01:00
Victor M. Alvarez 3a9201737d Rename PARANOID_EXEC to YR_PARANOID_EXEC. 2020-03-19 10:46:10 +01:00
Victor M. Alvarez 0043a4cfdd Rename PROFILING_ENABLED to YR_PROFILING_ENABLED and implement yr_scanner_get_profiling_info. 2020-03-16 17:44:44 +01:00
plusvic 429fb69efa Rename UNDEFINED to YR_UNDEFINED. 2020-03-12 17:21:44 +01:00
Victor M. Alvarez 2e9dc81a10 Merge branch 'master' into improve-profiling. 2020-03-12 10:43:17 +01:00
Victor M. Alvarez 88b7739a50 Fix issue with nested loops using more variables than MEM_SIZE. 2020-03-11 15:30:25 +01:00
Victor M. Alvarez 69e498bb78 Rename yr_stopwatch_elapsed_us to yr_stopwatch_elapsed_ns.
This function now returns nanoseconds instead of microseconds. The change avoids the use of division operations, which are slower than multiplication.
2020-03-06 18:41:52 +01:00
Victor M. Alvarez bf390c1dac Don't use separate lists for private string matches. 2020-03-06 11:15:20 +01:00
plusvic bcf7c17a08 Reduce frequency in which checks for timeouts are done in yr_execute.
Frequency has been reduced to a check every 10 instructions to a check every 100 instructions. This improves performance at the expense of timeout accuracy.
2020-03-06 10:43:06 +01:00
Victor M. Alvarez 36f3d6a2fe Rename arena2 to arena. 2020-03-05 10:56:45 +01:00
Victor M. Alvarez 9f75faf11c Remove all remaining dependencies from old arena. 2020-03-05 10:20:17 +01:00
Victor M. Alvarez 73aa6097f4 More refactoring. 2020-02-28 17:29:32 +01:00
Victor M. Alvarez b68deed0c3 Remove all remaining dependencies from tidx (except for the "magic" module). 2020-02-27 19:54:11 +01:00
Victor M. Alvarez 2b121b166d Track string matches using YR_SCAN_CONTEXT. 2020-02-27 13:51:47 +01:00
Victor M. Alvarez 34ec34fbe1 Start using bitmap for tracking rule matches. 2020-02-27 09:34:29 +01:00