Commit 0e2f6c3e authored by Tim Chen's avatar Tim Chen Committed by Marcelo Henrique Cerri

x86/enter: Use IBRS on syscall and interrupts

CVE-2017-5753
CVE-2017-5715

Set IBRS upon kernel entrance via syscall and interrupts. Clear it upon exit.
Signed-off-by: default avatarTim Chen <tim.c.chen@linux.intel.com>
Signed-off-by: default avatarAndy Whitcroft <apw@canonical.com>
(backported from commit bb6c1a01e82fb0eb14d1229fd71a99ed285d330d)
Signed-off-by: default avatarAndy Whitcroft <apw@canonical.com>
parent f011884a
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <asm/smap.h> #include <asm/smap.h>
#include <asm/pgtable_types.h> #include <asm/pgtable_types.h>
#include <asm/kaiser.h> #include <asm/kaiser.h>
#include <asm/spec_ctrl.h>
#include <linux/err.h> #include <linux/err.h>
/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */ /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
...@@ -173,6 +174,8 @@ GLOBAL(entry_SYSCALL_64_after_swapgs) ...@@ -173,6 +174,8 @@ GLOBAL(entry_SYSCALL_64_after_swapgs)
pushq %r11 /* pt_regs->r11 */ pushq %r11 /* pt_regs->r11 */
sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */ sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
ENABLE_IBRS
testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS) testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
jnz tracesys jnz tracesys
entry_SYSCALL_64_fastpath: entry_SYSCALL_64_fastpath:
...@@ -212,6 +215,9 @@ entry_SYSCALL_64_fastpath: ...@@ -212,6 +215,9 @@ entry_SYSCALL_64_fastpath:
movq RIP(%rsp), %rcx movq RIP(%rsp), %rcx
movq EFLAGS(%rsp), %r11 movq EFLAGS(%rsp), %r11
RESTORE_C_REGS_EXCEPT_RCX_R11 RESTORE_C_REGS_EXCEPT_RCX_R11
DISABLE_IBRS
/* /*
* This opens a window where we have a user CR3, but are * This opens a window where we have a user CR3, but are
* running in the kernel. This makes using the CS * running in the kernel. This makes using the CS
...@@ -355,6 +361,8 @@ GLOBAL(int_ret_from_sys_call) ...@@ -355,6 +361,8 @@ GLOBAL(int_ret_from_sys_call)
* perf profiles. Nothing jumps here. * perf profiles. Nothing jumps here.
*/ */
syscall_return_via_sysret: syscall_return_via_sysret:
DISABLE_IBRS
/* rcx and r11 are already restored (see code above) */ /* rcx and r11 are already restored (see code above) */
RESTORE_C_REGS_EXCEPT_RCX_R11 RESTORE_C_REGS_EXCEPT_RCX_R11
/* /*
...@@ -369,6 +377,7 @@ syscall_return_via_sysret: ...@@ -369,6 +377,7 @@ syscall_return_via_sysret:
USERGS_SYSRET64 USERGS_SYSRET64
opportunistic_sysret_failed: opportunistic_sysret_failed:
/* XXX: might we need a DISABLE_IBRS */
/* /*
* This opens a window where we have a user CR3, but are * This opens a window where we have a user CR3, but are
* running in the kernel. This makes using the CS * running in the kernel. This makes using the CS
...@@ -537,6 +546,8 @@ END(irq_entries_start) ...@@ -537,6 +546,8 @@ END(irq_entries_start)
SWAPGS SWAPGS
SWITCH_KERNEL_CR3 SWITCH_KERNEL_CR3
ENABLE_IBRS
/* /*
* We need to tell lockdep that IRQs are off. We can't do this until * We need to tell lockdep that IRQs are off. We can't do this until
* we fix gsbase, and we should do it before enter_from_user_mode * we fix gsbase, and we should do it before enter_from_user_mode
...@@ -595,6 +606,7 @@ GLOBAL(retint_user) ...@@ -595,6 +606,7 @@ GLOBAL(retint_user)
mov %rsp,%rdi mov %rsp,%rdi
call prepare_exit_to_usermode call prepare_exit_to_usermode
TRACE_IRQS_IRETQ TRACE_IRQS_IRETQ
DISABLE_IBRS
SWITCH_USER_CR3 SWITCH_USER_CR3
SWAPGS SWAPGS
jmp restore_regs_and_iret jmp restore_regs_and_iret
...@@ -1061,6 +1073,8 @@ ENTRY(paranoid_entry) ...@@ -1061,6 +1073,8 @@ ENTRY(paranoid_entry)
movq %rax, %cr3 movq %rax, %cr3
2: 2:
#endif #endif
ENABLE_IBRS_CLOBBER
ret ret
END(paranoid_entry) END(paranoid_entry)
...@@ -1087,6 +1101,7 @@ ENTRY(paranoid_exit) ...@@ -1087,6 +1101,7 @@ ENTRY(paranoid_exit)
/* No ALTERNATIVE for X86_FEATURE_KAISER: paranoid_entry sets %ebx */ /* No ALTERNATIVE for X86_FEATURE_KAISER: paranoid_entry sets %ebx */
testl $2, %ebx /* SWITCH_USER_CR3 needed? */ testl $2, %ebx /* SWITCH_USER_CR3 needed? */
jz paranoid_exit_no_switch jz paranoid_exit_no_switch
DISABLE_IBRS
SWITCH_USER_CR3 SWITCH_USER_CR3
paranoid_exit_no_switch: paranoid_exit_no_switch:
#endif #endif
...@@ -1126,6 +1141,8 @@ ENTRY(error_entry) ...@@ -1126,6 +1141,8 @@ ENTRY(error_entry)
*/ */
SWAPGS SWAPGS
ENABLE_IBRS
.Lerror_entry_from_usermode_after_swapgs: .Lerror_entry_from_usermode_after_swapgs:
/* /*
* We need to tell lockdep that IRQs are off. We can't do this until * We need to tell lockdep that IRQs are off. We can't do this until
...@@ -1176,6 +1193,7 @@ ENTRY(error_entry) ...@@ -1176,6 +1193,7 @@ ENTRY(error_entry)
* We came from an IRET to user mode, so we have user gsbase. * We came from an IRET to user mode, so we have user gsbase.
* Switch to kernel gsbase: * Switch to kernel gsbase:
*/ */
ENABLE_IBRS_CLOBBER
SWAPGS SWAPGS
/* /*
...@@ -1326,6 +1344,7 @@ ENTRY(nmi) ...@@ -1326,6 +1344,7 @@ ENTRY(nmi)
movq %rax, %cr3 movq %rax, %cr3
2: 2:
#endif #endif
ENABLE_IBRS
call do_nmi call do_nmi
#ifdef CONFIG_KAISER #ifdef CONFIG_KAISER
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <asm/smap.h> #include <asm/smap.h>
#include <asm/pgtable_types.h> #include <asm/pgtable_types.h>
#include <asm/kaiser.h> #include <asm/kaiser.h>
#include <asm/spec_ctrl.h>
#include <linux/linkage.h> #include <linux/linkage.h>
#include <linux/err.h> #include <linux/err.h>
...@@ -99,6 +100,8 @@ ENTRY(entry_SYSENTER_compat) ...@@ -99,6 +100,8 @@ ENTRY(entry_SYSENTER_compat)
pushq %r8 /* pt_regs->r15 = 0 */ pushq %r8 /* pt_regs->r15 = 0 */
cld cld
ENABLE_IBRS
/* /*
* Sysenter doesn't filter flags, so we need to clear NT * Sysenter doesn't filter flags, so we need to clear NT
* ourselves. To save a few cycles, we can check whether * ourselves. To save a few cycles, we can check whether
...@@ -197,6 +200,8 @@ ENTRY(entry_SYSCALL_compat) ...@@ -197,6 +200,8 @@ ENTRY(entry_SYSCALL_compat)
pushq %r8 /* pt_regs->r14 = 0 */ pushq %r8 /* pt_regs->r14 = 0 */
pushq %r8 /* pt_regs->r15 = 0 */ pushq %r8 /* pt_regs->r15 = 0 */
ENABLE_IBRS
/* /*
* User mode is traced as though IRQs are on, and SYSENTER * User mode is traced as though IRQs are on, and SYSENTER
* turned them off. * turned them off.
...@@ -212,6 +217,7 @@ ENTRY(entry_SYSCALL_compat) ...@@ -212,6 +217,7 @@ ENTRY(entry_SYSCALL_compat)
/* Opportunistic SYSRET */ /* Opportunistic SYSRET */
sysret32_from_system_call: sysret32_from_system_call:
TRACE_IRQS_ON /* User mode traces as IRQs on. */ TRACE_IRQS_ON /* User mode traces as IRQs on. */
DISABLE_IBRS
SWITCH_USER_CR3 SWITCH_USER_CR3
movq RBX(%rsp), %rbx /* pt_regs->rbx */ movq RBX(%rsp), %rbx /* pt_regs->rbx */
movq RBP(%rsp), %rbp /* pt_regs->rbp */ movq RBP(%rsp), %rbp /* pt_regs->rbp */
...@@ -305,6 +311,8 @@ ENTRY(entry_INT80_compat) ...@@ -305,6 +311,8 @@ ENTRY(entry_INT80_compat)
pushq %r15 /* pt_regs->r15 */ pushq %r15 /* pt_regs->r15 */
cld cld
ENABLE_IBRS
/* /*
* User mode is traced as though IRQs are on, and the interrupt * User mode is traced as though IRQs are on, and the interrupt
* gate turned them off. * gate turned them off.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment