Project: libredwg (libredwg/libredwg), language: c++
Vulnerability instance: libredwg.cve-2020-21841
Sanitizer: address

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/LibreDWG/libredwg/issues/188#issuecomment-574493775
## Description:
Issue: LibreDWG/libredwg#188
Title: Several bugs found by fuzzing
Referenced comment by linhlhq on 2020-01-15 04:45:02+00:00:

**15. heap-buffer-overflow in bit_read_B ../../src/bits.c:135**
POC: https://github.com/linhlhq/research/blob/master/PoCs/libreDWG_4c26d37/id:000008%2Csig:06%2Csrc:000009%2Cop:flip1%2Cpos:27724
```
=================================================================
==6101==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60c000000998 at pc 0x55fc249a4769 bp 0x7ffe70852d70 sp 0x7ffe70852d60
READ of size 1 at 0x60c000000998 thread T0
    #0 0x55fc249a4768 in bit_read_B ../../src/bits.c:135
    #1 0x55fc2502c10f in section_string_stream ../../src/decode_r2007.c:1372
    #2 0x55fc24a1f8b1 in read_2004_section_classes ../../src/decode.c:2488
    #3 0x55fc24fdc0d0 in decode_R2004 ../../src/decode.c:3353
    #4 0x55fc24fee08d in dwg_decode ../../src/decode.c:246
    #5 0x55fc24980fae in dwg_read_file ../../src/dwg.c:211
    #6 0x55fc2497f9d0 in get_bmp ../../programs/dwgbmp.c:111
    #7 0x55fc2497f066 in main ../../programs/dwgbmp.c:280
    #8 0x7fa7b531fb96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #9 0x55fc2497f6c9 in _start (/home/user/linhlhq/libredwg/asan_build/programs/dwgbmp+0x28f6c9)

Address 0x60c000000998 is a wild pointer.
SUMMARY: AddressSanitizer: heap-buffer-overflow ../../src/bits.c:135 in bit_read_B
Shadow bytes around the buggy address:
  0x0c187fff80e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff80f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8110: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8120: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x0c187fff8130: fa fa fa[fa]fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8140: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8150: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8160: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8170: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8180: 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
==6101==ABORTING
```

Build script:
#!/bin/bash -eu
# Minimized build script with only core build commands
set -eu
sh ./autogen.sh
./configure --disable-shared
make -j$(nproc)

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y autoconf libtool texinfo
RUN git clone https://github.com/libredwg/libredwg libredwg
RUN git -C libredwg checkout d5a51c01023d5585ddf3dd17c83dbb3c81efc97e
WORKDIR $SRC/libredwg
COPY build.sh $SRC/

Additional files:
filename: fuzz_dwg_decode.options
content: [libfuzzer]
detect_leaks=0