x86/sev: Skip DR7 write during kexec when it would trigger an unserviceable #VC
PATCH v2 1/1 x86/sev: Skip DR7 write during kexec when it would trigger an unserviceable #VC
From: Ashish Kalra <[email protected]>
machine_kexec() calls hw_breakpoint_disable(), which writes DR7. On SEV-ES/SNP guests that do not have DebugSwap enabled, DR7 accesses are intercepted and delivered to the guest as a #VC exception, which must be serviced through the GHCB.
By the time machine_kexec() runs, snp_kexec_finish() (called from native_machine_shutdown()) has already converted the per-CPU GHCBs and the boot GHCB back to private and set boot_ghcb to NULL, tearing down the GHCB infrastructure. The DR7 write therefore raises a #VC that dereferences a NULL GHCB pointer, causing a page fault and the kexec to fail.
The NULL boot_ghcb pointer is a result of commit 3645eb7e3915 ("x86/fred:
Fix early boot failures on SEV-ES/SNP guests"): that change makes __sev_get_ghcb() return the now-NULL boot_ghcb in this window, turning the previously-benign teardown-window #VC (a bogus VMGEXIT the host rejected but survived) into a NULL dereference.
Skip the DR7 write once the GHCBs have been torn down; the kexec'ed kernel re-initializes DR7.
---
v2:
- Move the check into hw_breakpoint_disable() through a new snp_hw_breakpoint_disable() helper, so the SNP-specific logic lives in SEV code (Borislav Petkov).
- Drop CC_ATTR_GUEST_DEBUG_VIRT and read DebugSwap from sev_status.
- Skip only once the GHCBs have been torn down, so use_temporary_mm() keeps disabling breakpoints at runtime.
- Trim the commit message (Borislav Petkov).
arch/x86/coco/sev/core.c | 16 ++++++++++++++++ arch/x86/include/asm/debugreg.h | 4 ++++ arch/x86/include/asm/sev.h | 2 ++ 3 files changed, 22 insertions(+)
arch/x86/coco/sev/core.c +16 -0
@@ -723,6 +723,22 @@ void snp_kexec_finish(void) } } +/* +* Without DebugSwap, a DR7 write raises a #VC, which can no longer be serviced +* once snp_kexec_finish() has torn down the GHCBs. The kexec'ed kernel +* re-initializes DR7, so skip the write in that case. +*/ +bool snp_hw_breakpoint_disable (void ) +{ +if (!cc_platform_has (CC_ATTR_GUEST_STATE_ENCRYPT )) +return false; + +if (sev_status &MSR_AMD64_SNP_DEBUG_SWAP ) +return false; + +return !sev_cfg.ghcbs_initialized; +} +#define __ATTR_BASE (SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK)#define INIT_CS_ATTRIBS (__ATTR_BASE | SVM_SELECTOR_READ_MASK | SVM_SELECTOR_CODE_MASK)#define INIT_DS_ATTRIBS (__ATTR_BASE | SVM_SELECTOR_WRITE_MASK)
arch/x86/include/asm/debugreg.h +4 -0
@@ -8,6 +8,7 @@#include <asm/cpufeature.h> #include <asm/msr.h> +#include <asm/sev.h> /* * Define bits that are always set to 1 in DR7, only bit 10 is
@@ -108,6 +109,9 @@ static __always_inline void native_set_debugreg(int regno, unsigned long value)static inline void hw_breakpoint_disable (void ) { +if (snp_hw_breakpoint_disable ()) +return ; +/* Reset the control register for HW Breakpoint */ set_debugreg (DR7_FIXED_1 ,7 );
arch/x86/include/asm/sev.h +2 -0
@@ -522,6 +522,7 @@ int prepare_pte_enc(struct pte_enc_desc *d);void set_pte_enc_mask (pte_t *kpte,unsigned long pfn,pgprot_t new_prot);void snp_kexec_finish (void );void snp_kexec_begin (void ); +bool snp_hw_breakpoint_disable (void );int snp_msg_init (struct snp_msg_desc *mdesc,int vmpck_id);struct snp_msg_desc *snp_msg_alloc (void );
@@ -632,6 +633,7 @@ static inline int prepare_pte_enc(struct pte_enc_desc *d) { return 0; }static inline void set_pte_enc_mask (pte_t *kpte,unsigned long pfn,pgprot_t new_prot) { }static inline void snp_kexec_finish (void ) { }static inline void snp_kexec_begin (void ) { } +static inline bool snp_hw_breakpoint_disable (void ) {return false; }static inline int snp_msg_init (struct snp_msg_desc *mdesc,int vmpck_id) {return -1 ; }static inline struct snp_msg_desc *snp_msg_alloc (void ) {return NULL ; }static inline void snp_msg_free (struct snp_msg_desc *mdesc) { }
base-commit: 9f74092fda91ef93c2b07585785c1dc83cd7971f
Signature · 1 line
2.43.0
Quoted 112 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
machine_kexec()callshw_breakpoint_disable(), which writes DR7. On SEV-ES/SNP guests that do not have DebugSwap enabled, DR7 accesses are intercepted and delivered to the guest as a #VC exception, which must be serviced through the GHCB.By the time
machine_kexec()runs,snp_kexec_finish()(called fromnative_machine_shutdown()) has already converted the per-CPU GHCBs and the boot GHCB back to private and setboot_ghcbto NULL, tearing down the GHCB infrastructure. The DR7 write therefore raises a #VC that dereferences a NULL GHCB pointer, causing a page fault and the kexec to fail.The NULL
boot_ghcbpointer is a result of commit3645eb7e3915("x86/fred:
Fix early boot failures on SEV-ES/SNP guests"): that change makes__sev_get_ghcb()return the now-NULLboot_ghcbin this window, turning the previously-benign teardown-window #VC (a bogus VMGEXIT the host rejected but survived) into a NULL dereference.Skip the DR7 write once the GHCBs have been torn down; the kexec'ed kernel re-initializes DR7.
Suggested-by Tom Lendacky <[email protected]>Fixes 3074152e56c9 ("x86/sev: Convert shared memory back to private on kexec")Signed-off-by Ashish Kalra <[email protected]>---
v2:
- Move the check intohw_breakpoint_disable()through a newsnp_hw_breakpoint_disable()helper, so the SNP-specific logic lives in SEV code (Borislav Petkov).
- DropCC_ATTR_GUEST_DEBUG_VIRTand read DebugSwap fromsev_status.
- Skip only once the GHCBs have been torn down, souse_temporary_mm()keeps disabling breakpoints at runtime.
- Trim the commit message (Borislav Petkov).arch/x86/coco/sev/core.c | 16 ++++++++++++++++ arch/x86/include/asm/debugreg.h | 4 ++++ arch/x86/include/asm/sev.h | 2 ++ 3 files changed, 22 insertions(+)arch/x86/coco/sev/core.c +16
-0@@ -723,6 +723,22 @@ void snp_kexec_finish(void) } } +/* +* Without DebugSwap, a DR7 write raises a #VC, which can no longer be serviced +* once snp_kexec_finish() has torn down the GHCBs. The kexec'ed kernel +* re-initializes DR7, so skip the write in that case. +*/ +bool snp_hw_breakpoint_disable (void ) +{ +if (!cc_platform_has (CC_ATTR_GUEST_STATE_ENCRYPT )) +return false; + +if (sev_status &MSR_AMD64_SNP_DEBUG_SWAP ) +return false; + +return !sev_cfg.ghcbs_initialized; +} +#define __ATTR_BASE (SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK)#define INIT_CS_ATTRIBS (__ATTR_BASE | SVM_SELECTOR_READ_MASK | SVM_SELECTOR_CODE_MASK)#define INIT_DS_ATTRIBS (__ATTR_BASE | SVM_SELECTOR_WRITE_MASK)arch/x86/include/asm/debugreg.h +4
-0@@ -8,6 +8,7 @@#include <asm/cpufeature.h> #include <asm/msr.h> +#include <asm/sev.h> /* * Define bits that are always set to 1 in DR7, only bit 10 is @@ -108,6 +109,9 @@ static __always_inline void native_set_debugreg(int regno, unsigned long value)static inline void hw_breakpoint_disable (void ) { +if (snp_hw_breakpoint_disable ()) +return ; +/* Reset the control register for HW Breakpoint */ set_debugreg (DR7_FIXED_1 ,7 );arch/x86/include/asm/sev.h +2
-0@@ -522,6 +522,7 @@ int prepare_pte_enc(struct pte_enc_desc *d);void set_pte_enc_mask (pte_t *kpte,unsigned long pfn,pgprot_t new_prot);void snp_kexec_finish (void );void snp_kexec_begin (void ); +bool snp_hw_breakpoint_disable (void );int snp_msg_init (struct snp_msg_desc *mdesc,int vmpck_id);struct snp_msg_desc *snp_msg_alloc (void );@@ -632,6 +633,7 @@ static inline int prepare_pte_enc(struct pte_enc_desc *d) { return 0; }static inline void set_pte_enc_mask (pte_t *kpte,unsigned long pfn,pgprot_t new_prot) { }static inline void snp_kexec_finish (void ) { }static inline void snp_kexec_begin (void ) { } +static inline bool snp_hw_breakpoint_disable (void ) {return false; }static inline int snp_msg_init (struct snp_msg_desc *mdesc,int vmpck_id) {return -1 ; }static inline struct snp_msg_desc *snp_msg_alloc (void ) {return NULL ; }static inline void snp_msg_free (struct snp_msg_desc *mdesc) { }
Quoted 1 line
base-commit:
9f74092fda91ef93c2b07585785c1dc83cd7971f
Tested v2 on top of 7.4.0-rc3[aebb727]; kexec works fine with this patch.
Borislav Petkov
maintainerQuoted 14 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
machine_kexec()callshw_breakpoint_disable(), which writes DR7. On SEV-ES/SNP guests that do not have DebugSwap enabled, DR7 accesses are intercepted and delivered to the guest as a #VC exception, which must be serviced through the GHCB.By the time
machine_kexec()runs,snp_kexec_finish()(called fromnative_machine_shutdown()) has already converted the per-CPU GHCBs and the boot GHCB back to private and setboot_ghcbto NULL, tearing down the GHCB infrastructure. The DR7 write therefore raises a #VC that dereferences a NULL GHCB pointer, causing a page fault and the kexec to fail.
Quoted 5 lines
The NULL
boot_ghcbpointer is a result of commit3645eb7e3915("x86/fred:
Fix early boot failures on SEV-ES/SNP guests"): that change makes__sev_get_ghcb()return the now-NULLboot_ghcbin this window, turning the previously-benign teardown-window #VC (a bogus VMGEXIT the host rejected but survived) into a NULL dereference.
So, before that patch from Nikunj, __sev_get_ghcb() would return the per-CPU GHCBs which are not initialized. That was fine for the kexec case - I don't understand the bogus VMGEXIT thing - but apparently it worked there.
For Nikunj, though, that didn't work because FRED can't have early #VCs with those uninitialized GHCBs so he falls back to the boot_ghcb.
Now, however, boot_ghcb is already cleared and DR7 writes cause #VCs.
So something about the lifetime of those things is still not good enough.
So it looks like this, AFAICT:
---
cpu_hotplug_enable();
pr_notice("Starting new kernel\n");
machine_shutdown(); <--- destroy GHCBs
}
kmsg_dump(KMSG_DUMP_SHUTDOWN);
machine_kexec(kexec_image); <--- DR7 write needing a GHCB---
And snp_kexec_finish() does this:
/*
* Switch to using the MSR protocol to change per-CPU GHCBs to
* private. All the per-CPU GHCBs have been switched back to private,
* so can't do any more GHCB calls to the hypervisor beyond this point
* until the kexec'ed kernel starts running.
*/
boot_ghcb = NULL;
sev_cfg.ghcbs_initialized = false;
So here we've switched to the MSR protocol.
And the MSR protocol is supposed to be valid at any time, exactly for cases like that.
And we do have a rudimentary do_vc_no_ghcb() for when no GHCB has been configured yet. After all, that's the point of the MSR protocol, no?
So, long story short, how about switching to that no_ghcb #VC handler instead and dealing with the DR7 VC exception that way and actually using that MSR protocol for what it has been designed?
Instead of piling more cards on the already unstable house of cards?
Hmmm.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteHello Boris,
Quoted 35 lines · Borislav Petkov
Quoted 14 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
machine_kexec()callshw_breakpoint_disable(), which writes DR7. On SEV-ES/SNP guests that do not have DebugSwap enabled, DR7 accesses are intercepted and delivered to the guest as a #VC exception, which must be serviced through the GHCB.By the time
machine_kexec()runs,snp_kexec_finish()(called fromnative_machine_shutdown()) has already converted the per-CPU GHCBs and the boot GHCB back to private and setboot_ghcbto NULL, tearing down the GHCB infrastructure. The DR7 write therefore raises a #VC that dereferences a NULL GHCB pointer, causing a page fault and the kexec to fail.Quoted 5 lines
The NULL
boot_ghcbpointer is a result of commit3645eb7e3915("x86/fred:
Fix early boot failures on SEV-ES/SNP guests"): that change makes__sev_get_ghcb()return the now-NULLboot_ghcbin this window, turning the previously-benign teardown-window #VC (a bogus VMGEXIT the host rejected but survived) into a NULL dereference.So, before that patch from Nikunj,
__sev_get_ghcb()would return the per-CPU GHCBs which are not initialized. That was fine for the kexec case - I don't understand the bogus VMGEXIT thing - but apparently it worked there.For Nikunj, though, that didn't work because FRED can't have early #VCs with those uninitialized GHCBs so he falls back to the
boot_ghcb.Now, however,
boot_ghcbis already cleared and DR7 writes cause #VCs.So something about the lifetime of those things is still not good enough.
So it looks like this, AFAICT:
---
Quoted 8 lines
cpu_hotplug_enable(); pr_notice("Starting new kernel\n"); machine_shutdown(); <--- destroy GHCBs } kmsg_dump(KMSG_DUMP_SHUTDOWN); machine_kexec(kexec_image); <--- DR7 write needing a GHCB---
Right, the window is: snp_kexec_finish() tears down the GHCBs in machine_shutdown(), and machine_kexec() still executes instructions that #VC.
On the "bogus VMGEXIT": before Nikunj's change, __sev_get_ghcb() returned the per-CPU GHCB, which snp_kexec_finish() had already converted back to private. The host mapped that GPA, read ciphertext (exit code 0) and returned GHCB_ERR_MISSING_INPUT, but couldn't write the result into the private page, so the guest saw its own stale contents, treated the call as successful and moved on. The DR7 write was simply dropped. It worked by accident.
Quoted 16 lines
And
snp_kexec_finish()does this:/*
* Switch to using the MSR protocol to change per-CPU GHCBs to
* private. All the per-CPU GHCBs have been switched back to private,
* so can't do any more GHCB calls to the hypervisor beyond this point
* until the kexec'ed kernel starts running.
*/
boot_ghcb= NULL;
sev_cfg.ghcbs_initialized = false;So here we've switched to the MSR protocol.>
And the MSR protocol is supposed to be valid at any time, exactly for cases like that.
Quoted 6 lines
And we do have a rudimentary
do_vc_no_ghcb()for when no GHCB has been configured yet. After all, that's the point of the MSR protocol, no?So, long story short, how about switching to that
no_ghcb#VC handler instead and dealing with the DR7 VC exception that way and actually using that MSR protocol for what it has been designed?
As for using do_vc_no_ghcb(): the GHCB MSR protocol requests are meant for specific guest-initiated operations (protocol negotiation, GHCB registration, page state changes, termination, etc.). The only one that corresponds to an intercepted instruction is CPUID, which is why do_vc_no_ghcb() handles only the CPUID exit code and terminates the guest for anything else. There's no MSR-protocol equivalent of a DR7 write, so the no-GHCB #VC handler can't handle it.
Thanks,
Ashish
Quoted 5 lines
Instead of piling more cards on the already unstable house of cards?
Hmmm.
Hi Boris,
Quoted 21 lines · Borislav Petkov
Quoted 14 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
machine_kexec()callshw_breakpoint_disable(), which writes DR7. On SEV-ES/SNP guests that do not have DebugSwap enabled, DR7 accesses are intercepted and delivered to the guest as a #VC exception, which must be serviced through the GHCB.By the time
machine_kexec()runs,snp_kexec_finish()(called fromnative_machine_shutdown()) has already converted the per-CPU GHCBs and the boot GHCB back to private and setboot_ghcbto NULL, tearing down the GHCB infrastructure. The DR7 write therefore raises a #VC that dereferences a NULL GHCB pointer, causing a page fault and the kexec to fail.Quoted 5 lines
The NULL
boot_ghcbpointer is a result of commit3645eb7e3915("x86/fred:
Fix early boot failures on SEV-ES/SNP guests"): that change makes__sev_get_ghcb()return the now-NULLboot_ghcbin this window, turning the previously-benign teardown-window #VC (a bogus VMGEXIT the host rejected but survived) into a NULL dereference.
Quoted 3 lines
So, before that patch from Nikunj,
__sev_get_ghcb()would return the per-CPU GHCBs which are not initialized. That was fine for the kexec case - I don't understand the bogus VMGEXIT thing - but apparently it worked there.
Before 3645eb7e3915, the DR7 write during kexec was silently dropped by the hypervisor because the GHCB had already been converted back to private. The guest did not notice and carried on, so kexec only worked because nothing depended on that write.
Regards,
Nikunj