免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2441 | 回复: 5
打印 上一主题 下一主题

[Linux] gcc加了-fstack-protector在ELF里面反而看不到保护段信息了 [复制链接]

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 10:16:532015元宵节徽章
日期:2015-03-06 15:53:22
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-09-29 12:00 |只看该作者 |倒序浏览
例如,我有一个cpp文件
  1. $ cat d.cpp
  2. struct Test
  3. {
  4.   int i;
  5.   Test(){
  6.     i=23;
  7.   }
  8. };
  9. int main()
  10. {
  11.   Test obj1;
  12.   return 0;
  13. }
复制代码
我用两种方式来编译生成这个文件,然后objdump到一个文本文件里面,最后用vimdiff来对比这两个elf信息
  1. $ gcc -fstack-protector d.cpp -g -o dpro
  2. $ gcc                   d.cpp -g -o dclean
  3. $ objdump -d dpro  >dpro.txt
  4. $ objdump -d dclean>dclean.txt
  5. $ vimdiff dpro.txt dclean.txt
复制代码

发现dclean多出来一个小块内容:
  1. |  0000000000400420 <__stack_chk_fail@plt>:                                                               
  2. |    400420:       ff 25 f2 0b 20 00       jmpq   *0x200bf2(%rip)        # 601018 <_GLOBAL_OFFSET_TABLE_+0
  3. |    400426:       68 00 00 00 00          pushq  $0x0                                                   
  4. |    40042b:       e9 e0 ff ff ff          jmpq   400410 <_init+0x20>
复制代码

这个没有加-fstack-protector编译出来的,反而有一个段叫做__stack_chk_fail的,而加了编译选项的反而没有。这情何以堪?
也因此,我发现dclean.txt的尺寸比dpro.txt更大,这个和我的预期有差别。
我觉得带有protector信息的文件是不是应该更大一点,包含堆栈检查的信息呢?


论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2016-09-29 12:37 |只看该作者
回复 1# cdsfiui

你这有问题不查文档的习惯什么时候能改改?

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 10:16:532015元宵节徽章
日期:2015-03-06 15:53:22
3 [报告]
发表于 2016-09-29 14:47 |只看该作者
man 页面看不出什么有用的信息:

-fstack-protector
           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.

       -fstack-protector-all
           Like -fstack-protector except that all functions are protected.

       -fstack-protector-strong
           Like -fstack-protector but includes additional functions to be protected --- those that have local array definitions, or have references to local frame addresses.

       -fstack-protector-explicit
           Like -fstack-protector but only protects those functions which have the "stack_protect" attribute

看起来-fstack-protector应该生成额外的保护信息才对。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2016-09-29 15:24 |只看该作者
回复 3# cdsfiui

-fstack-protector 是有条件的插入检测代码;-fstack-protector-all 是雾条件的插入检测代码。

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 10:16:532015元宵节徽章
日期:2015-03-06 15:53:22
5 [报告]
发表于 2016-09-29 23:56 |只看该作者
MMMIX 发表于 2016-09-29 15:24
回复 3# cdsfiui

-fstack-protector 是有条件的插入检测代码;-fstack-protector-all 是雾条件的插入检 ...

我比较了一下,不加这个选项,编译出来的文件最大。
其次是-fstack-protector-all
最小的是-fstack-protector.

我的疑问在于这里,为什么不加选项的时候反而能看到check_stack_fail这个段,而且可执行代码最大?

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
6 [报告]
发表于 2016-09-30 10:19 |只看该作者
回复 5# cdsfiui

我的疑问在于这里,为什么不加选项的时候反而能看到check_stack_fail这个段


Ubuntu gcc(1):
       -fstack-protector
           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.

           NOTE: In Ubuntu 6.10 and later versions this option is enabled by default for C, C++, ObjC, ObjC++, if none of -fno-stack-protector,
           -nostdlib, nor -ffreestanding are found.


现在我的疑问是,你到底看的哪的文档?另外,有疑问的时候,为什么不去通过 g++ -v 检查下编译过程中的实际参数?

BTW, check_stack_fail 就不是个段,而是个函数调用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP