linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] virt/coco/sev-guest: Initialize err in handle_guest_request
@ 2022-10-12  0:23 Dionna Glaze
  2022-10-12 23:31 ` Peter Gonda
  0 siblings, 1 reply; 5+ messages in thread
From: Dionna Glaze @ 2022-10-12  0:23 UTC (permalink / raw)
  To: x86, linux-kernel
  Cc: Dionna Glaze, Tom Lendacky, Paolo Bonzini, Joerg Roedel,
	Peter Gonda, Thomas Gleixner, Dave Hansen

The err variable may not be set in the call to snp_issue_guest_request,
yet it is unconditionally written back to fw_err if fw_err is non-null.
This is undefined behavior, and currently returns uninitialized kernel
stack memory to user space.

Cc: Tom Lendacky <Thomas.Lendacky@amd.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Joerg Roedel <jroedel@suse.de>
Cc: Peter Gonda <pgonda@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>

Signed-off-by: Dionna Glaze <dionnaglaze@google.com>
---
 drivers/virt/coco/sevguest/sevguest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/virt/coco/sevguest/sevguest.c b/drivers/virt/coco/sevguest/sevguest.c
index 112c0458cbda..7a62bfc063fc 100644
--- a/drivers/virt/coco/sevguest/sevguest.c
+++ b/drivers/virt/coco/sevguest/sevguest.c
@@ -307,7 +307,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
 				u8 type, void *req_buf, size_t req_sz, void *resp_buf,
 				u32 resp_sz, __u64 *fw_err)
 {
-	unsigned long err;
+	unsigned long err = 0;
 	u64 seqno;
 	int rc;
 
-- 
2.38.0.rc1.362.ged0d419d3c-goog


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

* Re: [PATCH] virt/coco/sev-guest: Initialize err in handle_guest_request
  2022-10-12  0:23 [PATCH] virt/coco/sev-guest: Initialize err in handle_guest_request Dionna Glaze
@ 2022-10-12 23:31 ` Peter Gonda
  2022-10-19 19:58   ` Dionna Amalie Glaze
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Gonda @ 2022-10-12 23:31 UTC (permalink / raw)
  To: Dionna Glaze
  Cc: x86, linux-kernel, Tom Lendacky, Paolo Bonzini, Joerg Roedel,
	Thomas Gleixner, Dave Hansen

On Tue, Oct 11, 2022 at 6:23 PM Dionna Glaze <dionnaglaze@google.com> wrote:
>
> The err variable may not be set in the call to snp_issue_guest_request,
> yet it is unconditionally written back to fw_err if fw_err is non-null.
> This is undefined behavior, and currently returns uninitialized kernel
> stack memory to user space.

Should this be fixed in: snp_issue_guest_request()? Since other
callers might make similar mistakes.

And currently we have:

static long snp_guest_ioctl(...)
{
..
  input.fw_err = 0xff;
..
}

Which I think is an attempt to make fw_err make sense if the FW is
never called, should we try to maintain that property?

>
> Cc: Tom Lendacky <Thomas.Lendacky@amd.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Joerg Roedel <jroedel@suse.de>
> Cc: Peter Gonda <pgonda@google.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Dave Hansen <dave.hansen@linux.intel.com>
>
> Signed-off-by: Dionna Glaze <dionnaglaze@google.com>
> ---
>  drivers/virt/coco/sevguest/sevguest.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/virt/coco/sevguest/sevguest.c b/drivers/virt/coco/sevguest/sevguest.c
> index 112c0458cbda..7a62bfc063fc 100644
> --- a/drivers/virt/coco/sevguest/sevguest.c
> +++ b/drivers/virt/coco/sevguest/sevguest.c
> @@ -307,7 +307,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
>                                 u8 type, void *req_buf, size_t req_sz, void *resp_buf,
>                                 u32 resp_sz, __u64 *fw_err)
>  {
> -       unsigned long err;
> +       unsigned long err = 0;
>         u64 seqno;
>         int rc;
>
> --
> 2.38.0.rc1.362.ged0d419d3c-goog
>

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

* Re: [PATCH] virt/coco/sev-guest: Initialize err in handle_guest_request
  2022-10-12 23:31 ` Peter Gonda
@ 2022-10-19 19:58   ` Dionna Amalie Glaze
  2022-10-27  9:45     ` Borislav Petkov
  0 siblings, 1 reply; 5+ messages in thread
From: Dionna Amalie Glaze @ 2022-10-19 19:58 UTC (permalink / raw)
  To: Peter Gonda
  Cc: x86, linux-kernel, Tom Lendacky, Paolo Bonzini, Joerg Roedel,
	Thomas Gleixner, Dave Hansen

