Skip to contentSkip to conversation

x86/sev: Harden #VC instruction emulation somewhat

PATCH New Wenshan Lan 1 participant 2 messages Feb 26 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86/sev: Harden #VC instruction emulation somewhat

From: "Borislav Petkov (AMD)" <[email protected]>

[ Upstream commit e3ef461af35a8c74f2f4ce6616491ddb355a208f ]

Compare the opcode bytes at rIP for each #VC exit reason to verify the instruction which raised the #VC exception is actually the right one.

---
 arch/x86/boot/compressed/sev.c |   4 ++
 arch/x86/kernel/sev-shared.c   | 102 ++++++++++++++++++++++++++++++++-
 arch/x86/kernel/sev.c          |   5 +-
 3 files changed, 108 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/sev.c +4 -0
index 92c9f8b79f0d..b9f53e38f1e6 100644
@@ -277,6 +277,10 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
 	if (result != ES_OK)
 		goto finish;
 
+	result = vc_check_opcode_bytes(&ctxt, exit_code);
+	if (result != ES_OK)
+		goto finish;
+
 	switch (exit_code) {
 	case SVM_EXIT_RDTSC:
 	case SVM_EXIT_RDTSCP:
arch/x86/kernel/sev-shared.c +100 -2

126 lines not on this page · raw message

arch/x86/kernel/sev.c +4 -1
index c6a9a9d3ff2f..ad2b3ea24dd4 100644
@@ -1749,7 +1749,10 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
 {
-	enum es_result result;
+	enum es_result result = vc_check_opcode_bytes(ctxt, exit_code);
+
+	if (result != ES_OK)
+		return result;
 
 	switch (exit_code) {
 	case SVM_EXIT_READ_DR7:
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 x86/sev: Check for MWAITX and MONITORX opcodes in the #VC handler

From: Tom Lendacky <[email protected]>

[ Upstream commit e70316d17f6ab49a6038ffd115397fd68f8c7be8 ]

The MWAITX and MONITORX instructions generate the same #VC error code as the MWAIT and MONITOR instructions, respectively. Update the #VC handler opcode checking to also support the MWAITX and MONITORX opcodes.

---
 arch/x86/kernel/sev-shared.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/kernel/sev-shared.c +4 -2
index 1dbf114b3365..caa3de2dcd0a 100644
@@ -1237,12 +1237,14 @@ static enum es_result vc_check_opcode_bytes(struct es_em_ctxt *ctxt,
 		break;
 
 	case SVM_EXIT_MONITOR:
-		if (opcode == 0x010f && modrm == 0xc8)
+		/* MONITOR and MONITORX instructions generate the same error code */
+		if (opcode == 0x010f && (modrm == 0xc8 || modrm == 0xfa))
 			return ES_OK;
 		break;
 
 	case SVM_EXIT_MWAIT:
-		if (opcode == 0x010f && modrm == 0xc9)
+		/* MWAIT and MWAITX instructions generate the same error code */
+		if (opcode == 0x010f && (modrm == 0xc9 || modrm == 0xfb))
 			return ES_OK;
 		break;
 
Signature · 1 line
2.43.0