Skip to content

Commit a13b9d0

Browse files
keesKAGA-KOKO
authored andcommitted
x86/cpu: Use pinning mask for CR4 bits needing to be 0
The X86_CR4_FSGSBASE bit of CR4 should not change after boot[1]. Older kernels should enforce this bit to zero, and newer kernels need to enforce it depending on boot-time configuration (e.g. "nofsgsbase"). To support a pinned bit being either 1 or 0, use an explicit mask in combination with the expected pinned bit values. [1] https://lore.kernel.org/lkml/[email protected] Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/202006082013.71E29A42@keescook
1 parent cc5277f commit a13b9d0

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

arch/x86/kernel/cpu/common.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ static __always_inline void setup_umip(struct cpuinfo_x86 *c)
347347
cr4_clear_bits(X86_CR4_UMIP);
348348
}
349349

350+
/* These bits should not change their value after CPU init is finished. */
351+
static const unsigned long cr4_pinned_mask =
352+
X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP | X86_CR4_FSGSBASE;
350353
static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
351354
static unsigned long cr4_pinned_bits __ro_after_init;
352355

@@ -371,20 +374,20 @@ EXPORT_SYMBOL(native_write_cr0);
371374

372375
void native_write_cr4(unsigned long val)
373376
{
374-
unsigned long bits_missing = 0;
377+
unsigned long bits_changed = 0;
375378

376379
set_register:
377380
asm volatile("mov %0,%%cr4": "+r" (val), "+m" (cr4_pinned_bits));
378381

379382
if (static_branch_likely(&cr_pinning)) {
380-
if (unlikely((val & cr4_pinned_bits) != cr4_pinned_bits)) {
381-
bits_missing = ~val & cr4_pinned_bits;
382-
val |= bits_missing;
383+
if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) {
384+
bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits;
385+
val = (val & ~cr4_pinned_mask) | cr4_pinned_bits;
383386
goto set_register;
384387
}
385-
/* Warn after we've set the missing bits. */
386-
WARN_ONCE(bits_missing, "CR4 bits went missing: %lx!?\n",
387-
bits_missing);
388+
/* Warn after we've corrected the changed bits. */
389+
WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n",
390+
bits_changed);
388391
}
389392
}
390393
#if IS_MODULE(CONFIG_LKDTM)
@@ -419,7 +422,7 @@ void cr4_init(void)
419422
if (boot_cpu_has(X86_FEATURE_PCID))
420423
cr4 |= X86_CR4_PCIDE;
421424
if (static_branch_likely(&cr_pinning))
422-
cr4 |= cr4_pinned_bits;
425+
cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits;
423426

424427
__write_cr4(cr4);
425428

@@ -434,10 +437,7 @@ void cr4_init(void)
434437
*/
435438
static void __init setup_cr_pinning(void)
436439
{
437-
unsigned long mask;
438-
439-
mask = (X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP);
440-
cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & mask;
440+
cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask;
441441
static_key_enable(&cr_pinning.key);
442442
}
443443

0 commit comments

Comments
 (0)