kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Fix SEV-ES INS/OUTS instructions for word, dword, and qword.
@ 2021-11-18  2:13 Michael Sterritt
  2021-11-18  2:41 ` Peter Gonda
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Michael Sterritt @ 2021-11-18  2:13 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Joerg Roedel, Peter Zijlstra, Tom Lendacky,
	linux-kernel, kvm, virtualization, linux-coco
  Cc: marcorr, pgonda, Michael Sterritt

Properly type the operands being passed to __put_user()/__get_user().
Otherwise, these routines truncate data for dependent instructions
(e.g., INSW) and only read/write one byte.

Tested: Tested by sending a string with `REP OUTSW` to a port and then
reading it back in with `REP INSW` on the same port. Previous behavior
was to only send and receive the first char of the size. For example,
word operations for "abcd" would only read/write "ac". With change, the
full string is now written and read back.

Signed-off-by: Michael Sterritt <sterritt@google.com>
Reviewed-by: Marc Orr <marcorr@google.com>
Reviewed-by: Peter Gonda <pgonda@google.com>
---
 arch/x86/kernel/sev.c | 57 +++++++++++++++++++++++++++++--------------
 1 file changed, 39 insertions(+), 18 deletions(-)

diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c
index 74f0ec955384..a9fc2ac7a8bd 100644
--- a/arch/x86/kernel/sev.c
+++ b/arch/x86/kernel/sev.c
@@ -294,11 +294,6 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
 				   char *dst, char *buf, size_t size)
 {
 	unsigned long error_code = X86_PF_PROT | X86_PF_WRITE;
-	char __user *target = (char __user *)dst;
-	u64 d8;
-	u32 d4;
-	u16 d2;
-	u8  d1;
 
 	/*
 	 * This function uses __put_user() independent of whether kernel or user
@@ -320,26 +315,42 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
 	 * instructions here would cause infinite nesting.
 	 */
 	switch (size) {
-	case 1:
+	case 1: {
+		u8 d1;
+		u8 __user *target = (u8 __user *)dst;
+
 		memcpy(&d1, buf, 1);
 		if (__put_user(d1, target))
 			goto fault;
 		break;
-	case 2:
+	}
+	case 2: {
+		u16 d2;
+		u16 __user *target = (u16 __user *)dst;
+
 		memcpy(&d2, buf, 2);
 		if (__put_user(d2, target))
 			goto fault;
 		break;
-	case 4:
+	}
+	case 4: {
+		u32 d4;
+		u32 __user *target = (u32 __user *)dst;
+
 		memcpy(&d4, buf, 4);
 		if (__put_user(d4, target))
 			goto fault;
 		break;
-	case 8:
+	}
+	case 8: {
+		u64 d8;
+		u64 __user *target = (u64 __user *)dst;
+
 		memcpy(&d8, buf, 8);
 		if (__put_user(d8, target))
 			goto fault;
 		break;
+	}
 	default:
 		WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
 		return ES_UNSUPPORTED;
@@ -362,11 +373,6 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
 				  char *src, char *buf, size_t size)
 {
 	unsigned long error_code = X86_PF_PROT;
-	char __user *s = (char __user *)src;
-	u64 d8;
-	u32 d4;
-	u16 d2;
-	u8  d1;
 
 	/*
 	 * This function uses __get_user() independent of whether kernel or user
@@ -388,26 +394,41 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
 	 * instructions here would cause infinite nesting.
 	 */
 	switch (size) {
-	case 1:
+	case 1: {
+		u8 d1;
+		u8 __user *s = (u8 __user *)src;
+
 		if (__get_user(d1, s))
 			goto fault;
 		memcpy(buf, &d1, 1);
 		break;
-	case 2:
+	}
+	case 2: {
+		u16 d2;
+		u16 __user *s = (u16 __user *)src;
+
 		if (__get_user(d2, s))
 			goto fault;
 		memcpy(buf, &d2, 2);
 		break;
-	case 4:
+	}
+	case 4: {
+		u32 d4;
+		u32 __user *s = (u32 __user *)src;
+
 		if (__get_user(d4, s))
 			goto fault;
 		memcpy(buf, &d4, 4);
 		break;
-	case 8:
+	}
+	case 8: {
+		u64 d8;
+		u64 __user *s = (u64 __user *)src;
 		if (__get_user(d8, s))
 			goto fault;
 		memcpy(buf, &d8, 8);
 		break;
+	}
 	default:
 		WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
 		return ES_UNSUPPORTED;
-- 
2.34.0.rc2.393.gf8c9666880-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Fix SEV-ES INS/OUTS instructions for word, dword, and qword.
  2021-11-18  2:13 [PATCH] Fix SEV-ES INS/OUTS instructions for word, dword, and qword Michael Sterritt
@ 2021-11-18  2:41 ` Peter Gonda
  2021-11-18 11:48 ` Paolo Bonzini
  2021-11-19  8:36 ` Joerg Roedel
  2 siblings, 0 replies; 4+ messages in thread
