Stack OverFlow 栈溢出 - stack smashing detected

在改造一个ota_ts_generator工具时,调试时,发生以下错误:
        ./app
**** stack smashing detected ***: ./app terminated*
*======= Backtrace: =========*
*/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x48)Aborted*
Stack Smashing is actually a protection mechanism used by gcc to detect buffer overflow attacks.
#include <stdio.h>

void func()
{
    char array[10];
    gets(array);
}

int main(int argc, char **argv)
{
    func();
}

【注释:】  An input of string greater than size 10 causes corruption of gcc inbuilt protection canary variable followed by SIGABRT to terminate the program. 数组

    You can disable this protection of gcc using option -fno-stack-protector while compiling.
In that case you will get a segmentation fault if you try to access illegal memory location. and of course you can detect the point of overflow say for example using  valgrind. app


另一个例子:
函数

#include <string.h> 
  
void fn(void) 
{ 
    char a[100]; 
    char *p = a; 
    bzero(p, 1000); 
} 
  
int main(int argc, char *argv[]) 
{ 
     fn(); 
     return 0; 
}

这里,数组a就会保存在栈中。当越界访问a[100]及后面的数据时,发生栈溢出,最容易出现的问题是返回指针被修改,进而函数返回时会发现返回的代码段指针错误,提示:“stack smashing detected...":

不少时候,当内存溢出问题不严重时,并不会直接终止咱们程序的运行。可是,咱们会在调试程序中碰到很是奇怪的问题,好比某一个变量平白无故变成乱码,无论是在堆中,仍是栈中。这便颇有多是指针的错误使用致使的。这种状况出现时,一种调试方法是:使用gdb加载程序,并用watch锁定被改为乱码的变量。这样,若是这个变量被修改,程序便会停下来,咱们就能够看究竟是哪条语句修改了这个程序。 工具

总结:栈溢出,经常程序abort的地方并非出问题的地方有,而是离产生问题的地方有必定距离,因此难以调试。 ui

相关文章
相关标签/搜索