linux 日志保护机制,Linux常用保护机制

Linux程序常见用的一些保护机制

一、NX(Windows中的DEP)

NX:No-eXecute、DEP:Data Execute Prevention

也就是数据不可执行,防止因为程序运行出现溢出而使得攻击者的shellcode可能会在数据区尝试执行的情况。

gcc默认开启,选项有:

gcc -o test test.c // 默认情况下,开启NX保护

gcc -z execstack -o test test.c // 禁用NX保护

gcc -z noexecstack -o test test.c // 开启NX保护

二、PIE(ASLR)

PIE:Position-Independent Excutable、ASLR:Address Space Layout Randomization

fpie/fPIE:需要和选项-pie一起使用开启pie选项编译可执行文件使得elf拥有共享库属性,可以在内存任何地方加载运行。与之相似的还有fpic/fPIC,关于其说明于https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html

-fpic

Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target machine. Such code accesses all constant addresses through a global offset table (GOT). The dynamic loader resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is part of the operating system). If the GOT size for the linked executable exceeds a machine-specific maximum size, you get an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead. (These maximums are 8k on the SPARC, 28k on AArch64 and 32k on the m68k and RS/6000. The x86 has no such limit.)

Position-independent code requires special support, and therefore works only on certain machines. For the x86, GCC supports PIC for System V but not for the Sun 386i. Code generated for the IBM RS/6000 is always position-independent.

When this flag is set, the macros `__pic__` and `__PIC__` are defined to 1.

-fPIC

If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table.This option makes a difference on AArch64, m68k, PowerPC and SPARC.

Position-independent code requires special support, and therefore works only on certain machines.

When this flag is set, the macros `__pic__` and `__PIC__` are defined to 2.

-fpie

-fPIE

These options are similar to -fpic and -fPIC, but the generated position-independent code can be only linked into executables. Usually these options are used to compile code that will be linked using the -pie GCC option.

-fpie and -fPIE both define the macros `__pie__` and `__PIE__`. The macros have the value 1 for `-fpie` and 2 for `-fPIE`.

区别在于fpic/fPIC用于共享库的编译,fpie/fPIE则是pie文件编译的选项。文档中说 pic(位置无关代码)生成的共享库只能链接于可执行文件,之后根据自己编译简单C程序,pie正常运行,即如网上许多文章说的 pie 选项生成的位置无关代码可假定于本程序,但是我也没看出fpie/fPIE有啥区别,只是宏定义只为1和2的区别,貌似...

编译命令(默认不开启PIE):

gcc -fpie -pie -o test test.c // 开启PIE

gcc -fPIE -pie -o test test.c // 开启PIE

gcc -fpic -o test test.c // 开启PIC

gcc -fPIC -o test test.c // 开启PIC

gcc -no-pie -o test test.c // 关闭PIE

而ASLR(地址空间随机化),当初设计时只负责栈、库、堆等段的地址随机化。ASLR的值存于/proc/sys/kernel/randomize_va_space中,如下:

0 - 表示关闭进程地址空间随机化。

1 - 表示将mmap的基址,stack和vdso页面随机化。

2 - 表示在1的基础上增加栈(heap)的随机化。(默认)

更改其值方式:echo 0 > /proc/sys/kernel/randomize_va_space

vDSO:virtual dynamic shared object;

mmap:即内存的映射。

PIE 则是负责可执行程序的基址随机。

以下摘自Wiki:

Position-independent executable (PIE) implements a random base address for the main executable binary and has been in place since 2003. It provides the same address randomness to the main executable as being used for the shared libraries.

PIE为ASLR的一部分,ASLR为系统功能,PIE则为编译选项。

注: 在heap分配时,有mmap()和brk()两种方式,由malloc()分配内存时调用,分配较小时brk,否则mmap,128k区别。参考文章:https://blog.csdn.net/gfgdsg/article/details/42709943

三、Canary(栈保护)

??Canary对于栈的保护,在函数每一次执行时,在栈上随机产生一个Canary值。之后当函数执行结束返回时检测Canary值,若不一致系统则报出异常。

Canaries or canary words are known values that are placed between a buffer and control data on the stack to monitor buffer overflows. When the buffer overflows, the first data to be corrupted will usually be the canary, and a failed verification of the canary data will therefore alert of an overflow, which can then be handled, for example, by invalidating the corrupted data. A canary value should not be confused with a sentinel value.

??如上所述,Canary值置于缓冲区和控制数据之间,当缓冲区溢出,该值被覆写,从而可以检测以判断是否运行出错或是受到攻击。缓解缓冲区溢出攻击。

编译选项:

gcc -o test test.c //默认关闭

gcc -fno-stack-protector -o test test.c //禁用栈保护

gcc -fstack-protector -o test test.c //启用堆栈保护,不过只为局部变量中含有 char 数组的函数插入保护代码

gcc -fstack-protector-all -o test test.c //启用堆栈保护,为所有函数插入保护代码

四、RELRO(RELocation Read Only)

在Linux中有两种RELRO模式:”Partial RELRO“ 和 ”Full RELRO“。Linux中Partical RELRO默认开启。

Partial RELRO:

编译命令:

gcc -o test test.c // 默认部分开启

