x86/speculation: Avoid LFENCE in FILL_RETURN_BUFFER on CPUs that lack it
Subsystems (4)
PATCH 1/1 x86/speculation: Avoid LFENCE in FILL_RETURN_BUFFER on CPUs that lack it
From: Ben Hutchings <[email protected]>
The mitigation for PBRSB includes adding LFENCE instructions to the RSB filling sequence. However, RSB filling is done on some older CPUs that don't support the LFENCE instruction.
Define and use a BARRIER_NOSPEC macro which makes the LFENCE conditional on X86_FEATURE_LFENCE_RDTSC, like the barrier_nospec() macro defined for C code in <asm/barrier.h>.
References: https://bugs.debian.org/1017425
---
Re-sending this with properly matched From address and server. Apologies if you got 2 copies.
Ben.
arch/x86/include/asm/nospec-branch.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
arch/x86/include/asm/nospec-branch.h +7 -4
@@ -34,6 +34,11 @@#define RSB_CLEAR_LOOPS 32/* To forcibly overwrite all entries */ +#ifdef __ASSEMBLY__ + +/* Prevent speculative execution past this barrier. */ +#define BARRIER_NOSPEC ALTERNATIVE "", "lfence", X86_FEATURE_LFENCE_RDTSC +/* * Google experimented with loop-unrolling and this turned out to be * the optimal version - two calls, each with their own speculation
@@ -62,9 +67,7 @@dec reg; \ jnz 771b; \/* barrier for jnz misprediction */ \ - lfence; - -#ifdef __ASSEMBLY__ +BARRIER_NOSPEC ;/* * This should be used immediately before an indirect jump/call. It tells
@@ -138,7 +141,7 @@ int3 .Lunbalanced_ret_guard_\@: add $(BITS_PER_LONG/8), %_ASM_SP - lfence +BARRIER_NOSPEC .endm/*
Peter Zijlstra
maintainerQuoted 6 lines · Ben Hutchings
From: Ben Hutchings <[email protected]>
The mitigation for PBRSB includes adding LFENCE instructions to the RSB filling sequence. However, RSB filling is done on some older CPUs that don't support the LFENCE instruction.
Wait; what? There are chips that enable the RSB mitigations and DONT have LFENCE ?!?
Peter Zijlstra
maintainerQuoted 10 lines · Peter Zijlstra
Quoted 6 lines · Ben Hutchings
From: Ben Hutchings <[email protected]>
The mitigation for PBRSB includes adding LFENCE instructions to the RSB filling sequence. However, RSB filling is done on some older CPUs that don't support the LFENCE instruction.
Wait; what? There are chips that enable the RSB mitigations and DONT have LFENCE ?!?
So I gave in and clicked on the horrible bugzilla thing. Apparently this is P3/Athlon64 era crud.
Anyway, the added LFENCE isn't because of retbleed; it is because you can steer the jnz and terminate the loop early and then not actually complete the RSB stuffing.
New insights etc.. So it's a geniune fix for the existing rsb stuffing.
I'm not entirly sure what to do here. On the one hand, it's 32bit, so who gives a crap, otoh we shouldn't break these ancient chips either I suppose.
How's something like so then? It goes on top of my other patch cleaning up this RSB mess:
https://lkml.kernel.org/r/Yv9m%2FhuNJLuyviIn%40worktop.programming.kicks-ass.net
---
Subject: x86/nospec: Fix i386 RSB stuffing
Turns out that i386 doesn't unconditionally have LFENCE, as such the loop in __FILL_RETURN_BUFFER isn't actually speculation safe on such chips.
---
arch/x86/include/asm/nospec-branch.h +12 -0
@@ -50,6 +50,7 @@* the optimal version - two calls, each with their own speculation * trap should their return address end up getting used, in a loop. */ +#ifdef CONFIG_X86_64 #define __FILL_RETURN_BUFFER (reg, nr) \ mov $(nr/2), reg; \ 771: \
@@ -60,6 +61,17 @@ jnz 771b; \/* barrier for jnz misprediction */ \ lfence; +#else +/* +* i386 doesn't unconditionally have LFENCE, as such it can't +* do a loop. +*/ +#define __FILL_RETURN_BUFFER (reg, nr) \ + .rept nr; \ + __FILL_RETURN_SLOT; \ + .endr; \ + add $(BITS_PER_LONG/8) * nr, %_ASM_SP; +#endif /* * Stuff a single RSB slot.
Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Ben Hutchings
From: Ben Hutchings <[email protected]>
The mitigation for PBRSB includes adding LFENCE instructions to the RSB filling sequence. However, RSB filling is done on some older CPUs that don't support the LFENCE instruction.
Wait; what? There are chips that enable the RSB mitigations and DONT have LFENCE ?!?
Yes, X86_FEATURE_RSB_CTXSW is enabled if any other Spectre v2 mitigation is enabled. And all Intel family 6 (except some early Atoms) and AMD family 5+ get Spectre v2 mitigation by default.
Ben.
Signature · 3 lines
Ben Hutchings Beware of bugs in the above code; I have only proved it correct, not tried it. - Donald Knuth
Quoted 3 lines · Peter Zijlstra
I'm not entirly sure what to do here. On the one hand, it's 32bit, so who gives a crap, otoh we shouldn't break these ancient chips either I suppose.
This is something that I've repeatedly had to bring up, whenever something breaks because someone meant well by enabling more security bells and whistles:
x86-32 is by definition legacy hardware. Enabling more bells and whistles essentially kills support for all but the very latest variants of the x86-32 family. This is the wrong approach. The right approach is to accept that building for x86-32 inherently means building for older and thus less secure architectures.
Martin-Éric
[tip: x86/urgent] x86/nospec: Fix i386 RSB stuffing
In reply to Peter ZijlstraThe following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: 332924973725e8cdcc783c175f68cf7e162cb9e5 Gitweb: https://git.kernel.org/tip/332924973725e8cdcc783c175f68cf7e162cb9e5 Author: Peter Zijlstra <[email protected]> AuthorDate: Fri, 19 Aug 2022 13:01:35 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 19 Aug 2022 13:24:33 +02:00
x86/nospec: Fix i386 RSB stuffing
Turns out that i386 doesn't unconditionally have LFENCE, as such the loop in __FILL_RETURN_BUFFER isn't actually speculation safe on such chips.
--- arch/x86/include/asm/nospec-branch.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/x86/include/asm/nospec-branch.h +12 -0
@@ -50,6 +50,7 @@* the optimal version - two calls, each with their own speculation * trap should their return address end up getting used, in a loop. */ +#ifdef CONFIG_X86_64 #define __FILL_RETURN_BUFFER (reg, nr) \ mov $(nr/2), reg; \ 771: \
@@ -60,6 +61,17 @@ jnz 771b; \/* barrier for jnz misprediction */ \ lfence; +#else +/* +* i386 doesn't unconditionally have LFENCE, as such it can't +* do a loop. +*/ +#define __FILL_RETURN_BUFFER (reg, nr) \ + .rept nr; \ + __FILL_RETURN_SLOT; \ + .endr; \ + add $(BITS_PER_LONG/8) * nr, %_ASM_SP; +#endif /* * Stuff a single RSB slot.
Quoted 12 lines · Peter Zijlstra
Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Ben Hutchings
From: Ben Hutchings <[email protected]>
The mitigation for PBRSB includes adding LFENCE instructions to the RSB filling sequence. However, RSB filling is done on some older CPUs that don't support the LFENCE instruction.
Wait; what? There are chips that enable the RSB mitigations and DONT have LFENCE ?!?
Quoted 6 lines
So I gave in and clicked on the horrible bugzilla thing. Apparently this is P3/Athlon64 era crud.
Anyway, the added LFENCE isn't because of retbleed; it is because you can steer the jnz and terminate the loop early and then not actually complete the RSB stuffing.
I know, I corrected that further down.
Quoted 10 lines
New insights etc.. So it's a geniune fix for the existing rsb stuffing.
I'm not entirly sure what to do here. On the one hand, it's 32bit, so who gives a crap, otoh we shouldn't break these ancient chips either I suppose.
How's something like so then? It goes on top of my other patch cleaning up this RSB mess:
https://lkml.kernel.org/r/Yv9m%2FhuNJLuyviIn%40worktop.programming.kicks-ass.net
[...]
That should be:
https://lore.kernel.org/all/Yv9m%[email protected]/
(the redirector unescapes the URL-escaped /).
So that puts the whole __FILL_RETURN_BUFFER inside an alternative, and we can't have nested alternatives. That's unfortunate.
Ben.
Signature · 3 lines
Ben Hutchings Beware of bugs in the above code; I have only proved it correct, not tried it. - Donald Knuth
Peter Zijlstra
maintainerQuoted 2 lines · Ben Hutchings
So that puts the whole
__FILL_RETURN_BUFFERinside an alternative, and we can't have nested alternatives. That's unfortunate.
Well, both alternatives end with the LFENCE instruction, so I could pull it out and do two consequtive ALTs, but unrolling the loop for i386 is a better solution in that the sequence, while larger, removes the need for the LFENCE.
Greetings,
Quoted 10 lines · Peter Zijlstra
Quoted 2 lines · Ben Hutchings
So that puts the whole
__FILL_RETURN_BUFFERinside an alternative, and we can't have nested alternatives. That's unfortunate.Well, both alternatives end with the LFENCE instruction, so I could pull it out and do two consequtive ALTs, but unrolling the loop for i386 is a better solution in that the sequence, while larger, removes the need for the LFENCE.
Have we reached a definitive conclusion on to how to fix this?
Martin-Éric
Peter Zijlstra
maintainerQuoted 9 lines · Martin-Éric Racine
Greetings,
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Ben Hutchings
So that puts the whole
__FILL_RETURN_BUFFERinside an alternative, and we can't have nested alternatives. That's unfortunate.
Quoted 6 lines
Quoted 4 lines
Well, both alternatives end with the LFENCE instruction, so I could pull it out and do two consequtive ALTs, but unrolling the loop for i386 is a better solution in that the sequence, while larger, removes the need for the LFENCE.
Have we reached a definitive conclusion on to how to fix this?
https://git.kernel.org/tip/332924973725e8cdcc783c175f68cf7e162cb9e5
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Martin-Éric Racine
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Ben Hutchings
So that puts the whole
__FILL_RETURN_BUFFERinside an alternative, and we can't have nested alternatives. That's unfortunate.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Well, both alternatives end with the LFENCE instruction, so I could pull it out and do two consequtive ALTs, but unrolling the loop for i386 is a better solution in that the sequence, while larger, removes the need for the LFENCE.
Have we reached a definitive conclusion on to how to fix this?
https://git.kernel.org/tip/332924973725e8cdcc783c175f68cf7e162cb9e5
Thanks.
Ben: When can we expect an updated kernel to security-updates at Debian?
Martin-Éric
Re: Bug#1017425: [PATCH] x86/speculation: Avoid LFENCE in FILL_RETURN_BUFFER on CPUs that lack it
Hi Martin,
Quoted 14 lines · Martin-??ric Racine
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Martin-??ric Racine
Quoted 10 lines · Peter Zijlstra
Quoted 2 lines · Ben Hutchings
So that puts the whole
__FILL_RETURN_BUFFERinside an alternative, and we can't have nested alternatives. That's unfortunate.Well, both alternatives end with the LFENCE instruction, so I could pull it out and do two consequtive ALTs, but unrolling the loop for i386 is a better solution in that the sequence, while larger, removes the need for the LFENCE.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Have we reached a definitive conclusion on to how to fix this?
https://git.kernel.org/tip/332924973725e8cdcc783c175f68cf7e162cb9e5
Thanks.
Ben: When can we expect an updated kernel to security-updates at Debian?
When the update is ready to go. Likely the update for the next point release for bullseye will contain the fix for this issue.
Regards,
Salvatore