I did some tests:
Code:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int main()
{
const int MAX_NAME_LENGTH = 20;
const char *chname = "duckduckduckduckduck";
char name [MAX_NAME_LENGTH];
strcpy(name, chname);
return 0;
}
gcc test.c
./a.out
that is ok.
BUT, if i compile using the same gcc parameters as TBA mud. i get this:
rescator@rescator-System-Product-Name:~$ gcc -g -O2 -Wall -Wno-char-subscripts -Wno-unused-but-set-variable test.c
In file included from /usr/include/string.h:635:0,
from test.c:2:
In function ‘strcpy’,
inlined from ‘main’ at test.c:11:2:
/usr/include/x86_64-linux-gnu/bits/string3.h:110:10: warning: call to __builtin___memcpy_chk will always overflow destination buffer
return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
^
rescator@rescator-System-Product-Name:~$ ./a.out
*** buffer overflow detected ***: ./a.out terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f77d3e6a7e5]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x5c)[0x7f77d3f0c15c]
/lib/x86_64-linux-gnu/libc.so.6(+0x117160)[0x7f77d3f0a160]
./a.out[0x4004eb]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f77d3e13830]
./a.out[0x400539]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:01 5505307 /home/rescator/a.out
00600000-00601000 r--p 00000000 08:01 5505307 /home/rescator/a.out
00601000-00602000 rw-p 00001000 08:01 5505307 /home/rescator/a.out
0066c000-0068d000 rw-p 00000000 00:00 0 [heap]
7f77d3bdd000-7f77d3bf3000 r-xp 00000000 08:01 5640757 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f77d3bf3000-7f77d3df2000 ---p 00016000 08:01 5640757 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f77d3df2000-7f77d3df3000 rw-p 00015000 08:01 5640757 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f77d3df3000-7f77d3fb3000 r-xp 00000000 08:01 5636313 /lib/x86_64-linux-gnu/libc-2.23.so
7f77d3fb3000-7f77d41b3000 ---p 001c0000 08:01 5636313 /lib/x86_64-linux-gnu/libc-2.23.so
7f77d41b3000-7f77d41b7000 r--p 001c0000 08:01 5636313 /lib/x86_64-linux-gnu/libc-2.23.so
7f77d41b7000-7f77d41b9000 rw-p 001c4000 08:01 5636313 /lib/x86_64-linux-gnu/libc-2.23.so
7f77d41b9000-7f77d41bd000 rw-p 00000000 00:00 0
7f77d41bd000-7f77d41e3000 r-xp 00000000 08:01 5636299 /lib/x86_64-linux-gnu/ld-2.23.so
7f77d43c0000-7f77d43c3000 rw-p 00000000 00:00 0
7f77d43e1000-7f77d43e2000 rw-p 00000000 00:00 0
7f77d43e2000-7f77d43e3000 r--p 00025000 08:01 5636299 /lib/x86_64-linux-gnu/ld-2.23.so
7f77d43e3000-7f77d43e4000 rw-p 00026000 08:01 5636299 /lib/x86_64-linux-gnu/ld-2.23.so
7f77d43e4000-7f77d43e5000 rw-p 00000000 00:00 0
7fffb41e9000-7fffb420a000 rw-p 00000000 00:00 0 [stack]
7fffb42bb000-7fffb42bd000 r--p 00000000 00:00 0 [vvar]
7fffb42bd000-7fffb42bf000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
But, this code:
Code:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int main()
{
const int MAX_NAME_LENGTH = 20;
const char *chname = "duckduckduckduckduck";
char name [MAX_NAME_LENGTH+1];
strcpy(name, chname);
return 0;
}
With the +1
rescator@rescator-System-Product-Name:~$ gcc -g -O2 -Wall -Wno-char-subscripts -Wno-unused-but-set-variable test.c
rescator@rescator-System-Product-Name:~$ ./a.out
No warnings, no crash.
Interesting.
Do you understand that?