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

相關文章
相關標籤/搜索