Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2013-2014. 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 <assert.h> |
31 | | #include <float.h> |
32 | | #include <math.h> |
33 | | #include <stdint.h> |
34 | | #include <string.h> |
35 | | #include <yara.h> |
36 | | #include <yara/arena.h> |
37 | | #include <yara/compiler.h> |
38 | | #include <yara/endian.h> |
39 | | #include <yara/error.h> |
40 | | #include <yara/exec.h> |
41 | | #include <yara/globals.h> |
42 | | #include <yara/limits.h> |
43 | | #include <yara/mem.h> |
44 | | #include <yara/modules.h> |
45 | | #include <yara/object.h> |
46 | | #include <yara/re.h> |
47 | | #include <yara/sizedstr.h> |
48 | | #include <yara/stopwatch.h> |
49 | | #include <yara/strutils.h> |
50 | | #include <yara/unaligned.h> |
51 | | #include <yara/utils.h> |
52 | | |
53 | 28.2k | #define MEM_SIZE YR_MAX_LOOP_NESTING*(YR_MAX_LOOP_VARS + YR_INTERNAL_LOOP_VARS) |
54 | | |
55 | | #define push(x) \ |
56 | 368k | if (stack.sp < stack.capacity) \ |
57 | 368k | { \ |
58 | 368k | stack.items[stack.sp++] = (x); \ |
59 | 368k | } \ |
60 | 368k | else \ |
61 | 368k | { \ |
62 | 0 | result = ERROR_EXEC_STACK_OVERFLOW; \ |
63 | 0 | stop = true; \ |
64 | 0 | break; \ |
65 | 0 | } |
66 | | |
67 | | #define pop(x) \ |
68 | 368k | { \ |
69 | 368k | assert(stack.sp > 0); \ |
70 | 368k | x = stack.items[--stack.sp]; \ |
71 | 368k | } |
72 | | |
73 | 372k | #define is_undef(x) IS_UNDEFINED((x).i) |
74 | | |
75 | | #define ensure_defined(x) \ |
76 | 259k | if (is_undef(x)) \ |
77 | 259k | { \ |
78 | 64.2k | r1.i = YR_UNDEFINED; \ |
79 | 64.2k | push(r1); \ |
80 | 64.2k | break; \ |
81 | 64.2k | } |
82 | | |
83 | | #define ensure_within_mem(x) \ |
84 | 0 | if (x < 0 || x >= MEM_SIZE) \ |
85 | 0 | { \ |
86 | 0 | stop = true; \ |
87 | 0 | result = ERROR_INTERNAL_FATAL_ERROR; \ |
88 | 0 | break; \ |
89 | 0 | } |
90 | | |
91 | | // Make sure that the string pointer is within the rules arena. |
92 | | #define ensure_within_rules_arena(x) \ |
93 | 196k | { \ |
94 | 196k | YR_ARENA_REF ref; \ |
95 | 196k | if (yr_arena_ptr_to_ref(context->rules->arena, x, &ref) == 0) \ |
96 | 196k | { \ |
97 | 0 | stop = true; \ |
98 | 0 | result = ERROR_INTERNAL_FATAL_ERROR; \ |
99 | 0 | break; \ |
100 | 0 | } \ |
101 | 196k | } |
102 | | |
103 | | // Make sure that the instruction pointer stays within the code section. A |
104 | | // crafted compiled-rules file can carry a jump whose offset moves ip outside |
105 | | // the code buffer, and the dispatch loop would then read the opcode out of |
106 | | // bounds. |
107 | | #define ensure_within_code(x) \ |
108 | 476k | if ((x) < code_start || (x) >= code_end) \ |
109 | 476k | { \ |
110 | 0 | stop = true; \ |
111 | 0 | result = ERROR_INTERNAL_FATAL_ERROR; \ |
112 | 0 | break; \ |
113 | 0 | } |
114 | | |
115 | | #define check_object_canary(o) \ |
116 | 71.5k | if (o->canary != context->canary) \ |
117 | 71.5k | { \ |
118 | 0 | stop = true; \ |
119 | 0 | result = ERROR_INTERNAL_FATAL_ERROR; \ |
120 | 0 | break; \ |
121 | 0 | } |
122 | | |
123 | 0 | #define little_endian_uint8_t(x) (x) |
124 | 0 | #define little_endian_int8_t(x) (x) |
125 | 0 | #define little_endian_uint16_t(x) yr_le16toh(x) |
126 | 0 | #define little_endian_int16_t(x) yr_le16toh(x) |
127 | 0 | #define little_endian_uint32_t(x) yr_le32toh(x) |
128 | 0 | #define little_endian_int32_t(x) yr_le32toh(x) |
129 | | |
130 | 0 | #define big_endian_uint8_t(x) (x) |
131 | 0 | #define big_endian_int8_t(x) (x) |
132 | 0 | #define big_endian_uint16_t(x) yr_be16toh(x) |
133 | 0 | #define big_endian_int16_t(x) yr_be16toh(x) |
134 | 0 | #define big_endian_uint32_t(x) yr_be32toh(x) |
135 | 0 | #define big_endian_int32_t(x) yr_be32toh(x) |
136 | | |
137 | | #define function_read(type, endianess) \ |
138 | | int64_t read_##type##_##endianess( \ |
139 | | YR_MEMORY_BLOCK_ITERATOR* iterator, size_t offset) \ |
140 | 0 | { \ |
141 | 0 | YR_MEMORY_BLOCK* block = iterator->first(iterator); \ |
142 | 0 | while (block != NULL) \ |
143 | 0 | { \ |
144 | 0 | if (offset >= block->base && block->size >= sizeof(type) && \ |
145 | 0 | offset <= block->base + block->size - sizeof(type)) \ |
146 | 0 | { \ |
147 | 0 | type result; \ |
148 | 0 | const uint8_t* data = yr_fetch_block_data(block); \ |
149 | 0 | if (data == NULL) \ |
150 | 0 | return YR_UNDEFINED; \ |
151 | 0 | result = *(type*) (data + offset - block->base); \ |
152 | 0 | result = endianess##_##type(result); \ |
153 | 0 | return result; \ |
154 | 0 | } \ |
155 | 0 | block = iterator->next(iterator); \ |
156 | 0 | } \ |
157 | 0 | return YR_UNDEFINED; \ |
158 | 0 | }; |
159 | | |
160 | 0 | function_read(uint8_t, little_endian); |
161 | 0 | function_read(uint16_t, little_endian); |
162 | 0 | function_read(uint32_t, little_endian); |
163 | 0 | function_read(int8_t, little_endian); |
164 | 0 | function_read(int16_t, little_endian); |
165 | 0 | function_read(int32_t, little_endian); |
166 | 0 | function_read(uint8_t, big_endian); |
167 | 0 | function_read(uint16_t, big_endian); |
168 | 0 | function_read(uint32_t, big_endian); |
169 | 0 | function_read(int8_t, big_endian); |
170 | 0 | function_read(int16_t, big_endian); |
171 | 0 | function_read(int32_t, big_endian); |
172 | | |
173 | | static const uint8_t* jmp_if(int condition, const uint8_t* ip) |
174 | 34.5k | { |
175 | 34.5k | int32_t off = 0; |
176 | | |
177 | 34.5k | if (condition) |
178 | 616 | { |
179 | | // The condition is true, the instruction pointer (ip) is incremented in |
180 | | // the amount specified by the jump's offset, which is a int32_t following |
181 | | // the jump opcode. The ip is currently past the opcode and pointing to |
182 | | // the offset. |
183 | | |
184 | | // Copy the offset from the instruction stream to a local variable. |
185 | 616 | off = yr_unaligned_u32(ip); |
186 | | |
187 | | // The offset is relative to the jump opcode, but now the ip is one byte |
188 | | // past the opcode, so we need to decrement it by one. |
189 | 616 | off -= 1; |
190 | 616 | } |
191 | 33.9k | else |
192 | 33.9k | { |
193 | | // The condition is false, the execution flow proceeds with the instruction |
194 | | // right after the jump. |
195 | 33.9k | off = sizeof(int32_t); |
196 | 33.9k | } |
197 | | |
198 | 34.5k | return ip + off; |
199 | 34.5k | } |
200 | | |
201 | | static int iter_array_next(YR_ITERATOR* self, YR_VALUE_STACK* stack) |
202 | 0 | { |
203 | | // Check that there's two available slots in the stack, one for the next |
204 | | // item returned by the iterator and another one for the boolean that |
205 | | // indicates if there are more items. |
206 | 0 | if (stack->sp + 1 >= stack->capacity) |
207 | 0 | return ERROR_EXEC_STACK_OVERFLOW; |
208 | | |
209 | | // If the array that must be iterated is undefined stop the iteration right |
210 | | // aways, as if the array would be empty. |
211 | 0 | if (IS_UNDEFINED(self->array_it.array)) |
212 | 0 | goto _stop_iter; |
213 | | |
214 | | // If the current index is equal or larger than array's length the iterator |
215 | | // has reached the end of the array. |
216 | 0 | if (self->array_it.index >= yr_object_array_length(self->array_it.array)) |
217 | 0 | goto _stop_iter; |
218 | | |
219 | | // Push the false value that indicates that the iterator is not exhausted. |
220 | 0 | stack->items[stack->sp++].i = 0; |
221 | |
|
222 | 0 | YR_OBJECT* obj = yr_object_array_get_item( |
223 | 0 | self->array_it.array, 0, self->array_it.index); |
224 | |
|
225 | 0 | if (obj != NULL) |
226 | 0 | stack->items[stack->sp++].o = obj; |
227 | 0 | else |
228 | 0 | stack->items[stack->sp++].i = YR_UNDEFINED; |
229 | |
|
230 | 0 | self->array_it.index++; |
231 | |
|
232 | 0 | return ERROR_SUCCESS; |
233 | | |
234 | 0 | _stop_iter: |
235 | | |
236 | | // Push true for indicating the iterator has been exhausted. |
237 | 0 | stack->items[stack->sp++].i = 1; |
238 | | // Push YR_UNDEFINED as a placeholder for the next item. |
239 | 0 | stack->items[stack->sp++].i = YR_UNDEFINED; |
240 | |
|
241 | 0 | return ERROR_SUCCESS; |
242 | 0 | } |
243 | | |
244 | | static int iter_dict_next(YR_ITERATOR* self, YR_VALUE_STACK* stack) |
245 | 0 | { |
246 | | // Check that there's three available slots in the stack, two for the next |
247 | | // item returned by the iterator and its key, and another one for the boolean |
248 | | // that indicates if there are more items. |
249 | 0 | if (stack->sp + 2 >= stack->capacity) |
250 | 0 | return ERROR_EXEC_STACK_OVERFLOW; |
251 | | |
252 | | // If the dictionary that must be iterated is undefined, stop the iteration |
253 | | // right away, as if the dictionary would be empty. |
254 | 0 | if (IS_UNDEFINED(self->dict_it.dict)) |
255 | 0 | goto _stop_iter; |
256 | | |
257 | 0 | YR_DICTIONARY_ITEMS* items = object_as_dictionary(self->dict_it.dict)->items; |
258 | | |
259 | | // If the dictionary has no items or the iterator reached the last item, abort |
260 | | // the iteration, if not push the next key and value. |
261 | 0 | if (items == NULL || self->dict_it.index == items->used) |
262 | 0 | goto _stop_iter; |
263 | | |
264 | | // Push the false value that indicates that the iterator is not exhausted. |
265 | 0 | stack->items[stack->sp++].i = 0; |
266 | |
|
267 | 0 | if (items->objects[self->dict_it.index].obj != NULL) |
268 | 0 | { |
269 | 0 | stack->items[stack->sp++].o = items->objects[self->dict_it.index].obj; |
270 | 0 | stack->items[stack->sp++].p = items->objects[self->dict_it.index].key; |
271 | 0 | } |
272 | 0 | else |
273 | 0 | { |
274 | 0 | stack->items[stack->sp++].i = YR_UNDEFINED; |
275 | 0 | stack->items[stack->sp++].i = YR_UNDEFINED; |
276 | 0 | } |
277 | |
|
278 | 0 | self->dict_it.index++; |
279 | |
|
280 | 0 | return ERROR_SUCCESS; |
281 | | |
282 | 0 | _stop_iter: |
283 | | |
284 | | // Push true for indicating the iterator has been exhausted. |
285 | 0 | stack->items[stack->sp++].i = 1; |
286 | | // Push YR_UNDEFINED as a placeholder for the next key and value. |
287 | 0 | stack->items[stack->sp++].i = YR_UNDEFINED; |
288 | 0 | stack->items[stack->sp++].i = YR_UNDEFINED; |
289 | |
|
290 | 0 | return ERROR_SUCCESS; |
291 | 0 | } |
292 | | |
293 | | static int iter_int_range_next(YR_ITERATOR* self, YR_VALUE_STACK* stack) |
294 | 0 | { |
295 | | // Check that there's two available slots in the stack, one for the next |
296 | | // item returned by the iterator and another one for the boolean that |
297 | | // indicates if there are more items. |
298 | 0 | if (stack->sp + 1 >= stack->capacity) |
299 | 0 | return ERROR_EXEC_STACK_OVERFLOW; |
300 | | |
301 | 0 | if (!IS_UNDEFINED(self->int_range_it.next) && |
302 | 0 | !IS_UNDEFINED(self->int_range_it.last) && |
303 | 0 | self->int_range_it.next <= self->int_range_it.last) |
304 | 0 | { |
305 | | // Push the false value that indicates that the iterator is not exhausted. |
306 | 0 | stack->items[stack->sp++].i = 0; |
307 | 0 | stack->items[stack->sp++].i = self->int_range_it.next; |
308 | 0 | self->int_range_it.next++; |
309 | 0 | } |
310 | 0 | else |
311 | 0 | { |
312 | | // Push true for indicating the iterator has been exhausted. |
313 | 0 | stack->items[stack->sp++].i = 1; |
314 | | // Push YR_UNDEFINED as a placeholder for the next item. |
315 | 0 | stack->items[stack->sp++].i = YR_UNDEFINED; |
316 | 0 | } |
317 | |
|
318 | 0 | return ERROR_SUCCESS; |
319 | 0 | } |
320 | | |
321 | | static int iter_int_enum_next(YR_ITERATOR* self, YR_VALUE_STACK* stack) |
322 | 0 | { |
323 | | // Check that there's two available slots in the stack, one for the next |
324 | | // item returned by the iterator and another one for the boolean that |
325 | | // indicates if there are more items. |
326 | 0 | if (stack->sp + 1 >= stack->capacity) |
327 | 0 | return ERROR_EXEC_STACK_OVERFLOW; |
328 | | |
329 | 0 | if (!IS_UNDEFINED(self->int_enum_it.next) && |
330 | 0 | !IS_UNDEFINED(self->int_enum_it.count) && |
331 | 0 | self->int_enum_it.next < self->int_enum_it.count) |
332 | 0 | { |
333 | | // Push the false value that indicates that the iterator is not exhausted. |
334 | 0 | stack->items[stack->sp++].i = 0; |
335 | 0 | stack->items[stack->sp++].i = |
336 | 0 | self->int_enum_it.items[self->int_enum_it.next]; |
337 | 0 | self->int_enum_it.next++; |
338 | 0 | } |
339 | 0 | else |
340 | 0 | { |
341 | | // Push true for indicating the iterator has been exhausted. |
342 | 0 | stack->items[stack->sp++].i = 1; |
343 | | // Push YR_UNDEFINED as a placeholder for the next item. |
344 | 0 | stack->items[stack->sp++].i = YR_UNDEFINED; |
345 | 0 | } |
346 | |
|
347 | 0 | return ERROR_SUCCESS; |
348 | 0 | } |
349 | | |
350 | | static int iter_string_set_next(YR_ITERATOR* self, YR_VALUE_STACK* stack) |
351 | 0 | { |
352 | | // Check that there's two available slots in the stack, one for the next |
353 | | // item returned by the iterator and another one for the boolean that |
354 | | // indicates if there are more items. |
355 | 0 | if (stack->sp + 1 >= stack->capacity) |
356 | 0 | return ERROR_EXEC_STACK_OVERFLOW; |
357 | | |
358 | | // If the current index is equal or larger than array's length the iterator |
359 | | // has reached the end of the array. |
360 | 0 | if (self->string_set_it.index >= self->string_set_it.count) |
361 | 0 | goto _stop_iter; |
362 | | |
363 | | // Push the false value that indicates that the iterator is not exhausted. |
364 | 0 | stack->items[stack->sp++].i = 0; |
365 | 0 | stack->items[stack->sp++].s = |
366 | 0 | self->string_set_it.strings[self->string_set_it.index]; |
367 | 0 | self->string_set_it.index++; |
368 | |
|
369 | 0 | return ERROR_SUCCESS; |
370 | | |
371 | 0 | _stop_iter: |
372 | | |
373 | | // Push true for indicating the iterator has been exhausted. |
374 | 0 | stack->items[stack->sp++].i = 1; |
375 | | // Push YR_UNDEFINED as a placeholder for the next item. |
376 | 0 | stack->items[stack->sp++].i = YR_UNDEFINED; |
377 | |
|
378 | 0 | return ERROR_SUCCESS; |
379 | 0 | } |
380 | | |
381 | | static int iter_text_string_set_next(YR_ITERATOR* self, YR_VALUE_STACK* stack) |
382 | 0 | { |
383 | | // Check that there's two available slots in the stack, one for the next |
384 | | // item returned by the iterator and another one for the boolean that |
385 | | // indicates if there are more items. |
386 | 0 | if (stack->sp + 1 >= stack->capacity) |
387 | 0 | return ERROR_EXEC_STACK_OVERFLOW; |
388 | | |
389 | | // If the current index is equal or larger than array's length the iterator |
390 | | // has reached the end of the array. |
391 | 0 | if (self->text_string_set_it.index >= self->text_string_set_it.count) |
392 | 0 | goto _stop_iter; |
393 | | |
394 | | // Push the false value that indicates that the iterator is not exhausted. |
395 | 0 | stack->items[stack->sp++].i = 0; |
396 | 0 | stack->items[stack->sp++].ss = |
397 | 0 | self->text_string_set_it.strings[self->text_string_set_it.index]; |
398 | 0 | self->text_string_set_it.index++; |
399 | |
|
400 | 0 | return ERROR_SUCCESS; |
401 | | |
402 | 0 | _stop_iter: |
403 | | |
404 | | // Push true for indicating the iterator has been exhausted. |
405 | 0 | stack->items[stack->sp++].i = 1; |
406 | | // Push YR_UNDEFINED as a placeholder for the next item. |
407 | 0 | stack->items[stack->sp++].i = YR_UNDEFINED; |
408 | |
|
409 | 0 | return ERROR_SUCCESS; |
410 | 0 | } |
411 | | |
412 | | // Global table that contains the "next" function for different types of |
413 | | // iterators. The reason for using this table is to avoid storing pointers |
414 | | // in the YARA's VM stack. Instead of the pointers we store an index within |
415 | | // this table. |
416 | | static YR_ITERATOR_NEXT_FUNC iter_next_func_table[] = { |
417 | | iter_array_next, |
418 | | iter_dict_next, |
419 | | iter_int_range_next, |
420 | | iter_int_enum_next, |
421 | | iter_string_set_next, |
422 | | iter_text_string_set_next, |
423 | | }; |
424 | | |
425 | 0 | #define ITER_NEXT_ARRAY 0 |
426 | 0 | #define ITER_NEXT_DICT 1 |
427 | 0 | #define ITER_NEXT_INT_RANGE 2 |
428 | 0 | #define ITER_NEXT_INT_ENUM 3 |
429 | 0 | #define ITER_NEXT_STRING_SET 4 |
430 | 0 | #define ITER_NEXT_TEXT_STRING_SET 5 |
431 | | |
432 | | int yr_execute_code(YR_SCAN_CONTEXT* context) |
433 | 28.2k | { |
434 | 28.2k | YR_DEBUG_FPRINTF(2, stderr, "+ %s() {\n", __FUNCTION__); |
435 | | |
436 | 28.2k | const uint8_t* code_start = context->rules->code_start; |
437 | 28.2k | const uint8_t* ip = code_start; |
438 | | |
439 | | // One byte past the last instruction of the code section, used by |
440 | | // ensure_within_code to reject a jump that leaves the code buffer. |
441 | 28.2k | const uint8_t* code_end = code_start + |
442 | 28.2k | yr_arena_get_current_offset( |
443 | 28.2k | context->rules->arena, YR_CODE_SECTION); |
444 | | |
445 | 28.2k | YR_VALUE mem[MEM_SIZE]; |
446 | 28.2k | YR_VALUE args[YR_MAX_FUNCTION_ARGS]; |
447 | 28.2k | YR_VALUE r1; |
448 | 28.2k | YR_VALUE r2; |
449 | 28.2k | YR_VALUE r3; |
450 | 28.2k | YR_VALUE r4; |
451 | | |
452 | 28.2k | YR_VALUE_STACK stack; |
453 | | |
454 | 28.2k | uint64_t elapsed_time; |
455 | | |
456 | | #ifdef YR_PROFILING_ENABLED |
457 | | uint64_t start_time; |
458 | | #endif |
459 | | |
460 | 28.2k | uint32_t current_rule_idx = 0; |
461 | 28.2k | YR_RULE* current_rule = NULL; |
462 | 28.2k | YR_RULE* rule; |
463 | 28.2k | YR_MATCH* match; |
464 | 28.2k | YR_OBJECT_FUNCTION* function; |
465 | 28.2k | YR_OBJECT** obj_ptr; |
466 | 28.2k | YR_ARENA* obj_arena; |
467 | 28.2k | YR_NOTEBOOK* it_notebook; |
468 | | |
469 | 28.2k | char* identifier; |
470 | 28.2k | char* args_fmt; |
471 | | |
472 | 28.2k | int found; |
473 | 28.2k | int count; |
474 | 28.2k | int result = ERROR_SUCCESS; |
475 | 28.2k | int cycle = 0; |
476 | 28.2k | int obj_count = 0; |
477 | | |
478 | 28.2k | bool stop = false; |
479 | | |
480 | 28.2k | uint8_t opcode; |
481 | | |
482 | 28.2k | yr_get_configuration_uint32(YR_CONFIG_STACK_SIZE, &stack.capacity); |
483 | | |
484 | 28.2k | if ((size_t) stack.capacity > SIZE_MAX / sizeof(YR_VALUE)) |
485 | 0 | return ERROR_INSUFFICIENT_MEMORY; |
486 | | |
487 | 28.2k | stack.sp = 0; |
488 | 28.2k | stack.items = (YR_VALUE*) yr_malloc(stack.capacity * sizeof(YR_VALUE)); |
489 | | |
490 | 28.2k | if (stack.items == NULL) |
491 | 0 | return ERROR_INSUFFICIENT_MEMORY; |
492 | | |
493 | 28.2k | FAIL_ON_ERROR_WITH_CLEANUP( |
494 | 28.2k | yr_arena_create(1, 512 * sizeof(YR_OBJECT*), &obj_arena), |
495 | 28.2k | yr_free(stack.items)); |
496 | | |
497 | 28.2k | FAIL_ON_ERROR_WITH_CLEANUP( |
498 | 28.2k | yr_notebook_create(512 * sizeof(YR_ITERATOR), &it_notebook), |
499 | 28.2k | yr_arena_release(obj_arena); |
500 | 28.2k | yr_free(stack.items)); |
501 | | |
502 | | #ifdef YR_PROFILING_ENABLED |
503 | | start_time = yr_stopwatch_elapsed_ns(&context->stopwatch); |
504 | | #endif |
505 | | |
506 | 28.2k | #if YR_PARANOID_EXEC |
507 | 28.2k | memset(mem, 0, MEM_SIZE * sizeof(mem[0])); |
508 | 28.2k | #endif |
509 | | |
510 | 505k | while (!stop) |
511 | 476k | { |
512 | 476k | #if YR_PARANOID_EXEC |
513 | 476k | ensure_within_code(ip); |
514 | 476k | #endif |
515 | | |
516 | | // Read the opcode from the address indicated by the instruction pointer. |
517 | 476k | opcode = *ip; |
518 | | |
519 | | // Advance the instruction pointer, which now points past the opcode. |
520 | 476k | ip++; |
521 | | |
522 | 476k | switch (opcode) |
523 | 476k | { |
524 | 0 | case OP_NOP: |
525 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_NOP: // %s()\n", __FUNCTION__); |
526 | 0 | break; |
527 | | |
528 | 28.2k | case OP_HALT: |
529 | 28.2k | YR_DEBUG_FPRINTF(2, stderr, "- case OP_HALT: // %s()\n", __FUNCTION__); |
530 | 28.2k | assert(stack.sp == 0); // When HALT is reached the stack should be empty. |
531 | 28.2k | stop = true; |
532 | 28.2k | break; |
533 | | |
534 | 0 | case OP_ITER_START_ARRAY: |
535 | 0 | YR_DEBUG_FPRINTF( |
536 | 0 | 2, stderr, "- case OP_ITER_START_ARRAY: // %s()\n", __FUNCTION__); |
537 | 0 | r2.p = yr_notebook_alloc(it_notebook, sizeof(YR_ITERATOR)); |
538 | |
|
539 | 0 | if (r2.p == NULL) |
540 | 0 | { |
541 | 0 | result = ERROR_INSUFFICIENT_MEMORY; |
542 | 0 | } |
543 | 0 | else |
544 | 0 | { |
545 | 0 | pop(r1); |
546 | 0 | r2.it->array_it.array = r1.o; |
547 | 0 | r2.it->array_it.index = 0; |
548 | 0 | r2.it->next_func_idx = ITER_NEXT_ARRAY; |
549 | 0 | push(r2); |
550 | 0 | } |
551 | | |
552 | 0 | stop = (result != ERROR_SUCCESS); |
553 | 0 | break; |
554 | | |
555 | 0 | case OP_ITER_START_DICT: |
556 | 0 | YR_DEBUG_FPRINTF( |
557 | 0 | 2, stderr, "- case OP_ITER_START_DICT: // %s()\n", __FUNCTION__); |
558 | 0 | r2.p = yr_notebook_alloc(it_notebook, sizeof(YR_ITERATOR)); |
559 | |
|
560 | 0 | if (r2.p == NULL) |
561 | 0 | { |
562 | 0 | result = ERROR_INSUFFICIENT_MEMORY; |
563 | 0 | } |
564 | 0 | else |
565 | 0 | { |
566 | 0 | pop(r1); |
567 | 0 | r2.it->dict_it.dict = r1.o; |
568 | 0 | r2.it->dict_it.index = 0; |
569 | 0 | r2.it->next_func_idx = ITER_NEXT_DICT; |
570 | 0 | push(r2); |
571 | 0 | } |
572 | | |
573 | 0 | stop = (result != ERROR_SUCCESS); |
574 | 0 | break; |
575 | | |
576 | 0 | case OP_ITER_START_INT_RANGE: |
577 | 0 | YR_DEBUG_FPRINTF( |
578 | 0 | 2, stderr, "- case OP_ITER_START_INT_RANGE: // %s()\n", __FUNCTION__); |
579 | | // Creates an iterator for an integer range. The higher bound of the |
580 | | // range is at the top of the stack followed by the lower bound. |
581 | 0 | r3.p = yr_notebook_alloc(it_notebook, sizeof(YR_ITERATOR)); |
582 | |
|
583 | 0 | if (r3.p == NULL) |
584 | 0 | { |
585 | 0 | result = ERROR_INSUFFICIENT_MEMORY; |
586 | 0 | } |
587 | 0 | else |
588 | 0 | { |
589 | 0 | pop(r2); |
590 | 0 | pop(r1); |
591 | 0 | r3.it->int_range_it.next = r1.i; |
592 | 0 | r3.it->int_range_it.last = r2.i; |
593 | 0 | r3.it->next_func_idx = ITER_NEXT_INT_RANGE; |
594 | 0 | push(r3); |
595 | 0 | } |
596 | | |
597 | 0 | stop = (result != ERROR_SUCCESS); |
598 | 0 | break; |
599 | | |
600 | 0 | case OP_ITER_START_INT_ENUM: |
601 | 0 | YR_DEBUG_FPRINTF( |
602 | 0 | 2, stderr, "- case OP_ITER_START_INT_ENUM: // %s()\n", __FUNCTION__); |
603 | | // Creates an iterator for an integer enumeration. The number of items |
604 | | // in the enumeration is at the top of the stack, followed by the |
605 | | // items in reverse order. |
606 | 0 | pop(r1); |
607 | | |
608 | | // The count comes from the compiled rules and a hand-crafted file can |
609 | | // set it to a value that overflows the allocation size below or makes |
610 | | // the loop pop past the stack. It can't be larger than the number of |
611 | | // items currently on the stack. |
612 | 0 | if (r1.i < 0 || (uint64_t) r1.i > stack.sp) |
613 | 0 | { |
614 | 0 | result = ERROR_INTERNAL_FATAL_ERROR; |
615 | 0 | stop = true; |
616 | 0 | break; |
617 | 0 | } |
618 | | |
619 | 0 | r3.p = yr_notebook_alloc( |
620 | 0 | it_notebook, sizeof(YR_ITERATOR) + sizeof(uint64_t) * (size_t) r1.i); |
621 | |
|
622 | 0 | if (r3.p == NULL) |
623 | 0 | { |
624 | 0 | result = ERROR_INSUFFICIENT_MEMORY; |
625 | 0 | } |
626 | 0 | else |
627 | 0 | { |
628 | 0 | r3.it->int_enum_it.count = r1.i; |
629 | 0 | r3.it->int_enum_it.next = 0; |
630 | 0 | r3.it->next_func_idx = ITER_NEXT_INT_ENUM; |
631 | |
|
632 | 0 | for (int64_t i = r1.i; i > 0; i--) |
633 | 0 | { |
634 | 0 | pop(r2); |
635 | 0 | r3.it->int_enum_it.items[i - 1] = r2.i; |
636 | 0 | } |
637 | |
|
638 | 0 | push(r3); |
639 | 0 | } |
640 | | |
641 | 0 | stop = (result != ERROR_SUCCESS); |
642 | 0 | break; |
643 | | |
644 | 0 | case OP_ITER_START_STRING_SET: |
645 | 0 | YR_DEBUG_FPRINTF( |
646 | 0 | 2, |
647 | 0 | stderr, |
648 | 0 | "- case OP_ITER_START_STRING_SET: // %s()\n", |
649 | 0 | __FUNCTION__); |
650 | |
|
651 | 0 | pop(r1); |
652 | | |
653 | | // One extra value (the undefined string) is popped below, so the count |
654 | | // must be strictly smaller than the number of items on the stack. A |
655 | | // hand-crafted count would otherwise overflow the allocation or pop past |
656 | | // the stack. |
657 | 0 | if (r1.i < 0 || (uint64_t) r1.i >= stack.sp) |
658 | 0 | { |
659 | 0 | result = ERROR_INTERNAL_FATAL_ERROR; |
660 | 0 | stop = true; |
661 | 0 | break; |
662 | 0 | } |
663 | | |
664 | 0 | r3.p = yr_notebook_alloc( |
665 | 0 | it_notebook, |
666 | 0 | sizeof(YR_ITERATOR) + sizeof(YR_STRING*) * (size_t) r1.i); |
667 | |
|
668 | 0 | if (r3.p == NULL) |
669 | 0 | { |
670 | 0 | result = ERROR_INSUFFICIENT_MEMORY; |
671 | 0 | } |
672 | 0 | else |
673 | 0 | { |
674 | 0 | r3.it->string_set_it.count = r1.i; |
675 | 0 | r3.it->string_set_it.index = 0; |
676 | 0 | r3.it->next_func_idx = ITER_NEXT_STRING_SET; |
677 | |
|
678 | 0 | for (int64_t i = r1.i; i > 0; i--) |
679 | 0 | { |
680 | 0 | pop(r2); |
681 | 0 | r3.it->string_set_it.strings[i - 1] = r2.s; |
682 | 0 | } |
683 | | |
684 | | // One last pop of the UNDEFINED string |
685 | 0 | pop(r2); |
686 | 0 | push(r3); |
687 | 0 | } |
688 | | |
689 | 0 | stop = (result != ERROR_SUCCESS); |
690 | 0 | break; |
691 | | |
692 | 0 | case OP_ITER_START_TEXT_STRING_SET: |
693 | 0 | YR_DEBUG_FPRINTF( |
694 | 0 | 2, |
695 | 0 | stderr, |
696 | 0 | "- case OP_ITER_START_TEXT_STRING_SET: // %s()\n", |
697 | 0 | __FUNCTION__); |
698 | |
|
699 | 0 | pop(r1); |
700 | | |
701 | | // The count comes from the compiled rules and can't be larger than the |
702 | | // number of items on the stack, otherwise the allocation below overflows |
703 | | // or the loop pops past the stack. |
704 | 0 | if (r1.i < 0 || (uint64_t) r1.i > stack.sp) |
705 | 0 | { |
706 | 0 | result = ERROR_INTERNAL_FATAL_ERROR; |
707 | 0 | stop = true; |
708 | 0 | break; |
709 | 0 | } |
710 | | |
711 | 0 | r3.p = yr_notebook_alloc( |
712 | 0 | it_notebook, |
713 | 0 | sizeof(YR_ITERATOR) + sizeof(SIZED_STRING*) * (size_t) r1.i); |
714 | |
|
715 | 0 | if (r3.p == NULL) |
716 | 0 | { |
717 | 0 | result = ERROR_INSUFFICIENT_MEMORY; |
718 | 0 | } |
719 | 0 | else |
720 | 0 | { |
721 | 0 | r3.it->text_string_set_it.count = r1.i; |
722 | 0 | r3.it->text_string_set_it.index = 0; |
723 | 0 | r3.it->next_func_idx = ITER_NEXT_TEXT_STRING_SET; |
724 | |
|
725 | 0 | for (int64_t i = r1.i; i > 0; i--) |
726 | 0 | { |
727 | 0 | pop(r2); |
728 | 0 | r3.it->text_string_set_it.strings[i - 1] = r2.ss; |
729 | 0 | } |
730 | |
|
731 | 0 | push(r3); |
732 | 0 | } |
733 | | |
734 | 0 | stop = (result != ERROR_SUCCESS); |
735 | 0 | break; |
736 | | |
737 | 0 | case OP_ITER_NEXT: |
738 | 0 | YR_DEBUG_FPRINTF( |
739 | 0 | 2, stderr, "- case OP_ITER_NEXT: // %s()\n", __FUNCTION__); |
740 | | // Loads the iterator in r1, but leaves the iterator in the stack. |
741 | 0 | pop(r1); |
742 | 0 | push(r1); |
743 | |
|
744 | 0 | if (r1.it->next_func_idx < |
745 | 0 | sizeof(iter_next_func_table) / sizeof(YR_ITERATOR_NEXT_FUNC)) |
746 | 0 | { |
747 | | // The iterator's next function is responsible for pushing the next |
748 | | // item in the stack, and a boolean indicating if there are more items |
749 | | // to retrieve. The boolean will be at the top of the stack after |
750 | | // calling "next". |
751 | 0 | result = iter_next_func_table[r1.it->next_func_idx](r1.it, &stack); |
752 | 0 | } |
753 | 0 | else |
754 | 0 | { |
755 | | // next_func_idx is outside the valid range, this should not happend. |
756 | 0 | result = ERROR_INTERNAL_FATAL_ERROR; |
757 | 0 | } |
758 | |
|
759 | 0 | stop = (result != ERROR_SUCCESS); |
760 | 0 | break; |
761 | | |
762 | 0 | case OP_ITER_CONDITION: |
763 | 0 | YR_DEBUG_FPRINTF( |
764 | 0 | 2, stderr, "- case OP_ITER_CONDITION: // %s()\n", __FUNCTION__); |
765 | | |
766 | | // Evaluate the iteration condition of the loop. This instruction |
767 | | // evaluates to 1 if the loop should continue and 0 if it shouldn't |
768 | | // (due to short-circuit evaluation). |
769 | |
|
770 | 0 | pop(r2); // min. expression - all, any, none, integer |
771 | 0 | pop(r3); // number of true expressions |
772 | 0 | pop(r4); // last expression result |
773 | | |
774 | | // In case of 'all' loop, end once we the body failed |
775 | 0 | if (is_undef(r2)) |
776 | 0 | { |
777 | 0 | r1.i = r4.i != 0 ? 1 : 0; |
778 | 0 | } |
779 | | // In case of 'none' loop, end once the body succeed |
780 | 0 | else if (r2.i == 0) |
781 | 0 | { |
782 | 0 | r1.i = r4.i != 1 ? 1 : 0; |
783 | 0 | } |
784 | | // In case of other loops, end once we satified min. expr. |
785 | 0 | else |
786 | 0 | { |
787 | 0 | r1.i = r3.i + r4.i < r2.i ? 1 : 0; |
788 | 0 | } |
789 | | |
790 | | // Push whether loop should continue and repush |
791 | | // the last expression result |
792 | 0 | push(r1); |
793 | 0 | push(r4); |
794 | 0 | break; |
795 | | |
796 | 0 | case OP_ITER_END: |
797 | 0 | YR_DEBUG_FPRINTF( |
798 | 0 | 2, stderr, "- case OP_ITER_END: // %s()\n", __FUNCTION__); |
799 | | |
800 | | // Evaluate the whole loop. Whether it was successful or not |
801 | | // and whether it satisfied it's quantifier. |
802 | |
|
803 | 0 | pop(r2); // min. expression - all, any, none, integer |
804 | 0 | pop(r3); // number of true expressions |
805 | 0 | pop(r4); // number of total iterations |
806 | | |
807 | | // If there was 0 iterations in total, it doesn't |
808 | | // matter what other numbers show. We can't evaluate |
809 | | // the loop as true. |
810 | 0 | if (r4.i == 0) |
811 | 0 | { |
812 | 0 | r1.i = 0; |
813 | 0 | } |
814 | 0 | else if (is_undef(r2)) |
815 | 0 | { |
816 | 0 | r1.i = r3.i == r4.i ? 1 : 0; |
817 | 0 | } |
818 | 0 | else if (r2.i == 0) |
819 | 0 | { |
820 | 0 | r1.i = r3.i == 0 ? 1 : 0; |
821 | 0 | } |
822 | 0 | else |
823 | 0 | { |
824 | 0 | r1.i = r3.i >= r2.i ? 1 : 0; |
825 | 0 | } |
826 | |
|
827 | 0 | push(r1); |
828 | 0 | break; |
829 | | |
830 | 12.9k | case OP_PUSH: |
831 | 12.9k | YR_DEBUG_FPRINTF(2, stderr, "- case OP_PUSH: // %s()\n", __FUNCTION__); |
832 | 12.9k | r1.i = yr_unaligned_u64(ip); |
833 | 12.9k | ip += sizeof(uint64_t); |
834 | 12.9k | push(r1); |
835 | 12.9k | break; |
836 | | |
837 | 57.0k | case OP_PUSH_8: |
838 | 57.0k | r1.i = *ip; |
839 | 57.0k | YR_DEBUG_FPRINTF( |
840 | 57.0k | 2, |
841 | 57.0k | stderr, |
842 | 57.0k | "- case OP_PUSH_8: r1.i=%" PRId64 " // %s()\n", |
843 | 57.0k | r1.i, |
844 | 57.0k | __FUNCTION__); |
845 | 57.0k | ip += sizeof(uint8_t); |
846 | 57.0k | push(r1); |
847 | 57.0k | break; |
848 | | |
849 | 0 | case OP_PUSH_16: |
850 | 0 | r1.i = yr_unaligned_u16(ip); |
851 | 0 | YR_DEBUG_FPRINTF( |
852 | 0 | 2, |
853 | 0 | stderr, |
854 | 0 | "- case OP_PUSH_16: r1.i=%" PRId64 " // %s()\n", |
855 | 0 | r1.i, |
856 | 0 | __FUNCTION__); |
857 | 0 | ip += sizeof(uint16_t); |
858 | 0 | push(r1); |
859 | 0 | break; |
860 | | |
861 | 0 | case OP_PUSH_32: |
862 | 0 | r1.i = yr_unaligned_u32(ip); |
863 | 0 | YR_DEBUG_FPRINTF( |
864 | 0 | 2, |
865 | 0 | stderr, |
866 | 0 | "- case OP_PUSH_32: r1.i=%" PRId64 " // %s()\n", |
867 | 0 | r1.i, |
868 | 0 | __FUNCTION__); |
869 | 0 | ip += sizeof(uint32_t); |
870 | 0 | push(r1); |
871 | 0 | break; |
872 | | |
873 | 0 | case OP_PUSH_U: |
874 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_PUSH_U: // %s()\n", __FUNCTION__); |
875 | 0 | r1.i = YR_UNDEFINED; |
876 | 0 | push(r1); |
877 | 0 | break; |
878 | | |
879 | 0 | case OP_POP: |
880 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_POP: // %s()\n", __FUNCTION__); |
881 | 0 | pop(r1); |
882 | 0 | break; |
883 | | |
884 | 0 | case OP_CLEAR_M: |
885 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_CLEAR_M: // %s()\n", __FUNCTION__); |
886 | 0 | r1.i = yr_unaligned_u64(ip); |
887 | 0 | ip += sizeof(uint64_t); |
888 | 0 | #if YR_PARANOID_EXEC |
889 | 0 | ensure_within_mem(r1.i); |
890 | 0 | #endif |
891 | 0 | mem[r1.i].i = 0; |
892 | 0 | break; |
893 | | |
894 | 0 | case OP_ADD_M: |
895 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_ADD_M: // %s()\n", __FUNCTION__); |
896 | 0 | r1.i = yr_unaligned_u64(ip); |
897 | 0 | ip += sizeof(uint64_t); |
898 | 0 | #if YR_PARANOID_EXEC |
899 | 0 | ensure_within_mem(r1.i); |
900 | 0 | #endif |
901 | 0 | pop(r2); |
902 | 0 | if (!is_undef(r2)) |
903 | 0 | mem[r1.i].i += r2.i; |
904 | 0 | break; |
905 | | |
906 | 0 | case OP_INCR_M: |
907 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INCR_M: // %s()\n", __FUNCTION__); |
908 | 0 | r1.i = yr_unaligned_u64(ip); |
909 | 0 | ip += sizeof(uint64_t); |
910 | 0 | #if YR_PARANOID_EXEC |
911 | 0 | ensure_within_mem(r1.i); |
912 | 0 | #endif |
913 | 0 | mem[r1.i].i++; |
914 | 0 | break; |
915 | | |
916 | 0 | case OP_PUSH_M: |
917 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_PUSH_M: // %s()\n", __FUNCTION__); |
918 | 0 | r1.i = yr_unaligned_u64(ip); |
919 | 0 | ip += sizeof(uint64_t); |
920 | 0 | #if YR_PARANOID_EXEC |
921 | 0 | ensure_within_mem(r1.i); |
922 | 0 | #endif |
923 | 0 | r1 = mem[r1.i]; |
924 | 0 | push(r1); |
925 | 0 | break; |
926 | | |
927 | 0 | case OP_POP_M: |
928 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_POP_M: // %s()\n", __FUNCTION__); |
929 | 0 | r1.i = yr_unaligned_u64(ip); |
930 | 0 | ip += sizeof(uint64_t); |
931 | 0 | #if YR_PARANOID_EXEC |
932 | 0 | ensure_within_mem(r1.i); |
933 | 0 | #endif |
934 | 0 | pop(r2); |
935 | 0 | mem[r1.i] = r2; |
936 | 0 | break; |
937 | | |
938 | 0 | case OP_SET_M: |
939 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_SET_M: // %s()\n", __FUNCTION__); |
940 | 0 | r1.i = yr_unaligned_u64(ip); |
941 | 0 | ip += sizeof(uint64_t); |
942 | 0 | #if YR_PARANOID_EXEC |
943 | 0 | ensure_within_mem(r1.i); |
944 | 0 | #endif |
945 | 0 | pop(r2); |
946 | 0 | push(r2); |
947 | 0 | if (!is_undef(r2)) |
948 | 0 | mem[r1.i] = r2; |
949 | 0 | break; |
950 | | |
951 | 0 | case OP_SWAPUNDEF: |
952 | 0 | YR_DEBUG_FPRINTF( |
953 | 0 | 2, stderr, "- case OP_SWAPUNDEF: // %s()\n", __FUNCTION__); |
954 | 0 | r1.i = yr_unaligned_u64(ip); |
955 | 0 | ip += sizeof(uint64_t); |
956 | 0 | #if YR_PARANOID_EXEC |
957 | 0 | ensure_within_mem(r1.i); |
958 | 0 | #endif |
959 | 0 | pop(r2); |
960 | |
|
961 | 0 | if (is_undef(r2)) |
962 | 0 | { |
963 | 0 | r1 = mem[r1.i]; |
964 | 0 | push(r1); |
965 | 0 | } |
966 | 0 | else |
967 | 0 | { |
968 | 0 | push(r2); |
969 | 0 | } |
970 | 0 | break; |
971 | | |
972 | 0 | case OP_JNUNDEF: |
973 | | // Jump if the top the stack is not undefined without modifying the stack. |
974 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_JNUNDEF: // %s()\n", __FUNCTION__); |
975 | 0 | pop(r1); |
976 | 0 | push(r1); |
977 | 0 | ip = jmp_if(!is_undef(r1), ip); |
978 | 0 | break; |
979 | | |
980 | 0 | case OP_JUNDEF_P: |
981 | | // Removes a value from the top of the stack and jump if the value is not |
982 | | // undefined. |
983 | 0 | YR_DEBUG_FPRINTF( |
984 | 0 | 2, stderr, "- case OP_JUNDEF_P: // %s()\n", __FUNCTION__); |
985 | 0 | pop(r1); |
986 | 0 | ip = jmp_if(is_undef(r1), ip); |
987 | 0 | break; |
988 | | |
989 | 0 | case OP_JL_P: |
990 | | // Pops two values A and B from the stack and jump if A < B. B is popped |
991 | | // first, and then A. |
992 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_JL_P: // %s()\n", __FUNCTION__); |
993 | 0 | pop(r2); |
994 | 0 | pop(r1); |
995 | 0 | ip = jmp_if(r1.i < r2.i, ip); |
996 | 0 | break; |
997 | | |
998 | 0 | case OP_JLE_P: |
999 | | // Pops two values A and B from the stack and jump if A <= B. B is popped |
1000 | | // first, and then A. |
1001 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_JLE_P: // %s()\n", __FUNCTION__); |
1002 | 0 | pop(r2); |
1003 | 0 | pop(r1); |
1004 | 0 | ip = jmp_if(r1.i <= r2.i, ip); |
1005 | 0 | break; |
1006 | | |
1007 | 0 | case OP_JTRUE: |
1008 | | // Jump if the top of the stack is true without modifying the stack. If |
1009 | | // the top of the stack is undefined the jump is not taken. |
1010 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_JTRUE: // %s()\n", __FUNCTION__); |
1011 | 0 | pop(r1); |
1012 | 0 | push(r1); |
1013 | 0 | ip = jmp_if(!is_undef(r1) && r1.i, ip); |
1014 | 0 | break; |
1015 | | |
1016 | 0 | case OP_JTRUE_P: |
1017 | | // Removes a value from the stack and jump if it is true. If the value |
1018 | | // is undefined the jump is not taken. |
1019 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_JTRUE_P: // %s()\n", __FUNCTION__); |
1020 | 0 | pop(r1); |
1021 | 0 | ip = jmp_if(!is_undef(r1) && r1.i, ip); |
1022 | 0 | break; |
1023 | | |
1024 | 13.4k | case OP_JFALSE: |
1025 | | // Jump if the top of the stack is false without modifying the stack. If |
1026 | | // the top of the stack is undefined the jump is not taken. |
1027 | 13.4k | YR_DEBUG_FPRINTF(2, stderr, "- case OP_JFALSE: // %s()\n", __FUNCTION__); |
1028 | 13.4k | pop(r1); |
1029 | 13.4k | push(r1); |
1030 | 13.4k | ip = jmp_if(!is_undef(r1) && !r1.i, ip); |
1031 | 13.4k | break; |
1032 | | |
1033 | 0 | case OP_JFALSE_P: |
1034 | | // Removes a value from the stack and jump if it is false. If the value |
1035 | | // is undefined the jump is not taken. |
1036 | 0 | YR_DEBUG_FPRINTF( |
1037 | 0 | 2, stderr, "- case OP_JFALSE_P: // %s()\n", __FUNCTION__); |
1038 | 0 | pop(r1); |
1039 | 0 | ip = jmp_if(!is_undef(r1) && !r1.i, ip); |
1040 | 0 | break; |
1041 | | |
1042 | 0 | case OP_JZ: |
1043 | | // Jump if the value at the top of the stack is 0 without modifying the |
1044 | | // stack. |
1045 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_JZ: // %s()\n", __FUNCTION__); |
1046 | 0 | pop(r1); |
1047 | 0 | push(r1); |
1048 | 0 | ip = jmp_if(r1.i == 0, ip); |
1049 | 0 | break; |
1050 | | |
1051 | 0 | case OP_JZ_P: |
1052 | | // Removes a value from the stack and jump if the value is 0. |
1053 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_JZ_P: // %s()\n", __FUNCTION__); |
1054 | 0 | pop(r1); |
1055 | 0 | ip = jmp_if(r1.i == 0, ip); |
1056 | 0 | break; |
1057 | | |
1058 | 12.8k | case OP_AND: |
1059 | 12.8k | YR_DEBUG_FPRINTF(2, stderr, "- case OP_AND: // %s()\n", __FUNCTION__); |
1060 | 12.8k | pop(r2); |
1061 | 12.8k | pop(r1); |
1062 | | |
1063 | 12.8k | if (is_undef(r1)) |
1064 | 0 | r1.i = 0; |
1065 | | |
1066 | 12.8k | if (is_undef(r2)) |
1067 | 4 | r2.i = 0; |
1068 | | |
1069 | 12.8k | r1.i = r1.i && r2.i; |
1070 | 12.8k | push(r1); |
1071 | 12.8k | break; |
1072 | | |
1073 | 0 | case OP_OR: |
1074 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_OR: // %s()\n", __FUNCTION__); |
1075 | 0 | pop(r2); |
1076 | 0 | pop(r1); |
1077 | |
|
1078 | 0 | if (is_undef(r1)) |
1079 | 0 | r1.i = 0; |
1080 | |
|
1081 | 0 | if (is_undef(r2)) |
1082 | 0 | r2.i = 0; |
1083 | |
|
1084 | 0 | r1.i = r1.i || r2.i; |
1085 | 0 | push(r1); |
1086 | 0 | break; |
1087 | | |
1088 | 0 | case OP_NOT: |
1089 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_NOT: // %s()\n", __FUNCTION__); |
1090 | 0 | pop(r1); |
1091 | |
|
1092 | 0 | if (is_undef(r1)) |
1093 | 0 | r1.i = YR_UNDEFINED; |
1094 | 0 | else |
1095 | 0 | r1.i = !r1.i; |
1096 | |
|
1097 | 0 | push(r1); |
1098 | 0 | break; |
1099 | | |
1100 | 0 | case OP_DEFINED: |
1101 | 0 | pop(r1); |
1102 | 0 | r1.i = !is_undef(r1); |
1103 | 0 | push(r1); |
1104 | 0 | break; |
1105 | | |
1106 | 0 | case OP_MOD: |
1107 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_MOD: // %s()\n", __FUNCTION__); |
1108 | 0 | pop(r2); |
1109 | 0 | pop(r1); |
1110 | 0 | ensure_defined(r2); |
1111 | 0 | ensure_defined(r1); |
1112 | | // If divisor is zero the result is undefined. It's also undefined |
1113 | | // when dividing INT64_MIN by -1. |
1114 | 0 | if (r2.i == 0 || (r1.i == INT64_MIN && r2.i == -1)) |
1115 | 0 | r1.i = YR_UNDEFINED; |
1116 | 0 | else |
1117 | 0 | r1.i = r1.i % r2.i; |
1118 | 0 | push(r1); |
1119 | 0 | break; |
1120 | | |
1121 | 0 | case OP_SHR: |
1122 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_SHR: // %s()\n", __FUNCTION__); |
1123 | 0 | pop(r2); |
1124 | 0 | pop(r1); |
1125 | 0 | ensure_defined(r2); |
1126 | 0 | ensure_defined(r1); |
1127 | 0 | if (r2.i < 0) |
1128 | 0 | r1.i = YR_UNDEFINED; |
1129 | 0 | else if (r2.i < 64) |
1130 | 0 | r1.i = r1.i >> r2.i; |
1131 | 0 | else |
1132 | 0 | r1.i = 0; |
1133 | 0 | push(r1); |
1134 | 0 | break; |
1135 | | |
1136 | 0 | case OP_SHL: |
1137 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_SHL: // %s()\n", __FUNCTION__); |
1138 | 0 | pop(r2); |
1139 | 0 | pop(r1); |
1140 | 0 | ensure_defined(r2); |
1141 | 0 | ensure_defined(r1); |
1142 | 0 | if (r2.i < 0) |
1143 | 0 | r1.i = YR_UNDEFINED; |
1144 | 0 | else if (r2.i < 64) |
1145 | 0 | r1.i = r1.i << r2.i; |
1146 | 0 | else |
1147 | 0 | r1.i = 0; |
1148 | 0 | push(r1); |
1149 | 0 | break; |
1150 | | |
1151 | 0 | case OP_BITWISE_NOT: |
1152 | 0 | YR_DEBUG_FPRINTF( |
1153 | 0 | 2, stderr, "- case OP_BITWISE_NOT: // %s()\n", __FUNCTION__); |
1154 | 0 | pop(r1); |
1155 | 0 | ensure_defined(r1); |
1156 | 0 | r1.i = ~r1.i; |
1157 | 0 | push(r1); |
1158 | 0 | break; |
1159 | | |
1160 | 0 | case OP_BITWISE_AND: |
1161 | 0 | YR_DEBUG_FPRINTF( |
1162 | 0 | 2, stderr, "- case OP_BITWISE_AND: // %s()\n", __FUNCTION__); |
1163 | 0 | pop(r2); |
1164 | 0 | pop(r1); |
1165 | 0 | ensure_defined(r2); |
1166 | 0 | ensure_defined(r1); |
1167 | 0 | r1.i = r1.i & r2.i; |
1168 | 0 | push(r1); |
1169 | 0 | break; |
1170 | | |
1171 | 0 | case OP_BITWISE_OR: |
1172 | 0 | YR_DEBUG_FPRINTF( |
1173 | 0 | 2, stderr, "- case OP_BITWISE_OR: // %s()\n", __FUNCTION__); |
1174 | 0 | pop(r2); |
1175 | 0 | pop(r1); |
1176 | 0 | ensure_defined(r2); |
1177 | 0 | ensure_defined(r1); |
1178 | 0 | r1.i = r1.i | r2.i; |
1179 | 0 | push(r1); |
1180 | 0 | break; |
1181 | | |
1182 | 0 | case OP_BITWISE_XOR: |
1183 | 0 | YR_DEBUG_FPRINTF( |
1184 | 0 | 2, stderr, "- case OP_BITWISE_XOR: // %s()\n", __FUNCTION__); |
1185 | 0 | pop(r2); |
1186 | 0 | pop(r1); |
1187 | 0 | ensure_defined(r2); |
1188 | 0 | ensure_defined(r1); |
1189 | 0 | r1.i = r1.i ^ r2.i; |
1190 | 0 | push(r1); |
1191 | 0 | break; |
1192 | | |
1193 | 0 | case OP_PUSH_RULE: |
1194 | 0 | YR_DEBUG_FPRINTF( |
1195 | 0 | 2, stderr, "- case OP_PUSH_RULE: // %s()\n", __FUNCTION__); |
1196 | 0 | r1.i = yr_unaligned_u64(ip); |
1197 | 0 | ip += sizeof(uint64_t); |
1198 | |
|
1199 | 0 | rule = &context->rules->rules_table[r1.i]; |
1200 | |
|
1201 | 0 | #if YR_PARANOID_EXEC |
1202 | 0 | ensure_within_rules_arena(rule); |
1203 | 0 | #endif |
1204 | |
|
1205 | 0 | if (RULE_IS_DISABLED(rule)) |
1206 | 0 | { |
1207 | | // Inside a rule set, as in "N of (a, b, c)", YR_UNDEFINED is the |
1208 | | // end-of-set marker, so there a disabled rule must be pushed as "not |
1209 | | // matched". The members of a set are consecutive OP_PUSH_RULE |
1210 | | // instructions followed by OP_OF or OP_OF_PERCENT. |
1211 | 0 | const uint8_t* next = ip; |
1212 | |
|
1213 | 0 | while (next < code_end && *next == OP_PUSH_RULE) |
1214 | 0 | next += 1 + sizeof(uint64_t); |
1215 | |
|
1216 | 0 | if (next < code_end && (*next == OP_OF || *next == OP_OF_PERCENT)) |
1217 | 0 | r2.i = 0; |
1218 | 0 | else |
1219 | 0 | r2.i = YR_UNDEFINED; |
1220 | 0 | } |
1221 | 0 | else |
1222 | 0 | { |
1223 | 0 | if (yr_bitmask_is_set(context->rule_matches_flags, r1.i)) |
1224 | 0 | r2.i = 1; |
1225 | 0 | else |
1226 | 0 | r2.i = 0; |
1227 | 0 | } |
1228 | |
|
1229 | 0 | push(r2); |
1230 | 0 | break; |
1231 | | |
1232 | 21.1k | case OP_INIT_RULE: |
1233 | 21.1k | YR_DEBUG_FPRINTF( |
1234 | 21.1k | 2, stderr, "- case OP_INIT_RULE: // %s()\n", __FUNCTION__); |
1235 | | |
1236 | | // After the opcode there's an int32_t corresponding to the jump's |
1237 | | // offset and an uint32_t corresponding to the rule's index. |
1238 | 21.1k | current_rule_idx = yr_unaligned_u32(ip + sizeof(int32_t)); |
1239 | | |
1240 | | // The curent rule index can't be larger than the number of rules. |
1241 | 21.1k | assert(current_rule_idx < context->rules->num_rules); |
1242 | | |
1243 | 21.1k | current_rule = &context->rules->rules_table[current_rule_idx]; |
1244 | | |
1245 | 21.1k | #if YR_PARANOID_EXEC |
1246 | 21.1k | ensure_within_rules_arena(current_rule); |
1247 | 21.1k | #endif |
1248 | | |
1249 | | // If the rule is disabled, let's skip its code. |
1250 | 21.1k | bool skip_rule = RULE_IS_DISABLED(current_rule); |
1251 | | |
1252 | | // The rule is also skipped if it is not required to be evaluated. |
1253 | 21.1k | skip_rule |= yr_bitmask_is_not_set( |
1254 | 21.1k | context->required_eval, current_rule_idx); |
1255 | | |
1256 | 21.1k | ip = jmp_if(skip_rule, ip); |
1257 | | |
1258 | 21.1k | if (skip_rule) |
1259 | 0 | { |
1260 | | // If the rule is skipped it is false, and if a global rule is false |
1261 | | // we must mark its namespace as unsatisfied. |
1262 | 0 | if (RULE_IS_GLOBAL(current_rule)) |
1263 | 0 | yr_bitmask_set(context->ns_unsatisfied_flags, current_rule->ns->idx); |
1264 | 0 | } |
1265 | 21.1k | else |
1266 | 21.1k | { |
1267 | | // If not taking the jump, skip the bytes corresponding to the |
1268 | | // rule's index. |
1269 | 21.1k | ip += sizeof(uint32_t); |
1270 | 21.1k | } |
1271 | | |
1272 | 21.1k | break; |
1273 | | |
1274 | 21.1k | case OP_MATCH_RULE: |
1275 | 21.1k | YR_DEBUG_FPRINTF( |
1276 | 21.1k | 2, stderr, "- case OP_MATCH_RULE: // %s()\n", __FUNCTION__); |
1277 | 21.1k | pop(r1); |
1278 | | |
1279 | 21.1k | r2.i = yr_unaligned_u64(ip); |
1280 | 21.1k | ip += sizeof(uint64_t); |
1281 | | |
1282 | 21.1k | rule = &context->rules->rules_table[r2.i]; |
1283 | | |
1284 | 21.1k | #if YR_PARANOID_EXEC |
1285 | 21.1k | ensure_within_rules_arena(rule); |
1286 | 21.1k | #endif |
1287 | | |
1288 | 21.1k | if (!is_undef(r1) && r1.i) |
1289 | 2.20k | yr_bitmask_set(context->rule_matches_flags, r2.i); |
1290 | 18.9k | else if (RULE_IS_GLOBAL(rule)) |
1291 | 0 | yr_bitmask_set(context->ns_unsatisfied_flags, rule->ns->idx); |
1292 | | |
1293 | | #ifdef YR_PROFILING_ENABLED |
1294 | | elapsed_time = yr_stopwatch_elapsed_ns(&context->stopwatch); |
1295 | | context->profiling_info[r2.i].exec_time += (elapsed_time - start_time); |
1296 | | start_time = elapsed_time; |
1297 | | #endif |
1298 | | |
1299 | 21.1k | assert(stack.sp == 0); // at this point the stack should be empty. |
1300 | 21.1k | break; |
1301 | | |
1302 | 50.3k | case OP_OBJ_LOAD: |
1303 | 50.3k | YR_DEBUG_FPRINTF( |
1304 | 50.3k | 2, stderr, "- case OP_OBJ_LOAD: // %s()\n", __FUNCTION__); |
1305 | | |
1306 | 50.3k | identifier = yr_unaligned_char_ptr(ip); |
1307 | 50.3k | ip += sizeof(uint64_t); |
1308 | | |
1309 | 50.3k | #if YR_PARANOID_EXEC |
1310 | 50.3k | ensure_within_rules_arena(identifier); |
1311 | 50.3k | #endif |
1312 | | |
1313 | 50.3k | r1.o = (YR_OBJECT*) yr_hash_table_lookup( |
1314 | 50.3k | context->objects_table, identifier, NULL); |
1315 | | |
1316 | 50.3k | assert(r1.o != NULL); |
1317 | 50.3k | push(r1); |
1318 | 50.3k | break; |
1319 | | |
1320 | 75.2k | case OP_OBJ_FIELD: |
1321 | 75.2k | YR_DEBUG_FPRINTF( |
1322 | 75.2k | 2, stderr, "- case OP_OBJ_FIELD: // %s()\n", __FUNCTION__); |
1323 | | |
1324 | 75.2k | identifier = yr_unaligned_char_ptr(ip); |
1325 | 75.2k | ip += sizeof(uint64_t); |
1326 | | |
1327 | 75.2k | #if YR_PARANOID_EXEC |
1328 | 75.2k | ensure_within_rules_arena(identifier); |
1329 | 75.2k | #endif |
1330 | | |
1331 | 75.2k | pop(r1); |
1332 | 75.2k | ensure_defined(r1); |
1333 | | |
1334 | 54.9k | r1.o = yr_object_lookup_field(r1.o, identifier); |
1335 | | |
1336 | 54.9k | if (r1.o == NULL) |
1337 | 0 | { |
1338 | 0 | result = ERROR_INVALID_FIELD_NAME; |
1339 | 0 | stop = true; |
1340 | 0 | break; |
1341 | 0 | } |
1342 | | |
1343 | 54.9k | push(r1); |
1344 | 54.9k | break; |
1345 | | |
1346 | 50.3k | case OP_OBJ_VALUE: |
1347 | 50.3k | YR_DEBUG_FPRINTF( |
1348 | 50.3k | 2, stderr, "- case OP_OBJ_VALUE: // %s()\n", __FUNCTION__); |
1349 | 50.3k | pop(r1); |
1350 | 50.3k | ensure_defined(r1); |
1351 | | |
1352 | 27.8k | #if YR_PARANOID_EXEC |
1353 | 27.8k | check_object_canary(r1.o); |
1354 | 27.8k | #endif |
1355 | | |
1356 | 27.8k | switch (r1.o->type) |
1357 | 27.8k | { |
1358 | 10.9k | case OBJECT_TYPE_INTEGER: |
1359 | 10.9k | r1.i = r1.o->value.i; |
1360 | 10.9k | break; |
1361 | | |
1362 | 9.84k | case OBJECT_TYPE_FLOAT: |
1363 | 9.84k | if (yr_isnan(r1.o->value.d)) |
1364 | 4 | r1.i = YR_UNDEFINED; |
1365 | 9.83k | else |
1366 | 9.83k | r1.d = r1.o->value.d; |
1367 | 9.84k | break; |
1368 | | |
1369 | 7.08k | case OBJECT_TYPE_STRING: |
1370 | 7.08k | if (r1.o->value.ss == NULL) |
1371 | 6.58k | r1.i = YR_UNDEFINED; |
1372 | 498 | else |
1373 | 498 | r1.ss = r1.o->value.ss; |
1374 | 7.08k | break; |
1375 | | |
1376 | 0 | default: |
1377 | 0 | assert(false); |
1378 | 27.8k | } |
1379 | | |
1380 | 27.8k | push(r1); |
1381 | 27.8k | break; |
1382 | | |
1383 | 24.9k | case OP_INDEX_ARRAY: |
1384 | 24.9k | YR_DEBUG_FPRINTF( |
1385 | 24.9k | 2, stderr, "- case OP_INDEX_ARRAY: // %s()\n", __FUNCTION__); |
1386 | 24.9k | pop(r1); // index |
1387 | 24.9k | pop(r2); // array |
1388 | | |
1389 | 24.9k | ensure_defined(r1); |
1390 | 24.9k | ensure_defined(r2); |
1391 | | |
1392 | 20.9k | assert(r2.o->type == OBJECT_TYPE_ARRAY); |
1393 | | |
1394 | 20.9k | #if YR_PARANOID_EXEC |
1395 | 20.9k | check_object_canary(r2.o); |
1396 | 20.9k | #endif |
1397 | | |
1398 | 20.9k | r1.o = yr_object_array_get_item(r2.o, 0, (int) r1.i); |
1399 | | |
1400 | 20.9k | if (r1.o == NULL) |
1401 | 16.3k | r1.i = YR_UNDEFINED; |
1402 | | |
1403 | 20.9k | push(r1); |
1404 | 20.9k | break; |
1405 | | |
1406 | 0 | case OP_LOOKUP_DICT: |
1407 | 0 | YR_DEBUG_FPRINTF( |
1408 | 0 | 2, stderr, "- case OP_LOOKUP_DICT: // %s()\n", __FUNCTION__); |
1409 | 0 | pop(r1); // key |
1410 | 0 | pop(r2); // dictionary |
1411 | |
|
1412 | 0 | ensure_defined(r1); |
1413 | 0 | ensure_defined(r2); |
1414 | |
|
1415 | 0 | assert(r2.o->type == OBJECT_TYPE_DICTIONARY); |
1416 | |
|
1417 | 0 | #if YR_PARANOID_EXEC |
1418 | 0 | check_object_canary(r2.o); |
1419 | 0 | #endif |
1420 | |
|
1421 | 0 | r1.o = yr_object_dict_get_item(r2.o, 0, r1.ss->c_string); |
1422 | |
|
1423 | 0 | if (r1.o == NULL) |
1424 | 0 | r1.i = YR_UNDEFINED; |
1425 | |
|
1426 | 0 | push(r1); |
1427 | 0 | break; |
1428 | | |
1429 | 22.7k | case OP_CALL: |
1430 | 22.7k | YR_DEBUG_FPRINTF(2, stderr, "- case OP_CALL: // %s()\n", __FUNCTION__); |
1431 | | |
1432 | 22.7k | args_fmt = yr_unaligned_char_ptr(ip); |
1433 | 22.7k | ip += sizeof(uint64_t); |
1434 | | |
1435 | 22.7k | int i = (int) strlen(args_fmt); |
1436 | 22.7k | count = 0; |
1437 | | |
1438 | 22.7k | #if YR_PARANOID_EXEC |
1439 | 22.7k | if (i > YR_MAX_FUNCTION_ARGS) |
1440 | 0 | { |
1441 | 0 | stop = true; |
1442 | 0 | result = ERROR_INTERNAL_FATAL_ERROR; |
1443 | 0 | break; |
1444 | 0 | } |
1445 | 22.7k | #endif |
1446 | | |
1447 | | // pop arguments from stack and copy them to args array |
1448 | | |
1449 | 64.7k | while (i > 0) |
1450 | 42.0k | { |
1451 | 42.0k | pop(r1); |
1452 | | |
1453 | 42.0k | if (is_undef(r1)) // count the number of undefined args |
1454 | 6.11k | count++; |
1455 | | |
1456 | 42.0k | args[i - 1] = r1; |
1457 | 42.0k | i--; |
1458 | 42.0k | } |
1459 | | |
1460 | 45.4k | pop(r2); |
1461 | 45.4k | ensure_defined(r2); |
1462 | | |
1463 | 22.7k | #if YR_PARANOID_EXEC |
1464 | 22.7k | check_object_canary(r2.o); |
1465 | 22.7k | #endif |
1466 | | |
1467 | 22.7k | if (count > 0) |
1468 | 6.11k | { |
1469 | | // If there are undefined args, result for function call |
1470 | | // is undefined as well. |
1471 | | |
1472 | 6.11k | r1.i = YR_UNDEFINED; |
1473 | 6.11k | push(r1); |
1474 | 6.11k | break; |
1475 | 6.11k | } |
1476 | | |
1477 | 16.6k | function = object_as_function(r2.o); |
1478 | 16.6k | result = ERROR_INTERNAL_FATAL_ERROR; |
1479 | | |
1480 | 16.6k | for (i = 0; i < YR_MAX_OVERLOADED_FUNCTIONS; i++) |
1481 | 16.6k | { |
1482 | 16.6k | if (function->prototypes[i].arguments_fmt == NULL) |
1483 | 0 | break; |
1484 | | |
1485 | 16.6k | if (strcmp(function->prototypes[i].arguments_fmt, args_fmt) == 0) |
1486 | 16.6k | { |
1487 | 16.6k | result = function->prototypes[i].code(args, context, function); |
1488 | 16.6k | break; |
1489 | 16.6k | } |
1490 | 16.6k | } |
1491 | | |
1492 | | // If i == YR_MAX_OVERLOADED_FUNCTIONS at this point no matching |
1493 | | // prototype was found, but this shouldn't happen. |
1494 | 16.6k | assert(i < YR_MAX_OVERLOADED_FUNCTIONS); |
1495 | | |
1496 | | // Make a copy of the returned object and push the copy into the stack, |
1497 | | // function->return_obj can't be pushed because it can change in |
1498 | | // subsequent calls to the same function. |
1499 | 16.6k | if (result == ERROR_SUCCESS) |
1500 | 16.6k | result = yr_object_copy(function->return_obj, &r1.o); |
1501 | | |
1502 | | // A pointer to the copied object is stored in a arena in order to |
1503 | | // free the object before exiting yr_execute_code, obj_count tracks |
1504 | | // the number of objects written. |
1505 | 16.6k | if (result == ERROR_SUCCESS) |
1506 | 16.6k | { |
1507 | 16.6k | result = yr_arena_write_data(obj_arena, 0, &r1.o, sizeof(r1.o), NULL); |
1508 | 16.6k | obj_count++; |
1509 | 16.6k | } |
1510 | 0 | else |
1511 | 0 | { |
1512 | 0 | r1.i = YR_UNDEFINED; |
1513 | 0 | } |
1514 | | |
1515 | 16.6k | stop = (result != ERROR_SUCCESS); |
1516 | 16.6k | push(r1); |
1517 | 16.6k | break; |
1518 | | |
1519 | 0 | case OP_FOUND: |
1520 | 0 | pop(r1); |
1521 | 0 | r2.i = context->matches[r1.s->idx].tail != NULL ? 1 : 0; |
1522 | 0 | YR_DEBUG_FPRINTF( |
1523 | 0 | 2, |
1524 | 0 | stderr, |
1525 | 0 | "- case OP_FOUND: r2.i=%" PRId64 " // %s()\n", |
1526 | 0 | r2.i, |
1527 | 0 | __FUNCTION__); |
1528 | 0 | push(r2); |
1529 | 0 | break; |
1530 | | |
1531 | 0 | case OP_FOUND_AT: |
1532 | 0 | YR_DEBUG_FPRINTF( |
1533 | 0 | 2, stderr, "- case OP_FOUND_AT: // %s()\n", __FUNCTION__); |
1534 | 0 | pop(r2); |
1535 | 0 | pop(r1); |
1536 | |
|
1537 | 0 | ensure_defined(r1); |
1538 | |
|
1539 | 0 | #if YR_PARANOID_EXEC |
1540 | 0 | ensure_within_rules_arena(r2.p); |
1541 | 0 | #endif |
1542 | |
|
1543 | 0 | match = context->matches[r2.s->idx].head; |
1544 | 0 | r3.i = false; |
1545 | |
|
1546 | 0 | while (match != NULL) |
1547 | 0 | { |
1548 | 0 | if (r1.i == match->base + match->offset) |
1549 | 0 | { |
1550 | 0 | r3.i = true; |
1551 | 0 | break; |
1552 | 0 | } |
1553 | | |
1554 | 0 | if (r1.i < match->base + match->offset) |
1555 | 0 | break; |
1556 | | |
1557 | 0 | match = match->next; |
1558 | 0 | } |
1559 | |
|
1560 | 0 | push(r3); |
1561 | 0 | break; |
1562 | | |
1563 | 0 | case OP_FOUND_IN: |
1564 | 0 | YR_DEBUG_FPRINTF( |
1565 | 0 | 2, stderr, "- case OP_FOUND_IN: // %s()\n", __FUNCTION__); |
1566 | 0 | pop(r3); |
1567 | 0 | pop(r2); |
1568 | 0 | pop(r1); |
1569 | |
|
1570 | 0 | ensure_defined(r1); |
1571 | 0 | ensure_defined(r2); |
1572 | |
|
1573 | 0 | #if YR_PARANOID_EXEC |
1574 | 0 | ensure_within_rules_arena(r3.p); |
1575 | 0 | #endif |
1576 | |
|
1577 | 0 | match = context->matches[r3.s->idx].head; |
1578 | 0 | r4.i = false; |
1579 | |
|
1580 | 0 | while (match != NULL && !r4.i) |
1581 | 0 | { |
1582 | 0 | if (match->base + match->offset >= r1.i && |
1583 | 0 | match->base + match->offset <= r2.i) |
1584 | 0 | { |
1585 | 0 | r4.i = true; |
1586 | 0 | } |
1587 | |
|
1588 | 0 | if (match->base + match->offset > r2.i) |
1589 | 0 | break; |
1590 | | |
1591 | 0 | match = match->next; |
1592 | 0 | } |
1593 | |
|
1594 | 0 | push(r4); |
1595 | 0 | break; |
1596 | | |
1597 | 0 | case OP_COUNT: |
1598 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_COUNT: // %s()\n", __FUNCTION__); |
1599 | 0 | pop(r1); |
1600 | |
|
1601 | 0 | #if YR_PARANOID_EXEC |
1602 | 0 | ensure_within_rules_arena(r1.p); |
1603 | 0 | #endif |
1604 | |
|
1605 | 0 | r2.i = context->matches[r1.s->idx].count; |
1606 | 0 | push(r2); |
1607 | 0 | break; |
1608 | | |
1609 | 0 | case OP_COUNT_IN: |
1610 | 0 | YR_DEBUG_FPRINTF( |
1611 | 0 | 2, stderr, "- case OP_COUNT_IN: // %s()\n", __FUNCTION__); |
1612 | 0 | pop(r3); |
1613 | 0 | pop(r2); |
1614 | 0 | pop(r1); |
1615 | |
|
1616 | 0 | ensure_defined(r1); |
1617 | 0 | ensure_defined(r2); |
1618 | |
|
1619 | 0 | #if YR_PARANOID_EXEC |
1620 | 0 | ensure_within_rules_arena(r3.p); |
1621 | 0 | #endif |
1622 | |
|
1623 | 0 | match = context->matches[r3.s->idx].head; |
1624 | 0 | r4.i = 0; |
1625 | |
|
1626 | 0 | while (match != NULL) |
1627 | 0 | { |
1628 | 0 | if (match->base + match->offset >= r1.i && |
1629 | 0 | match->base + match->offset <= r2.i) |
1630 | 0 | { |
1631 | 0 | r4.i++; |
1632 | 0 | } |
1633 | |
|
1634 | 0 | if (match->base + match->offset > r2.i) |
1635 | 0 | break; |
1636 | | |
1637 | 0 | match = match->next; |
1638 | 0 | } |
1639 | |
|
1640 | 0 | push(r4); |
1641 | 0 | break; |
1642 | | |
1643 | 0 | case OP_OFFSET: |
1644 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_OFFSET: // %s()\n", __FUNCTION__); |
1645 | 0 | pop(r2); |
1646 | 0 | pop(r1); |
1647 | |
|
1648 | 0 | ensure_defined(r1); |
1649 | |
|
1650 | 0 | #if YR_PARANOID_EXEC |
1651 | 0 | ensure_within_rules_arena(r2.p); |
1652 | 0 | #endif |
1653 | |
|
1654 | 0 | match = context->matches[r2.s->idx].head; |
1655 | |
|
1656 | 0 | i = 1; |
1657 | 0 | r3.i = YR_UNDEFINED; |
1658 | |
|
1659 | 0 | while (match != NULL && r3.i == YR_UNDEFINED) |
1660 | 0 | { |
1661 | 0 | if (r1.i == i) |
1662 | 0 | r3.i = match->base + match->offset; |
1663 | |
|
1664 | 0 | i++; |
1665 | 0 | match = match->next; |
1666 | 0 | } |
1667 | |
|
1668 | 0 | push(r3); |
1669 | 0 | break; |
1670 | | |
1671 | 0 | case OP_LENGTH: |
1672 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_LENGTH: // %s()\n", __FUNCTION__); |
1673 | 0 | pop(r2); |
1674 | 0 | pop(r1); |
1675 | |
|
1676 | 0 | ensure_defined(r1); |
1677 | |
|
1678 | 0 | #if YR_PARANOID_EXEC |
1679 | 0 | ensure_within_rules_arena(r2.p); |
1680 | 0 | #endif |
1681 | |
|
1682 | 0 | match = context->matches[r2.s->idx].head; |
1683 | |
|
1684 | 0 | i = 1; |
1685 | 0 | r3.i = YR_UNDEFINED; |
1686 | |
|
1687 | 0 | while (match != NULL && r3.i == YR_UNDEFINED) |
1688 | 0 | { |
1689 | 0 | if (r1.i == i) |
1690 | 0 | r3.i = match->match_length; |
1691 | |
|
1692 | 0 | i++; |
1693 | 0 | match = match->next; |
1694 | 0 | } |
1695 | |
|
1696 | 0 | push(r3); |
1697 | 0 | break; |
1698 | | |
1699 | 0 | case OP_OF: |
1700 | 0 | case OP_OF_PERCENT: |
1701 | 0 | r2.i = yr_unaligned_u64(ip); |
1702 | 0 | ip += sizeof(uint64_t); |
1703 | 0 | assert(r2.i == OF_STRING_SET || r2.i == OF_RULE_SET); |
1704 | 0 | found = 0; |
1705 | 0 | count = 0; |
1706 | 0 | pop(r1); |
1707 | |
|
1708 | 0 | while (!is_undef(r1)) |
1709 | 0 | { |
1710 | 0 | if (r2.i == OF_STRING_SET) |
1711 | 0 | { |
1712 | 0 | if (context->matches[r1.s->idx].tail != NULL) |
1713 | 0 | { |
1714 | 0 | found++; |
1715 | 0 | } |
1716 | 0 | } |
1717 | 0 | else |
1718 | 0 | { |
1719 | | // r1.i is 1 if the rule has already matched and zero otherwise. |
1720 | 0 | found += r1.i; |
1721 | 0 | } |
1722 | 0 | count++; |
1723 | 0 | pop(r1); |
1724 | 0 | } |
1725 | |
|
1726 | 0 | pop(r2); |
1727 | |
|
1728 | 0 | if (opcode == OP_OF) |
1729 | 0 | { |
1730 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_OF: // %s()\n", __FUNCTION__); |
1731 | | |
1732 | | // Quantifier is "all" |
1733 | 0 | if (is_undef(r2)) |
1734 | 0 | { |
1735 | 0 | r1.i = found >= count ? 1 : 0; |
1736 | 0 | } |
1737 | | // Quantifier is 0 or none. This is a special case in which we want |
1738 | | // exactly 0 strings matching. More information at: |
1739 | | // https://github.com/VirusTotal/yara/issues/1695 |
1740 | 0 | else if (r2.i == 0) |
1741 | 0 | { |
1742 | 0 | r1.i = found == 0 ? 1 : 0; |
1743 | 0 | } |
1744 | | // In all other cases the number of strings matching should be at |
1745 | | // least the amount specified by the quantifier. |
1746 | 0 | else |
1747 | 0 | { |
1748 | 0 | r1.i = found >= r2.i ? 1 : 0; |
1749 | 0 | } |
1750 | 0 | } |
1751 | 0 | else // OP_OF_PERCENT |
1752 | 0 | { |
1753 | 0 | YR_DEBUG_FPRINTF( |
1754 | 0 | 2, stderr, "- case OP_OF_PERCENT: // %s()\n", __FUNCTION__); |
1755 | | |
1756 | | // If, by some weird reason, we manage to get an undefined string |
1757 | | // reference as the first thing on the stack then count would be zero. |
1758 | | // I don't know how this could ever happen but better to check for it. |
1759 | 0 | if (is_undef(r2) || count == 0) |
1760 | 0 | r1.i = YR_UNDEFINED; |
1761 | 0 | else |
1762 | 0 | r1.i = (((double) found / count) * 100) >= r2.i ? 1 : 0; |
1763 | 0 | } |
1764 | |
|
1765 | 0 | push(r1); |
1766 | 0 | break; |
1767 | | |
1768 | 0 | case OP_OF_FOUND_IN: |
1769 | 0 | YR_DEBUG_FPRINTF( |
1770 | 0 | 2, stderr, "- case OP_OF_FOUND_IN: // %s()\n", __FUNCTION__); |
1771 | |
|
1772 | 0 | found = 0; |
1773 | 0 | count = 0; |
1774 | |
|
1775 | 0 | pop(r2); // Offset range end |
1776 | 0 | pop(r1); // Offset range start |
1777 | 0 | pop(r3); // First string |
1778 | | |
1779 | | // If any of the range boundaries are undefined the result is also |
1780 | | // undefined, be we need to unwind the stack first. |
1781 | 0 | if (is_undef(r1) || is_undef(r2)) |
1782 | 0 | { |
1783 | | // Remove all the strings. |
1784 | 0 | while (!is_undef(r3)) pop(r3); |
1785 | | // Remove the quantifier at the bottom of the stack. |
1786 | 0 | pop(r3); |
1787 | 0 | r1.i = YR_UNDEFINED; |
1788 | 0 | push(r1); |
1789 | 0 | break; |
1790 | 0 | } |
1791 | | |
1792 | 0 | while (!is_undef(r3)) |
1793 | 0 | { |
1794 | 0 | #if YR_PARANOID_EXEC |
1795 | 0 | ensure_within_rules_arena(r3.p); |
1796 | 0 | #endif |
1797 | 0 | match = context->matches[r3.s->idx].head; |
1798 | |
|
1799 | 0 | while (match != NULL) |
1800 | 0 | { |
1801 | | // String match within range start and range end? |
1802 | 0 | if (match->base + match->offset >= r1.i && |
1803 | 0 | match->base + match->offset <= r2.i) |
1804 | 0 | { |
1805 | 0 | found++; |
1806 | 0 | break; |
1807 | 0 | } |
1808 | | |
1809 | | // If current match is past range end, we can stop as matches |
1810 | | // are sorted by offset in increasing order, so all remaining |
1811 | | // matches are part the range end too. |
1812 | 0 | if (match->base + match->offset > r1.i) |
1813 | 0 | break; |
1814 | | |
1815 | 0 | match = match->next; |
1816 | 0 | } |
1817 | |
|
1818 | 0 | count++; |
1819 | 0 | pop(r3); |
1820 | 0 | } |
1821 | | |
1822 | 0 | pop(r2); // Quantifier X in expressions like "X of string_set in range" |
1823 | | |
1824 | | // Quantifier is "all". |
1825 | 0 | if (is_undef(r2)) |
1826 | 0 | { |
1827 | 0 | r1.i = found >= count ? 1 : 0; |
1828 | 0 | } |
1829 | | // Quantifier is 0 or none. This is a special case in which we want |
1830 | | // exactly 0 strings matching. More information at: |
1831 | | // https://github.com/VirusTotal/yara/issues/1695 |
1832 | 0 | else if (r2.i == 0) |
1833 | 0 | { |
1834 | 0 | r1.i = found == 0 ? 1 : 0; |
1835 | 0 | } |
1836 | | // In all other cases the number of strings matching should be at least |
1837 | | // the amount specified by the quantifier. |
1838 | 0 | else |
1839 | 0 | { |
1840 | 0 | r1.i = found >= r2.i ? 1 : 0; |
1841 | 0 | } |
1842 | |
|
1843 | 0 | push(r1); |
1844 | 0 | break; |
1845 | | |
1846 | 0 | case OP_OF_FOUND_AT: |
1847 | 0 | YR_DEBUG_FPRINTF( |
1848 | 0 | 2, stderr, "- case OP_OF_FOUND_AT: // %s()\n", __FUNCTION__); |
1849 | |
|
1850 | 0 | found = 0; |
1851 | 0 | count = 0; |
1852 | |
|
1853 | 0 | pop(r2); // Match location |
1854 | 0 | pop(r1); // First string |
1855 | | |
1856 | | // Match location must be defined. |
1857 | 0 | if (is_undef(r2)) |
1858 | 0 | { |
1859 | | // Remove all the strings. |
1860 | 0 | while (!is_undef(r1)) pop(r1); |
1861 | | // Remove the quantifier at the bottom of the stack. |
1862 | 0 | pop(r1); |
1863 | 0 | r1.i = YR_UNDEFINED; |
1864 | 0 | push(r1); |
1865 | 0 | break; |
1866 | 0 | } |
1867 | | |
1868 | 0 | while (!is_undef(r1)) |
1869 | 0 | { |
1870 | 0 | #if YR_PARANOID_EXEC |
1871 | 0 | ensure_within_rules_arena(r1.p); |
1872 | 0 | #endif |
1873 | 0 | match = context->matches[r1.s->idx].head; |
1874 | |
|
1875 | 0 | while (match != NULL) |
1876 | 0 | { |
1877 | | // String match at the desired location? |
1878 | 0 | if (match->base + match->offset == r2.i) |
1879 | 0 | { |
1880 | 0 | found++; |
1881 | 0 | break; |
1882 | 0 | } |
1883 | | |
1884 | | // If current match is past desired location, we can stop as matches |
1885 | | // are sorted by offset in increasing order, so all remaining |
1886 | | // matches are past it. |
1887 | 0 | if (match->base + match->offset > r2.i) |
1888 | 0 | break; |
1889 | | |
1890 | 0 | match = match->next; |
1891 | 0 | } |
1892 | |
|
1893 | 0 | count++; |
1894 | 0 | pop(r1); |
1895 | 0 | } |
1896 | | |
1897 | 0 | pop(r2); // Quantifier X in expressions like "X of string_set in range" |
1898 | | |
1899 | | // Quantifier is "all". |
1900 | 0 | if (is_undef(r2)) |
1901 | 0 | { |
1902 | 0 | r1.i = found >= count ? 1 : 0; |
1903 | 0 | } |
1904 | | // Quantifier is 0 or none. This is a special case in which we want |
1905 | | // exactly 0 strings matching. More information at: |
1906 | | // https://github.com/VirusTotal/yara/issues/1695 |
1907 | 0 | else if (r2.i == 0) |
1908 | 0 | { |
1909 | 0 | r1.i = found == 0 ? 1 : 0; |
1910 | 0 | } |
1911 | | // In all other cases the number of strings matching should be at least |
1912 | | // the amount specified by the quantifier. |
1913 | 0 | else |
1914 | 0 | { |
1915 | 0 | r1.i = found >= r2.i ? 1 : 0; |
1916 | 0 | } |
1917 | |
|
1918 | 0 | push(r1); |
1919 | 0 | break; |
1920 | | |
1921 | 14.5k | case OP_FILESIZE: |
1922 | 14.5k | r1.i = context->file_size; |
1923 | 14.5k | YR_DEBUG_FPRINTF( |
1924 | 14.5k | 2, |
1925 | 14.5k | stderr, |
1926 | 14.5k | "- case OP_FILESIZE: r1.i=%" PRId64 "%s // %s()\n", |
1927 | 14.5k | r1.i, |
1928 | 14.5k | r1.i == YR_UNDEFINED ? " AKA YR_UNDEFINED" : "", |
1929 | 14.5k | __FUNCTION__); |
1930 | 14.5k | push(r1); |
1931 | 14.5k | break; |
1932 | | |
1933 | 0 | case OP_ENTRYPOINT: |
1934 | 0 | YR_DEBUG_FPRINTF( |
1935 | 0 | 2, stderr, "- case OP_ENTRYPOINT: // %s()\n", __FUNCTION__); |
1936 | 0 | r1.i = context->entry_point; |
1937 | 0 | push(r1); |
1938 | 0 | break; |
1939 | | |
1940 | 0 | case OP_INT8: |
1941 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT8: // %s()\n", __FUNCTION__); |
1942 | 0 | pop(r1); |
1943 | 0 | r1.i = read_int8_t_little_endian(context->iterator, (size_t) r1.i); |
1944 | 0 | push(r1); |
1945 | 0 | break; |
1946 | | |
1947 | 0 | case OP_INT16: |
1948 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT16: // %s()\n", __FUNCTION__); |
1949 | 0 | pop(r1); |
1950 | 0 | r1.i = read_int16_t_little_endian(context->iterator, (size_t) r1.i); |
1951 | 0 | push(r1); |
1952 | 0 | break; |
1953 | | |
1954 | 0 | case OP_INT32: |
1955 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT32: // %s()\n", __FUNCTION__); |
1956 | 0 | pop(r1); |
1957 | 0 | r1.i = read_int32_t_little_endian(context->iterator, (size_t) r1.i); |
1958 | 0 | push(r1); |
1959 | 0 | break; |
1960 | | |
1961 | 0 | case OP_UINT8: |
1962 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_UINT8: // %s()\n", __FUNCTION__); |
1963 | 0 | pop(r1); |
1964 | 0 | r1.i = read_uint8_t_little_endian(context->iterator, (size_t) r1.i); |
1965 | 0 | push(r1); |
1966 | 0 | break; |
1967 | | |
1968 | 0 | case OP_UINT16: |
1969 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_UINT16: // %s()\n", __FUNCTION__); |
1970 | 0 | pop(r1); |
1971 | 0 | r1.i = read_uint16_t_little_endian(context->iterator, (size_t) r1.i); |
1972 | 0 | push(r1); |
1973 | 0 | break; |
1974 | | |
1975 | 0 | case OP_UINT32: |
1976 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_UINT32: // %s()\n", __FUNCTION__); |
1977 | 0 | pop(r1); |
1978 | 0 | r1.i = read_uint32_t_little_endian(context->iterator, (size_t) r1.i); |
1979 | 0 | push(r1); |
1980 | 0 | break; |
1981 | | |
1982 | 0 | case OP_INT8BE: |
1983 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT8BE: // %s()\n", __FUNCTION__); |
1984 | 0 | pop(r1); |
1985 | 0 | r1.i = read_int8_t_big_endian(context->iterator, (size_t) r1.i); |
1986 | 0 | push(r1); |
1987 | 0 | break; |
1988 | | |
1989 | 0 | case OP_INT16BE: |
1990 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT16BE: // %s()\n", __FUNCTION__); |
1991 | 0 | pop(r1); |
1992 | 0 | r1.i = read_int16_t_big_endian(context->iterator, (size_t) r1.i); |
1993 | 0 | push(r1); |
1994 | 0 | break; |
1995 | | |
1996 | 0 | case OP_INT32BE: |
1997 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT32BE: // %s()\n", __FUNCTION__); |
1998 | 0 | pop(r1); |
1999 | 0 | r1.i = read_int32_t_big_endian(context->iterator, (size_t) r1.i); |
2000 | 0 | push(r1); |
2001 | 0 | break; |
2002 | | |
2003 | 0 | case OP_UINT8BE: |
2004 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_UINT8BE: // %s()\n", __FUNCTION__); |
2005 | 0 | pop(r1); |
2006 | 0 | r1.i = read_uint8_t_big_endian(context->iterator, (size_t) r1.i); |
2007 | 0 | push(r1); |
2008 | 0 | break; |
2009 | | |
2010 | 0 | case OP_UINT16BE: |
2011 | 0 | YR_DEBUG_FPRINTF( |
2012 | 0 | 2, stderr, "- case OP_UINT16BE: // %s()\n", __FUNCTION__); |
2013 | 0 | pop(r1); |
2014 | 0 | r1.i = read_uint16_t_big_endian(context->iterator, (size_t) r1.i); |
2015 | 0 | push(r1); |
2016 | 0 | break; |
2017 | | |
2018 | 0 | case OP_UINT32BE: |
2019 | 0 | YR_DEBUG_FPRINTF( |
2020 | 0 | 2, stderr, "- case OP_UINT32BE: // %s()\n", __FUNCTION__); |
2021 | 0 | pop(r1); |
2022 | 0 | r1.i = read_uint32_t_big_endian(context->iterator, (size_t) r1.i); |
2023 | 0 | push(r1); |
2024 | 0 | break; |
2025 | | |
2026 | 28.2k | case OP_IMPORT: |
2027 | 28.2k | YR_DEBUG_FPRINTF(2, stderr, "- case OP_IMPORT: // %s()\n", __FUNCTION__); |
2028 | 28.2k | r1.i = yr_unaligned_u64(ip); |
2029 | 28.2k | ip += sizeof(uint64_t); |
2030 | | |
2031 | 28.2k | #if YR_PARANOID_EXEC |
2032 | 28.2k | ensure_within_rules_arena(r1.p); |
2033 | 28.2k | #endif |
2034 | | |
2035 | 28.2k | result = yr_modules_load((char*) r1.p, context); |
2036 | | |
2037 | 28.2k | if (result != ERROR_SUCCESS) |
2038 | 0 | stop = true; |
2039 | | |
2040 | 28.2k | break; |
2041 | | |
2042 | 0 | case OP_MATCHES: |
2043 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_MATCHES: // %s()\n", __FUNCTION__); |
2044 | 0 | pop(r2); |
2045 | 0 | pop(r1); |
2046 | |
|
2047 | 0 | ensure_defined(r2); |
2048 | 0 | ensure_defined(r1); |
2049 | |
|
2050 | 0 | result = yr_re_exec( |
2051 | 0 | context, |
2052 | 0 | (uint8_t*) r2.re->code, |
2053 | 0 | (uint8_t*) r1.ss->c_string, |
2054 | 0 | r1.ss->length, |
2055 | 0 | 0, |
2056 | 0 | r2.re->flags | RE_FLAGS_SCAN, |
2057 | 0 | NULL, |
2058 | 0 | NULL, |
2059 | 0 | &found); |
2060 | |
|
2061 | 0 | if (result != ERROR_SUCCESS) |
2062 | 0 | stop = true; |
2063 | |
|
2064 | 0 | r1.i = found >= 0; |
2065 | 0 | push(r1); |
2066 | 0 | break; |
2067 | | |
2068 | 9.84k | case OP_INT_TO_DBL: |
2069 | 9.84k | YR_DEBUG_FPRINTF( |
2070 | 9.84k | 2, stderr, "- case OP_INT_TO_DBL: // %s()\n", __FUNCTION__); |
2071 | 9.84k | r1.i = yr_unaligned_u64(ip); |
2072 | 9.84k | ip += sizeof(uint64_t); |
2073 | | |
2074 | 9.84k | #if YR_PARANOID_EXEC |
2075 | 9.84k | if (r1.i > stack.sp || stack.sp - r1.i >= stack.capacity) |
2076 | 0 | { |
2077 | 0 | stop = true; |
2078 | 0 | result = ERROR_INTERNAL_FATAL_ERROR; |
2079 | 0 | break; |
2080 | 0 | } |
2081 | 9.84k | #endif |
2082 | | |
2083 | 9.84k | r2 = stack.items[stack.sp - r1.i]; |
2084 | | |
2085 | 9.84k | if (is_undef(r2)) |
2086 | 0 | stack.items[stack.sp - r1.i].i = YR_UNDEFINED; |
2087 | 9.84k | else |
2088 | 9.84k | stack.items[stack.sp - r1.i].d = (double) r2.i; |
2089 | 9.84k | break; |
2090 | | |
2091 | 0 | case OP_STR_TO_BOOL: |
2092 | 0 | YR_DEBUG_FPRINTF( |
2093 | 0 | 2, stderr, "- case OP_STR_TO_BOOL: // %s()\n", __FUNCTION__); |
2094 | 0 | pop(r1); |
2095 | 0 | ensure_defined(r1); |
2096 | 0 | r1.i = r1.ss->length > 0; |
2097 | 0 | push(r1); |
2098 | 0 | break; |
2099 | | |
2100 | 8.20k | case OP_INT_EQ: |
2101 | 8.20k | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT_EQ: // %s()\n", __FUNCTION__); |
2102 | 8.20k | pop(r2); |
2103 | 8.20k | pop(r1); |
2104 | 8.20k | ensure_defined(r2); |
2105 | 2.09k | ensure_defined(r1); |
2106 | 1.48k | r1.i = r1.i == r2.i; |
2107 | 1.48k | push(r1); |
2108 | 1.48k | break; |
2109 | | |
2110 | 0 | case OP_INT_NEQ: |
2111 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT_NEQ: // %s()\n", __FUNCTION__); |
2112 | 0 | pop(r2); |
2113 | 0 | pop(r1); |
2114 | 0 | ensure_defined(r2); |
2115 | 0 | ensure_defined(r1); |
2116 | 0 | r1.i = r1.i != r2.i; |
2117 | 0 | push(r1); |
2118 | 0 | break; |
2119 | | |
2120 | 0 | case OP_INT_LT: |
2121 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT_LT: // %s()\n", __FUNCTION__); |
2122 | 0 | pop(r2); |
2123 | 0 | pop(r1); |
2124 | 0 | ensure_defined(r2); |
2125 | 0 | ensure_defined(r1); |
2126 | 0 | r1.i = r1.i < r2.i; |
2127 | 0 | push(r1); |
2128 | 0 | break; |
2129 | | |
2130 | 0 | case OP_INT_GT: |
2131 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT_GT: // %s()\n", __FUNCTION__); |
2132 | 0 | pop(r2); |
2133 | 0 | pop(r1); |
2134 | 0 | ensure_defined(r2); |
2135 | 0 | ensure_defined(r1); |
2136 | 0 | r1.i = r1.i > r2.i; |
2137 | 0 | push(r1); |
2138 | 0 | break; |
2139 | | |
2140 | 0 | case OP_INT_LE: |
2141 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT_LE: // %s()\n", __FUNCTION__); |
2142 | 0 | pop(r2); |
2143 | 0 | pop(r1); |
2144 | 0 | ensure_defined(r2); |
2145 | 0 | ensure_defined(r1); |
2146 | 0 | r1.i = r1.i <= r2.i; |
2147 | 0 | push(r1); |
2148 | 0 | break; |
2149 | | |
2150 | 4.67k | case OP_INT_GE: |
2151 | 4.67k | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT_GE: // %s()\n", __FUNCTION__); |
2152 | 4.67k | pop(r2); |
2153 | 4.67k | pop(r1); |
2154 | 4.67k | ensure_defined(r2); |
2155 | 4.67k | ensure_defined(r1); |
2156 | 4.67k | r1.i = r1.i >= r2.i; |
2157 | 4.67k | push(r1); |
2158 | 4.67k | break; |
2159 | | |
2160 | 0 | case OP_INT_ADD: |
2161 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT_ADD: // %s()\n", __FUNCTION__); |
2162 | 0 | pop(r2); |
2163 | 0 | pop(r1); |
2164 | 0 | ensure_defined(r2); |
2165 | 0 | ensure_defined(r1); |
2166 | 0 | r1.i = r1.i + r2.i; |
2167 | 0 | push(r1); |
2168 | 0 | break; |
2169 | | |
2170 | 0 | case OP_INT_SUB: |
2171 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT_SUB: // %s()\n", __FUNCTION__); |
2172 | 0 | pop(r2); |
2173 | 0 | pop(r1); |
2174 | 0 | ensure_defined(r2); |
2175 | 0 | ensure_defined(r1); |
2176 | 0 | r1.i = r1.i - r2.i; |
2177 | 0 | push(r1); |
2178 | 0 | break; |
2179 | | |
2180 | 0 | case OP_INT_MUL: |
2181 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT_MUL: // %s()\n", __FUNCTION__); |
2182 | 0 | pop(r2); |
2183 | 0 | pop(r1); |
2184 | 0 | ensure_defined(r2); |
2185 | 0 | ensure_defined(r1); |
2186 | 0 | r1.i = r1.i * r2.i; |
2187 | 0 | push(r1); |
2188 | 0 | break; |
2189 | | |
2190 | 0 | case OP_INT_DIV: |
2191 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_INT_DIV: // %s()\n", __FUNCTION__); |
2192 | 0 | pop(r2); |
2193 | 0 | pop(r1); |
2194 | 0 | ensure_defined(r2); |
2195 | 0 | ensure_defined(r1); |
2196 | | // If divisor is zero the result is undefined. It's also undefined |
2197 | | // when dividing INT64_MIN by -1. |
2198 | 0 | if (r2.i == 0 || (r1.i == INT64_MIN && r2.i == -1)) |
2199 | 0 | r1.i = YR_UNDEFINED; |
2200 | 0 | else |
2201 | 0 | r1.i = r1.i / r2.i; |
2202 | 0 | push(r1); |
2203 | 0 | break; |
2204 | | |
2205 | 0 | case OP_INT_MINUS: |
2206 | 0 | YR_DEBUG_FPRINTF( |
2207 | 0 | 2, stderr, "- case OP_INT_MINUS: // %s()\n", __FUNCTION__); |
2208 | 0 | pop(r1); |
2209 | 0 | ensure_defined(r1); |
2210 | 0 | r1.i = -r1.i; |
2211 | 0 | push(r1); |
2212 | 0 | break; |
2213 | | |
2214 | 0 | case OP_DBL_LT: |
2215 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_DBL_LT: // %s()\n", __FUNCTION__); |
2216 | 0 | pop(r2); |
2217 | 0 | pop(r1); |
2218 | 0 | if (is_undef(r1) || is_undef(r2)) |
2219 | 0 | r1.i = false; |
2220 | 0 | else |
2221 | 0 | r1.i = r1.d < r2.d; |
2222 | 0 | push(r1); |
2223 | 0 | break; |
2224 | | |
2225 | 0 | case OP_DBL_GT: |
2226 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_DBL_GT: // %s()\n", __FUNCTION__); |
2227 | 0 | pop(r2); |
2228 | 0 | pop(r1); |
2229 | 0 | ensure_defined(r2); |
2230 | 0 | ensure_defined(r1); |
2231 | 0 | r1.i = r1.d > r2.d; |
2232 | 0 | push(r1); |
2233 | 0 | break; |
2234 | | |
2235 | 0 | case OP_DBL_LE: |
2236 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_DBL_LE: // %s()\n", __FUNCTION__); |
2237 | 0 | pop(r2); |
2238 | 0 | pop(r1); |
2239 | 0 | ensure_defined(r2); |
2240 | 0 | ensure_defined(r1); |
2241 | 0 | r1.i = r1.d <= r2.d; |
2242 | 0 | push(r1); |
2243 | 0 | break; |
2244 | | |
2245 | 9.84k | case OP_DBL_GE: |
2246 | 9.84k | YR_DEBUG_FPRINTF(2, stderr, "- case OP_DBL_GE: // %s()\n", __FUNCTION__); |
2247 | 9.84k | pop(r2); |
2248 | 9.84k | pop(r1); |
2249 | 9.84k | ensure_defined(r2); |
2250 | 9.84k | ensure_defined(r1); |
2251 | 9.83k | r1.i = r1.d >= r2.d; |
2252 | 9.83k | push(r1); |
2253 | 9.83k | break; |
2254 | | |
2255 | 0 | case OP_DBL_EQ: |
2256 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_DBL_EQ: // %s()\n", __FUNCTION__); |
2257 | 0 | pop(r2); |
2258 | 0 | pop(r1); |
2259 | 0 | ensure_defined(r2); |
2260 | 0 | ensure_defined(r1); |
2261 | 0 | r1.i = fabs(r1.d - r2.d) < DBL_EPSILON; |
2262 | 0 | push(r1); |
2263 | 0 | break; |
2264 | | |
2265 | 0 | case OP_DBL_NEQ: |
2266 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_DBL_NEQ: // %s()\n", __FUNCTION__); |
2267 | 0 | pop(r2); |
2268 | 0 | pop(r1); |
2269 | 0 | ensure_defined(r2); |
2270 | 0 | ensure_defined(r1); |
2271 | 0 | r1.i = fabs(r1.d - r2.d) >= DBL_EPSILON; |
2272 | 0 | push(r1); |
2273 | 0 | break; |
2274 | | |
2275 | 0 | case OP_DBL_ADD: |
2276 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_DBL_ADD: // %s()\n", __FUNCTION__); |
2277 | 0 | pop(r2); |
2278 | 0 | pop(r1); |
2279 | 0 | ensure_defined(r2); |
2280 | 0 | ensure_defined(r1); |
2281 | 0 | r1.d = r1.d + r2.d; |
2282 | 0 | push(r1); |
2283 | 0 | break; |
2284 | | |
2285 | 0 | case OP_DBL_SUB: |
2286 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_DBL_SUB: // %s()\n", __FUNCTION__); |
2287 | 0 | pop(r2); |
2288 | 0 | pop(r1); |
2289 | 0 | ensure_defined(r2); |
2290 | 0 | ensure_defined(r1); |
2291 | 0 | r1.d = r1.d - r2.d; |
2292 | 0 | push(r1); |
2293 | 0 | break; |
2294 | | |
2295 | 0 | case OP_DBL_MUL: |
2296 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_DBL_MUL: // %s()\n", __FUNCTION__); |
2297 | 0 | pop(r2); |
2298 | 0 | pop(r1); |
2299 | 0 | ensure_defined(r2); |
2300 | 0 | ensure_defined(r1); |
2301 | 0 | r1.d = r1.d * r2.d; |
2302 | 0 | push(r1); |
2303 | 0 | break; |
2304 | | |
2305 | 0 | case OP_DBL_DIV: |
2306 | 0 | YR_DEBUG_FPRINTF(2, stderr, "- case OP_DBL_DIV: // %s()\n", __FUNCTION__); |
2307 | 0 | pop(r2); |
2308 | 0 | pop(r1); |
2309 | 0 | ensure_defined(r2); |
2310 | 0 | ensure_defined(r1); |
2311 | 0 | r1.d = r1.d / r2.d; |
2312 | 0 | push(r1); |
2313 | 0 | break; |
2314 | | |
2315 | 0 | case OP_DBL_MINUS: |
2316 | 0 | YR_DEBUG_FPRINTF( |
2317 | 0 | 2, stderr, "- case OP_DBL_MINUS: // %s()\n", __FUNCTION__); |
2318 | 0 | pop(r1); |
2319 | 0 | ensure_defined(r1); |
2320 | 0 | r1.d = -r1.d; |
2321 | 0 | push(r1); |
2322 | 0 | break; |
2323 | | |
2324 | 11.2k | case OP_STR_EQ: |
2325 | 11.2k | case OP_STR_NEQ: |
2326 | 11.2k | case OP_STR_LT: |
2327 | 11.2k | case OP_STR_LE: |
2328 | 11.2k | case OP_STR_GT: |
2329 | 11.2k | case OP_STR_GE: |
2330 | 11.2k | pop(r2); |
2331 | 11.2k | pop(r1); |
2332 | | |
2333 | 11.2k | ensure_defined(r2); |
2334 | 11.2k | ensure_defined(r1); |
2335 | | |
2336 | 498 | switch (opcode) |
2337 | 498 | { |
2338 | 498 | case OP_STR_EQ: |
2339 | 498 | YR_DEBUG_FPRINTF( |
2340 | 498 | 2, stderr, "- case OP_STR_EQ: // %s()\n", __FUNCTION__); |
2341 | 498 | r1.i = (ss_compare(r1.ss, r2.ss) == 0); |
2342 | 498 | break; |
2343 | 0 | case OP_STR_NEQ: |
2344 | 0 | YR_DEBUG_FPRINTF( |
2345 | 0 | 2, stderr, "- case OP_STR_NEQ: // %s()\n", __FUNCTION__); |
2346 | 0 | r1.i = (ss_compare(r1.ss, r2.ss) != 0); |
2347 | 0 | break; |
2348 | 0 | case OP_STR_LT: |
2349 | 0 | YR_DEBUG_FPRINTF( |
2350 | 0 | 2, stderr, "- case OP_STR_LT: // %s()\n", __FUNCTION__); |
2351 | 0 | r1.i = (ss_compare(r1.ss, r2.ss) < 0); |
2352 | 0 | break; |
2353 | 0 | case OP_STR_LE: |
2354 | 0 | YR_DEBUG_FPRINTF( |
2355 | 0 | 2, stderr, "- case OP_STR_LE: // %s()\n", __FUNCTION__); |
2356 | 0 | r1.i = (ss_compare(r1.ss, r2.ss) <= 0); |
2357 | 0 | break; |
2358 | 0 | case OP_STR_GT: |
2359 | 0 | YR_DEBUG_FPRINTF( |
2360 | 0 | 2, stderr, "- case OP_STR_GT: // %s()\n", __FUNCTION__); |
2361 | 0 | r1.i = (ss_compare(r1.ss, r2.ss) > 0); |
2362 | 0 | break; |
2363 | 0 | case OP_STR_GE: |
2364 | 0 | YR_DEBUG_FPRINTF( |
2365 | 0 | 2, stderr, "- case OP_STR_GE: // %s()\n", __FUNCTION__); |
2366 | 0 | r1.i = (ss_compare(r1.ss, r2.ss) >= 0); |
2367 | 0 | break; |
2368 | 498 | } |
2369 | | |
2370 | 498 | push(r1); |
2371 | 498 | break; |
2372 | | |
2373 | 0 | case OP_CONTAINS: |
2374 | 0 | case OP_ICONTAINS: |
2375 | 0 | case OP_STARTSWITH: |
2376 | 0 | case OP_ISTARTSWITH: |
2377 | 0 | case OP_ENDSWITH: |
2378 | 0 | case OP_IENDSWITH: |
2379 | 0 | case OP_IEQUALS: |
2380 | 0 | pop(r2); |
2381 | 0 | pop(r1); |
2382 | |
|
2383 | 0 | ensure_defined(r1); |
2384 | 0 | ensure_defined(r2); |
2385 | |
|
2386 | 0 | switch (opcode) |
2387 | 0 | { |
2388 | 0 | case OP_CONTAINS: |
2389 | 0 | YR_DEBUG_FPRINTF( |
2390 | 0 | 2, stderr, "- case OP_CONTAINS: // %s()\n", __FUNCTION__); |
2391 | 0 | r1.i = ss_contains(r1.ss, r2.ss); |
2392 | 0 | break; |
2393 | 0 | case OP_ICONTAINS: |
2394 | 0 | YR_DEBUG_FPRINTF( |
2395 | 0 | 2, stderr, "- case OP_ICONTAINS: // %s()\n", __FUNCTION__); |
2396 | 0 | r1.i = ss_icontains(r1.ss, r2.ss); |
2397 | 0 | break; |
2398 | 0 | case OP_STARTSWITH: |
2399 | 0 | YR_DEBUG_FPRINTF( |
2400 | 0 | 2, stderr, "- case OP_STARTSWITH: // %s()\n", __FUNCTION__); |
2401 | 0 | r1.i = ss_startswith(r1.ss, r2.ss); |
2402 | 0 | break; |
2403 | 0 | case OP_ISTARTSWITH: |
2404 | 0 | YR_DEBUG_FPRINTF( |
2405 | 0 | 2, stderr, "- case OP_ISTARTSWITH: // %s()\n", __FUNCTION__); |
2406 | 0 | r1.i = ss_istartswith(r1.ss, r2.ss); |
2407 | 0 | break; |
2408 | 0 | case OP_ENDSWITH: |
2409 | 0 | YR_DEBUG_FPRINTF( |
2410 | 0 | 2, stderr, "- case OP_ENDSWITH: // %s()\n", __FUNCTION__); |
2411 | 0 | r1.i = ss_endswith(r1.ss, r2.ss); |
2412 | 0 | break; |
2413 | 0 | case OP_IENDSWITH: |
2414 | 0 | YR_DEBUG_FPRINTF( |
2415 | 0 | 2, stderr, "- case OP_IENDSWITH: // %s()\n", __FUNCTION__); |
2416 | 0 | r1.i = ss_iendswith(r1.ss, r2.ss); |
2417 | 0 | break; |
2418 | 0 | case OP_IEQUALS: |
2419 | 0 | YR_DEBUG_FPRINTF( |
2420 | 0 | 2, stderr, "- case OP_IEQUALS: // %s()\n", __FUNCTION__); |
2421 | 0 | r1.i = ss_icompare(r1.ss, r2.ss) == 0; |
2422 | 0 | break; |
2423 | 0 | } |
2424 | | |
2425 | 0 | push(r1); |
2426 | 0 | break; |
2427 | | |
2428 | 0 | default: |
2429 | 0 | YR_DEBUG_FPRINTF( |
2430 | 0 | 2, stderr, "- case <unknown instruction>: // %s()\n", __FUNCTION__); |
2431 | | // Unknown instruction, this shouldn't happen. |
2432 | 0 | assert(false); |
2433 | 476k | } |
2434 | | |
2435 | | // Check for timeout every 100 instruction cycles. If timeout == 0 it means |
2436 | | // no timeout at all. |
2437 | | |
2438 | 476k | if (context->timeout > 0ULL && ++cycle == 100) |
2439 | 0 | { |
2440 | 0 | elapsed_time = yr_stopwatch_elapsed_ns(&context->stopwatch); |
2441 | |
|
2442 | 0 | if (elapsed_time > context->timeout) |
2443 | 0 | { |
2444 | | #ifdef YR_PROFILING_ENABLED |
2445 | | context->profiling_info[current_rule_idx].exec_time += |
2446 | | (elapsed_time - start_time); |
2447 | | #endif |
2448 | 0 | result = ERROR_SCAN_TIMEOUT; |
2449 | 0 | stop = true; |
2450 | 0 | } |
2451 | |
|
2452 | 0 | cycle = 0; |
2453 | 0 | } |
2454 | 476k | } |
2455 | | |
2456 | 28.2k | obj_ptr = yr_arena_get_ptr(obj_arena, 0, 0); |
2457 | | |
2458 | 44.8k | for (int i = 0; i < obj_count; i++) yr_object_destroy(obj_ptr[i]); |
2459 | | |
2460 | 28.2k | yr_arena_release(obj_arena); |
2461 | 28.2k | yr_notebook_destroy(it_notebook); |
2462 | 28.2k | yr_modules_unload_all(context); |
2463 | 28.2k | yr_free(stack.items); |
2464 | | |
2465 | 28.2k | YR_DEBUG_FPRINTF( |
2466 | 28.2k | 2, |
2467 | 28.2k | stderr, |
2468 | 28.2k | "} = %d AKA %s // %s()\n", |
2469 | 28.2k | result, |
2470 | 28.2k | yr_debug_error_as_string(result), |
2471 | 28.2k | __FUNCTION__); |
2472 | | |
2473 | 28.2k | return result; |
2474 | 28.2k | } |