t-w / ADFlib

A free, portable and open implementation of the Amiga filesystem
GNU General Public License v2.0
0 stars 1 forks source link

Fix memory issues #1

Closed t-w closed 1 year ago

t-w commented 1 year ago

After compilation with address sanitizer there are several issues caught, mostly memory leaks but also one buffer overflow.

All to fix.

t-w commented 1 year ago

Sanitizer: 076d514785aea4d0d68c743930c0f38905c40fa8

Memory leaks already done:

Buffer overflow caught by the sanitizer:

=================================================================
==1328524==ERROR: AddressSanitizer: global-buffer-overflow on address 0x55e5eb489cd8 at pc 0x55e5eb4786ca bp 0x7ffd11529120 sp 0x7ffd11529118
READ of size 4 at 0x55e5eb489cd8 thread T0
    #0 0x55e5eb4786c9 in adfIsBlockFree /home/user/src/github/ADFlib/src/adf_bitm.c:200
    #1 0x55e5eb47299d in adfGetDelEnt /home/user/src/github/ADFlib/src/adf_salv.c:87
    #2 0x55e5eb456a21 in main /home/user/src/github/ADFlib/regtests/Test/undel.c:85
    #3 0x7f05d579a7ec in __libc_start_main ../csu/libc-start.c:332
    #4 0x55e5eb456379 in _start (/home/user/src/github/ADFlib/build/debug/regtests/Test/undel+0x8379)

0x55e5eb489cd8 is located 8 bytes to the left of global variable 'bitMask' defined in '/home/user/src/github/ADFlib/src/adf_disk.c:45:10' (0x55e5eb489ce0) of size 128
SUMMARY: AddressSanitizer: global-buffer-overflow /home/user/src/github/ADFlib/src/adf_bitm.c:200 in adfIsBlockFree
Shadow bytes around the buggy address:
  0x0abd3d689340: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x0abd3d689350: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x0abd3d689360: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x0abd3d689370: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x0abd3d689380: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
=>0x0abd3d689390: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9[f9]00 00 00 00
  0x0abd3d6893a0: 00 00 00 00 00 00 00 00 00 00 00 00 f9 f9 f9 f9
  0x0abd3d6893b0: 00 00 00 00 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x0abd3d6893c0: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x0abd3d6893d0: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x0abd3d6893e0: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
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
==1328524==ABORTING
t-w commented 1 year ago

Backtrace:

undel: /home/user/src/github/ADFlib/src/adf_bitm.c:188: adfIsBlockFree: Assertion `nSect >= 2' failed.

Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:49
49      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:49
#1  0x00007ffff7422536 in __GI_abort () at abort.c:79
#2  0x00007ffff742241f in __assert_fail_base (fmt=0x7ffff7588998 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", 
    assertion=0x55555558b140 "nSect >= 2", file=0x55555558b0e0 "/home/user/src/github/ADFlib/src/adf_bitm.c", 
    line=188, function=<optimized out>) at assert.c:92
#3  0x00007ffff7431212 in __GI___assert_fail (assertion=0x55555558b140 "nSect >= 2", 
    file=0x55555558b0e0 "/home/user/src/github/ADFlib/src/adf_bitm.c", line=188, 
    function=0x55555558b4a0 <__PRETTY_FUNCTION__.0> "adfIsBlockFree") at assert.c:101
#4  0x000055555557e59a in adfIsBlockFree (vol=0x608000000020, nSect=0)
    at /home/user/src/github/ADFlib/src/adf_bitm.c:188
#5  0x00005555555789ae in adfGetDelEnt (vol=0x608000000020) at /home/user/src/github/ADFlib/src/adf_salv.c:87
#6  0x000055555555ca32 in main (argc=1, argv=0x7fffffffe0c8)
    at /home/user/src/github/ADFlib/regtests/Test/undel.c:85