x86/bhi: BHI mitigation can trigger warning in #DB handler
Subsystems (3)
PATCH 1/1 x86/bhi: BHI mitigation can trigger warning in #DB handler
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 flag 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>
--- arch/x86/entry/entry_64_compat.S | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/entry/entry_64_compat.S +6 -1
@@ -91,7 +91,6 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) IBRS_ENTER UNTRAIN_RET - CLEAR_BRANCH_HISTORY /* * SYSENTER doesn't filter flags, so we need to clear NT and AC
@@ -116,6 +115,12 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) jnz .Lsysenter_fix_flags .Lsysenter_flags_fixed: + /* + * CLEAR_BRANCH_HISTORY can call other functions. It should be invoked + * after making sure TF is cleared because single-step is ignored only + * for instructions inside the entry_SYSENTER_compat function. + */ + CLEAR_BRANCH_HISTORY movq %rsp, %rdi call do_SYSENTER_32 jmp sysret32_from_system_call
Signature · 1 line
2.39.3
Quoted 82 lines · Alexandre Chartre
When BHI mitigation is enabled, if sysenter is invoked with the TF flag set then
entry_SYSENTER_compatusesCLEAR_BRANCH_HISTORYand calls theclear_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 theentry_SYSENTER_compatfunction.To address this issue,
entry_SYSENTER_compat()should useCLEAR_BRANCH_HISTORYafter making sure flag 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>Fixes 7390db8aea0d ("x86/bhi: Add support for clearing branch history at syscall entry")Reported-by Suman Maity <[email protected]>Signed-off-by Alexandre Chartre <[email protected]>--- arch/x86/entry/entry_64_compat.S | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)arch/x86/entry/entry_64_compat.S +6
-1@@ -91,7 +91,6 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) IBRS_ENTER UNTRAIN_RET - CLEAR_BRANCH_HISTORY /* * SYSENTER doesn't filter flags, so we need to clear NT and AC@@ -116,6 +115,12 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) jnz .Lsysenter_fix_flags .Lsysenter_flags_fixed: + /* + * CLEAR_BRANCH_HISTORY can call other functions. It should be invoked + * after making sure TF is cleared because single-step is ignored only + * for instructions inside the entry_SYSENTER_compat function. + */ + CLEAR_BRANCH_HISTORY movq %rsp, %rdi call do_SYSENTER_32 jmp sysret32_from_system_callSignature · 1 line
2.39.3
Hi,
This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.
You are receiving this message because of the following common error(s) as indicated below:
- You have marked a patch with a "Fixes:" tag for a commit that is in an older released kernel, yet you do not have a cc: stable line in the signed-off-by area at all, which means that the patch will not be applied to any older kernel releases. To properly fix this, please follow the documented rules in the Documentation/process/stable-kernel-rules.rst file for how to resolve this.
If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.
thanks,
greg k-h's patch email bot
Quoted 13 lines · Alexandre Chartre
arch/x86/entry/entry_64_compat.S +0
-1@@ -91,7 +91,6 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) IBRS_ENTER UNTRAIN_RET - CLEAR_BRANCH_HISTORY /* * SYSENTER doesn't filter flags, so we need to clear NT and AC
arch/x86/entry/entry_64_compat.S SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) · line 123
Quoted 9 lines
arch/x86/entry/entry_64_compat.S +6
-0@@ -116,6 +115,12 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) jnz .Lsysenter_fix_flags .Lsysenter_flags_fixed: + /* + * CLEAR_BRANCH_HISTORY can call other functions. It should be invoked + * after making sure TF is cleared because single-step is ignored only + * for instructions inside the entry_SYSENTER_compat function. + */ + CLEAR_BRANCH_HISTORY
Exactly the same is true of UNTRAIN_RET, although it will only manifest in i386 builds running on AMD hardware (SYSENTER is #UD on AMD hardware in Long mode.)
#DB is IST so does handle it's own speculation safety. It should be safe to move all the speculation safety logic in the sysenter handler to after .Lsysenter_flags_fixed:, I think?
~Andrew
Quoted 28 lines · Andrew Cooper
Quoted 13 lines · Alexandre Chartre
arch/x86/entry/entry_64_compat.S +0
-1@@ -91,7 +91,6 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) IBRS_ENTER UNTRAIN_RET - CLEAR_BRANCH_HISTORY /* * SYSENTER doesn't filter flags, so we need to clear NT and ACarch/x86/entry/entry_64_compat.S SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) · line 123
Quoted 9 lines
arch/x86/entry/entry_64_compat.S +6
-0@@ -116,6 +115,12 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) jnz .Lsysenter_fix_flags .Lsysenter_flags_fixed: + /* + * CLEAR_BRANCH_HISTORY can call other functions. It should be invoked + * after making sure TF is cleared because single-step is ignored only + * for instructions inside the entry_SYSENTER_compat function. + */ + CLEAR_BRANCH_HISTORYExactly the same is true of
UNTRAIN_RET, although it will only manifest in i386 builds running on AMD hardware (SYSENTER is #UD on AMD hardware in Long mode.)
Quoted 4 lines
#DB is IST so does handle it's own speculation safety. It should be safe to move all the speculation safety logic in the sysenter handler to after .Lsysenter_flags_fixed:, I think?
Right, so something like this:
arch/x86/entry/entry_64_compat.S +9 -4
@@ -89,10 +89,6 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) cld - IBRS_ENTER - UNTRAIN_RET - CLEAR_BRANCH_HISTORY - /* * SYSENTER doesn't filter flags, so we need to clear NT and AC * ourselves. To save a few cycles, we can check whether
@@ -116,6 +112,15 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) jnz .Lsysenter_fix_flags .Lsysenter_flags_fixed: + /* + * CPU bugs mitigations mechanisms can call other functions. They + * should be invoked after making sure TF is cleared because + * single-step is ignored only for instructions inside the + * entry_SYSENTER_compat function. + */ + IBRS_ENTER + UNTRAIN_RET + CLEAR_BRANCH_HISTORY movq %rsp, %rdi call do_SYSENTER_32 jmp sysret32_from_system_call
alex.
Quoted 60 lines · Alexandre Chartre
Quoted 30 lines · Andrew Cooper
Quoted 16 lines · Alexandre Chartre
arch/x86/entry/entry_64_compat.S +0
-0@@ -91,7 +91,6 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL)
IBRS_ENTER
UNTRAIN_RET
-CLEAR_BRANCH_HISTORY
/*
* SYSENTER doesn't filter flags, so we need to clear NT and ACarch/x86/entry/entry_64_compat.S +0
-0@@ -116,6 +115,12 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL)jnz .Lsysenter_fix_flags
.Lsysenter_flags_fixed:Quoted 8 lines
+ /*
+ *CLEAR_BRANCH_HISTORYcan call other functions. It should be invoked
+ * after making sure TF is cleared because single-step is ignored only
+ * for instructions inside theentry_SYSENTER_compatfunction. + */
+CLEAR_BRANCH_HISTORYExactly the same is true of
UNTRAIN_RET, although it will only manifest in i386 builds running on AMD hardware (SYSENTER is #UD on AMD hardware in Long mode.)Quoted 4 lines
#DB is IST so does handle it's own speculation safety. It should be safe to move all the speculation safety logic in the sysenter handler to after .Lsysenter_flags_fixed:, I think?
Right, so something like this:
arch/x86/entry/entry_64_compat.S +0
-0@@ -89,10 +89,6 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL)
cld
-IBRS_ENTER
-UNTRAIN_RET
-CLEAR_BRANCH_HISTORY
-
/*
* SYSENTER doesn't filter flags, so we need to clear NT and AC
* ourselves. To save a few cycles, we can check whetherarch/x86/entry/entry_64_compat.S +0
-0@@ -116,6 +112,15 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL)jnz .Lsysenter_fix_flags
.Lsysenter_flags_fixed:
+ /*
Quoted 8 lines
+ * CPU bugs mitigations mechanisms can call other functions. They + * should be invoked after making sure TF is cleared because + * single-step is ignored only for instructions inside the + *
entry_SYSENTER_compatfunction.
+ */
+IBRS_ENTER
+UNTRAIN_RET
+CLEAR_BRANCH_HISTORY
Yeah - this looks rather better.
Although I'd suggest a blank line here if you're going to formalise the patch.
~Andrew
Quoted 6 lines · Alexandre Chartre
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"); }
Quoted 6 lines
$ 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.
Should we wrap up this gem and put it with the other entry selftests?
Quoted 7 lines · Dave Hansen
Quoted 6 lines · Alexandre Chartre
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"); }
Quoted 8 lines
Quoted 6 lines
$ 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.
Should we wrap up this gem and put it with the other entry selftests?
It looks like tools/testing/selftests/x86/single_step_syscall.c tests sysenter with TF set but it doesn't check if the kernel issues any warning.
alex.
Quoted 14 lines · Alexandre Chartre
Quoted 12 lines · Dave Hansen
Quoted 11 lines · Alexandre Chartre
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)
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
The program is expected to crash, and the #DB handler will issue a warning.
Should we wrap up this gem and put it with the other entry selftests?
It looks like
tools/testing/selftests/x86/single_step_syscall.ctests sysenter with TF set but it doesn't check if the kernel issues any warning.
But shouldn't the SIGSEGV still cause the selftest to notice?
Also, there should be a selftest for NT. (mis)handling of that will take the entire kernel down.
AC for good measure too, as that's the other flag handled specially.
~Andrew
Quoted 5 lines · Alexandre Chartre
Quoted 1 line
Should we wrap up this gem and put it with the other entry selftests?
It looks like
tools/testing/selftests/x86/single_step_syscall.ctests sysenter with TF set but it doesn't check if the kernel issues any warning.
Does it actually trip the warning though? I'm a bit surprised that nobody reported it if so.
Quoted 9 lines · Dave Hansen
Quoted 5 lines · Alexandre Chartre
Quoted 1 line
Should we wrap up this gem and put it with the other entry selftests?
It looks like
tools/testing/selftests/x86/single_step_syscall.ctests sysenter with TF set but it doesn't check if the kernel issues any warning.Does it actually trip the warning though? I'm a bit surprised that nobody reported it if so.
single_step_syscall does trigger the warning:
$ ./single_step_syscall [RUN] Set TF and check nop [OK] Survived with TF set and 26 traps [RUN] Set TF and check syscall-less opportunistic sysret [OK] Survived with TF set and 30 traps [RUN] Set TF and check a fast syscall [OK] Survived with TF set and 40 traps [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened [RUN] Set TF and check SYSENTER Got SIGSEGV with RIP=ed7fe579, TF=256 [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened
On the console:
[ 1546.656252] WARNING: CPU: 124 PID: 8413 at arch/x86/kernel/traps.c:1009 exc_debug_kernel+0xd2/0x160 ... [ 1546.656352] RIP: 0010:clear_bhb_loop+0x0/0xb0
alex.
Quoted 14 lines · Alexandre Chartre
Quoted 9 lines · Dave Hansen
Quoted 5 lines · Alexandre Chartre
Quoted 1 line
Should we wrap up this gem and put it with the other entry selftests?
It looks like
tools/testing/selftests/x86/single_step_syscall.ctests sysenter with TF set but it doesn't check if the kernel issues any warning.Does it actually trip the warning though? I'm a bit surprised that nobody reported it if so.
single_step_syscalldoes trigger the warning:
Quoted 13 lines
$ ./single_step_syscall [RUN] Set TF and check nop [OK] Survived with TF set and 26 traps [RUN] Set TF and check syscall-less opportunistic sysret [OK] Survived with TF set and 30 traps [RUN] Set TF and check a fast syscall [OK] Survived with TF set and 40 traps [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened [RUN] Set TF and check SYSENTER Got SIGSEGV with RIP=ed7fe579, TF=256 [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened
:-/
What about the exit code?
I find the absence of a [FAIL] concerning...
~Andrew
Quoted 29 lines · Andrew Cooper
Quoted 14 lines · Alexandre Chartre
Quoted 9 lines · Dave Hansen
Quoted 5 lines · Alexandre Chartre
Quoted 1 line
Should we wrap up this gem and put it with the other entry selftests?
It looks like
tools/testing/selftests/x86/single_step_syscall.ctests sysenter with TF set but it doesn't check if the kernel issues any warning.Does it actually trip the warning though? I'm a bit surprised that nobody reported it if so.
single_step_syscalldoes trigger the warning:Quoted 13 lines
$ ./single_step_syscall [RUN] Set TF and check nop [OK] Survived with TF set and 26 traps [RUN] Set TF and check syscall-less opportunistic sysret [OK] Survived with TF set and 30 traps [RUN] Set TF and check a fast syscall [OK] Survived with TF set and 40 traps [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened [RUN] Set TF and check SYSENTER Got SIGSEGV with RIP=ed7fe579, TF=256 [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened
Quoted 6 lines
:-/
What about the exit code?
I find the absence of a [FAIL] concerning...
$ ./single_step_syscall [RUN] Set TF and check nop [OK] Survived with TF set and 26 traps [RUN] Set TF and check syscall-less opportunistic sysret [OK] Survived with TF set and 30 traps [RUN] Set TF and check a fast syscall [OK] Survived with TF set and 40 traps [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened [RUN] Set TF and check SYSENTER Got SIGSEGV with RIP=bb44b579, TF=256 [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened
$ echo $? 0
The program runs as expected (but it doesn't expect much than a SIGSEGV). It triggers a warning from the kernel but it doesn't check if a warning was produced.
alex.
Quoted 53 lines · Alexandre Chartre
Quoted 29 lines · Andrew Cooper
Quoted 14 lines · Alexandre Chartre
Quoted 9 lines · Dave Hansen
Quoted 5 lines · Alexandre Chartre
Quoted 1 line
Should we wrap up this gem and put it with the other entry selftests?
It looks like
tools/testing/selftests/x86/single_step_syscall.ctests sysenter with TF set but it doesn't check if the kernel issues any warning.Does it actually trip the warning though? I'm a bit surprised that nobody reported it if so.
single_step_syscalldoes trigger the warning:Quoted 13 lines
$ ./single_step_syscall [RUN] Set TF and check nop [OK] Survived with TF set and 26 traps [RUN] Set TF and check syscall-less opportunistic sysret [OK] Survived with TF set and 30 traps [RUN] Set TF and check a fast syscall [OK] Survived with TF set and 40 traps [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened [RUN] Set TF and check SYSENTER Got SIGSEGV with RIP=ed7fe579, TF=256 [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happenedQuoted 6 lines
:-/
What about the exit code?
I find the absence of a [FAIL] concerning...
$ ./single_step_syscall [RUN] Set TF and check nop [OK] Survived with TF set and 26 traps [RUN] Set TF and check syscall-less opportunistic sysret [OK] Survived with TF set and 30 traps [RUN] Set TF and check a fast syscall [OK] Survived with TF set and 40 traps [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened [RUN] Set TF and check SYSENTER Got SIGSEGV with RIP=bb44b579, TF=256 [RUN] Fast syscall with TF cleared [OK] Nothing unexpected happened
Quoted 7 lines
$ echo $? 0The program runs as expected (but it doesn't expect much than a SIGSEGV). It triggers a warning from the kernel but it doesn't check if a warning was produced.
Actually it checks that TF isn't cleared after the sysenter, but that's all.
alex.