diff mbox series

x86/bhi: BHI mitigation can trigger warning in #DB handler

Message ID 20240523123322.3326690-1-alexandre.chartre@oracle.com (mailing list archive)
State New, archived
Headers show
Series x86/bhi: BHI mitigation can trigger warning in #DB handler | expand

Commit Message

Alexandre Chartre May 23, 2024, 12:33 p.m. UTC
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>

Fixes: 7390db8aea0d ("x86/bhi: Add support for clearing branch history at syscall entry")
Reported-by: Suman Maity <suman.m.maity@oracle.com>
Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
---
 arch/x86/entry/entry_64_compat.S | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Greg Kroah-Hartman May 23, 2024, 12:36 p.m. UTC | #1
On Thu, May 23, 2024 at 02:33:22PM +0200, Alexandre Chartre wrote:
> 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>
> 
> Fixes: 7390db8aea0d ("x86/bhi: Add support for clearing branch history at syscall entry")
> Reported-by: Suman Maity <suman.m.maity@oracle.com>
> Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
> ---
>  arch/x86/entry/entry_64_compat.S | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S
> index 11c9b8efdc4c..7fa04edc87e9 100644
> --- a/arch/x86/entry/entry_64_compat.S
> +++ b/arch/x86/entry/entry_64_compat.S
> @@ -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
> -- 
> 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
Andrew Cooper May 23, 2024, 12:42 p.m. UTC | #2
On 23/05/2024 1:33 pm, Alexandre Chartre wrote:
> diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S
> index 11c9b8efdc4c..7fa04edc87e9 100644
> --- a/arch/x86/entry/entry_64_compat.S
> +++ b/arch/x86/entry/entry_64_compat.S
> @@ -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

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
Alexandre Chartre May 23, 2024, 12:59 p.m. UTC | #3
On 5/23/24 14:42, Andrew Cooper wrote:
> On 23/05/2024 1:33 pm, Alexandre Chartre wrote:
>> diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S
>> index 11c9b8efdc4c..7fa04edc87e9 100644
>> --- a/arch/x86/entry/entry_64_compat.S
>> +++ b/arch/x86/entry/entry_64_compat.S
>> @@ -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
> 
> 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?
> 

Right, so something like this:

--- a/arch/x86/entry/entry_64_compat.S
+++ b/arch/x86/entry/entry_64_compat.S
@@ -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.
Dave Hansen May 23, 2024, 2:28 p.m. UTC | #4
On 5/23/24 05:33, Alexandre Chartre wrote:
> 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.

Should we wrap up this gem and put it with the other entry selftests?
Alexandre Chartre May 23, 2024, 2:52 p.m. UTC | #5
On 5/23/24 16:28, Dave Hansen wrote:
> On 5/23/24 05:33, Alexandre Chartre wrote:
>> 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.
> 
> 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.
Andrew Cooper May 23, 2024, 2:59 p.m. UTC | #6
On 23/05/2024 1:59 pm, Alexandre Chartre wrote:
>
>
> On 5/23/24 14:42, Andrew Cooper wrote:
>> On 23/05/2024 1:33 pm, Alexandre Chartre wrote:
>>> diff --git a/arch/x86/entry/entry_64_compat.S
>>> b/arch/x86/entry/entry_64_compat.S
>>> index 11c9b8efdc4c..7fa04edc87e9 100644
>>> --- a/arch/x86/entry/entry_64_compat.S
>>> +++ b/arch/x86/entry/entry_64_compat.S
>>> @@ -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
>>
>> 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?
>>
>
> Right, so something like this:
>
> --- a/arch/x86/entry/entry_64_compat.S
> +++ b/arch/x86/entry/entry_64_compat.S
> @@ -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

Yeah - this looks rather better.

Although I'd suggest a blank line here if you're going to formalise the
patch.

~Andrew
Andrew Cooper May 23, 2024, 3:34 p.m. UTC | #7
On 23/05/2024 3:52 pm, Alexandre Chartre wrote:
>
> On 5/23/24 16:28, Dave Hansen wrote:
>> On 5/23/24 05:33, Alexandre Chartre wrote:
>>> 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.
>>
>> 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.

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
Dave Hansen May 23, 2024, 3:36 p.m. UTC | #8
On 5/23/24 07:52, Alexandre Chartre wrote:
>> 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.

Does it actually trip the warning though? I'm a bit surprised that
nobody reported it if so.
Alexandre Chartre May 23, 2024, 5:03 p.m. UTC | #9
On 5/23/24 17:36, Dave Hansen wrote:
> On 5/23/24 07:52, Alexandre Chartre wrote:
>>> 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.
> 
> 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.
Andrew Cooper May 23, 2024, 5:53 p.m. UTC | #10
On 23/05/2024 6:03 pm, Alexandre Chartre wrote:
>
> On 5/23/24 17:36, Dave Hansen wrote:
>> On 5/23/24 07:52, Alexandre Chartre wrote:
>>>> 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.
>>
>> 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

:-/

What about the exit code?

I find the absence of a [FAIL] concerning...

~Andrew
Alexandre Chartre May 23, 2024, 6:27 p.m. UTC | #11
On 5/23/24 19:53, Andrew Cooper wrote:
> On 23/05/2024 6:03 pm, Alexandre Chartre wrote:
>>
>> On 5/23/24 17:36, Dave Hansen wrote:
>>> On 5/23/24 07:52, Alexandre Chartre wrote:
>>>>> 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.
>>>
>>> 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
> 
> :-/
> 
> 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.
Alexandre Chartre May 23, 2024, 6:30 p.m. UTC | #12
On 5/23/24 20:27, Alexandre Chartre wrote:
> 
> 
> On 5/23/24 19:53, Andrew Cooper wrote:
>> On 23/05/2024 6:03 pm, Alexandre Chartre wrote:
>>>
>>> On 5/23/24 17:36, Dave Hansen wrote:
>>>> On 5/23/24 07:52, Alexandre Chartre wrote:
>>>>>> 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.
>>>>
>>>> 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
>>
>> :-/
>>
>> 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.
> 

Actually it checks that TF isn't cleared after the sysenter, but that's all.

alex.
diff mbox series

Patch

diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S
index 11c9b8efdc4c..7fa04edc87e9 100644
--- a/arch/x86/entry/entry_64_compat.S
+++ b/arch/x86/entry/entry_64_compat.S
@@ -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