c语言 - 为什么我不能禁用堆栈保护?



我目前使用的操作系统是64位Ubuntu 14.04,其gcc版本为4.8.4。

我写了一个简单的程序,如下所示,来做一些与缓冲区溢出相关的测试,但不知怎么的,我发现我无法正确地溢出本地字符串。

/*test.c*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int bof(char *str)
{
    char buffer[4];
    strcpy(buffer, str);
    return 1;
}
int main()
{
    char str[]="123456789012345'; 
    /* This is the maximum length the string
     can be, which is 16 bytes including the null character at the end, and 
    any strings that are longer than this would result in a segmentation fault */
    bof(str);
}

这个程序是用命令编译的

gcc -o test -fno-stack-protector test.c

因此本应禁用所谓的堆栈保护。

根据我的观察,任何长度小于或等于16个字符的字符串(包括空字符)都可以;否则,它将导致分割错误。

有什么想法吗?我为什么以及如何才能让它发挥作用?提前感谢!

您可以使用GNU调试来查找距离,这里有一个教程http://www.cs.umd.edu/~srhuang/taching/cmsc212/gdb-tutorial-handout.pdf

最新更新