## Affected version 

the latest commit: https://github.com/LibreDWG/libredwg/tree/45d2a290c65ed691be0901ba2b2ef51044e07a16 and 0.12.4

## What's the problem?


A heap buffer overflow was discovered in dwg_convert_SAB_to_SAT1 in out_dxf.c:2230.

ASAN report:

```
==3850==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x629000068200 at pc 0x00000043a150 bp 0x7fffffffc6e0 sp 0x7fffffffbe78
WRITE of size 22 at 0x629000068200 thread T0
    #0 0x43a14f in vsprintf /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:1670:1
    #1 0x43b209 in __sprintf_chk /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:1718:1
    #2 0xe3f2cd in dwg_convert_SAB_to_SAT1 /src/libredwg/src/out_dxf.c:2230:17
    #3 0xeebd9e in dxf_3dsolid /src/libredwg/src/out_dxf.c:2334:20
    #4 0xeecb01 in dwg_dxf__3DSOLID_private /src/libredwg/src/./dwg.spec:2370:3
    #5 0xea23e2 in dwg_dxf__3DSOLID /src/libredwg/src/./dwg.spec:2369:1
    #6 0xe9677d in dwg_dxf_object /src/libredwg/src/out_dxf.c:2690:14
    #7 0xe6817c in dxf_entities_write /src/libredwg/src/out_dxf.c:3532:16
    #8 0xe41b4c in dwg_write_dxf /src/libredwg/src/out_dxf.c:3682:7
    #9 0x4c864b in main /src/libredwg/programs/dxfwrite.c:451:11
    #10 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #11 0x41bc98 in _start (/mnt/disk/out/libredwg/programs_asan/dxfwrite+0x41bc98)

0x629000068200 is located 0 bytes to the right of 16384-byte region [0x629000064200,0x629000068200)
allocated by thread T0 here:
    #0 0x496b43 in realloc /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x4d7d12 in bit_chain_alloc /src/libredwg/src/bits.c:3026:30
    #2 0xe3f0ec in dwg_convert_SAB_to_SAT1 /src/libredwg/src/out_dxf.c:2196:15
    #3 0xeebd9e in dxf_3dsolid /src/libredwg/src/out_dxf.c:2334:20
    #4 0xeecb01 in dwg_dxf__3DSOLID_private /src/libredwg/src/./dwg.spec:2370:3
    #5 0xea23e2 in dwg_dxf__3DSOLID /src/libredwg/src/./dwg.spec:2369:1
    #6 0xe9677d in dwg_dxf_object /src/libredwg/src/out_dxf.c:2690:14
    #7 0xe6817c in dxf_entities_write /src/libredwg/src/out_dxf.c:3532:16
    #8 0xe41b4c in dwg_write_dxf /src/libredwg/src/out_dxf.c:3682:7
    #9 0x4c864b in main /src/libredwg/programs/dxfwrite.c:451:11
    #10 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291

SUMMARY: AddressSanitizer: heap-buffer-overflow /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:1670:1 in vsprintf
Shadow bytes around the buggy address:
  0x0c5280004ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c5280005000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c5280005010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c5280005020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c5280005030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c5280005040:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c5280005050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c5280005060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c5280005070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c5280005080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c5280005090: 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
==3850==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

```
unzip tests_64265.zip
dxfwrite -I DWG -o /dev/null -y ./tests_64265
```
POC file :  
[tests_64265.zip](https://github.com/LibreDWG/libredwg/files/6652592/tests_64265.zip)

Heap-buffer-overflow in dwg_convert_SAB_to_SAT1 in out_dxf.c
This is a GitHub Issue
repo:libredwg
owner:LibreDWG
Title : Heap-buffer-overflow in dwg_convert_SAB_to_SAT1 in out_dxf.c
Issue date:

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

the latest commit: https://github.com/LibreDWG/libredwg/tree/45d2a290c65ed691be0901ba2b2ef51044e07a16 and 0.12.4

## What's the problem?


A heap buffer overflow was discovered in dwg_convert_SAB_to_SAT1 in out_dxf.c:2230.

ASAN report:

```
==3850==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x629000068200 at pc 0x00000043a150 bp 0x7fffffffc6e0 sp 0x7fffffffbe78
WRITE of size 22 at 0x629000068200 thread T0
    #0 0x43a14f in vsprintf /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:1670:1
    #1 0x43b209 in __sprintf_chk /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:1718:1
    #2 0xe3f2cd in dwg_convert_SAB_to_SAT1 /src/libredwg/src/out_dxf.c:2230:17
    #3 0xeebd9e in dxf_3dsolid /src/libredwg/src/out_dxf.c:2334:20
    #4 0xeecb01 in dwg_dxf__3DSOLID_private /src/libredwg/src/./dwg.spec:2370:3
    #5 0xea23e2 in dwg_dxf__3DSOLID /src/libredwg/src/./dwg.spec:2369:1
    #6 0xe9677d in dwg_dxf_object /src/libredwg/src/out_dxf.c:2690:14
    #7 0xe6817c in dxf_entities_write /src/libredwg/src/out_dxf.c:3532:16
    #8 0xe41b4c in dwg_write_dxf /src/libredwg/src/out_dxf.c:3682:7
    #9 0x4c864b in main /src/libredwg/programs/dxfwrite.c:451:11
    #10 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #11 0x41bc98 in _start (/mnt/disk/out/libredwg/programs_asan/dxfwrite+0x41bc98)

0x629000068200 is located 0 bytes to the right of 16384-byte region [0x629000064200,0x629000068200)
allocated by thread T0 here:
    #0 0x496b43 in realloc /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x4d7d12 in bit_chain_alloc /src/libredwg/src/bits.c:3026:30
    #2 0xe3f0ec in dwg_convert_SAB_to_SAT1 /src/libredwg/src/out_dxf.c:2196:15
    #3 0xeebd9e in dxf_3dsolid /src/libredwg/src/out_dxf.c:2334:20
    #4 0xeecb01 in dwg_dxf__3DSOLID_private /src/libredwg/src/./dwg.spec:2370:3
    #5 0xea23e2 in dwg_dxf__3DSOLID /src/libredwg/src/./dwg.spec:2369:1
    #6 0xe9677d in dwg_dxf_object /src/libredwg/src/out_dxf.c:2690:14
    #7 0xe6817c in dxf_entities_write /src/libredwg/src/out_dxf.c:3532:16
    #8 0xe41b4c in dwg_write_dxf /src/libredwg/src/out_dxf.c:3682:7
    #9 0x4c864b in main /src/libredwg/programs/dxfwrite.c:451:11
    #10 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291

SUMMARY: AddressSanitizer: heap-buffer-overflow /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:1670:1 in vsprintf
Shadow bytes around the buggy address:
  0x0c5280004ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c5280005000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c5280005010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c5280005020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c5280005030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c5280005040:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c5280005050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c5280005060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c5280005070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c5280005080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c5280005090: 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
==3850==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

```
unzip tests_64265.zip
dxfwrite -I DWG -o /dev/null -y ./tests_64265
```
POC file :  
[tests_64265.zip](https://github.com/LibreDWG/libredwg/files/6652592/tests_64265.zip)

--- end body ---

A heap buffer overflow vulnerability exists in the function dwg_convert_SAB_to_SAT1 in out_dxf.c of LibreDWG. This vulnerability can be exploited by providing a specially crafted DWG file, leading to potential arbitrary code execution or application crashes. The issue is confirmed by AddressSanitizer reports indicating a heap buffer overflow during the execution of the function.