Xen Security Advisory 469 v2 (CVE-2024-28956) - x86: Indirect Target Selection
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Xen Security Advisory CVE-2024-28956 / XSA-469
version 2
x86: Indirect Target Selection
UPDATES IN VERSION 2
====================
State the CVE.
ISSUE DESCRIPTION
=================
Researchers at VU Amsterdam have released Training Solo, detailing several speculative attacks which bypass current protections.
One issue, which Intel have named Indirect Target Selection, is a bug in the hardware support for prediction-domain isolation. The mitigation for this involves both microcode and software changes in Xen.
For more details, see:
https://vusec.net/projects/training-solo
https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/advisory-guidance/indirect-target-selection.html
Another issue discussed in the Training Solo paper pertains to classic-BPF. Xen does not have any capability similar to BPF filters, so is not believed to be affected by this issue.
IMPACT
======
An attacker might be able to infer the contents of arbitrary host memory, including memory assigned to other guests.
VULNERABLE SYSTEMS
==================
Systems running all versions of Xen are affected.
Only Intel x86 CPUs are potentially affected. CPUs from other manufacturers are not known to be affected.
The affected Intel CPUs are believed to be those which have eIBRS (hardware Spectre-v2 mitigations, starting with Cascade Lake) up to but not including those which have BHI_CTRL (Alder Lake and Sapphire Rapids). See the Intel whitepaper for full details.
MITIGATION
==========
There are no mitigations.
RESOLUTION
==========
Intel are producing microcode to address part of this issue, by extending IBPB. This was released in IPU 2025.1 (February 2025). Consult your dom0 OS vendor and/or hardware vendor for updated microcode.
In addition to the microcode, changes are required to Xen to address the other part of the issue. These involve recompiling Xen using Return Thunks (-mfunction-return). Support for Return Thunks is available in GCC 8 and Clang 15. Therefore, the Xen patches further rely on a toolchain at least this new.
Note that patches for released versions are generally prepared to apply to the stable branches, and may not apply cleanly to the most recent release tarball. Downstreams are encouraged to update to the tip of the stable branch before applying these patches.
xsa469/xsa469-??.patch xen-unstable xsa469/xsa469-4.20-??.patch Xen 4.20.x xsa469/xsa469-4.19-??.patch Xen 4.19.x xsa469/xsa469-4.18-??.patch Xen 4.18.x xsa469/xsa469-4.17-??.patch Xen 4.17.x
$ sha256sum xsa469*/* 24648f43282a4c4917f49a828f0889139ffd08710884c2d5d2a149213f889316 xsa469/xsa469-01.patch f9e1e999e5121b71263ccac9e5dbc7f2422751cd8776526b59a3a535245ee271 xsa469/xsa469-02.patch 3aebd84d8c9374e8db4f28a1bd0265ff46d60a142e8cc983c0eae99a3caa4459 xsa469/xsa469-03.patch 1f836801a543d00a4e79daff0438932c7d7b5e9ee76dead124d290e805adc0f2 xsa469/xsa469-4.17-01.patch aaa7b6967b3ff8b279675edafbccff4e1882202251e8f37c1aaab8e16f4ca8c9 xsa469/xsa469-4.17-02.patch cf9a224628f218a7afdf7a275da270b8f469342133d3239c19a8b05adb8d9d9e xsa469/xsa469-4.17-03.patch 5179885f9452f932b9829e942a6942d7a1bba6820c383b0e0ac7ade34f6f444c xsa469/xsa469-4.17-04.patch 7d44272960d7ecdc920a14eb19c686c6808fe980bb85c3a467b5e343f137d927 xsa469/xsa469-4.17-05.patch 3984f20f3b84a579a4828da8c60268f55f1ff2dfc030d1790efb52a6ad659cd9 xsa469/xsa469-4.17-06.patch 8ec20c0943def1764c858c76699991aa0b7af76a8773be8219d62c240cf0b294 xsa469/xsa469-4.17-07.patch 00943be479ac428a496dec248c33e9ae9a0002e437a50c46a43d15146ddcccd8 xsa469/xsa469-4.18-01.patch b5e9ab3ba33c7a5ad2948521a71d73b8e587d4b86fb73b3ce06557343753b097 xsa469/xsa469-4.18-02.patch cf9a224628f218a7afdf7a275da270b8f469342133d3239c19a8b05adb8d9d9e xsa469/xsa469-4.18-03.patch 5179885f9452f932b9829e942a6942d7a1bba6820c383b0e0ac7ade34f6f444c xsa469/xsa469-4.18-04.patch d7271118c516ea2b57b82afbaa67e44ebc0bee9067925850671a47ba7e6916fc xsa469/xsa469-4.18-05.patch 99aa42f3b4d492c21d3027f74abe20183cdd411fdf94d62c10c423e6516db5aa xsa469/xsa469-4.18-06.patch 893e3b721fe00623a8d75414f40f11033a4a3999d368a141eec8652d9733a77e xsa469/xsa469-4.18-07.patch a5a958452c4b3adc820692349103892ec3ffc3195c9b4e1fc55547d2ec7aec57 xsa469/xsa469-4.19-01.patch 2f3a3dadc69026ffa0a093ae34c239023e2336b3f9ec90a1ddc5df7b103c158c xsa469/xsa469-4.19-02.patch 3aebd84d8c9374e8db4f28a1bd0265ff46d60a142e8cc983c0eae99a3caa4459 xsa469/xsa469-4.19-03.patch bbdb2fcda3d2fe36ceaaf235700158f5a4f08c8983732265725cc7ac811c9bd2 xsa469/xsa469-4.19-04.patch fd01af8b8cc66b6a1f0e2a7383b4c320cbb1513b7f23a7bd84fbb3e626751122 xsa469/xsa469-4.19-05.patch 1ed51e005c7f07a99793bfc97680bc02e8661412d88a853594f40a02599e03c2 xsa469/xsa469-4.19-06.patch 4dda661661f23b48f62caeaadd1b02ef79b2d7b08e61ab44c2eacdd2171f63b8 xsa469/xsa469-4.19-07.patch 27ce09ab9bb9460bec08761b2f739ce5adb292dc13664dfa425e42e5b8db8731 xsa469/xsa469-4.20-01.patch f9e1e999e5121b71263ccac9e5dbc7f2422751cd8776526b59a3a535245ee271 xsa469/xsa469-4.20-02.patch 3aebd84d8c9374e8db4f28a1bd0265ff46d60a142e8cc983c0eae99a3caa4459 xsa469/xsa469-4.20-03.patch bbdb2fcda3d2fe36ceaaf235700158f5a4f08c8983732265725cc7ac811c9bd2 xsa469/xsa469-4.20-04.patch 656031e56bbf8c8d0e8bd294c1e8857dd80850ae3cc68efbf42564f2b9efdb1f xsa469/xsa469-4.20-05.patch 3343a834e0931b56490a50cc9fc7488a13a98a00923e4399020f628b3aab0220 xsa469/xsa469-4.20-06.patch 465d916cac02bb4ae1edf9db34fe5d2426ac1681a1078d331fbdf449834692c4 xsa469/xsa469-4.20-07.patch 97335c870407928039464307611c0b9687fd7cdfde5124ec5441a4fddfaa4165 xsa469/xsa469-04.patch 226771d9359b3c341c0978a145c29478d4c6ca196ec67d48c3df6115aee27940 xsa469/xsa469-05.patch 17eae8965871975569be4228943d5136eb65951d9c9513d2b0fe743e8cf8e4ef xsa469/xsa469-06.patch fa8b7ed035e242a3881431150ee86965f26520dab4432e3f7ac9862063491f72 xsa469/xsa469-07.patch $ -----BEGIN PGP SIGNATURE-----
iQFABAEBCAAqFiEEI+MiLBRfRHX6gGCng/4UyVfoK9kFAmgiLJAMHHBncEB4ZW4u b3JnAAoJEIP+FMlX6CvZ7uUIAIrV83bfhcHAnZvWo5CGOtuyuwQQuobYKqUcC3fq /LGKVwmrQVCR6qm2YeI8TIhJfet8OiqutteaVknAJlegVR8uTIznIr7CzjmzgoR5 ojnw0Ae0rQ5rhzSeMvBD1VsbheBI7nmLR9dorMs+Rp9MiVzboT81XOpWDTMHisjI 4LpN+TB+yIB76hQQ4divDnqFKxA1SFQWrmn2bcr3v5cJ1cctvLOic/67WO9Uto92 5tG7iV68cStski0wMvjO1isalCTKSaU7eDjTe1ar+/SwhE4T0RKfcpim3xcc5ZVk cH/xKcK4vbJUKIxjqnDrEdT3uGk1vdVxJfNAOCpfZc5EwiI=
=A2YZ
-----END PGP SIGNATURE-----
From: Andrew Cooper <[email protected]>
Subject: x86/alternative: Support replacements when a feature is not present
Use the top bit of a->cpuid to express inverted polarity. This requires stripping the top bit back out when performing the sanity checks.
Despite only being used once, create a replace boolean to express the decision more clearly in _apply_alternatives().
xen/arch/x86/alternative.c +11 -3
@@ -228,6 +228,8 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,uint8_t *repl =ALT_REPL_PTR (a);uint8_t buf[MAX_PATCH_LEN ];unsigned int total_len = a->orig_len + a->pad_len; +unsigned int feat = a->cpuid & ~ALT_FLAG_NOT ; +bool inv = a->cpuid &ALT_FLAG_NOT , replace;if ( a->repl_len > total_len ) {
@@ -245,11 +247,11 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,return -ENOSPC ; } -if ( a->cpuid >=NCAPINTS *32 ) +if ( feat >=NCAPINTS *32 ) {printk (XENLOG_ERR "Alt for %ps, feature %#x outside of featureset range %#x\n" , -ALT_ORIG_PTR (a), a->cpuid,NCAPINTS *32 ); +ALT_ORIG_PTR (a), feat,NCAPINTS *32 );return -ERANGE ; }
@@ -271,8 +273,14 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,if ( a->priv )continue ; +/* +* Should a replacement be performed? Most replacements have positive +* polarity, but we support negative polarity too. +*/ + replace =boot_cpu_has (feat) ^ inv; +/* If there is no replacement to make, see about optimising the nops. */ -if ( !boot_cpu_has (a->cpuid) ) +if ( !replace ) {/* Origin site site already touched? Don't nop anything. */ if ( base->priv )
xen/arch/x86/include/asm/alternative-asm.h +1 -1
@@ -12,7 +12,7 @@* instruction. See apply_alternatives(). */ .macro altinstruction_entry orig, repl, feature, orig_len, repl_len, pad_len - .if \feature >= NCAPINTS * 32 + .if ((\feature) & ~ALT_FLAG_NOT) >= NCAPINTS * 32 .error "alternative feature outside of featureset range" .endif .long \orig - .
xen/arch/x86/include/asm/alternative.h +9 -2
@@ -1,6 +1,13 @@#ifndef __X86_ALTERNATIVE_H__#define __X86_ALTERNATIVE_H__ +/* +* Common to both C and ASM. Express a replacement when a feature is not +* available. +*/ +#define ALT_FLAG_NOT (1 << 15) +#define ALT_NOT (x) (ALT_FLAG_NOT | (x)) +#ifdef __ASSEMBLY__#include <asm/alternative-asm.h> #else
@@ -14,7 +21,7 @@struct __packed alt_instr {int32_t orig_offset;/* original instruction */ int32_t repl_offset;/* offset to replacement instruction */ -uint16_t cpuid;/* cpuid bit set for replacement */ +uint16_t cpuid;/* cpuid bit set for replacement (top bit is polarity) */ uint8_t orig_len;/* length of original instruction */ uint8_t repl_len;/* length of new instruction */ uint8_t pad_len;/* length of build-time padding */
@@ -61,7 +68,7 @@ extern void alternative_instructions(void); alt_repl_len(n2)) "-" alt_orig_len)#define ALTINSTR_ENTRY (feature, num) \ - " .if " STR(feature) " >= " STR(NCAPINTS * 32) "\n" \ + " .if (" STR(feature & ~ALT_FLAG_NOT) ") >= " STR(NCAPINTS * 32) "\n" \ " .error \"alternative feature outside of featureset range\"\n" \ " .endif\n" \ " .long .LXEN%=_orig_s - .\n"/* label */ \
From: Andrew Cooper <[email protected]>
Subject: x86/guest: Remove use of the Xen hypercall_page
In order to protect against ITS, Xen needs to start using return thunks. Therefore the advice in XSA-466 becomes relevant, and the hypercall_page needs to be removed.
Implement early_hypercall(), with infrastructure to figure out the correct instruction on first use. Use ALTERNATIVE()s to result in inline hypercalls, including the ALT_NOT() form so we only need a single synthetic feature bit.
No overall change.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/guest/xen/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-y += hypercall_page.o +obj-bin-y += hypercall.init.o obj-y += xen.o obj-bin-$(CONFIG_PVH_GUEST) += pvh-boot.init.o
xen/arch/x86/guest/xen/hypercall.S new file +50 -0
50 lines not on this page · raw message
xen/arch/x86/guest/xen/hypercall_page.S deleted +0 -76
76 lines not on this page · raw message
xen/arch/x86/guest/xen/xen.c +44 -4
66 lines not on this page · raw message
xen/arch/x86/include/asm/cpufeatures.h +1 -0
@@ -42,6 +42,7 @@ XEN_CPUFEATURE(XEN_SHSTK, X86_SYNTH(26)) /* Xen uses CET Shadow Stacks * XEN_CPUFEATURE(XEN_IBT, X86_SYNTH(27)) /* Xen uses CET Indirect Branch Tracking */ XEN_CPUFEATURE(IBPB_ENTRY_PV, X86_SYNTH(28)) /* MSR_PRED_CMD used by Xen for PV */ XEN_CPUFEATURE(IBPB_ENTRY_HVM, X86_SYNTH(29)) /* MSR_PRED_CMD used by Xen for HVM */ +XEN_CPUFEATURE (USE_VMCALL ,X86_SYNTH (30 ))/* Use VMCALL instead of VMMCALL */ /* Bug words follow the synthetic words. */#define X86_NR_BUG 1
xen/arch/x86/include/asm/guest/xen-hcall.h +16 -8
@@ -30,9 +30,11 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__) ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)) \ : "memory" ); \ (type)res; \
@@ -42,10 +44,12 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__) \ ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)), "2" ((long)(a2)) \ : "memory" ); \ (type)res; \
@@ -55,10 +59,12 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__), "=d" (tmp__) \ ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)), "2" ((long)(a2)), "3" ((long)(a3)) \ : "memory" ); \ (type)res; \
@@ -69,10 +75,12 @@long res, tmp__; \ register long _a4 asm ("r10") = ((long)(a4)); \ asm volatile ( \ -"call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__), "=d" (tmp__), \ "=&r" (tmp__) ASM_CALL_CONSTRAINT \ - : [offset]"i" (hcall *32 ), \ + :"0" (hcall), \"1" ((long )(a1)),"2" ((long )(a2)),"3" ((long )(a3)), \"4" (_a4) \ : "memory" ); \
From: Jan Beulich <[email protected]>
Subject: x86/thunk: (Mis)align __x86_indirect_thunk_* to mitigate ITS
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
Arrange for __x86_indirect_thunk_* to always be in the second half.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/indirect-thunk.S +14 -0
@@ -11,6 +11,10 @@ #include <asm/asm_defns.h> +/* Alignment is dealt with explicitly here; override the respective macro. */ +#undef SYM_ALIGN +#define SYM_ALIGN(align...) + .macro IND_THUNK_RETPOLINE reg:req call 1f int3
@@ -35,6 +39,16 @@ .macro GEN_INDIRECT_THUNK reg:req .section .text.__x86_indirect_thunk_\reg, "ax", @progbits + /* + * The Indirect Target Selection speculative vulnerability means that + * indirect branches (including RETs) are unsafe when in the first + * half of a cacheline. Arrange for them to be in the second half. + * + * Align to 64, then skip 32. + */ + .balign 64 + .fill 32, 1, 0xcc + FUNC(__x86_indirect_thunk_\reg) ALTERNATIVE_2 __stringify(IND_THUNK_RETPOLINE \reg), \ __stringify(IND_THUNK_LFENCE \reg), X86_FEATURE_IND_THUNK_LFENCE, \
From: Andrew Cooper <[email protected]>
Subject: x86/alternative: Support replacements when a feature is not present
Use the top bit of a->cpuid to express inverted polarity. This requires stripping the top bit back out when performing the sanity checks.
Despite only being used once, create a replace boolean to express the decision more clearly in _apply_alternatives().
xen/arch/x86/alternative.c +10 -2
@@ -209,10 +209,12 @@ static void init_or_livepatch _apply_alternatives(struct alt_instr *start,uint8_t *repl =ALT_REPL_PTR (a);uint8_t buf[MAX_PATCH_LEN ];unsigned int total_len = a->orig_len + a->pad_len; +unsigned int feat = a->cpuid & ~ALT_FLAG_NOT ; +bool inv = a->cpuid &ALT_FLAG_NOT , replace;BUG_ON (a->repl_len > total_len);BUG_ON (total_len >sizeof (buf)); -BUG_ON (a->cpuid >=NCAPINTS *32 ); +BUG_ON (feat >=NCAPINTS *32 );/* * Detect sequences of alt_instr's patching the same origin site, and
@@ -235,8 +237,14 @@ static void init_or_livepatch _apply_alternatives(struct alt_instr *start,continue ; } +/* +* Should a replacement be performed? Most replacements have positive +* polarity, but we support negative polarity too. +*/ + replace =boot_cpu_has (feat) ^ inv; +/* If there is no replacement to make, see about optimising the nops. */ -if ( !boot_cpu_has (a->cpuid) ) +if ( !replace ) {/* Origin site site already touched? Don't nop anything. */ if ( base->priv )
xen/arch/x86/include/asm/alternative.h +8 -1
@@ -1,6 +1,13 @@#ifndef __X86_ALTERNATIVE_H__#define __X86_ALTERNATIVE_H__ +/* +* Common to both C and ASM. Express a replacement when a feature is not +* available. +*/ +#define ALT_FLAG_NOT (1 << 15) +#define ALT_NOT (x) (ALT_FLAG_NOT | (x)) +#ifdef __ASSEMBLY__#include <asm/alternative-asm.h> #else
@@ -11,7 +18,7 @@struct __packed alt_instr {int32_t orig_offset;/* original instruction */ int32_t repl_offset;/* offset to replacement instruction */ -uint16_t cpuid;/* cpuid bit set for replacement */ +uint16_t cpuid;/* cpuid bit set for replacement (top bit is polarity) */ uint8_t orig_len;/* length of original instruction */ uint8_t repl_len;/* length of new instruction */ uint8_t pad_len;/* length of build-time padding */
From: Andrew Cooper <[email protected]>
Subject: x86/guest: Remove use of the Xen hypercall_page
In order to protect against ITS, Xen needs to start using return thunks. Therefore the advice in XSA-466 becomes relevant, and the hypercall_page needs to be removed.
Implement early_hypercall(), with infrastructure to figure out the correct instruction on first use. Use ALTERNATIVE()s to result in inline hypercalls, including the ALT_NOT() form so we only need a single synthetic feature bit.
No overall change.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/guest/xen/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-y += hypercall_page.o +obj-bin-y += hypercall.init.o obj-y += xen.o obj-bin-$(CONFIG_PVH_GUEST) += pvh-boot.init.o
xen/arch/x86/guest/xen/hypercall.S new file +52 -0
52 lines not on this page · raw message
xen/arch/x86/guest/xen/hypercall_page.S deleted +0 -78
78 lines not on this page · raw message
xen/arch/x86/guest/xen/xen.c +44 -4
66 lines not on this page · raw message
xen/arch/x86/include/asm/cpufeatures.h +1 -0
@@ -42,6 +42,7 @@ XEN_CPUFEATURE(XEN_SHSTK, X86_SYNTH(26)) /* Xen uses CET Shadow Stacks * XEN_CPUFEATURE(XEN_IBT, X86_SYNTH(27)) /* Xen uses CET Indirect Branch Tracking */ XEN_CPUFEATURE(IBPB_ENTRY_PV, X86_SYNTH(28)) /* MSR_PRED_CMD used by Xen for PV */ XEN_CPUFEATURE(IBPB_ENTRY_HVM, X86_SYNTH(29)) /* MSR_PRED_CMD used by Xen for HVM */ +XEN_CPUFEATURE (USE_VMCALL ,X86_SYNTH (30 ))/* Use VMCALL instead of VMMCALL */ /* Bug words follow the synthetic words. */#define X86_NR_BUG 1
xen/arch/x86/include/asm/guest/xen-hcall.h +16 -8
@@ -41,9 +41,11 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__) ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)) \ : "memory" ); \ (type)res; \
@@ -53,10 +55,12 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__) \ ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)), "2" ((long)(a2)) \ : "memory" ); \ (type)res; \
@@ -66,10 +70,12 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__), "=d" (tmp__) \ ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)), "2" ((long)(a2)), "3" ((long)(a3)) \ : "memory" ); \ (type)res; \
@@ -80,10 +86,12 @@long res, tmp__; \ register long _a4 asm ("r10") = ((long)(a4)); \ asm volatile ( \ -"call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__), "=d" (tmp__), \ "=&r" (tmp__) ASM_CALL_CONSTRAINT \ - : [offset]"i" (hcall *32 ), \ + :"0" (hcall), \"1" ((long )(a1)),"2" ((long )(a2)),"3" ((long )(a3)), \"4" (_a4) \ : "memory" ); \
From: Jan Beulich <[email protected]>
Subject: x86/thunk: (Mis)align __x86_indirect_thunk_* to mitigate ITS
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
Arrange for __x86_indirect_thunk_* to always be in the second half.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/indirect-thunk.S +10 -0
@@ -35,6 +35,16 @@ .macro GEN_INDIRECT_THUNK reg:req .section .text.__x86_indirect_thunk_\reg, "ax", @progbits + /* + * The Indirect Target Selection speculative vulnerability means that + * indirect branches (including RETs) are unsafe when in the first + * half of a cacheline. Arrange for them to be in the second half. + * + * Align to 64, then skip 32. + */ + .balign 64 + .fill 32, 1, 0xcc + ENTRY(__x86_indirect_thunk_\reg) ALTERNATIVE_2 __stringify(IND_THUNK_RETPOLINE \reg), \ __stringify(IND_THUNK_LFENCE \reg), X86_FEATURE_IND_THUNK_LFENCE, \
From: Andrew Cooper <[email protected]>
Subject: x86/thunk: (Mis)align the RETs in clear_bhb_loops() to mitigate ITS
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
clear_bhb_loops() has a precise layout of branches. The alignment for performance cause the RETs to always be in an unsafe position, and converting those to return thunks changes the branching pattern. While such a conversion is believed to be safe, clear_bhb_loops() is also a performance-relevant fastpath, so (mis)align the RETs to be in a safe position.
No functional change.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/bhb-thunk.S +12 -5
@@ -52,7 +52,12 @@ ENTRY(clear_bhb_tsx) * ret * * The CALL/RETs are necessary to prevent the Loop Stream Detector from - * interfering. The alignment is for performance and not safety. + * interfering. + * + * The .balign's are for performance, but they cause the RETs to be in unsafe + * positions with respect to Indirect Target Selection. The .skips are to + * move the RETs into ITS-safe positions, rather than using the slowpath + * through __x86_return_thunk. * * The "short" sequence (5 and 5) is for CPUs prior to Alder Lake / Sapphire * Rapids (i.e. Cores prior to Golden Cove and/or Gracemont).
@@ -68,12 +73,14 @@ ENTRY(clear_bhb_loops) jmp 5f int3 - .align 64 + .balign 64 + .skip 32 - (.Lr1 - 1f), 0xcc 1: call 2f - ret +.Lr1: ret int3 - .align 64 + .balign 64 + .skip 32 - 18 /* (.Lr2 - 2f) but Clang IAS doesn't like this */, 0xcc 2: ALTERNATIVE "mov $5, %eax", "mov $7, %eax", X86_SPEC_BHB_LOOPS_LONG 3: jmp 4f
@@ -85,7 +92,7 @@ ENTRY(clear_bhb_loops) sub $1, %ecx jnz 1b - ret +.Lr2: ret 5: /* * The Intel sequence has an LFENCE here. The purpose is to ensure
From: Andrew Cooper <[email protected]>
Subject: x86/stubs: Introduce place_ret() to abstract away raw 0xc3's
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline. This means it's not safe for logic using the stubs to write raw 0xc3's.
Introduce place_ret() which, for now, writes a raw 0xc3 but will contain additional logic when return thunks are in use.
stub_selftest() doesn't strictly need to be converted as they only run on boot, but doing so gets us a partial test of place_ret() too.
No functional change.
This is part of XSA-469 / CVE-2024-28956
tools/tests/x86_emulator/x86-emulate.h +6 -0
@@ -67,6 +67,12 @@#define is_canonical_address (x) (((int64_t)(x) >> 47) == ((int64_t)(x) >> 63)) +static inline void *place_ret (void *ptr) +{ + *(uint8_t *)ptr =0xc3 ; +return ptr +1 ; +} +extern uint32_t mxcsr_mask;extern struct cpu_policy cp;
xen/arch/x86/Makefile +2 -4
@@ -10,9 +10,7 @@ obj-$(CONFIG_XENOPROF) += oprofile/ obj-$(CONFIG_PV) += pv/ obj-y += x86_64/ -alternative-y := alternative.init.o -alternative-$(CONFIG_LIVEPATCH) := -obj-bin-y += $(alternative-y) +obj-y += alternative.o obj-y += apic.o obj-y += bhb-thunk.o obj-y += bitops.o
@@ -40,7 +38,7 @@ obj-y += hypercall.o obj-y += i387.o obj-y += i8259.o obj-y += io_apic.o -obj-$(CONFIG_LIVEPATCH) += alternative.o livepatch.o +obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-y += msi.o obj-y += msr.o obj-$(CONFIG_INDIRECT_THUNK) += indirect-thunk.o
xen/arch/x86/alternative.c +14 -0
@@ -149,6 +149,20 @@ void init_or_livepatch add_nops(void *insns, unsigned int len) } } +/* +* Place a return at @ptr. @ptr must be in the writable alias of a stub. +* +* Returns the next position to write into the stub. +*/ +void *place_ret (void *ptr) +{ +uint8_t *p = ptr; + + *p++ =0xc3 ; + +return p; +} +/* * text_poke - Update instructions on a live kernel or non-executed code. * @addr: address to modify
xen/arch/x86/extable.c +6 -5
@@ -135,20 +135,20 @@ search_exception_table(const struct cpu_user_regs *regs, unsigned long *stub_ra) int __init cf_check stub_selftest(void) {static const struct { -uint8_t opc[8 ]; +uint8_t opc[7 ];uint64_t rax;union stub_exception_token res; } tests[]__initconst = { #define endbr64 0xf3, 0x0f, 0x1e, 0xfa - { .opc = { endbr64,0x0f ,0xb9 ,0xc3 ,0xc3 },/* ud1 */ + { .opc = { endbr64,0x0f ,0xb9 ,0x90 },/* ud1 */ .res.fields.trapnr = TRAP_invalid_op }, - { .opc = { endbr64,0x90 ,0x02 ,0x00 ,0xc3 },/* nop; add (%rax),%al */ + { .opc = { endbr64,0x90 ,0x02 ,0x00 },/* nop; add (%rax),%al */ .rax =0x0123456789abcdef , .res.fields.trapnr = TRAP_gp_fault }, - { .opc = { endbr64,0x02 ,0x04 ,0x04 ,0xc3 },/* add (%rsp,%rax),%al */ + { .opc = { endbr64,0x02 ,0x04 ,0x04 },/* add (%rsp,%rax),%al */ .rax =0xfedcba9876543210 , .res.fields.trapnr = TRAP_stack_error }, - { .opc = { endbr64,0xcc ,0xc3 ,0xc3 ,0xc3 },/* int3 */ + { .opc = { endbr64,0xcc ,0x90 ,0x90 },/* int3 */ .res.fields.trapnr = TRAP_int3 }, #undef endbr64 };
@@ -167,6 +167,7 @@ int __init cf_check stub_selftest(void)memset (ptr,0xcc ,STUB_BUF_SIZE /2 );memcpy (ptr, tests[i].opc,ARRAY_SIZE (tests[i].opc)); +place_ret (ptr +ARRAY_SIZE (tests[i].opc));unmap_domain_page (ptr); asm volatile ( "INDIRECT_CALL %[stb]\n"
xen/arch/x86/include/asm/alternative.h +2 -0
@@ -30,6 +30,8 @@ struct __packed alt_instr {#define ALT_REPL_PTR (a) __ALT_PTR(a, repl_offset)extern void add_nops (void *insns,unsigned int len); +void *place_ret (void *ptr); +/* Similar to alternative_instructions except it can be run with IRQs enabled. */ extern void apply_alternatives (struct alt_instr *start,struct alt_instr *end);extern void alternative_instructions (void );
xen/arch/x86/pv/emul-priv-op.c +3 -2
@@ -88,7 +88,6 @@ static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode,0x41 ,0x5c ,/* pop %r12 */ 0x5d ,/* pop %rbp */ 0x5b, /* pop %rbx */ - 0xc3, /* ret */ };const struct stubs *this_stubs = &this_cpu (stubs);
@@ -138,11 +137,13 @@ static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode,APPEND_CALL (save_guest_gprs);APPEND_BUFF (epilogue); + p =place_ret (p);/* Build-time best effort attempt to catch problems. */ BUILD_BUG_ON (STUB_BUF_SIZE /2 < (sizeof (prologue) +sizeof (epilogue) +10 /* 2x call */ + -MAX (3 /* default stub */ ,IOEMUL_QUIRK_STUB_BYTES ))); +MAX (3 /* default stub */ ,IOEMUL_QUIRK_STUB_BYTES ) + +1 /* ret */ ));/* Runtime confirmation that we haven't clobbered an adjacent stub. */ BUG_ON (STUB_BUF_SIZE /2 < (p - ctxt->io_emul_stub));
xen/arch/x86/x86_emulate/x86_emulate.c +44 -38
302 lines not on this page · raw message
From: Jan Beulich <[email protected]>
Subject: x86/thunk: Build Xen with Return Thunks
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
In order to mitigate this, build with return thunks and arrange for __x86_return_thunk to be (mis)aligned in the same manner as __x86_indirect_thunk_* so the RET instruction is placed in a safe location.
place_ret() needs to conditionally emit JMP __x86_return_thunk instead of RET.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/Kconfig +5 -0
@@ -35,9 +35,14 @@ config ARCH_DEFCONFIG default "arch/x86/configs/x86_64_defconfig" config CC_HAS_INDIRECT_THUNK + # GCC >= 8 or Clang >= 6 def_bool $(cc-option,-mindirect-branch-register) || \ $(cc-option,-mretpoline-external-thunk) +config CC_HAS_RETURN_THUNK + # GCC >= 8 or Clang >= 15 + def_bool $(cc-option,-mfunction-return=thunk-extern) + config HAS_AS_CET_SS # binutils >= 2.29 or LLVM >= 6 def_bool $(as-instr,wrssq %rax$(comma)0;setssbsy)
xen/arch/x86/Makefile +1 -0
@@ -42,6 +42,7 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-y += msi.o obj-y += msr.o obj-$(CONFIG_INDIRECT_THUNK) += indirect-thunk.o +obj-$(CONFIG_RETURN_THUNK) += indirect-thunk.o obj-$(CONFIG_PV) += ioport_emulate.o obj-y += irq.o obj-$(CONFIG_KEXEC) += machine_kexec.o
xen/arch/x86/acpi/wakeup_prot.S +1 -1
@@ -131,7 +131,7 @@ ENTRY(s3_resume) pop %r12 pop %rbx pop %rbp - ret + RET .data .align 16
xen/arch/x86/alternative.c +30 -1
@@ -149,16 +149,45 @@ void init_or_livepatch add_nops(void *insns, unsigned int len) } } +void nocall __x86_return_thunk(void); +/* * Place a return at @ptr. @ptr must be in the writable alias of a stub. * +* When CONFIG_RETURN_THUNK is active, this may be a JMP __x86_return_thunk +* instead, depending on the safety of @ptr with respect to Indirect Target +* Selection. +* * Returns the next position to write into the stub. */ void *place_ret (void *ptr) { +unsigned long addr = (unsigned long )ptr;uint8_t *p = ptr; - *p++ =0xc3 ; +/* +* When Return Thunks are used, if a RET would be unsafe at this location +* with respect to Indirect Target Selection (i.e. if addr is in the first +* half of a cacheline), insert a JMP __x86_return_thunk instead. +* +* The displacement needs to be relative to the executable alias of the +* stub, not to @ptr which is the writeable alias. +*/ +if (IS_ENABLED (CONFIG_RETURN_THUNK ) && !(addr &0x20 ) ) + { +long stub_va = (this_cpu (stubs.addr) &PAGE_MASK ) + (addr & ~PAGE_MASK ); +long disp = (long )__x86_return_thunk - (stub_va +5 ); + +BUG_ON ((int32_t )disp != disp); + + *p++ =0xe9 ; + *(int32_t *)p = disp; + p +=4 ; + } +else + { + *p++ =0xc3 ; + }return p; }
xen/arch/x86/arch.mk +3 -0
@@ -50,6 +50,9 @@ CFLAGS-$(CONFIG_CC_IS_GCC) += -fno-jump-tables CFLAGS-$(CONFIG_CC_IS_CLANG) += -mretpoline-external-thunk endif +# Compile with return thunk support if selected. +CFLAGS-$(CONFIG_RETURN_THUNK) += -mfunction-return=thunk-extern + ifdef CONFIG_XEN_IBT # Force -fno-jump-tables to work around # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104816
xen/arch/x86/bhb-thunk.S +1 -1
@@ -23,7 +23,7 @@ ENTRY(clear_bhb_tsx) 0: .byte 0xc6, 0xf8, 0 /* xabort $0 */ int3 1: - ret + RET .size clear_bhb_tsx, . - clear_bhb_tsx .type clear_bhb_tsx, @function
xen/arch/x86/clear_page.S +2 -1
@@ -1,5 +1,6 @@ .file __FILE__ +#include <asm/asm_defns.h> #include <asm/page.h> ENTRY(clear_page_sse2)
@@ -15,4 +16,4 @@ ENTRY(clear_page_sse2) jnz 0b sfence - ret + RET
xen/arch/x86/copy_page.S +2 -1
@@ -1,5 +1,6 @@ .file __FILE__ +#include <asm/asm_defns.h> #include <asm/page.h> #define src_reg %rsi
@@ -40,4 +41,4 @@ ENTRY(copy_page_sse2) movnti tmp4_reg, 3*WORD_SIZE(dst_reg) sfence - ret + RET
xen/arch/x86/efi/check.c +3 -0
@@ -3,6 +3,9 @@ int __attribute__((__ms_abi__)) test(int i)return i; } +/* In case -mfunction-return is in use. */ +void __x86_return_thunk (void ) {}; +/* * Populate an array with "addresses" of relocatable and absolute values. * This is to probe ld for (a) emitting base relocations at all and (b) not
xen/arch/x86/include/asm/asm-defns.h +6 -0
@@ -47,6 +47,12 @@ .endif .endm +#ifdef CONFIG_RETURN_THUNK +# define RET jmp __x86_return_thunk +#else +# define RET ret +#endif +#ifdef CONFIG_XEN_IBT # define ENDBR64 endbr64#else
xen/arch/x86/indirect-thunk.S +27 -0
@@ -11,6 +11,9 @@ #include <asm/asm_defns.h> + +#ifdef CONFIG_INDIRECT_THUNK + .macro IND_THUNK_RETPOLINE reg:req call 1f int3
@@ -60,3 +63,27 @@ ENTRY(__x86_indirect_thunk_\reg) .irp reg, ax, cx, dx, bx, bp, si, di, 8, 9, 10, 11, 12, 13, 14, 15 GEN_INDIRECT_THUNK reg=r\reg .endr + +#endif /* CONFIG_INDIRECT_THUNK */ + +#ifdef CONFIG_RETURN_THUNK + .section .text.entry.__x86_return_thunk, "ax", @progbits + + /* + * The Indirect Target Selection speculative vulnerability means that + * indirect branches (including RETs) are unsafe when in the first + * half of a cacheline. Arrange for them to be in the second half. + * + * Align to 64, then skip 32. + */ + .balign 64 + .fill 32, 1, 0xcc + +ENTRY(__x86_return_thunk) + ret + int3 /* Halt straight-line speculation */ + + .size __x86_return_thunk, . - __x86_return_thunk + .type __x86_return_thunk, @function + +#endif /* CONFIG_RETURN_THUNK */
xen/arch/x86/pv/emul-priv-op.c +1 -1
@@ -143,7 +143,7 @@ static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode,BUILD_BUG_ON (STUB_BUF_SIZE /2 < (sizeof (prologue) +sizeof (epilogue) +10 /* 2x call */ +MAX (3 /* default stub */ ,IOEMUL_QUIRK_STUB_BYTES ) + -1 /* ret */ )); + (IS_ENABLED (CONFIG_RETURN_THUNK ) ?5 :1 )/* ret */ ));/* Runtime confirmation that we haven't clobbered an adjacent stub. */ BUG_ON (STUB_BUF_SIZE /2 < (p - ctxt->io_emul_stub));
xen/arch/x86/pv/gpr_switch.S +2 -2
@@ -26,7 +26,7 @@ ENTRY(load_guest_gprs) movq UREGS_r15(%rdi), %r15 movq UREGS_rcx(%rdi), %rcx movq UREGS_rdi(%rdi), %rdi - ret + RET .size load_guest_gprs, . - load_guest_gprs .type load_guest_gprs, STT_FUNC
@@ -51,7 +51,7 @@ ENTRY(save_guest_gprs) movq %rbx, UREGS_rbx(%rdi) movq %rdx, UREGS_rdx(%rdi) movq %rcx, UREGS_rcx(%rdi) - ret + RET .size save_guest_gprs, . - save_guest_gprs .type save_guest_gprs, STT_FUNC
xen/arch/x86/spec_ctrl.c +3 -0
@@ -575,6 +575,9 @@ static void __init print_details(enum ind_thunk thunk)#ifdef CONFIG_INDIRECT_THUNK " INDIRECT_THUNK"#endif +#ifdef CONFIG_RETURN_THUNK + " RETURN_THUNK" +#endif #ifdef CONFIG_SHADOW_PAGING " SHADOW_PAGING"#endif
xen/arch/x86/x86_64/compat/entry.S +3 -3
@@ -183,7 +183,7 @@ ENTRY(cr4_pv32_restore) mov %rax, %cr4 mov %rax, (%rdx) pop %rdx - ret + RET 0: #ifndef NDEBUG /* Check that _all_ of the bits intended to be set actually are. */
@@ -202,7 +202,7 @@ ENTRY(cr4_pv32_restore) #endif pop %rdx xor %eax, %eax - ret + RET ENTRY(compat_syscall) /* Fix up reported %cs/%ss for compat domains. */
@@ -329,7 +329,7 @@ __UNLIKELY_END(compat_bounce_null_selector) xor %eax, %eax mov %ax, TRAPBOUNCE_cs(%rdx) mov %al, TRAPBOUNCE_flags(%rdx) - ret + RET .section .fixup,"ax" .Lfx13:
xen/arch/x86/x86_64/entry.S +1 -1
@@ -598,7 +598,7 @@ __UNLIKELY_END(create_bounce_frame_bad_bounce_ip) xor %eax, %eax mov %rax, TRAPBOUNCE_eip(%rdx) mov %al, TRAPBOUNCE_flags(%rdx) - ret + RET .pushsection .fixup, "ax", @progbits # Numeric tags below represent the intended overall %rsi adjustment.
xen/arch/x86/xen.lds.S +1 -0
@@ -86,6 +86,7 @@ SECTIONS . = ALIGN(PAGE_SIZE); _stextentry = .; *(.text.entry) + *(.text.entry.*) . = ALIGN(PAGE_SIZE); _etextentry = .;
xen/common/Kconfig +11 -0
@@ -112,6 +112,17 @@ config INDIRECT_THUNK When enabled, indirect branches are implemented using a new construct called "retpoline" that prevents speculation. +config RETURN_THUNK + bool "Out-of-line Returns" + depends on CC_HAS_RETURN_THUNK + default INDIRECT_THUNK + help + Compile Xen with out-of-line returns. + + This allows Xen to mitigate a variety of speculative vulnerabilities + by choosing a hardware-dependent instruction sequence to implement + function returns safely. + config SPECULATIVE_HARDEN_ARRAY bool "Speculative Array Hardening" default y
From: Andrew Cooper <[email protected]>
Subject: x86/spec-ctrl: Synthesise ITS_NO to guests on unaffected hardware
It is easier to express feature word 17 in terms of word 16 + [32, 64) as that's how the layout is given in documentation.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/include/asm/cpufeature.h +1 -0
@@ -164,6 +164,7 @@#define cpu_has_gds_no boot_cpu_has(X86_FEATURE_GDS_NO)#define cpu_has_rfds_no boot_cpu_has(X86_FEATURE_RFDS_NO)#define cpu_has_rfds_clear boot_cpu_has(X86_FEATURE_RFDS_CLEAR) +#define cpu_has_its_no boot_cpu_has(X86_FEATURE_ITS_NO)/* Synthesized. */ #define cpu_has_arch_perfmon boot_cpu_has(X86_FEATURE_ARCH_PERFMON)
xen/arch/x86/spec_ctrl.c +86 -0
98 lines not on this page · raw message
xen/include/public/arch-x86/cpufeatureset.h +2 -1
@@ -334,7 +334,8 @@ XEN_CPUFEATURE(GDS_NO, 16*32+26) /*A No Gather Data Sampling */ XEN_CPUFEATURE(RFDS_NO, 16*32+27) /*A No Register File Data Sampling */XEN_CPUFEATURE (RFDS_CLEAR ,16 *32 +28 )/*!A Register File(s) cleared by VERW */ -/* Intel-defined CPU features, MSR_ARCH_CAPS 0x10a.edx, word 17 */ +/* Intel-defined CPU features, MSR_ARCH_CAPS 0x10a.edx, word 17 (express in terms of word 16) */ +XEN_CPUFEATURE(ITS_NO, 16*32+62) /*!A No Indirect Target Selection */#endif /* XEN_CPUFEATURE */
xen/tools/gen-cpuid.py +1 -1
@@ -51,7 +51,7 @@ def parse_definitions(state): r"\s+/\*([\w!]*) .*$") word_regex = re.compile( - r"^/\* .* word (\d*) \*/$") + r"^/\* .* word (\d*) .*\*/$") last_word = -1 this = sys.modules[__name__]
From: Andrew Cooper <[email protected]>
Subject: x86/alternative: Support replacements when a feature is not present
Use the top bit of a->cpuid to express inverted polarity. This requires stripping the top bit back out when performing the sanity checks.
Despite only being used once, create a replace boolean to express the decision more clearly in _apply_alternatives().
xen/arch/x86/alternative.c +11 -3
@@ -197,6 +197,8 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,uint8_t *repl =ALT_REPL_PTR (a);uint8_t buf[MAX_PATCH_LEN ];unsigned int total_len = a->orig_len + a->pad_len; +unsigned int feat = a->cpuid & ~ALT_FLAG_NOT ; +bool inv = a->cpuid &ALT_FLAG_NOT , replace;if ( a->repl_len > total_len ) {
@@ -214,11 +216,11 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,return -ENOSPC ; } -if ( a->cpuid >=NCAPINTS *32 ) +if ( feat >=NCAPINTS *32 ) {printk (XENLOG_ERR "Alt for %ps, feature %#x outside of featureset range %#x\n" , -ALT_ORIG_PTR (a), a->cpuid,NCAPINTS *32 ); +ALT_ORIG_PTR (a), feat,NCAPINTS *32 );return -ERANGE ; }
@@ -243,8 +245,14 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,continue ; } +/* +* Should a replacement be performed? Most replacements have positive +* polarity, but we support negative polarity too. +*/ + replace =boot_cpu_has (feat) ^ inv; +/* If there is no replacement to make, see about optimising the nops. */ -if ( !boot_cpu_has (a->cpuid) ) +if ( !replace ) {/* Origin site site already touched? Don't nop anything. */ if ( base->priv )
xen/arch/x86/include/asm/alternative.h +8 -1
@@ -1,6 +1,13 @@#ifndef __X86_ALTERNATIVE_H__#define __X86_ALTERNATIVE_H__ +/* +* Common to both C and ASM. Express a replacement when a feature is not +* available. +*/ +#define ALT_FLAG_NOT (1 << 15) +#define ALT_NOT (x) (ALT_FLAG_NOT | (x)) +#ifdef __ASSEMBLY__#include <asm/alternative-asm.h> #else
@@ -11,7 +18,7 @@struct __packed alt_instr {int32_t orig_offset;/* original instruction */ int32_t repl_offset;/* offset to replacement instruction */ -uint16_t cpuid;/* cpuid bit set for replacement */ +uint16_t cpuid;/* cpuid bit set for replacement (top bit is polarity) */ uint8_t orig_len;/* length of original instruction */ uint8_t repl_len;/* length of new instruction */ uint8_t pad_len;/* length of build-time padding */
From: Andrew Cooper <[email protected]>
Subject: x86/guest: Remove use of the Xen hypercall_page
In order to protect against ITS, Xen needs to start using return thunks. Therefore the advice in XSA-466 becomes relevant, and the hypercall_page needs to be removed.
Implement early_hypercall(), with infrastructure to figure out the correct instruction on first use. Use ALTERNATIVE()s to result in inline hypercalls, including the ALT_NOT() form so we only need a single synthetic feature bit.
No overall change.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/guest/xen/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-y += hypercall_page.o +obj-bin-y += hypercall.init.o obj-y += xen.o obj-bin-$(CONFIG_PVH_GUEST) += pvh-boot.init.o
xen/arch/x86/guest/xen/hypercall.S new file +52 -0
52 lines not on this page · raw message
xen/arch/x86/guest/xen/hypercall_page.S deleted +0 -78
78 lines not on this page · raw message
xen/arch/x86/guest/xen/xen.c +44 -4
66 lines not on this page · raw message
xen/arch/x86/include/asm/cpufeatures.h +1 -0
@@ -42,6 +42,7 @@ XEN_CPUFEATURE(XEN_SHSTK, X86_SYNTH(26)) /* Xen uses CET Shadow Stacks * XEN_CPUFEATURE(XEN_IBT, X86_SYNTH(27)) /* Xen uses CET Indirect Branch Tracking */ XEN_CPUFEATURE(IBPB_ENTRY_PV, X86_SYNTH(28)) /* MSR_PRED_CMD used by Xen for PV */ XEN_CPUFEATURE(IBPB_ENTRY_HVM, X86_SYNTH(29)) /* MSR_PRED_CMD used by Xen for HVM */ +XEN_CPUFEATURE (USE_VMCALL ,X86_SYNTH (30 ))/* Use VMCALL instead of VMMCALL */ /* Bug words follow the synthetic words. */#define X86_NR_BUG 1
xen/arch/x86/include/asm/guest/xen-hcall.h +16 -8
@@ -30,9 +30,11 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__) ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)) \ : "memory" ); \ (type)res; \
@@ -42,10 +44,12 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__) \ ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)), "2" ((long)(a2)) \ : "memory" ); \ (type)res; \
@@ -55,10 +59,12 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__), "=d" (tmp__) \ ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)), "2" ((long)(a2)), "3" ((long)(a3)) \ : "memory" ); \ (type)res; \
@@ -69,10 +75,12 @@long res, tmp__; \ register long _a4 asm ("r10") = ((long)(a4)); \ asm volatile ( \ -"call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__), "=d" (tmp__), \ "=&r" (tmp__) ASM_CALL_CONSTRAINT \ - : [offset]"i" (hcall *32 ), \ + :"0" (hcall), \"1" ((long )(a1)),"2" ((long )(a2)),"3" ((long )(a3)), \"4" (_a4) \ : "memory" ); \
From: Jan Beulich <[email protected]>
Subject: x86/thunk: (Mis)align __x86_indirect_thunk_* to mitigate ITS
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
Arrange for __x86_indirect_thunk_* to always be in the second half.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/indirect-thunk.S +10 -0
@@ -35,6 +35,16 @@ .macro GEN_INDIRECT_THUNK reg:req .section .text.__x86_indirect_thunk_\reg, "ax", @progbits + /* + * The Indirect Target Selection speculative vulnerability means that + * indirect branches (including RETs) are unsafe when in the first + * half of a cacheline. Arrange for them to be in the second half. + * + * Align to 64, then skip 32. + */ + .balign 64 + .fill 32, 1, 0xcc + ENTRY(__x86_indirect_thunk_\reg) ALTERNATIVE_2 __stringify(IND_THUNK_RETPOLINE \reg), \ __stringify(IND_THUNK_LFENCE \reg), X86_FEATURE_IND_THUNK_LFENCE, \
From: Andrew Cooper <[email protected]>
Subject: x86/thunk: (Mis)align the RETs in clear_bhb_loops() to mitigate ITS
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
clear_bhb_loops() has a precise layout of branches. The alignment for performance cause the RETs to always be in an unsafe position, and converting those to return thunks changes the branching pattern. While such a conversion is believed to be safe, clear_bhb_loops() is also a performance-relevant fastpath, so (mis)align the RETs to be in a safe position.
No functional change.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/bhb-thunk.S +12 -5
@@ -52,7 +52,12 @@ ENTRY(clear_bhb_tsx) * ret * * The CALL/RETs are necessary to prevent the Loop Stream Detector from - * interfering. The alignment is for performance and not safety. + * interfering. + * + * The .balign's are for performance, but they cause the RETs to be in unsafe + * positions with respect to Indirect Target Selection. The .skips are to + * move the RETs into ITS-safe positions, rather than using the slowpath + * through __x86_return_thunk. * * The "short" sequence (5 and 5) is for CPUs prior to Alder Lake / Sapphire * Rapids (i.e. Cores prior to Golden Cove and/or Gracemont).
@@ -68,12 +73,14 @@ ENTRY(clear_bhb_loops) jmp 5f int3 - .align 64 + .balign 64 + .skip 32 - (.Lr1 - 1f), 0xcc 1: call 2f - ret +.Lr1: ret int3 - .align 64 + .balign 64 + .skip 32 - 18 /* (.Lr2 - 2f) but Clang IAS doesn't like this */, 0xcc 2: ALTERNATIVE "mov $5, %eax", "mov $7, %eax", X86_SPEC_BHB_LOOPS_LONG 3: jmp 4f
@@ -85,7 +92,7 @@ ENTRY(clear_bhb_loops) sub $1, %ecx jnz 1b - ret +.Lr2: ret 5: /* * The Intel sequence has an LFENCE here. The purpose is to ensure
From: Andrew Cooper <[email protected]>
Subject: x86/stubs: Introduce place_ret() to abstract away raw 0xc3's
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline. This means it's not safe for logic using the stubs to write raw 0xc3's.
Introduce place_ret() which, for now, writes a raw 0xc3 but will contain additional logic when return thunks are in use.
stub_selftest() doesn't strictly need to be converted as they only run on boot, but doing so gets us a partial test of place_ret() too.
No functional change.
This is part of XSA-469 / CVE-2024-28956
tools/tests/x86_emulator/x86-emulate.h +6 -0
@@ -68,6 +68,12 @@#define is_canonical_address (x) (((int64_t)(x) >> 47) == ((int64_t)(x) >> 63)) +static inline void *place_ret (void *ptr) +{ + *(uint8_t *)ptr =0xc3 ; +return ptr +1 ; +} +extern uint32_t mxcsr_mask;extern struct cpu_policy cp;
xen/arch/x86/Makefile +2 -4
@@ -11,9 +11,7 @@ obj-$(CONFIG_PV) += pv/ obj-y += x86_64/ obj-y += x86_emulate/ -alternative-y := alternative.init.o -alternative-$(CONFIG_LIVEPATCH) := -obj-bin-y += $(alternative-y) +obj-y += alternative.o obj-y += apic.o obj-y += bhb-thunk.o obj-y += bitops.o
@@ -42,7 +40,7 @@ obj-y += hypercall.o obj-y += i387.o obj-y += i8259.o obj-y += io_apic.o -obj-$(CONFIG_LIVEPATCH) += alternative.o livepatch.o +obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-y += msi.o obj-y += msr.o obj-$(CONFIG_INDIRECT_THUNK) += indirect-thunk.o
xen/arch/x86/alternative.c +14 -0
@@ -137,6 +137,20 @@ void init_or_livepatch add_nops(void *insns, unsigned int len) } } +/* +* Place a return at @ptr. @ptr must be in the writable alias of a stub. +* +* Returns the next position to write into the stub. +*/ +void *place_ret (void *ptr) +{ +uint8_t *p = ptr; + + *p++ =0xc3 ; + +return p; +} +/* * text_poke - Update instructions on a live kernel or non-executed code. * @addr: address to modify
xen/arch/x86/extable.c +6 -5
@@ -135,20 +135,20 @@ search_exception_table(const struct cpu_user_regs *regs, unsigned long *stub_ra) int __init cf_check stub_selftest(void) {static const struct { -uint8_t opc[8 ]; +uint8_t opc[7 ];uint64_t rax;union stub_exception_token res; } tests[]__initconst = { #define endbr64 0xf3, 0x0f, 0x1e, 0xfa - { .opc = { endbr64,0x0f ,0xb9 ,0xc3 ,0xc3 },/* ud1 */ + { .opc = { endbr64,0x0f ,0xb9 ,0x90 },/* ud1 */ .res.fields.trapnr =X86_EXC_UD }, - { .opc = { endbr64,0x90 ,0x02 ,0x00 ,0xc3 },/* nop; add (%rax),%al */ + { .opc = { endbr64,0x90 ,0x02 ,0x00 },/* nop; add (%rax),%al */ .rax =0x0123456789abcdef , .res.fields.trapnr =X86_EXC_GP }, - { .opc = { endbr64,0x02 ,0x04 ,0x04 ,0xc3 },/* add (%rsp,%rax),%al */ + { .opc = { endbr64,0x02 ,0x04 ,0x04 },/* add (%rsp,%rax),%al */ .rax =0xfedcba9876543210 , .res.fields.trapnr =X86_EXC_SS }, - { .opc = { endbr64,0xcc ,0xc3 ,0xc3 ,0xc3 },/* int3 */ + { .opc = { endbr64,0xcc ,0x90 ,0x90 },/* int3 */ .res.fields.trapnr =X86_EXC_BP }, #undef endbr64 };
@@ -167,6 +167,7 @@ int __init cf_check stub_selftest(void)memset (ptr,0xcc ,STUB_BUF_SIZE /2 );memcpy (ptr, tests[i].opc,ARRAY_SIZE (tests[i].opc)); +place_ret (ptr +ARRAY_SIZE (tests[i].opc));unmap_domain_page (ptr); asm volatile ( "INDIRECT_CALL %[stb]\n"
xen/arch/x86/include/asm/alternative.h +2 -0
@@ -30,6 +30,8 @@ struct __packed alt_instr {#define ALT_REPL_PTR (a) __ALT_PTR(a, repl_offset)extern void add_nops (void *insns,unsigned int len); +void *place_ret (void *ptr); +/* Similar to alternative_instructions except it can be run with IRQs enabled. */ extern int apply_alternatives (struct alt_instr *start,struct alt_instr *end);extern void alternative_instructions (void );
xen/arch/x86/pv/emul-priv-op.c +3 -2
@@ -76,7 +76,6 @@ static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode,0x41 ,0x5c ,/* pop %r12 */ 0x5d ,/* pop %rbp */ 0x5b, /* pop %rbx */ - 0xc3, /* ret */ };const struct stubs *this_stubs = &this_cpu (stubs);
@@ -126,11 +125,13 @@ static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode,APPEND_CALL (save_guest_gprs);APPEND_BUFF (epilogue); + p =place_ret (p);/* Build-time best effort attempt to catch problems. */ BUILD_BUG_ON (STUB_BUF_SIZE /2 < (sizeof (prologue) +sizeof (epilogue) +10 /* 2x call */ + -MAX (3 /* default stub */ ,IOEMUL_QUIRK_STUB_BYTES ))); +MAX (3 /* default stub */ ,IOEMUL_QUIRK_STUB_BYTES ) + +1 /* ret */ ));/* Runtime confirmation that we haven't clobbered an adjacent stub. */ BUG_ON (STUB_BUF_SIZE /2 < (p - ctxt->io_emul_stub));
xen/arch/x86/x86_emulate/fpu.c +12 -6
@@ -32,36 +32,42 @@ static inline bool fpu_check_write(void)#define emulate_fpu_insn_memdst (opc, ext, arg) \ do { \ + void *_p = get_stub(stub); \/* ModRM: mod=0, reg=ext, rm=0, i.e. a (%rax) operand */ \ *insn_bytes =2 ; \ -memcpy (get_stub (stub), \ - ((uint8_t []){ opc, ((ext) &7 ) <<3 ,0xc3 }),3 ); \ +memcpy (_p, ((uint8_t []){ opc, ((ext) &7 ) <<3 }),2 ); _p +=2 ; \ +place_ret (_p); \ invoke_stub("", "", "+m" (arg) : "a" (&(arg))); \put_stub (stub); \ } while (0)#define emulate_fpu_insn_memsrc (opc, ext, arg) \ do { \ + void *_p = get_stub(stub); \/* ModRM: mod=0, reg=ext, rm=0, i.e. a (%rax) operand */ \ -memcpy (get_stub (stub), \ - ((uint8_t []){ opc, ((ext) &7 ) <<3 ,0xc3 }),3 ); \ +memcpy (_p, ((uint8_t []){ opc, ((ext) &7 ) <<3 }),2 ); _p +=2 ; \ +place_ret (_p); \ invoke_stub("", "", "=m" (dummy) : "m" (arg), "a" (&(arg))); \put_stub (stub); \ } while (0) #define emulate_fpu_insn_stub(bytes...) \do { \ +void *_p =get_stub (stub); \unsigned int nr_ =sizeof ((uint8_t []){ bytes }); \ -memcpy (get_stub (stub), ((uint8_t []){ bytes,0xc3 }), nr_ +1 ); \ +memcpy (_p, ((uint8_t []){ bytes }), nr_); _p += nr_; \ +place_ret (_p); \ invoke_stub("", "", "=m" (dummy) : "i" (0)); \put_stub (stub); \ } while (0) #define emulate_fpu_insn_stub_eflags(bytes...) \ do { \ + void *_p = get_stub(stub); \ unsigned int nr_ = sizeof((uint8_t[]){ bytes }); \ unsigned long tmp_; \ - memcpy(get_stub(stub), ((uint8_t[]){ bytes, 0xc3 }), nr_ + 1); \ + memcpy(_p, ((uint8_t[]){ bytes }), nr_); _p += nr_; \ + place_ret(_p); \ invoke_stub(_PRE_EFLAGS("[eflags]", "[mask]", "[tmp]"), \ _POST_EFLAGS("[eflags]", "[mask]", "[tmp]"), \ [eflags] "+g" (regs->eflags), [tmp] "=&r" (tmp_) \
xen/arch/x86/x86_emulate/x86_emulate.c +32 -32
254 lines not on this page · raw message
From: Jan Beulich <[email protected]>
Subject: x86/thunk: Build Xen with Return Thunks
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
In order to mitigate this, build with return thunks and arrange for __x86_return_thunk to be (mis)aligned in the same manner as __x86_indirect_thunk_* so the RET instruction is placed in a safe location.
place_ret() needs to conditionally emit JMP __x86_return_thunk instead of RET.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/Kconfig +5 -0
@@ -35,9 +35,14 @@ config ARCH_DEFCONFIG default "arch/x86/configs/x86_64_defconfig" config CC_HAS_INDIRECT_THUNK + # GCC >= 8 or Clang >= 6 def_bool $(cc-option,-mindirect-branch-register) || \ $(cc-option,-mretpoline-external-thunk) +config CC_HAS_RETURN_THUNK + # GCC >= 8 or Clang >= 15 + def_bool $(cc-option,-mfunction-return=thunk-extern) + config HAS_AS_CET_SS # binutils >= 2.29 or LLVM >= 6 def_bool $(as-instr,wrssq %rax$(comma)0;setssbsy)
xen/arch/x86/Makefile +1 -0
@@ -44,6 +44,7 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-y += msi.o obj-y += msr.o obj-$(CONFIG_INDIRECT_THUNK) += indirect-thunk.o +obj-$(CONFIG_RETURN_THUNK) += indirect-thunk.o obj-$(CONFIG_PV) += ioport_emulate.o obj-y += irq.o obj-$(CONFIG_KEXEC) += machine_kexec.o
xen/arch/x86/acpi/wakeup_prot.S +1 -1
@@ -133,7 +133,7 @@ ENTRY(s3_resume) pop %r12 pop %rbx pop %rbp - ret + RET .data .align 16
xen/arch/x86/alternative.c +30 -1
@@ -137,16 +137,45 @@ void init_or_livepatch add_nops(void *insns, unsigned int len) } } +void nocall __x86_return_thunk(void); +/* * Place a return at @ptr. @ptr must be in the writable alias of a stub. * +* When CONFIG_RETURN_THUNK is active, this may be a JMP __x86_return_thunk +* instead, depending on the safety of @ptr with respect to Indirect Target +* Selection. +* * Returns the next position to write into the stub. */ void *place_ret (void *ptr) { +unsigned long addr = (unsigned long )ptr;uint8_t *p = ptr; - *p++ =0xc3 ; +/* +* When Return Thunks are used, if a RET would be unsafe at this location +* with respect to Indirect Target Selection (i.e. if addr is in the first +* half of a cacheline), insert a JMP __x86_return_thunk instead. +* +* The displacement needs to be relative to the executable alias of the +* stub, not to @ptr which is the writeable alias. +*/ +if (IS_ENABLED (CONFIG_RETURN_THUNK ) && !(addr &0x20 ) ) + { +long stub_va = (this_cpu (stubs.addr) &PAGE_MASK ) + (addr & ~PAGE_MASK ); +long disp = (long )__x86_return_thunk - (stub_va +5 ); + +BUG_ON ((int32_t )disp != disp); + + *p++ =0xe9 ; + *(int32_t *)p = disp; + p +=4 ; + } +else + { + *p++ =0xc3 ; + }return p; }
xen/arch/x86/arch.mk +3 -0
@@ -46,6 +46,9 @@ CFLAGS-$(CONFIG_CC_IS_GCC) += -fno-jump-tables CFLAGS-$(CONFIG_CC_IS_CLANG) += -mretpoline-external-thunk endif +# Compile with return thunk support if selected. +CFLAGS-$(CONFIG_RETURN_THUNK) += -mfunction-return=thunk-extern + # Disable the addition of a .note.gnu.property section to object files when # livepatch support is enabled. The contents of that section can change # depending on the instructions used, and livepatch-build-tools doesn't know
xen/arch/x86/bhb-thunk.S +1 -1
@@ -23,7 +23,7 @@ ENTRY(clear_bhb_tsx) 0: .byte 0xc6, 0xf8, 0 /* xabort $0 */ int3 1: - ret + RET .size clear_bhb_tsx, . - clear_bhb_tsx .type clear_bhb_tsx, @function
xen/arch/x86/clear_page.S +2 -1
@@ -1,5 +1,6 @@ .file __FILE__ +#include <asm/asm_defns.h> #include <asm/page.h> ENTRY(clear_page_sse2)
@@ -15,7 +16,7 @@ ENTRY(clear_page_sse2) jnz 0b sfence - ret + RET .type clear_page_sse2, @function .size clear_page_sse2, . - clear_page_sse2
xen/arch/x86/copy_page.S +2 -1
@@ -1,5 +1,6 @@ .file __FILE__ +#include <asm/asm_defns.h> #include <asm/page.h> #define src_reg %rsi
@@ -40,7 +41,7 @@ ENTRY(copy_page_sse2) movnti tmp4_reg, 3*WORD_SIZE(dst_reg) sfence - ret + RET .type copy_page_sse2, @function .size copy_page_sse2, . - copy_page_sse2
xen/arch/x86/efi/check.c +3 -0
@@ -3,6 +3,9 @@ int __attribute__((__ms_abi__)) test(int i)return i; } +/* In case -mfunction-return is in use. */ +void __x86_return_thunk (void ) {}; +/* * Populate an array with "addresses" of relocatable and absolute values. * This is to probe ld for (a) emitting base relocations at all and (b) not
xen/arch/x86/include/asm/asm-defns.h +6 -0
@@ -58,6 +58,12 @@ .endif .endm +#ifdef CONFIG_RETURN_THUNK +# define RET jmp __x86_return_thunk +#else +# define RET ret +#endif +#ifdef CONFIG_XEN_IBT # define ENDBR64 endbr64#else
xen/arch/x86/indirect-thunk.S +27 -0
@@ -11,6 +11,9 @@ #include <asm/asm_defns.h> + +#ifdef CONFIG_INDIRECT_THUNK + .macro IND_THUNK_RETPOLINE reg:req call 1f int3
@@ -60,3 +63,27 @@ ENTRY(__x86_indirect_thunk_\reg) .irp reg, ax, cx, dx, bx, bp, si, di, 8, 9, 10, 11, 12, 13, 14, 15 GEN_INDIRECT_THUNK reg=r\reg .endr + +#endif /* CONFIG_INDIRECT_THUNK */ + +#ifdef CONFIG_RETURN_THUNK + .section .text.entry.__x86_return_thunk, "ax", @progbits + + /* + * The Indirect Target Selection speculative vulnerability means that + * indirect branches (including RETs) are unsafe when in the first + * half of a cacheline. Arrange for them to be in the second half. + * + * Align to 64, then skip 32. + */ + .balign 64 + .fill 32, 1, 0xcc + +ENTRY(__x86_return_thunk) + ret + int3 /* Halt straight-line speculation */ + + .size __x86_return_thunk, . - __x86_return_thunk + .type __x86_return_thunk, @function + +#endif /* CONFIG_RETURN_THUNK */
xen/arch/x86/pv/emul-priv-op.c +1 -1
@@ -131,7 +131,7 @@ static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode,BUILD_BUG_ON (STUB_BUF_SIZE /2 < (sizeof (prologue) +sizeof (epilogue) +10 /* 2x call */ +MAX (3 /* default stub */ ,IOEMUL_QUIRK_STUB_BYTES ) + -1 /* ret */ )); + (IS_ENABLED (CONFIG_RETURN_THUNK ) ?5 :1 )/* ret */ ));/* Runtime confirmation that we haven't clobbered an adjacent stub. */ BUG_ON (STUB_BUF_SIZE /2 < (p - ctxt->io_emul_stub));
xen/arch/x86/pv/gpr_switch.S +2 -3
@@ -26,12 +26,11 @@ ENTRY(load_guest_gprs) movq UREGS_r15(%rdi), %r15 movq UREGS_rcx(%rdi), %rcx movq UREGS_rdi(%rdi), %rdi - ret + RET .size load_guest_gprs, . - load_guest_gprs .type load_guest_gprs, STT_FUNC - /* Save guest GPRs. Parameter on the stack above the return address. */ ENTRY(save_guest_gprs) pushq %rdi
@@ -51,7 +50,7 @@ ENTRY(save_guest_gprs) movq %rbx, UREGS_rbx(%rdi) movq %rdx, UREGS_rdx(%rdi) movq %rcx, UREGS_rcx(%rdi) - ret + RET .size save_guest_gprs, . - save_guest_gprs .type save_guest_gprs, STT_FUNC
xen/arch/x86/spec_ctrl.c +3 -0
@@ -569,6 +569,9 @@ static void __init print_details(enum ind_thunk thunk)#ifdef CONFIG_INDIRECT_THUNK " INDIRECT_THUNK"#endif +#ifdef CONFIG_RETURN_THUNK + " RETURN_THUNK" +#endif #ifdef CONFIG_SHADOW_PAGING " SHADOW_PAGING"#endif
xen/arch/x86/x86_64/compat/entry.S +3 -3
@@ -183,7 +183,7 @@ ENTRY(cr4_pv32_restore) mov %rax, %cr4 mov %rax, (%rdx) pop %rdx - ret + RET 0: #ifndef NDEBUG /* Check that _all_ of the bits intended to be set actually are. */
@@ -202,7 +202,7 @@ ENTRY(cr4_pv32_restore) #endif pop %rdx xor %eax, %eax - ret + RET ENTRY(compat_syscall) /* Fix up reported %cs/%ss for compat domains. */
@@ -329,7 +329,7 @@ __UNLIKELY_END(compat_bounce_null_selector) xor %eax, %eax mov %ax, TRAPBOUNCE_cs(%rdx) mov %al, TRAPBOUNCE_flags(%rdx) - ret + RET .section .fixup,"ax" .Lfx13:
xen/arch/x86/x86_64/entry.S +1 -1
@@ -598,7 +598,7 @@ __UNLIKELY_END(create_bounce_frame_bad_bounce_ip) xor %eax, %eax mov %rax, TRAPBOUNCE_eip(%rdx) mov %al, TRAPBOUNCE_flags(%rdx) - ret + RET .pushsection .fixup, "ax", @progbits # Numeric tags below represent the intended overall %rsi adjustment.
xen/arch/x86/xen.lds.S +1 -0
@@ -86,6 +86,7 @@ SECTIONS . = ALIGN(PAGE_SIZE); _stextentry = .; *(.text.entry) + *(.text.entry.*) . = ALIGN(PAGE_SIZE); _etextentry = .;
xen/common/Kconfig +11 -0
@@ -127,6 +127,17 @@ config INDIRECT_THUNK When enabled, indirect branches are implemented using a new construct called "retpoline" that prevents speculation. +config RETURN_THUNK + bool "Out-of-line Returns" + depends on CC_HAS_RETURN_THUNK + default INDIRECT_THUNK + help + Compile Xen with out-of-line returns. + + This allows Xen to mitigate a variety of speculative vulnerabilities + by choosing a hardware-dependent instruction sequence to implement + function returns safely. + config SPECULATIVE_HARDEN_ARRAY bool "Speculative Array Hardening" default y
From: Andrew Cooper <[email protected]>
Subject: x86/spec-ctrl: Synthesise ITS_NO to guests on unaffected hardware
It is easier to express feature word 17 in terms of word 16 + [32, 64) as that's how the layout is given in documentation.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/include/asm/cpufeature.h +1 -0
@@ -211,6 +211,7 @@ static inline bool boot_cpu_has(unsigned int feat)#define cpu_has_gds_no boot_cpu_has(X86_FEATURE_GDS_NO)#define cpu_has_rfds_no boot_cpu_has(X86_FEATURE_RFDS_NO)#define cpu_has_rfds_clear boot_cpu_has(X86_FEATURE_RFDS_CLEAR) +#define cpu_has_its_no boot_cpu_has(X86_FEATURE_ITS_NO)/* Synthesized. */ #define cpu_has_arch_perfmon boot_cpu_has(X86_FEATURE_ARCH_PERFMON)
xen/arch/x86/spec_ctrl.c +86 -0
98 lines not on this page · raw message
xen/include/public/arch-x86/cpufeatureset.h +2 -1
@@ -344,7 +344,8 @@ XEN_CPUFEATURE(GDS_NO, 16*32+26) /*A No Gather Data Sampling */ XEN_CPUFEATURE(RFDS_NO, 16*32+27) /*A No Register File Data Sampling */XEN_CPUFEATURE (RFDS_CLEAR ,16 *32 +28 )/*!A Register File(s) cleared by VERW */ -/* Intel-defined CPU features, MSR_ARCH_CAPS 0x10a.edx, word 17 */ +/* Intel-defined CPU features, MSR_ARCH_CAPS 0x10a.edx, word 17 (express in terms of word 16) */ +XEN_CPUFEATURE(ITS_NO, 16*32+62) /*!A No Indirect Target Selection */#endif /* XEN_CPUFEATURE */
xen/tools/gen-cpuid.py +1 -1
@@ -51,7 +51,7 @@ def parse_definitions(state): r"\s+/\*([\w!]*) .*$") word_regex = re.compile( - r"^/\* .* word (\d*) \*/$") + r"^/\* .* word (\d*) .*\*/$") last_word = -1 this = sys.modules[__name__]
From: Andrew Cooper <[email protected]>
Subject: x86/alternative: Support replacements when a feature is not present
Use the top bit of a->cpuid to express inverted polarity. This requires stripping the top bit back out when performing the sanity checks.
Despite only being used once, create a replace boolean to express the decision more clearly in _apply_alternatives().
xen/arch/x86/alternative.c +11 -3
@@ -197,6 +197,8 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,uint8_t *repl =ALT_REPL_PTR (a);uint8_t buf[MAX_PATCH_LEN ];unsigned int total_len = a->orig_len + a->pad_len; +unsigned int feat = a->cpuid & ~ALT_FLAG_NOT ; +bool inv = a->cpuid &ALT_FLAG_NOT , replace;if ( a->repl_len > total_len ) {
@@ -214,11 +216,11 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,return -ENOSPC ; } -if ( a->cpuid >=NCAPINTS *32 ) +if ( feat >=NCAPINTS *32 ) {printk (XENLOG_ERR "Alt for %ps, feature %#x outside of featureset range %#x\n" , -ALT_ORIG_PTR (a), a->cpuid,NCAPINTS *32 ); +ALT_ORIG_PTR (a), feat,NCAPINTS *32 );return -ERANGE ; }
@@ -243,8 +245,14 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,continue ; } +/* +* Should a replacement be performed? Most replacements have positive +* polarity, but we support negative polarity too. +*/ + replace =boot_cpu_has (feat) ^ inv; +/* If there is no replacement to make, see about optimising the nops. */ -if ( !boot_cpu_has (a->cpuid) ) +if ( !replace ) {/* Origin site site already touched? Don't nop anything. */ if ( base->priv )
xen/arch/x86/include/asm/alternative.h +8 -1
@@ -1,6 +1,13 @@#ifndef __X86_ALTERNATIVE_H__#define __X86_ALTERNATIVE_H__ +/* +* Common to both C and ASM. Express a replacement when a feature is not +* available. +*/ +#define ALT_FLAG_NOT (1 << 15) +#define ALT_NOT (x) (ALT_FLAG_NOT | (x)) +#ifdef __ASSEMBLY__#include <asm/alternative-asm.h> #else
@@ -11,7 +18,7 @@struct __packed alt_instr {int32_t orig_offset;/* original instruction */ int32_t repl_offset;/* offset to replacement instruction */ -uint16_t cpuid;/* cpuid bit set for replacement */ +uint16_t cpuid;/* cpuid bit set for replacement (top bit is polarity) */ uint8_t orig_len;/* length of original instruction */ uint8_t repl_len;/* length of new instruction */ uint8_t pad_len;/* length of build-time padding */
From: Andrew Cooper <[email protected]>
Subject: x86/guest: Remove use of the Xen hypercall_page
In order to protect against ITS, Xen needs to start using return thunks. Therefore the advice in XSA-466 becomes relevant, and the hypercall_page needs to be removed.
Implement early_hypercall(), with infrastructure to figure out the correct instruction on first use. Use ALTERNATIVE()s to result in inline hypercalls, including the ALT_NOT() form so we only need a single synthetic feature bit.
No overall change.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/guest/xen/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-y += hypercall_page.o +obj-bin-y += hypercall.init.o obj-y += xen.o obj-bin-$(CONFIG_PVH_GUEST) += pvh-boot.init.o
xen/arch/x86/guest/xen/hypercall.S new file +50 -0
50 lines not on this page · raw message
xen/arch/x86/guest/xen/hypercall_page.S deleted +0 -76
76 lines not on this page · raw message
xen/arch/x86/guest/xen/xen.c +44 -4
66 lines not on this page · raw message
xen/arch/x86/include/asm/cpufeatures.h +1 -0
@@ -42,6 +42,7 @@ XEN_CPUFEATURE(XEN_SHSTK, X86_SYNTH(26)) /* Xen uses CET Shadow Stacks * XEN_CPUFEATURE(XEN_IBT, X86_SYNTH(27)) /* Xen uses CET Indirect Branch Tracking */ XEN_CPUFEATURE(IBPB_ENTRY_PV, X86_SYNTH(28)) /* MSR_PRED_CMD used by Xen for PV */ XEN_CPUFEATURE(IBPB_ENTRY_HVM, X86_SYNTH(29)) /* MSR_PRED_CMD used by Xen for HVM */ +XEN_CPUFEATURE (USE_VMCALL ,X86_SYNTH (30 ))/* Use VMCALL instead of VMMCALL */ /* Bug words follow the synthetic words. */#define X86_NR_BUG 1
xen/arch/x86/include/asm/guest/xen-hcall.h +16 -8
@@ -30,9 +30,11 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__) ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)) \ : "memory" ); \ (type)res; \
@@ -42,10 +44,12 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__) \ ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)), "2" ((long)(a2)) \ : "memory" ); \ (type)res; \
@@ -55,10 +59,12 @@ ({ \ long res, tmp__; \ asm volatile ( \ - "call hypercall_page + %c[offset]" \ + ALTERNATIVE_2("call early_hypercall", \ + "vmmcall", ALT_NOT(X86_FEATURE_USE_VMCALL), \ + "vmcall", X86_FEATURE_USE_VMCALL) \ : "=a" (res), "=D" (tmp__), "=S" (tmp__), "=d" (tmp__) \ ASM_CALL_CONSTRAINT \ - : [offset] "i" (hcall * 32), \ + : "0" (hcall), \ "1" ((long)(a1)), "2" ((long)(a2)), "3" ((long)(a3)) \ : "memory" ); \
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: x86/thunk: (Mis)align __x86_indirect_thunk_* to mitigate ITS
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
Arrange for __x86_indirect_thunk_* to always be in the second half.
This is part of XSA-469 / CVE-2024-28956
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/thunk: (Mis)align the RETs in clear_bhb_loops() to mitigate ITS
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
clear_bhb_loops() has a precise layout of branches. The alignment for performance cause the RETs to always be in an unsafe position, and converting those to return thunks changes the branching pattern. While such a conversion is believed to be safe, clear_bhb_loops() is also a performance-relevant fastpath, so (mis)align the RETs to be in a safe position.
No functional change.
This is part of XSA-469 / CVE-2024-28956
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/stubs: Introduce place_ret() to abstract away raw 0xc3's
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline. This means it's not safe for logic using the stubs to write raw 0xc3's.
Introduce place_ret() which, for now, writes a raw 0xc3 but will contain additional logic when return thunks are in use.
stub_selftest() doesn't strictly need to be converted as they only run on boot, but doing so gets us a partial test of place_ret() too.
No functional change.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/x86_emulate/x86_emulate.c +32 -32
64 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: x86/thunk: Build Xen with Return Thunks
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
In order to mitigate this, build with return thunks and arrange for __x86_return_thunk to be (mis)aligned in the same manner as __x86_indirect_thunk_* so the RET instruction is placed in a safe location.
place_ret() needs to conditionally emit JMP __x86_return_thunk instead of RET.
This is part of XSA-469 / CVE-2024-28956
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/spec-ctrl: Synthesise ITS_NO to guests on unaffected hardware
It is easier to express feature word 17 in terms of word 16 + [32, 64) as that's how the layout is given in documentation.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/spec_ctrl.c +86 -0
86 lines not on this page · raw message
xen/include/public/arch-x86/cpufeatureset.h +2 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/alternative: Support replacements when a feature is not present
Use the top bit of a->cpuid to express inverted polarity. This requires stripping the top bit back out when performing the sanity checks.
Despite only being used once, create a replace boolean to express the decision more clearly in _apply_alternatives().
xen/arch/x86/include/asm/alternative-asm.h +1 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/guest: Remove use of the Xen hypercall_page
In order to protect against ITS, Xen needs to start using return thunks. Therefore the advice in XSA-466 becomes relevant, and the hypercall_page needs to be removed.
Implement early_hypercall(), with infrastructure to figure out the correct instruction on first use. Use ALTERNATIVE()s to result in inline hypercalls, including the ALT_NOT() form so we only need a single synthetic feature bit.
No overall change.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/guest/xen/hypercall.S new file +50 -0
50 lines not on this page · raw message
xen/arch/x86/guest/xen/hypercall_page.S deleted +0 -76
76 lines not on this page · raw message
xen/arch/x86/guest/xen/xen.c +44 -4
48 lines not on this page · raw message
xen/arch/x86/include/asm/guest/xen-hcall.h +16 -8
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: x86/thunk: (Mis)align __x86_indirect_thunk_* to mitigate ITS
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
Arrange for __x86_indirect_thunk_* to always be in the second half.
This is part of XSA-469 / CVE-2024-28956
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/thunk: (Mis)align the RETs in clear_bhb_loops() to mitigate ITS
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
clear_bhb_loops() has a precise layout of branches. The alignment for performance cause the RETs to always be in an unsafe position, and converting those to return thunks changes the branching pattern. While such a conversion is believed to be safe, clear_bhb_loops() is also a performance-relevant fastpath, so (mis)align the RETs to be in a safe position.
No functional change.
This is part of XSA-469 / CVE-2024-28956
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/stubs: Introduce place_ret() to abstract away raw 0xc3's
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline. This means it's not safe for logic using the stubs to write raw 0xc3's.
Introduce place_ret() which, for now, writes a raw 0xc3 but will contain additional logic when return thunks are in use.
stub_selftest() doesn't strictly need to be converted as they only run on boot, but doing so gets us a partial test of place_ret() too.
No functional change.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/x86_emulate/x86_emulate.c +32 -32
64 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: x86/thunk: Build Xen with Return Thunks
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
In order to mitigate this, build with return thunks and arrange for __x86_return_thunk to be (mis)aligned in the same manner as __x86_indirect_thunk_* so the RET instruction is placed in a safe location.
place_ret() needs to conditionally emit JMP __x86_return_thunk instead of RET.
This is part of XSA-469 / CVE-2024-28956
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/spec-ctrl: Synthesise ITS_NO to guests on unaffected hardware
It is easier to express feature word 17 in terms of word 16 + [32, 64) as that's how the layout is given in documentation.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/spec_ctrl.c +86 -0
86 lines not on this page · raw message
xen/include/public/arch-x86/cpufeatureset.h +2 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/thunk: (Mis)align the RETs in clear_bhb_loops() to mitigate ITS
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
clear_bhb_loops() has a precise layout of branches. The alignment for performance cause the RETs to always be in an unsafe position, and converting those to return thunks changes the branching pattern. While such a conversion is believed to be safe, clear_bhb_loops() is also a performance-relevant fastpath, so (mis)align the RETs to be in a safe position.
No functional change.
This is part of XSA-469 / CVE-2024-28956
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/stubs: Introduce place_ret() to abstract away raw 0xc3's
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline. This means it's not safe for logic using the stubs to write raw 0xc3's.
Introduce place_ret() which, for now, writes a raw 0xc3 but will contain additional logic when return thunks are in use.
stub_selftest() doesn't strictly need to be converted as they only run on boot, but doing so gets us a partial test of place_ret() too.
No functional change.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/x86_emulate/x86_emulate.c +32 -32
64 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: x86/thunk: Build Xen with Return Thunks
The Indirect Target Selection speculative vulnerability means that indirect branches (including RETs) are unsafe when in the first half of a cacheline.
In order to mitigate this, build with return thunks and arrange for __x86_return_thunk to be (mis)aligned in the same manner as __x86_indirect_thunk_* so the RET instruction is placed in a safe location.
place_ret() needs to conditionally emit JMP __x86_return_thunk instead of RET.
This is part of XSA-469 / CVE-2024-28956
The rest of this diff is not shown. View the raw message.
From: Andrew Cooper <[email protected]>
Subject: x86/spec-ctrl: Synthesise ITS_NO to guests on unaffected hardware
It is easier to express feature word 17 in terms of word 16 + [32, 64) as that's how the layout is given in documentation.
This is part of XSA-469 / CVE-2024-28956
xen/arch/x86/spec_ctrl.c +86 -0
86 lines not on this page · raw message
xen/include/public/arch-x86/cpufeatureset.h +2 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.