[PATCH] i386/x86-64: Fix x87 information leak between processes

AMD K7/K8 CPUs only save/restore the FOP/FIP/FDP x87 registers in FXSAVE
when an exception is pending. This means the value leak through
context switches and allow processes to observe some x87 instruction
state of other processes.

This was actually documented by AMD, but nobody recognized it as
being different from Intel before.

The fix first adds an optimization: instead of unconditionally
calling FNCLEX after each FXSAVE test if ES is pending and skip
it when not needed. Then do a x87 load from a kernel variable to
clear FOP/FIP/FDP.

This means other processes always will only see a constant value
defined by the kernel in their FP state.

I took some pain to make sure to chose a variable that's already
in L1 during context switch to make the overhead of this low.

Also alternative() is used to patch away the new code on CPUs
who don't need it.

Patch for both i386/x86-64.

The problem was discovered originally by Jan Beulich. Richard
Brunner provided the basic code for the workarounds, with contribution
from Jan.

This is CVE-2006-1056

Cc: richard.brunner@amd.com
Cc: jbeulich@novell.com

Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by Andi Kleen and committed by Linus Torvalds 18bd057b 5dc5cf7d

+56 -6
+2
arch/i386/kernel/cpu/amd.c
··· 207 set_bit(X86_FEATURE_K7, c->x86_capability); 208 break; 209 } 210 211 display_cacheinfo(c); 212
··· 207 set_bit(X86_FEATURE_K7, c->x86_capability); 208 break; 209 } 210 + if (c->x86 >= 6) 211 + set_bit(X86_FEATURE_FXSAVE_LEAK, c->x86_capability); 212 213 display_cacheinfo(c); 214
+3 -1
arch/x86_64/kernel/process.c
··· 575 prev->userrsp = read_pda(oldrsp); 576 write_pda(oldrsp, next->userrsp); 577 write_pda(pcurrent, next_p); 578 /* This must be here to ensure both math_state_restore() and 579 - kernel_fpu_begin() work consistently. */ 580 unlazy_fpu(prev_p); 581 write_pda(kernelstack, 582 task_stack_page(next_p) + THREAD_SIZE - PDA_STACKOFFSET);
··· 575 prev->userrsp = read_pda(oldrsp); 576 write_pda(oldrsp, next->userrsp); 577 write_pda(pcurrent, next_p); 578 + 579 /* This must be here to ensure both math_state_restore() and 580 + kernel_fpu_begin() work consistently. 581 + And the AMD workaround requires it to be after DS reload. */ 582 unlazy_fpu(prev_p); 583 write_pda(kernelstack, 584 task_stack_page(next_p) + THREAD_SIZE - PDA_STACKOFFSET);
+4
arch/x86_64/kernel/setup.c
··· 930 if (c->x86 == 15 && ((level >= 0x0f48 && level < 0x0f50) || level >= 0x0f58)) 931 set_bit(X86_FEATURE_REP_GOOD, &c->x86_capability); 932 933 r = get_model_name(c); 934 if (!r) { 935 switch (c->x86) {
··· 930 if (c->x86 == 15 && ((level >= 0x0f48 && level < 0x0f50) || level >= 0x0f58)) 931 set_bit(X86_FEATURE_REP_GOOD, &c->x86_capability); 932 933 + /* Enable workaround for FXSAVE leak */ 934 + if (c->x86 >= 6) 935 + set_bit(X86_FEATURE_FXSAVE_LEAK, &c->x86_capability); 936 + 937 r = get_model_name(c); 938 if (!r) { 939 switch (c->x86) {
+1
include/asm-i386/cpufeature.h
··· 71 #define X86_FEATURE_P4 (3*32+ 7) /* P4 */ 72 #define X86_FEATURE_CONSTANT_TSC (3*32+ 8) /* TSC ticks at a constant rate */ 73 #define X86_FEATURE_UP (3*32+ 9) /* smp kernel running on up */ 74 75 /* Intel-defined CPU features, CPUID level 0x00000001 (ecx), word 4 */ 76 #define X86_FEATURE_XMM3 (4*32+ 0) /* Streaming SIMD Extensions-3 */
··· 71 #define X86_FEATURE_P4 (3*32+ 7) /* P4 */ 72 #define X86_FEATURE_CONSTANT_TSC (3*32+ 8) /* TSC ticks at a constant rate */ 73 #define X86_FEATURE_UP (3*32+ 9) /* smp kernel running on up */ 74 + #define X86_FEATURE_FXSAVE_LEAK (3*32+10) /* FXSAVE leaks FOP/FIP/FOP */ 75 76 /* Intel-defined CPU features, CPUID level 0x00000001 (ecx), word 4 */ 77 #define X86_FEATURE_XMM3 (4*32+ 0) /* Streaming SIMD Extensions-3 */
+26 -4
include/asm-i386/i387.h
··· 13 14 #include <linux/sched.h> 15 #include <linux/init.h> 16 #include <asm/processor.h> 17 #include <asm/sigcontext.h> 18 #include <asm/user.h> ··· 39 extern void kernel_fpu_begin(void); 40 #define kernel_fpu_end() do { stts(); preempt_enable(); } while(0) 41 42 /* 43 * These must be called with preempt disabled 44 */ 45 static inline void __save_init_fpu( struct task_struct *tsk ) 46 { 47 alternative_input( 48 - "fnsave %1 ; fwait ;" GENERIC_NOP2, 49 - "fxsave %1 ; fnclex", 50 X86_FEATURE_FXSR, 51 - "m" (tsk->thread.i387.fxsave) 52 - :"memory"); 53 task_thread_info(tsk)->status &= ~TS_USEDFPU; 54 } 55
··· 13 14 #include <linux/sched.h> 15 #include <linux/init.h> 16 + #include <linux/kernel_stat.h> 17 #include <asm/processor.h> 18 #include <asm/sigcontext.h> 19 #include <asm/user.h> ··· 38 extern void kernel_fpu_begin(void); 39 #define kernel_fpu_end() do { stts(); preempt_enable(); } while(0) 40 41 + /* We need a safe address that is cheap to find and that is already 42 + in L1 during context switch. The best choices are unfortunately 43 + different for UP and SMP */ 44 + #ifdef CONFIG_SMP 45 + #define safe_address (__per_cpu_offset[0]) 46 + #else 47 + #define safe_address (kstat_cpu(0).cpustat.user) 48 + #endif 49 + 50 /* 51 * These must be called with preempt disabled 52 */ 53 static inline void __save_init_fpu( struct task_struct *tsk ) 54 { 55 + /* Use more nops than strictly needed in case the compiler 56 + varies code */ 57 alternative_input( 58 + "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4, 59 + "fxsave %[fx]\n" 60 + "bt $7,%[fsw] ; jc 1f ; fnclex\n1:", 61 X86_FEATURE_FXSR, 62 + [fx] "m" (tsk->thread.i387.fxsave), 63 + [fsw] "m" (tsk->thread.i387.fxsave.swd) : "memory"); 64 + /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception 65 + is pending. Clear the x87 state here by setting it to fixed 66 + values. __per_cpu_offset[0] is a random variable that should be in L1 */ 67 + alternative_input( 68 + GENERIC_NOP8 GENERIC_NOP2, 69 + "emms\n\t" /* clear stack tags */ 70 + "fildl %[addr]", /* set F?P to defined value */ 71 + X86_FEATURE_FXSAVE_LEAK, 72 + [addr] "m" (safe_address)); 73 task_thread_info(tsk)->status &= ~TS_USEDFPU; 74 } 75
+1
include/asm-x86_64/cpufeature.h
··· 64 #define X86_FEATURE_REP_GOOD (3*32+ 4) /* rep microcode works well on this CPU */ 65 #define X86_FEATURE_CONSTANT_TSC (3*32+5) /* TSC runs at constant rate */ 66 #define X86_FEATURE_SYNC_RDTSC (3*32+6) /* RDTSC syncs CPU core */ 67 68 /* Intel-defined CPU features, CPUID level 0x00000001 (ecx), word 4 */ 69 #define X86_FEATURE_XMM3 (4*32+ 0) /* Streaming SIMD Extensions-3 */
··· 64 #define X86_FEATURE_REP_GOOD (3*32+ 4) /* rep microcode works well on this CPU */ 65 #define X86_FEATURE_CONSTANT_TSC (3*32+5) /* TSC runs at constant rate */ 66 #define X86_FEATURE_SYNC_RDTSC (3*32+6) /* RDTSC syncs CPU core */ 67 + #define X86_FEATURE_FXSAVE_LEAK (3*32+7) /* FIP/FOP/FDP leaks through FXSAVE */ 68 69 /* Intel-defined CPU features, CPUID level 0x00000001 (ecx), word 4 */ 70 #define X86_FEATURE_XMM3 (4*32+ 0) /* Streaming SIMD Extensions-3 */
+19 -1
include/asm-x86_64/i387.h
··· 72 #define set_fpu_swd(t,val) ((t)->thread.i387.fxsave.swd = (val)) 73 #define set_fpu_fxsr_twd(t,val) ((t)->thread.i387.fxsave.twd = (val)) 74 75 static inline int restore_fpu_checking(struct i387_fxsave_struct *fx) 76 { 77 int err; ··· 136 #endif 137 if (unlikely(err)) 138 __clear_user(fx, sizeof(struct i387_fxsave_struct)); 139 return err; 140 } 141 ··· 167 "i" (offsetof(__typeof__(*tsk), 168 thread.i387.fxsave))); 169 #endif 170 - __asm__ __volatile__("fnclex"); 171 } 172 173 static inline void kernel_fpu_begin(void)
··· 72 #define set_fpu_swd(t,val) ((t)->thread.i387.fxsave.swd = (val)) 73 #define set_fpu_fxsr_twd(t,val) ((t)->thread.i387.fxsave.twd = (val)) 74 75 + #define X87_FSW_ES (1 << 7) /* Exception Summary */ 76 + 77 + /* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception 78 + is pending. Clear the x87 state here by setting it to fixed 79 + values. The kernel data segment can be sometimes 0 and sometimes 80 + new user value. Both should be ok. 81 + Use the PDA as safe address because it should be already in L1. */ 82 + static inline void clear_fpu_state(struct i387_fxsave_struct *fx) 83 + { 84 + if (unlikely(fx->swd & X87_FSW_ES)) 85 + asm volatile("fnclex"); 86 + alternative_input(ASM_NOP8 ASM_NOP2, 87 + " emms\n" /* clear stack tags */ 88 + " fildl %%gs:0", /* load to clear state */ 89 + X86_FEATURE_FXSAVE_LEAK); 90 + } 91 + 92 static inline int restore_fpu_checking(struct i387_fxsave_struct *fx) 93 { 94 int err; ··· 119 #endif 120 if (unlikely(err)) 121 __clear_user(fx, sizeof(struct i387_fxsave_struct)); 122 + /* No need to clear here because the caller clears USED_MATH */ 123 return err; 124 } 125 ··· 149 "i" (offsetof(__typeof__(*tsk), 150 thread.i387.fxsave))); 151 #endif 152 + clear_fpu_state(&tsk->thread.i387.fxsave); 153 } 154 155 static inline void kernel_fpu_begin(void)