我在编译我正在处理的项目(商业多平台 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
,否则我无法抑制它的出现.
-fstack-protector
?(例如,在开发过程中的所有时间,或者只是在跟踪错误时?)-fstack-protector-all
?-Wstack-protector
告诉我什么?是否建议我减少缓冲区的最小大小?-Wstack-protector
似乎不是您想要始终启用的那种标志.对吗?堆栈保护是一种加固策略,而不是调试策略.如果您的游戏具有网络感知能力或数据来自不受控制的来源,请将其打开.如果它没有来自不受控制的地方的数据,请不要打开它.
它是如何发生的:如果您有错误并根据攻击者可以控制的内容更改缓冲区,则该攻击者可以覆盖返回地址或堆栈的类似部分,以使其执行他们的代码而不是您的代码代码.如果检测到这种情况发生,堆栈保护将中止您的程序.您的用户不会高兴,但他们也不会被黑客入侵.这不是一种关于在游戏中作弊的黑客行为,而是一种关于有人利用您代码中的漏洞来创建可能感染您的用户的漏洞的黑客行为.
对于面向调试的解决方案,请查看诸如 mudflap 之类的东西.
关于您的具体问题:
如果您想要额外的保护以换取某些性能损失,则可以使用所有缓冲区的堆栈保护.来自 gcc4.4.2 手册:
<块引用><块引用><块引用>发出额外的代码来检查缓冲区溢出,例如堆栈粉碎攻击.这是通过向具有易受攻击对象的函数添加保护变量来完成的.这包括调用 alloca 的函数,以及缓冲区大于 8 字节的函数.守卫在进入函数时初始化,然后在函数退出时检查.如果防护检查失败,则会打印错误消息并退出程序.
与 -fstack-protector 类似,只是所有功能都受到保护.
警告告诉您堆栈保护无法保护的缓冲区.
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
.
-fstack-protector
be used? (as in, for instance, all the time during dev, or just when tracking bugs down?) -fstack-protector-all
be used? -Wstack-protector
telling me? Is it suggesting that I decrease the buffer minimum size? -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:
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.
The warnings tell you what buffers the stack protection can't protect.
这篇关于何时以及如何使用 GCC 的堆栈保护功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!