## 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 add_MULTILEADER in in_dxf.c:4306.

ASAN report:

```
==2370992==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60c000000300 at pc 0x000001b28d25 bp 0x7fffffffba40 sp 0x7fffffffba38
WRITE of size 8 at 0x60c000000300 thread T0
    #0 0x1b28d24 in add_MULTILEADER /src/libredwg/src/in_dxf.c:4306:45
    #1 0x1a90923 in new_object /src/libredwg/src/in_dxf.c:11636:26
    #2 0x1a763d9 in dxf_blocks_read /src/libredwg/src/in_dxf.c:12042:22
    #3 0x1a6cfbd in dwg_read_dxf /src/libredwg/src/in_dxf.c:12810:23
    #4 0x4cb82a in dxf_read_file /src/libredwg/src/dwg.c:381:13
    #5 0x4c91c4 in main /src/libredwg/programs/dxf2dwg.c:312:15
    #6 0x7ffff7c580b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16
    #7 0x41c40d in _start (/mnt/disk/out/libredwg/programs_asan/dxf2dwg+0x41c40d)

0x60c000000300 is located 0 bytes to the right of 128-byte region [0x60c000000280,0x60c000000300)
allocated by thread T0 here:
    #0 0x497eb2 in calloc /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3
    #1 0x1a687d9 in xcalloc /src/libredwg/src/in_dxf.c:216:7
    #2 0x1b25093 in add_MULTILEADER /src/libredwg/src/in_dxf.c:4300:35
    #3 0x1a90923 in new_object /src/libredwg/src/in_dxf.c:11636:26
    #4 0x1a763d9 in dxf_blocks_read /src/libredwg/src/in_dxf.c:12042:22
    #5 0x1a6cfbd in dwg_read_dxf /src/libredwg/src/in_dxf.c:12810:23
    #6 0x4cb82a in dxf_read_file /src/libredwg/src/dwg.c:381:13
    #7 0x4c91c4 in main /src/libredwg/programs/dxf2dwg.c:312:15
    #8 0x7ffff7c580b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16

SUMMARY: AddressSanitizer: heap-buffer-overflow /src/libredwg/src/in_dxf.c:4306:45 in add_MULTILEADER
Shadow bytes around the buggy address:
  0x0c187fff8010: fd fd fd fd fd fd fd fd fa fa fa fa fa fa fa fa
  0x0c187fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fa
  0x0c187fff8030: fa fa fa fa fa fa fa fa fd fd fd fd fd fd fd fd
  0x0c187fff8040: fd fd fd fd fd fd fd fa fa fa fa fa fa fa fa fa
  0x0c187fff8050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c187fff8060:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff80a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff80b0: 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
==2370992==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

```
unzip tests_64277.zip
dxf2dwg ./tests_64277 -o /dev/null
```
POC file :  
[tests_64277.zip](https://github.com/LibreDWG/libredwg/files/6652517/tests_64277.zip)
Heap-buffer-overflow in add_MULTILEADER in in_dxf.c
This is a GitHub Issue
repo:libredwg
owner:LibreDWG
Title : Heap-buffer-overflow in add_MULTILEADER in in_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 add_MULTILEADER in in_dxf.c:4306.

ASAN report:

```
==2370992==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60c000000300 at pc 0x000001b28d25 bp 0x7fffffffba40 sp 0x7fffffffba38
WRITE of size 8 at 0x60c000000300 thread T0
    #0 0x1b28d24 in add_MULTILEADER /src/libredwg/src/in_dxf.c:4306:45
    #1 0x1a90923 in new_object /src/libredwg/src/in_dxf.c:11636:26
    #2 0x1a763d9 in dxf_blocks_read /src/libredwg/src/in_dxf.c:12042:22
    #3 0x1a6cfbd in dwg_read_dxf /src/libredwg/src/in_dxf.c:12810:23
    #4 0x4cb82a in dxf_read_file /src/libredwg/src/dwg.c:381:13
    #5 0x4c91c4 in main /src/libredwg/programs/dxf2dwg.c:312:15
    #6 0x7ffff7c580b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16
    #7 0x41c40d in _start (/mnt/disk/out/libredwg/programs_asan/dxf2dwg+0x41c40d)

0x60c000000300 is located 0 bytes to the right of 128-byte region [0x60c000000280,0x60c000000300)
allocated by thread T0 here:
    #0 0x497eb2 in calloc /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3
    #1 0x1a687d9 in xcalloc /src/libredwg/src/in_dxf.c:216:7
    #2 0x1b25093 in add_MULTILEADER /src/libredwg/src/in_dxf.c:4300:35
    #3 0x1a90923 in new_object /src/libredwg/src/in_dxf.c:11636:26
    #4 0x1a763d9 in dxf_blocks_read /src/libredwg/src/in_dxf.c:12042:22
    #5 0x1a6cfbd in dwg_read_dxf /src/libredwg/src/in_dxf.c:12810:23
    #6 0x4cb82a in dxf_read_file /src/libredwg/src/dwg.c:381:13
    #7 0x4c91c4 in main /src/libredwg/programs/dxf2dwg.c:312:15
    #8 0x7ffff7c580b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16

SUMMARY: AddressSanitizer: heap-buffer-overflow /src/libredwg/src/in_dxf.c:4306:45 in add_MULTILEADER
Shadow bytes around the buggy address:
  0x0c187fff8010: fd fd fd fd fd fd fd fd fa fa fa fa fa fa fa fa
  0x0c187fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fa
  0x0c187fff8030: fa fa fa fa fa fa fa fa fd fd fd fd fd fd fd fd
  0x0c187fff8040: fd fd fd fd fd fd fd fa fa fa fa fa fa fa fa fa
  0x0c187fff8050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c187fff8060:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff80a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff80b0: 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
==2370992==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

```
unzip tests_64277.zip
dxf2dwg ./tests_64277 -o /dev/null
```
POC file :  
[tests_64277.zip](https://github.com/LibreDWG/libredwg/files/6652517/tests_64277.zip)
--- end body ---

A heap buffer overflow vulnerability exists in the add_MULTILEADER function in in_dxf.c of LibreDWG. This vulnerability can be exploited by providing a specially crafted DXF file, leading to potential arbitrary code execution or application crash. The issue is confirmed by AddressSanitizer reports indicating a write operation beyond allocated memory bounds.