Skip to contentSkip to conversation

x86/sev-es: Fix SEV-ES #VC handler for string port IO

PATCH Superseded Hyunwook (Wooky) Baek 2 participants 3 messages Jan 8, 2021 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/sev-es: Fix SEV-ES #VC handler for string port IO

Don't assume dest/source buffers are userspace addresses when manually copying data for string I/O or MOVS MMIO, as {get,put}_user() will fail if handed a kernel address and ultimately lead to a kernel panic.

---
 arch/x86/kernel/sev-es.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/x86/kernel/sev-es.c +12 -0
index 0bd1a0fc587e..812494d216ba 100644
@@ -286,6 +286,12 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
 	u16 d2;
 	u8  d1;
 
+	/* If instruction ran in kernel mode and the I/O buffer is in kernel space */
+	if (!user_mode(ctxt->regs) && !access_ok(s, size)) {
+		memcpy(dst, buf, size);
+		return ES_OK;
+	}
+
 	switch (size) {
 	case 1:
 		memcpy(&d1, buf, 1);
@@ -335,6 +341,12 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
 	u16 d2;
 	u8  d1;
 
+	/* If instruction ran in kernel mode and the I/O buffer is in kernel space */
+	if (!user_mode(ctxt->regs) && !access_ok(s, size)) {
+		memcpy(buf, src, size);
+		return ES_OK;
+	}
+
 	switch (size) {
 	case 1:
 		if (get_user(d1, s))
Signature · 1 line
2.29.2.729.g45daf8777d-goog
maintainer
#1 lore raw
Quoted 15 lines · Hyunwook (Wooky) Baek

Don't assume dest/source buffers are userspace addresses when manually copying data for string I/O or MOVS MMIO, as {get,put}_user() will fail if handed a kernel address and ultimately lead to a kernel panic.

---
 arch/x86/kernel/sev-es.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/x86/kernel/sev-es.c +0 -0
index 0bd1a0fc587e..812494d216ba 100644

arch/x86/kernel/sev-es.c static enum es_result vc_write_mem(struct es_em_ctxt *ctxt, · line 290

Quoted 5 lines
arch/x86/kernel/sev-es.c +2 -0
@@ -286,6 +286,12 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
 	u16 d2;
 	u8  d1;
 
+	/* If instruction ran in kernel mode and the I/O buffer is in kernel space */
+	if (!user_mode(ctxt->regs) && !access_ok(s, size)) {
						^^^

Just by looking at this patch I know you haven't tested it before sending.

:-(

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#2 lore raw

Hi Borisslav,

My sincere apologies for it.
We actually tested the patch but I made a bad mistake while generating the patch file to email.
I will send a PATCH V2 after testing the email version patch file from the scratch.

Regards,
Wooky

Quoted 33 lines · Borislav Petkov
Quoted 15 lines · Hyunwook (Wooky) Baek

Don't assume dest/source buffers are userspace addresses when manually copying data for string I/O or MOVS MMIO, as {get,put}_user() will fail if handed a kernel address and ultimately lead to a kernel panic.

---
 arch/x86/kernel/sev-es.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/x86/kernel/sev-es.c +0 -0
index 0bd1a0fc587e..812494d216ba 100644

arch/x86/kernel/sev-es.c static enum es_result vc_write_mem(struct es_em_ctxt *ctxt, · line 290

Quoted 5 lines
arch/x86/kernel/sev-es.c +2 -0
@@ -286,6 +286,12 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
      u16 d2;
      u8  d1;
 
+     /* If instruction ran in kernel mode and the I/O buffer is in kernel space */
+     if (!user_mode(ctxt->regs) && !access_ok(s, size)) {
                                                ^^^

Just by looking at this patch I know you haven't tested it before sending.

:-(

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette