/src/yara/tests/oss-fuzz/pe_fuzzer.cc
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  | Copyright (c) 2017. The YARA Authors. All Rights Reserved.  | 
3  |  |  | 
4  |  | Redistribution and use in source and binary forms, with or without modification,  | 
5  |  | are permitted provided that the following conditions are met:  | 
6  |  |  | 
7  |  | 1. Redistributions of source code must retain the above copyright notice, this  | 
8  |  | list of conditions and the following disclaimer.  | 
9  |  |  | 
10  |  | 2. Redistributions in binary form must reproduce the above copyright notice,  | 
11  |  | this list of conditions and the following disclaimer in the documentation and/or  | 
12  |  | other materials provided with the distribution.  | 
13  |  |  | 
14  |  | 3. Neither the name of the copyright holder nor the names of its contributors  | 
15  |  | may be used to endorse or promote products derived from this software without  | 
16  |  | specific prior written permission.  | 
17  |  |  | 
18  |  | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND  | 
19  |  | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  | 
20  |  | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE  | 
21  |  | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  | 
22  |  | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  | 
23  |  | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;  | 
24  |  | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON  | 
25  |  | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  | 
26  |  | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS  | 
27  |  | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  | 
28  |  | */  | 
29  |  |  | 
30  |  | #include <stddef.h>  | 
31  |  | #include <stdint.h>  | 
32  |  | #include <yara.h>  | 
33  |  |  | 
34  |  | const char* RULES = "import \"pe\""  | 
35  |  |                     "rule test {" | 
36  |  |                     " condition:"  | 
37  |  |                     "   pe.rva_to_offset(pe.sections[0].virtual_address) == "  | 
38  |  |                     "pe.sections[0].raw_data_offset"  | 
39  |  |                     "}";  | 
40  |  |  | 
41  |  | YR_RULES* rules = NULL;  | 
42  |  |  | 
43  |  | extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)  | 
44  | 2  | { | 
45  | 2  |   YR_COMPILER* compiler;  | 
46  |  |  | 
47  | 2  |   if (yr_initialize() != ERROR_SUCCESS)  | 
48  | 0  |     return 0;  | 
49  |  |  | 
50  | 2  |   if (yr_compiler_create(&compiler) != ERROR_SUCCESS)  | 
51  | 0  |     return 0;  | 
52  |  |  | 
53  | 2  |   if (yr_compiler_add_string(compiler, RULES, NULL) == 0)  | 
54  | 2  |     yr_compiler_get_rules(compiler, &rules);  | 
55  |  |  | 
56  | 2  |   yr_compiler_destroy(compiler);  | 
57  |  |  | 
58  | 2  |   return 0;  | 
59  | 2  | }  | 
60  |  |  | 
61  |  |  | 
62  |  | int callback(  | 
63  |  |     YR_SCAN_CONTEXT* context,  | 
64  |  |     int message,  | 
65  |  |     void* message_data,  | 
66  |  |     void* user_data)  | 
67  | 33.2k  | { | 
68  | 33.2k  |   return CALLBACK_CONTINUE;  | 
69  | 33.2k  | }  | 
70  |  |  | 
71  |  |  | 
72  |  | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)  | 
73  | 8.32k  | { | 
74  | 8.32k  |   if (rules == NULL)  | 
75  | 0  |     return 0;  | 
76  |  |  | 
77  | 8.32k  |   yr_rules_scan_mem(  | 
78  | 8.32k  |       rules, data, size, SCAN_FLAGS_NO_TRYCATCH, callback, NULL, 0);  | 
79  |  |  | 
80  | 8.32k  |   return 0;  | 
81  | 8.32k  | }  |