Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A heap-buffer-overflow issue in revstr function in Misc.c:146 #4

Open
wcventure opened this issue Jun 9, 2021 · 0 comments
Open

A heap-buffer-overflow issue in revstr function in Misc.c:146 #4

wcventure opened this issue Jun 9, 2021 · 0 comments

Comments

@wcventure
Copy link

Hi,

There exist a heap buffer-overflow issue in revstr function in Misc.c:146. Space of size is allocated for pointer p in p = (char *)calloc(size, sizeof(char));. However, the index used in for (i = size; i >= 0; i--, s++) is out of bound.

void revstr(char *str)
{
	char *p, *s;
	int i;
	int size;

	if ((size = strlen(str)) == 0)
		return;
	p = (char *)calloc(size, sizeof(char));
	s = p;
	for (i = size; i >= 0; i--, s++)
		*s = *(str + i - 1);
	*s = '\0';
	strncpy(str, p, size);
	str[size] = '\0';
	free(p);
}

To fix this bug, a patched version could be:

--- aget-master-new/Misc.c   2021-06-09 15:47:35.962171900 +0800
+++ aget-master/Misc.c  2021-06-09 15:48:13.950936000 +0800
@@ -140,9 +140,9 @@ void revstr(char *str)

        if ((size = strlen(str)) == 0)
                return;
-       p = (char *)calloc(size, sizeof(char));
+       p = (char *)calloc(size+1, sizeof(char));
        s = p;
-       for (i = size; i >= 0; i--, s++)
+       for (i = size; i > 0; i--, s++)
                *s = *(str + i - 1);
        *s = '\0';
        strncpy(str, p, size);

Here is the report from AddressSanitizer for this issue:

=================================================================
==5839==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000017 at pc 0x55b0344b9c89 bp 0x7ffc1b493450 sp 0x7ffc1b493440
WRITE of size 1 at 0x602000000017 thread T0
    #0 0x55b0344b9c88 in revstr /Aget-master/Misc.c:146
    #1 0x55b0344b97de in parse_url /Aget-master/aget-devel/Misc.c:84
    #2 0x55b0344b718e in main /Aget-master/aget-devel/main.c:108
    #3 0x7f5f56cea0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
    #4 0x55b0344b6a4d in _start (/Aget-master/aget-devel/aget+0x4a4d)

0x602000000017 is located 0 bytes to the right of 7-byte region [0x602000000010,0x602000000017)
allocated by thread T0 here:
    #0 0x7f5f56fe5dc6 in calloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10ddc6)
    #1 0x55b0344b9bf4 in revstr /Aget-master/aget-devel/Misc.c:143
    #2 0x55b0344b97de in parse_url /Aget-master/aget-devel/Misc.c:84
    #3 0x55b0344b718e in main /Aget-master/aget-devel/main.c:108
    #4 0x7f5f56cea0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)

SUMMARY: AddressSanitizer: heap-buffer-overflow /Aget-master/aget-devel/Misc.c:146 in revstr
Shadow bytes around the buggy address:
  0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c047fff8000: fa fa[07]fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8050: 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
==5839==ABORTING
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant