gcc选项-stack-protector栈保护机制

本文介绍了C语言中的stack-protector选项,如何防止栈溢出攻击,以及stack-protector、stack-protector-all、stack-protector-strong和stack-protector-explicit的不同特性。通过实例演示了这些选项在Makefile中的应用和对栈溢出检测的影响。
摘要由CSDN通过智能技术生成

介绍

stack-protector是一个安全相关的编译器选项,用于防止栈溢出攻击。当启用这个选项时,编译器会在函数的栈帧中插入一个称为"canary"的特殊值,并在函数返回之前检查这个值是否被篡改。如果"canary"值发生变化,这意味着栈被非法修改,编译器会触发一个信号(通常是SIGABRT),导致程序异常终止。这样可以防止攻击者通过栈溢出来执行任意代码。

关于stack-protector包含三个选项,分别是stack-protector、stack-protector-all、stack-protector-strong、stack-protector-explicit四种。

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-all > stack-protector-strong > stack-protector

例子

C语言代码

#include <string.h>
#include <stdio.h>

int main(void)
{
    char array[2] = {0};
    strcpy(array, "aaaaaaaaaa");
    printf("%s\n", array);
    return 0;
}

Makefile

TARGET := gcc_para_test

CROSS_COMPILE = arm-linux-gnueabihf-

STRIP = $(CROSS_COMPILE)strip
CC = $(CROSS_COMPILE)gcc
AR = $(CROSS_COMPILE)ar

CFLAGS += -Wall -std=gnu99 -g
CFLAGS += -fstack-protector-all

OBJS += main.o  

all: $(TARGET)
$(TARGET):$(OBJS)
	$(CC) -o $@ $^ $(LDFLAGS)
    
clean:
	-rm $(OBJS) $(TARGET) -f

运行结果

当Makefile中不使用-fstack-protecto相关参数和使用参数-fstack-protecto时,输出如下:

# ./gcc_para_test 
aaaaaaaaaa
Segmentation fault

未能检查出栈溢出

当Makefile中使用-fstack-protector-all参数和时,输出如下:

# ./gcc_para_test 
aaaaaaaaaa
*** stack smashing detected ***: ./gcc_para_test terminated
Aborted

可以看出stack-protector-strong和stack-protector-all相对于stack-protector更多的检测了栈溢出。

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

参考链接

gcc栈溢出保护机制:stack-protector

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值