Coverage Report

Created: 2025-12-09 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/yara/libyara/parser.c
Line
Count
Source
1
/*
2
Copyright (c) 2013. 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 <limits.h>
31
#include <stddef.h>
32
#include <string.h>
33
#include <yara/ahocorasick.h>
34
#include <yara/arena.h>
35
#include <yara/base64.h>
36
#include <yara/error.h>
37
#include <yara/exec.h>
38
#include <yara/integers.h>
39
#include <yara/mem.h>
40
#include <yara/modules.h>
41
#include <yara/object.h>
42
#include <yara/parser.h>
43
#include <yara/re.h>
44
#include <yara/strutils.h>
45
#include <yara/utils.h>
46
#include "yara/compiler.h"
47
#include "yara/types.h"
48
49
#define todigit(x)                                        \
50
  ((x) >= 'A' && (x) <= 'F') ? ((uint8_t) (x - 'A' + 10)) \
51
                             : ((uint8_t) (x - '0'))
52
53
int yr_parser_emit(
54
    yyscan_t yyscanner,
55
    uint8_t instruction,
56
    YR_ARENA_REF* instruction_ref)
57
99.9k
{
58
99.9k
  return yr_arena_write_data(
59
99.9k
      yyget_extra(yyscanner)->arena,
60
99.9k
      YR_CODE_SECTION,
61
99.9k
      &instruction,
62
99.9k
      sizeof(uint8_t),
63
99.9k
      instruction_ref);
64
99.9k
}
65
66
int yr_parser_emit_with_arg_double(
67
    yyscan_t yyscanner,
68
    uint8_t instruction,
69
    double argument,
70
    YR_ARENA_REF* instruction_ref,
71
    YR_ARENA_REF* argument_ref)
72
2.30k
{
73
2.30k
  int result = yr_arena_write_data(
74
2.30k
      yyget_extra(yyscanner)->arena,
75
2.30k
      YR_CODE_SECTION,
76
2.30k
      &instruction,
77
2.30k
      sizeof(uint8_t),
78
2.30k
      instruction_ref);
79
80
2.30k
  if (result == ERROR_SUCCESS)
81
2.30k
    result = yr_arena_write_data(
82
2.30k
        yyget_extra(yyscanner)->arena,
83
2.30k
        YR_CODE_SECTION,
84
2.30k
        &argument,
85
2.30k
        sizeof(double),
86
2.30k
        argument_ref);
87
88
2.30k
  return result;
89
2.30k
}
90
91
int yr_parser_emit_with_arg_int32(
92
    yyscan_t yyscanner,
93
    uint8_t instruction,
94
    int32_t argument,
95
    YR_ARENA_REF* instruction_ref,
96
    YR_ARENA_REF* argument_ref)
97
20.7k
{
98
20.7k
  int result = yr_arena_write_data(
99
20.7k
      yyget_extra(yyscanner)->arena,
100
20.7k
      YR_CODE_SECTION,
101
20.7k
      &instruction,
102
20.7k
      sizeof(uint8_t),
103
20.7k
      instruction_ref);
104
105
20.7k
  if (result == ERROR_SUCCESS)
106
20.7k
    result = yr_arena_write_data(
107
20.7k
        yyget_extra(yyscanner)->arena,
108
20.7k
        YR_CODE_SECTION,
109
20.7k
        &argument,
110
20.7k
        sizeof(int32_t),
111
20.7k
        argument_ref);
112
113
20.7k
  return result;
114
20.7k
}
115
116
int yr_parser_emit_with_arg(
117
    yyscan_t yyscanner,
118
    uint8_t instruction,
119
    int64_t argument,
120
    YR_ARENA_REF* instruction_ref,
121
    YR_ARENA_REF* argument_ref)
122
38.1k
{
123
38.1k
  int result = yr_arena_write_data(
124
38.1k
      yyget_extra(yyscanner)->arena,
125
38.1k
      YR_CODE_SECTION,
126
38.1k
      &instruction,
127
38.1k
      sizeof(uint8_t),
128
38.1k
      instruction_ref);
129
130
38.1k
  if (result == ERROR_SUCCESS)
131
38.1k
    result = yr_arena_write_data(
132
38.1k
        yyget_extra(yyscanner)->arena,
133
38.1k
        YR_CODE_SECTION,
134
38.1k
        &argument,
135
38.1k
        sizeof(int64_t),
136
38.1k
        argument_ref);
137
138
38.1k
  return result;
139
38.1k
}
140
141
int yr_parser_emit_with_arg_reloc(
142
    yyscan_t yyscanner,
143
    uint8_t instruction,
144
    void* argument,
145
    YR_ARENA_REF* instruction_ref,
146
    YR_ARENA_REF* argument_ref)
147
882k
{
148
882k
  YR_ARENA_REF ref = YR_ARENA_NULL_REF;
149
150
882k
  DECLARE_REFERENCE(void*, ptr) arg;
151
152
882k
  memset(&arg, 0, sizeof(arg));
153
882k
  arg.ptr = argument;
154
155
882k
  int result = yr_arena_write_data(
156
882k
      yyget_extra(yyscanner)->arena,
157
882k
      YR_CODE_SECTION,
158
882k
      &instruction,
159
882k
      sizeof(uint8_t),
160
882k
      instruction_ref);
161
162
882k
  if (result == ERROR_SUCCESS)
163
882k
    result = yr_arena_write_data(
164
882k
        yyget_extra(yyscanner)->arena,
165
882k
        YR_CODE_SECTION,
166
882k
        &arg,
167
882k
        sizeof(arg),
168
882k
        &ref);
169
170
882k
  if (result == ERROR_SUCCESS)
171
882k
    result = yr_arena_make_ptr_relocatable(
172
882k
        yyget_extra(yyscanner)->arena, YR_CODE_SECTION, ref.offset, EOL);
173
174
882k
  if (argument_ref != NULL)
175
0
    *argument_ref = ref;
176
177
882k
  return result;
178
882k
}
179
180
int yr_parser_emit_pushes_for_strings(
181
    yyscan_t yyscanner,
182
    const char* identifier,
183
    YR_STRING_SET* strings)
184
8.96k
{
185
8.96k
  YR_COMPILER* compiler = yyget_extra(yyscanner);
186
187
8.96k
  YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
188
8.96k
      compiler, compiler->current_rule_idx);
189
190
8.96k
  YR_STRING* string;
191
192
8.96k
  const char* string_identifier;
193
8.96k
  const char* target_identifier;
194
195
8.96k
  strings->count = 0;
196
8.96k
  strings->head = NULL;
197
8.96k
  YR_STRING_SET_ELEMENT** tail_ptr = &strings->head;
198
199
8.96k
  yr_rule_strings_foreach(current_rule, string)
200
882k
  {
201
    // Don't generate pushes for strings chained to another one, we are
202
    // only interested in non-chained strings or the head of the chain.
203
204
882k
    if (string->chained_to == NULL)
205
877k
    {
206
877k
      string_identifier = string->identifier;
207
877k
      target_identifier = identifier;
208
209
1.75M
      while (*target_identifier != '\0' && *string_identifier != '\0' &&
210
878k
             *target_identifier == *string_identifier)
211
877k
      {
212
877k
        target_identifier++;
213
877k
        string_identifier++;
214
877k
      }
215
216
877k
      if ((*target_identifier == '\0' && *string_identifier == '\0') ||
217
8.23k
          *target_identifier == '*')
218
871k
      {
219
871k
        yr_parser_emit_with_arg_reloc(yyscanner, OP_PUSH, string, NULL, NULL);
220
221
871k
        string->flags |= STRING_FLAGS_REFERENCED;
222
871k
        string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
223
871k
        strings->count++;
224
225
871k
        *tail_ptr = yr_malloc(sizeof(YR_STRING_SET_ELEMENT));
226
871k
        yr_arena_ptr_to_ref(compiler->arena, string, &((*tail_ptr)->element));
227
871k
        (*tail_ptr)->next = NULL;
228
871k
        tail_ptr = &(*tail_ptr)->next;
229
871k
      }
230
877k
    }
231
882k
  }
232
233
8.96k
  if (strings->count == 0)
234
37
  {
235
37
    yr_compiler_set_error_extra_info(
236
37
        compiler, identifier) return ERROR_UNDEFINED_STRING;
237
37
  }
238
239
8.92k
  return ERROR_SUCCESS;
240
8.96k
}
241
242
// Emit OP_PUSH_RULE instructions for all rules whose identifier has given
243
// prefix.
244
int yr_parser_emit_pushes_for_rules(
245
    yyscan_t yyscanner,
246
    const char* prefix,
247
    int* count)
248
796
{
249
796
  YR_COMPILER* compiler = yyget_extra(yyscanner);
250
251
  // Make sure the compiler is parsing a rule
252
796
  assert(compiler->current_rule_idx != UINT32_MAX);
253
254
796
  YR_RULE* rule;
255
796
  int matching = 0;
256
257
796
  YR_NAMESPACE* ns = (YR_NAMESPACE*) yr_arena_get_ptr(
258
796
      compiler->arena,
259
796
      YR_NAMESPACES_TABLE,
260
796
      compiler->current_namespace_idx * sizeof(struct YR_NAMESPACE));
261
262
  // Can't use yr_rules_foreach here as that requires the rules to have been
263
  // finalized (inserting a NULL rule at the end). This is done when
264
  // yr_compiler_get_rules() is called, which also inserts a HALT instruction
265
  // into the current position in the code arena. Obviously we aren't done
266
  // compiling the rules yet so inserting a HALT is a bad idea. To deal with
267
  // this I'm manually walking all the currently compiled rules (up to the
268
  // current rule index) and comparing identifiers to see if it is one we should
269
  // use.
270
  //
271
  // Further, we have to get compiler->current_rule_idx before we start because
272
  // if we emit an OP_PUSH_RULE
273
796
  rule = yr_arena_get_ptr(compiler->arena, YR_RULES_TABLE, 0);
274
275
4.22k
  for (uint32_t i = 0; i <= compiler->current_rule_idx; i++)
276
3.43k
  {
277
    // Is rule->identifier prefixed by prefix?
278
3.43k
    if (strncmp(prefix, rule->identifier, strlen(prefix)) == 0)
279
821
    {
280
821
      uint32_t rule_idx = yr_hash_table_lookup_uint32(
281
821
          compiler->rules_table, rule->identifier, ns->name);
282
283
821
      if (rule_idx != UINT32_MAX)
284
821
      {
285
821
        FAIL_ON_ERROR(yr_parser_emit_with_arg(
286
821
            yyscanner, OP_PUSH_RULE, rule_idx, NULL, NULL));
287
821
        matching++;
288
821
      }
289
821
    }
290
291
3.43k
    rule++;
292
3.43k
  }
293
294
796
  if (count != NULL)
295
796
  {
296
796
    *count = matching;
297
796
  }
298
299
796
  if (matching == 0)
300
112
  {
301
112
    yr_compiler_set_error_extra_info(compiler, prefix);
302
112
    return ERROR_UNDEFINED_IDENTIFIER;
303
112
  }
304
305
684
  return ERROR_SUCCESS;
306
796
}
307
308
int yr_parser_emit_push_const(yyscan_t yyscanner, uint64_t argument)
309
55.7k
{
310
55.7k
  uint8_t opcode[9];
311
55.7k
  int opcode_len = 1;
312
313
55.7k
  if (argument == YR_UNDEFINED)
314
10.6k
  {
315
10.6k
    opcode[0] = OP_PUSH_U;
316
10.6k
  }
317
45.1k
  else if (argument <= 0xff)
318
41.4k
  {
319
41.4k
    opcode[0] = OP_PUSH_8;
320
41.4k
    opcode[1] = (uint8_t) argument;
321
41.4k
    opcode_len += sizeof(uint8_t);
322
41.4k
  }
323
3.71k
  else if (argument <= 0xffff)
324
1.88k
  {
325
1.88k
    opcode[0] = OP_PUSH_16;
326
1.88k
    uint16_t u = (uint16_t) argument;
327
1.88k
    memcpy(opcode + 1, &u, sizeof(uint16_t));
328
1.88k
    opcode_len += sizeof(uint16_t);
329
1.88k
  }
330
1.83k
  else if (argument <= 0xffffffff)
331
1.02k
  {
332
1.02k
    opcode[0] = OP_PUSH_32;
333
1.02k
    uint32_t u = (uint32_t) argument;
334
1.02k
    memcpy(opcode + 1, &u, sizeof(uint32_t));
335
1.02k
    opcode_len += sizeof(uint32_t);
336
1.02k
  }
337
811
  else
338
811
  {
339
811
    opcode[0] = OP_PUSH;
340
811
    memcpy(opcode + 1, &argument, sizeof(uint64_t));
341
811
    opcode_len += sizeof(uint64_t);
342
811
  }
343
344
55.7k
  return yr_arena_write_data(
345
55.7k
      yyget_extra(yyscanner)->arena, YR_CODE_SECTION, opcode, opcode_len, NULL);
346
55.7k
}
347
348
int yr_parser_check_types(
349
    YR_COMPILER* compiler,
350
    YR_OBJECT_FUNCTION* function,
351
    const char* actual_args_fmt)
352
419
{
353
419
  int i;
354
355
572
  for (i = 0; i < YR_MAX_OVERLOADED_FUNCTIONS; i++)
356
572
  {
357
572
    if (function->prototypes[i].arguments_fmt == NULL)
358
12
      break;
359
360
560
    if (strcmp(function->prototypes[i].arguments_fmt, actual_args_fmt) == 0)
361
407
      return ERROR_SUCCESS;
362
560
  }
363
364
12
  yr_compiler_set_error_extra_info(compiler, function->identifier)
365
366
12
      return ERROR_WRONG_ARGUMENTS;
367
419
}
368
369
int yr_parser_lookup_string(
370
    yyscan_t yyscanner,
371
    const char* identifier,
372
    YR_STRING** string)
373
3.05k
{
374
3.05k
  YR_COMPILER* compiler = yyget_extra(yyscanner);
375
376
3.05k
  YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
377
3.05k
      compiler, compiler->current_rule_idx);
378
379
3.05k
  yr_rule_strings_foreach(current_rule, *string)
380
3.43k
  {
381
    // If some string $a gets fragmented into multiple chained
382
    // strings, all those fragments have the same $a identifier
383
    // but we are interested in the heading fragment, which is
384
    // that with chained_to == NULL
385
386
3.43k
    if ((*string)->chained_to == NULL &&
387
3.27k
        strcmp((*string)->identifier, identifier) == 0)
388
3.02k
    {
389
3.02k
      return ERROR_SUCCESS;
390
3.02k
    }
391
3.43k
  }
392
393
36
  yr_compiler_set_error_extra_info(compiler, identifier)
394
395
36
      * string = NULL;
396
397
36
  return ERROR_UNDEFINED_STRING;
398
3.05k
}
399
400
////////////////////////////////////////////////////////////////////////////////
401
// Searches for a variable with the given identifier in the scope of the current
402
// "for" loop. In case of nested "for" loops the identifier is searched starting
403
// at the top-level loop and going down thorough the nested loops until the
404
// current one. This is ok because inner loops can not re-define an identifier
405
// already defined by an outer loop.
406
//
407
// If the variable is found, the return value is the position that the variable
408
// occupies among all the currently defined variables. If the variable doesn't
409
// exist the return value is -1.
410
//
411
// The function can receive a pointer to a YR_EXPRESSION that will populated
412
// with information about the variable if found. This pointer can be NULL if
413
// the caller is not interested in getting that information.
414
//
415
int yr_parser_lookup_loop_variable(
416
    yyscan_t yyscanner,
417
    const char* identifier,
418
    YR_EXPRESSION* expr)
419
16.1k
{
420
16.1k
  YR_COMPILER* compiler = yyget_extra(yyscanner);
421
16.1k
  int i, j;
422
16.1k
  int var_offset = 0;
423
424
20.0k
  for (i = 0; i <= compiler->loop_index; i++)
425
13.2k
  {
426
13.2k
    var_offset += compiler->loop[i].vars_internal_count;
427
428
21.9k
    for (j = 0; j < compiler->loop[i].vars_count; j++)
429
18.1k
    {
430
18.1k
      if (compiler->loop[i].vars[j].identifier.ptr != NULL &&
431
17.7k
          strcmp(identifier, compiler->loop[i].vars[j].identifier.ptr) == 0)
432
9.41k
      {
433
9.41k
        if (expr != NULL)
434
9.38k
          *expr = compiler->loop[i].vars[j];
435
436
9.41k
        return var_offset + j;
437
9.41k
      }
438
18.1k
    }
439
440
3.81k
    var_offset += compiler->loop[i].vars_count;
441
3.81k
  }
442
443
6.78k
  return -1;
444
16.1k
}
445
446
static int _yr_parser_write_string(
447
    const char* identifier,
448
    YR_MODIFIER modifier,
449
    YR_COMPILER* compiler,
450
    SIZED_STRING* str,
451
    RE_AST* re_ast,
452
    YR_ARENA_REF* string_ref,
453
    int* min_atom_quality,
454
    int* num_atom)
455
25.0k
{
456
25.0k
  SIZED_STRING* literal_string;
457
25.0k
  YR_ATOM_LIST_ITEM* atom;
458
25.0k
  YR_ATOM_LIST_ITEM* atom_list = NULL;
459
460
25.0k
  int c, result;
461
25.0k
  int max_string_len;
462
25.0k
  bool free_literal = false;
463
464
25.0k
  FAIL_ON_ERROR(yr_arena_allocate_struct(
465
25.0k
      compiler->arena,
466
25.0k
      YR_STRINGS_TABLE,
467
25.0k
      sizeof(YR_STRING),
468
25.0k
      string_ref,
469
25.0k
      offsetof(YR_STRING, identifier),
470
25.0k
      offsetof(YR_STRING, string),
471
25.0k
      offsetof(YR_STRING, chained_to),
472
25.0k
      EOL));
473
474
25.0k
  YR_STRING* string = (YR_STRING*) yr_arena_ref_to_ptr(
475
25.0k
      compiler->arena, string_ref);
476
477
25.0k
  YR_ARENA_REF ref;
478
479
25.0k
  FAIL_ON_ERROR(_yr_compiler_store_string(compiler, identifier, &ref));
480
481
25.0k
  string->identifier = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref);
482
25.0k
  string->rule_idx = compiler->current_rule_idx;
483
25.0k
  string->idx = compiler->current_string_idx;
484
25.0k
  string->fixed_offset = YR_UNDEFINED;
485
486
25.0k
  compiler->current_string_idx++;
487
488
25.0k
  if (modifier.flags & STRING_FLAGS_HEXADECIMAL ||
489
22.9k
      modifier.flags & STRING_FLAGS_REGEXP ||
490
3.20k
      modifier.flags & STRING_FLAGS_BASE64 ||
491
2.27k
      modifier.flags & STRING_FLAGS_BASE64_WIDE)
492
23.3k
  {
493
23.3k
    literal_string = yr_re_ast_extract_literal(re_ast);
494
495
23.3k
    if (literal_string != NULL)
496
16.7k
      free_literal = true;
497
23.3k
  }
498
1.73k
  else
499
1.73k
  {
500
1.73k
    literal_string = str;
501
1.73k
  }
502
503
25.0k
  if (literal_string != NULL)
504
18.4k
  {
505
18.4k
    modifier.flags |= STRING_FLAGS_LITERAL;
506
507
18.4k
    result = _yr_compiler_store_data(
508
18.4k
        compiler,
509
18.4k
        literal_string->c_string,
510
18.4k
        literal_string->length + 1,  // +1 to include terminating NULL
511
18.4k
        &ref);
512
513
18.4k
    if (result != ERROR_SUCCESS)
514
0
      goto cleanup;
515
516
18.4k
    string->length = (uint32_t) literal_string->length;
517
18.4k
    string->string = (uint8_t*) yr_arena_ref_to_ptr(compiler->arena, &ref);
518
519
18.4k
    if (modifier.flags & STRING_FLAGS_WIDE)
520
1.58k
      max_string_len = string->length * 2;
521
16.8k
    else
522
16.8k
      max_string_len = string->length;
523
524
18.4k
    if (max_string_len <= YR_MAX_ATOM_LENGTH)
525
14.4k
      modifier.flags |= STRING_FLAGS_FITS_IN_ATOM;
526
527
18.4k
    result = yr_atoms_extract_from_string(
528
18.4k
        &compiler->atoms_config,
529
18.4k
        (uint8_t*) literal_string->c_string,
530
18.4k
        (int32_t) literal_string->length,
531
18.4k
        modifier,
532
18.4k
        &atom_list,
533
18.4k
        min_atom_quality);
534
535
18.4k
    if (result != ERROR_SUCCESS)
536
0
      goto cleanup;
537
18.4k
  }
538
6.59k
  else
539
6.59k
  {
540
    // Non-literal strings can't be marked as fixed offset because once we
541
    // find a string atom in the scanned data we don't know the offset where
542
    // the string should start, as the non-literal strings can contain
543
    // variable-length portions.
544
6.59k
    modifier.flags &= ~STRING_FLAGS_FIXED_OFFSET;
545
546
    // Save the position where the RE forward code starts for later reference.
547
6.59k
    yr_arena_off_t forward_code_start = yr_arena_get_current_offset(
548
6.59k
        compiler->arena, YR_RE_CODE_SECTION);
549
550
    // Emit forwards code
551
6.59k
    result = yr_re_ast_emit_code(re_ast, compiler->arena, false);
552
553
6.59k
    if (result != ERROR_SUCCESS)
554
131
      goto cleanup;
555
556
    // Emit backwards code
557
6.46k
    result = yr_re_ast_emit_code(re_ast, compiler->arena, true);
558
559
6.46k
    if (result != ERROR_SUCCESS)
560
8
      goto cleanup;
561
562
    // Extract atoms from the regular expression.
563
6.45k
    result = yr_atoms_extract_from_re(
564
6.45k
        &compiler->atoms_config,
565
6.45k
        re_ast,
566
6.45k
        modifier,
567
6.45k
        &atom_list,
568
6.45k
        min_atom_quality);
569
570
6.45k
    if (result != ERROR_SUCCESS)
571
0
      goto cleanup;
572
573
    // If no atom was extracted let's add a zero-length atom.
574
6.45k
    if (atom_list == NULL)
575
2.36k
    {
576
2.36k
      atom_list = (YR_ATOM_LIST_ITEM*) yr_malloc(sizeof(YR_ATOM_LIST_ITEM));
577
578
2.36k
      if (atom_list == NULL)
579
0
      {
580
0
        result = ERROR_INSUFFICIENT_MEMORY;
581
0
        goto cleanup;
582
0
      }
583
584
2.36k
      atom_list->atom.length = 0;
585
2.36k
      atom_list->backtrack = 0;
586
2.36k
      atom_list->backward_code_ref = YR_ARENA_NULL_REF;
587
2.36k
      atom_list->next = NULL;
588
589
2.36k
      yr_arena_ptr_to_ref(
590
2.36k
          compiler->arena,
591
2.36k
          yr_arena_get_ptr(
592
2.36k
              compiler->arena, YR_RE_CODE_SECTION, forward_code_start),
593
2.36k
          &(atom_list->forward_code_ref));
594
2.36k
    }
595
6.45k
  }
596
597
24.9k
  string->flags = modifier.flags;
598
599
  // Add the string to Aho-Corasick automaton.
600
24.9k
  result = yr_ac_add_string(
601
24.9k
      compiler->automaton, string, string->idx, atom_list, compiler->arena);
602
603
24.9k
  if (result != ERROR_SUCCESS)
604
0
    goto cleanup;
605
606
24.9k
  atom = atom_list;
607
24.9k
  c = 0;
608
609
1.87M
  while (atom != NULL)
610
1.84M
  {
611
1.84M
    atom = atom->next;
612
1.84M
    c++;
613
1.84M
  }
614
615
24.9k
  (*num_atom) += c;
616
617
25.0k
cleanup:
618
25.0k
  if (free_literal)
619
16.7k
    yr_free(literal_string);
620
621
25.0k
  if (atom_list != NULL)
622
24.9k
    yr_atoms_list_destroy(atom_list);
623
624
25.0k
  return result;
625
24.9k
}
626
627
static int _yr_parser_check_string_modifiers(
628
    yyscan_t yyscanner,
629
    YR_MODIFIER modifier)
630
20.4k
{
631
20.4k
  YR_COMPILER* compiler = yyget_extra(yyscanner);
632
633
  // xor and nocase together is not implemented.
634
20.4k
  if (modifier.flags & STRING_FLAGS_XOR &&
635
714
      modifier.flags & STRING_FLAGS_NO_CASE)
636
0
  {
637
0
    yr_compiler_set_error_extra_info(
638
0
        compiler, "invalid modifier combination: xor nocase");
639
0
    return ERROR_INVALID_MODIFIER;
640
0
  }
641
642
  // base64 and nocase together is not implemented.
643
20.4k
  if (modifier.flags & STRING_FLAGS_NO_CASE &&
644
3.24k
      (modifier.flags & STRING_FLAGS_BASE64 ||
645
3.23k
       modifier.flags & STRING_FLAGS_BASE64_WIDE))
646
4
  {
647
4
    yr_compiler_set_error_extra_info(
648
4
        compiler,
649
4
        modifier.flags & STRING_FLAGS_BASE64
650
4
            ? "invalid modifier combination: base64 nocase"
651
4
            : "invalid modifier combination: base64wide nocase");
652
4
    return ERROR_INVALID_MODIFIER;
653
4
  }
654
655
  // base64 and fullword together is not implemented.
656
20.4k
  if (modifier.flags & STRING_FLAGS_FULL_WORD &&
657
123
      (modifier.flags & STRING_FLAGS_BASE64 ||
658
123
       modifier.flags & STRING_FLAGS_BASE64_WIDE))
659
0
  {
660
0
    yr_compiler_set_error_extra_info(
661
0
        compiler,
662
0
        modifier.flags & STRING_FLAGS_BASE64
663
0
            ? "invalid modifier combination: base64 fullword"
664
0
            : "invalid modifier combination: base64wide fullword");
665
0
    return ERROR_INVALID_MODIFIER;
666
0
  }
667
668
  // base64 and xor together is not implemented.
669
20.4k
  if (modifier.flags & STRING_FLAGS_XOR &&
670
714
      (modifier.flags & STRING_FLAGS_BASE64 ||
671
714
       modifier.flags & STRING_FLAGS_BASE64_WIDE))
672
4
  {
673
4
    yr_compiler_set_error_extra_info(
674
4
        compiler,
675
4
        modifier.flags & STRING_FLAGS_BASE64
676
4
            ? "invalid modifier combination: base64 xor"
677
4
            : "invalid modifier combination: base64wide xor");
678
4
    return ERROR_INVALID_MODIFIER;
679
4
  }
680
681
20.4k
  return ERROR_SUCCESS;
682
20.4k
}
683
684
int yr_parser_reduce_string_declaration(
685
    yyscan_t yyscanner,
686
    YR_MODIFIER modifier,
687
    const char* identifier,
688
    SIZED_STRING* str,
689
    YR_ARENA_REF* string_ref)
690
20.4k
{
691
20.4k
  int result = ERROR_SUCCESS;
692
20.4k
  int min_atom_quality = YR_MAX_ATOM_QUALITY;
693
20.4k
  int atom_quality;
694
695
20.4k
  char message[512];
696
697
20.4k
  int32_t min_gap = 0;
698
20.4k
  int32_t max_gap = 0;
699
700
20.4k
  YR_COMPILER* compiler = yyget_extra(yyscanner);
701
702
20.4k
  RE_AST* re_ast = NULL;
703
20.4k
  RE_AST* remainder_re_ast = NULL;
704
20.4k
  RE_ERROR re_error;
705
706
20.4k
  YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
707
20.4k
      compiler, compiler->current_rule_idx);
708
709
  // Determine if a string with the same identifier was already defined
710
  // by searching for the identifier in strings_table.
711
20.4k
  uint32_t string_idx = yr_hash_table_lookup_uint32(
712
20.4k
      compiler->strings_table, identifier, NULL);
713
714
  // The string was already defined, return an error.
715
20.4k
  if (string_idx != UINT32_MAX)
716
15
  {
717
15
    yr_compiler_set_error_extra_info(compiler, identifier);
718
15
    return ERROR_DUPLICATED_STRING_IDENTIFIER;
719
15
  }
720
721
  // Empty strings are not allowed.
722
20.4k
  if (str->length == 0)
723
4
  {
724
4
    yr_compiler_set_error_extra_info(compiler, identifier);
725
4
    return ERROR_EMPTY_STRING;
726
4
  }
727
728
20.4k
  if (str->flags & SIZED_STRING_FLAGS_NO_CASE)
729
3.14k
    modifier.flags |= STRING_FLAGS_NO_CASE;
730
731
20.4k
  if (str->flags & SIZED_STRING_FLAGS_DOT_ALL)
732
120
    modifier.flags |= STRING_FLAGS_DOT_ALL;
733
734
  // Hex strings are always handled as DOT_ALL regexps.
735
20.4k
  if (modifier.flags & STRING_FLAGS_HEXADECIMAL)
736
955
    modifier.flags |= STRING_FLAGS_DOT_ALL;
737
738
20.4k
  if (!(modifier.flags & STRING_FLAGS_WIDE) &&
739
19.3k
      !(modifier.flags & STRING_FLAGS_BASE64 ||
740
18.5k
        modifier.flags & STRING_FLAGS_BASE64_WIDE))
741
18.1k
  {
742
18.1k
    modifier.flags |= STRING_FLAGS_ASCII;
743
18.1k
  }
744
745
  // The STRING_FLAGS_SINGLE_MATCH flag indicates that finding
746
  // a single match for the string is enough. This is true in
747
  // most cases, except when the string count (#) and string offset (@)
748
  // operators are used. All strings are marked STRING_FLAGS_SINGLE_MATCH
749
  // initially, and unmarked later if required.
750
20.4k
  modifier.flags |= STRING_FLAGS_SINGLE_MATCH;
751
752
  // The STRING_FLAGS_FIXED_OFFSET indicates that the string doesn't
753
  // need to be searched all over the file because the user is using the
754
  // "at" operator. The string must be searched at a fixed offset in the
755
  // file. All strings are marked STRING_FLAGS_FIXED_OFFSET initially,
756
  // and unmarked later if required.
757
20.4k
  modifier.flags |= STRING_FLAGS_FIXED_OFFSET;
758
759
  // If string identifier is $ this is an anonymous string, if not add the
760
  // identifier to strings_table.
761
20.4k
  if (strcmp(identifier, "$") == 0)
762
19.2k
  {
763
19.2k
    modifier.flags |= STRING_FLAGS_ANONYMOUS;
764
19.2k
  }
765
1.15k
  else
766
1.15k
  {
767
1.15k
    FAIL_ON_ERROR(yr_hash_table_add_uint32(
768
1.15k
        compiler->strings_table,
769
1.15k
        identifier,
770
1.15k
        NULL,
771
1.15k
        compiler->current_string_idx));
772
1.15k
  }
773
774
  // Make sure that the the string does not have an invalid combination of
775
  // modifiers.
776
20.4k
  FAIL_ON_ERROR(_yr_parser_check_string_modifiers(yyscanner, modifier));
777
778
20.4k
  if (modifier.flags & STRING_FLAGS_HEXADECIMAL ||
779
19.4k
      modifier.flags & STRING_FLAGS_REGEXP ||
780
3.20k
      modifier.flags & STRING_FLAGS_BASE64 ||
781
2.27k
      modifier.flags & STRING_FLAGS_BASE64_WIDE)
782
18.6k
  {
783
18.6k
    if (modifier.flags & STRING_FLAGS_HEXADECIMAL)
784
955
      result = yr_re_parse_hex(str->c_string, &re_ast, &re_error);
785
17.7k
    else if (modifier.flags & STRING_FLAGS_REGEXP)
786
16.2k
    {
787
16.2k
      int flags = RE_PARSER_FLAG_NONE;
788
16.2k
      if (compiler->strict_escape)
789
0
        flags |= RE_PARSER_FLAG_ENABLE_STRICT_ESCAPE_SEQUENCES;
790
16.2k
      result = yr_re_parse(str->c_string, &re_ast, &re_error, flags);
791
16.2k
    }
792
1.46k
    else
793
1.46k
      result = yr_base64_ast_from_string(str, modifier, &re_ast, &re_error);
794
795
18.6k
    if (result != ERROR_SUCCESS)
796
806
    {
797
806
      if (result == ERROR_UNKNOWN_ESCAPE_SEQUENCE)
798
0
      {
799
0
        yywarning(yyscanner, "unknown escape sequence");
800
0
      }
801
806
      else
802
806
      {
803
806
        snprintf(
804
806
            message,
805
806
            sizeof(message),
806
806
            "invalid %s \"%s\": %s",
807
806
            (modifier.flags & STRING_FLAGS_HEXADECIMAL) ? "hex string"
808
806
                                                        : "regular expression",
809
806
            identifier,
810
806
            re_error.message);
811
812
806
        yr_compiler_set_error_extra_info(compiler, message);
813
806
        goto _exit;
814
806
      }
815
806
    }
816
817
17.8k
    if (re_ast->flags & RE_FLAGS_FAST_REGEXP)
818
567
      modifier.flags |= STRING_FLAGS_FAST_REGEXP;
819
820
17.8k
    if (re_ast->flags & RE_FLAGS_GREEDY)
821
938
      modifier.flags |= STRING_FLAGS_GREEDY_REGEXP;
822
823
    // Regular expressions in the strings section can't mix greedy and
824
    // ungreedy quantifiers like .* and .*?. That's because these regular
825
    // expressions can be matched forwards and/or backwards depending on the
826
    // atom found, and we need the regexp to be all-greedy or all-ungreedy to
827
    // be able to properly calculate the length of the match.
828
829
17.8k
    if ((re_ast->flags & RE_FLAGS_GREEDY) &&
830
938
        (re_ast->flags & RE_FLAGS_UNGREEDY))
831
7
    {
832
7
      result = ERROR_INVALID_REGULAR_EXPRESSION;
833
834
7
      yr_compiler_set_error_extra_info(
835
7
          compiler,
836
7
          "greedy and ungreedy quantifiers can't be mixed in a regular "
837
7
          "expression");
838
839
7
      goto _exit;
840
7
    }
841
842
17.8k
    if (yr_re_ast_has_unbounded_quantifier_for_dot(re_ast))
843
1.72k
    {
844
1.72k
      yywarning(
845
1.72k
          yyscanner,
846
1.72k
          "%s contains .*, .+ or .{x,} consider using .{,N}, .{1,N} or {x,N} "
847
1.72k
          "with a reasonable value for N",
848
1.72k
          identifier);
849
1.72k
    }
850
851
17.8k
    if (compiler->re_ast_callback != NULL)
852
0
    {
853
0
      compiler->re_ast_callback(
854
0
          current_rule, identifier, re_ast, compiler->re_ast_clbk_user_data);
855
0
    }
856
857
17.8k
    *string_ref = YR_ARENA_NULL_REF;
858
859
41.0k
    while (re_ast != NULL)
860
23.3k
    {
861
23.3k
      YR_ARENA_REF ref;
862
863
23.3k
      uint32_t prev_string_idx = compiler->current_string_idx - 1;
864
865
23.3k
      int32_t prev_min_gap = min_gap;
866
23.3k
      int32_t prev_max_gap = max_gap;
867
868
23.3k
      result = yr_re_ast_split_at_chaining_point(
869
23.3k
          re_ast, &remainder_re_ast, &min_gap, &max_gap);
870
871
23.3k
      if (result != ERROR_SUCCESS)
872
0
        goto _exit;
873
874
23.3k
      result = _yr_parser_write_string(
875
23.3k
          identifier,
876
23.3k
          modifier,
877
23.3k
          compiler,
878
23.3k
          NULL,
879
23.3k
          re_ast,
880
23.3k
          &ref,
881
23.3k
          &atom_quality,
882
23.3k
          &current_rule->num_atoms);
883
884
23.3k
      if (result != ERROR_SUCCESS)
885
139
        goto _exit;
886
887
23.1k
      if (atom_quality < min_atom_quality)
888
17.2k
        min_atom_quality = atom_quality;
889
890
23.1k
      if (YR_ARENA_IS_NULL_REF(*string_ref))
891
17.7k
      {
892
        // This is the first string in the chain, the string reference
893
        // returned by this function must point to this string.
894
17.7k
        *string_ref = ref;
895
17.7k
      }
896
5.45k
      else
897
5.45k
      {
898
        // This is not the first string in the chain, set the appropriate
899
        // flags and fill the chained_to, chain_gap_min and chain_gap_max
900
        // fields.
901
5.45k
        YR_STRING* prev_string = (YR_STRING*) yr_arena_get_ptr(
902
5.45k
            compiler->arena,
903
5.45k
            YR_STRINGS_TABLE,
904
5.45k
            prev_string_idx * sizeof(YR_STRING));
905
906
5.45k
        YR_STRING* new_string = (YR_STRING*) yr_arena_ref_to_ptr(
907
5.45k
            compiler->arena, &ref);
908
909
5.45k
        new_string->chained_to = prev_string;
910
5.45k
        new_string->chain_gap_min = prev_min_gap;
911
5.45k
        new_string->chain_gap_max = prev_max_gap;
912
913
        // A string chained to another one can't have a fixed offset, only the
914
        // head of the string chain can have a fixed offset.
915
5.45k
        new_string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
916
917
        // There is a previous string, but that string wasn't marked as part
918
        // of a chain because we can't do that until knowing there will be
919
        // another string, let's flag it now the we know.
920
5.45k
        prev_string->flags |= STRING_FLAGS_CHAIN_PART;
921
922
        // There is a previous string, so this string is part of a chain, but
923
        // there will be no more strings because there are no more AST to
924
        // split, which means that this is the chain's tail.
925
5.45k
        if (remainder_re_ast == NULL)
926
1.35k
          new_string->flags |= STRING_FLAGS_CHAIN_PART |
927
1.35k
                               STRING_FLAGS_CHAIN_TAIL;
928
5.45k
      }
929
930
23.1k
      yr_re_ast_destroy(re_ast);
931
23.1k
      re_ast = remainder_re_ast;
932
23.1k
    }
933
17.8k
  }
934
1.73k
  else  // not a STRING_FLAGS_HEXADECIMAL or STRING_FLAGS_REGEXP or
935
        // STRING_FLAGS_BASE64 or STRING_FLAGS_BASE64_WIDE
936
1.73k
  {
937
1.73k
    result = _yr_parser_write_string(
938
1.73k
        identifier,
939
1.73k
        modifier,
940
1.73k
        compiler,
941
1.73k
        str,
942
1.73k
        NULL,
943
1.73k
        string_ref,
944
1.73k
        &min_atom_quality,
945
1.73k
        &current_rule->num_atoms);
946
947
1.73k
    if (result != ERROR_SUCCESS)
948
0
      goto _exit;
949
1.73k
  }
950
951
19.4k
  if (min_atom_quality < compiler->atoms_config.quality_warning_threshold)
952
5.94k
  {
953
5.94k
    yywarning(yyscanner, "string \"%s\" may slow down scanning", identifier);
954
5.94k
  }
955
956
20.4k
_exit:
957
958
20.4k
  if (re_ast != NULL)
959
490
    yr_re_ast_destroy(re_ast);
960
961
20.4k
  if (remainder_re_ast != NULL)
962
1
    yr_re_ast_destroy(remainder_re_ast);
963
964
20.4k
  return result;
965
19.4k
}
966
967
static int wildcard_iterator(
968
    void* prefix,
969
    size_t prefix_len,
970
    void* _value,
971
    void* data)
972
3.49k
{
973
3.49k
  const char* identifier = (const char*) data;
974
975
  // If the identifier is prefixed by prefix, then it matches the wildcard.
976
3.49k
  if (!strncmp(prefix, identifier, prefix_len))
977
272
    return ERROR_IDENTIFIER_MATCHES_WILDCARD;
978
979
3.21k
  return ERROR_SUCCESS;
980
3.49k
}
981
982
int yr_parser_reduce_rule_declaration_phase_1(
983
    yyscan_t yyscanner,
984
    int32_t flags,
985
    const char* identifier,
986
    YR_ARENA_REF* rule_ref)
987
25.7k
{
988
25.7k
  int result;
989
25.7k
  YR_FIXUP* fixup;
990
25.7k
  YR_COMPILER* compiler = yyget_extra(yyscanner);
991
992
25.7k
  YR_NAMESPACE* ns = (YR_NAMESPACE*) yr_arena_get_ptr(
993
25.7k
      compiler->arena,
994
25.7k
      YR_NAMESPACES_TABLE,
995
25.7k
      compiler->current_namespace_idx * sizeof(struct YR_NAMESPACE));
996
997
25.7k
  if (yr_hash_table_lookup_uint32(
998
25.7k
          compiler->rules_table, identifier, ns->name) != UINT32_MAX ||
999
9.93k
      yr_hash_table_lookup(compiler->objects_table, identifier, NULL) != NULL)
1000
15.7k
  {
1001
    // A rule or variable with the same identifier already exists, return the
1002
    // appropriate error.
1003
1004
15.7k
    yr_compiler_set_error_extra_info(compiler, identifier);
1005
15.7k
    return ERROR_DUPLICATED_IDENTIFIER;
1006
15.7k
  }
1007
1008
  // Iterate over all identifiers in wildcard_identifiers_table, and check if
1009
  // any of them are a prefix of the identifier being declared. If so, return
1010
  // ERROR_IDENTIFIER_MATCHES_WILDCARD.
1011
9.93k
  result = yr_hash_table_iterate(
1012
9.93k
      compiler->wildcard_identifiers_table,
1013
9.93k
      ns->name,
1014
9.93k
      wildcard_iterator,
1015
9.93k
      (void*) identifier);
1016
1017
9.93k
  if (result == ERROR_IDENTIFIER_MATCHES_WILDCARD)
1018
272
  {
1019
    // This rule matches an existing wildcard rule set.
1020
272
    yr_compiler_set_error_extra_info(compiler, identifier);
1021
272
  }
1022
1023
9.93k
  FAIL_ON_ERROR(result);
1024
1025
9.65k
  FAIL_ON_ERROR(yr_arena_allocate_struct(
1026
9.65k
      compiler->arena,
1027
9.65k
      YR_RULES_TABLE,
1028
9.65k
      sizeof(YR_RULE),
1029
9.65k
      rule_ref,
1030
9.65k
      offsetof(YR_RULE, identifier),
1031
9.65k
      offsetof(YR_RULE, tags),
1032
9.65k
      offsetof(YR_RULE, strings),
1033
9.65k
      offsetof(YR_RULE, metas),
1034
9.65k
      offsetof(YR_RULE, ns),
1035
9.65k
      EOL));
1036
1037
9.65k
  YR_RULE* rule = (YR_RULE*) yr_arena_ref_to_ptr(compiler->arena, rule_ref);
1038
1039
9.65k
  YR_ARENA_REF ref;
1040
1041
9.65k
  FAIL_ON_ERROR(_yr_compiler_store_string(compiler, identifier, &ref));
1042
1043
9.65k
  rule->identifier = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref);
1044
9.65k
  rule->flags = flags;
1045
9.65k
  rule->ns = ns;
1046
9.65k
  rule->num_atoms = 0;
1047
1048
9.65k
  YR_ARENA_REF jmp_offset_ref;
1049
1050
  // We are starting to parse a new rule, set current_rule_idx accordingly.
1051
9.65k
  compiler->current_rule_idx = compiler->next_rule_idx;
1052
9.65k
  compiler->next_rule_idx++;
1053
1054
  // The OP_INIT_RULE instruction behaves like a jump. When the rule is
1055
  // disabled it skips over the rule's code and go straight to the next rule's
1056
  // code. The jmp_offset_ref variable points to the jump's offset. The offset
1057
  // is set to 0 as we don't know the jump target yet. When we finish
1058
  // generating the rule's code in yr_parser_reduce_rule_declaration_phase_2
1059
  // the jump offset is set to its final value.
1060
1061
9.65k
  FAIL_ON_ERROR(yr_parser_emit_with_arg_int32(
1062
9.65k
      yyscanner, OP_INIT_RULE, 0, NULL, &jmp_offset_ref));
1063
1064
9.65k
  FAIL_ON_ERROR(yr_arena_write_data(
1065
9.65k
      compiler->arena,
1066
9.65k
      YR_CODE_SECTION,
1067
9.65k
      &compiler->current_rule_idx,
1068
9.65k
      sizeof(compiler->current_rule_idx),
1069
9.65k
      NULL));
1070
1071
  // Create a fixup entry for the jump and push it in the stack
1072
9.65k
  fixup = (YR_FIXUP*) yr_malloc(sizeof(YR_FIXUP));
1073
1074
9.65k
  if (fixup == NULL)
1075
0
    return ERROR_INSUFFICIENT_MEMORY;
1076
1077
9.65k
  fixup->ref = jmp_offset_ref;
1078
9.65k
  fixup->next = compiler->fixup_stack_head;
1079
9.65k
  compiler->fixup_stack_head = fixup;
1080
1081
  // Clean strings_table as we are starting to parse a new rule.
1082
9.65k
  yr_hash_table_clean(compiler->strings_table, NULL);
1083
1084
9.65k
  FAIL_ON_ERROR(yr_hash_table_add_uint32(
1085
9.65k
      compiler->rules_table, identifier, ns->name, compiler->current_rule_idx));
1086
1087
9.65k
  return ERROR_SUCCESS;
1088
9.65k
}
1089
1090
int yr_parser_reduce_rule_declaration_phase_2(
1091
    yyscan_t yyscanner,
1092
    YR_ARENA_REF* rule_ref)
1093
485
{
1094
485
  uint32_t max_strings_per_rule;
1095
485
  uint32_t strings_in_rule = 0;
1096
1097
485
  YR_FIXUP* fixup;
1098
485
  YR_STRING* string;
1099
485
  YR_COMPILER* compiler = yyget_extra(yyscanner);
1100
1101
485
  yr_get_configuration_uint32(
1102
485
      YR_CONFIG_MAX_STRINGS_PER_RULE, &max_strings_per_rule);
1103
1104
485
  YR_RULE* rule = (YR_RULE*) yr_arena_ref_to_ptr(compiler->arena, rule_ref);
1105
1106
  // Show warning if the rule is generating too many atoms. The warning is
1107
  // shown if the number of atoms is greater than 20 times the maximum number
1108
  // of strings allowed for a rule, as 20 is minimum number of atoms generated
1109
  // for a string using *nocase*, *ascii* and *wide* modifiers simultaneously.
1110
1111
485
  if (rule->num_atoms > YR_ATOMS_PER_RULE_WARNING_THRESHOLD)
1112
20
  {
1113
20
    yywarning(yyscanner, "rule is slowing down scanning");
1114
20
  }
1115
1116
485
  yr_rule_strings_foreach(rule, string)
1117
2.34k
  {
1118
    // Only the heading fragment in a chain of strings (the one with
1119
    // chained_to == NULL) must be referenced. All other fragments
1120
    // are never marked as referenced.
1121
    //
1122
    // Any string identifier that starts with '_' can be unreferenced. Anonymous
1123
    // strings must always be referenced.
1124
1125
2.34k
    if (!STRING_IS_REFERENCED(string) && string->chained_to == NULL &&
1126
150
        (STRING_IS_ANONYMOUS(string) ||
1127
143
         (!STRING_IS_ANONYMOUS(string) && string->identifier[1] != '_')))
1128
19
    {
1129
19
      yr_compiler_set_error_extra_info(
1130
19
          compiler, string->identifier) return ERROR_UNREFERENCED_STRING;
1131
19
    }
1132
1133
    // If a string is unreferenced we need to unset the FIXED_OFFSET flag so
1134
    // that it will match anywhere.
1135
2.32k
    if (!STRING_IS_REFERENCED(string) && string->chained_to == NULL &&
1136
131
        STRING_IS_FIXED_OFFSET(string))
1137
89
    {
1138
89
      string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
1139
89
    }
1140
1141
2.32k
    strings_in_rule++;
1142
1143
2.32k
    if (strings_in_rule > max_strings_per_rule)
1144
0
    {
1145
0
      yr_compiler_set_error_extra_info(
1146
0
          compiler, rule->identifier) return ERROR_TOO_MANY_STRINGS;
1147
0
    }
1148
2.32k
  }
1149
1150
466
  FAIL_ON_ERROR(yr_parser_emit_with_arg(
1151
466
      yyscanner, OP_MATCH_RULE, compiler->current_rule_idx, NULL, NULL));
1152
1153
466
  fixup = compiler->fixup_stack_head;
1154
1155
466
  int32_t* jmp_offset_addr = (int32_t*) yr_arena_ref_to_ptr(
1156
466
      compiler->arena, &fixup->ref);
1157
1158
466
  int32_t jmp_offset = yr_arena_get_current_offset(
1159
466
                           compiler->arena, YR_CODE_SECTION) -
1160
466
                       fixup->ref.offset + 1;
1161
1162
466
  memcpy(jmp_offset_addr, &jmp_offset, sizeof(jmp_offset));
1163
1164
  // Remove fixup from the stack.
1165
466
  compiler->fixup_stack_head = fixup->next;
1166
466
  yr_free(fixup);
1167
1168
  // We have finished parsing the current rule set current_rule_idx to
1169
  // UINT32_MAX indicating that we are not currently parsing a rule.
1170
466
  compiler->current_rule_idx = UINT32_MAX;
1171
1172
466
  return ERROR_SUCCESS;
1173
466
}
1174
1175
int yr_parser_reduce_string_identifier(
1176
    yyscan_t yyscanner,
1177
    const char* identifier,
1178
    uint8_t instruction,
1179
    uint64_t at_offset)
1180
15.7k
{
1181
15.7k
  YR_STRING* string;
1182
15.7k
  YR_COMPILER* compiler = yyget_extra(yyscanner);
1183
1184
15.7k
  if (strcmp(identifier, "$") == 0)  // is an anonymous string ?
1185
12.7k
  {
1186
12.7k
    if (compiler->loop_for_of_var_index >= 0)  // inside a loop ?
1187
12.6k
    {
1188
12.6k
      yr_parser_emit_with_arg(
1189
12.6k
          yyscanner, OP_PUSH_M, compiler->loop_for_of_var_index, NULL, NULL);
1190
1191
12.6k
      yr_parser_emit(yyscanner, instruction, NULL);
1192
1193
12.6k
      YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
1194
12.6k
          compiler, compiler->current_rule_idx);
1195
1196
12.6k
      yr_rule_strings_foreach(current_rule, string)
1197
883k
      {
1198
883k
        if (instruction != OP_FOUND)
1199
883k
          string->flags &= ~STRING_FLAGS_SINGLE_MATCH;
1200
1201
883k
        if (instruction == OP_FOUND_AT)
1202
877
        {
1203
          // Avoid overwriting any previous fixed offset
1204
877
          if (string->fixed_offset == YR_UNDEFINED)
1205
399
            string->fixed_offset = at_offset;
1206
1207
          // If a previous fixed offset was different, disable
1208
          // the STRING_GFLAGS_FIXED_OFFSET flag because we only
1209
          // have room to store a single fixed offset value
1210
877
          if (string->fixed_offset != at_offset)
1211
408
            string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
1212
877
        }
1213
882k
        else
1214
882k
        {
1215
882k
          string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
1216
882k
        }
1217
883k
      }
1218
12.6k
    }
1219
89
    else
1220
89
    {
1221
      // Anonymous strings not allowed outside of a loop
1222
89
      return ERROR_MISPLACED_ANONYMOUS_STRING;
1223
89
    }
1224
12.7k
  }
1225
3.05k
  else
1226
3.05k
  {
1227
3.05k
    FAIL_ON_ERROR(yr_parser_lookup_string(yyscanner, identifier, &string));
1228
1229
3.02k
    FAIL_ON_ERROR(
1230
3.02k
        yr_parser_emit_with_arg_reloc(yyscanner, OP_PUSH, string, NULL, NULL));
1231
1232
3.02k
    if (instruction != OP_FOUND)
1233
2.68k
      string->flags &= ~STRING_FLAGS_SINGLE_MATCH;
1234
1235
3.02k
    if (instruction == OP_FOUND_AT)
1236
764
    {
1237
      // Avoid overwriting any previous fixed offset
1238
1239
764
      if (string->fixed_offset == YR_UNDEFINED)
1240
237
        string->fixed_offset = at_offset;
1241
1242
      // If a previous fixed offset was different, disable
1243
      // the STRING_GFLAGS_FIXED_OFFSET flag because we only
1244
      // have room to store a single fixed offset value
1245
1246
764
      if (string->fixed_offset == YR_UNDEFINED ||
1247
536
          string->fixed_offset != at_offset)
1248
551
      {
1249
551
        string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
1250
551
      }
1251
764
    }
1252
2.25k
    else
1253
2.25k
    {
1254
2.25k
      string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
1255
2.25k
    }
1256
1257
3.02k
    FAIL_ON_ERROR(yr_parser_emit(yyscanner, instruction, NULL));
1258
1259
3.02k
    string->flags |= STRING_FLAGS_REFERENCED;
1260
3.02k
  }
1261
1262
15.6k
  return ERROR_SUCCESS;
1263
15.7k
}
1264
1265
int yr_parser_reduce_meta_declaration(
1266
    yyscan_t yyscanner,
1267
    int32_t type,
1268
    const char* identifier,
1269
    const char* string,
1270
    int64_t integer,
1271
    YR_ARENA_REF* meta_ref)
1272
851
{
1273
851
  YR_ARENA_REF ref;
1274
851
  YR_COMPILER* compiler = yyget_extra(yyscanner);
1275
1276
851
  FAIL_ON_ERROR(yr_arena_allocate_struct(
1277
851
      compiler->arena,
1278
851
      YR_METAS_TABLE,
1279
851
      sizeof(YR_META),
1280
851
      meta_ref,
1281
851
      offsetof(YR_META, identifier),
1282
851
      offsetof(YR_META, string),
1283
851
      EOL));
1284
1285
851
  YR_META* meta = (YR_META*) yr_arena_ref_to_ptr(compiler->arena, meta_ref);
1286
1287
851
  meta->type = type;
1288
851
  meta->integer = integer;
1289
1290
851
  FAIL_ON_ERROR(_yr_compiler_store_string(compiler, identifier, &ref));
1291
1292
851
  meta->identifier = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref);
1293
1294
851
  if (string != NULL)
1295
263
  {
1296
263
    FAIL_ON_ERROR(_yr_compiler_store_string(compiler, string, &ref));
1297
1298
263
    meta->string = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref);
1299
263
  }
1300
588
  else
1301
588
  {
1302
588
    meta->string = NULL;
1303
588
  }
1304
1305
851
  compiler->current_meta_idx++;
1306
1307
851
  return ERROR_SUCCESS;
1308
851
}
1309
1310
static int _yr_parser_valid_module_name(SIZED_STRING* module_name)
1311
5.31k
{
1312
5.31k
  if (module_name->length == 0)
1313
176
    return false;
1314
1315
5.13k
  if (strlen(module_name->c_string) != module_name->length)
1316
173
    return false;
1317
1318
4.96k
  return true;
1319
5.13k
}
1320
1321
int yr_parser_reduce_import(yyscan_t yyscanner, SIZED_STRING* module_name)
1322
5.31k
{
1323
5.31k
  int result;
1324
1325
5.31k
  YR_ARENA_REF ref;
1326
5.31k
  YR_COMPILER* compiler = yyget_extra(yyscanner);
1327
5.31k
  YR_OBJECT* module_structure;
1328
1329
5.31k
  if (!_yr_parser_valid_module_name(module_name))
1330
349
  {
1331
349
    yr_compiler_set_error_extra_info(compiler, module_name->c_string);
1332
1333
349
    return ERROR_INVALID_MODULE_NAME;
1334
349
  }
1335
1336
4.96k
  YR_NAMESPACE* ns = (YR_NAMESPACE*) yr_arena_get_ptr(
1337
4.96k
      compiler->arena,
1338
4.96k
      YR_NAMESPACES_TABLE,
1339
4.96k
      compiler->current_namespace_idx * sizeof(struct YR_NAMESPACE));
1340
1341
4.96k
  module_structure = (YR_OBJECT*) yr_hash_table_lookup(
1342
4.96k
      compiler->objects_table, module_name->c_string, ns->name);
1343
1344
  // if module already imported, do nothing
1345
1346
4.96k
  if (module_structure != NULL)
1347
4.51k
    return ERROR_SUCCESS;
1348
1349
445
  FAIL_ON_ERROR(yr_object_create(
1350
445
      OBJECT_TYPE_STRUCTURE, module_name->c_string, NULL, &module_structure));
1351
1352
445
  FAIL_ON_ERROR(yr_hash_table_add(
1353
445
      compiler->objects_table,
1354
445
      module_name->c_string,
1355
445
      ns->name,
1356
445
      module_structure));
1357
1358
445
  result = yr_modules_do_declarations(module_name->c_string, module_structure);
1359
1360
445
  if (result == ERROR_UNKNOWN_MODULE)
1361
247
    yr_compiler_set_error_extra_info(compiler, module_name->c_string);
1362
1363
445
  if (result != ERROR_SUCCESS)
1364
247
    return result;
1365
1366
198
  FAIL_ON_ERROR(
1367
198
      _yr_compiler_store_string(compiler, module_name->c_string, &ref));
1368
1369
198
  FAIL_ON_ERROR(yr_parser_emit_with_arg_reloc(
1370
198
      yyscanner,
1371
198
      OP_IMPORT,
1372
198
      yr_arena_ref_to_ptr(compiler->arena, &ref),
1373
198
      NULL,
1374
198
      NULL));
1375
1376
198
  return ERROR_SUCCESS;
1377
198
}
1378
1379
static int _yr_parser_operator_to_opcode(const char* op, int expression_type)
1380
18.7k
{
1381
18.7k
  int opcode = 0;
1382
1383
18.7k
  switch (expression_type)
1384
18.7k
  {
1385
12.4k
  case EXPRESSION_TYPE_INTEGER:
1386
12.4k
    opcode = OP_INT_BEGIN;
1387
12.4k
    break;
1388
3.31k
  case EXPRESSION_TYPE_FLOAT:
1389
3.31k
    opcode = OP_DBL_BEGIN;
1390
3.31k
    break;
1391
2.98k
  case EXPRESSION_TYPE_STRING:
1392
2.98k
    opcode = OP_STR_BEGIN;
1393
2.98k
    break;
1394
0
  default:
1395
0
    assert(false);
1396
18.7k
  }
1397
1398
18.7k
  if (op[0] == '<')
1399
1.86k
  {
1400
1.86k
    if (op[1] == '=')
1401
428
      opcode += _OP_LE;
1402
1.43k
    else
1403
1.43k
      opcode += _OP_LT;
1404
1.86k
  }
1405
16.8k
  else if (op[0] == '>')
1406
1.67k
  {
1407
1.67k
    if (op[1] == '=')
1408
495
      opcode += _OP_GE;
1409
1.18k
    else
1410
1.18k
      opcode += _OP_GT;
1411
1.67k
  }
1412
15.2k
  else if (op[1] == '=')
1413
1.27k
  {
1414
1.27k
    if (op[0] == '=')
1415
750
      opcode += _OP_EQ;
1416
526
    else
1417
526
      opcode += _OP_NEQ;
1418
1.27k
  }
1419
13.9k
  else if (op[0] == '+')
1420
3.56k
  {
1421
3.56k
    opcode += _OP_ADD;
1422
3.56k
  }
1423
10.3k
  else if (op[0] == '-')
1424
7.31k
  {
1425
7.31k
    opcode += _OP_SUB;
1426
7.31k
  }
1427
3.06k
  else if (op[0] == '*')
1428
1.71k
  {
1429
1.71k
    opcode += _OP_MUL;
1430
1.71k
  }
1431
1.35k
  else if (op[0] == '\\')
1432
1.35k
  {
1433
1.35k
    opcode += _OP_DIV;
1434
1.35k
  }
1435
1436
18.7k
  if (IS_INT_OP(opcode) || IS_DBL_OP(opcode) || IS_STR_OP(opcode))
1437
18.7k
  {
1438
18.7k
    return opcode;
1439
18.7k
  }
1440
1441
8
  return OP_ERROR;
1442
18.7k
}
1443
1444
int yr_parser_reduce_operation(
1445
    yyscan_t yyscanner,
1446
    const char* op,
1447
    YR_EXPRESSION left_operand,
1448
    YR_EXPRESSION right_operand)
1449
18.9k
{
1450
18.9k
  int expression_type;
1451
1452
18.9k
  YR_COMPILER* compiler = yyget_extra(yyscanner);
1453
1454
18.9k
  if ((left_operand.type == EXPRESSION_TYPE_INTEGER ||
1455
4.95k
       left_operand.type == EXPRESSION_TYPE_FLOAT) &&
1456
15.7k
      (right_operand.type == EXPRESSION_TYPE_INTEGER ||
1457
2.35k
       right_operand.type == EXPRESSION_TYPE_FLOAT))
1458
15.7k
  {
1459
15.7k
    if (left_operand.type != right_operand.type)
1460
2.53k
    {
1461
      // One operand is double and the other is integer,
1462
      // cast the integer to double
1463
1464
2.53k
      FAIL_ON_ERROR(yr_parser_emit_with_arg(
1465
2.53k
          yyscanner,
1466
2.53k
          OP_INT_TO_DBL,
1467
2.53k
          (left_operand.type == EXPRESSION_TYPE_INTEGER) ? 2 : 1,
1468
2.53k
          NULL,
1469
2.53k
          NULL));
1470
2.53k
    }
1471
1472
15.7k
    expression_type = EXPRESSION_TYPE_FLOAT;
1473
1474
15.7k
    if (left_operand.type == EXPRESSION_TYPE_INTEGER &&
1475
14.0k
        right_operand.type == EXPRESSION_TYPE_INTEGER)
1476
12.4k
    {
1477
12.4k
      expression_type = EXPRESSION_TYPE_INTEGER;
1478
12.4k
    }
1479
1480
15.7k
    FAIL_ON_ERROR(yr_parser_emit(
1481
15.7k
        yyscanner, _yr_parser_operator_to_opcode(op, expression_type), NULL));
1482
15.7k
  }
1483
3.21k
  else if (
1484
3.21k
      left_operand.type == EXPRESSION_TYPE_STRING &&
1485
3.05k
      right_operand.type == EXPRESSION_TYPE_STRING)
1486
2.98k
  {
1487
2.98k
    int opcode = _yr_parser_operator_to_opcode(op, EXPRESSION_TYPE_STRING);
1488
1489
2.98k
    if (opcode != OP_ERROR)
1490
2.97k
    {
1491
2.97k
      FAIL_ON_ERROR(yr_parser_emit(yyscanner, opcode, NULL));
1492
2.97k
    }
1493
8
    else
1494
8
    {
1495
8
      yr_compiler_set_error_extra_info_fmt(
1496
8
          compiler, "strings don't support \"%s\" operation", op);
1497
1498
8
      return ERROR_WRONG_TYPE;
1499
8
    }
1500
2.98k
  }
1501
228
  else
1502
228
  {
1503
228
    yr_compiler_set_error_extra_info(compiler, "type mismatch");
1504
1505
228
    return ERROR_WRONG_TYPE;
1506
228
  }
1507
1508
18.7k
  return ERROR_SUCCESS;
1509
18.9k
}
1510
1511
int yr_parser_mark_nonfast(
1512
   yyscan_t yyscanner,
1513
   YR_STRING_SET string_set
1514
6.30k
) {
1515
6.30k
 YR_COMPILER* compiler = yyget_extra(yyscanner);
1516
1517
6.30k
 YR_STRING_SET_ELEMENT* head = string_set.head;
1518
865k
  while (head != NULL) {
1519
859k
    YR_STRING* string_ptr = yr_arena_ref_to_ptr(compiler->arena, &head->element);
1520
859k
    string_ptr->flags &= ~STRING_FLAGS_SINGLE_MATCH;
1521
859k
    head = head->next;
1522
859k
  }
1523
6.30k
  return ERROR_SUCCESS;
1524
6.30k
}