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

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

**12. heap-buffer-overflow in bit_search_sentinel ../../src/bits.c:1985**
POC: https://github.com/linhlhq/research/blob/master/PoCs/libreDWG_4c26d37/id:000009%2Csig:06%2Csrc:000009%2Cop:flip1%2Cpos:27725

```
=================================================================
==30593==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000003f1 at pc 0x562e9b28f678 bp 0x7ffe7e543550 sp 0x7ffe7e543540
READ of size 1 at 0x6020000003f1 thread T0
    #0 0x562e9b28f677 in bit_search_sentinel ../../src/bits.c:1985
    #1 0x562e9b2bf2c4 in read_2004_section_classes ../../src/decode.c:2449
    #2 0x562e9b87e0d0 in decode_R2004 ../../src/decode.c:3353
    #3 0x562e9b89008d in dwg_decode ../../src/decode.c:246
    #4 0x562e9b222fae in dwg_read_file ../../src/dwg.c:211
    #5 0x562e9b2219d0 in get_bmp ../../programs/dwgbmp.c:111
    #6 0x562e9b221066 in main ../../programs/dwgbmp.c:280
    #7 0x7f561af17b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #8 0x562e9b2216c9 in _start (/home/user/linhlhq/libredwg/asan_build/programs/dwgbmp+0x28f6c9)

0x6020000003f1 is located 0 bytes to the right of 1-byte region [0x6020000003f0,0x6020000003f1)
allocated by thread T0 here:
    #0 0x7f561b763d38 in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xded38)
    #1 0x562e9b2b9f25 in read_2004_compressed_section ../../src/decode.c:2321
    #2 0x562e9bd644b2  (/home/user/linhlhq/libredwg/asan_build/programs/dwgbmp+0xdd24b2)

SUMMARY: AddressSanitizer: heap-buffer-overflow ../../src/bits.c:1985 in bit_search_sentinel
Shadow bytes around the buggy address:
  0x0c047fff8020: fa fa 00 00 fa fa 00 fa fa fa 00 fa fa fa 00 fa
  0x0c047fff8030: fa fa 00 fa fa fa 00 fa fa fa 00 fa fa fa 04 fa
  0x0c047fff8040: fa fa 02 fa fa fa 02 fa fa fa 02 fa fa fa 04 fa
  0x0c047fff8050: fa fa 02 fa fa fa 02 fa fa fa 02 fa fa fa 02 fa
  0x0c047fff8060: fa fa 02 fa fa fa 02 fa fa fa 02 fa fa fa 00 02
=>0x0c047fff8070: fa fa 02 fa fa fa 02 fa fa fa 02 fa fa fa[01]fa
  0x0c047fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff80a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff80b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff80c0: 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
==30593==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