为什么粉碎后不会立即出现"堆栈粉碎检测"?

tim*_*nYE 4 c buffer-overflow linux-kernel stack-smash

我理解"堆栈粉碎检测"是什么意思.关于这一点,这里已经有很多问题了.但我没有找到以下问题的答案.拿C代码

int main(int argc, char **args) {
   char puffer[5];
   strcpy(puffer, *++args);
   printf("%s\n",puffer);
   return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

当我在Ubuntu 13.10中使用gcc 4.8.1编译时./buffer 123456789会引发预期的错误stack smashing detected.但为什么不./buffer 12345678引起错误呢?我希望每个超过5个字符的字符串都会引发错误.

Man*_*mar 5

理论上你是对的,这就是它应该如何表现.程序使用超过5个字节的那一刻,这可能会导致未定义的行为.但是由于各种性能原因,堆栈指针通常与某些边界对齐.对齐因架构而异.因此,对于大于5的每个输入,您都不会看到此问题.

程序的反汇编显示如下.查看sub $0x20,%rsp指令,该指令在堆栈上为此函数分配16个字节的内存.

(gdb) disassemble main
Dump of assembler code for function main(int, char**):
   0x00000000004008b0 <+0>: push   %rbp
   0x00000000004008b1 <+1>: mov    %rsp,%rbp
=> 0x00000000004008b4 <+4>: sub    $0x20,%rsp
   0x00000000004008b8 <+8>: mov    %edi,-0x14(%rbp)
   0x00000000004008bb <+11>:    mov    %rsi,-0x20(%rbp) 
  0x00000000004008bf <+15>: mov    %fs:0x28,%rax
   0x00000000004008c8 <+24>:    mov    %rax,-0x8(%rbp)
   0x00000000004008cc <+28>:    xor    %eax,%eax
   0x00000000004008ce <+30>:    addq   $0x8,-0x20(%rbp)
   0x00000000004008d3 <+35>:    mov    -0x20(%rbp),%rax
   0x00000000004008d7 <+39>:    mov    (%rax),%rdx
   0x00000000004008da <+42>:    lea    -0x10(%rbp),%rax
   0x00000000004008de <+46>:    mov    %rdx,%rsi
   0x00000000004008e1 <+49>:    mov    %rax,%rdi
   0x00000000004008e4 <+52>:    callq  0x400770 <strcpy@plt>
   0x00000000004008e9 <+57>:    lea    -0x10(%rbp),%rax
   0x00000000004008ed <+61>:    mov    %rax,%rdi
   0x00000000004008f0 <+64>:    callq  0x400710 <puts@plt>
   0x00000000004008f5 <+69>:    mov    $0x0,%eax
   0x00000000004008fa <+74>:    mov    -0x8(%rbp),%rcx
   0x00000000004008fe <+78>:    xor    %fs:0x28,%rcx
   0x0000000000400907 <+87>:    je     0x400918 <main(int, char**)+104>
   0x0000000000400909 <+89>:    jmp    0x400913 <main(int, char**)+99>
   0x000000000040090b <+91>:    mov    %rax,%rdi
   0x000000000040090e <+94>:    callq  0x400790 <_Unwind_Resume@plt>
   0x0000000000400913 <+99>:    callq  0x400760 <__stack_chk_fail@plt>
   0x0000000000400918 <+104>:   leaveq 
   0x0000000000400919 <+105>:   retq   
Run Code Online (Sandbox Code Playgroud)