Skip to content

Commit c38d169

Browse files
achartrevijay-suman
authored andcommitted
x86/bhi: Avoid warning in #DB handler due to BHI mitigation
[ Upstream commit ac8b270 ] When BHI mitigation is enabled, if SYSENTER is invoked with the TF flag set then entry_SYSENTER_compat() uses CLEAR_BRANCH_HISTORY and calls the clear_bhb_loop() before the TF flag is cleared. This causes the #DB handler (exc_debug_kernel()) to issue a warning because single-step is used outside the entry_SYSENTER_compat() function. To address this issue, entry_SYSENTER_compat() should use CLEAR_BRANCH_HISTORY after making sure the TF flag is cleared. The problem can be reproduced with the following sequence: $ cat sysenter_step.c int main() { asm("pushf; pop %ax; bts $8,%ax; push %ax; popf; sysenter"); } $ gcc -o sysenter_step sysenter_step.c $ ./sysenter_step Segmentation fault (core dumped) The program is expected to crash, and the #DB handler will issue a warning. Kernel log: WARNING: CPU: 27 PID: 7000 at arch/x86/kernel/traps.c:1009 exc_debug_kernel+0xd2/0x160 ... RIP: 0010:exc_debug_kernel+0xd2/0x160 ... Call Trace: <#DB> ? show_regs+0x68/0x80 ? __warn+0x8c/0x140 ? exc_debug_kernel+0xd2/0x160 ? report_bug+0x175/0x1a0 ? handle_bug+0x44/0x90 ? exc_invalid_op+0x1c/0x70 ? asm_exc_invalid_op+0x1f/0x30 ? exc_debug_kernel+0xd2/0x160 exc_debug+0x43/0x50 asm_exc_debug+0x1e/0x40 RIP: 0010:clear_bhb_loop+0x0/0xb0 ... </#DB> <TASK> ? entry_SYSENTER_compat_after_hwframe+0x6e/0x8d </TASK> [ bp: Massage commit message. ] Fixes: 7390db8 ("x86/bhi: Add support for clearing branch history at syscall entry") Reported-by: Suman Maity <[email protected]> Signed-off-by: Alexandre Chartre <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Reviewed-by: Andrew Cooper <[email protected]> Reviewed-by: Pawan Gupta <[email protected]> Reviewed-by: Josh Poimboeuf <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sasha Levin <[email protected]> (cherry picked from commit db56615e96c439e13783d7715330e824b4fd4b84) FOF: 0924 Signed-off-by: Vijayendra Suman <[email protected]>
1 parent 5199da0 commit c38d169

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

arch/x86/entry/entry_64_compat.S

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL)
8989

9090
cld
9191

92-
IBRS_ENTER
93-
UNTRAIN_RET
94-
CLEAR_BRANCH_HISTORY
95-
9692
/*
9793
* SYSENTER doesn't filter flags, so we need to clear NT and AC
9894
* ourselves. To save a few cycles, we can check whether
@@ -116,6 +112,16 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL)
116112
jnz .Lsysenter_fix_flags
117113
.Lsysenter_flags_fixed:
118114

115+
/*
116+
* CPU bugs mitigations mechanisms can call other functions. They
117+
* should be invoked after making sure TF is cleared because
118+
* single-step is ignored only for instructions inside the
119+
* entry_SYSENTER_compat function.
120+
*/
121+
IBRS_ENTER
122+
UNTRAIN_RET
123+
CLEAR_BRANCH_HISTORY
124+
119125
movq %rsp, %rdi
120126
call do_SYSENTER_32
121127
/* XEN PV guests always use IRET path */

0 commit comments

Comments
 (0)