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

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

**7.heap-buffer-overflow in htmlescape ../../programs/escape.c:48**
POC: https://github.com/linhlhq/research/blob/master/PoCs/libreDWG_7b9cb829/id:000144%2Csig:06%2Csrc:005649%2Cop:havoc%2Crep:2
ASAN says:
```
==9632==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x604000000abd at pc 0x7f1b8c9f066e bp 0x7ffe890ea740 sp 0x7ffe890e9ee8
READ of size 10 at 0x604000000abd thread T0
    #0 0x7f1b8c9f066d  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x5166d)
    #1 0x55ff72e70067 in strcat /usr/include/x86_64-linux-gnu/bits/string_fortified.h:128
    #2 0x55ff72e70067 in htmlescape ../../programs/escape.c:48
    #3 0x55ff72e6dbb5 in output_TEXT ../../programs/dwg2SVG.c:113
    #4 0x55ff72e6dbb5 in output_object ../../programs/dwg2SVG.c:312
    #5 0x55ff72e6dbb5 in output_BLOCK_HEADER ../../programs/dwg2SVG.c:371
    #6 0x55ff72e6b544 in output_SVG ../../programs/dwg2SVG.c:418
    #7 0x55ff72e6b544 in main ../../programs/dwg2SVG.c:525
    #8 0x7f1b8c231b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #9 0x55ff72e6bd19 in _start (/home/user/linhlhq/libredwg/asan_build/programs/dwg2SVG+0x27ad19)

0x604000000abd is located 0 bytes to the right of 45-byte region [0x604000000a90,0x604000000abd)
allocated by thread T0 here:
    #0 0x7f1b8ca7df40 in realloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdef40)
    #1 0x55ff72e6ffa1 in htmlescape ../../programs/escape.c:39

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x5166d)
Shadow bytes around the buggy address:
  0x0c087fff8100: fa fa fd fd fd fd fd fa fa fa 00 00 00 00 00 fa
  0x0c087fff8110: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fa
  0x0c087fff8120: fa fa 00 00 00 00 00 fa fa fa 00 00 00 00 00 fa
  0x0c087fff8130: fa fa 00 00 00 00 00 fa fa fa 00 00 00 00 05 fa
  0x0c087fff8140: fa fa 00 00 00 00 00 00 fa fa fd fd fd fd fd fa
=>0x0c087fff8150: fa fa 00 00 00 00 00[05]fa fa fa fa fa fa fa fa
  0x0c087fff8160: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8170: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8190: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff81a0: 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
==9632==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