ITS mitigation
v3 cover ITS mitigation
v3:
- Added patches:
x86/its: Fix build errors when CONFIG_MODULES=n
x86/its: FineIBT-paranoid vs ITS
v2:
- Added missing patch to 6.1 backport.
This is a backport of mitigation for Indirect Target Selection (ITS).
ITS is a bug in some Intel CPUs that affects indirect branches including RETs in the first half of a cacheline. Mitigation is to relocate the affected branches to an ITS-safe thunk.
Below additional upstream commits are required to cover some of the special cases like indirects in asm and returns in static calls:
cfceff8526a4 ("x86/speculation: Simplify and make CALL_NOSPEC consistent") 052040e34c08 ("x86/speculation: Add a conditional CS prefix to CALL_NOSPEC") c8c81458863a ("x86/speculation: Remove the extra #ifdef around CALL_NOSPEC") d2408e043e72 ("x86/alternative: Optimize returns patching") 4ba89dd6ddec ("x86/alternatives: Remove faulty optimization")
[1] https://github.com/torvalds/linux/commit/6f5bf947bab06f37ff931c359fd5770c4d9cbf87
---
Borislav Petkov (AMD) (1):
x86/alternative: Optimize returns patching
Eric Biggers (1):
x86/its: Fix build errors when CONFIG_MODULES=n
Josh Poimboeuf (1):
x86/alternatives: Remove faulty optimization
Pawan Gupta (10):
x86/speculation: Simplify and make CALL_NOSPEC consistent x86/speculation: Add a conditional CS prefix to CALL_NOSPEC x86/speculation: Remove the extra #ifdef around CALL_NOSPEC
Documentation: x86/bugs/its: Add ITS documentation x86/its: Enumerate Indirect Target Selection (ITS) bug x86/its: Add support for ITS-safe indirect thunk x86/its: Add support for ITS-safe return thunk x86/its: Enable Indirect Target Selection mitigation x86/its: Add "vmexit" option to skip mitigation on some CPUs x86/its: Align RETs in BHB clear sequence to avoid thunking
Peter Zijlstra (3):
x86,nospec: Simplify {JMP,CALL}_NOSPEC x86/its: Use dynamic thunks for indirect branches x86/its: FineIBT-paranoid vs ITS
Documentation/ABI/testing/sysfs-devices-system-cpu | 1 + Documentation/admin-guide/hw-vuln/index.rst | 1 + .../hw-vuln/indirect-target-selection.rst | 156 +++++++++++++ Documentation/admin-guide/kernel-parameters.txt | 15 ++ arch/x86/Kconfig | 11 + arch/x86/entry/entry_64.S | 20 +- arch/x86/include/asm/alternative.h | 32 +++ arch/x86/include/asm/cpufeatures.h | 3 + arch/x86/include/asm/msr-index.h | 8 + arch/x86/include/asm/nospec-branch.h | 57 +++-- arch/x86/kernel/alternative.c | 243 ++++++++++++++++++++- arch/x86/kernel/cpu/bugs.c | 139 +++++++++++- arch/x86/kernel/cpu/common.c | 63 +++++- arch/x86/kernel/ftrace.c | 2 +- arch/x86/kernel/module.c | 7 + arch/x86/kernel/static_call.c | 2 +- arch/x86/kernel/vmlinux.lds.S | 10 + arch/x86/kvm/x86.c | 4 +- arch/x86/lib/retpoline.S | 39 ++++ arch/x86/net/bpf_jit_comp.c | 8 +- drivers/base/cpu.c | 8 + include/linux/cpu.h | 2 + include/linux/module.h | 5 + 23 files changed, 793 insertions(+), 43 deletions(-)
---
change-id: 20250512-its-5-15-0e0385221e32
PATCH v3 1/16 x86,nospec: Simplify {JMP,CALL}_NOSPEC
From: Peter Zijlstra <[email protected]>
commit 09d09531a51a24635bc3331f56d92ee7092f5516 upstream.
Have {JMP,CALL}_NOSPEC generate the same code GCC does for indirect calls and rely on the objtool retpoline patching infrastructure.
There's no reason these should be alternatives while the vast bulk of compiler generated retpolines are not.
--- arch/x86/include/asm/nospec-branch.h | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
arch/x86/include/asm/nospec-branch.h +18 -6
@@ -118,6 +118,19 @@#endif .endm +/* + * Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call + * to the retpoline thunk with a CS prefix when the register requires + * a RAX prefix byte to encode. Also see apply_retpolines(). + */ +.macro __CS_PREFIX reg:req + .irp rs,r8,r9,r10,r11,r12,r13,r14,r15 + .ifc \reg,\rs + .byte 0x2e + .endif + .endr +.endm +/* * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple * indirect jmp/call which may be susceptible to the Spectre variant 2
@@ -125,19 +138,18 @@*/ .macro JMP_NOSPEC reg:req #ifdef CONFIG_RETPOLINE - ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), \ - __stringify(jmp __x86_indirect_thunk_\reg), X86_FEATURE_RETPOLINE, \ - __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), X86_FEATURE_RETPOLINE_LFENCE + __CS_PREFIX \reg + jmp __x86_indirect_thunk_\reg #else jmp *%\reg + int3 #endif .endm .macro CALL_NOSPEC reg:req #ifdef CONFIG_RETPOLINE - ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *%\reg), \ - __stringify(call __x86_indirect_thunk_\reg), X86_FEATURE_RETPOLINE, \ - __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *%\reg), X86_FEATURE_RETPOLINE_LFENCE + __CS_PREFIX \reg + call __x86_indirect_thunk_\reg #else call *%\reg #endif
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 01/16] x86,nospec: Simplify {JMP,CALL}_NOSPEC
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: 09d09531a51a24635bc3331f56d92ee7092f5516
WARNING: Author mismatch between patch and upstream commit:
Backport author: Pawan Gupta<[email protected]> Commit author: Peter Zijlstra<[email protected]>
Status in newer kernel trees:
6.14.y | Present (exact SHA1)
6.12.y | Present (exact SHA1)
6.6.y | Present (exact SHA1)
6.1.y | Present (exact SHA1)
Note: The patch differs from the upstream commit:
---
1: 09d09531a51a2 ! 1: c6ae69642e75d x86,nospec: Simplify {JMP,CALL}_NOSPEC
@@ Metadata
## Commit message ##
x86,nospec: Simplify {JMP,CALL}_NOSPEC
+ commit 09d09531a51a24635bc3331f56d92ee7092f5516 upstream.
+
Have {JMP,CALL}_NOSPEC generate the same code GCC does for indirect calls and rely on the objtool retpoline patching infrastructure.
@@ Commit message
compiler generated retpolines are not.
+ Signed-off-by: Pawan Gupta <[email protected]>
## arch/x86/include/asm/nospec-branch.h ##
@@
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-5.15.y | Success | Success |
PATCH v3 2/16 x86/speculation: Simplify and make CALL_NOSPEC consistent
In reply to Pawan Guptacommit cfceff8526a426948b53445c02bcb98453c7330d upstream.
CALL_NOSPEC macro is used to generate Spectre-v2 mitigation friendly indirect branches. At compile time the macro defaults to indirect branch, and at runtime those can be patched to thunk based mitigations.
This approach is opposite of what is done for the rest of the kernel, where the compile time default is to replace indirect calls with retpoline thunk calls.
Make CALL_NOSPEC consistent with the rest of the kernel, default to retpoline thunk at compile time when CONFIG_RETPOLINE is enabled.
[ pawan: s/CONFIG_MITIGATION_RETPOLINE/CONFIG_RETPOLINE/ ]
--- arch/x86/include/asm/nospec-branch.h | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
arch/x86/include/asm/nospec-branch.h +5 -10
@@ -285,16 +285,11 @@ extern retpoline_thunk_t __x86_indirect_thunk_array[];* Inline asm uses the %V modifier which is only in newer GCC * which is ensured when CONFIG_RETPOLINE is defined. */ -# define CALL_NOSPEC \ - ALTERNATIVE_2( \ - ANNOTATE_RETPOLINE_SAFE \ - "call *%[thunk_target]\n", \ - "call __x86_indirect_thunk_%V[thunk_target]\n", \ - X86_FEATURE_RETPOLINE, \ - "lfence;\n" \ - ANNOTATE_RETPOLINE_SAFE \ - "call *%[thunk_target]\n", \ - X86_FEATURE_RETPOLINE_LFENCE) +#ifdef CONFIG_RETPOLINE +#define CALL_NOSPEC "call __x86_indirect_thunk_%V[thunk_target]\n" +#else +#define CALL_NOSPEC "call *%[thunk_target]\n" +#endif # define THUNK_TARGET (addr) [thunk_target] "r" (addr)
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 02/16] x86/speculation: Simplify and make CALL_NOSPEC consistent
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: cfceff8526a426948b53445c02bcb98453c7330d
Status in newer kernel trees:
6.14.y | Present (different SHA1: 010c4a461c1d) 6.12.y | Present (different SHA1: ca16a0acacd7) 6.6.y | Present (different SHA1: ffe12f8aa62c) 6.1.y | Present (different SHA1: 7f6da764325a)
Note: The patch differs from the upstream commit:
---
1: cfceff8526a42 ! 1: cf7360bfe0353 x86/speculation: Simplify and make CALL_NOSPEC consistent
@@ Metadata
## Commit message ##
x86/speculation: Simplify and make CALL_NOSPEC consistent
+ commit cfceff8526a426948b53445c02bcb98453c7330d upstream.
+
CALL_NOSPEC macro is used to generate Spectre-v2 mitigation friendly indirect branches. At compile time the macro defaults to indirect branch, and at runtime those can be patched to thunk based mitigations.
@@ Commit message
calls.
Make CALL_NOSPEC consistent with the rest of the kernel, default to
- retpoline thunk at compile time when CONFIG_MITIGATION_RETPOLINE is
+ retpoline thunk at compile time when CONFIG_RETPOLINE is
enabled.
+ [ pawan: s/CONFIG_MITIGATION_RETPOLINE/CONFIG_RETPOLINE/ ]
+
@@ Commit message
## arch/x86/include/asm/nospec-branch.h ##
-@@ arch/x86/include/asm/nospec-branch.h: static inline void call_depth_return_thunk(void) {} +@@ arch/x86/include/asm/nospec-branch.h: extern retpoline_thunk_t __x86_indirect_thunk_array[];
* Inline asm uses the %V modifier which is only in newer GCC
- * which is ensured when CONFIG_MITIGATION_RETPOLINE is defined.
+ * which is ensured when CONFIG_RETPOLINE is defined.
*/
-# define CALL_NOSPEC \
- ALTERNATIVE_2( \
@@ arch/x86/include/asm/nospec-branch.h: static inline void call_depth_return_thunk
- ANNOTATE_RETPOLINE_SAFE \
- "call *%[thunk_target]\n", \
- X86_FEATURE_RETPOLINE_LFENCE)
-+#ifdef CONFIG_MITIGATION_RETPOLINE
++#ifdef CONFIG_RETPOLINE
+#define CALL_NOSPEC "call __x86_indirect_thunk_%V[thunk_target]\n"
+#else
+#define CALL_NOSPEC "call *%[thunk_target]\n"
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 3/16 x86/speculation: Add a conditional CS prefix to CALL_NOSPEC
In reply to Pawan Guptacommit 052040e34c08428a5a388b85787e8531970c0c67 upstream.
Retpoline mitigation for spectre-v2 uses thunks for indirect branches. To support this mitigation compilers add a CS prefix with -mindirect-branch-cs-prefix. For an indirect branch in asm, this needs to be added manually.
CS prefix is already being added to indirect branches in asm files, but not in inline asm. Add CS prefix to CALL_NOSPEC for inline asm as well. There is no JMP_NOSPEC for inline asm.
--- arch/x86/include/asm/nospec-branch.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
arch/x86/include/asm/nospec-branch.h +15 -4
@@ -119,9 +119,8 @@ .endm /* - * Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call - * to the retpoline thunk with a CS prefix when the register requires - * a RAX prefix byte to encode. Also see apply_retpolines(). + * Emits a conditional CS prefix that is compatible with + * -mindirect-branch-cs-prefix. */ .macro __CS_PREFIX reg:req .irp rs,r8,r9,r10,r11,r12,r13,r14,r15
@@ -281,12 +280,24 @@ extern retpoline_thunk_t __x86_indirect_thunk_array[];#ifdef CONFIG_X86_64 +/* +* Emits a conditional CS prefix that is compatible with +* -mindirect-branch-cs-prefix. +*/ +#define __CS_PREFIX (reg) \ + ".irp rs,r8,r9,r10,r11,r12,r13,r14,r15\n" \ + ".ifc \\rs," reg "\n" \ + ".byte 0x2e\n" \ + ".endif\n" \ + ".endr\n" +/* * Inline asm uses the %V modifier which is only in newer GCC * which is ensured when CONFIG_RETPOLINE is defined. */ #ifdef CONFIG_RETPOLINE -#define CALL_NOSPEC "call __x86_indirect_thunk_%V[thunk_target]\n" +#define CALL_NOSPEC __CS_PREFIX("%V[thunk_target]") \ + "call __x86_indirect_thunk_%V[thunk_target]\n"#else #define CALL_NOSPEC "call *%[thunk_target]\n"#endif
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 03/16] x86/speculation: Add a conditional CS prefix to CALL_NOSPEC
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: 052040e34c08428a5a388b85787e8531970c0c67
Status in newer kernel trees:
6.14.y | Present (different SHA1: 9af9ad85ac44) 6.12.y | Present (different SHA1: 226419e0d451) 6.6.y | Present (different SHA1: 14fc94d095b2) 6.1.y | Present (different SHA1: f23cf7ef532e)
Note: The patch differs from the upstream commit:
---
1: 052040e34c084 ! 1: 686ac3e0289f6 x86/speculation: Add a conditional CS prefix to CALL_NOSPEC
@@ Metadata
## Commit message ##
x86/speculation: Add a conditional CS prefix to CALL_NOSPEC
+ commit 052040e34c08428a5a388b85787e8531970c0c67 upstream.
+
Retpoline mitigation for spectre-v2 uses thunks for indirect branches. To support this mitigation compilers add a CS prefix with -mindirect-branch-cs-prefix. For an indirect branch in asm, this needs to
@@ arch/x86/include/asm/nospec-branch.h
*/
.macro __CS_PREFIX reg:req
.irp rs,r8,r9,r10,r11,r12,r13,r14,r15
-@@ arch/x86/include/asm/nospec-branch.h: static inline void call_depth_return_thunk(void) {} +@@ arch/x86/include/asm/nospec-branch.h: extern retpoline_thunk_t __x86_indirect_thunk_array[];
#ifdef CONFIG_X86_64
@@ arch/x86/include/asm/nospec-branch.h: static inline void call_depth_return_thunk
+
/*
* Inline asm uses the %V modifier which is only in newer GCC
- * which is ensured when CONFIG_MITIGATION_RETPOLINE is defined.
+ * which is ensured when CONFIG_RETPOLINE is defined.
*/
- #ifdef CONFIG_MITIGATION_RETPOLINE
+ #ifdef CONFIG_RETPOLINE
-#define CALL_NOSPEC "call __x86_indirect_thunk_%V[thunk_target]\n"
+#define CALL_NOSPEC __CS_PREFIX("%V[thunk_target]") \
+ "call __x86_indirect_thunk_%V[thunk_target]\n"
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 4/16 x86/speculation: Remove the extra #ifdef around CALL_NOSPEC
In reply to Pawan Guptacommit c8c81458863ab686cda4fe1e603fccaae0f12460 upstream.
Commit:
010c4a461c1d ("x86/speculation: Simplify and make CALL_NOSPEC consistent")
added an #ifdef CONFIG_RETPOLINE around the CALL_NOSPEC definition. This is not required as this code is already under a larger #ifdef.
Remove the extra #ifdef, no functional change.
vmlinux size remains same before and after this change:
CONFIG_RETPOLINE=y:
text data bss dec hex filename 25434752 7342290 2301212 35078254 217406e vmlinux.before 25434752 7342290 2301212 35078254 217406e vmlinux.after
# CONFIG_RETPOLINE is not set:
text data bss dec hex filename 22943094 6214994 1550152 30708240 1d49210 vmlinux.before 22943094 6214994 1550152 30708240 1d49210 vmlinux.after
[ pawan: s/CONFIG_MITIGATION_RETPOLINE/CONFIG_RETPOLINE/ ]
--- arch/x86/include/asm/nospec-branch.h | 4 ---- 1 file changed, 4 deletions(-)
arch/x86/include/asm/nospec-branch.h +0 -4
@@ -295,12 +295,8 @@ extern retpoline_thunk_t __x86_indirect_thunk_array[];* Inline asm uses the %V modifier which is only in newer GCC * which is ensured when CONFIG_RETPOLINE is defined. */ -#ifdef CONFIG_RETPOLINE #define CALL_NOSPEC __CS_PREFIX("%V[thunk_target]") \ "call __x86_indirect_thunk_%V[thunk_target]\n" -#else -#define CALL_NOSPEC "call *%[thunk_target]\n" -#endif # define THUNK_TARGET (addr) [thunk_target] "r" (addr)
Signature · 1 line
2.34.1
Re: [PATCH 5.15 v3 04/16] x86/speculation: Remove the extra #ifdef around CALL_NOSPEC
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: c8c81458863ab686cda4fe1e603fccaae0f12460
Status in newer kernel trees:
6.14.y | Not found
6.12.y | Present (different SHA1: 02a1d6e26129) 6.6.y | Present (different SHA1: 8149e4bc5b6a) 6.1.y | Present (different SHA1: 84e06e948fc5)
Note: The patch differs from the upstream commit:
---
1: c8c81458863ab ! 1: 31b985df4b5be x86/speculation: Remove the extra #ifdef around CALL_NOSPEC
@@ Metadata
## Commit message ##
x86/speculation: Remove the extra #ifdef around CALL_NOSPEC
+ commit c8c81458863ab686cda4fe1e603fccaae0f12460 upstream.
+
Commit:
010c4a461c1d ("x86/speculation: Simplify and make CALL_NOSPEC consistent")
- added an #ifdef CONFIG_MITIGATION_RETPOLINE around the CALL_NOSPEC definition.
- This is not required as this code is already under a larger #ifdef.
+ added an #ifdef CONFIG_RETPOLINE around the CALL_NOSPEC definition. This is
+ not required as this code is already under a larger #ifdef.
Remove the extra #ifdef, no functional change.
vmlinux size remains same before and after this change:
- CONFIG_MITIGATION_RETPOLINE=y:
+ CONFIG_RETPOLINE=y:
text data bss dec hex filename
25434752 7342290 2301212 35078254 217406e vmlinux.before
25434752 7342290 2301212 35078254 217406e vmlinux.after
- # CONFIG_MITIGATION_RETPOLINE is not set:
+ # CONFIG_RETPOLINE is not set:
text data bss dec hex filename
22943094 6214994 1550152 30708240 1d49210 vmlinux.before
22943094 6214994 1550152 30708240 1d49210 vmlinux.after
+ [ pawan: s/CONFIG_MITIGATION_RETPOLINE/CONFIG_RETPOLINE/ ]
+
## arch/x86/include/asm/nospec-branch.h ##
-@@ arch/x86/include/asm/nospec-branch.h: static inline void call_depth_return_thunk(void) {} +@@ arch/x86/include/asm/nospec-branch.h: extern retpoline_thunk_t __x86_indirect_thunk_array[];
* Inline asm uses the %V modifier which is only in newer GCC
- * which is ensured when CONFIG_MITIGATION_RETPOLINE is defined.
+ * which is ensured when CONFIG_RETPOLINE is defined.
*/
--#ifdef CONFIG_MITIGATION_RETPOLINE
+-#ifdef CONFIG_RETPOLINE
#define CALL_NOSPEC __CS_PREFIX("%V[thunk_target]") \
"call __x86_indirect_thunk_%V[thunk_target]\n"
-#else
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 5/16 Documentation: x86/bugs/its: Add ITS documentation
In reply to Pawan Guptacommit 1ac116ce6468670eeda39345a5585df308243dca upstream.
Add the admin-guide for Indirect Target Selection (ITS).
--- Documentation/admin-guide/hw-vuln/index.rst | 1 + .../hw-vuln/indirect-target-selection.rst | 156 +++++++++++++++++++++ 2 files changed, 157 insertions(+)
Documentation/admin-guide/hw-vuln/index.rst +1 -0
@@ -22,3 +22,4 @@ are configurable at compile, boot or run time. gather_data_sampling.rst srso reg-file-data-sampling + indirect-target-selection
Documentation/admin-guide/hw-vuln/indirect-target-selection.rst new file +156 -0
156 lines not on this page · raw message
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 05/16] Documentation: x86/bugs/its: Add ITS documentation
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: 1ac116ce6468670eeda39345a5585df308243dca
Status in newer kernel trees:
6.14.y | Present (different SHA1: 5e32c75352c6) 6.12.y | Present (different SHA1: a35104170df5) 6.6.y | Present (different SHA1: 4c6a960c62ef) 6.1.y | Present (different SHA1: d16fc1930163)
Note: The patch differs from the upstream commit:
---
1: 1ac116ce64686 ! 1: d920ed9999e58 Documentation: x86/bugs/its: Add ITS documentation
@@ Metadata
## Commit message ##
Documentation: x86/bugs/its: Add ITS documentation
+ commit 1ac116ce6468670eeda39345a5585df308243dca upstream.
+
Add the admin-guide for Indirect Target Selection (ITS).
@@ Commit message
## Documentation/admin-guide/hw-vuln/index.rst ##
@@ Documentation/admin-guide/hw-vuln/index.rst: are configurable at compile, boot or run time.
- gather_data_sampling
+ gather_data_sampling.rst
+ srso
reg-file-data-sampling
- rsb
+ indirect-target-selection
## Documentation/admin-guide/hw-vuln/indirect-target-selection.rst (new) ##
@@ Documentation/admin-guide/hw-vuln/indirect-target-selection.rst (new)
+reason, when retpoline is enabled, ITS mitigation only relocates the RETs to +safe thunks. Unless user requested the RSB-stuffing mitigation.
+
-+RSB Stuffing
-+~~~~~~~~~~~~
-+RSB-stuffing via Call Depth Tracking is a mitigation for Retbleed RSB-underflow -+attacks. And it also mitigates RETs that are vulnerable to ITS. -+
+Mitigation in guests
+^^^^^^^^^^^^^^^^^^^^
+All guests deploy ITS mitigation by default, irrespective of eIBRS enumeration
@@ Documentation/admin-guide/hw-vuln/indirect-target-selection.rst (new)
+ useful when host userspace is not in the threat model, and only
+ attacks from guest to host are considered.
+
-+ stuff Deploy RSB-fill mitigation when retpoline is also deployed.
-+ Otherwise, deploy the default mitigation. When retpoline mitigation
-+ is enabled, RSB-stuffing via Call-Depth-Tracking also mitigates
-+ ITS.
-+
+ force Force the ITS bug and deploy the default mitigation.
+ ======== ===================================================================
+
@@ Documentation/admin-guide/hw-vuln/indirect-target-selection.rst (new)
+ * - Mitigation: Aligned branch/return thunks
+ - The mitigation is enabled, affected indirect branches and RETs are
+ relocated to safe thunks.
-+ * - Mitigation: Retpolines, Stuffing RSB
-+ - The mitigation is enabled using retpoline and RSB stuffing.
+
+References
+----------
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 6/16 x86/its: Enumerate Indirect Target Selection (ITS) bug
In reply to Pawan Guptacommit 159013a7ca18c271ff64192deb62a689b622d860 upstream.
ITS bug in some pre-Alderlake Intel CPUs may allow indirect branches in the first half of a cache line get predicted to a target of a branch located in the second half of the cache line.
Set X86_BUG_ITS on affected CPUs. Mitigation to follow in later commits.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/msr-index.h | 8 ++++++ arch/x86/kernel/cpu/common.c | 58 ++++++++++++++++++++++++++++++-------- arch/x86/kvm/x86.c | 4 ++- 4 files changed, 58 insertions(+), 13 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -483,4 +483,5 @@#define X86_BUG_RFDS X86_BUG(1*32 + 2)/* CPU is vulnerable to Register File Data Sampling */ #define X86_BUG_BHI X86_BUG(1*32 + 3)/* CPU is affected by Branch History Injection */ #define X86_BUG_IBPB_NO_RET X86_BUG(1*32 + 4)/* "ibpb_no_ret" IBPB omits return target predictions */ +#define X86_BUG_ITS X86_BUG(1*32 + 5)/* CPU is affected by Indirect Target Selection */ #endif /* _ASM_X86_CPUFEATURES_H */
arch/x86/include/asm/msr-index.h +8 -0
@@ -183,6 +183,14 @@* VERW clears CPU Register * File. */ +#define ARCH_CAP_ITS_NO BIT_ULL(62)/* +* Not susceptible to +* Indirect Target Selection. +* This bit is not set by +* HW, but is synthesized by +* VMMs for guests to know +* their affected status. +*/ #define MSR_IA32_FLUSH_CMD 0x0000010b#define L1D_FLUSH BIT(0)/*
arch/x86/kernel/cpu/common.c +46 -12
86 lines not on this page · raw message
arch/x86/kvm/x86.c +3 -1
@@ -1499,7 +1499,7 @@ static unsigned int num_msr_based_features; ARCH_CAP_PSCHANGE_MC_NO | ARCH_CAP_TSX_CTRL_MSR | ARCH_CAP_TAA_NO | \ ARCH_CAP_SBDR_SSDP_NO | ARCH_CAP_FBSDP_NO | ARCH_CAP_PSDP_NO | \ ARCH_CAP_FB_CLEAR | ARCH_CAP_RRSBA | ARCH_CAP_PBRSB_NO | ARCH_CAP_GDS_NO | \ - ARCH_CAP_RFDS_NO | ARCH_CAP_RFDS_CLEAR | ARCH_CAP_BHI_NO) + ARCH_CAP_RFDS_NO | ARCH_CAP_RFDS_CLEAR | ARCH_CAP_BHI_NO | ARCH_CAP_ITS_NO)static u64 kvm_get_arch_capabilities (void ) {
@@ -1538,6 +1538,8 @@ static u64 kvm_get_arch_capabilities(void) data |=ARCH_CAP_MDS_NO ;if (!boot_cpu_has_bug (X86_BUG_RFDS )) data |=ARCH_CAP_RFDS_NO ; +if (!boot_cpu_has_bug (X86_BUG_ITS )) + data |=ARCH_CAP_ITS_NO ;if (!boot_cpu_has (X86_FEATURE_RTM )) {/*
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 06/16] x86/its: Enumerate Indirect Target Selection (ITS) bug
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: 159013a7ca18c271ff64192deb62a689b622d860
Status in newer kernel trees:
6.14.y | Present (different SHA1: 6826e2ec2d88) 6.12.y | Present (different SHA1: d89cff63d3eb) 6.6.y | Present (different SHA1: 81b9dc008193) 6.1.y | Present (different SHA1: 527651c1e381)
Note: The patch differs from the upstream commit:
---
1: 159013a7ca18c ! 1: 796d194a26dde x86/its: Enumerate Indirect Target Selection (ITS) bug
@@ Metadata
## Commit message ##
x86/its: Enumerate Indirect Target Selection (ITS) bug
+ commit 159013a7ca18c271ff64192deb62a689b622d860 upstream.
+
ITS bug in some pre-Alderlake Intel CPUs may allow indirect branches in the first half of a cache line get predicted to a target of a branch located in the second half of the cache line.
@@ Commit message
## arch/x86/include/asm/cpufeatures.h ##
@@
- #define X86_BUG_BHI X86_BUG(1*32 + 3) /* "bhi" CPU is affected by Branch History Injection */
- #define X86_BUG_IBPB_NO_RET X86_BUG(1*32 + 4) /* "ibpb_no_ret" IBPB omits return target predictions */
- #define X86_BUG_SPECTRE_V2_USER X86_BUG(1*32 + 5) /* "spectre_v2_user" CPU is affected by Spectre variant 2 attack between user processes */
-+#define X86_BUG_ITS X86_BUG(1*32 + 6) /* "its" CPU is affected by Indirect Target Selection */
+ #define X86_BUG_RFDS X86_BUG(1*32 + 2) /* CPU is vulnerable to Register File Data Sampling */
+ #define X86_BUG_BHI X86_BUG(1*32 + 3) /* CPU is affected by Branch History Injection */
+ #define X86_BUG_IBPB_NO_RET X86_BUG(1*32 + 4) /* "ibpb_no_ret" IBPB omits return target predictions */
++#define X86_BUG_ITS X86_BUG(1*32 + 5) /* CPU is affected by Indirect Target Selection */
#endif /* _ASM_X86_CPUFEATURES_H */
## arch/x86/include/asm/msr-index.h ##
@@ arch/x86/kernel/cpu/common.c: static const __initconst struct x86_cpu_id cpu_vul
+#define ITS BIT(8)
static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
- VULNBL_INTEL_STEPS(INTEL_IVYBRIDGE, X86_STEP_MAX, SRBDS),
+ VULNBL_INTEL_STEPPINGS(IVYBRIDGE, X86_STEPPING_ANY, SRBDS),
@@ arch/x86/kernel/cpu/common.c: static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
- VULNBL_INTEL_STEPS(INTEL_BROADWELL_G, X86_STEP_MAX, SRBDS),
- VULNBL_INTEL_STEPS(INTEL_BROADWELL_X, X86_STEP_MAX, MMIO),
- VULNBL_INTEL_STEPS(INTEL_BROADWELL, X86_STEP_MAX, SRBDS),
-- VULNBL_INTEL_STEPS(INTEL_SKYLAKE_X, X86_STEP_MAX, MMIO | RETBLEED | GDS),
-+ VULNBL_INTEL_STEPS(INTEL_SKYLAKE_X, 0x5, MMIO | RETBLEED | GDS),
-+ VULNBL_INTEL_STEPS(INTEL_SKYLAKE_X, X86_STEP_MAX, MMIO | RETBLEED | GDS | ITS),
- VULNBL_INTEL_STEPS(INTEL_SKYLAKE_L, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS),
- VULNBL_INTEL_STEPS(INTEL_SKYLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS),
-- VULNBL_INTEL_STEPS(INTEL_KABYLAKE_L, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS),
-- VULNBL_INTEL_STEPS(INTEL_KABYLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS),
-+ VULNBL_INTEL_STEPS(INTEL_KABYLAKE_L, 0xb, MMIO | RETBLEED | GDS | SRBDS),
-+ VULNBL_INTEL_STEPS(INTEL_KABYLAKE_L, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_KABYLAKE, 0xc, MMIO | RETBLEED | GDS | SRBDS),
-+ VULNBL_INTEL_STEPS(INTEL_KABYLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS | ITS),
- VULNBL_INTEL_STEPS(INTEL_CANNONLAKE_L, X86_STEP_MAX, RETBLEED),
-- VULNBL_INTEL_STEPS(INTEL_ICELAKE_L, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS),
-- VULNBL_INTEL_STEPS(INTEL_ICELAKE_D, X86_STEP_MAX, MMIO | GDS),
-- VULNBL_INTEL_STEPS(INTEL_ICELAKE_X, X86_STEP_MAX, MMIO | GDS),
-- VULNBL_INTEL_STEPS(INTEL_COMETLAKE, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS),
-- VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L, 0x0, MMIO | RETBLEED),
-- VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS),
-- VULNBL_INTEL_STEPS(INTEL_TIGERLAKE_L, X86_STEP_MAX, GDS),
-- VULNBL_INTEL_STEPS(INTEL_TIGERLAKE, X86_STEP_MAX, GDS),
-+ VULNBL_INTEL_STEPS(INTEL_ICELAKE_L, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_ICELAKE_D, X86_STEP_MAX, MMIO | GDS | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_ICELAKE_X, X86_STEP_MAX, MMIO | GDS | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_COMETLAKE, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L, 0x0, MMIO | RETBLEED | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_TIGERLAKE_L, X86_STEP_MAX, GDS | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_TIGERLAKE, X86_STEP_MAX, GDS | ITS),
- VULNBL_INTEL_STEPS(INTEL_LAKEFIELD, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED),
-- VULNBL_INTEL_STEPS(INTEL_ROCKETLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS),
-+ VULNBL_INTEL_STEPS(INTEL_ROCKETLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS | ITS),
- VULNBL_INTEL_TYPE(INTEL_ALDERLAKE, ATOM, RFDS),
- VULNBL_INTEL_STEPS(INTEL_ALDERLAKE_L, X86_STEP_MAX, RFDS),
- VULNBL_INTEL_TYPE(INTEL_RAPTORLAKE, ATOM, RFDS),
+ VULNBL_INTEL_STEPPINGS(BROADWELL_G, X86_STEPPING_ANY, SRBDS),
+ VULNBL_INTEL_STEPPINGS(BROADWELL_X, X86_STEPPING_ANY, MMIO),
+ VULNBL_INTEL_STEPPINGS(BROADWELL, X86_STEPPING_ANY, SRBDS),
+- VULNBL_INTEL_STEPPINGS(SKYLAKE_X, X86_STEPPING_ANY, MMIO | RETBLEED | GDS),
++ VULNBL_INTEL_STEPPINGS(SKYLAKE_X, X86_STEPPINGS(0x0, 0x5), MMIO | RETBLEED | GDS),
++ VULNBL_INTEL_STEPPINGS(SKYLAKE_X, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | ITS),
+ VULNBL_INTEL_STEPPINGS(SKYLAKE_L, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS),
+ VULNBL_INTEL_STEPPINGS(SKYLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS),
+- VULNBL_INTEL_STEPPINGS(KABYLAKE_L, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS),
+- VULNBL_INTEL_STEPPINGS(KABYLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS),
++ VULNBL_INTEL_STEPPINGS(KABYLAKE_L, X86_STEPPINGS(0x0, 0xb), MMIO | RETBLEED | GDS | SRBDS),
++ VULNBL_INTEL_STEPPINGS(KABYLAKE_L, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS | ITS),
++ VULNBL_INTEL_STEPPINGS(KABYLAKE, X86_STEPPINGS(0x0, 0xc), MMIO | RETBLEED | GDS | SRBDS),
++ VULNBL_INTEL_STEPPINGS(KABYLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS | ITS),
+ VULNBL_INTEL_STEPPINGS(CANNONLAKE_L, X86_STEPPING_ANY, RETBLEED),
+- VULNBL_INTEL_STEPPINGS(ICELAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS),
+- VULNBL_INTEL_STEPPINGS(ICELAKE_D, X86_STEPPING_ANY, MMIO | GDS),
+- VULNBL_INTEL_STEPPINGS(ICELAKE_X, X86_STEPPING_ANY, MMIO | GDS),
+- VULNBL_INTEL_STEPPINGS(COMETLAKE, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS),
+- VULNBL_INTEL_STEPPINGS(COMETLAKE_L, X86_STEPPINGS(0x0, 0x0), MMIO | RETBLEED),
+- VULNBL_INTEL_STEPPINGS(COMETLAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS),
+- VULNBL_INTEL_STEPPINGS(TIGERLAKE_L, X86_STEPPING_ANY, GDS),
+- VULNBL_INTEL_STEPPINGS(TIGERLAKE, X86_STEPPING_ANY, GDS),
++ VULNBL_INTEL_STEPPINGS(ICELAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
++ VULNBL_INTEL_STEPPINGS(ICELAKE_D, X86_STEPPING_ANY, MMIO | GDS | ITS),
++ VULNBL_INTEL_STEPPINGS(ICELAKE_X, X86_STEPPING_ANY, MMIO | GDS | ITS),
++ VULNBL_INTEL_STEPPINGS(COMETLAKE, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
++ VULNBL_INTEL_STEPPINGS(COMETLAKE_L, X86_STEPPINGS(0x0, 0x0), MMIO | RETBLEED | ITS),
++ VULNBL_INTEL_STEPPINGS(COMETLAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
++ VULNBL_INTEL_STEPPINGS(TIGERLAKE_L, X86_STEPPING_ANY, GDS | ITS),
++ VULNBL_INTEL_STEPPINGS(TIGERLAKE, X86_STEPPING_ANY, GDS | ITS),
+ VULNBL_INTEL_STEPPINGS(LAKEFIELD, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED),
+- VULNBL_INTEL_STEPPINGS(ROCKETLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS),
++ VULNBL_INTEL_STEPPINGS(ROCKETLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | ITS),
+ VULNBL_INTEL_STEPPINGS(ALDERLAKE, X86_STEPPING_ANY, RFDS),
+ VULNBL_INTEL_STEPPINGS(ALDERLAKE_L, X86_STEPPING_ANY, RFDS),
+ VULNBL_INTEL_STEPPINGS(RAPTORLAKE, X86_STEPPING_ANY, RFDS),
@@ arch/x86/kernel/cpu/common.c: static bool __init vulnerable_to_rfds(u64 x86_arch_cap_msr)
return cpu_matches(cpu_vuln_blacklist, RFDS);
}
@@ arch/x86/kernel/cpu/common.c: static void __init cpu_set_bug_bits(struct cpuinfo
## arch/x86/kvm/x86.c ##
-@@ arch/x86/kvm/x86.c: EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc);
+@@ arch/x86/kvm/x86.c: static unsigned int num_msr_based_features;
ARCH_CAP_PSCHANGE_MC_NO | ARCH_CAP_TSX_CTRL_MSR | ARCH_CAP_TAA_NO | \
ARCH_CAP_SBDR_SSDP_NO | ARCH_CAP_FBSDP_NO | ARCH_CAP_PSDP_NO | \
ARCH_CAP_FB_CLEAR | ARCH_CAP_RRSBA | ARCH_CAP_PBRSB_NO | ARCH_CAP_GDS_NO | \
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 7/16 x86/its: Add support for ITS-safe indirect thunk
In reply to Pawan Guptacommit 8754e67ad4ac692c67ff1f99c0d07156f04ae40c upstream.
Due to ITS, indirect branches in the lower half of a cacheline may be vulnerable to branch target injection attack.
Introduce ITS-safe thunks to patch indirect branches in the lower half of cacheline with the thunk. Also thunk any eBPF generated indirect branches in emit_indirect_jump().
Below category of indirect branches are not mitigated:
- Indirect branches in the .init section are not mitigated because they are discarded after boot.
- Indirect branches that are explicitly marked retpoline-safe.
Note that retpoline also mitigates the indirect branches against ITS. This is because the retpoline sequence fills an RSB entry before RET, and it does not suffer from RSB-underflow part of the ITS.
--- arch/x86/Kconfig | 11 ++++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/nospec-branch.h | 5 +++ arch/x86/kernel/alternative.c | 77 ++++++++++++++++++++++++++++++++++++ arch/x86/kernel/vmlinux.lds.S | 6 +++ arch/x86/lib/retpoline.S | 28 +++++++++++++ arch/x86/net/bpf_jit_comp.c | 6 ++- 7 files changed, 133 insertions(+), 1 deletion(-)
arch/x86/Kconfig +11 -0
@@ -2517,6 +2517,17 @@ config MITIGATION_SPECTRE_BHI indirect branches. See <file:Documentation/admin-guide/hw-vuln/spectre.rst> +config MITIGATION_ITS + bool "Enable Indirect Target Selection mitigation" + depends on CPU_SUP_INTEL && X86_64 + depends on RETPOLINE && RETHUNK + default y + help + Enable Indirect Target Selection (ITS) mitigation. ITS is a bug in + BPU on some Intel CPUs that may allow Spectre V2 style attacks. If + disabled, mitigation cannot be enabled via cmdline. + See <file:Documentation/admin-guide/hw-vuln/indirect-target-selection.rst> + endif config ARCH_HAS_ADD_PAGES
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -433,6 +433,7 @@#define X86_FEATURE_BHI_CTRL (21*32+ 2)/* "" BHI_DIS_S HW control available */ #define X86_FEATURE_CLEAR_BHB_HW (21*32+ 3)/* "" BHI_DIS_S HW control enabled */ #define X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT (21*32+ 4)/* "" Clear branch history at vmexit using SW loop */ +#define X86_FEATURE_INDIRECT_THUNK_ITS (21*32 + 5)/* "" Use thunk for indirect branches in lower half of cacheline */ /* * BUG word(s)
arch/x86/include/asm/nospec-branch.h +5 -0
@@ -271,6 +271,11 @@ extern void (*x86_return_thunk)(void);typedef u8 retpoline_thunk_t [RETPOLINE_THUNK_SIZE ]; +#define ITS_THUNK_SIZE 64 +typedef u8 its_thunk_t [ITS_THUNK_SIZE ]; + +extern its_thunk_t __x86_indirect_its_thunk_array[]; +#define GEN (reg) \ extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;#include <asm/GEN-for-each-reg.h>
arch/x86/kernel/alternative.c +77 -0
89 lines not on this page · raw message
arch/x86/kernel/vmlinux.lds.S +6 -0
@@ -532,6 +532,12 @@ INIT_PER_CPU(irq_stack_backing_store); "SRSO function pair won't alias"); #endif +#if defined(CONFIG_MITIGATION_ITS) && !defined(CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B) +. = ASSERT(__x86_indirect_its_thunk_rax & 0x20, "__x86_indirect_thunk_rax not in second half of cacheline"); +. = ASSERT(((__x86_indirect_its_thunk_rcx - __x86_indirect_its_thunk_rax) % 64) == 0, "Indirect thunks are not cacheline apart"); +. = ASSERT(__x86_indirect_its_thunk_array == __x86_indirect_its_thunk_rax, "Gap in ITS thunk array"); +#endif + #endif /* CONFIG_X86_64 */ #ifdef CONFIG_KEXEC_CORE
arch/x86/lib/retpoline.S +28 -0
@@ -254,6 +254,34 @@ SYM_FUNC_START(entry_untrain_ret) SYM_FUNC_END(entry_untrain_ret) __EXPORT_THUNK(entry_untrain_ret) +#ifdef CONFIG_MITIGATION_ITS + +.macro ITS_THUNK reg + +SYM_INNER_LABEL(__x86_indirect_its_thunk_\reg, SYM_L_GLOBAL) + UNWIND_HINT_EMPTY + ANNOTATE_NOENDBR + ANNOTATE_RETPOLINE_SAFE + jmp *%\reg + int3 + .align 32, 0xcc /* fill to the end of the line */ + .skip 32, 0xcc /* skip to the next upper half */ +.endm + +/* ITS mitigation requires thunks be aligned to upper half of cacheline */ +.align 64, 0xcc +.skip 32, 0xcc +SYM_CODE_START(__x86_indirect_its_thunk_array) + +#define GEN(reg) ITS_THUNK reg +#include <asm/GEN-for-each-reg.h> +#undef GEN + + .align 64, 0xcc +SYM_CODE_END(__x86_indirect_its_thunk_array) + +#endif + SYM_CODE_START(__x86_return_thunk) UNWIND_HINT_FUNC ANNOTATE_NOENDBR
arch/x86/net/bpf_jit_comp.c +5 -1
@@ -446,7 +446,11 @@ static void emit_indirect_jump(u8 **pprog, int reg, u8 *ip)u8 *prog = *pprog; #ifdef CONFIG_RETPOLINE -if (cpu_feature_enabled (X86_FEATURE_RETPOLINE_LFENCE )) { + if (IS_ENABLED(CONFIG_MITIGATION_ITS) && + cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS)) { + OPTIMIZER_HIDE_VAR(reg); + emit_jump(&prog, &__x86_indirect_its_thunk_array[reg], ip); + } else if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) { EMIT_LFENCE(); EMIT2(0xFF, 0xE0 + reg); } else if (cpu_feature_enabled(X86_FEATURE_RETPOLINE)) {
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 07/16] x86/its: Add support for ITS-safe indirect thunk
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: 8754e67ad4ac692c67ff1f99c0d07156f04ae40c
Status in newer kernel trees:
6.14.y | Present (different SHA1: c6dd7a78b9ae) 6.12.y | Present (different SHA1: 1565863153af) 6.6.y | Present (different SHA1: 6b1c379b8824) 6.1.y | Present (different SHA1: 5f4dfa416aee)
Note: The patch differs from the upstream commit:
---
1: 8754e67ad4ac6 ! 1: cf5c88c17b7cb x86/its: Add support for ITS-safe indirect thunk
@@ Metadata
## Commit message ##
x86/its: Add support for ITS-safe indirect thunk
+ commit 8754e67ad4ac692c67ff1f99c0d07156f04ae40c upstream.
+
Due to ITS, indirect branches in the lower half of a cacheline may be
vulnerable to branch target injection attack.
@@ Commit message
Reviewed-by: Alexandre Chartre <[email protected]>
## arch/x86/Kconfig ##
-@@ arch/x86/Kconfig: config MITIGATION_SSB
- of speculative execution in a similar way to the Meltdown and Spectre
- security vulnerabilities.
+@@ arch/x86/Kconfig: config MITIGATION_SPECTRE_BHI
+ indirect branches.
+ See <file:Documentation/admin-guide/hw-vuln/spectre.rst>
+config MITIGATION_ITS
+ bool "Enable Indirect Target Selection mitigation"
+ depends on CPU_SUP_INTEL && X86_64
-+ depends on MITIGATION_RETPOLINE && MITIGATION_RETHUNK
++ depends on RETPOLINE && RETHUNK
+ default y
+ help
+ Enable Indirect Target Selection (ITS) mitigation. ITS is a bug in
@@ arch/x86/Kconfig: config MITIGATION_SSB
## arch/x86/include/asm/cpufeatures.h ##
@@
- #define X86_FEATURE_AMD_HETEROGENEOUS_CORES (21*32 + 6) /* Heterogeneous Core Topology */
- #define X86_FEATURE_AMD_WORKLOAD_CLASS (21*32 + 7) /* Workload Classification */
- #define X86_FEATURE_PREFER_YMM (21*32 + 8) /* Avoid ZMM registers due to downclocking */
-+#define X86_FEATURE_INDIRECT_THUNK_ITS (21*32 + 9) /* Use thunk for indirect branches in lower half of cacheline */
+ #define X86_FEATURE_BHI_CTRL (21*32+ 2) /* "" BHI_DIS_S HW control available */
+ #define X86_FEATURE_CLEAR_BHB_HW (21*32+ 3) /* "" BHI_DIS_S HW control enabled */
+ #define X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT (21*32+ 4) /* "" Clear branch history at vmexit using SW loop */
++#define X86_FEATURE_INDIRECT_THUNK_ITS (21*32 + 5) /* "" Use thunk for indirect branches in lower half of cacheline */
/*
* BUG word(s)
## arch/x86/include/asm/nospec-branch.h ##
-@@
+@@ arch/x86/include/asm/nospec-branch.h: extern void (*x86_return_thunk)(void);
- #else /* __ASSEMBLER__ */
+ typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
+#define ITS_THUNK_SIZE 64
-+
- typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
+typedef u8 its_thunk_t[ITS_THUNK_SIZE];
- extern retpoline_thunk_t __x86_indirect_thunk_array[];
- extern retpoline_thunk_t __x86_indirect_call_thunk_array[];
- extern retpoline_thunk_t __x86_indirect_jump_thunk_array[];
++
+extern its_thunk_t __x86_indirect_its_thunk_array[];
-
- #ifdef CONFIG_MITIGATION_RETHUNK
- extern void __x86_return_thunk(void);
++
+ #define GEN(reg) \
+ extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;
+ #include <asm/GEN-for-each-reg.h>
## arch/x86/kernel/alternative.c ##
@@ arch/x86/kernel/alternative.c: static int emit_indirect(int op, int reg, u8 *bytes)
return i;
}
--static int emit_call_track_retpoline(void *addr, struct insn *insn, int reg, u8 *bytes)
++#ifdef CONFIG_MITIGATION_ITS
++
+static int __emit_trampoline(void *addr, struct insn *insn, u8 *bytes,
+ void *call_dest, void *jmp_dest)
- {
- u8 op = insn->opcode.bytes[0];
- int i = 0;
-@@ arch/x86/kernel/alternative.c: static int emit_call_track_retpoline(void *addr, struct insn *insn, int reg, u8
- switch (op) {
- case CALL_INSN_OPCODE:
- __text_gen_insn(bytes+i, op, addr+i,
-- __x86_indirect_call_thunk_array[reg],
++{
++ u8 op = insn->opcode.bytes[0];
++ int i = 0;
++
++ /*
++ * Clang does 'weird' Jcc __x86_indirect_thunk_r11 conditional
++ * tail-calls. Deal with them.
++ */
++ if (is_jcc32(insn)) {
++ bytes[i++] = op;
++ op = insn->opcode.bytes[1];
++ goto clang_jcc;
++ }
++
++ if (insn->length == 6)
++ bytes[i++] = 0x2e; /* CS-prefix */
++
++ switch (op) {
++ case CALL_INSN_OPCODE:
++ __text_gen_insn(bytes+i, op, addr+i,
+ call_dest,
- CALL_INSN_SIZE);
- i += CALL_INSN_SIZE;
- break;
-@@ arch/x86/kernel/alternative.c: static int emit_call_track_retpoline(void *addr, struct insn *insn, int reg, u8
- case JMP32_INSN_OPCODE:
- clang_jcc:
- __text_gen_insn(bytes+i, op, addr+i,
-- __x86_indirect_jump_thunk_array[reg],
++ CALL_INSN_SIZE);
++ i += CALL_INSN_SIZE;
++ break;
++
++ case JMP32_INSN_OPCODE:
++clang_jcc:
++ __text_gen_insn(bytes+i, op, addr+i,
+ jmp_dest,
- JMP32_INSN_SIZE);
- i += JMP32_INSN_SIZE;
- break;
-@@ arch/x86/kernel/alternative.c: static int emit_call_track_retpoline(void *addr, struct insn *insn, int reg, u8
- return i;
- }
-
-+static int emit_call_track_retpoline(void *addr, struct insn *insn, int reg, u8 *bytes)
-+{
-+ return __emit_trampoline(addr, insn, bytes,
-+ __x86_indirect_call_thunk_array[reg],
-+ __x86_indirect_jump_thunk_array[reg]);
++ JMP32_INSN_SIZE);
++ i += JMP32_INSN_SIZE;
++ break;
++
++ default:
++ WARN(1, "%pS %px %*ph\n", addr, addr, 6, addr);
++ return -1;
++ }
++
++ WARN_ON_ONCE(i != insn->length);
++
++ return i;
+}
+
-+#ifdef CONFIG_MITIGATION_ITS
+static int emit_its_trampoline(void *addr, struct insn *insn, int reg, u8 *bytes)
+{
+ return __emit_trampoline(addr, insn, bytes,
@@ arch/x86/kernel/alternative.c: static int patch_retpoline(void *addr, struct ins
return ret;
## arch/x86/kernel/vmlinux.lds.S ##
-@@ arch/x86/kernel/vmlinux.lds.S: PROVIDE(__ref_stack_chk_guard = __stack_chk_guard);
+@@ arch/x86/kernel/vmlinux.lds.S: INIT_PER_CPU(irq_stack_backing_store);
"SRSO function pair won't alias");
#endif
@@ arch/x86/kernel/vmlinux.lds.S: PROVIDE(__ref_stack_chk_guard = __stack_chk_guard
+
#endif /* CONFIG_X86_64 */
- /*
+ #ifdef CONFIG_KEXEC_CORE
## arch/x86/lib/retpoline.S ##
-@@ arch/x86/lib/retpoline.S: SYM_FUNC_END(call_depth_return_thunk)
-
- #endif /* CONFIG_MITIGATION_CALL_DEPTH_TRACKING */
+@@ arch/x86/lib/retpoline.S: SYM_FUNC_START(entry_untrain_ret)
+ SYM_FUNC_END(entry_untrain_ret)
+ __EXPORT_THUNK(entry_untrain_ret)
+#ifdef CONFIG_MITIGATION_ITS
+
+.macro ITS_THUNK reg
+
+SYM_INNER_LABEL(__x86_indirect_its_thunk_\reg, SYM_L_GLOBAL)
-+ UNWIND_HINT_UNDEFINED
++ UNWIND_HINT_EMPTY
+ ANNOTATE_NOENDBR
+ ANNOTATE_RETPOLINE_SAFE
+ jmp *%\reg
@@ arch/x86/lib/retpoline.S: SYM_FUNC_END(call_depth_return_thunk)
+
+#endif
+
- /*
- * This function name is magical and is used by -mfunction-return=thunk-extern
- * for the compiler to generate JMPs to it.
+ SYM_CODE_START(__x86_return_thunk)
+ UNWIND_HINT_FUNC
+ ANNOTATE_NOENDBR
## arch/x86/net/bpf_jit_comp.c ##
@@ arch/x86/net/bpf_jit_comp.c: static void emit_indirect_jump(u8 **pprog, int reg, u8 *ip)
- {
u8 *prog = *pprog;
+ #ifdef CONFIG_RETPOLINE
- if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) {
-+ if (cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS)) {
++ if (IS_ENABLED(CONFIG_MITIGATION_ITS) &&
++ cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS)) {
+ OPTIMIZER_HIDE_VAR(reg);
+ emit_jump(&prog, &__x86_indirect_its_thunk_array[reg], ip);
+ } else if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) {---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 8/16 x86/alternative: Optimize returns patching
In reply to Pawan GuptaFrom: "Borislav Petkov (AMD)" <[email protected]>
commit d2408e043e7296017420aa5929b3bba4d5e61013 upstream.
Instead of decoding each instruction in the return sites range only to realize that that return site is a jump to the default return thunk which is needed - X86_FEATURE_RETHUNK is enabled - lift that check before the loop and get rid of that loop overhead.
Add comments about what gets patched, while at it.
--- arch/x86/kernel/alternative.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
arch/x86/kernel/alternative.c +10 -3
@@ -620,13 +620,12 @@ static int patch_return(void *addr, struct insn *insn, u8 *bytes) {int i =0 ; +/* Patch the custom return thunks... */ if (cpu_feature_enabled (X86_FEATURE_RETHUNK )) { -if (x86_return_thunk == __x86_return_thunk) -return -1 ; - i =JMP32_INSN_SIZE ;__text_gen_insn (bytes,JMP32_INSN_OPCODE , addr, x86_return_thunk, i); }else { +/* ... or patch them out if not needed. */ bytes[i++] =RET_INSN_OPCODE ; }
@@ -639,6 +638,14 @@ void __init_or_module noinline apply_returns(s32 *start, s32 *end) {s32 *s; +/* +* Do not patch out the default return thunks if those needed are the +* ones generated by the compiler. +*/ +if (cpu_feature_enabled (X86_FEATURE_RETHUNK ) && + (x86_return_thunk == __x86_return_thunk)) +return ; +for (s = start; s < end; s++) {void *dest =NULL , *addr = (void *)s + *s;struct insn insn;
Signature · 1 line
2.34.1
Re: [PATCH 5.15 v3 08/16] x86/alternative: Optimize returns patching
[ Sasha's backport helper bot ]
Hi,
Summary of potential issues:
ℹ️ This is part 08/16 of a series
⚠️ Found follow-up fixes in mainline
The upstream commit SHA1 provided is correct: d2408e043e7296017420aa5929b3bba4d5e61013
WARNING: Author mismatch between patch and upstream commit:
Backport author: Pawan Gupta<[email protected]> Commit author: Borislav Petkov (AMD)<[email protected]>
Status in newer kernel trees:
6.14.y | Present (exact SHA1)
6.12.y | Present (exact SHA1)
6.6.y | Present (exact SHA1)
6.1.y | Present (different SHA1: 3de55fa8eadb)
Found fixes commits:4ba89dd6ddec x86/alternatives: Remove faulty optimization
Note: The patch differs from the upstream commit:
---
1: d2408e043e729 ! 1: 94c28300c096e x86/alternative: Optimize returns patching
@@ Metadata
## Commit message ##
x86/alternative: Optimize returns patching
+ commit d2408e043e7296017420aa5929b3bba4d5e61013 upstream.
+
Instead of decoding each instruction in the return sites range only to realize that that return site is a jump to the default return thunk which is needed - X86_FEATURE_RETHUNK is enabled - lift that check
@@ Commit message
+ Signed-off-by: Pawan Gupta <[email protected]>
## arch/x86/kernel/alternative.c ##
@@ arch/x86/kernel/alternative.c: static int patch_return(void *addr, struct insn *insn, u8 *bytes)
---
NOTE: These results are for this patch alone. Full series testing will be performed when all parts are received.
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 9/16 x86/alternatives: Remove faulty optimization
In reply to Pawan GuptaFrom: Josh Poimboeuf <[email protected]>
commit 4ba89dd6ddeca2a733bdaed7c9a5cbe4e19d9124 upstream.
The following commit
095b8303f383 ("x86/alternative: Make custom return thunk unconditional")
made '__x86_return_thunk' a placeholder value. All code setting X86_FEATURE_RETHUNK also changes the value of 'x86_return_thunk'. So the optimization at the beginning of apply_returns() is dead code.
Also, before the above-mentioned commit, the optimization actually had a bug It bypassed __static_call_fixup(), causing some raw returns to remain unpatched in static call trampolines. Thus the 'Fixes' tag.
--- arch/x86/kernel/alternative.c | 8 -------- 1 file changed, 8 deletions(-)
arch/x86/kernel/alternative.c +0 -8
@@ -638,14 +638,6 @@ void __init_or_module noinline apply_returns(s32 *start, s32 *end) {s32 *s; -/* -* Do not patch out the default return thunks if those needed are the -* ones generated by the compiler. -*/ -if (cpu_feature_enabled (X86_FEATURE_RETHUNK ) && - (x86_return_thunk == __x86_return_thunk)) -return ; -for (s = start; s < end; s++) {void *dest =NULL , *addr = (void *)s + *s;struct insn insn;
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 09/16] x86/alternatives: Remove faulty optimization
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: 4ba89dd6ddeca2a733bdaed7c9a5cbe4e19d9124
WARNING: Author mismatch between patch and upstream commit:
Backport author: Pawan Gupta<[email protected]> Commit author: Josh Poimboeuf<[email protected]>
Status in newer kernel trees:
6.14.y | Present (exact SHA1)
6.12.y | Present (exact SHA1)
6.6.y | Present (exact SHA1)
6.1.y | Present (different SHA1: de5d7298bcd0)
Note: The patch differs from the upstream commit:
---
1: 4ba89dd6ddeca < -: ------------- x86/alternatives: Remove faulty optimization -: ------------- > 1: 3b8db0e4f2631 Linux 5.15.182
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 10/16 x86/its: Add support for ITS-safe return thunk
In reply to Pawan Guptacommit a75bf27fe41abe658c53276a0c486c4bf9adecfc upstream.
RETs in the lower half of cacheline may be affected by ITS bug, specifically when the RSB-underflows. Use ITS-safe return thunk for such RETs.
RETs that are not patched:
- RET in retpoline sequence does not need to be patched, because the sequence itself fills an RSB before RET.
- RETs in .init section are not reachable after init.
- RETs that are explicitly marked safe with ANNOTATE_UNRET_SAFE.
--- arch/x86/include/asm/alternative.h | 14 ++++++++++++++ arch/x86/include/asm/nospec-branch.h | 6 ++++++ arch/x86/kernel/alternative.c | 17 ++++++++++++++++- arch/x86/kernel/ftrace.c | 2 +- arch/x86/kernel/static_call.c | 2 +- arch/x86/kernel/vmlinux.lds.S | 4 ++++ arch/x86/lib/retpoline.S | 13 ++++++++++++- arch/x86/net/bpf_jit_comp.c | 2 +- 8 files changed, 55 insertions(+), 5 deletions(-)
arch/x86/include/asm/alternative.h +14 -0
@@ -80,6 +80,20 @@ extern void apply_returns(s32 *start, s32 *end);struct module ; +#ifdef CONFIG_RETHUNK +extern bool cpu_wants_rethunk (void ); +extern bool cpu_wants_rethunk_at (void *addr); +#else +static __always_inline bool cpu_wants_rethunk(void) +{ +return false; +} +static __always_inline bool cpu_wants_rethunk_at(void *addr) +{ +return false; +} +#endif +#ifdef CONFIG_SMP extern void alternatives_smp_module_add(struct module *mod, char *name, void *locks, void *locks_end,
arch/x86/include/asm/nospec-branch.h +6 -0
@@ -250,6 +250,12 @@ extern void __x86_return_thunk(void);static inline void __x86_return_thunk (void ) {}#endif +#ifdef CONFIG_MITIGATION_ITS +extern void its_return_thunk (void ); +#else +static inline void its_return_thunk (void ) {} +#endif +extern void retbleed_return_thunk (void );extern void srso_return_thunk (void );extern void srso_alias_return_thunk (void );
arch/x86/kernel/alternative.c +16 -1
@@ -605,6 +605,21 @@ void __init_or_module noinline apply_retpolines(s32 *start, s32 *end)#ifdef CONFIG_RETHUNK +bool cpu_wants_rethunk (void ) +{ +return cpu_feature_enabled (X86_FEATURE_RETHUNK ); +} + +bool cpu_wants_rethunk_at (void *addr) +{ +if (!cpu_feature_enabled (X86_FEATURE_RETHUNK )) +return false; +if (x86_return_thunk != its_return_thunk) +return true; + +return !((unsigned long )addr &0x20 ); +} +/* * Rewrite the compiler generated return thunk tail-calls. *
@@ -621,7 +636,7 @@ static int patch_return(void *addr, struct insn *insn, u8 *bytes)int i =0 ;/* Patch the custom return thunks... */ -if (cpu_feature_enabled (X86_FEATURE_RETHUNK )) { +if (cpu_wants_rethunk_at (addr)) { i =JMP32_INSN_SIZE ;__text_gen_insn (bytes,JMP32_INSN_OPCODE , addr, x86_return_thunk, i); }else {
arch/x86/kernel/ftrace.c +1 -1
@@ -367,7 +367,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) goto fail; ip = trampoline + size; -if (cpu_feature_enabled (X86_FEATURE_RETHUNK )) +if (cpu_wants_rethunk_at (ip))__text_gen_insn (ip,JMP32_INSN_OPCODE , ip, x86_return_thunk,JMP32_INSN_SIZE );else memcpy (ip, retq,sizeof (retq));
arch/x86/kernel/static_call.c +1 -1
@@ -81,7 +81,7 @@ static void __ref __static_call_transform(void *insn, enum insn_type type,break ;case RET : -if (cpu_feature_enabled (X86_FEATURE_RETHUNK )) +if (cpu_wants_rethunk_at (insn)) code =text_gen_insn (JMP32_INSN_OPCODE , insn, x86_return_thunk);else code = &retinsn;
arch/x86/kernel/vmlinux.lds.S +4 -0
@@ -538,6 +538,10 @@ INIT_PER_CPU(irq_stack_backing_store); . = ASSERT(__x86_indirect_its_thunk_array == __x86_indirect_its_thunk_rax, "Gap in ITS thunk array"); #endif +#if defined(CONFIG_MITIGATION_ITS) && !defined(CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B) +. = ASSERT(its_return_thunk & 0x20, "its_return_thunk not in second half of cacheline"); +#endif + #endif /* CONFIG_X86_64 */ #ifdef CONFIG_KEXEC_CORE
arch/x86/lib/retpoline.S +12 -1
@@ -280,7 +280,18 @@ SYM_CODE_START(__x86_indirect_its_thunk_array) .align 64, 0xcc SYM_CODE_END(__x86_indirect_its_thunk_array) -#endif +.align 64, 0xcc +.skip 32, 0xcc +SYM_CODE_START(its_return_thunk) + UNWIND_HINT_FUNC + ANNOTATE_NOENDBR + ANNOTATE_UNRET_SAFE + ret + int3 +SYM_CODE_END(its_return_thunk) +EXPORT_SYMBOL(its_return_thunk) + +#endif /* CONFIG_MITIGATION_ITS */ SYM_CODE_START(__x86_return_thunk) UNWIND_HINT_FUNC
arch/x86/net/bpf_jit_comp.c +1 -1
@@ -466,7 +466,7 @@ static void emit_return(u8 **pprog, u8 *ip) {u8 *prog = *pprog; -if (cpu_feature_enabled (X86_FEATURE_RETHUNK )) { +if (cpu_wants_rethunk ()) {emit_jump (&prog, x86_return_thunk, ip); }else {EMIT1 (0xC3 );/* ret */
Signature · 1 line
2.34.1
Re: [PATCH 5.15 v3 10/16] x86/its: Add support for ITS-safe return thunk
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: a75bf27fe41abe658c53276a0c486c4bf9adecfc
Status in newer kernel trees:
6.14.y | Present (different SHA1: f9a449a04ad6) 6.12.y | Present (different SHA1: 22d1efbb1e99) 6.6.y | Present (different SHA1: 2bacac79dd22) 6.1.y | Present (different SHA1: e1d254d4a267)
Note: The patch differs from the upstream commit:
---
1: a75bf27fe41ab ! 1: 335c313faf43f x86/its: Add support for ITS-safe return thunk
@@ Metadata
## Commit message ##
x86/its: Add support for ITS-safe return thunk
+ commit a75bf27fe41abe658c53276a0c486c4bf9adecfc upstream.
+
RETs in the lower half of cacheline may be affected by ITS bug, specifically when the RSB-underflows. Use ITS-safe return thunk for such RETs.
@@ Commit message
- RET in retpoline sequence does not need to be patched, because the sequence itself fills an RSB before RET.
- - RET in Call Depth Tracking (CDT) thunks __x86_indirect_{call|jump}_thunk
- and call_depth_return_thunk are not patched because CDT by design
- prevents RSB-underflow.
- RETs in .init section are not reachable after init.
- RETs that are explicitly marked safe with ANNOTATE_UNRET_SAFE.
@@ Commit message
## arch/x86/include/asm/alternative.h ##
-@@ arch/x86/include/asm/alternative.h: static __always_inline int x86_call_depth_emit_accounting(u8 **pprog,
- }
- #endif
+@@ arch/x86/include/asm/alternative.h: extern void apply_returns(s32 *start, s32 *end);
+
+ struct module;
-+#if defined(CONFIG_MITIGATION_RETHUNK) && defined(CONFIG_OBJTOOL) ++#ifdef CONFIG_RETHUNK
+extern bool cpu_wants_rethunk(void);
+extern bool cpu_wants_rethunk_at(void *addr);
+#else
@@ arch/x86/include/asm/alternative.h: static __always_inline int x86_call_depth_em
void *locks, void *locks_end,
## arch/x86/include/asm/nospec-branch.h ##
-@@ arch/x86/include/asm/nospec-branch.h: static inline void srso_return_thunk(void) {}
- static inline void srso_alias_return_thunk(void) {}
+@@ arch/x86/include/asm/nospec-branch.h: extern void __x86_return_thunk(void);
+ static inline void __x86_return_thunk(void) {}
#endif
+#ifdef CONFIG_MITIGATION_ITS
@@ arch/x86/include/asm/nospec-branch.h: static inline void srso_return_thunk(void)
## arch/x86/kernel/alternative.c ##
@@ arch/x86/kernel/alternative.c: void __init_or_module noinline apply_retpolines(s32 *start, s32 *end)
- #ifdef CONFIG_MITIGATION_RETHUNK
+ #ifdef CONFIG_RETHUNK
+bool cpu_wants_rethunk(void)
+{
@@ arch/x86/kernel/alternative.c: static int patch_return(void *addr, struct insn *
i = JMP32_INSN_SIZE;
__text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i);
} else {
-@@ arch/x86/kernel/alternative.c: void __init_or_module noinline apply_returns(s32 *start, s32 *end)
- {
- s32 *s;
-
-- if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
-+ if (cpu_wants_rethunk())
- static_call_force_reinit();
-
- for (s = start; s < end; s++) {
## arch/x86/kernel/ftrace.c ##
@@ arch/x86/kernel/ftrace.c: create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
@@ arch/x86/kernel/static_call.c: static void __ref __static_call_transform(void *i
code = text_gen_insn(JMP32_INSN_OPCODE, insn, x86_return_thunk);
else
code = &retinsn;
-@@ arch/x86/kernel/static_call.c: static void __ref __static_call_transform(void *insn, enum insn_type type,
- case JCC:
- if (!func) {
- func = __static_call_return;
-- if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
-+ if (cpu_wants_rethunk())
- func = x86_return_thunk;
- }
-
## arch/x86/kernel/vmlinux.lds.S ##
-@@ arch/x86/kernel/vmlinux.lds.S: PROVIDE(__ref_stack_chk_guard = __stack_chk_guard); +@@ arch/x86/kernel/vmlinux.lds.S: INIT_PER_CPU(irq_stack_backing_store);
. = ASSERT(__x86_indirect_its_thunk_array == __x86_indirect_its_thunk_rax, "Gap in ITS thunk array"); #endif
@@ arch/x86/kernel/vmlinux.lds.S: PROVIDE(__ref_stack_chk_guard = __stack_chk_guard
+
#endif /* CONFIG_X86_64 */
- /*
+ #ifdef CONFIG_KEXEC_CORE
## arch/x86/lib/retpoline.S ##
@@ arch/x86/lib/retpoline.S: SYM_CODE_START(__x86_indirect_its_thunk_array)
@@ arch/x86/lib/retpoline.S: SYM_CODE_START(__x86_indirect_its_thunk_array)
+
+#endif /* CONFIG_MITIGATION_ITS */
- /*
- * This function name is magical and is used by -mfunction-return=thunk-extern
+ SYM_CODE_START(__x86_return_thunk)
+ UNWIND_HINT_FUNC
## arch/x86/net/bpf_jit_comp.c ##
@@ arch/x86/net/bpf_jit_comp.c: static void emit_return(u8 **pprog, u8 *ip)
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 11/16 x86/its: Enable Indirect Target Selection mitigation
In reply to Pawan Guptacommit f4818881c47fd91fcb6d62373c57c7844e3de1c0 upstream.
Indirect Target Selection (ITS) is a bug in some pre-ADL Intel CPUs with eIBRS. It affects prediction of indirect branch and RETs in the lower half of cacheline. Due to ITS such branches may get wrongly predicted to a target of (direct or indirect) branch that is located in the upper half of the cacheline.
Scope of impact
===============
Guest/host isolation
--------------------
When eIBRS is used for guest/host isolation, the indirect branches in the VMM may still be predicted with targets corresponding to branches in the guest.
Intra-mode
----------
cBPF or other native gadgets can be used for intra-mode training and disclosure using ITS.
User/kernel isolation
---------------------
When eIBRS is enabled user/kernel isolation is not impacted.
Indirect Branch Prediction Barrier (IBPB)
-----------------------------------------
After an IBPB, indirect branches may be predicted with targets corresponding to direct branches which were executed prior to IBPB. This is mitigated by a microcode update.
Add cmdline parameter indirect_target_selection=off|on|force to control the mitigation to relocate the affected branches to an ITS-safe thunk i.e. located in the upper half of cacheline. Also add the sysfs reporting.
When retpoline mitigation is deployed, ITS safe-thunks are not needed, because retpoline sequence is already ITS-safe. Similarly, when call depth tracking (CDT) mitigation is deployed (retbleed=stuff), ITS safe return thunk is not used, as CDT prevents RSB-underflow.
To not overcomplicate things, ITS mitigation is not supported with spectre-v2 lfence;jmp mitigation. Moreover, it is less practical to deploy lfence;jmp mitigation on ITS affected parts anyways.
--- Documentation/ABI/testing/sysfs-devices-system-cpu | 1 + Documentation/admin-guide/kernel-parameters.txt | 13 +++ arch/x86/kernel/cpu/bugs.c | 128 ++++++++++++++++++++- drivers/base/cpu.c | 8 ++ include/linux/cpu.h | 2 + 5 files changed, 149 insertions(+), 3 deletions(-)
Documentation/ABI/testing/sysfs-devices-system-cpu +1 -0
@@ -512,6 +512,7 @@ Description: information about CPUs heterogeneity. What: /sys/devices/system/cpu/vulnerabilities /sys/devices/system/cpu/vulnerabilities/gather_data_sampling + /sys/devices/system/cpu/vulnerabilities/indirect_target_selection /sys/devices/system/cpu/vulnerabilities/itlb_multihit /sys/devices/system/cpu/vulnerabilities/l1tf /sys/devices/system/cpu/vulnerabilities/mds
Documentation/admin-guide/kernel-parameters.txt +13 -0
@@ -1926,6 +1926,18 @@ different crypto accelerators. This option can be used to achieve best performance for particular HW. + indirect_target_selection= [X86,Intel] Mitigation control for Indirect + Target Selection(ITS) bug in Intel CPUs. Updated + microcode is also required for a fix in IBPB. + + on: Enable mitigation (default). + off: Disable mitigation. + force: Force the ITS bug and deploy default + mitigation. + + For details see: + Documentation/admin-guide/hw-vuln/indirect-target-selection.rst + init= [KNL] Format: <full_path> Run specified binary instead of /sbin/init as init
@@ -3073,6 +3085,7 @@ improves system performance, but it may also expose users to several CPU vulnerabilities. Equivalent to: gather_data_sampling=off [X86] + indirect_target_selection=off [X86] kpti=0 [ARM64] kvm.nx_huge_pages=off [X86] l1tf=off [X86]
arch/x86/kernel/cpu/bugs.c +125 -3
182 lines not on this page · raw message
drivers/base/cpu.c +8 -0
@@ -595,6 +595,12 @@ ssize_t __weak cpu_show_reg_file_data_sampling(struct device *dev,return sysfs_emit (buf,"Not affected\n" ); } +ssize_t __weak cpu_show_indirect_target_selection (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"Not affected\n" ); +} + static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL); static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL); static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
@@ -609,6 +615,7 @@ static DEVICE_ATTR(retbleed, 0444, cpu_show_retbleed, NULL); static DEVICE_ATTR(gather_data_sampling, 0444, cpu_show_gds, NULL); static DEVICE_ATTR(spec_rstack_overflow, 0444, cpu_show_spec_rstack_overflow, NULL); static DEVICE_ATTR(reg_file_data_sampling, 0444, cpu_show_reg_file_data_sampling, NULL); +static DEVICE_ATTR(indirect_target_selection, 0444, cpu_show_indirect_target_selection, NULL);static struct attribute *cpu_root_vulnerabilities_attrs[] = { &dev_attr_meltdown.attr,
@@ -625,6 +632,7 @@ static struct attribute *cpu_root_vulnerabilities_attrs[] = { &dev_attr_gather_data_sampling.attr, &dev_attr_spec_rstack_overflow.attr, &dev_attr_reg_file_data_sampling.attr, + &dev_attr_indirect_target_selection.attr, NULL };
include/linux/cpu.h +2 -0
@@ -76,6 +76,8 @@ extern ssize_t cpu_show_gds(struct device *dev, struct device_attribute *attr, char *buf);extern ssize_t cpu_show_reg_file_data_sampling (struct device *dev,struct device_attribute *attr,char *buf); +extern ssize_t cpu_show_indirect_target_selection (struct device *dev, +struct device_attribute *attr,char *buf); extern __printf(4, 5) struct device *cpu_device_create(struct device *parent, void *drvdata,
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 11/16] x86/its: Enable Indirect Target Selection mitigation
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: f4818881c47fd91fcb6d62373c57c7844e3de1c0
Status in newer kernel trees:
6.14.y | Present (different SHA1: 35311149d329) 6.12.y | Present (different SHA1: 6968bef67f0f) 6.6.y | Present (different SHA1: be8e627b89f4) 6.1.y | Present (different SHA1: fb02629ab89d)
Note: The patch differs from the upstream commit:
---
1: f4818881c47fd ! 1: 1a74f28934512 x86/its: Enable Indirect Target Selection mitigation
@@ Metadata
## Commit message ##
x86/its: Enable Indirect Target Selection mitigation
+ commit f4818881c47fd91fcb6d62373c57c7844e3de1c0 upstream.
+
Indirect Target Selection (ITS) is a bug in some pre-ADL Intel CPUs with
eIBRS. It affects prediction of indirect branch and RETs in the
lower half of cacheline. Due to ITS such branches may get wrongly predicted
@@ Documentation/admin-guide/kernel-parameters.txt
Format: <full_path>
Run specified binary instead of /sbin/init as init
@@
+ improves system performance, but it may also
expose users to several CPU vulnerabilities.
- Equivalent to: if nokaslr then kpti=0 [ARM64]
- gather_data_sampling=off [X86]
+ Equivalent to: gather_data_sampling=off [X86]
+ indirect_target_selection=off [X86]
+ kpti=0 [ARM64]
kvm.nx_huge_pages=off [X86]
l1tf=off [X86]
- mds=off [X86]
## arch/x86/kernel/cpu/bugs.c ##
@@ arch/x86/kernel/cpu/bugs.c: static void __init srbds_select_mitigation(void);
static void __init l1d_flush_select_mitigation(void);
- static void __init srso_select_mitigation(void);
static void __init gds_select_mitigation(void);
+ static void __init srso_select_mitigation(void);
+static void __init its_select_mitigation(void);
/* The base value of the SPEC_CTRL MSR without task-specific bits set */
u64 x86_spec_ctrl_base;
@@ arch/x86/kernel/cpu/bugs.c: static DEFINE_MUTEX(spec_ctrl_mutex);
- void (*x86_return_thunk)(void) __ro_after_init = __x86_return_thunk;
+ void (*x86_return_thunk)(void) __ro_after_init = &__x86_return_thunk;
+static void __init set_return_thunk(void *thunk)
+{
@@ arch/x86/kernel/cpu/bugs.c: void __init cpu_select_mitigations(void)
/*
@@ arch/x86/kernel/cpu/bugs.c: static void __init retbleed_select_mitigation(void)
- setup_force_cpu_cap(X86_FEATURE_RETHUNK);
setup_force_cpu_cap(X86_FEATURE_UNRET);
-- x86_return_thunk = retbleed_return_thunk;
-+ set_return_thunk(retbleed_return_thunk);
+ if (IS_ENABLED(CONFIG_RETHUNK))
+- x86_return_thunk = retbleed_return_thunk;
++ set_return_thunk(retbleed_return_thunk);
if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
-@@ arch/x86/kernel/cpu/bugs.c: static void __init retbleed_select_mitigation(void)
- setup_force_cpu_cap(X86_FEATURE_RETHUNK);
- setup_force_cpu_cap(X86_FEATURE_CALL_DEPTH);
-
-- x86_return_thunk = call_depth_return_thunk;
-+ set_return_thunk(call_depth_return_thunk);
- break;
-
- default:
@@ arch/x86/kernel/cpu/bugs.c: static void __init retbleed_select_mitigation(void)
pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
}
@@ arch/x86/kernel/cpu/bugs.c: static void __init retbleed_select_mitigation(void)
+enum its_mitigation {
+ ITS_MITIGATION_OFF,
+ ITS_MITIGATION_ALIGNED_THUNKS,
-+ ITS_MITIGATION_RETPOLINE_STUFF,
+};
+
+static const char * const its_strings[] = {
+ [ITS_MITIGATION_OFF] = "Vulnerable",
+ [ITS_MITIGATION_ALIGNED_THUNKS] = "Mitigation: Aligned branch/return thunks",
-+ [ITS_MITIGATION_RETPOLINE_STUFF] = "Mitigation: Retpolines, Stuffing RSB",
+};
+
+static enum its_mitigation its_mitigation __ro_after_init = ITS_MITIGATION_ALIGNED_THUNKS;
@@ arch/x86/kernel/cpu/bugs.c: static void __init retbleed_select_mitigation(void)
+ return;
+ }
+
-+ /* Retpoline+CDT mitigates ITS, bail out */
-+ if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
-+ boot_cpu_has(X86_FEATURE_CALL_DEPTH)) {
-+ its_mitigation = ITS_MITIGATION_RETPOLINE_STUFF;
-+ goto out;
-+ }
-+
+ /* Exit early to avoid irrelevant warnings */
+ if (cmd == ITS_CMD_OFF) {
+ its_mitigation = ITS_MITIGATION_OFF;
@@ arch/x86/kernel/cpu/bugs.c: static void __init retbleed_select_mitigation(void)
+ its_mitigation = ITS_MITIGATION_OFF;
+ goto out;
+ }
-+ if (!IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) ||
-+ !IS_ENABLED(CONFIG_MITIGATION_RETHUNK)) {
++ if (!IS_ENABLED(CONFIG_RETPOLINE) || !IS_ENABLED(CONFIG_RETHUNK)) {
+ pr_err("WARNING: ITS mitigation depends on retpoline and rethunk support\n");
+ its_mitigation = ITS_MITIGATION_OFF;
+ goto out;
@@ arch/x86/kernel/cpu/bugs.c: static void __init srso_select_mitigation(void)
- x86_return_thunk = srso_return_thunk;
+ set_return_thunk(srso_return_thunk);
}
- if (has_microcode)
- srso_mitigation = SRSO_MITIGATION_SAFE_RET;
+ srso_mitigation = SRSO_MITIGATION_SAFE_RET;
+ } else {
@@ arch/x86/kernel/cpu/bugs.c: static ssize_t rfds_show_state(char *buf)
return sysfs_emit(buf, "%s\n", rfds_strings[rfds_mitigation]);
}
@@ arch/x86/kernel/cpu/bugs.c: ssize_t cpu_show_reg_file_data_sampling(struct devic
+ return cpu_show_common(dev, attr, buf, X86_BUG_ITS);
+}
#endif
-
- void __warn_thunk(void)
## drivers/base/cpu.c ##
-@@ drivers/base/cpu.c: CPU_SHOW_VULN_FALLBACK(spec_rstack_overflow);
- CPU_SHOW_VULN_FALLBACK(gds);
- CPU_SHOW_VULN_FALLBACK(reg_file_data_sampling);
- CPU_SHOW_VULN_FALLBACK(ghostwrite);
-+CPU_SHOW_VULN_FALLBACK(indirect_target_selection);
+@@ drivers/base/cpu.c: ssize_t __weak cpu_show_reg_file_data_sampling(struct device *dev,
+ return sysfs_emit(buf, "Not affected\n");
+ }
++ssize_t __weak cpu_show_indirect_target_selection(struct device *dev,
++ struct device_attribute *attr, char *buf)
++{
++ return sysfs_emit(buf, "Not affected\n");
++}
++
static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
-@@ drivers/base/cpu.c: static DEVICE_ATTR(spec_rstack_overflow, 0444, cpu_show_spec_rstack_overflow, NU
+ static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
+@@ drivers/base/cpu.c: static DEVICE_ATTR(retbleed, 0444, cpu_show_retbleed, NULL);
static DEVICE_ATTR(gather_data_sampling, 0444, cpu_show_gds, NULL);
+ static DEVICE_ATTR(spec_rstack_overflow, 0444, cpu_show_spec_rstack_overflow, NULL);
static DEVICE_ATTR(reg_file_data_sampling, 0444, cpu_show_reg_file_data_sampling, NULL);
- static DEVICE_ATTR(ghostwrite, 0444, cpu_show_ghostwrite, NULL);
+static DEVICE_ATTR(indirect_target_selection, 0444, cpu_show_indirect_target_selection, NULL);
static struct attribute *cpu_root_vulnerabilities_attrs[] = {
&dev_attr_meltdown.attr,
@@ drivers/base/cpu.c: static struct attribute *cpu_root_vulnerabilities_attrs[] = {
&dev_attr_gather_data_sampling.attr,
+ &dev_attr_spec_rstack_overflow.attr,
&dev_attr_reg_file_data_sampling.attr,
- &dev_attr_ghostwrite.attr,
+ &dev_attr_indirect_target_selection.attr,
NULL
};
@@ drivers/base/cpu.c: static struct attribute *cpu_root_vulnerabilities_attrs[] =
## include/linux/cpu.h ##
@@ include/linux/cpu.h: extern ssize_t cpu_show_gds(struct device *dev,
+ struct device_attribute *attr, char *buf);
extern ssize_t cpu_show_reg_file_data_sampling(struct device *dev,
struct device_attribute *attr, char *buf);
- extern ssize_t cpu_show_ghostwrite(struct device *dev, struct device_attribute *attr, char *buf);
+extern ssize_t cpu_show_indirect_target_selection(struct device *dev,
+ struct device_attribute *attr, char *buf);---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 12/16 x86/its: Add "vmexit" option to skip mitigation on some CPUs
In reply to Pawan Guptacommit 2665281a07e19550944e8354a2024635a7b2714a upstream.
Ice Lake generation CPUs are not affected by guest/host isolation part of ITS. If a user is only concerned about KVM guests, they can now choose a new cmdline option "vmexit" that will not deploy the ITS mitigation when CPU is not affected by guest/host isolation. This saves the performance overhead of ITS mitigation on Ice Lake gen CPUs.
When "vmexit" option selected, if the CPU is affected by ITS guest/host isolation, the default ITS mitigation is deployed.
--- Documentation/admin-guide/kernel-parameters.txt | 2 ++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/bugs.c | 11 +++++++++++ arch/x86/kernel/cpu/common.c | 19 ++++++++++++------- 4 files changed, 26 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +2 -0
@@ -1934,6 +1934,8 @@ off: Disable mitigation. force: Force the ITS bug and deploy default mitigation. + vmexit: Only deploy mitigation if CPU is affected by + guest/host isolation part of ITS. For details see: Documentation/admin-guide/hw-vuln/indirect-target-selection.rst
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -485,4 +485,5 @@#define X86_BUG_BHI X86_BUG(1*32 + 3)/* CPU is affected by Branch History Injection */ #define X86_BUG_IBPB_NO_RET X86_BUG(1*32 + 4)/* "ibpb_no_ret" IBPB omits return target predictions */ #define X86_BUG_ITS X86_BUG(1*32 + 5)/* CPU is affected by Indirect Target Selection */ +#define X86_BUG_ITS_NATIVE_ONLY X86_BUG(1*32 + 6)/* CPU is affected by ITS, VMX is not affected */ #endif /* _ASM_X86_CPUFEATURES_H */
arch/x86/kernel/cpu/bugs.c +11 -0
@@ -1158,15 +1158,18 @@ static void __init retbleed_select_mitigation(void)enum its_mitigation_cmd {ITS_CMD_OFF ,ITS_CMD_ON , +ITS_CMD_VMEXIT , };enum its_mitigation {ITS_MITIGATION_OFF , +ITS_MITIGATION_VMEXIT_ONLY ,ITS_MITIGATION_ALIGNED_THUNKS , };static const char *const its_strings[] = { [ITS_MITIGATION_OFF ] ="Vulnerable" , + [ITS_MITIGATION_VMEXIT_ONLY ] ="Mitigation: Vulnerable, KVM: Not affected" , [ITS_MITIGATION_ALIGNED_THUNKS ] ="Mitigation: Aligned branch/return thunks" , };
@@ -1192,6 +1195,8 @@ static int __init its_parse_cmdline(char *str) }else if (!strcmp (str,"force" )) { its_cmd =ITS_CMD_ON ;setup_force_cpu_bug (X86_BUG_ITS ); + }else if (!strcmp (str,"vmexit" )) { + its_cmd =ITS_CMD_VMEXIT ; }else {pr_err ("Ignoring unknown indirect_target_selection option (%s)." , str); }
@@ -1239,6 +1244,12 @@ static void __init its_select_mitigation(void)case ITS_CMD_OFF : its_mitigation =ITS_MITIGATION_OFF ;break ; +case ITS_CMD_VMEXIT : +if (boot_cpu_has_bug (X86_BUG_ITS_NATIVE_ONLY )) { + its_mitigation =ITS_MITIGATION_VMEXIT_ONLY ; + goto out; + } + fallthrough;case ITS_CMD_ON : its_mitigation =ITS_MITIGATION_ALIGNED_THUNKS ;if (!boot_cpu_has (X86_FEATURE_RETPOLINE ))
arch/x86/kernel/cpu/common.c +12 -7
@@ -1143,6 +1143,8 @@ static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {#define RFDS BIT(7)/* CPU is affected by Indirect Target Selection */ #define ITS BIT(8) +/* CPU is affected by Indirect Target Selection, but guest-host isolation is not affected */ +#define ITS_NATIVE_ONLY BIT(9)static const struct x86_cpu_id cpu_vuln_blacklist[]__initconst = {VULNBL_INTEL_STEPPINGS (IVYBRIDGE ,X86_STEPPING_ANY ,SRBDS ),
@@ -1163,16 +1165,16 @@ static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {VULNBL_INTEL_STEPPINGS (KABYLAKE ,X86_STEPPINGS (0x0 ,0xc ),MMIO |RETBLEED |GDS |SRBDS ),VULNBL_INTEL_STEPPINGS (KABYLAKE ,X86_STEPPING_ANY ,MMIO |RETBLEED |GDS |SRBDS |ITS ),VULNBL_INTEL_STEPPINGS (CANNONLAKE_L ,X86_STEPPING_ANY ,RETBLEED ), -VULNBL_INTEL_STEPPINGS (ICELAKE_L ,X86_STEPPING_ANY ,MMIO |MMIO_SBDS |RETBLEED |GDS |ITS ), -VULNBL_INTEL_STEPPINGS (ICELAKE_D ,X86_STEPPING_ANY ,MMIO |GDS |ITS ), -VULNBL_INTEL_STEPPINGS (ICELAKE_X ,X86_STEPPING_ANY ,MMIO |GDS |ITS ), +VULNBL_INTEL_STEPPINGS (ICELAKE_L ,X86_STEPPING_ANY ,MMIO |MMIO_SBDS |RETBLEED |GDS |ITS |ITS_NATIVE_ONLY ), +VULNBL_INTEL_STEPPINGS (ICELAKE_D ,X86_STEPPING_ANY ,MMIO |GDS |ITS |ITS_NATIVE_ONLY ), +VULNBL_INTEL_STEPPINGS (ICELAKE_X ,X86_STEPPING_ANY ,MMIO |GDS |ITS |ITS_NATIVE_ONLY ),VULNBL_INTEL_STEPPINGS (COMETLAKE ,X86_STEPPING_ANY ,MMIO |MMIO_SBDS |RETBLEED |GDS |ITS ),VULNBL_INTEL_STEPPINGS (COMETLAKE_L ,X86_STEPPINGS (0x0 ,0x0 ),MMIO |RETBLEED |ITS ),VULNBL_INTEL_STEPPINGS (COMETLAKE_L ,X86_STEPPING_ANY ,MMIO |MMIO_SBDS |RETBLEED |GDS |ITS ), -VULNBL_INTEL_STEPPINGS (TIGERLAKE_L ,X86_STEPPING_ANY ,GDS |ITS ), -VULNBL_INTEL_STEPPINGS (TIGERLAKE ,X86_STEPPING_ANY ,GDS |ITS ), +VULNBL_INTEL_STEPPINGS (TIGERLAKE_L ,X86_STEPPING_ANY ,GDS |ITS |ITS_NATIVE_ONLY ), +VULNBL_INTEL_STEPPINGS (TIGERLAKE ,X86_STEPPING_ANY ,GDS |ITS |ITS_NATIVE_ONLY ),VULNBL_INTEL_STEPPINGS (LAKEFIELD ,X86_STEPPING_ANY ,MMIO |MMIO_SBDS |RETBLEED ), -VULNBL_INTEL_STEPPINGS (ROCKETLAKE ,X86_STEPPING_ANY ,MMIO |RETBLEED |GDS |ITS ), +VULNBL_INTEL_STEPPINGS (ROCKETLAKE ,X86_STEPPING_ANY ,MMIO |RETBLEED |GDS |ITS |ITS_NATIVE_ONLY ),VULNBL_INTEL_STEPPINGS (ALDERLAKE ,X86_STEPPING_ANY ,RFDS ),VULNBL_INTEL_STEPPINGS (ALDERLAKE_L ,X86_STEPPING_ANY ,RFDS ), VULNBL_INTEL_STEPPINGS(RAPTORLAKE, X86_STEPPING_ANY, RFDS),
@@ -1389,8 +1391,11 @@ static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)if (cpu_has (c,X86_FEATURE_AMD_IBPB ) && !cpu_has (c,X86_FEATURE_AMD_IBPB_RET ))setup_force_cpu_bug (X86_BUG_IBPB_NO_RET ); -if (vulnerable_to_its (x86_arch_cap_msr)) +if (vulnerable_to_its (x86_arch_cap_msr)) {setup_force_cpu_bug (X86_BUG_ITS ); +if (cpu_matches (cpu_vuln_blacklist,ITS_NATIVE_ONLY )) +setup_force_cpu_bug (X86_BUG_ITS_NATIVE_ONLY ); + }if (cpu_matches (cpu_vuln_whitelist,NO_MELTDOWN ))return ;
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 12/16] x86/its: Add "vmexit" option to skip mitigation on some CPUs
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: 2665281a07e19550944e8354a2024635a7b2714a
Status in newer kernel trees:
6.14.y | Present (different SHA1: 06c937efb63f) 6.12.y | Present (different SHA1: 301637f1f8ed) 6.6.y | Present (different SHA1: 6bea1c7c2ca4) 6.1.y | Present (different SHA1: ee9a8754703e)
Note: The patch differs from the upstream commit:
---
1: 2665281a07e19 ! 1: 0d02405d66808 x86/its: Add "vmexit" option to skip mitigation on some CPUs
@@ Metadata
## Commit message ##
x86/its: Add "vmexit" option to skip mitigation on some CPUs
+ commit 2665281a07e19550944e8354a2024635a7b2714a upstream.
+
Ice Lake generation CPUs are not affected by guest/host isolation part of ITS. If a user is only concerned about KVM guests, they can now choose a new cmdline option "vmexit" that will not deploy the ITS mitigation when
@@ Documentation/admin-guide/kernel-parameters.txt
## arch/x86/include/asm/cpufeatures.h ##
@@
- #define X86_BUG_IBPB_NO_RET X86_BUG(1*32 + 4) /* "ibpb_no_ret" IBPB omits return target predictions */
- #define X86_BUG_SPECTRE_V2_USER X86_BUG(1*32 + 5) /* "spectre_v2_user" CPU is affected by Spectre variant 2 attack between user processes */
- #define X86_BUG_ITS X86_BUG(1*32 + 6) /* "its" CPU is affected by Indirect Target Selection */
-+#define X86_BUG_ITS_NATIVE_ONLY X86_BUG(1*32 + 7) /* "its_native_only" CPU is affected by ITS, VMX is not affected */
+ #define X86_BUG_BHI X86_BUG(1*32 + 3) /* CPU is affected by Branch History Injection */
+ #define X86_BUG_IBPB_NO_RET X86_BUG(1*32 + 4) /* "ibpb_no_ret" IBPB omits return target predictions */
+ #define X86_BUG_ITS X86_BUG(1*32 + 5) /* CPU is affected by Indirect Target Selection */
++#define X86_BUG_ITS_NATIVE_ONLY X86_BUG(1*32 + 6) /* CPU is affected by ITS, VMX is not affected */
#endif /* _ASM_X86_CPUFEATURES_H */
## arch/x86/kernel/cpu/bugs.c ##
@@ arch/x86/kernel/cpu/bugs.c: static void __init retbleed_select_mitigation(void)
ITS_MITIGATION_OFF,
+ ITS_MITIGATION_VMEXIT_ONLY,
ITS_MITIGATION_ALIGNED_THUNKS,
- ITS_MITIGATION_RETPOLINE_STUFF,
};
static const char * const its_strings[] = {
[ITS_MITIGATION_OFF] = "Vulnerable",
+ [ITS_MITIGATION_VMEXIT_ONLY] = "Mitigation: Vulnerable, KVM: Not affected",
[ITS_MITIGATION_ALIGNED_THUNKS] = "Mitigation: Aligned branch/return thunks",
- [ITS_MITIGATION_RETPOLINE_STUFF] = "Mitigation: Retpolines, Stuffing RSB",
};
+
@@ arch/x86/kernel/cpu/bugs.c: static int __init its_parse_cmdline(char *str)
} else if (!strcmp(str, "force")) {
its_cmd = ITS_CMD_ON;
@@ arch/x86/kernel/cpu/common.c: static const __initconst struct x86_cpu_id cpu_vul
+#define ITS_NATIVE_ONLY BIT(9)
static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
- VULNBL_INTEL_STEPS(INTEL_IVYBRIDGE, X86_STEP_MAX, SRBDS),
+ VULNBL_INTEL_STEPPINGS(IVYBRIDGE, X86_STEPPING_ANY, SRBDS),
@@ arch/x86/kernel/cpu/common.c: static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
- VULNBL_INTEL_STEPS(INTEL_KABYLAKE, 0xc, MMIO | RETBLEED | GDS | SRBDS),
- VULNBL_INTEL_STEPS(INTEL_KABYLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS | ITS),
- VULNBL_INTEL_STEPS(INTEL_CANNONLAKE_L, X86_STEP_MAX, RETBLEED),
-- VULNBL_INTEL_STEPS(INTEL_ICELAKE_L, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
-- VULNBL_INTEL_STEPS(INTEL_ICELAKE_D, X86_STEP_MAX, MMIO | GDS | ITS),
-- VULNBL_INTEL_STEPS(INTEL_ICELAKE_X, X86_STEP_MAX, MMIO | GDS | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_ICELAKE_L, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY),
-+ VULNBL_INTEL_STEPS(INTEL_ICELAKE_D, X86_STEP_MAX, MMIO | GDS | ITS | ITS_NATIVE_ONLY),
-+ VULNBL_INTEL_STEPS(INTEL_ICELAKE_X, X86_STEP_MAX, MMIO | GDS | ITS | ITS_NATIVE_ONLY),
- VULNBL_INTEL_STEPS(INTEL_COMETLAKE, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
- VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L, 0x0, MMIO | RETBLEED | ITS),
- VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
-- VULNBL_INTEL_STEPS(INTEL_TIGERLAKE_L, X86_STEP_MAX, GDS | ITS),
-- VULNBL_INTEL_STEPS(INTEL_TIGERLAKE, X86_STEP_MAX, GDS | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_TIGERLAKE_L, X86_STEP_MAX, GDS | ITS | ITS_NATIVE_ONLY),
-+ VULNBL_INTEL_STEPS(INTEL_TIGERLAKE, X86_STEP_MAX, GDS | ITS | ITS_NATIVE_ONLY),
- VULNBL_INTEL_STEPS(INTEL_LAKEFIELD, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED),
-- VULNBL_INTEL_STEPS(INTEL_ROCKETLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS | ITS),
-+ VULNBL_INTEL_STEPS(INTEL_ROCKETLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY),
- VULNBL_INTEL_TYPE(INTEL_ALDERLAKE, ATOM, RFDS),
- VULNBL_INTEL_STEPS(INTEL_ALDERLAKE_L, X86_STEP_MAX, RFDS),
- VULNBL_INTEL_TYPE(INTEL_RAPTORLAKE, ATOM, RFDS),
+ VULNBL_INTEL_STEPPINGS(KABYLAKE, X86_STEPPINGS(0x0, 0xc), MMIO | RETBLEED | GDS | SRBDS),
+ VULNBL_INTEL_STEPPINGS(KABYLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS | ITS),
+ VULNBL_INTEL_STEPPINGS(CANNONLAKE_L, X86_STEPPING_ANY, RETBLEED),
+- VULNBL_INTEL_STEPPINGS(ICELAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
+- VULNBL_INTEL_STEPPINGS(ICELAKE_D, X86_STEPPING_ANY, MMIO | GDS | ITS),
+- VULNBL_INTEL_STEPPINGS(ICELAKE_X, X86_STEPPING_ANY, MMIO | GDS | ITS),
++ VULNBL_INTEL_STEPPINGS(ICELAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY),
++ VULNBL_INTEL_STEPPINGS(ICELAKE_D, X86_STEPPING_ANY, MMIO | GDS | ITS | ITS_NATIVE_ONLY),
++ VULNBL_INTEL_STEPPINGS(ICELAKE_X, X86_STEPPING_ANY, MMIO | GDS | ITS | ITS_NATIVE_ONLY),
+ VULNBL_INTEL_STEPPINGS(COMETLAKE, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
+ VULNBL_INTEL_STEPPINGS(COMETLAKE_L, X86_STEPPINGS(0x0, 0x0), MMIO | RETBLEED | ITS),
+ VULNBL_INTEL_STEPPINGS(COMETLAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS),
+- VULNBL_INTEL_STEPPINGS(TIGERLAKE_L, X86_STEPPING_ANY, GDS | ITS),
+- VULNBL_INTEL_STEPPINGS(TIGERLAKE, X86_STEPPING_ANY, GDS | ITS),
++ VULNBL_INTEL_STEPPINGS(TIGERLAKE_L, X86_STEPPING_ANY, GDS | ITS | ITS_NATIVE_ONLY),
++ VULNBL_INTEL_STEPPINGS(TIGERLAKE, X86_STEPPING_ANY, GDS | ITS | ITS_NATIVE_ONLY),
+ VULNBL_INTEL_STEPPINGS(LAKEFIELD, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED),
+- VULNBL_INTEL_STEPPINGS(ROCKETLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | ITS),
++ VULNBL_INTEL_STEPPINGS(ROCKETLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY),
+ VULNBL_INTEL_STEPPINGS(ALDERLAKE, X86_STEPPING_ANY, RFDS),
+ VULNBL_INTEL_STEPPINGS(ALDERLAKE_L, X86_STEPPING_ANY, RFDS),
+ VULNBL_INTEL_STEPPINGS(RAPTORLAKE, X86_STEPPING_ANY, RFDS),
@@ arch/x86/kernel/cpu/common.c: static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
if (cpu_has(c, X86_FEATURE_AMD_IBPB) && !cpu_has(c, X86_FEATURE_AMD_IBPB_RET))
setup_force_cpu_bug(X86_BUG_IBPB_NO_RET);
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 13/16 x86/its: Align RETs in BHB clear sequence to avoid thunking
In reply to Pawan Guptacommit f0cd7091cc5a032c8870b4285305d9172569d126 upstream.
The software mitigation for BHI is to execute BHB clear sequence at syscall entry, and possibly after a cBPF program. ITS mitigation thunks RETs in the lower half of the cacheline. This causes the RETs in the BHB clear sequence to be thunked as well, adding unnecessary branches to the BHB clear sequence.
Since the sequence is in hot path, align the RET instructions in the sequence to avoid thunking.
This is how disassembly clear_bhb_loop() looks like after this change:
0x44 <+4>: mov $0x5,%ecx 0x49 <+9>: call 0xffffffff81001d9b <clear_bhb_loop+91> 0x4e <+14>: jmp 0xffffffff81001de5 <clear_bhb_loop+165> 0x53 <+19>: int3 ... 0x9b <+91>: call 0xffffffff81001dce <clear_bhb_loop+142> 0xa0 <+96>: ret 0xa1 <+97>: int3 ... 0xce <+142>: mov $0x5,%eax 0xd3 <+147>: jmp 0xffffffff81001dd6 <clear_bhb_loop+150> 0xd5 <+149>: nop 0xd6 <+150>: sub $0x1,%eax 0xd9 <+153>: jne 0xffffffff81001dd3 <clear_bhb_loop+147> 0xdb <+155>: sub $0x1,%ecx 0xde <+158>: jne 0xffffffff81001d9b <clear_bhb_loop+91> 0xe0 <+160>: ret 0xe1 <+161>: int3 0xe2 <+162>: int3 0xe3 <+163>: int3 0xe4 <+164>: int3 0xe5 <+165>: lfence 0xe8 <+168>: pop %rbp 0xe9 <+169>: ret
--- arch/x86/entry/entry_64.S | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
arch/x86/entry/entry_64.S +17 -3
@@ -1530,7 +1530,9 @@ SYM_CODE_END(rewind_stack_and_make_dead) * ORC to unwind properly. * * The alignment is for performance and not for safety, and may be safely - * refactored in the future if needed. + * refactored in the future if needed. The .skips are for safety, to ensure + * that all RETs are in the second half of a cacheline to mitigate Indirect + * Target Selection, rather than taking the slowpath via its_return_thunk. */ SYM_FUNC_START(clear_bhb_loop) push %rbp
@@ -1540,10 +1542,22 @@ SYM_FUNC_START(clear_bhb_loop) call 1f jmp 5f .align 64, 0xcc + /* + * Shift instructions so that the RET is in the upper half of the + * cacheline and don't take the slowpath to its_return_thunk. + */ + .skip 32 - (.Lret1 - 1f), 0xcc ANNOTATE_INTRA_FUNCTION_CALL 1: call 2f - RET +.Lret1: RET .align 64, 0xcc + /* + * As above shift instructions for RET at .Lret2 as well. + * + * This should be ideally be: .skip 32 - (.Lret2 - 2f), 0xcc + * but some Clang versions (e.g. 18) don't like this. + */ + .skip 32 - 18, 0xcc 2: movl $5, %eax 3: jmp 4f nop
@@ -1551,7 +1565,7 @@ SYM_FUNC_START(clear_bhb_loop) jnz 3b sub $1, %ecx jnz 1b - RET +.Lret2: RET 5: lfence pop %rbp RET
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 13/16] x86/its: Align RETs in BHB clear sequence to avoid thunking
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: f0cd7091cc5a032c8870b4285305d9172569d126
Status in newer kernel trees:
6.14.y | Present (different SHA1: bd83f74dcbb9) 6.12.y | Present (different SHA1: 56aabf26bac5) 6.6.y | Present (different SHA1: fb3103513d53) 6.1.y | Present (different SHA1: 31e4ffa249de)
Note: The patch differs from the upstream commit:
---
1: f0cd7091cc5a0 ! 1: fde65bafa97bf x86/its: Align RETs in BHB clear sequence to avoid thunking
@@ Metadata
## Commit message ##
x86/its: Align RETs in BHB clear sequence to avoid thunking
+ commit f0cd7091cc5a032c8870b4285305d9172569d126 upstream.
+
The software mitigation for BHI is to execute BHB clear sequence at syscall entry, and possibly after a cBPF program. ITS mitigation thunks RETs in the lower half of the cacheline. This causes the RETs in the BHB clear sequence
@@ arch/x86/entry/entry_64.S: SYM_CODE_END(rewind_stack_and_make_dead)
+ * Target Selection, rather than taking the slowpath via its_return_thunk. */
SYM_FUNC_START(clear_bhb_loop)
- ANNOTATE_NOENDBR
+ push %rbp
@@ arch/x86/entry/entry_64.S: SYM_FUNC_START(clear_bhb_loop)
call 1f
jmp 5f
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 14/16 x86/its: Use dynamic thunks for indirect branches
In reply to Pawan GuptaFrom: Peter Zijlstra <[email protected]>
commit 872df34d7c51a79523820ea6a14860398c639b87 upstream.
ITS mitigation moves the unsafe indirect branches to a safe thunk. This could degrade the prediction accuracy as the source address of indirect branches becomes same for different execution paths.
To improve the predictions, and hence the performance, assign a separate thunk for each indirect callsite. This is also a defense-in-depth measure to avoid indirect branches aliasing with each other.
As an example, 5000 dynamic thunks would utilize around 16 bits of the address space, thereby gaining entropy. For a BTB that uses 32 bits for indexing, dynamic thunks could provide better prediction accuracy over fixed thunks.
Have ITS thunks be variable sized and use EXECMEM_MODULE_TEXT such that they are both more flexible (got to extend them later) and live in 2M TLBs, just like kernel code, avoiding undue TLB pressure.
[ pawan: CONFIG_EXECMEM and CONFIG_EXECMEM_ROX are not supported on
backport kernel, made changes to use module_alloc() and set_memory_*() for dynamic thunks. ]
--- arch/x86/include/asm/alternative.h | 10 +++ arch/x86/kernel/alternative.c | 133 ++++++++++++++++++++++++++++++++++++- arch/x86/kernel/module.c | 7 ++ include/linux/module.h | 5 ++ 4 files changed, 152 insertions(+), 3 deletions(-)
arch/x86/include/asm/alternative.h +10 -0
@@ -80,6 +80,16 @@ extern void apply_returns(s32 *start, s32 *end);struct module ; +#ifdef CONFIG_MITIGATION_ITS +extern void its_init_mod (struct module *mod); +extern void its_fini_mod (struct module *mod); +extern void its_free_mod (struct module *mod); +#else /* CONFIG_MITIGATION_ITS */ +static inline void its_init_mod (struct module *mod) { } +static inline void its_fini_mod (struct module *mod) { } +static inline void its_free_mod (struct module *mod) { } +#endif +#ifdef CONFIG_RETHUNK extern bool cpu_wants_rethunk (void );extern bool cpu_wants_rethunk_at (void *addr);
arch/x86/kernel/alternative.c +130 -3
157 lines not on this page · raw message
arch/x86/kernel/module.c +7 -0
@@ -283,10 +283,16 @@ int module_finalize(const Elf_Ehdr *hdr,void *pseg = (void *)para->sh_addr;apply_paravirt (pseg, pseg + para->sh_size); } + +its_init_mod (me); +if (retpolines) {void *rseg = (void *)retpolines->sh_addr;apply_retpolines (rseg, rseg + retpolines->sh_size); } + +its_fini_mod (me); +if (returns) {void *rseg = (void *)returns->sh_addr;apply_returns (rseg, rseg + returns->sh_size);
@@ -317,4 +323,5 @@ int module_finalize(const Elf_Ehdr *hdr,void module_arch_cleanup (struct module *mod) {alternatives_smp_module_del (mod); +its_free_mod (mod); }
include/linux/module.h +5 -0
@@ -528,6 +528,11 @@ struct module {atomic_t refcnt;#endif +#ifdef CONFIG_MITIGATION_ITS +int its_num_pages; +void **its_page_array; +#endif +#ifdef CONFIG_CONSTRUCTORS /* Constructor functions. */ ctor_fn_t *ctors;
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 14/16] x86/its: Use dynamic thunks for indirect branches
[ Sasha's backport helper bot ]
Hi,
Summary of potential issues:
ℹ️ This is part 14/16 of a series
⚠️ Found follow-up fixes in mainline
The upstream commit SHA1 provided is correct: 872df34d7c51a79523820ea6a14860398c639b87
WARNING: Author mismatch between patch and upstream commit:
Backport author: Pawan Gupta<[email protected]> Commit author: Peter Zijlstra<[email protected]>
Status in newer kernel trees:
6.14.y | Present (different SHA1: 37526e8a94dd)
6.12.y | Present (different SHA1: 5f6966e6a709)
6.6.y | Present (different SHA1: cb4b8d845fc5)
6.1.y | Present (different SHA1: 383a65981c30)
Found fixes commits:9f35e33144ae x86/its: Fix build errors when CONFIG_MODULES=n
Note: The patch differs from the upstream commit:
---
1: 872df34d7c51a ! 1: 98921616793d1 x86/its: Use dynamic thunks for indirect branches
@@ Metadata
## Commit message ##
x86/its: Use dynamic thunks for indirect branches
+ commit 872df34d7c51a79523820ea6a14860398c639b87 upstream.
+
ITS mitigation moves the unsafe indirect branches to a safe thunk. This
could degrade the prediction accuracy as the source address of indirect
branches becomes same for different execution paths.
@@ Commit message
they are both more flexible (got to extend them later) and live in 2M TLBs,
just like kernel code, avoiding undue TLB pressure.
+ [ pawan: CONFIG_EXECMEM and CONFIG_EXECMEM_ROX are not supported on
+ backport kernel, made changes to use module_alloc() and
+ set_memory_*() for dynamic thunks. ]
+
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Signed-off-by: Pawan Gupta <[email protected]>
Signed-off-by: Dave Hansen <[email protected]>
Reviewed-by: Alexandre Chartre <[email protected]>
- ## arch/x86/Kconfig ##
-@@ arch/x86/Kconfig: config MITIGATION_ITS
- bool "Enable Indirect Target Selection mitigation"
- depends on CPU_SUP_INTEL && X86_64
- depends on MITIGATION_RETPOLINE && MITIGATION_RETHUNK
-+ select EXECMEM
- default y
- help
- Enable Indirect Target Selection (ITS) mitigation. ITS is a bug in
-
## arch/x86/include/asm/alternative.h ##
-@@ arch/x86/include/asm/alternative.h: static __always_inline int x86_call_depth_emit_accounting(u8 **pprog,
- }
- #endif
+@@ arch/x86/include/asm/alternative.h: extern void apply_returns(s32 *start, s32 *end);
+
+ struct module;
+#ifdef CONFIG_MITIGATION_ITS
+extern void its_init_mod(struct module *mod);
@@ arch/x86/include/asm/alternative.h: static __always_inline int x86_call_depth_em
+static inline void its_free_mod(struct module *mod) { }
+#endif
+
- #if defined(CONFIG_MITIGATION_RETHUNK) && defined(CONFIG_OBJTOOL)
+ #ifdef CONFIG_RETHUNK
extern bool cpu_wants_rethunk(void);
extern bool cpu_wants_rethunk_at(void *addr);
@@ arch/x86/kernel/alternative.c
#include <linux/mmu_context.h>
#include <linux/bsearch.h>
#include <linux/sync_core.h>
-+#include <linux/execmem.h>
++#include <linux/moduleloader.h>
#include <asm/text-patching.h>
#include <asm/alternative.h>
#include <asm/sections.h>
@@
+ #include <asm/fixmap.h>
+ #include <asm/paravirt.h>
#include <asm/asm-prototypes.h>
- #include <asm/cfi.h>
- #include <asm/ibt.h>
+#include <asm/set_memory.h>
int __read_mostly alternatives_patched;
-@@ arch/x86/kernel/alternative.c: const unsigned char * const x86_nops[ASM_NOP_MAX+1] =
- #endif
- };
+@@ arch/x86/kernel/alternative.c: static int emit_indirect(int op, int reg, u8 *bytes)
+
+ #ifdef CONFIG_MITIGATION_ITS
-+#ifdef CONFIG_MITIGATION_ITS
-+
+static struct module *its_mod;
+static void *its_page;
+static unsigned int its_offset;
@@ arch/x86/kernel/alternative.c: const unsigned char * const x86_nops[ASM_NOP_MAX+
+
+void its_fini_mod(struct module *mod)
+{
++ int i;
++
+ if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS))
+ return;
+
@@ arch/x86/kernel/alternative.c: const unsigned char * const x86_nops[ASM_NOP_MAX+
+ its_page = NULL;
+ mutex_unlock(&text_mutex);
+
-+ for (int i = 0; i < mod->its_num_pages; i++) {
++ for (i = 0; i < mod->its_num_pages; i++) {
+ void *page = mod->its_page_array[i];
-+ execmem_restore_rox(page, PAGE_SIZE);
++ set_memory_ro((unsigned long)page, 1);
++ set_memory_x((unsigned long)page, 1);
+ }
+}
+
+void its_free_mod(struct module *mod)
+{
++ int i;
++
+ if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS))
+ return;
+
-+ for (int i = 0; i < mod->its_num_pages; i++) {
++ for (i = 0; i < mod->its_num_pages; i++) {
+ void *page = mod->its_page_array[i];
-+ execmem_free(page);
++ module_memfree(page);
+ }
+ kfree(mod->its_page_array);
+}
+
+static void *its_alloc(void)
+{
-+ void *page __free(execmem) = execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SIZE);
++ void *page = module_alloc(PAGE_SIZE);
+
+ if (!page)
+ return NULL;
@@ arch/x86/kernel/alternative.c: const unsigned char * const x86_nops[ASM_NOP_MAX+
+ void *tmp = krealloc(its_mod->its_page_array,
+ (its_mod->its_num_pages+1) * sizeof(void *),
+ GFP_KERNEL);
-+ if (!tmp)
++ if (!tmp) {
++ module_memfree(page);
+ return NULL;
++ }
+
+ its_mod->its_page_array = tmp;
+ its_mod->its_page_array[its_mod->its_num_pages++] = page;
-+
-+ execmem_make_temp_rw(page, PAGE_SIZE);
+ }
+
-+ return no_free_ptr(page);
++ return page;
+}
+
+static void *its_allocate_thunk(int reg)
@@ arch/x86/kernel/alternative.c: const unsigned char * const x86_nops[ASM_NOP_MAX+
+ thunk = its_page + its_offset;
+ its_offset += size;
+
-+ return its_init_thunk(thunk, reg);
-+}
++ set_memory_rw((unsigned long)its_page, 1);
++ thunk = its_init_thunk(thunk, reg);
++ set_memory_ro((unsigned long)its_page, 1);
++ set_memory_x((unsigned long)its_page, 1);
+
-+#endif
++ return thunk;
++}
+
- /*
- * Nomenclature for variable names to simplify and clarify this code and ease
- * any potential staring at it:
-@@ arch/x86/kernel/alternative.c: static int emit_call_track_retpoline(void *addr, struct insn *insn, int reg, u8
- #ifdef CONFIG_MITIGATION_ITS
+ static int __emit_trampoline(void *addr, struct insn *insn, u8 *bytes,
+ void *call_dest, void *jmp_dest)
+ {
+@@ arch/x86/kernel/alternative.c: static int __emit_trampoline(void *addr, struct insn *insn, u8 *bytes,
+
static int emit_its_trampoline(void *addr, struct insn *insn, int reg, u8 *bytes)
{
- return __emit_trampoline(addr, insn, bytes,
@@ arch/x86/kernel/alternative.c: static int emit_call_track_retpoline(void *addr,
## arch/x86/kernel/module.c ##
@@ arch/x86/kernel/module.c: int module_finalize(const Elf_Ehdr *hdr,
- ibt_endbr = s;
+ void *pseg = (void *)para->sh_addr;
+ apply_paravirt(pseg, pseg + para->sh_size);
}
-
++
+ its_init_mod(me);
+
- if (retpolines || cfi) {
- void *rseg = NULL, *cseg = NULL;
- unsigned int rsize = 0, csize = 0;
-@@ arch/x86/kernel/module.c: int module_finalize(const Elf_Ehdr *hdr,
+ if (retpolines) {
void *rseg = (void *)retpolines->sh_addr;
apply_retpolines(rseg, rseg + retpolines->sh_size);
}
@@ arch/x86/kernel/module.c: int module_finalize(const Elf_Ehdr *hdr,
+ its_free_mod(mod);
}
- ## include/linux/execmem.h ##
-@@
-
- #include <linux/types.h>
- #include <linux/moduleloader.h>
-+#include <linux/cleanup.h>
-
- #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
- !defined(CONFIG_KASAN_VMALLOC)
-@@ include/linux/execmem.h: void *execmem_alloc(enum execmem_type type, size_t size);
- */
- void execmem_free(void *ptr);
-
-+DEFINE_FREE(execmem, void *, if (_T) execmem_free(_T));
-+
- #ifdef CONFIG_MMU
- /**
- * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
-
## include/linux/module.h ##
@@ include/linux/module.h: struct module {
atomic_t refcnt;---
NOTE: These results are for this patch alone. Full series testing will be performed when all parts are received.
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 15/16 x86/its: Fix build errors when CONFIG_MODULES=n
In reply to Pawan GuptaFrom: Eric Biggers <[email protected]>
commit 9f35e33144ae5377d6a8de86dd3bd4d995c6ac65 upstream.
Fix several build errors when CONFIG_MODULES=n, including the following:
../arch/x86/kernel/alternative.c:195:25: error: incomplete definition of type 'struct module'
195 | for (int i = 0; i < mod->its_num_pages; i++) {
[ pawan: backport: Bring ITS dynamic thunk code under CONFIG_MODULES ]
--- arch/x86/kernel/alternative.c | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/x86/kernel/alternative.c +9 -0
@@ -399,6 +399,7 @@ static int emit_indirect(int op, int reg, u8 *bytes)#ifdef CONFIG_MITIGATION_ITS +#ifdef CONFIG_MODULES static struct module *its_mod;static void *its_page;static unsigned int its_offset;
@@ -519,6 +520,14 @@ static void *its_allocate_thunk(int reg) return thunk; } +#else /* CONFIG_MODULES */ + +static void *its_allocate_thunk (int reg) +{ +return NULL ; +} + +#endif /* CONFIG_MODULES */ static int __emit_trampoline (void *addr,struct insn *insn,u8 *bytes,void *call_dest,void *jmp_dest)
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 15/16] x86/its: Fix build errors when CONFIG_MODULES=n
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: 9f35e33144ae5377d6a8de86dd3bd4d995c6ac65
WARNING: Author mismatch between patch and upstream commit:
Backport author: Pawan Gupta<[email protected]> Commit author: Eric Biggers<[email protected]>
Status in newer kernel trees:
6.14.y | Present (different SHA1: 562da7368caf)
6.12.y | Present (different SHA1: 4e02382eca37)
6.6.y | Present (different SHA1: 11982473786f)
6.1.y | Present (different SHA1: b11b8c7be111)
Note: The patch differs from the upstream commit:
---
1: 9f35e33144ae5 < -: ------------- x86/its: Fix build errors when CONFIG_MODULES=n -: ------------- > 1: 6f92400618dd5 x86/its: Fix build errors when CONFIG_MODULES=n
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
PATCH v3 16/16 x86/its: FineIBT-paranoid vs ITS
In reply to Pawan GuptaFrom: Peter Zijlstra <[email protected]>
commit e52c1dc7455d32c8a55f9949d300e5e87d011fa6 upstream.
FineIBT-paranoid was using the retpoline bytes for the paranoid check, disabling retpolines, because all parts that have IBT also have eIBRS and thus don't need no stinking retpolines.
Except... ITS needs the retpolines for indirect calls must not be in the first half of a cacheline :-/
So what was the paranoid call sequence:
<fineibt_paranoid_start>:
0: 41 ba 78 56 34 12 mov $0x12345678, %r10d 6: 45 3b 53 f7 cmp -0x9(%r11), %r10d a: 4d 8d 5b <f0> lea -0x10(%r11), %r11 e: 75 fd jne d <fineibt_paranoid_start+0xd> 10: 41 ff d3 call *%r11 13: 90 nop
Now becomes:
<fineibt_paranoid_start>:
0: 41 ba 78 56 34 12 mov $0x12345678, %r10d 6: 45 3b 53 f7 cmp -0x9(%r11), %r10d a: 4d 8d 5b f0 lea -0x10(%r11), %r11 e: 2e e8 XX XX XX XX cs call __x86_indirect_paranoid_thunk_r11
Where the paranoid_thunk looks like:
1d: <ea> (bad)
__x86_indirect_paranoid_thunk_r11:
1e: 75 fd jne 1d
__x86_indirect_its_thunk_r11:
20: 41 ff eb jmp *%r11 23: cc int3
[ dhansen: remove initialization to false ]
[ Just a portion of the original commit, in order to fix a build issue
in stable kernels due to backports ]
--- arch/x86/include/asm/alternative.h | 8 ++++++++ arch/x86/kernel/alternative.c | 8 ++++++++ arch/x86/net/bpf_jit_comp.c | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-)
arch/x86/include/asm/alternative.h +8 -0
@@ -5,6 +5,7 @@#include <linux/types.h> #include <linux/stringify.h> #include <asm/asm.h> +#include <asm/bug.h> #define ALTINSTR_FLAG_INV (1 << 15)#define ALT_NOT (feat) ((feat) | ALTINSTR_FLAG_INV)
@@ -84,10 +85,17 @@ struct module;extern void its_init_mod (struct module *mod);extern void its_fini_mod (struct module *mod);extern void its_free_mod (struct module *mod); +extern u8 *its_static_thunk (int reg);#else /* CONFIG_MITIGATION_ITS */ static inline void its_init_mod (struct module *mod) { }static inline void its_fini_mod (struct module *mod) { }static inline void its_free_mod (struct module *mod) { } +static inline u8 *its_static_thunk (int reg) +{ +WARN_ONCE (1 ,"ITS not compiled in" ); + +return NULL ; +}#endif #ifdef CONFIG_RETHUNK
arch/x86/kernel/alternative.c +8 -0
@@ -597,6 +597,14 @@ static bool cpu_wants_indirect_its_thunk_at(unsigned long addr, int reg)/* Lower-half of the cacheline? */ return !(addr & 0x20); } + +u8 *its_static_thunk (int reg) +{ +u8 *thunk = __x86_indirect_its_thunk_array[reg]; + +return thunk; +} +#endif /*
arch/x86/net/bpf_jit_comp.c +1 -1
@@ -449,7 +449,7 @@ static void emit_indirect_jump(u8 **pprog, int reg, u8 *ip)if (IS_ENABLED (CONFIG_MITIGATION_ITS ) &&cpu_feature_enabled (X86_FEATURE_INDIRECT_THUNK_ITS )) {OPTIMIZER_HIDE_VAR (reg); -emit_jump (&prog, &__x86_indirect_its_thunk_array[reg], ip); +emit_jump (&prog,its_static_thunk (reg), ip); }else if (cpu_feature_enabled (X86_FEATURE_RETPOLINE_LFENCE )) {EMIT_LFENCE ();EMIT2 (0xFF ,0xE0 + reg);
Signature · 1 line
2.34.1
Sasha Levin
maintainerRe: [PATCH 5.15 v3 16/16] x86/its: FineIBT-paranoid vs ITS
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: e52c1dc7455d32c8a55f9949d300e5e87d011fa6
WARNING: Author mismatch between patch and upstream commit:
Backport author: Pawan Gupta<[email protected]> Commit author: Peter Zijlstra<[email protected]>
Status in newer kernel trees:
6.14.y | Present (different SHA1: ca25959b8c58)
6.12.y | Present (different SHA1: 093cb85ddbc5)
6.6.y | Present (different SHA1: 98d84892140b)
6.1.y | Present (different SHA1: 3326321f5b9a)
Note: The patch differs from the upstream commit:
---
1: e52c1dc7455d3 < -: ------------- x86/its: FineIBT-paranoid vs ITS -: ------------- > 1: 192d94a36841e x86/its: FineIBT-paranoid vs ITS
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.1.y | Success | Success |
Hi Pawan,
Quoted 17 lines · Pawan Gupta
v3:
- Added patches:
x86/its: Fix build errors whenCONFIG_MODULES=n
x86/its: FineIBT-paranoid vs ITSv2:
- Added missing patch to 6.1 backport.This is a backport of mitigation for Indirect Target Selection (ITS).
ITS is a bug in some Intel CPUs that affects indirect branches including RETs in the first half of a cacheline. Mitigation is to relocate the affected branches to an ITS-safe thunk.
Below additional upstream commits are required to cover some of the special cases like indirects in asm and returns in static calls:
Quoted 7 lines
cfceff8526a4("x86/speculation: Simplify and makeCALL_NOSPECconsistent")052040e34c08("x86/speculation: Add a conditional CS prefix toCALL_NOSPEC")c8c81458863a("x86/speculation: Remove the extra #ifdef aroundCALL_NOSPEC")d2408e043e72("x86/alternative: Optimize returns patching")4ba89dd6ddec("x86/alternatives: Remove faulty optimization")[1] https://github.com/torvalds/linux/commit/6f5bf947bab06f37ff931c359fd5770c4d9cbf87
AFAICS there are no backports yet for as well older stable series than 5.15, in particular 5.10.y (which is used in Debian bullseye yet). Are you planning to make as well backports for the 5.10.y stable series?
Regards,
Salvatore
Quoted 26 lines · Salvatore Bonaccorso
Hi Pawan,
Quoted 23 lines · Pawan Gupta
v3:
- Added patches:
x86/its: Fix build errors whenCONFIG_MODULES=n
x86/its: FineIBT-paranoid vs ITSv2:
- Added missing patch to 6.1 backport.This is a backport of mitigation for Indirect Target Selection (ITS).
ITS is a bug in some Intel CPUs that affects indirect branches including RETs in the first half of a cacheline. Mitigation is to relocate the affected branches to an ITS-safe thunk.
Below additional upstream commits are required to cover some of the special cases like indirects in asm and returns in static calls:
cfceff8526a4("x86/speculation: Simplify and makeCALL_NOSPECconsistent")052040e34c08("x86/speculation: Add a conditional CS prefix toCALL_NOSPEC")c8c81458863a("x86/speculation: Remove the extra #ifdef aroundCALL_NOSPEC")d2408e043e72("x86/alternative: Optimize returns patching")4ba89dd6ddec("x86/alternatives: Remove faulty optimization")
Quoted 5 lines
AFAICS there are no backports yet for as well older stable series than 5.15, in particular 5.10.y (which is used in Debian bullseye yet). Are you planning to make as well backports for the 5.10.y stable series?
I wasn't planning it earlier, but now that there is a good reason, I will work on the backport for 5.10.
Thanks,
Pawan