$ cat main.c
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char ch;
scanf("%3c", &ch);
printf("%c\n", ch);
//system("pause");
return 0;
}
$ gcc -g -Wall -o main main.c
$ ./main
xyz
x
*** stack smashing detected ***: terminated
zsh: IOT instruction (core dumped) ./main
$
首先,这个代码是错误的
可以看到核心转存了
输出x是因为,在ch的这个位置存储的确实是x,y和z写到紧挨着ch的后面了,如果有哪个倒霉蛋在这个位置的话,那他就被覆盖了
cat main.c
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char ch;
scanf("%3c", &ch);
printf("%c\n", ch);
//system("pause");
return 0;
}
$ gcc-debug -o main main.c
$ ./main
xyz
=================================================================
==818830==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffce937b4f1 at pc 0x7fc05dc73f29 bp 0x7ffce937b370 sp 0x7ffce937aaf8
WRITE of size 3 at 0x7ffce937b4f1 thread T0
#0 0x7fc05dc73f28 in scanf_common /usr/src/debug/gcc/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors_format.inc:342
#1 0x7fc05dc74ae3 in __interceptor___isoc99_vscanf /usr/src/debug/gcc/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:1527
#2 0x7fc05dc74bf7 in __interceptor___isoc99_scanf /usr/src/debug/gcc/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:1548
#3 0x557058487256 in main /tmp/main.c:6
#4 0x7fc05d23c28f (/usr/lib/libc.so.6+0x2328f)
#5 0x7fc05d23c349 in __libc_start_main (/usr/lib/libc.so.6+0x23349)
#6 0x5570584870e4 in _start (/tmp/main+0x10e4)
Address 0x7ffce937b4f1 is located in stack of thread T0 at offset 33 in frame
#0 0x5570584871c8 in main /tmp/main.c:4
This frame has 1 object(s):
[32, 33) 'ch' (line 5) <== Memory access at offset 33 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow /usr/src/debug/gcc/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors_format.inc:342 in scanf_common
Shadow bytes around the buggy address:
0x10001d267640: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10001d267650: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10001d267660: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10001d267670: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10001d267680: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x10001d267690: 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1[01]f3
0x10001d2676a0: f3 f3 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10001d2676b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10001d2676c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10001d2676d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10001d2676e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
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
==818830==ABORTING
$
|