Coverage Report

Created: 2026-12-04 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/yara/libyara/scan.c
Line
Count
Source
1
/*
2
Copyright (c) 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 <ctype.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <yara/bitmask.h>
35
#include <yara/error.h>
36
#include <yara/globals.h>
37
#include <yara/libyara.h>
38
#include <yara/limits.h>
39
#include <yara/re.h>
40
#include <yara/rules.h>
41
#include <yara/scan.h>
42
#include <yara/stopwatch.h>
43
#include <yara/strutils.h>
44
#include <yara/types.h>
45
#include <yara/utils.h>
46
47
typedef struct _CALLBACK_ARGS
48
{
49
  YR_STRING* string;
50
  YR_SCAN_CONTEXT* context;
51
52
  const uint8_t* data;
53
  size_t data_size;
54
  uint64_t data_base;
55
56
  int forward_matches;
57
  int full_word;
58
  int xor_key;
59
60
} CALLBACK_ARGS;
61
62
static int _yr_scan_xor_compare(
63
    const uint8_t* data,
64
    size_t data_size,
65
    uint8_t* string,
66
    size_t string_length,
67
    uint8_t* xor_key)
68
0
{
69
0
  int result = 0;
70
0
  const uint8_t* s1 = data;
71
0
  const uint8_t* s2 = string;
72
0
  uint8_t k = 0;
73
74
0
  size_t i = 0;
75
76
0
  if (data_size < string_length)
77
0
    goto _exit;
78
79
  // Calculate the xor key to compare with. *s1 is the start of the string we
80
  // matched on and *s2 is the "plaintext" string, so *s1 ^ *s2 is the key to
81
  // every *s2 as we compare.
82
0
  k = *s1 ^ *s2;
83
84
0
  while (i < string_length && *s1++ == ((*s2++) ^ k)) i++;
85
86
0
  result = (int) ((i == string_length) ? i : 0);
87
88
0
_exit:;
89
90
0
  YR_DEBUG_FPRINTF(
91
0
      2,
92
0
      stderr,
93
0
      "- %s(data_size=%zu string_length=%zu) {} = %d\n",
94
0
      __FUNCTION__,
95
0
      data_size,
96
0
      string_length,
97
0
      result);
98
99
0
  if (result > 0)
100
0
    *xor_key = k;
101
102
0
  return result;
103
0
}
104
105
static int _yr_scan_xor_wcompare(
106
    const uint8_t* data,
107
    size_t data_size,
108
    uint8_t* string,
109
    size_t string_length,
110
    uint8_t* xor_key)
111
0
{
112
0
  int result = 0;
113
0
  const uint8_t* s1 = data;
114
0
  const uint8_t* s2 = string;
115
0
  uint8_t k = 0;
116
117
0
  size_t i = 0;
118
119
0
  if (data_size < string_length * 2)
120
0
    return 0;
121
122
  // Calculate the xor key to compare with. *s1 is the start of the string we
123
  // matched on and *s2 is the "plaintext" string, so *s1 ^ *s2 is the key to
124
  // every *s2 as we compare.
125
0
  k = *s1 ^ *s2;
126
127
0
  while (i < string_length && *s1 == ((*s2) ^ k) && ((*(s1 + 1)) ^ k) == 0x00)
128
0
  {
129
0
    s1 += 2;
130
0
    s2++;
131
0
    i++;
132
0
  }
133
134
0
  result = (int) ((i == string_length) ? i * 2 : 0);
135
136
0
  if (result > 0)
137
0
    *xor_key = k;
138
139
0
  return result;
140
0
}
141
142
static int _yr_scan_compare(
143
    const uint8_t* data,
144
    size_t data_size,
145
    uint8_t* string,
146
    size_t string_length)
147
0
{
148
0
  const uint8_t* s1 = data;
149
0
  const uint8_t* s2 = string;
150
151
0
  size_t i = 0;
152
153
0
  if (data_size < string_length)
154
0
    return 0;
155
156
0
  while (i < string_length && *s1++ == *s2++) i++;
157
158
0
  return (int) ((i == string_length) ? i : 0);
159
0
}
160
161
static int _yr_scan_icompare(
162
    const uint8_t* data,
163
    size_t data_size,
164
    uint8_t* string,
165
    size_t string_length)
166
0
{
167
0
  const uint8_t* s1 = data;
168
0
  const uint8_t* s2 = string;
169
170
0
  size_t i = 0;
171
172
0
  if (data_size < string_length)
173
0
    return 0;
174
175
0
  while (i < string_length && yr_lowercase[*s1++] == yr_lowercase[*s2++]) i++;
176
177
0
  return (int) ((i == string_length) ? i : 0);
178
0
}
179
180
static int _yr_scan_wcompare(
181
    const uint8_t* data,
182
    size_t data_size,
183
    uint8_t* string,
184
    size_t string_length)
185
0
{
186
0
  int result = 0;
187
0
  const uint8_t* s1 = data;
188
0
  const uint8_t* s2 = string;
189
190
0
  size_t i = 0;
191
192
0
  if (data_size < string_length * 2)
193
0
    goto _exit;
194
195
0
  while (i < string_length && *s1 == *s2 && *(s1 + 1) == 0x00)
196
0
  {
197
0
    s1 += 2;
198
0
    s2++;
199
0
    i++;
200
0
  }
201
202
0
  result = (int) ((i == string_length) ? i * 2 : 0);
203
204
0
_exit:;
205
206
0
  YR_DEBUG_FPRINTF(
207
0
      2,
208
0
      stderr,
209
0
      "- %s(data_size=%zu string_length=%zu) {} = %d\n",
210
0
      __FUNCTION__,
211
0
      data_size,
212
0
      string_length,
213
0
      result);
214
215
0
  return result;
216
0
}
217
218
static int _yr_scan_wicompare(
219
    const uint8_t* data,
220
    size_t data_size,
221
    uint8_t* string,
222
    size_t string_length)
223
0
{
224
0
  int result = 0;
225
0
  const uint8_t* s1 = data;
226
0
  const uint8_t* s2 = string;
227
228
0
  size_t i = 0;
229
230
0
  if (data_size < string_length * 2)
231
0
    goto _exit;
232
233
0
  while (i < string_length && yr_lowercase[*s1] == yr_lowercase[*s2] &&
234
0
         *(s1 + 1) == 0x00)
235
0
  {
236
0
    s1 += 2;
237
0
    s2++;
238
0
    i++;
239
0
  }
240
241
0
  result = (int) ((i == string_length) ? i * 2 : 0);
242
243
0
_exit:;
244
245
0
  YR_DEBUG_FPRINTF(
246
0
      2,
247
0
      stderr,
248
0
      "- %s(data_size=%zu string_length=%zu) {} = %d\n",
249
0
      __FUNCTION__,
250
0
      data_size,
251
0
      string_length,
252
0
      result);
253
254
0
  return result;
255
0
}
256
257
static void _yr_scan_update_match_chain_length(
258
    YR_SCAN_CONTEXT* context,
259
    YR_STRING* string,
260
    YR_MATCH* match_to_update,
261
    int chain_length)
262
0
{
263
0
  YR_MATCH* match;
264
265
0
  if (match_to_update->chain_length == chain_length)
266
0
    return;
267
268
0
  match_to_update->chain_length = chain_length;
269
270
0
  if (string->chained_to == NULL)
271
0
    return;
272
273
0
  match = context->unconfirmed_matches[string->chained_to->idx].head;
274
275
0
  while (match != NULL)
276
0
  {
277
0
    int64_t ending_offset = match->offset + match->match_length;
278
279
0
    if (ending_offset + string->chain_gap_max >= match_to_update->offset &&
280
0
        ending_offset + string->chain_gap_min <= match_to_update->offset)
281
0
    {
282
0
      _yr_scan_update_match_chain_length(
283
0
          context, string->chained_to, match, chain_length + 1);
284
0
    }
285
286
0
    match = match->next;
287
0
  }
288
0
}
289
290
static int _yr_scan_add_match_to_list(
291
    YR_MATCH* match,
292
    YR_MATCHES* matches_list,
293
    int replace_if_exists)
294
0
{
295
0
  int result = ERROR_SUCCESS;
296
297
#if YR_DEBUG_VERBOSITY > 0
298
  int32_t count_orig = matches_list->count;
299
#endif
300
301
0
  YR_MATCH* insertion_point = matches_list->tail;
302
303
0
  if (matches_list->count == YR_MAX_STRING_MATCHES)
304
0
  {
305
0
    result = ERROR_TOO_MANY_MATCHES;
306
0
    goto _exit;
307
0
  }
308
309
0
  while (insertion_point != NULL)
310
0
  {
311
0
    if ((match->base + match->offset) ==
312
0
        (insertion_point->base + insertion_point->offset))
313
0
    {
314
0
      if (replace_if_exists)
315
0
      {
316
0
        insertion_point->match_length = match->match_length;
317
0
        insertion_point->data_length = match->data_length;
318
0
        insertion_point->data = match->data;
319
0
      }
320
321
0
      goto _exit;  // return ERROR_SUCCESS
322
0
    }
323
324
0
    if ((match->base + match->offset) >
325
0
        (insertion_point->base + insertion_point->offset))
326
0
      break;
327
328
0
    insertion_point = insertion_point->prev;
329
0
  }
330
331
0
  match->prev = insertion_point;
332
333
0
  if (insertion_point != NULL)
334
0
  {
335
0
    match->next = insertion_point->next;
336
0
    insertion_point->next = match;
337
0
  }
338
0
  else
339
0
  {
340
0
    match->next = matches_list->head;
341
0
    matches_list->head = match;
342
0
  }
343
344
0
  matches_list->count++;
345
346
0
  if (match->next != NULL)
347
0
    match->next->prev = match;
348
0
  else
349
0
    matches_list->tail = match;
350
351
0
_exit:;
352
353
0
  YR_DEBUG_FPRINTF(
354
0
      2,
355
0
      stderr,
356
0
      "- %s(replace_if_exists=%d) {} = %d //"
357
0
      " match->base=0x%" PRIx64 " match->offset=%" PRIi64
358
0
      " matches_list->count=%u += %u\n",
359
0
      __FUNCTION__,
360
0
      replace_if_exists,
361
0
      result,
362
0
      match->base,
363
0
      match->offset,
364
0
      count_orig,
365
0
      matches_list->count - count_orig);
366
367
0
  return result;
368
0
}
369
370
static void _yr_scan_remove_match_from_list(
371
    YR_MATCH* match,
372
    YR_MATCHES* matches_list)
373
0
{
374
0
  if (match->prev != NULL)
375
0
    match->prev->next = match->next;
376
377
0
  if (match->next != NULL)
378
0
    match->next->prev = match->prev;
379
380
0
  if (matches_list->head == match)
381
0
    matches_list->head = match->next;
382
383
0
  if (matches_list->tail == match)
384
0
    matches_list->tail = match->prev;
385
386
0
  matches_list->count--;
387
0
  match->next = NULL;
388
0
  match->prev = NULL;
389
0
}
390
391
//
392
// _yr_scan_verify_chained_string_match
393
//
394
// Given a string that is part of a string chain and is matching at some
395
// point in the scanned data, this function determines if the whole string
396
// chain is also matching. For example, if the string S was splitted and
397
// converted in a chain S1 <- S2 <- S3 (see yr_re_ast_split_at_chaining_point),
398
// and a match for S3 was found, this functions finds out if there are matches
399
// for S1 and S2 that together with the match found for S3 conform a match for
400
// the whole S.
401
//
402
// Notice that this function operates in a non-greedy fashion. Matches found
403
// for S will be the shortest possible ones.
404
//
405
406
static int _yr_scan_verify_chained_string_match(
407
    YR_STRING* matching_string,
408
    YR_SCAN_CONTEXT* context,
409
    const uint8_t* match_data,
410
    uint64_t match_base,
411
    uint64_t match_offset,
412
    int32_t match_length,
413
    uint8_t xor_key)
414
0
{
415
0
  YR_DEBUG_FPRINTF(
416
0
      2,
417
0
      stderr,
418
0
      "- %s (match_data=%p match_base=%" PRIx64 " match_offset=0x%" PRIx64
419
0
      " match_length=%'d) {} \n",
420
0
      __FUNCTION__,
421
0
      match_data,
422
0
      match_base,
423
0
      match_offset,
424
0
      match_length);
425
426
0
  YR_STRING* string;
427
0
  YR_MATCH* match;
428
0
  YR_MATCH* next_match;
429
0
  YR_MATCH* new_match;
430
431
0
  uint64_t lowest_offset;
432
0
  uint64_t ending_offset;
433
0
  int32_t full_chain_length;
434
435
0
  bool add_match = false;
436
437
0
  if (matching_string->chained_to == NULL)
438
0
  {
439
    // The matching string is the head of the chain, this match should be
440
    // added to the list of unconfirmed matches. The match will remain
441
    // unconfirmed until all the strings in the chain are found with the
442
    // correct distances between them.
443
0
    add_match = true;
444
0
  }
445
0
  else
446
0
  {
447
    // If some unconfirmed match exists, the lowest possible offset where the
448
    // whole string chain can match is the offset of the first string in the
449
    // list of unconfirmed matches. Unconfirmed matches are sorted in ascending
450
    // offset order. If no unconfirmed match exists, the lowest possible offset
451
    // is the offset of the current match.
452
0
    match = context->unconfirmed_matches[matching_string->idx].head;
453
454
0
    if (match != NULL)
455
0
      lowest_offset = match->offset;
456
0
    else
457
0
      lowest_offset = match_offset;
458
459
    // Iterate over the list of unconfirmed matches for the string that
460
    // precedes the currently matching string. If we have a string chain like:
461
    // S1 <- S2 <- S3, and we just found a match for S2, we are iterating the
462
    // list of unconfirmed matches of S1.
463
0
    match = context->unconfirmed_matches[matching_string->chained_to->idx].head;
464
465
0
    while (match != NULL)
466
0
    {
467
      // Store match->next so that we can use it later for advancing in the
468
      // list, if _yr_scan_remove_match_from_list is called, match->next is
469
      // set to NULL, that's why we store its current value before that happens.
470
0
      next_match = match->next;
471
472
      // The unconfirmed match starts at match->offset and finishes at
473
      // ending_offset.
474
0
      ending_offset = match->offset + match->match_length;
475
476
0
      if (ending_offset + matching_string->chain_gap_max < lowest_offset)
477
0
      {
478
        // If the current match is too far away from the unconfirmed match,
479
        // remove the unconfirmed match from the list because it has been
480
        // negatively confirmed (i.e: we can be sure that this unconfirmed
481
        // match can't be an actual match)
482
0
        _yr_scan_remove_match_from_list(
483
0
            match,
484
0
            &context->unconfirmed_matches[matching_string->chained_to->idx]);
485
0
      }
486
0
      else if (
487
0
          ending_offset + matching_string->chain_gap_max >= match_offset &&
488
0
          ending_offset + matching_string->chain_gap_min <= match_offset)
489
0
      {
490
        // If the distance between the end of the unconfirmed match and the
491
        // start of the current match is within the range specified in the
492
        // regexp or hex string, this could be an actual match.
493
0
        add_match = true;
494
0
        break;
495
0
      }
496
497
0
      match = next_match;
498
0
    }
499
0
  }
500
501
0
  if (add_match)
502
0
  {
503
0
    uint32_t max_match_data;
504
505
0
    FAIL_ON_ERROR(
506
0
        yr_get_configuration_uint32(YR_CONFIG_MAX_MATCH_DATA, &max_match_data))
507
508
0
    if (STRING_IS_CHAIN_TAIL(matching_string))
509
0
    {
510
      // The matching string is the tail of the string chain. It must be
511
      // chained to some other string.
512
0
      assert(matching_string->chained_to != NULL);
513
514
      // Iterate over the list of unconfirmed matches of the preceding string
515
      // in the chain and update the chain_length field for each of them. This
516
      // is a recursive operation that will update the chain_length field for
517
      // every unconfirmed match in all the strings in the chain up to the head
518
      // of the chain.
519
0
      match =
520
0
          context->unconfirmed_matches[matching_string->chained_to->idx].head;
521
522
0
      while (match != NULL)
523
0
      {
524
0
        ending_offset = match->offset + match->match_length;
525
526
0
        if (ending_offset + matching_string->chain_gap_max >= match_offset &&
527
0
            ending_offset + matching_string->chain_gap_min <= match_offset)
528
0
        {
529
0
          _yr_scan_update_match_chain_length(
530
0
              context, matching_string->chained_to, match, 1);
531
0
        }
532
533
0
        match = match->next;
534
0
      }
535
536
0
      full_chain_length = 0;
537
0
      string = matching_string;
538
539
0
      while (string->chained_to != NULL)
540
0
      {
541
0
        full_chain_length++;
542
0
        string = string->chained_to;
543
0
      }
544
545
      // "string" points now to the head of the strings chain.
546
0
      match = context->unconfirmed_matches[string->idx].head;
547
548
      // Iterate over the list of unconfirmed matches of the head of the chain,
549
      // and move to the list of confirmed matches those with a chain_length
550
      // equal to full_chain_length, which means that the whole chain has been
551
      // confirmed to match.
552
0
      while (match != NULL)
553
0
      {
554
0
        next_match = match->next;
555
556
0
        if (match->chain_length == full_chain_length)
557
0
        {
558
0
          _yr_scan_remove_match_from_list(
559
0
              match, &context->unconfirmed_matches[string->idx]);
560
561
0
          uint64_t full_match_length =
562
0
              match_offset - match->offset + match_length;
563
564
0
          match->match_length = (int32_t) yr_min(
565
0
              full_match_length, (uint64_t) INT32_MAX);
566
567
0
          match->data_length = yr_min(
568
0
              match->match_length, (int32_t) max_match_data);
569
570
0
          if (match->data_length > 0)
571
0
          {
572
0
            match->data = yr_notebook_alloc(
573
0
                context->matches_notebook, match->data_length);
574
575
0
            if (match->data == NULL)
576
0
              return ERROR_INSUFFICIENT_MEMORY;
577
578
0
            memcpy(
579
0
                (void*) match->data,
580
0
                match_data - match_offset + match->offset,
581
0
                match->data_length);
582
0
          }
583
0
          else
584
0
          {
585
0
            match->data = NULL;
586
0
          }
587
588
          // Once a string is found, the rule containing that string is
589
          // required to be evaluated.
590
0
          yr_bitmask_set(context->required_eval, string->rule_idx);
591
592
0
          FAIL_ON_ERROR(_yr_scan_add_match_to_list(
593
0
              match, &context->matches[string->idx], false));
594
0
        }
595
596
0
        match = next_match;
597
0
      }
598
0
    }
599
0
    else  // It's a part of a chain, but not the tail.
600
0
    {
601
0
      new_match = yr_notebook_alloc(
602
0
          context->matches_notebook, sizeof(YR_MATCH));
603
604
0
      if (new_match == NULL)
605
0
        return ERROR_INSUFFICIENT_MEMORY;
606
607
0
      new_match->base = match_base;
608
0
      new_match->offset = match_offset;
609
0
      new_match->match_length = match_length;
610
0
      new_match->chain_length = 0;
611
0
      new_match->prev = NULL;
612
0
      new_match->next = NULL;
613
0
      new_match->is_private = STRING_IS_PRIVATE(matching_string);
614
0
      new_match->xor_key = xor_key;
615
616
      // A copy of the matching data is written to the matches_arena, the
617
      // amount of data copies is limited by YR_CONFIG_MAX_MATCH_DATA.
618
0
      new_match->data_length = yr_min(match_length, (int32_t) max_match_data);
619
620
0
      if (new_match->data_length > 0)
621
0
      {
622
0
        new_match->data = yr_notebook_alloc(
623
0
            context->matches_notebook, new_match->data_length);
624
625
0
        if (new_match->data == NULL)
626
0
          return ERROR_INSUFFICIENT_MEMORY;
627
628
0
        memcpy((void*) new_match->data, match_data, new_match->data_length);
629
0
      }
630
0
      else
631
0
      {
632
0
        new_match->data = NULL;
633
0
      }
634
635
      // Add the match to the list of unconfirmed matches because the string
636
      // is part of a chain but not its tail, so we can't be sure the this is
637
      // an actual match until finding the remaining parts of the chain.
638
0
      FAIL_ON_ERROR(_yr_scan_add_match_to_list(
639
0
          new_match,
640
0
          &context->unconfirmed_matches[matching_string->idx],
641
0
          false));
642
0
    }
643
0
  }
644
645
0
  return ERROR_SUCCESS;
646
0
}
647
648
static int _yr_scan_match_callback(
649
    const uint8_t* match_data,
650
    int32_t match_length,
651
    int flags,
652
    void* args)
653
0
{
654
0
  CALLBACK_ARGS* callback_args = (CALLBACK_ARGS*) args;
655
656
0
  YR_STRING* string = callback_args->string;
657
0
  YR_MATCH* new_match;
658
659
0
  int result = ERROR_SUCCESS;
660
661
0
  if (match_data < callback_args->data)
662
0
    return ERROR_INTERNAL_FATAL_ERROR;
663
664
0
  size_t match_offset = match_data - callback_args->data;
665
666
0
  YR_DEBUG_FPRINTF(
667
0
      2,
668
0
      stderr,
669
0
      "+ %s(match_data=%p match_length=%d) { //"
670
0
      " match_offset=%zu args->data=%p args->string.length=%u"
671
0
      " args->data_base=0x%" PRIx64 " args->data_size=%zu"
672
0
      " args->forward_matches=%'u\n",
673
0
      __FUNCTION__,
674
0
      match_data,
675
0
      match_length,
676
0
      match_offset,
677
0
      callback_args->data,
678
0
      callback_args->string->length,
679
0
      callback_args->data_base,
680
0
      callback_args->data_size,
681
0
      callback_args->forward_matches);
682
683
  // total match length is the sum of backward and forward matches.
684
0
  match_length += callback_args->forward_matches;
685
686
  // make sure that match fits into the data.
687
0
  if (match_length < 0 ||
688
0
      match_offset > callback_args->data_size ||
689
0
      (size_t) match_length > callback_args->data_size - match_offset)
690
0
  {
691
0
    return ERROR_INTERNAL_FATAL_ERROR;
692
0
  }
693
694
0
  if (callback_args->full_word)
695
0
  {
696
0
    if (flags & RE_FLAGS_WIDE)
697
0
    {
698
0
      if (match_offset >= 2 && *(match_data - 1) == 0 &&
699
0
          yr_isalnum(match_data - 2))
700
0
        goto _exit;  // return ERROR_SUCCESS;
701
702
0
      if (match_offset + match_length + 1 < callback_args->data_size &&
703
0
          *(match_data + match_length + 1) == 0 &&
704
0
          yr_isalnum(match_data + match_length))
705
0
        goto _exit;  // return ERROR_SUCCESS;
706
0
    }
707
0
    else
708
0
    {
709
0
      if (match_offset >= 1 && yr_isalnum(match_data - 1))
710
0
        goto _exit;  // return ERROR_SUCCESS;
711
712
0
      if (match_offset + match_length < callback_args->data_size &&
713
0
          yr_isalnum(match_data + match_length))
714
0
        goto _exit;  // return ERROR_SUCCESS;
715
0
    }
716
0
  }
717
718
0
  if (STRING_IS_CHAIN_PART(string))
719
0
  {
720
0
    result = _yr_scan_verify_chained_string_match(
721
0
        string,
722
0
        callback_args->context,
723
0
        match_data,
724
0
        callback_args->data_base,
725
0
        match_offset,
726
0
        match_length,
727
0
        callback_args->xor_key);
728
0
  }
729
0
  else
730
0
  {
731
0
    uint32_t max_match_data;
732
733
0
    FAIL_ON_ERROR(
734
0
        yr_get_configuration_uint32(YR_CONFIG_MAX_MATCH_DATA, &max_match_data));
735
736
0
    new_match = yr_notebook_alloc(
737
0
        callback_args->context->matches_notebook, sizeof(YR_MATCH));
738
739
0
    if (new_match == NULL)
740
0
    {
741
0
      result = ERROR_INSUFFICIENT_MEMORY;
742
0
      goto _exit;
743
0
    }
744
745
0
    new_match->data_length = yr_min(match_length, (int32_t) max_match_data);
746
747
0
    if (new_match->data_length > 0)
748
0
    {
749
0
      new_match->data = yr_notebook_alloc(
750
0
          callback_args->context->matches_notebook, new_match->data_length);
751
752
0
      if (new_match->data == NULL)
753
0
      {
754
0
        result = ERROR_INSUFFICIENT_MEMORY;
755
0
        goto _exit;
756
0
      }
757
758
0
      memcpy((void*) new_match->data, match_data, new_match->data_length);
759
0
    }
760
0
    else
761
0
    {
762
0
      new_match->data = NULL;
763
0
    }
764
765
0
    if (result == ERROR_SUCCESS)
766
0
    {
767
0
      new_match->base = callback_args->data_base;
768
0
      new_match->offset = match_offset;
769
0
      new_match->match_length = match_length;
770
0
      new_match->prev = NULL;
771
0
      new_match->next = NULL;
772
0
      new_match->is_private = STRING_IS_PRIVATE(string);
773
0
      new_match->xor_key = callback_args->xor_key;
774
775
0
      yr_bitmask_set(callback_args->context->required_eval, string->rule_idx);
776
777
0
      FAIL_ON_ERROR(_yr_scan_add_match_to_list(
778
0
          new_match,
779
0
          &callback_args->context->matches[string->idx],
780
0
          STRING_IS_GREEDY_REGEXP(string)));
781
0
    }
782
0
  }
783
784
0
_exit:;
785
786
0
  YR_DEBUG_FPRINTF(2, stderr, "} = %d // %s()\n", result, __FUNCTION__);
787
788
0
  return result;
789
0
}
790
791
typedef int (*RE_EXEC_FUNC)(
792
    YR_SCAN_CONTEXT* context,
793
    const uint8_t* code,
794
    const uint8_t* input,
795
    size_t input_forwards_size,
796
    size_t input_backwards_size,
797
    int flags,
798
    RE_MATCH_CALLBACK_FUNC callback,
799
    void* callback_args,
800
    int* matches);
801
802
static int _yr_scan_verify_re_match(
803
    YR_SCAN_CONTEXT* context,
804
    YR_AC_MATCH* ac_match,
805
    const uint8_t* data,
806
    size_t data_size,
807
    uint64_t data_base,
808
    size_t offset)
809
0
{
810
0
  YR_DEBUG_FPRINTF(
811
0
      2,
812
0
      stderr,
813
0
      "- %s(data=%p data_size=%zu data_base=0x%" PRIx64 " offset=%zu) {}\n",
814
0
      __FUNCTION__,
815
0
      data,
816
0
      data_size,
817
0
      data_base,
818
0
      offset);
819
820
0
  CALLBACK_ARGS callback_args;
821
0
  RE_EXEC_FUNC exec;
822
823
0
  int flags = 0;
824
825
0
  if (STRING_IS_GREEDY_REGEXP(ac_match->string))
826
0
    flags |= RE_FLAGS_GREEDY;
827
828
0
  if (STRING_IS_NO_CASE(ac_match->string))
829
0
    flags |= RE_FLAGS_NO_CASE;
830
831
0
  if (STRING_IS_DOT_ALL(ac_match->string))
832
0
    flags |= RE_FLAGS_DOT_ALL;
833
834
0
  if (STRING_IS_FAST_REGEXP(ac_match->string))
835
0
    exec = yr_re_fast_exec;
836
0
  else
837
0
    exec = yr_re_exec;
838
839
0
  callback_args.string = ac_match->string;
840
0
  callback_args.context = context;
841
0
  callback_args.data = data;
842
0
  callback_args.data_size = data_size;
843
0
  callback_args.data_base = data_base;
844
0
  callback_args.forward_matches = -1;
845
0
  callback_args.full_word = STRING_IS_FULL_WORD(ac_match->string);
846
  // xor modifier is not valid for RE but set it so we don't leak stack values.
847
0
  callback_args.xor_key = 0;
848
849
0
  if (STRING_IS_ASCII(ac_match->string) ||
850
      // The base64 and base64wide are not supported in regexps, but strings
851
      // with these modifiers are converted to a regexp with three
852
      // alternatives.
853
0
      STRING_IS_BASE64(ac_match->string) ||
854
0
      STRING_IS_BASE64_WIDE(ac_match->string))
855
0
  {
856
0
    FAIL_ON_ERROR(exec(
857
0
        context,
858
0
        ac_match->forward_code,
859
0
        data + offset,
860
0
        data_size - offset,
861
0
        offset,
862
0
        flags,
863
0
        NULL,
864
0
        NULL,
865
0
        &callback_args.forward_matches));
866
867
0
    if (callback_args.forward_matches != -1 && ac_match->backward_code != NULL)
868
0
    {
869
0
      FAIL_ON_ERROR(exec(
870
0
          context,
871
0
          ac_match->backward_code,
872
0
          data + offset,
873
0
          data_size - offset,
874
0
          offset,
875
0
          flags | RE_FLAGS_BACKWARDS | RE_FLAGS_EXHAUSTIVE,
876
0
          _yr_scan_match_callback,
877
0
          (void*) &callback_args,
878
0
          NULL));
879
0
    }
880
0
    else if (callback_args.forward_matches >= 0)
881
0
    {
882
0
      FAIL_ON_ERROR(
883
0
          _yr_scan_match_callback(data + offset, 0, flags, &callback_args));
884
0
    }
885
0
  }
886
887
0
  if (STRING_IS_WIDE(ac_match->string))
888
0
  {
889
0
    FAIL_ON_ERROR(exec(
890
0
        context,
891
0
        ac_match->forward_code,
892
0
        data + offset,
893
0
        data_size - offset,
894
0
        offset,
895
0
        flags | RE_FLAGS_WIDE,
896
0
        NULL,
897
0
        NULL,
898
0
        &callback_args.forward_matches));
899
900
0
    if (callback_args.forward_matches != -1 && ac_match->backward_code != NULL)
901
0
    {
902
0
      FAIL_ON_ERROR(exec(
903
0
          context,
904
0
          ac_match->backward_code,
905
0
          data + offset,
906
0
          data_size - offset,
907
0
          offset,
908
0
          flags | RE_FLAGS_WIDE | RE_FLAGS_BACKWARDS | RE_FLAGS_EXHAUSTIVE,
909
0
          _yr_scan_match_callback,
910
0
          (void*) &callback_args,
911
0
          NULL));
912
0
    }
913
0
    else if (callback_args.forward_matches >= 0)
914
0
    {
915
0
      FAIL_ON_ERROR(
916
0
          _yr_scan_match_callback(data + offset, 0, flags, &callback_args));
917
0
    }
918
0
  }
919
920
0
  return ERROR_SUCCESS;
921
0
}
922
923
static int _yr_scan_verify_literal_match(
924
    YR_SCAN_CONTEXT* context,
925
    YR_AC_MATCH* ac_match,
926
    const uint8_t* data,
927
    size_t data_size,
928
    uint64_t data_base,
929
    size_t offset)
930
0
{
931
0
  YR_DEBUG_FPRINTF(
932
0
      2,
933
0
      stderr,
934
0
      "- %s(data=%p data_size=%zu data_base=0x%" PRIx64 " offset=%zu) {}\n",
935
0
      __FUNCTION__,
936
0
      data,
937
0
      data_size,
938
0
      data_base,
939
0
      offset);
940
941
0
  int flags = 0;
942
0
  int forward_matches = 0;
943
0
  uint8_t xor_key = 0;
944
945
0
  CALLBACK_ARGS callback_args;
946
0
  YR_STRING* string = ac_match->string;
947
948
0
  if (STRING_FITS_IN_ATOM(string))
949
0
  {
950
0
    forward_matches = ac_match->backtrack;
951
0
    if (STRING_IS_XOR(string))
952
0
    {
953
0
      if (STRING_IS_WIDE(string))
954
0
      {
955
0
        _yr_scan_xor_wcompare(
956
0
            data + offset,
957
0
            data_size - offset,
958
0
            string->string,
959
0
            string->length,
960
0
            &xor_key);
961
0
      }
962
963
0
      if (STRING_IS_ASCII(string))
964
0
      {
965
0
        _yr_scan_xor_compare(
966
0
            data + offset,
967
0
            data_size - offset,
968
0
            string->string,
969
0
            string->length,
970
0
            &xor_key);
971
0
      }
972
0
    }
973
0
  }
974
0
  else if (STRING_IS_NO_CASE(string))
975
0
  {
976
0
    if (STRING_IS_ASCII(string))
977
0
    {
978
0
      forward_matches = _yr_scan_icompare(
979
0
          data + offset, data_size - offset, string->string, string->length);
980
0
    }
981
982
0
    if (STRING_IS_WIDE(string) && forward_matches == 0)
983
0
    {
984
0
      forward_matches = _yr_scan_wicompare(
985
0
          data + offset, data_size - offset, string->string, string->length);
986
0
    }
987
0
  }
988
0
  else
989
0
  {
990
0
    if (STRING_IS_ASCII(string))
991
0
    {
992
0
      forward_matches = _yr_scan_compare(
993
0
          data + offset, data_size - offset, string->string, string->length);
994
0
    }
995
996
0
    if (STRING_IS_WIDE(string) && forward_matches == 0)
997
0
    {
998
0
      forward_matches = _yr_scan_wcompare(
999
0
          data + offset, data_size - offset, string->string, string->length);
1000
0
    }
1001
1002
0
    if (STRING_IS_XOR(string) && forward_matches == 0)
1003
0
    {
1004
0
      if (STRING_IS_WIDE(string))
1005
0
      {
1006
0
        forward_matches = _yr_scan_xor_wcompare(
1007
0
            data + offset,
1008
0
            data_size - offset,
1009
0
            string->string,
1010
0
            string->length,
1011
0
            &xor_key);
1012
0
      }
1013
1014
0
      if (forward_matches == 0)
1015
0
      {
1016
0
        forward_matches = _yr_scan_xor_compare(
1017
0
            data + offset,
1018
0
            data_size - offset,
1019
0
            string->string,
1020
0
            string->length,
1021
0
            &xor_key);
1022
0
      }
1023
0
    }
1024
0
  }
1025
1026
0
  if (forward_matches == 0)
1027
0
    return ERROR_SUCCESS;
1028
1029
0
  if (forward_matches == string->length * 2)
1030
0
    flags |= RE_FLAGS_WIDE;
1031
1032
0
  if (STRING_IS_NO_CASE(string))
1033
0
    flags |= RE_FLAGS_NO_CASE;
1034
1035
0
  callback_args.context = context;
1036
0
  callback_args.string = string;
1037
0
  callback_args.data = data;
1038
0
  callback_args.data_size = data_size;
1039
0
  callback_args.data_base = data_base;
1040
0
  callback_args.forward_matches = forward_matches;
1041
0
  callback_args.full_word = STRING_IS_FULL_WORD(string);
1042
0
  callback_args.xor_key = xor_key;
1043
1044
0
  FAIL_ON_ERROR(
1045
0
      _yr_scan_match_callback(data + offset, 0, flags, &callback_args));
1046
1047
0
  return ERROR_SUCCESS;
1048
0
}
1049
1050
int yr_scan_verify_match(
1051
    YR_SCAN_CONTEXT* context,
1052
    YR_AC_MATCH* ac_match,
1053
    const uint8_t* data,
1054
    size_t data_size,
1055
    uint64_t data_base,
1056
    size_t offset)
1057
0
{
1058
0
  YR_DEBUG_FPRINTF(
1059
0
      2,
1060
0
      stderr,
1061
0
      "- %s(data=%p data_size=%zu data_base=0x%" PRIx64 " offset=%zu) {}\n",
1062
0
      __FUNCTION__,
1063
0
      data,
1064
0
      data_size,
1065
0
      data_base,
1066
0
      offset);
1067
1068
0
  YR_STRING* string = ac_match->string;
1069
0
  YR_CALLBACK_FUNC callback = context->callback;
1070
1071
0
  int result;
1072
1073
0
  if (data_size - offset <= 0)
1074
0
    return ERROR_SUCCESS;
1075
1076
0
  if (yr_bitmask_is_set(context->strings_temp_disabled, string->idx))
1077
0
    return ERROR_SUCCESS;
1078
1079
0
  if (context->flags & SCAN_FLAGS_FAST_MODE && STRING_IS_SINGLE_MATCH(string) &&
1080
0
      context->matches[string->idx].head != NULL)
1081
0
    return ERROR_SUCCESS;
1082
1083
0
  if (STRING_IS_FIXED_OFFSET(string) &&
1084
0
      string->fixed_offset != data_base + offset)
1085
0
    return ERROR_SUCCESS;
1086
1087
#ifdef YR_PROFILING_ENABLED
1088
  uint64_t start_time;
1089
  bool sample = context->profiling_info[string->rule_idx].atom_matches %
1090
                    YR_MATCH_VERIFICATION_PROFILING_RATE ==
1091
                0;
1092
1093
  if (sample)
1094
    start_time = yr_stopwatch_elapsed_ns(&context->stopwatch);
1095
#endif
1096
1097
0
  if (STRING_IS_LITERAL(string))
1098
0
  {
1099
0
    result = _yr_scan_verify_literal_match(
1100
0
        context, ac_match, data, data_size, data_base, offset);
1101
0
  }
1102
0
  else
1103
0
  {
1104
0
    result = _yr_scan_verify_re_match(
1105
0
        context, ac_match, data, data_size, data_base, offset);
1106
0
  }
1107
1108
  // If _yr_scan_verify_literal_match or _yr_scan_verify_re_match return
1109
  // ERROR_TOO_MANY_MATCHES call the callback with CALLBACK_MSG_TOO_MANY_MATCHES
1110
  // in order to ask what to do. If the callback returns CALLBACK_CONTINUE
1111
  // this error is ignored, if not, the error is propagated to the caller.
1112
0
  if (result == ERROR_TOO_MANY_MATCHES)
1113
0
  {
1114
0
    result = callback(
1115
0
        context,
1116
0
        CALLBACK_MSG_TOO_MANY_MATCHES,
1117
0
        (void*) string,
1118
0
        context->user_data);
1119
1120
0
    switch (result)
1121
0
    {
1122
0
    case CALLBACK_CONTINUE:
1123
0
      yr_bitmask_set(context->strings_temp_disabled, string->idx);
1124
0
      result = ERROR_SUCCESS;
1125
0
      break;
1126
1127
0
    default:
1128
0
      result = ERROR_TOO_MANY_MATCHES;
1129
0
      break;
1130
0
    }
1131
0
  }
1132
1133
#ifdef YR_PROFILING_ENABLED
1134
  if (sample)
1135
  {
1136
    uint64_t finish_time = yr_stopwatch_elapsed_ns(&context->stopwatch);
1137
    context->profiling_info[string->rule_idx].match_time +=
1138
        (finish_time - start_time);
1139
  }
1140
  context->profiling_info[string->rule_idx].atom_matches++;
1141
#endif
1142
1143
0
  if (result != ERROR_SUCCESS)
1144
0
    context->last_error_string = string;
1145
1146
0
  return result;
1147
0
}