关键词:stack-protector、stack-protector-strong、stack-protector-all等等。

1. gcc栈保护机制stack-protector简介

gcc提供了栈保护机制stack-protector。关于stack-protector包含三个选项,分别是stack-protector、stack-protector-all、stack-protector-strong、stack-protector-explicit四种。

关于stack-protector原理、实现、效果、局限参考《GCC 中的编译器堆栈保护技术》。

gcc中对这几种选项的介绍如下:

-Wstack-protector
This option is only active when -fstack-protector is active. It warns about functions that are not protected against stack smashing. -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.

stack-protector:保护函数中通过alloca()分配缓存以及存在大于8字节的缓存。缺点是保护能力有限。

stack-protector-all:保护所有函数的栈。缺点是增加很多额外栈空间,增加程序体积。

stack-protector-strong:在stack-protector基础上,增加本地数组、指向本地帧栈地址空间保护。

stack-protector-explicit:在stack-protector基础上,增加程序中显式属性"stack_protect"空间。

如果要停止使用stack-protector功能,需要加上-fno-stack-protector。

stack-protector性能:stack-protector > stack-protector-strong > stack-protector-all。

stack-protector覆盖范围:stack-protector < stack-protector-strong < stack-protector-all。

2. stack-protector测试

针对stack-protector的测试,主要对比stack-protector、stack-protector-strong、stack-protector-all三个选项的区别。

#include <string.h>
#include <stdio.h> int main(void)
{
char array[] = {}; strcpy(array, "stackwilloverflow"); return ;
}

分别使用如下编译选项:(1) gcc stack.c -o stack -ggdb -fstack-protector、(2)gcc stack.c -o stack -ggdb -fstack-protector-strong、(3)gcc stack.c -o stack -ggdb -fstack-protector-all。

(1)未能检查出栈溢出,(2)、(3)都检查出了stack smashing detected。结论:可以看出stack-protector-strong和stack-protector-all相对于stack-protector更多的检测了栈溢出。

*** stack smashing detected ***: ./stack terminated
Aborted (core dumped)

查看core文件的bt full,可以勘测处发生栈溢出的点在array。

...
# 0x000014653e07915c in __GI___fortify_fail (msg=<optimized out>, msg@entry=0x14653e0ef481 "stack smashing detected") at fortify_fail.c:
do_abort =
# 0x000014653e079100 in __stack_chk_fail () at stack_chk_fail.c:
No locals.
# 0x00000000004005a1 in main () at stack.c:
array = "st"

修改array大小超过8字节之后,重新使用stack-protector进行测试。结论:当数组大小超过8字节过后,stack-protector才能检测出栈溢出。

#include <string.h>
#include <stdio.h> int main(void)
{
char array[] = {}; strcpy(array, "stackwilloverflowoooooooooo"); return ;
}

发现了stack smashing:

*** stack smashing detected ***: ./stack terminated
Aborted (core dumped)

gdb查看backtrace如下:

...
# 0x000015062304c15c in __GI___fortify_fail (msg=<optimized out>, msg@entry=0x1506230c2481 "stack smashing detected") at fortify_fail.c:
do_abort =
# 0x000015062304c100 in __stack_chk_fail () at stack_chk_fail.c:
No locals.
# 0x00000000004005b8 in main () at stack.c:
array = "stackwillo"

3. 内核中使用stack-protector

首先需要定义HAVE_CC_STACKPROTECTOR,然后通过make menuconfig进行配置。

路径为General setup->Stack Protector buffer overflow detection。

参考文档:《stack-protector-strong》、《-fstack-protector-strong》、《"Strong" stack protection for GCC》。