> And currently we have:
>
> static long snp_guest_ioctl(...)
> {
> ..
>   input.fw_err = 0xff;
> ..
> }
>
> Which I think is an attempt to make fw_err make sense if the FW is
> never called, should we try to maintain that property?

fw_err = 0xff doesn't make sense to me actually. It's not a documented
code that the firmware was never called.
Still, we can simply pass fw_err to snp_issue_guest_request rather
than an unsigned long err, since a null pointer results in an -EINVAL.


-- 
-Dionna Glaze, PhD (she/her)

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

* Re: [PATCH] virt/coco/sev-guest: Initialize err in handle_guest_request
  2022-10-19 19:58   ` Dionna Amalie Glaze
@ 2022-10-27  9:45     ` Borislav Petkov
  2022-10-27 14:16       ` Peter Gonda
  0 siblings, 1 reply; 5+ messages in thread
From: Borislav Petkov @ 2022-10-27  9:45 UTC (permalink / raw)
  To: Dionna Amalie Glaze
  Cc: Peter Gonda, x86, linux-kernel, Tom Lendacky, Paolo Bonzini,
	Joerg Roedel, Thomas Gleixner, Dave Hansen

On Wed, Oct 19, 2022 at 12:58:12PM -0700, Dionna Amalie Glaze wrote:
> fw_err = 0xff doesn't make sense to me actually. It's not a documented
> code that the firmware was never called.
> Still, we can simply pass fw_err to snp_issue_guest_request rather
> than an unsigned long err, since a null pointer results in an -EINVAL.

Yes, pls do that. Such I/O function args are always a PITA anyway.

In retrospect, that handle_guest_request() with gazillion args should
have been made to take a struct as a single argument and populate it as
it operates.

The callers then would look at it and decide what to do.

Looking at the callers, they all take members of struct
snp_guest_request_ioctl and pass them in. A first step in cleaning that
up could be to simply pass that struct snp_guest_request_ioctl pointer
instead...

Oh well, in case folks feel bored. :-)

Thx.

-- 
Regards/Gruss,
    Boris.

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

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

* Re: [PATCH] virt/coco/sev-guest: Initialize err in handle_guest_request
  2022-10-27  9:45     ` Borislav Petkov
@ 2022-10-27 14:16       ` Peter Gonda
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Gonda @ 2022-10-27 14:16 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Dionna Amalie Glaze, x86, linux-kernel, Tom Lendacky,
	Paolo Bonzini, Joerg Roedel, Thomas Gleixner, Dave Hansen

On Thu, Oct 27, 2022 at 3:45 AM Borislav Petkov <bp@alien8.de> wrote:
>
> On Wed, Oct 19, 2022 at 12:58:12PM -0700, Dionna Amalie Glaze wrote:
> > fw_err = 0xff doesn't make sense to me actually. It's not a documented
> > code that the firmware was never called.
> > Still, we can simply pass fw_err to snp_issue_guest_request rather
> > than an unsigned long err, since a null pointer results in an -EINVAL.

Yes this was not what my comments on the patch series intended.

On the host side we have `psp_ret = -1` inside of
__sev_platform_init_locked(). I think defining SEV_RET_NO_FW_CALL as
UINT32_MAX and moving to make all values of psp_ret where the psp is
not yet called would help callers understand errors more clearly.

>
> Yes, pls do that. Such I/O function args are always a PITA anyway.
>
> In retrospect, that handle_guest_request() with gazillion args should
> have been made to take a struct as a single argument and populate it as
> it operates.
>
> The callers then would look at it and decide what to do.
>
> Looking at the callers, they all take members of struct
> snp_guest_request_ioctl and pass them in. A first step in cleaning that
> up could be to simply pass that struct snp_guest_request_ioctl pointer
> instead...
>
> Oh well, in case folks feel bored. :-)

Ah, good idea. If I have some spare cycles.

>
> Thx.
>
> --
> Regards/Gruss,
>     Boris.
>
> https://people.kernel.org/tglx/notes-about-netiquette

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

end of thread, other threads:[~2022-10-27 14:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-12  0:23 [PATCH] virt/coco/sev-guest: Initialize err in handle_guest_request Dionna Glaze
2022-10-12 23:31 ` Peter Gonda
2022-10-19 19:58   ` Dionna Amalie Glaze
2022-10-27  9:45     ` Borislav Petkov
2022-10-27 14:16       ` Peter Gonda

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).