Project: yara (virustotal/yara), language: c++
Vulnerability instance: yara.cve-2023-40857
Sanitizer: address

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/VirusTotal/yara/issues/1945
## Description:
Issue: VirusTotal/yara#1945
Title: heap-buffer-overflow libyara/exec.c:1426 in yr_execute_code
State: closed
Created by: shinibufa
Created at: 2023-08-10 08:11:16+00:00
Labels: bug

Issue Body:
**Describe the bug**
AddressSanitizer: heap-buffer-overflow libyara/exec.c:1426 in yr_execute_code

**To Reproduce**
Steps to reproduce the behavior:
1, compile yara with asan: ./configure CC=gcc CXX=g++ CFLAGS="-g -O0 -fsanitize=address" CXXFLAGS="-g -O0 -fsanitize=address" LDFLAGS="-g -O0 -fsanitize=address"
2, run this command: ./yara -C [PoC](https://github.com/shinibufa/crash_seeds/blob/main/yara/HBO_1.yarac) [binFile](https://github.com/shinibufa/crash_seeds/blob/main/yara/hello_gmon)

**Please complete the following information:**
 - OS: ubuntu 20.04
 - YARA version:  4.3.2

**Additional context**
ASAN reprot:
=================================================================
==1855158==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x604000000248 at pc 0x7f168933dfaf bp 0x7ffd229d5530 sp 0x7ffd229d5520
READ of size 8 at 0x604000000248 thread T0
    #0 0x7f168933dfae in yr_execute_code libyara/exec.c:1426
    #1 0x7f16893a1cd8 in yr_scanner_scan_mem_blocks libyara/scanner.c:526
    #2 0x7f16893a27a0 in yr_scanner_scan_mem libyara/scanner.c:670
    #3 0x7f16893a2b3e in yr_scanner_scan_fd libyara/scanner.c:706
    #4 0x55aa2e6ed11a in scan_file cli/yara.c:736
    #5 0x55aa2e6f1444 in main cli/yara.c:1654
    #6 0x7f1688c22082 in __libc_start_main ../csu/libc-start.c:308
    #7 0x55aa2e6e9ced in _start (/home/root/latestFiles/yara-4.3.2/.libs/yara+0x7ced)

0x604000000248 is located 8 bytes to the left of 48-byte region [0x604000000250,0x604000000280)
allocated by thread T0 here:
    #0 0x7f1689535a06 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:153
    #1 0x7f168936db41 in yr_calloc libyara/mem.c:127
    #2 0x7f168939fe48 in yr_scanner_create libyara/scanner.c:242
    #3 0x55aa2e6f12af in main cli/yara.c:1640
    #4 0x7f1688c22082 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: heap-buffer-overflow libyara/exec.c:1426 in yr_execute_code
Shadow bytes around the buggy address:
  0x0c087fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c087fff8000: fa fa fd fd fd fd fd fa fa fa 00 00 00 00 00 fa
  0x0c087fff8010: fa fa fd fd fd fd fd fa fa fa 00 00 00 00 00 fa
  0x0c087fff8020: fa fa fd fd fd fd fd fa fa fa 00 00 00 00 00 fa
  0x0c087fff8030: fa fa fd fd fd fd fd fa fa fa 00 00 00 00 00 fa
=>0x0c087fff8040: fa fa 00 00 00 00 00 00 fa[fa]00 00 00 00 00 00
  0x0c087fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==1855158==ABORTING


Comments:
Comment by plusvic on 2023-08-14 20:37:14+00:00:
Closing as this is crash is the result of fuzzing compiled rules. See #1948 for more context.

Build script:
#!/bin/bash -eu
# Minimized build script with only core build commands
set -eu
./bootstrap.sh
./configure --enable-macho --enable-debug --enable-dex --enable-dotnet --without-crypto
make clean -j$(nproc)
make -j$(nproc) all
make install -j$(nproc)

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y automake autoconf make libtool pkg-config bison flex
RUN git clone https://github.com/virustotal/yara yara
RUN git -C yara checkout d1ff3ecc465d7ed882943fd1278dd8981b704f02
WORKDIR $SRC/yara
COPY build.sh $SRC/

Additional files: