VC的Buffer Security Check
来源:互联网 发布:淘宝后台手机端 编辑:程序博客网 时间:2024/05/22 03:27
C/C++对内存的访问非常自由,也会带来一些问题。比如对一个局部数组变量的越界写入就可能导致栈数据被意外修改。
为了防止线程栈被有意无意错误改写,VC编译器提供了/GS开关,指示编译器在栈的某些变量的前后留出空白内存区。编译器在原函数体代码前面插入代码,向空白内存区中写入某个数值(security cookie),并在原函数体代码后面插入代码检查空白内存区中的数值是否保持不变。如果发生了变化,则报异常。这被称为Buffer Security Check。
所有符合下面条件的局部变量会受到这一措施的保护:
- An array that is larger than 4 bytes, has more than two elements,and has an element type that is not a pointer type.
- A data structure whose size is more than 8 bytes and contains nopointers.
- A buffer allocated by using the _allocafunction.
- Any class or structure that contains a GS buffer.
比如,以下变量是符合条件的:
- char buffer[20];
- int buffer[20];
- struct { int a; int b; int c; int d; } myStruct;
- struct { int a; char buf[20]; };
比如,以下是不符合条件的(x86平台下),不会生成security cookies:
- char *pBuf[20];
- void *pv[20];
- char buf[4];
- int buf[2];
- struct { int a; int b; };
打开GS开关后,有较大可能性检查到栈内存的非法写入,显然这也不是百分之百的保证。在VS2012下面,不管是Debug还是Release,GS开关默认都是被打开的。
测试程序
#include <cstring>#include <stdlib.h>// Vulnerable functionvoid vulnerable(const char *str) {char buffer[10] = "abcdefg";int i = 0;char buffer2[20] = "ABCDEFGHIJKLMN";int k = 30;int l = 50;for( i = 0 ; i < 10; i++)buffer[i] = '0' + i;for( k = 0 ; k < 20; k++)buffer2[k] = '0' + i;}int main() { char large_buffer[] = ""; vulnerable(large_buffer);}
下面利用VC的调试功能,对vulnerable函数进行栈分析。下图是运行到vulnerable函数的时候,VC调试器的截图。其中标注了栈顶指针的值、各个局部变量的位置和值。可以看到每个buffer数组的前后都分配了一组"CC"作为隔离带,而变量k和l之间没有隔离带。
- VC的Buffer Security Check
- VC check box 的用法
- VC check box 的用法
- 地铁“安检” security check
- Security Check
- vc中 Check Box 的使用方法
- VC中MFC check box的用法
- VC中MFC check box的用法
- Check Point R80 Security Management
- VC控件 Check Box
- VC check box用法
- VC中check box的两种用法
- VC Check box (转载新浪博客于超峰的)
- Security check diff between Everett and Whidbey
- Check Point NGX R65 Security Administration
- ping 网络(数据中心security check)
- myeclipse 2015 SECURITY ALERT INTEGERITY CHECK ERROR
- java.security.cert.CertPathValidatorException: timestamp check failed
- LOG
- Android酱油笔记之AlertDialog
- ABAP modify screen:修改屏幕,实现隐藏、禁止输入字段
- InterviewStreet题目(1)
- mysq数据库导入
- VC的Buffer Security Check
- 通过new BitmapDrawable()方法将Bitmap转化为Drawable的getIntrinsicHeight()和getIntrinsicWidth() 问题。
- Linux下常用命令及举例
- 用javah 导出类的头文件, 常见的错误及正确的使用方法
- PL/SQL异常处理总结
- BB.Flashback.Pro.v4.1.1.2498.Cracked.by.yoza[UpK]
- memset的用法
- Android 4.2 Input 流程分析
- IOS设置静态库的生成目录为默认的build目录