Skip to content

Commit 8dbec27

Browse files
keesKAGA-KOKO
authored andcommitted
x86/asm: Pin sensitive CR0 bits
With sensitive CR4 bits pinned now, it's possible that the WP bit for CR0 might become a target as well. Following the same reasoning for the CR4 pinning, pin CR0's WP bit. Contrary to the cpu feature dependend CR4 pinning this can be done with a constant value. Suggested-by: Peter Zijlstra <[email protected]> Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Dave Hansen <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 873d50d commit 8dbec27

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

arch/x86/include/asm/special_insns.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,20 @@ static inline unsigned long native_read_cr0(void)
3131

3232
static inline void native_write_cr0(unsigned long val)
3333
{
34-
asm volatile("mov %0,%%cr0": : "r" (val), "m" (__force_order));
34+
unsigned long bits_missing = 0;
35+
36+
set_register:
37+
asm volatile("mov %0,%%cr0": "+r" (val), "+m" (__force_order));
38+
39+
if (static_branch_likely(&cr_pinning)) {
40+
if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
41+
bits_missing = X86_CR0_WP;
42+
val |= bits_missing;
43+
goto set_register;
44+
}
45+
/* Warn after we've set the missing bits. */
46+
WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
47+
}
3548
}
3649

3750
static inline unsigned long native_read_cr2(void)

0 commit comments

Comments
 (0)