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

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

**2.heap-buffer-overflow in htmlescape ../../programs/escape.c:46**
POC: https://github.com/linhlhq/research/blob/master/PoCs/libreDWG_7b9cb829/id:000016%2Csig:06%2Csrc:000000%2Cop:flip1%2Cpos:125355
ASAN says:
```
==4460==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000044cc6 at pc 0x7f2ccba2f66e bp 0x7ffd5b9c04e0 sp 0x7ffd5b9bfc88
READ of size 22 at 0x603000044cc6 thread T0
    #0 0x7f2ccba2f66d  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x5166d)
    #1 0x561be92280e7 in strcat /usr/include/x86_64-linux-gnu/bits/string_fortified.h:128
    #2 0x561be92280e7 in htmlescape ../../programs/escape.c:46
    #3 0x561be9225b38 in output_BLOCK_HEADER ../../programs/dwg2SVG.c:361
    #4 0x561be9223544 in output_SVG ../../programs/dwg2SVG.c:418
    #5 0x561be9223544 in main ../../programs/dwg2SVG.c:525
    #6 0x7f2ccb270b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #7 0x561be9223d19 in _start (/home/user/linhlhq/libredwg/asan_build/programs/dwg2SVG+0x27ad19)

0x603000044cc6 is located 0 bytes to the right of 22-byte region [0x603000044cb0,0x603000044cc6)
allocated by thread T0 here:
    #0 0x7f2ccbabcb50 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
    #1 0x561be9227de0 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:
  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 fd fd fd fa fa fa
=>0x0c0680000990: fd fd fd fa fa fa 00 00[06]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
  0x0c06800009e0: 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
==4460==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