何时以及如何使用 GCC 的堆栈保护功能?

When and how to use GCC#39;s stack protection feature?(何时以及如何使用 GCC 的堆栈保护功能?)

本文介绍了何时以及如何使用 GCC 的堆栈保护功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编译我正在处理的项目(商业多平台 C++ 游戏引擎,在 Mac OS X 10.6 和 GCC 4.2 上编译)时启用了 -Wstack-protector 警告.此标志警告即使 -fstack-protector 已启用,函数也不会受到堆栈粉碎保护.GCC 在构建项目时发出一些警告:

<块引用>

没有保护功能:没有至少 8 字节长的缓冲区
不保护局部变量:可变长度缓冲区

对于第一个警告,我发现可以调整在函数中使用时缓冲区必须具有的最小大小,以保护该函数免受堆栈粉碎:--param ssp-buffer-size可以使用=X,其中X默认为8,可以低至1.

对于第二个警告,除非我停止使用 -Wstack-protector,否则我无法抑制它的出现.

  1. 什么时候应该使用-fstack-protector?(例如,在开发过程中的所有时间,或者只是在跟踪错误时?)
  2. 什么时候应该使用-fstack-protector-all?
  3. -Wstack-protector 告诉我什么?是否建议我减少缓冲区的最小大小?
  4. 如果是这样,将大小设置为 1 有什么缺点吗?
  5. 如果您想要无警告构建,-Wstack-protector 似乎不是您想要始终启用的那种标志.对吗?

解决方案

堆栈保护是一种加固策略,而不是调试策略.如果您的游戏具有网络感知能力或数据来自不受控制的来源,请将其打开.如果它没有来自不受控制的地方的数据,请不要打开它.

它是如何发生的:如果您有错误并根据攻击者可以控制的内容更改缓冲区,则该攻击者可以覆盖返回地址或堆栈的类似部分,以使其执行他们的代码而不是您的代码代码.如果检测到这种情况发生,堆栈保护将中止您的程序.您的用户不会高兴,但他们也不会被黑客入侵.这不是一种关于在游戏中作弊的黑客行为,而是一种关于有人利用您代码中的漏洞来创建可能感染您的用户的漏洞的黑客行为.

对于面向调试的解决方案,请查看诸如 mudflap 之类的东西.

关于您的具体问题:

  1. 如果您从不受控制的来源获取数据,请使用堆栈保护器.答案很可能是肯定的.所以使用它.即使您没有来自不受控制的来源的数据,您也可能最终会或已经拥有但没有意识到这一点.
  2. 如果您想要额外的保护以换取某些性能损失,则可以使用所有缓冲区的堆栈保护.来自 gcc4.4.2 手册:

    <块引用><块引用><块引用>

    -fstack-protector

    发出额外的代码来检查缓冲区溢出,例如堆栈粉碎攻击.这是通过向具有易受攻击对象的函数添加保护变量来完成的.这包括调用 alloca 的函数,以及缓冲区大于 8 字节的函数.守卫在进入函数时初始化,然后在函数退出时检查.如果防护检查失败,则会打印错误消息并退出程序.

    -fstack-protector-all

    与 -fstack-protector 类似,只是所有功能都受到保护.

  3. 警告告诉您堆栈保护无法保护的缓冲区.

  4. 不一定建议您减小最小缓冲区大小,大小为 0/1 时,它与 stack-protector-all 相同.如果您决定重新设计代码以保护缓冲区,它只是向您指出这一点.
  5. 不,这些警告不代表问题,它们只是向您指出信息.不要经常使用它们.

I have enabled the -Wstack-protector warning when compiling the project I'm working on (a commercial multi-platform C++ game engine, compiling on Mac OS X 10.6 with GCC 4.2). This flag warns about functions that will not be protected against stack smashing even though -fstack-protector is enabled. GCC emits some warnings when building the project:

not protecting function: no buffer at least 8 bytes long
not protecting local variables: variable length buffer

For the first warning, I found that it is possible to adjust the minimum size a buffer must have when used in a function, for this function to be protected against stack smashing: --param ssp-buffer-size=X can be used, where X is 8 by default and can be as low as 1.

For the second warning, I can't suppress its occurrences unless I stop using -Wstack-protector.

  1. When should -fstack-protector be used? (as in, for instance, all the time during dev, or just when tracking bugs down?)
  2. When should -fstack-protector-all be used?
  3. What is -Wstack-protector telling me? Is it suggesting that I decrease the buffer minimum size?
  4. If so, are there any downsides to putting the size to 1?
  5. It appears that -Wstack-protector is not the kind of flag you want enabled at all times if you want a warning-free build. Is this right?

解决方案

Stack-protection is a hardening strategy, not a debugging strategy. If your game is network-aware or otherwise has data coming from an uncontrolled source, turn it on. If it doesn't have data coming from somewhere uncontrolled, don't turn it on.

Here's how it plays out: If you have a bug and make a buffer change based on something an attacker can control, that attacker can overwrite the return address or similar portions of the stack to cause it to execute their code instead of your code. Stack protection will abort your program if it detects this happening. Your users won't be happy, but they won't be hacked either. This isn't the sort of hacking that is about cheating in the game, it's the sort of hacking that is about someone using a vulnerability in your code to create an exploit that potentially infects your user.

For debugging-oriented solutions, look at things like mudflap.

As to your specific questions:

  1. Use stack protector if you get data from uncontrolled sources. The answer to this is probably yes. So use it. Even if you don't have data from uncontrolled sources, you probably will eventually or already do and don't realize it.
  2. Stack protections for all buffers can be used if you want extra protection in exchange for some performance hit. From gcc4.4.2 manual:

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

  3. The warnings tell you what buffers the stack protection can't protect.

  4. It is not necessarily suggesting you decrease your minimum buffer size, and at a size of 0/1, it is the same as stack-protector-all. It is only pointing it out to you so that you can, if you decide redesign the code so that buffer is protected.
  5. No, those warnings don't represent issues, they just point out information to you. Don't use them regularly.

这篇关于何时以及如何使用 GCC 的堆栈保护功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:何时以及如何使用 GCC 的堆栈保护功能?

基础教程推荐