[其他ST产品] 【GCC 栈溢出的保护机制】

[复制链接]
946|8
 楼主| 年轻的国王 发表于 2020-12-8 20:18 | 显示全部楼层 |阅读模式
GCC, TE, ST, ck, AC
代码中出现栈溢出的问题是程序员特别头疼的问题,gcc 提供了栈保护的选项能够在一定程度上帮助我们发现程序中存在的栈溢出的问题。


  • Emit extra code to check for buffer overflows, such as stack smashing attacks.
  • This is done by adding a guard variable to functions with vulnerable objects.
  • This includes functions that call alloca, and functions with buffers larger than
  • 8 bytes. The guards are initialized when a function is entered and then checked
  • when the function exits. If a guard check fails, an error message is printed and
  • the program exits.

[color=rgb(51, 102, 153) !important]复制代码


编译时加上上述的编译选项,会在栈空间中额外追加8个字节保护变量(guard variable),函数退出的时候会check该guard variable,如果值被修改了,则会输出error message 并退出程序
按照上述的描述,编译选项也不是完全保证检测出栈溢出的问题,如果跳过了描述的规则也是无法检出的,如果正好避开了检查规则的话也是无法检出问题的。


测试代码如下:
  • //gcc -fno-stack-protector fno-stack-protector_test.c -o fno-stack-protector_test
  • //gcc fno-stack-protector_test.c -o fno-stack-protector_test
  • ///sdk/4.3.2/bin/arm-linux-gcc-4.3.2 -fno-stack-protector -fomit-frame-pointer fno-stack-protector_test.c -o fno-stack-protector_test
  • ///sdk/4.3.2/bin/arm-linux-gcc-4.3.2 -fstack-protector-all -fomit-frame-pointer fno-stack-protector_test.c -o fno-stack-protector_test
  • ///sdk/4.3.2/bin/arm-linux-objdump -S fno-stack-protector_test > fno-stack-protector_test.dis
  • #include <stdio.h>
  • int main(void)
  • {
  •         char buffer[2];
  •         int i;
  •         i = 1;
  •         buffer[0] = 'a';
  •         buffer[9] = 'b';
  •         printf("hello.world\n");
  •         //while(1);
  •         return 0;
  • }

[color=rgb(51, 102, 153) !important]复制代码

上述代码加上栈保护选项和去除时的代码比较会发现,加上编译选项的代码会在函数栈中额外多开辟8个字节的溢出检出空间,并在函数退出的时候追加固定值的比较,一旦发生变化则跳到对应的栈溢出处理函数中,从而产生core 文件让程序员解析问题。通过反汇编的代码可以看出来,实际比较的长度为四个字节,这个应该时跟编译器有关系,回头在用别的编译器在验证下。

嵌入小菜菜 发表于 2020-12-8 22:26 | 显示全部楼层
这个**比较贴合实际的应用
734774645 发表于 2020-12-9 15:23 | 显示全部楼层
不是很懂堆栈。
643757107 发表于 2020-12-9 16:42 | 显示全部楼层
学习学习。
drer 发表于 2021-1-5 12:54 | 显示全部楼层
如何在早期发现这种溢出呢
gwsan 发表于 2021-1-5 12:56 | 显示全部楼层
溢出之后如何解决呢
kxsi 发表于 2021-1-5 12:57 | 显示全部楼层
请问GCC栈是什么啊
nawu 发表于 2021-1-5 12:58 | 显示全部楼层
这样做非常的不错
qcliu 发表于 2021-1-5 12:59 | 显示全部楼层
这真是一个非常巧妙的办法
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:每天都要开心呀

210

主题

505

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部