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

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

**5.heap-buffer-overflow in read_2004_section_appinfo ../../src/decode.c:2842**
POC: https://github.com/linhlhq/research/blob/master/PoCs/libreDWG_4c26d37/id:000091%2Csig:06%2Csrc:005906%2B004569%2Cop:splice%2Crep:64

```
=================================================================
==8956==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x62c0000070c0 at pc 0x7ff0838d6733 bp 0x7ffd5c5ceb70 sp 0x7ffd5c5ce318
READ of size 32768 at 0x62c0000070c0 thread T0
    #0 0x7ff0838d6732  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x79732)
    #1 0x5599a2abd7b6 in memcpy /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34
    #2 0x5599a2abd7b6 in read_2004_compressed_section ../../src/decode.c:2417
    #3 0x5599a3083a9b in read_2004_section_appinfo ../../src/decode.c:2842
    #4 0x5599a3083a9b in decode_R2004 ../../src/decode.c:3359
    #5 0x5599a309308d in dwg_decode ../../src/decode.c:246
    #6 0x5599a2a25fae in dwg_read_file ../../src/dwg.c:211
    #7 0x5599a2a249d0 in get_bmp ../../programs/dwgbmp.c:111
    #8 0x5599a2a24066 in main ../../programs/dwgbmp.c:280
    #9 0x7ff0830efb96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #10 0x5599a2a246c9 in _start (/home/user/linhlhq/libredwg/asan_build/programs/dwgbmp+0x28f6c9)

0x62c0000070c0 is located 0 bytes to the right of 28352-byte region [0x62c000000200,0x62c0000070c0)
allocated by thread T0 here:
    #0 0x7ff08393bd38 in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xded38)
    #1 0x5599a2a25f41 in dat_read_file ../../src/dwg.c:74
    #2 0x5599a2a25f41 in dwg_read_file ../../src/dwg.c:204

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x79732)
Shadow bytes around the buggy address:
  0x0c587fff8dc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c587fff8dd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c587fff8de0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c587fff8df0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c587fff8e00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c587fff8e10: 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa fa fa
  0x0c587fff8e20: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c587fff8e30: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c587fff8e40: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c587fff8e50: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c587fff8e60: 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
==8956==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