From: Peter Gonda @ 2021-11-18  2:41 UTC (permalink / raw)
  To: Michael Sterritt
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Joerg Roedel, Peter Zijlstra, Tom Lendacky,
	linux-kernel, kvm, virtualization, linux-coco, marcorr

On Wed, Nov 17, 2021 at 7:13 PM Michael Sterritt <sterritt@google.com> wrote:
>
> Properly type the operands being passed to __put_user()/__get_user().
> Otherwise, these routines truncate data for dependent instructions
> (e.g., INSW) and only read/write one byte.
>
> Tested: Tested by sending a string with `REP OUTSW` to a port and then
> reading it back in with `REP INSW` on the same port. Previous behavior
> was to only send and receive the first char of the size. For example,
> word operations for "abcd" would only read/write "ac". With change, the
> full string is now written and read back.

I think this should include:

Fixes: f980f9c31a923 (x86/sev-es: Compile early handler code into kernel image)

>
> Signed-off-by: Michael Sterritt <sterritt@google.com>
> Reviewed-by: Marc Orr <marcorr@google.com>
> Reviewed-by: Peter Gonda <pgonda@google.com>
> ---
>  arch/x86/kernel/sev.c | 57 +++++++++++++++++++++++++++++--------------
>  1 file changed, 39 insertions(+), 18 deletions(-)
>
> diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c
> index 74f0ec955384..a9fc2ac7a8bd 100644
> --- a/arch/x86/kernel/sev.c
> +++ b/arch/x86/kernel/sev.c
> @@ -294,11 +294,6 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
>                                    char *dst, char *buf, size_t size)
>  {
>         unsigned long error_code = X86_PF_PROT | X86_PF_WRITE;
> -       char __user *target = (char __user *)dst;
> -       u64 d8;
> -       u32 d4;
> -       u16 d2;
> -       u8  d1;
>
>         /*
>          * This function uses __put_user() independent of whether kernel or user
> @@ -320,26 +315,42 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
>          * instructions here would cause infinite nesting.
>          */
>         switch (size) {
> -       case 1:
> +       case 1: {
> +               u8 d1;
> +               u8 __user *target = (u8 __user *)dst;
> +
>                 memcpy(&d1, buf, 1);
>                 if (__put_user(d1, target))
>                         goto fault;
>                 break;
> -       case 2:
> +       }
> +       case 2: {
> +               u16 d2;
> +               u16 __user *target = (u16 __user *)dst;
> +
>                 memcpy(&d2, buf, 2);
>                 if (__put_user(d2, target))
>                         goto fault;
>                 break;
> -       case 4:
> +       }
> +       case 4: {
> +               u32 d4;
> +               u32 __user *target = (u32 __user *)dst;
> +
>                 memcpy(&d4, buf, 4);
>                 if (__put_user(d4, target))
>                         goto fault;
>                 break;
> -       case 8:
> +       }
> +       case 8: {
> +               u64 d8;
> +               u64 __user *target = (u64 __user *)dst;
> +
>                 memcpy(&d8, buf, 8);
>                 if (__put_user(d8, target))
>                         goto fault;
>                 break;
> +       }
>         default:
>                 WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
>                 return ES_UNSUPPORTED;
> @@ -362,11 +373,6 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
>                                   char *src, char *buf, size_t size)
>  {
>         unsigned long error_code = X86_PF_PROT;
> -       char __user *s = (char __user *)src;
> -       u64 d8;
> -       u32 d4;
> -       u16 d2;
> -       u8  d1;
>
>         /*
>          * This function uses __get_user() independent of whether kernel or user
> @@ -388,26 +394,41 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
>          * instructions here would cause infinite nesting.
>          */
>         switch (size) {
> -       case 1:
> +       case 1: {
> +               u8 d1;
> +               u8 __user *s = (u8 __user *)src;
> +
>                 if (__get_user(d1, s))
>                         goto fault;
>                 memcpy(buf, &d1, 1);
>                 break;
> -       case 2:
> +       }
> +       case 2: {
> +               u16 d2;
> +               u16 __user *s = (u16 __user *)src;
> +
>                 if (__get_user(d2, s))
>                         goto fault;
>                 memcpy(buf, &d2, 2);
>                 break;
> -       case 4:
> +       }
> +       case 4: {
> +               u32 d4;
> +               u32 __user *s = (u32 __user *)src;
> +
>                 if (__get_user(d4, s))
>                         goto fault;
>                 memcpy(buf, &d4, 4);
>                 break;
> -       case 8:
> +       }
> +       case 8: {
> +               u64 d8;
> +               u64 __user *s = (u64 __user *)src;
>                 if (__get_user(d8, s))
>                         goto fault;
>                 memcpy(buf, &d8, 8);
>                 break;
> +       }
>         default:
>                 WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
>                 return ES_UNSUPPORTED;
> --
> 2.34.0.rc2.393.gf8c9666880-goog
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Fix SEV-ES INS/OUTS instructions for word, dword, and qword.
  2021-11-18  2:13 [PATCH] Fix SEV-ES INS/OUTS instructions for word, dword, and qword Michael Sterritt
  2021-11-18  2:41 ` Peter Gonda
@ 2021-11-18 11:48 ` Paolo Bonzini
  2021-11-19  8:36 ` Joerg Roedel
  2 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2021-11-18 11:48 UTC (permalink / raw)
  To: Michael Sterritt, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Joerg Roedel, Peter Zijlstra,
	Tom Lendacky, linux-kernel, kvm, virtualization, linux-coco
  Cc: marcorr, pgonda

On 11/18/21 03:13, Michael Sterritt wrote:
> Properly type the operands being passed to __put_user()/__get_user().
> Otherwise, these routines truncate data for dependent instructions
> (e.g., INSW) and only read/write one byte.
> 
> Tested: Tested by sending a string with `REP OUTSW` to a port and then
> reading it back in with `REP INSW` on the same port. Previous behavior
> was to only send and receive the first char of the size. For example,
> word operations for "abcd" would only read/write "ac". With change, the
> full string is now written and read back.
> 
> Signed-off-by: Michael Sterritt <sterritt@google.com>
> Reviewed-by: Marc Orr <marcorr@google.com>
> Reviewed-by: Peter Gonda <pgonda@google.com>
> ---
>   arch/x86/kernel/sev.c | 57 +++++++++++++++++++++++++++++--------------
>   1 file changed, 39 insertions(+), 18 deletions(-)
> 
> diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c
> index 74f0ec955384..a9fc2ac7a8bd 100644
> --- a/arch/x86/kernel/sev.c
> +++ b/arch/x86/kernel/sev.c
> @@ -294,11 +294,6 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
>   				   char *dst, char *buf, size_t size)
>   {
>   	unsigned long error_code = X86_PF_PROT | X86_PF_WRITE;
> -	char __user *target = (char __user *)dst;
> -	u64 d8;
> -	u32 d4;
> -	u16 d2;
> -	u8  d1;
>   
>   	/*
>   	 * This function uses __put_user() independent of whether kernel or user
> @@ -320,26 +315,42 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
>   	 * instructions here would cause infinite nesting.
>   	 */
>   	switch (size) {
> -	case 1:
> +	case 1: {
> +		u8 d1;
> +		u8 __user *target = (u8 __user *)dst;
> +
>   		memcpy(&d1, buf, 1);
>   		if (__put_user(d1, target))
>   			goto fault;
>   		break;
> -	case 2:
> +	}
> +	case 2: {
> +		u16 d2;
> +		u16 __user *target = (u16 __user *)dst;
> +
>   		memcpy(&d2, buf, 2);
>   		if (__put_user(d2, target))
>   			goto fault;
>   		break;
> -	case 4:
> +	}
> +	case 4: {
> +		u32 d4;
> +		u32 __user *target = (u32 __user *)dst;
> +
>   		memcpy(&d4, buf, 4);
>   		if (__put_user(d4, target))
>   			goto fault;
>   		break;
> -	case 8:
> +	}
> +	case 8: {
> +		u64 d8;
> +		u64 __user *target = (u64 __user *)dst;
> +
>   		memcpy(&d8, buf, 8);
>   		if (__put_user(d8, target))
>   			goto fault;
>   		break;
> +	}
>   	default:
>   		WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
>   		return ES_UNSUPPORTED;
> @@ -362,11 +373,6 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
>   				  char *src, char *buf, size_t size)
>   {
>   	unsigned long error_code = X86_PF_PROT;
> -	char __user *s = (char __user *)src;
> -	u64 d8;
> -	u32 d4;
> -	u16 d2;
> -	u8  d1;
>   
>   	/*
>   	 * This function uses __get_user() independent of whether kernel or user
> @@ -388,26 +394,41 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
>   	 * instructions here would cause infinite nesting.
>   	 */
>   	switch (size) {
> -	case 1:
> +	case 1: {
> +		u8 d1;
> +		u8 __user *s = (u8 __user *)src;
> +
>   		if (__get_user(d1, s))
>   			goto fault;
>   		memcpy(buf, &d1, 1);
>   		break;
> -	case 2:
> +	}
> +	case 2: {
> +		u16 d2;
> +		u16 __user *s = (u16 __user *)src;
> +
>   		if (__get_user(d2, s))
>   			goto fault;
>   		memcpy(buf, &d2, 2);
>   		break;
> -	case 4:
> +	}
> +	case 4: {
> +		u32 d4;
> +		u32 __user *s = (u32 __user *)src;
> +
>   		if (__get_user(d4, s))
>   			goto fault;
>   		memcpy(buf, &d4, 4);
>   		break;
> -	case 8:
> +	}
> +	case 8: {
> +		u64 d8;
> +		u64 __user *s = (u64 __user *)src;
>   		if (__get_user(d8, s))
>   			goto fault;
>   		memcpy(buf, &d8, 8);
>   		break;
> +	}
>   	default:
>   		WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
>   		return ES_UNSUPPORTED;
> 

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Fix SEV-ES INS/OUTS instructions for word, dword, and qword.
  2021-11-18  2:13 [PATCH] Fix SEV-ES INS/OUTS instructions for word, dword, and qword Michael Sterritt
  2021-11-18  2:41 ` Peter Gonda
  2021-11-18 11:48 ` Paolo Bonzini
@ 2021-11-19  8:36 ` Joerg Roedel
  2 siblings, 0 replies; 4+ messages in thread
From: Joerg Roedel @ 2021-11-19  8:36 UTC (permalink / raw)
  To: Michael Sterritt
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Peter Zijlstra, Tom Lendacky, linux-kernel, kvm,
	virtualization, linux-coco, marcorr, pgonda

Hi Michael,

On Wed, Nov 17, 2021 at 06:13:26PM -0800, Michael Sterritt wrote:
> Properly type the operands being passed to __put_user()/__get_user().
> Otherwise, these routines truncate data for dependent instructions
> (e.g., INSW) and only read/write one byte.
> 
> Tested: Tested by sending a string with `REP OUTSW` to a port and then
> reading it back in with `REP INSW` on the same port. Previous behavior
> was to only send and receive the first char of the size. For example,
> word operations for "abcd" would only read/write "ac". With change, the
> full string is now written and read back.

Thanks for fixing this! When you re-send, please change the subject to

	x86/sev-es: Fix SEV-ES INS/OUTS instructions for word, dword, and qword

Regards,

-- 
Jörg Rödel
jroedel@suse.de

SUSE Software Solutions Germany GmbH
Maxfeldstr. 5
90409 Nürnberg
Germany
 
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-11-19  8:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-18  2:13 [PATCH] Fix SEV-ES INS/OUTS instructions for word, dword, and qword Michael Sterritt
2021-11-18  2:41 ` Peter Gonda
2021-11-18 11:48 ` Paolo Bonzini
2021-11-19  8:36 ` Joerg Roedel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).