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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/LibreDWG/libredwg/issues/182#issuecomment-572891083
## Description:
Issue: LibreDWG/libredwg#182
Title: Several bugs found by fuzzing
Referenced comment by linhlhq on 2020-01-10 06:18:13+00:00:

**8.heap-buffer-overflow in htmlwescape ../../programs/escape.c:97**
POC: https://github.com/linhlhq/research/blob/master/PoCs/libreDWG_7b9cb829/id:000099%2Csig:06%2Csrc:005125%2B003017%2Cop:splice%2Crep:8
ASAN says:
```
==682==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6040000019fa at pc 0x7ff79912866e bp 0x7ffdca846b10 sp 0x7ffdca8462b8
READ of size 28 at 0x6040000019fa thread T0
    #0 0x7ff79912866d  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x5166d)
    #1 0x5586562889e7 in strcat /usr/include/x86_64-linux-gnu/bits/string_fortified.h:128
    #2 0x5586562889e7 in htmlwescape ../../programs/escape.c:97
    #3 0x5586562848de in output_TEXT ../../programs/dwg2SVG.c:111
    #4 0x5586562848de in output_object ../../programs/dwg2SVG.c:312
    #5 0x5586562848de in output_BLOCK_HEADER ../../programs/dwg2SVG.c:371
    #6 0x558656283477 in output_SVG ../../programs/dwg2SVG.c:411
    #7 0x558656283477 in main ../../programs/dwg2SVG.c:525
    #8 0x7ff798969b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #9 0x558656283d19 in _start (/home/user/linhlhq/libredwg/asan_build/programs/dwg2SVG+0x27ad19)

0x6040000019fa is located 0 bytes to the right of 42-byte region [0x6040000019d0,0x6040000019fa)
allocated by thread T0 here:
    #0 0x7ff7991b5b50 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
    #1 0x5586562883f2 in htmlwescape ../../programs/escape.c:79

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x5166d)
Shadow bytes around the buggy address:
  0x0c087fff82e0: fa fa 00 00 00 00 00 fa fa fa 00 00 00 00 00 00
  0x0c087fff82f0: fa fa fd fd fd fd fd fa fa fa 00 00 00 00 00 fa
  0x0c087fff8300: fa fa 00 00 00 00 00 fa fa fa fd fd fd fd fd fa
  0x0c087fff8310: fa fa 00 00 00 00 00 00 fa fa 00 00 00 00 00 fa
  0x0c087fff8320: fa fa 00 00 00 00 00 fa fa fa fd fd fd fd fd fa
=>0x0c087fff8330: fa fa fd fd fd fd fd fa fa fa 00 00 00 00 00[02]
  0x0c087fff8340: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8350: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8360: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8370: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8380: 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
==682==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 7b9cb829f93d1c1f67584345ed65ea830de8bdda
WORKDIR $SRC/libredwg
COPY build.sh $SRC/

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