A heap buffer overflow was discovered in copy_compressed_bytes in decode_r2007.c in dwgread before 0.12.4 via a crafted dwg file.
cpe:2.3:a:gnu:libredwg:*:*:*:*:*:*:*:*
030087DE-C04A-4AA0-864A-FD276745EB75
0.12.4
OR
## Affected version 

[the latest commit](https://github.com/LibreDWG/libredwg/commit/4c210bbae04957b7d4aba55aee8dca42337f8d47) and 0.12.4

## What's the problem?


A heap buffer overflow was discovered in copy_compressed_bytes in decode_r2007.c:332.

ASAN report:

```
./dwgread ./tests_64205
ERROR: Section[7]->pages[0] overflow
Warning: Failed to find section_info[1]
ERROR: Failed to read header section
Warning: Failed to find section_info[3]
ERROR: Failed to read class section
Warning: Failed to find section_info[7]
ERROR: Failed to read objects section
Warning: Failed to find section_info[2]
Warning: thumbnail.size mismatch: 185216 != 163880
ERROR: Failed to find page 222
=================================================================
==9485==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000002e3 at pc 0x000000ca5f2b bp 0x7ffdbc6926d0 sp 0x7ffdbc6926c8
WRITE of size 8 at 0x6020000002e3 thread T0
    #0 0xca5f2a in memcpy /usr/include/x86_64-linux-gnu/bits/string3.h:53:10
    #1 0xca5f2a in copy_compressed_bytes /root/fuzz/libredwg/src/decode_r2007.c:332:7
    #2 0xca4195 in decompress_r2007 /root/fuzz/libredwg/src/decode_r2007.c:523:7
    #3 0xcaa7fe in read_data_page /root/fuzz/libredwg/src/decode_r2007.c:728:13
    #4 0xcaa319 in read_data_section /root/fuzz/libredwg/src/decode_r2007.c:811:19
    #5 0xc9be6b in read_2007_section_revhistory /root/fuzz/libredwg/src/decode_r2007.c:1945:11
    #6 0xc83d65 in read_r2007_meta_data /root/fuzz/libredwg/src/decode_r2007.c:2387:12
    #7 0x4f9fcc in decode_R2007 /root/fuzz/libredwg/src/decode.c:3836:11
    #8 0x4e49ab in dwg_decode /root/fuzz/libredwg/src/decode.c:248:29
    #9 0x4c75cc in dwg_read_file /root/fuzz/libredwg/src/dwg.c:254:11
    #10 0x4c5d47 in main /root/fuzz/libredwg/programs/dwgread.c
    #11 0x7f581684583f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #12 0x41ba58 in _start (/root/fuzz/pocs/dwgread+0x41ba58)

0x6020000002e3 is located 3 bytes to the right of 16-byte region [0x6020000002d0,0x6020000002e0)
allocated by thread T0 here:
    #0 0x495a22 in calloc (/root/fuzz/pocs/dwgread+0x495a22)
    #1 0xca9e98 in read_data_section /root/fuzz/libredwg/src/decode_r2007.c:774:26

SUMMARY: AddressSanitizer: heap-buffer-overflow /usr/include/x86_64-linux-gnu/bits/string3.h:53:10 in memcpy
Shadow bytes around the buggy address:
  0x0c047fff8000: fa fa 00 00 fa fa 00 fa fa fa 00 fa fa fa 00 fa
  0x0c047fff8010: fa fa 00 fa fa fa 00 fa fa fa 00 fa fa fa 06 fa
  0x0c047fff8020: fa fa 04 fa fa fa 04 fa fa fa 04 fa fa fa 04 fa
  0x0c047fff8030: fa fa 04 fa fa fa 00 06 fa fa 04 fa fa fa 04 fa
  0x0c047fff8040: fa fa 00 00 fa fa 00 00 fa fa 02 fa fa fa 02 fa
=>0x0c047fff8050: fa fa 02 fa fa fa 02 fa fa fa 00 00[fa]fa fa fa
  0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff80a0: 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
  Shadow gap:              cc
==9485==ABORTING
```


## Compile command

```
CC="clang" CFLAGS="-O1 -g -fsanitize=address" ./configure --enable-release --disable-shared && make
```

## How can we reproduce the issue?

```
unzip tests_64205.zip
dwgread ./tests_64205
```
POC file :  
[tests_64205.zip](https://github.com/LibreDWG/libredwg/files/6606900/tests_64205.zip)



Heap-buffer-overflow in copy_compressed_bytes in decode_r2007.c:332
This is a GitHub Issue
repo:libredwg
owner:LibreDWG
Title : Heap-buffer-overflow in copy_compressed_bytes in decode_r2007.c:332
Issue date:

--- start body ---
## Affected version 

[the latest commit](https://github.com/LibreDWG/libredwg/commit/4c210bbae04957b7d4aba55aee8dca42337f8d47) and 0.12.4

## What's the problem?


A heap buffer overflow was discovered in copy_compressed_bytes in decode_r2007.c:332.

ASAN report:

```
./dwgread ./tests_64205
ERROR: Section[7]->pages[0] overflow
Warning: Failed to find section_info[1]
ERROR: Failed to read header section
Warning: Failed to find section_info[3]
ERROR: Failed to read class section
Warning: Failed to find section_info[7]
ERROR: Failed to read objects section
Warning: Failed to find section_info[2]
Warning: thumbnail.size mismatch: 185216 != 163880
ERROR: Failed to find page 222
=================================================================
==9485==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000002e3 at pc 0x000000ca5f2b bp 0x7ffdbc6926d0 sp 0x7ffdbc6926c8
WRITE of size 8 at 0x6020000002e3 thread T0
    #0 0xca5f2a in memcpy /usr/include/x86_64-linux-gnu/bits/string3.h:53:10
    #1 0xca5f2a in copy_compressed_bytes /root/fuzz/libredwg/src/decode_r2007.c:332:7
    #2 0xca4195 in decompress_r2007 /root/fuzz/libredwg/src/decode_r2007.c:523:7
    #3 0xcaa7fe in read_data_page /root/fuzz/libredwg/src/decode_r2007.c:728:13
    #4 0xcaa319 in read_data_section /root/fuzz/libredwg/src/decode_r2007.c:811:19
    #5 0xc9be6b in read_2007_section_revhistory /root/fuzz/libredwg/src/decode_r2007.c:1945:11
    #6 0xc83d65 in read_r2007_meta_data /root/fuzz/libredwg/src/decode_r2007.c:2387:12
    #7 0x4f9fcc in decode_R2007 /root/fuzz/libredwg/src/decode.c:3836:11
    #8 0x4e49ab in dwg_decode /root/fuzz/libredwg/src/decode.c:248:29
    #9 0x4c75cc in dwg_read_file /root/fuzz/libredwg/src/dwg.c:254:11
    #10 0x4c5d47 in main /root/fuzz/libredwg/programs/dwgread.c
    #11 0x7f581684583f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #12 0x41ba58 in _start (/root/fuzz/pocs/dwgread+0x41ba58)

0x6020000002e3 is located 3 bytes to the right of 16-byte region [0x6020000002d0,0x6020000002e0)
allocated by thread T0 here:
    #0 0x495a22 in calloc (/root/fuzz/pocs/dwgread+0x495a22)
    #1 0xca9e98 in read_data_section /root/fuzz/libredwg/src/decode_r2007.c:774:26

SUMMARY: AddressSanitizer: heap-buffer-overflow /usr/include/x86_64-linux-gnu/bits/string3.h:53:10 in memcpy
Shadow bytes around the buggy address:
  0x0c047fff8000: fa fa 00 00 fa fa 00 fa fa fa 00 fa fa fa 00 fa
  0x0c047fff8010: fa fa 00 fa fa fa 00 fa fa fa 00 fa fa fa 06 fa
  0x0c047fff8020: fa fa 04 fa fa fa 04 fa fa fa 04 fa fa fa 04 fa
  0x0c047fff8030: fa fa 04 fa fa fa 00 06 fa fa 04 fa fa fa 04 fa
  0x0c047fff8040: fa fa 00 00 fa fa 00 00 fa fa 02 fa fa fa 02 fa
=>0x0c047fff8050: fa fa 02 fa fa fa 02 fa fa fa 00 00[fa]fa fa fa
  0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff80a0: 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
  Shadow gap:              cc
==9485==ABORTING
```


## Compile command

```
CC="clang" CFLAGS="-O1 -g -fsanitize=address" ./configure --enable-release --disable-shared && make
```

## How can we reproduce the issue?

```
unzip tests_64205.zip
dwgread ./tests_64205
```
POC file :  
[tests_64205.zip](https://github.com/LibreDWG/libredwg/files/6606900/tests_64205.zip)



--- end body ---

comments :
--- start comments ---

--- end comments ---