Supervisor mode access prevention

Linux内核将引入Intel的SMAP特性,旨在阻止用户空间代码对内核内存的读写操作,增强系统的整体安全性。此特性预计在Linux 3.7版本中出现,并通过指令控制用户空间与内核空间之间的访问权限,减少潜在的漏洞利用风险。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Operating system designers and hardware designers tend to put a lot of thought into how the kernel can be protected from user-space processes. The security of the system as a whole depends on that protection. But there can also be value in protecting user space from the kernel. The Linux kernel will soon have support for a new Intel processor feature intended to make that possible.

Under anything but the strangest (out of tree) memory configurations, the kernel's memory is always mapped, so user-space code could conceivably read and modify it. But the page protections are set to disallow that access; any attempt by user space to examine or modify the kernel's part of the address space will result in a segmentation violation (SIGSEGV) signal. Access in the other direction is rather less controlled: when the processor is in kernel mode, it has full access to any address that is valid in the page tables. Or nearly full access; the processor will still not normally allow writes to read-only memory, but that check can be disabled when the need arises.

Intel's new "Supervisor Mode Access Prevention" (SMAP) feature changes that situation; those wanting the details can find them starting on page 408 of this reference manual [PDF]. This extension defines a new SMAP bit in the CR4 control register; when that bit is set, any attempt to access user-space memory while running in a privileged mode will lead to a page fault. Linux support for this feature has been posted by H. Peter Anvin to generally positive reviews; it could show up in the mainline as early as 3.7.

Naturally, there are times when the kernel needs to work with user-space memory. To that end, Intel has defined a separate "AC" flag that controls the SMAP feature. If the AC flag is set, SMAP protection is in force; otherwise access to user-space memory is allowed. Two new instructions (STAC and CLAC) are provided to manipulate that flag relatively quickly. Unsurprisingly, much of Peter's patch set is concerned with adding STAC and CLAC instructions in the right places. User-space access functions (get_user(), for example, or copy_to_user()) clearly need to have user-space access enabled. Other places include transitions between kernel and user mode, futex operations, floating-point unit state saving, and so on. Signal handling, as usual, has special requirements; Peter had to make some significant changes to allow signal delivery to happen without excessive overhead.

Speaking of overhead, support for this feature will clearly have its costs. User-space access functions tend to be expanded inline, so there will be a lot of STAC and CLAC instructions spread around the kernel. The "alternatives" mechanism is used to patch them out if the SMAP feature is not in use (either not supported by the kernel or disabled with the nosmap boot flag), but the kernel will grow a little regardless. The STAC and CLAC instructions also require a little time to execute. Thus far, no benchmarks have been posted to quantify what the cost is; one assumes that it is small but not nonexistent.

The kernel will treat SMAP violations like it treats any other bad pointer access: the result will be an oops.

One might well ask what the value of this protection is, given that the kernel can turn it off at will. The answer is that it can block a whole class of exploits where the kernel is fooled into reading from (or writing to) user-space memory by mistake. The set of null pointer vulnerabilities exposed a few years ago is one obvious example. There have been many situations where an attacker has found a way to get the kernel to use a bad pointer, while the cases where the attacker could execute arbitrary code in kernel space (before exploiting the bad pointer) have been far less common. SMAP should block the more common attacks nicely.

The other benefit, of course, is simply finding kernel bugs. Driver writers (should) know that they cannot dereference user-space pointers directly from the kernel, but code that does so tends to work on some architectures anyway. With SMAP enabled, that kind of mistake will be found and fixed earlier, before the bad code is shipped in a mainline kernel. As is so often the case, there is real value in having the system enforce the rules that developers are supposed to be following.

Linus liked the patch set and nobody else has complained, so the changes have found their way into the "tip" tree. That makes it quite likely that we will see them again quite soon, probably once the 3.7 merge window opens. It will take a little longer, though, to get processors that support this feature; SMAP is set to first appear in the Haswell line, which should start shipping in 2013. But, once the hardware is available, Linux will be able to take advantage of this new feature.


转载于:https://my.oschina.net/linuxhunter/blog/84365

### 设置位以禁用控制寄存器的编程操作 在计算机体系结构中,为了防止恶意软件或其他未授权程序修改重要的系统状态,通常会采用多种安全措施。对于控制流执行技术而言,在启用该特性的情况下可以有效阻止非法跳转指令的发生[^1]。 然而针对具体如何设置位来禁用对某些敏感资源如控制寄存器的操作,则取决于具体的硬件平台架构设计。以x86架构为例,在其保护模式下可以通过配置相应的权限级别以及页面属性等方式来进行访问控制[^2]: - **CR0 寄存器**:此寄存器包含了多个标志用于定义CPU的工作方式。其中WP(Write Protect)位决定了是否允许向只读页面写入数据;PE(Protection Enable)位则启用了整个保护机制。 - **CR4 寄存器**:现代处理器增加了更多高级功能的支持选项,比如通过开启或关闭特定的功能模块。例如SMAP (Supervisor Mode Access Prevention) 和 SMEP (Supervisor Mode Execution Protection),这两个特性能进一步增强系统的安全性。 如果目标是在操作系统内核层面实现对控制寄存器本身的防护,那么可能涉及到如下方法之一: #### 方法一: 使用 CR0 的 WP 位 当设置了 CR0 中的 WP 位之后, 即使处于最高特权级(Ring 0), 对于标记为不可写的用户态代码段仍然无法被改写。虽然这并不是直接“禁用”了对所有控制寄存器的操作,但在一定程度上提高了整体的安全性。 ```assembly mov eax, cr0 ; 将当前cr0值加载到eax中 or al, 0x10 ; 设置wp位(第四个bit) mov cr0, eax ; 更新回cr0 ``` #### 方法二: 利用 CR4 上的相关位域 一些较新的 CPU 可能支持额外的安全扩展,像前面提到过的 SMAP/SMEP 。这些都可以通过对 CR4 进行适当设定而激活。特别是SMEP能够使得运行在监督者模式下的进程不能够执行来自用户空间映射区域内的任何指令序列,从而间接达到了限制目的。 ```assembly mov eax, cr4 ; 获取现有cr4值 bts eax, 20 ; 启用smap(supervisor mode access prevention) ; 或者 bts eax, 21 ; 启用smep(supervisor mode execution protection) mov cr4, eax ; 应用更改后的cr4值 ``` 需要注意的是上述汇编代码片段仅作为概念说明用途,并不代表实际可行的最佳实践方案。真实环境中应当依据所使用的具体硬件手册指导完成相应配置工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值