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

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

**3.heap-buffer-overflow in htmlescape ../../programs/escape.c:51**
POC: https://github.com/linhlhq/research/blob/master/PoCs/libreDWG_7b9cb829/id:000015%2Csig:06%2Csrc:000000%2Cop:flip1%2Cpos:46436
ASAN says:
```
==1959==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000044c67 at pc 0x7f57a7a3f66e bp 0x7fff31617330 sp 0x7fff31616ad8
READ of size 16 at 0x603000044c67 thread T0
    #0 0x7f57a7a3f66d  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x5166d)
    #1 0x561c57375e53 in strcat /usr/include/x86_64-linux-gnu/bits/string_fortified.h:128
    #2 0x561c57375e53 in htmlescape ../../programs/escape.c:51
    #3 0x561c57373bb5 in output_TEXT ../../programs/dwg2SVG.c:113
    #4 0x561c57373bb5 in output_object ../../programs/dwg2SVG.c:312
    #5 0x561c57373bb5 in output_BLOCK_HEADER ../../programs/dwg2SVG.c:371
    #6 0x561c57371477 in output_SVG ../../programs/dwg2SVG.c:411
    #7 0x561c57371477 in main ../../programs/dwg2SVG.c:525
    #8 0x7f57a7280b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #9 0x561c57371d19 in _start (/home/user/linhlhq/libredwg/asan_build/programs/dwg2SVG+0x27ad19)

0x603000044c67 is located 0 bytes to the right of 23-byte region [0x603000044c50,0x603000044c67)
allocated by thread T0 here:
    #0 0x7f57a7accb50 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
    #1 0x561c57375de0 in htmlescape ../../programs/escape.c:30

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x5166d)
Shadow bytes around the buggy address:
  0x0c0680000930: 00 00 00 00 fa fa 00 00 00 00 fa fa 00 00 00 fa
  0x0c0680000940: fa fa 00 00 00 00 fa fa 00 00 00 00 fa fa 00 00
  0x0c0680000950: 00 00 fa fa 00 00 00 fa fa fa 00 00 00 00 fa fa
  0x0c0680000960: 00 00 00 00 fa fa 00 00 00 00 fa fa 00 00 00 fa
  0x0c0680000970: fa fa 00 00 00 00 fa fa 00 00 00 00 fa fa 00 00
=>0x0c0680000980: 00 00 fa fa fd fd fd fa fa fa 00 00[07]fa fa fa
  0x0c0680000990: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c06800009a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c06800009b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c06800009c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c06800009d0: 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
==1959==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