gcc栈溢出保护机制:stack-protector的更多相关文章

  1. 逆向工程学习第四天--Windows栈溢出保护机制(GS)原理及绕过测试

    GS简介: Windows的缓冲区安全监测机制(GS)可以有效的阻止经典的BOF攻击,因为GS会在函数调用前往函数栈帧内压入一个随机数(canary),然后等函数返回前,会对canary进行核查,判断 ...

  2. GCC栈溢出保护

    逆向过elf程序都知道,GCC的canary,x86_64下从fs:0x28偏移处获取,32位下从gs:0x14偏移处获取.但知道canary如何产生,为什么在这里取的人比较少. 下面以x86_64平 ...

  3. linux程序的常用保护机制

    操作系统提供了许多安全机制来尝试降低或阻止缓冲区溢出攻击带来的安全风险,包括DEP.ASLR等.在编写漏洞利用代码的时候,需要特别注意目标进程是否开启了DEP(Linux下对应NX).ASLR(Lin ...

  4. Linux中的保护机制

    Linux中的保护机制 在编写漏洞利用代码的时候,需要特别注意目标进程是否开启了NX.PIE等机制,例如存在NX的话就不能直接执行栈上的数据,存在PIE 的话各个系统调用的地址就是随机化的. 一:ca ...

  5. Linux保护机制和绕过方式

    Linux保护机制和绕过方式 CANNARY(栈保护) ​ 栈溢出保护是一种缓冲区溢出攻击缓解手段,当函数存在缓冲区溢出攻击漏洞时,攻击者可以覆盖栈上的返回地址来让shellcode能够得到执行.用C ...

  6. 编译驱动模块时,出现“stack protector enabled but no compiler support”[解决办法]【转】

    转自:http://blog.chinaunix.net/uid-26847859-id-3297170.html 原文地址:编译驱动模块时,出现“stack protector enabled bu ...

  7. Linux常用保护机制

    Linux程序常见用的一些保护机制 一.NX(Windows中的DEP) NX:No-eXecute.DEP:Data Execute Prevention 也就是数据不可执行,防止因为程序运行出现溢 ...

  8. 内存保护机制及绕过方法——通过伪造SEHOP链绕过SEHOP保护机制

    1.1    SEHOP保护机制 1.1.1    SEHOP工作原理: SEHOP保护机制的核心就是检查SEH链的完整性,其验证代码如下: BOOL RtlIsValidHandler(handle ...

  9. Hystrix针对不可用服务的保护机制以及引入缓存

    之前我写过一篇博文,通过案例了解Hystrix的各种基本使用方式,在这篇文章里,我们是通过Hystrix调用正常工作的服务,也就是说,Hytrix的保护机制并没有起作用,这里我们将在HystrixPr ...

随机推荐

  1. 《Netty Zookeeper Redis 高并发实战》 图书简介

    <Netty Zookeeper Redis 高并发实战> 图书简介 本书为 高并发社群 -- 疯狂创客圈 倾力编著, 高度剖析底层原理,深度解读面试难题 疯狂创客圈 Java 高并发[ ...

  2. OLTP

    On-Line Transaction Processing,联机事务处理过程(OLTP),也称为面向交易的处理过程 其基本特征是前台接收的用户数据可以立即传送到计算中心进行处理,并在很短的时间内给出 ...

  3. Jenkins之自动部署、代码安全扫描、自动化接口测试

    搭建Jenkins wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.reporpm --i ...

  4. Ansible-下部

    ansible-playbook  playbook是由一个或多个模块组成的,使用多个不同的模块,完成一件事情. ansible软件特点 可以实现批量管理可以实现批量部署ad-hoc(批量执行命令)- ...

  5. css实现内容不相同的左右两个div等高

    问题提出 现在有两个div左右排列,但是两个div的内容不相同,如何设置两个div的css做到在两个div等高排列呢? 下面是网上找的3种实现方法,觉得很有代表性,所以索性收藏起来. 方法一 通过父元 ...

  6. 计算机组成原理——I/O接口以及I/O设备数据传送控制方式

    接口可以看作是两个部件之间交接的部分.硬件与硬件之间有接口,硬件与软件之间有接口,软件与软件之间也有接口. 这里我们所说的I/O接口,一边连接着主机,一边连接着外设. I/O接口的功能 I/O接口的基 ...

  7. 4. java基础之修饰符

    其他修饰符 public 可以修饰属性.方法.构造方法.类 protected 可以修饰属性.方法.构造方法 default 可以修饰属性.方法.构造方法.类 private 可以修饰属性.方法.构造 ...

  8. java基础学习笔记 第二周(面向对象)

    Day01 什么是抽象数据类型:将不同数据类型的集合组成的一个整体,我们称为抽象数据类型 类就是一个抽象数据类型 成员变量:类中的数据类型就是成员变量(属性) 方法:类中的一些行为就是方法 面向过程( ...

  9. 使用“npm init”初始化项目

    使用npm init初始化项目 为什么要使用npm init初始化项目 在node开发中使用npm init会生成一个pakeage.json文件,这个文件主要是用来记录这个项目的详细信息的,它会将我 ...

  10. Python语法速查: 7. 函数基础

    返回目录 (1)函数基本 ● 函数是第一类对象 Python中万物皆对象,所有对象都是第一类的(first class),函数也不例外,也是第一类对象.既然是对象,那就可以当作普通的对象数据处理,比如 ...