gcc -Wl,-z,relro -o test test.c // 开启部分RELRO

gcc -z lazy -o test test.c // 部分开启

该ELF文件的各个部分被重新排序。内数据段(internal data sections)(如.got,.dtors等)置于程序数据段(program‘s data sections)(如.data和.bss)之前;

无 plt 指向的GOT是只读的;

GOT表可写(应该是与上面有所区别的)。

Full RELRO:

编译命令:

gcc -Wl,-z,relro,-z,now -o test test.c // 开启Full RELRO

gcc -z now -o test test.c // 全部开启

支持Partial模式的所有功能;

整个GOT表映射为只读的。

gcc -z norelro -o a a.c // RELRO关闭,即No RELRO

Note:

.dtors:当定义有.dtors的共享库被加载时调用;

在bss或数据溢出错误的情况下,Partial和Full RELRO保护ELF内数据段不被覆盖。 但只有Full RELRO可以缓解GOT表覆写攻击,但是相比较而言开销较大,因为程序在启动之前需要解析所有的符号。

参考文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《深入Linux设备驱动程序内核机制》是一本非常经典的Linux设备驱动程序内核开发方面的专业书籍。该书由深入浅出的方式,全面介绍了Linux设备驱动程序的基本知识、内核框架、字符设备驱动程序、块设备驱动程序以及网络设备驱动程序等内容。 该书首先详细介绍了Linux操作系统的内核架构和设备驱动程序的基本概念,让读者对Linux内核的组成、系统调用、进程管理等有一个清晰的了解。接着,该书介绍了设备驱动程序的开发流程和编写规范,并重点讲解了字符设备驱动程序的开发方法。通过具体的代码实例,读者可以深入了解字符设备的注册、读写操作以及地址映射等关键步骤。 此外,该书还涵盖了块设备驱动程序和网络设备驱动程序等领域的知识。块设备驱动程序的开发涉及磁盘操作、缓冲区管理等内容,而网络设备驱动程序的开发则包括套接字的初始化、数据传输等方面。通过学习这些内容,读者不仅可以掌握Linux设备驱动程序的内核机制,还能够应对更加复杂的设备驱动开发工作。 综上所述,《深入Linux设备驱动程序内核机制》是一本非常实用和权威的Linux设备驱动程序开发方面的书籍。通过阅读本书,读者可以系统地学习Linux设备驱动的原理和开发方法,提升自己在Linux内核开发领域的技能和水平。无论是对于初学者还是有一定经验的开发者来说,都是一本不可或缺的参考书。 ### 回答2: "深入Linux设备驱动程序内核机制"是一本非常重要的图书,主要介绍了Linux操作系统中的设备驱动程序开发原理和实践技巧。这本书非常适合那些对Linux设备驱动程序开发感兴趣的人阅读。 首先,这本书详细讲解了Linux设备驱动程序的内核机制。它介绍了设备驱动程序的基本概念、内核模块的加载和卸载、驱动程序的注册和注销、设备的访问和控制等重要知识点。通过深入了解这些机制,读者可以对设备驱动程序的工作原理有清晰的认识。 此外,这本书还对Linux设备模型进行了详细的解释。它介绍了字符设备、块设备和网络设备等不同类型的设备,并讲解了它们在内核中的实现方式和工作原理。同时,它还提供了许多实例和示例代码,方便读者理解和实践。 除了内核机制和设备模型,这本书还介绍了在Linux设备驱动程序开发过程中常用的工具和技术。比如,它详细介绍了调试技术、日志记录、内核模块参数传递等实用的开发技巧。这些内容对于提高驱动程序的稳定性和可靠性非常有帮助。 总之,"深入Linux设备驱动程序内核机制"是一本非常重要的图书,它深入探讨了Linux设备驱动程序的内核机制,提供了丰富的实例和示例代码,帮助读者更好地理解和应用设备驱动程序开发技术。无论是对于初学者还是有经验的开发者来说,这本书都是不可或缺的学习资料。 ### 回答3: 《深入Linux设备驱动程序内核机制》是一本在CSDN上提供的PDF文档,涵盖了Linux设备驱动程序的内核机制。本书主要介绍了Linux内核中设备驱动的基本知识、原理和设计方法。其中详细讲解了设备驱动程序的注册、驱动与设备的交互、设备的初始化和释放、设备操作的原理等内容。 该书着重介绍了字符设备驱动、块设备驱动和网络设备驱动等常见类型的设备驱动程序的内核实现机制。对于想要了解Linux设备驱动开发的开发人员来说,这本书提供了非常有价值的知识,可以帮助他们理解和掌握设备驱动程序的编写和调试技巧。 该书从理论和实践的角度出发,结合了大量的源代码示例和实际案例,使读者更好地理解和掌握设备驱动程序的内核机制。此外,书中还介绍了设备树和设备模型的相关知识,以及错误处理和调试技术。通过阅读和学习该书,读者可以更好地理解和应用Linux设备驱动程序的内核机制。 总之,《深入Linux设备驱动程序内核机制》这本PDF文档对于想要深入理解Linux设备驱动程序的人来说是非常有价值的资料。通过CSDN渠道获取该文档可以方便大家进行学习和参考。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值