Commit Graph
100 Commits
Author SHA1 Message Date
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 e8ddf33061 Upgrade to OpenSSL 1.1.1 in Visual Studio projects. 2020-03-13 10:03:07 -07:00
plusvic bab4483227 Merge branch 'issue_856' 2020-03-13 10:28:06 +01:00
plusvic 53512b6d4b Merge branch 'v4' 2020-03-13 10:23:21 +01:00
plusvic cc72b1aff2 Merge branch 'v4' 2020-03-13 10:06:27 +01:00
plusvic ece08f59d1 Compiler's callback function now receives a pointer the affected YR_RULE
The callback called by the compiler to report errors and warnings now received a pointer to the rule causing the error/warning. This allows richer error messages.
2020-03-12 19:22:28 +01:00
plusvic 429fb69efa Rename UNDEFINED to YR_UNDEFINED. 2020-03-12 17:21:44 +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 9593801787 Optimize loops by emitting less instructions (#1034)
Added instructions JLE_P and JFALSE_P, which are similar to their counterparts JLE and JFALSE, but pop their arguments from the stack instead of leaving the stack unmodified. These new instructions allow generating tighter loops and simplify the code a little bit.
2019-03-08 12:06:34 +01:00
Victor M. Alvarez 5fcbd4a02c Fixes issue #1006. (#1008) 2019-01-15 17:39:46 +01:00
Victor M. Alvarez 0a3ede0125 Fix issue #999 (#1001)
* Add additional check in OP_COUNT for making sure that the string pointer is not a fake one.

* Initialize scratch memory in order to avoid maliciously crafted YARA rules from reading values left in the stack.
2018-12-13 12:23:09 +01:00
Victor M. Alvarez f54b6cac7a Reduce stack footprint (#995)
* Reduce stack footprint by allowing concatenation nodes in regexp's AST to have more than one children.

Previously all nodes in a regexp AST has two children (left and right), including nodes of type RE_NODE_CONCAT. Consequently, the AST for long regexps and hex strings was a very deep tree, which required a lot of stack space for traversing it recursively. Now regexp's AST nodes have a linked list of children, allowing multiple children per node, which reduces the AST's depth.

* Fix memory leak.
2018-12-04 14:57:59 +01:00
Victor M. Alvarez 72fd000469 Fix contention while incrementing profiling counters.
In 3cceccd8b6 the increment of profiling counters was changed to an atomic operation. This fixed race-conditions but introduced a noticeable contention while using the same YR_RULES structure with multiple threads. This commit fixes this problem by using one counter per thread, which are aggregated later, outside the critical path.
2018-08-20 14:57:45 +02:00
Victor M. Alvarez 3cceccd8b6 Fix race conditions when using the same YR_RULES struct from differen… (#925)
* Fix race conditions when using the same YR_RULES struct from different threads with PROFILING_ENABLED.

* Make time_cost signed as InterlockedAdd64 expects a signed integer.

Closes #918.
2018-08-06 02:13:38 -07:00
Victor M. Alvarez 39602514e2 Use YR_BUILDING_DLL and YR_IMPORTING_DLL for controlling the semantics of YR_API (#924)
For compiling YARA in Windows as a DLL you must define YR_BUILDING_DLL. For using it that DLL in your project you must define YR_IMPORTING_DLL. This allows to use the same header files in both cases. If YR_BUILDING_DLL is defined, YR_API translates to __declspec(dllexport), if YR_IMPORTING_DLL is defined YR_API translates to __declspec(dllimport).
2018-08-02 11:57:53 +02:00
Victor M. Alvarez 96bb602f3f Refactoring of compiler's error handling. (#919)
This simplifies the compiler's error handling logic by removing the last_result field.
2018-07-24 18:17:55 +02:00
Victor M. Alvarez 307ba3d719 Simplify compiler errors (#912)
* Unify compiler's last_result and last_error fields into a single last_error field.

* Rename RE_LEX_ENVIRONMENT's last_error_code to last_error, for coherency with YR_COMPILER.
2018-07-12 10:58:47 +02:00
Victor M. Alvarez 7c1c663523 Closes #908. (#909)
Calling yr_compiler_define_xxx_variable twice with the same identifier now produces ERROR_DUPLICATED_EXTERNAL_VARIABLE.
2018-07-10 18:50:40 +02:00
Victor M. Alvarez 2f72bbfc26 Implement calculation of atom quality based in a prevalence table (#904)
With this new feature you can pass an atom prevalence table to YARA that will be used for computing the quality of each atom based in real-life data instead of using heuristics. The atom prevalence table consists in a list of atoms, sorted lexicographically and accompanied by a score between 0-255 that indicates how frequent is that atom in the data you use to scan with YARA.  With that information YARA can make better selection of the atoms.
2018-07-06 08:59:39 +02:00
Victor M. Alvarez 6298031fa7 Fix issue 891 (#893)
A number of measures has been taken in order to prevent exploitation hand-crafted compiled rules files. The measures include:

* Validate bounds while reading/writing from/to scratch memory.

* Additional checks for preventing an out-of-bound access to the "stack" buffer in OP_INT_TO_DBL and a stack overflow in OP_CALL.

* Ensure that pointers point to somewhere within the arena after relocation.

* Add a canary value to objects for preventing maliciously hand-crafted objects to be used by yr_execute_code.

These measures can be disabled by defining PARANOID_EXEC as 0, but they are enabled by default.
2018-06-19 13:29:13 +02:00
Victor M. Alvarez ec78333a68 Validate bounds while reading/writing from/to scratch memory. (#892) 2018-06-08 10:50:56 +02:00
Victor M. Alvarez 54059646ed Fix issue 884 (#887)
The yr_compiler_add_fd was reading only the first 1024 bytes of the file.
2018-06-01 12:33:11 +02:00
Victor M. Alvarez 4429d5a14a Fix some issues while building with cygwin in Appveyor (#885) 2018-05-31 19:55:53 +02:00
Victor M. Alvarez 2c66fc9e05 Build with cygwin in Appveyor. (#883) 2018-05-31 17:18:43 +02:00
Victor M. Alvarez f2627d6d57 Start using bool defined in stdbool.h for boolean types. (#878) 2018-05-22 09:24:07 +02:00
Victor M. Alvarez a6b0cde592 Start using the "bool" type in stdbool.h for boolean variables. (#874) 2018-05-14 10:04:58 +02:00
Victor M. Alvarez 6dcb6f61fe Optimize generation of Aho-Corasick automaton. (#873)
This commit speeds up the generation of the Aho-Corasick automaton and changes the type of each item in the automaton transition table from uint64_t to uin32_t. This also speeds-up scanning because the more compact improves the CPU cache usage.
2018-05-13 18:55:51 +02:00
Victor M. Alvarez 7509a7b0f5 Fix undefined shift operation (#833)
* Do not allow negative right operand in shift operation.

* Add test case for negative shift operands.
2018-02-21 10:47:00 +01:00
Victor M. Alvarez 6c3900670a Improve rules profiling (#829)
* Improve rules profiling. The most relevant changes are:

* Stop using time() and difftime() for measuring time. Use yr_stopwatch_elapsed_ns instead.

* clock_ticks field in YR_RULE and YR_STRING structures has been renamed to time_cost.

*  When the time_cost for some YR_STRING structure is incremented, it's incremented for its associated YR_RULE structure too. Iterating over the YR_STRING structures is not necessary for computing the total cost for some YR_STRING.

* A time_cost field was added to the YR_RULES structure. The ratio between YR_RULES structure's time_cost and YR_RULE structure's time_cost offers a better measure of how costly a rule is.
2018-02-16 16:59:53 +01:00
Victor M. Alvarez bc4dde0b2b Optimize memory usage (#826)
This commit reduces YARA's memory footprint by:

* Stripping relocation information from those arena don't need to be relocated. This apply particularly to arenas created by yr_arena_load_stream, which implies that those arenas can't be saved back with yr_arena_save_stream.

* Reduce the default amount of matching data hold in YR_MATCH structures.
2018-02-09 14:20:36 +01:00
Victor M. Alvarez 6d5c1e682b Do not use thread local storage for storing fiber pools in regexp eng… (#823)
* Do not use thread local storage for storing fiber pools in regexp engine.

The regexp engine maintains a fiber pool for each thread that executes the yr_re_exec function. The fiber pool was stored using thread local storage (TLS), but this design as proven to be a bad decision, as each thread was responsible for freeing its own fiber pool and this was problematic in languages like Go, where the runtime can create multiple threads but the programmer don't have full control of such threads. Now the fiber pool is stored in the YR_SCAN_CONTEXT structure.

* Remove re_match macro and make calls to yr_re_match explicit in modules.
2018-02-05 16:39:20 +01:00
Victor M. Alvarez be0fc3f52f Implement scanner API.
A new set of functions yr_scanner_* has been added to the API. The purpose of these functions is providing a way for using the same YR_RULES object with different sets of values for external variables. With the existing yr_rules_scan_* functions all clients sharing a YR_RULES object were forced to use the same set of values for externals variables, because the external variables were associated to the YR_RULES object. With the new API each client can create a separate YR_SCANNER to be used with the same YR_RULES object. Each YR_SCANNER can have its own set of external variables. The existing API keeps working as usual.
2018-01-31 17:32:08 +01:00
Victor M. Alvarez a575b76392 Refactor includes logic (#781)
* Refactor logic for "include" statement.

* Fix errors.

* Include <sys/stat.h> in Windows too.

* Replace _WIN32 with _MSC_VER.
2017-11-01 20:22:44 +01:00
Victor M. Alvarez a83cc9c7aa Improve integer sanitation (#780)
* Improve sanitation of decimal integers

* Improve sanitation of hexadecimal and octal integers
2017-11-01 15:03:07 +01:00
Victor M. Alvarez 8598bbc702 Add "const" qualifier to a bunch of pointers to buffer that shouldn't be modified. (#775) 2017-10-27 12:31:41 +02:00
Victor M. Alvarez c6afdd10ba Implement ELF fuzzer to be integrated in oss-fuzz. (#773) 2017-10-26 21:46:48 +02:00
Victor M. Alvarez c16c7d90cd Break process memory iterator in Windows if GetLastError != 0. (#759) 2017-10-24 13:40:46 +02:00
Victor M. Alvarez 554b5df4db Fix issue 765 (#767)
* Fix issue #765.

* Simplify logic of _yr_re_is_char_in_class
2017-10-24 13:00:00 +02:00
Victor M. Alvarez c17d16345b Implement a faster time measurement method for profiling purposes. (#760)
The clock() function used until now is very slow. For more information read: https://upvoid.com/devblog/2014/05/linux-timers/
2017-10-13 13:20:57 +02:00
Victor M. Alvarez 3799a18000 Fix memory leaks in hex and regexp grammars
A memory leak occurred when parsing a hex string or regexp exceeding the RE_MAX_AST_LEVELS limit. In those cases the parsing was aborted without releasing some RE_NODE structs.
2017-10-11 10:59:16 +02:00
Victor M. Alvarez c0fa0bf760 Edhoedt callback on include (#749)
* Full structure implemented, callback available. TODO: actually use the callback's result to import rules files.

* Importing files via external callback fully implemented and working

* Removing useless comments

* Adding documentation

* Updating doc for include callbacks

* Fixing compiler_set_include_callback's signature

* Fixing issues according to @plusvic 's advice
 * Limited lines to 80 chars
 * include callback is now using its own user_data and does not conflict with other callbacks
 * Copying include callback result to the stack before pushing it into the flex scanner
 * yara's memory management functions are now exposed through the API
 * Making the lexer reponsible for freeing the memory allocated by the include callback
 * Updating docs accordingly

* * fixing memory management issues in callbacks
* adding a callback to allow external code to free the memory used to return the include callback's result
* API: un-exposing yara's memory management functions
* making include callbacks the only way ton include files
* adding a default include callback to open files from the disk (mimicking original behavior)
* updating docs accordingly

* fixing missing pointer cast

* re-generating lexer.c

* removing imports ununsed since commit 92877db48e

* fixing default file read callback

* Fixing line numbers in errors

* Fix some issues related to includes callback.

* Remove allow_includes field from YR_COMPILER. Includes now can be disallowed by setting the include callback to NULL.
* Fix memory and file handle leaks.
* Calculate size of error message buffer correctly.
* Add test case.
2017-10-06 13:48:28 +02:00
Victor M. Alvarez 8fce2bf962 Implement API for disabling/enabling rules (#724)
* Increment ip before executing the instruction.

* Implement API for disabling/enabling rules.

* Fix assertion error in 32bit platforms
2017-08-19 15:59:03 +02:00
Victor M. Alvarez a484e862bf Re-generate re_lexer.c 2017-06-26 18:55:08 +02:00
Victor M. Alvarez 6b715cc33f Fix issue #685 2017-06-26 18:04:06 +02:00
Victor M. Alvarez b3495c10bb Remove unused variable 2017-06-19 16:17:54 +02:00
Victor M. Alvarez 1aaac7ba91 Increase RE_MAX_AST_LEVELS as discussed in https://github.com/VirusTotal/yara/issues/674 2017-06-06 19:17:06 +02:00
Victor M. Alvarez 992480c30f Fix buffer overrun (issue #678). Add assert for detecting this kind of issues earlier. 2017-06-06 19:07:27 +02:00
Victor M. Alvarez cccfce3f9f Check for header files in configure.ac 2017-06-06 16:50:21 +02:00
Victor M. Alvarez 11ffa8817f Initialize local variable to avoid random bytes from the stack from being saved to compiled rules in 32 bits mode.
DECLARE_REFERENCE force variables to be 64bit long even when YARA is compiled in 32bit mode, but in that case the higher 4 bytes are unused. Those 4 bytes weren't being initialized and random values from the stack were saved into compiled rules files. This was actually innocuous unless you used rules files compiled by 32-bit yarac with 64-bit yara.
2017-06-05 18:38:57 +02:00
Victor M. Alvarez 5e2d279804 Increase RE_MAX_AST_LEVELS from 1000 to 2000. 2017-05-29 16:56:39 +02:00
Victor M. Alvarez 58f72d4d57 Fix issue #674. Move regexp limits to limits.h. 2017-05-29 16:44:56 +02:00
Victor M. Alvarez 511e38c8ac Add data file for test case 2017-05-25 11:35:10 +02:00
Victor M. Alvarez 4ee750b683 Fix issue #669
pe.overlay.size was undefined for files not having overlaid data. Conditions like math.entropy(0, filesize - pe.overlay.size) >= 7.0 were failing because of this, even if they make perfect sense. Now for PE files not having overlaid data pe.overlay.size is set to zero. The value is still undefined for non-PE files or malformed PEs.
2017-05-25 10:26:50 +02:00
Victor M. Alvarez cdbacf53a4 Make loop variable declaration C89-compliant 2017-05-22 19:52:36 +02:00
Victor M. Alvarez 0dfe1e8fa2 Move variable declaration to beginning of block 2017-05-22 12:24:23 +02:00
Victor M. Alvarez 3dc7dc4a2a Prevent warning due to macro redefinition 2017-05-19 14:01:40 +02:00
Victor M. Alvarez 75fba43bbf Prevent warning due to possible loss of data while converting size_t to int16_t 2017-05-19 13:55:48 +02:00
Victor M. Alvarez f3e12ec15d Disable warning due to zero length array in Microsoft's compiler 2017-05-19 13:36:38 +02:00
Victor M. Alvarez e05645d5cf Merge branch 'master' of https://github.com/VirusTotal/yara 2017-05-19 12:42:10 +02:00
Victor M. Alvarez 8dc265d6a4 Reconfigure and clean up Windows projects 2017-05-19 12:36:16 +02:00
plusvic 0e85b50552 Fix memory leak.
Function yr_re_exec was returning with error after RE_MAX_FIBERS was reached, but the currently running fibers weren’t put back into the fiber pool, causing a memory leak. Additionally, if yr_re_exec was called again by the same thread it returned immediately with ERROR_TOO_MANY_RE_FIBERS, as no ready-to run thread was found in the pool.
2017-05-16 16:53:14 +02:00
plusvic f76e1d057a Expose Windows executables generated by appveyor 2017-05-16 13:58:14 +02:00
plusvic 52fa317aff Remove unnecessary #include <config.h> 2017-05-16 09:27:02 +02:00
Victor M. Alvarez be2849aef9 Remove unused file 2017-05-15 19:18:11 +02:00
plusvic e20c4a5374 Remove unnecessary #include <config.h> 2017-05-15 19:11:01 +02:00
plusvic 00ebed73d6 Add test case for making sure that version numbers in configure.ac and libyara.h are in sync 2017-05-15 18:39:36 +02:00
plusvic 2c169dff38 Remove dependencies from “config.h” 2017-05-15 18:38:52 +02:00
plusvic 48c97732e9 Increase version number in librara.h 2017-05-15 18:18:15 +02:00
plusvic c3a6f1ef45 Fix warnings and minor code re-styling 2017-05-15 15:10:51 +02:00
Victor M. Alvarez 7be98d1489 Improve legibility of object-related code 2017-05-15 13:09:28 +02:00
Victor M. Alvarez 053e67e3ec Fix issue #658 2017-05-14 10:27:39 +02:00
Victor M. Alvarez c2ed7a4661 Fix issue #654 2017-05-14 10:25:43 +02:00
plusvic eef165b9a4 Move variable definitions to the beginning of code block 2017-05-08 16:51:00 +02:00
plusvic 0cc8c0b95f Move variable definition to the beginning of code block 2017-05-08 16:21:42 +02:00
plusvic f70a131f10 Include yara/integers.h instead of stdint.h 2017-05-08 16:20:40 +02:00
plusvic 94e68908ae Make integer conversion explicit to avoid warnings 2017-05-05 19:28:58 +02:00
plusvic 9b3b8815f0 Make integer conversion explicit to avoid warnings 2017-05-05 19:26:27 +02:00
plusvic dc503542ae Fix wrong endianness conversion.
section->flags is 64bit or 32bit for ELF64 and ELF32 files respectively. Different endianness conversion functions must be called for each case.
2017-05-05 19:13:36 +02:00
plusvic 74a4d95a2c Change type from int to size_t 2017-05-05 19:08:35 +02:00
plusvic 43a8efee45 Set version number to 3.6.0 2017-05-05 18:41:58 +02:00
plusvic 3c798dde6c Refactor error returning in yr_re_exec 2017-04-27 15:34:34 +02:00
Victor M. Alvarez 83d7998046 Fix issue #646 (#648)
* Fix issue #646 and some edge cases with wide regexps using \b and \B

* Rename function IS_WORD_CHAR to _yr_re_is_word_char
2017-04-27 11:39:04 +02:00
plusvic f12262c21f Fix issue with ERROR_TOO_MANY_RE_FIBERS being returned spuriously 2017-04-27 09:34:34 +02:00
plusvic 5df6581e3b Fix alignment tests 2017-04-26 20:01:07 +02:00
plusvic bbbb712cc6 Fix issue #647 2017-04-26 14:04:01 +02:00
plusvic 62f1ef730a Replace tab with spaces 2017-04-25 16:21:53 +02:00
plusvic c08cbfb2e9 Remove unused struct field 2017-04-25 16:21:39 +02:00
plusvic 21070094e4 Allow defining MAX_THREADS during compile time 2017-04-25 16:21:10 +02:00
plusvic 64e67ade08 Fix issue #626
The zero-sized repeat in regexps like /a{0}/ and /a{0,0}/ prevented the code for the regexp from being generated. Many regexp engines accept 0 repeats, but they are useless and we are forbidding them in YARA.
2017-04-24 18:21:16 +02:00
plusvic a378d26bca Fix bug in regexp engine introduced in recent changes. 2017-03-16 18:33:34 +01:00
plusvic dd0f7411d8 Rename exported_dlls to exported_functions 2017-03-16 17:23:03 +01:00
plusvic 2d3b6d0b7b Fix user after free bug 2017-03-16 17:20:03 +01:00
plusvic 3c68cf0429 Add test cases for PE module 2017-03-16 17:13:21 +01:00
plusvic 327d38b8b7 Do some re-styling 2017-03-16 16:52:31 +01:00
plusvic b6851ea05b Add missing endianness conversions 2017-03-16 16:38:16 +01:00
plusvic 6cec393227 Remove unnecessary complexity.
If a function name is truncated at the end of the new code still adds the truncated name it to the imported functions list. But we can live with that in favor of a simpler code.
2017-03-16 13:22:23 +01:00
plusvic 5ae6f31941 Do some re-styling in pe.c 2017-03-16 13:06:04 +01:00
plusvic a6aaae3bb0 Fix some style issues and add —print-string-length option to man page. 2017-03-16 11:22:10 +01:00
plusvic 6f3d5e9af0 Update “Getting started” section in the documentation to mention dotnet module. Change link to Windows binaries. 2017-03-15 18:01:44 +01:00