找熟人做网站的弊端,网站建设 模板网站,如何注册品牌名称和商标,开发网站最后进行的步骤是什么说明
内核页表安全的最终目标是#xff1a;将内核使用到的内存页#xff08;内核与module占用#xff09;的属性#xff08;读/写/可执行#xff09;配置成安全的#xff0c;即#xff1a;代码段和rodata段只读#xff0c;非代码段不能执行等#xff0c;用来防御堆栈…说明
内核页表安全的最终目标是将内核使用到的内存页内核与module占用的属性读/写/可执行配置成安全的即代码段和rodata段只读非代码段不能执行等用来防御堆栈执行和代码段被修改的攻击。
内核页表安全
相关选项
CONFIG_DEBUG_KERNELy (4.11版本之前)
CONFIG_DEBUG_RODATAy (4.11版本)
CONFIG_STRICT_KERNEL_RWXy (4.11至最新版本)开启该选项后内核的text段和rodata段内存将会变成只读并且非代码段的内存将会变成不可执行。
影响
kernel code, init等占用的预留内存会变大。
开启配置后内核链接脚本中会对kernel的代码段等内存做size对齐操作默认2MB对齐预留内存占用会多3~4MB如下
* 开启配置启动log (kernel code,init大小2M对齐)
Memory: 32360K/131072K available (4096K kernel code, 277K rwdata, 1240K rodata, 2048K init, 139K bss, 98712K reserved, 0K cma-reserved)
* 未开启配置启动log
Memory: 35788K/131072K available (3122K kernel code, 282K rwdata, 1304K rodata, 144K init, 140K bss, 95284K reserved, 0K cma-reserved)arm 内核链接脚本
//file: arch/arm/include/asm/pgtable-2level.h:
#define SECTION_SHIFT 20
...//file: arch/arm/kernel/vmlinux.lds.S
...
#ifdef CONFIG_STRICT_KERNEL_RWX. ALIGN(1SECTION_SHIFT);
#else. ALIGN(PAGE_SIZE);
#endif
...阻止使用gdb调试软件断点jtag硬件断点可用blog 说明。
module页表安全
相关选项
CONFIG_STRICT_MODULE_RWX作用和CONFIG_STRICT_KERNEL_RWX一样只是作用对象变成了module。
实现原理
初始化 rodata_enabled值开启配置后默认为true。
//file: init/main.c
...
#if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
bool rodata_enabled __ro_after_init true;
static int __init set_debug_rodata(char *str)
{return strtobool(str, rodata_enabled);
}
__setup(rodata, set_debug_rodata);
#endif
...mmap
//file: arch/arm64/mm/mmu.c
static void __init map_kernel(pgd_t *pgdp)
{static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,vmlinux_initdata, vmlinux_data;/** External debuggers may need to write directly to the text* mapping to install SW breakpoints. Allow this (only) when* explicitly requested with rodataoff.*/pgprot_t text_prot rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;/** If we have a CPU that supports BTI and a kernel built for* BTI then mark the kernel executable text as guarded pages* now so we dont have to rewrite the page tables later.*/if (arm64_early_this_cpu_has_bti())text_prot __pgprot_modify(text_prot, PTE_GP, PTE_GP);/** Only rodata will be remapped with different permissions later on,* all other segments are allowed to use contiguous mappings.*/map_kernel_segment(pgdp, _text, _etext, text_prot, vmlinux_text, 0,VM_NO_GUARD);.....
}现状
未开启相关配置的Linux内核内存页权限没做什么限制。以上选项对于较新的CPU架构和kernel版本已经是默认开启Linux内核中会根据ARCHCPU架构来确定是否默认支持如下
//file: arch/Kconfig
config ARCH_OPTIONAL_KERNEL_RWXdef_bool nconfig ARCH_OPTIONAL_KERNEL_RWX_DEFAULTdef_bool nconfig ARCH_HAS_STRICT_KERNEL_RWXdef_bool nconfig STRICT_KERNEL_RWXbool Make kernel text and rodata read-only if ARCH_OPTIONAL_KERNEL_RWXdepends on ARCH_HAS_STRICT_KERNEL_RWXdefault !ARCH_OPTIONAL_KERNEL_RWX || ARCH_OPTIONAL_KERNEL_RWX_DEFAULT...//file: arch/riscv/Kconfig // riscv架构cpu配置
config RISCVdef_bool y...select ARCH_HAS_STRICT_KERNEL_RWX if MMU...select ARCH_OPTIONAL_KERNEL_RWX if ARCH_HAS_STRICT_KERNEL_RWXselect ARCH_OPTIONAL_KERNEL_RWX_DEFAULT也可以手动对以上选项进行配置如下
General architecture-dependent options ---[*] Make kernel text and rodata read-only (NEW)[*] Set loadable kernel module data as NX and text as RO (NEW)对于服务器产品是默认开启选项但是在封闭的小型嵌入式设备上的Linux时常会选择不开启该配置来节省内存。