Message ID | 20221104230040.2346862-2-dionnaglaze@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp687699wru; Fri, 4 Nov 2022 16:03:58 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4D+rObnrda32m8KtU8hGr50MC3ZnmT+odiaE0SQ/uhkZh1+4W49M5ZSCWksWZvHPIKeyuB X-Received: by 2002:a05:6402:1bda:b0:464:4eb2:8c25 with SMTP id ch26-20020a0564021bda00b004644eb28c25mr8594093edb.278.1667603038045; Fri, 04 Nov 2022 16:03:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667603038; cv=none; d=google.com; s=arc-20160816; b=zPM67wjZKsTYVAD5K/JO5MYfriWgLGWKjBIr66yLd8ArXh8GaeRjyMA1JbdJdE5pXP oT5onzNbrpDAcpI2AxmeXcTnT4Slx0P+lJxObO3Y9V6rkcu8wMRxL6ZR+Wl6fMKzgM3K lZxMxlwdDOu9WB6Vi6RGiwhfiyYhDqux7n47Tiz+rOMBJKMVQe8/CbWxDzbeYkL+1/om 37Oj+IQrz1RezQVVv6O8zkzzI0de4JJftVgzfFiBJ4rm3k7j3zADs2FBELEgtVPp22op TPEv0uuqhYGJZChqt7R5Wxet17nMLNFDSYiH6EPLa6jqkp2Si+DWLiPQpQ7Puumi9IKR LwqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=vzksqxZzngSMnVq4LNIxjbY1eQuHqjVmZsqNyLkHpLQ=; b=p4wbSJshCbJrfNvjN3ntg3iPqFFUWM9DvdlXW/pNFZ9TC1YDIxy8f5bMTKrhXs5/Xm spFvbA38AdBFiUkCUGuhhi7LdpJtaAn0ulfYu3tdLTwPaTghGjp77XEhNqFi2/eUaK5C NtkyjQmxcgxiK1GoKzkvcp/REXsTe2gKi2zVOAWdFV5duP9HY9xzNr1LlyloFJ9Opesk IS4niKdi83JpyUfEzgbZVA14R7T0a+SaTDWAS9GG65+T44PLjHvP0EBCgzX6hSjbXoav SDC3bKP0MYsRDnbeu7TSVlGwX8IYOhPsMvCqOBdErUU2BbvOJcw/i6Ji01gGYV4Ns6BV D61A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=BIBsVrxM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bb6-20020a1709070a0600b007acbaeed9besi403874ejc.398.2022.11.04.16.03.31; Fri, 04 Nov 2022 16:03:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=BIBsVrxM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229650AbiKDXBP (ORCPT <rfc822;hjfbswb@gmail.com> + 99 others); Fri, 4 Nov 2022 19:01:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229637AbiKDXBH (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 4 Nov 2022 19:01:07 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 985482FC1B for <linux-kernel@vger.kernel.org>; Fri, 4 Nov 2022 16:01:06 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id i8-20020a170902c94800b0018712ccd6bbso4447202pla.1 for <linux-kernel@vger.kernel.org>; Fri, 04 Nov 2022 16:01:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vzksqxZzngSMnVq4LNIxjbY1eQuHqjVmZsqNyLkHpLQ=; b=BIBsVrxM73h3e1apgZ8GqU8UNyKW2HQO0OE44bbRUUT2OwpePR3ksSeoLDIF2AAppi UIvjXfIZw49dRr5/3v10oZ8lc5T4uPjeV/3CGwjrqgx0buubBD8+N4gLUeuJaN0NYmOG OecG9IMwFWLDFm0hRdU8UOZmOh0C6d++yXgxgfV3JZmREWL5YSlUHX+ExpsRQ1FG2Hug fgm3pE7U7RMfGNdDVyG/8tzKc+SHK18KiVIT6HKOW0XsOvxQa3LGQ9HctRR9Pq3gdd/6 7M5H5aK29XyvTmiBstvkHci0qjyG4M91NxCGr+1PprGcPrD0XVSzWCnoKLmhACWt/u6y zJsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vzksqxZzngSMnVq4LNIxjbY1eQuHqjVmZsqNyLkHpLQ=; b=Or6dpAX2rrQNBsDSS+xyqMFUQZKH29tjlS+BndrKgdaaG2x5p9U8sPQCSknm2btlrm /1YWdpUh2hCzc0H2UM2ZM7J3mZc2RArJo+Npw4NefcHh++P9/0HzD2sVZHa8rPKpi9Vs TQ316UkNPkiAQa8o/EK1RRuwdshBG/SF2GTnIbuQkP2lGLlKPvTKcEi11olKT1E5SC1l zB9Pdtwg4ad/BMeqAss2530JKLFUbvVOGby40mM9jLSbkZPssqRFoZTK46iCpZ3YzI96 D1zTSihHBeQF06qSKDKKQjf955JsYHFqIBBEVIHCw1X0XRhs9Ds5k48/Bh43jR6OymqH FT6w== X-Gm-Message-State: ACrzQf2rN7Ci4n3kjNN2pTzS/ReIqEX6F8Hd1sPexfabgVWb6jwwGDfS FkPX32Ls85p+tmTHBlB15I5osT0iI3JLip8xmDwr6R4L04JD0j3358EnufvUQBFZ7MYzo6TteTm X/FgGgCcHDaOS+0625xlii111eiVlCNGvn7Ah1V6wbw0n2dOwhhsnu26UnAiee7u8KdPg/+WJKO RJ+uSm+yo= X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a17:902:a383:b0:187:34f6:439d with SMTP id x3-20020a170902a38300b0018734f6439dmr22005841pla.35.1667602866059; Fri, 04 Nov 2022 16:01:06 -0700 (PDT) Date: Fri, 4 Nov 2022 23:00:37 +0000 In-Reply-To: <20221104230040.2346862-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221104230040.2346862-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Message-ID: <20221104230040.2346862-2-dionnaglaze@google.com> Subject: [PATCH v8 1/4] crypto: ccp - Name -1 return value as SEV_RET_NO_FW_CALL From: Dionna Glaze <dionnaglaze@google.com> To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Peter Gonda <pgonda@google.com>, Thomas Lendacky <Thomas.Lendacky@amd.com>, Paolo Bonzini <pbonzini@redhat.com>, Joerg Roedel <jroedel@suse.de>, Ingo Molnar <mingo@redhat.com>, Andy Lutomirsky <luto@kernel.org>, John Allen <john.allen@amd.com>, Herbert Xu <herbert@gondor.apana.org.au>, "David S. Miller" <davem@davemloft.net>, Dionna Glaze <dionnaglaze@google.com> Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748608523305130687?= X-GMAIL-MSGID: =?utf-8?q?1748608523305130687?= |
Series |
Add throttling detection to sev-guest
|
|
Commit Message
Dionna Amalie Glaze
Nov. 4, 2022, 11 p.m. UTC
From: Peter Gonda <pgonda@google.com> The PSP can return a "firmware error" code of -1 in circumstances where the PSP is not actually called. To make this protocol unambiguous, we add a constant naming the return value. Cc: Thomas Lendacky <Thomas.Lendacky@amd.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Joerg Roedel <jroedel@suse.de> Cc: Ingo Molnar <mingo@redhat.com> Cc: Andy Lutomirsky <luto@kernel.org> Cc: John Allen <john.allen@amd.com> Cc: Herbert Xu <herbert@gondor.apana.org.au> Cc: "David S. Miller" <davem@davemloft.net> Signed-off-by: Peter Gonda <pgonda@google.com> Signed-off-by: Dionna Glaze <dionnaglaze@google.com> --- drivers/crypto/ccp/sev-dev.c | 2 +- include/uapi/linux/psp-sev.h | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-)
Comments
On 11/4/22 18:00, Dionna Glaze wrote: > From: Peter Gonda <pgonda@google.com> > > The PSP can return a "firmware error" code of -1 in circumstances where > the PSP is not actually called. To make this protocol unambiguous, we > add a constant naming the return value. > > Cc: Thomas Lendacky <Thomas.Lendacky@amd.com> > Cc: Paolo Bonzini <pbonzini@redhat.com> > Cc: Joerg Roedel <jroedel@suse.de> > Cc: Ingo Molnar <mingo@redhat.com> > Cc: Andy Lutomirsky <luto@kernel.org> > Cc: John Allen <john.allen@amd.com> > Cc: Herbert Xu <herbert@gondor.apana.org.au> > Cc: "David S. Miller" <davem@davemloft.net> > > Signed-off-by: Peter Gonda <pgonda@google.com> > Signed-off-by: Dionna Glaze <dionnaglaze@google.com> Looks like you missed my Reviewed-by: from an earlier version, so... Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com> > --- > drivers/crypto/ccp/sev-dev.c | 2 +- > include/uapi/linux/psp-sev.h | 7 +++++++ > 2 files changed, 8 insertions(+), 1 deletion(-) > > diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c > index 06fc7156c04f..97eb3544ab36 100644 > --- a/drivers/crypto/ccp/sev-dev.c > +++ b/drivers/crypto/ccp/sev-dev.c > @@ -444,7 +444,7 @@ static int __sev_platform_init_locked(int *error) > { > struct psp_device *psp = psp_master; > struct sev_device *sev; > - int rc = 0, psp_ret = -1; > + int rc = 0, psp_ret = SEV_RET_NO_FW_CALL; > int (*init_function)(int *error); > > if (!psp || !psp->sev_data) > diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h > index 91b4c63d5cbf..1ad7f0a7e328 100644 > --- a/include/uapi/linux/psp-sev.h > +++ b/include/uapi/linux/psp-sev.h > @@ -36,6 +36,13 @@ enum { > * SEV Firmware status code > */ > typedef enum { > + /* > + * This error code is not in the SEV spec but is added to convey that > + * there was an error that prevented the SEV Firmware from being called. > + * This is (u32)-1 since the firmware error code is represented as a > + * 32-bit integer. > + */ > + SEV_RET_NO_FW_CALL = 0xffffffff, > SEV_RET_SUCCESS = 0, > SEV_RET_INVALID_PLATFORM_STATE, > SEV_RET_INVALID_GUEST_STATE,
On Fri, Nov 04, 2022 at 11:00:37PM +0000, Dionna Glaze wrote: > From: Peter Gonda <pgonda@google.com> > > The PSP can return a "firmware error" code of -1 in circumstances where > the PSP is not actually called. To make this protocol unambiguous, we Please use passive voice in your commit message: no "we" or "I", etc, and describe your changes in imperative mood. > add a constant naming the return value. > > Cc: Thomas Lendacky <Thomas.Lendacky@amd.com> > Cc: Paolo Bonzini <pbonzini@redhat.com> > Cc: Joerg Roedel <jroedel@suse.de> > Cc: Ingo Molnar <mingo@redhat.com> > Cc: Andy Lutomirsky <luto@kernel.org> > Cc: John Allen <john.allen@amd.com> > Cc: Herbert Xu <herbert@gondor.apana.org.au> > Cc: "David S. Miller" <davem@davemloft.net> > > Signed-off-by: Peter Gonda <pgonda@google.com> > Signed-off-by: Dionna Glaze <dionnaglaze@google.com> > --- > drivers/crypto/ccp/sev-dev.c | 2 +- > include/uapi/linux/psp-sev.h | 7 +++++++ > 2 files changed, 8 insertions(+), 1 deletion(-) > > diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c > index 06fc7156c04f..97eb3544ab36 100644 > --- a/drivers/crypto/ccp/sev-dev.c > +++ b/drivers/crypto/ccp/sev-dev.c > @@ -444,7 +444,7 @@ static int __sev_platform_init_locked(int *error) > { > struct psp_device *psp = psp_master; > struct sev_device *sev; > - int rc = 0, psp_ret = -1; > + int rc = 0, psp_ret = SEV_RET_NO_FW_CALL; > int (*init_function)(int *error); > > if (!psp || !psp->sev_data) Ok, lemme chase down this flow here: __sev_platform_init_locked() calls that automatic variable function pointer ->init_function which already looks funky. See the end of this mail for a diff removing it and making the code more readable. The called function can be one of two and both get the pointer to psp_ret as its only argument. 1. __sev_init_ex_locked() calls __sev_do_cmd_locked() and passes down *psp_ret. or 2. __sev_init_locked(). Ditto. Now, __sev_do_cmd_locked() will overwrite psp_ret when sev_wait_cmd_ioc() fails. So far so good. In the case __sev_do_cmd_locked() succeeds, it'll put there something else: if (psp_ret) *psp_ret = reg & PSP_CMDRESP_ERR_MASK; So no caller will ever see SEV_RET_NO_FW_CALL, as far as I can tell. And looking further through the rest of the set, nothing tests SEV_RET_NO_FW_CALL - it only gets assigned. So why are we even bothering with this? You can hand in *psp_ret uninitialized and you'll get a value in all cases. Unless I'm missing an angle. > diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h > index 91b4c63d5cbf..1ad7f0a7e328 100644 > --- a/include/uapi/linux/psp-sev.h > +++ b/include/uapi/linux/psp-sev.h > @@ -36,6 +36,13 @@ enum { > * SEV Firmware status code > */ > typedef enum { > + /* > + * This error code is not in the SEV spec but is added to convey that > + * there was an error that prevented the SEV Firmware from being called. > + * This is (u32)-1 since the firmware error code is represented as a > + * 32-bit integer. > + */ > + SEV_RET_NO_FW_CALL = 0xffffffff, What's wrong with having -1 here? > SEV_RET_SUCCESS = 0, > SEV_RET_INVALID_PLATFORM_STATE, > SEV_RET_INVALID_GUEST_STATE, > -- diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 97eb3544ab36..8bc4209b338b 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -440,12 +440,20 @@ static int __sev_init_ex_locked(int *error) return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error); } +static inline int __sev_do_init_locked(int *psp_ret) +{ + if (sev_init_ex_buffer) + return __sev_init_ex_locked(psp_ret); + else + + return __sev_init_locked(psp_ret); +} + static int __sev_platform_init_locked(int *error) { struct psp_device *psp = psp_master; struct sev_device *sev; - int rc = 0, psp_ret = SEV_RET_NO_FW_CALL; - int (*init_function)(int *error); + int rc = 0, psp_ret; if (!psp || !psp->sev_data) return -ENODEV; @@ -456,15 +464,12 @@ static int __sev_platform_init_locked(int *error) return 0; if (sev_init_ex_buffer) { - init_function = __sev_init_ex_locked; rc = sev_read_init_ex_file(); if (rc) return rc; - } else { - init_function = __sev_init_locked; } - rc = init_function(&psp_ret); + rc = __sev_do_init_locked(&psp_ret); if (rc && psp_ret == SEV_RET_SECURE_DATA_INVALID) { /* * Initialization command returned an integrity check failure @@ -473,9 +478,12 @@ static int __sev_platform_init_locked(int *error) * initialization function should succeed by replacing the state * with a reset state. */ - dev_err(sev->dev, "SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state."); - rc = init_function(&psp_ret); + dev_err(sev->dev, +"SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state."); + + rc = __sev_do_init_locked(&psp_ret); } + if (error) *error = psp_ret; diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 1ad7f0a7e328..a9ed9e846cd2 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -42,7 +42,7 @@ typedef enum { * This is (u32)-1 since the firmware error code is represented as a * 32-bit integer. */ - SEV_RET_NO_FW_CALL = 0xffffffff, + SEV_RET_NO_FW_CALL = -1, SEV_RET_SUCCESS = 0, SEV_RET_INVALID_PLATFORM_STATE, SEV_RET_INVALID_GUEST_STATE,
On Sat, Dec 3, 2022 at 4:26 AM Borislav Petkov <bp@alien8.de> wrote: > > On Fri, Nov 04, 2022 at 11:00:37PM +0000, Dionna Glaze wrote: > > From: Peter Gonda <pgonda@google.com> > > > > The PSP can return a "firmware error" code of -1 in circumstances where > > the PSP is not actually called. To make this protocol unambiguous, we > > Please use passive voice in your commit message: no "we" or "I", etc, > and describe your changes in imperative mood. > > > add a constant naming the return value. > > > > Cc: Thomas Lendacky <Thomas.Lendacky@amd.com> > > Cc: Paolo Bonzini <pbonzini@redhat.com> > > Cc: Joerg Roedel <jroedel@suse.de> > > Cc: Ingo Molnar <mingo@redhat.com> > > Cc: Andy Lutomirsky <luto@kernel.org> > > Cc: John Allen <john.allen@amd.com> > > Cc: Herbert Xu <herbert@gondor.apana.org.au> > > Cc: "David S. Miller" <davem@davemloft.net> > > > > Signed-off-by: Peter Gonda <pgonda@google.com> > > Signed-off-by: Dionna Glaze <dionnaglaze@google.com> > > --- > > drivers/crypto/ccp/sev-dev.c | 2 +- > > include/uapi/linux/psp-sev.h | 7 +++++++ > > 2 files changed, 8 insertions(+), 1 deletion(-) > > > > diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c > > index 06fc7156c04f..97eb3544ab36 100644 > > --- a/drivers/crypto/ccp/sev-dev.c > > +++ b/drivers/crypto/ccp/sev-dev.c > > @@ -444,7 +444,7 @@ static int __sev_platform_init_locked(int *error) > > { > > struct psp_device *psp = psp_master; > > struct sev_device *sev; > > - int rc = 0, psp_ret = -1; > > + int rc = 0, psp_ret = SEV_RET_NO_FW_CALL; > > int (*init_function)(int *error); > > > > if (!psp || !psp->sev_data) > > Ok, lemme chase down this flow here: > > __sev_platform_init_locked() calls that automatic variable function > pointer ->init_function which already looks funky. See the end of this > mail for a diff removing it and making the code more readable. > I'm fine removing it if possible for the sev-dev.c code, but I'll still need the enum for the next patches in this series. I added it specifically because of the uninitialized memory problem with `err` that I witnessed in user space, and to replace the arbitrary 0xff value in existing code. > The called function can be one of two and both get the pointer to > psp_ret as its only argument. > > 1. __sev_init_ex_locked() calls __sev_do_cmd_locked() and passes down > *psp_ret. > > or > > 2. __sev_init_locked(). Ditto. > > Now, __sev_do_cmd_locked() will overwrite psp_ret when > sev_wait_cmd_ioc() fails. So far so good. It doesn't always overwrite psp_ret, such as the initial error checking. The value remains uninitialized for -ENODEV, -EBUSY, -EINVAL. Thus *error in __sev_platform_init_locked can be set to uninitialized memory if psp_ret is not first initialized. That error points to the kernel copy of the user's argument struct, which the ioctl always copies back. In the case of those error codes then, without SEV_RET_NO_FW_CALL, user space will get uninitialized kernel memory. > > In the case __sev_do_cmd_locked() succeeds, it'll put there something > else: > > if (psp_ret) > *psp_ret = reg & PSP_CMDRESP_ERR_MASK; > > So no caller will ever see SEV_RET_NO_FW_CALL, as far as I can tell. > > And looking further through the rest of the set, nothing tests > SEV_RET_NO_FW_CALL - it only gets assigned. > > So why are we even bothering with this? > > You can hand in *psp_ret uninitialized and you'll get a value in all > cases. Unless I'm missing an angle. > I think my above comment points out the wrinkle. > > diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h > > index 91b4c63d5cbf..1ad7f0a7e328 100644 > > --- a/include/uapi/linux/psp-sev.h > > +++ b/include/uapi/linux/psp-sev.h > > @@ -36,6 +36,13 @@ enum { > > * SEV Firmware status code > > */ > > typedef enum { > > + /* > > + * This error code is not in the SEV spec but is added to convey that > > + * there was an error that prevented the SEV Firmware from being called. > > + * This is (u32)-1 since the firmware error code is represented as a > > + * 32-bit integer. > > + */ > > + SEV_RET_NO_FW_CALL = 0xffffffff, > > What's wrong with having -1 here? > C++ brain not trusting what type enum has even in C. I can change it to -1. > > SEV_RET_SUCCESS = 0, > > SEV_RET_INVALID_PLATFORM_STATE, > > SEV_RET_INVALID_GUEST_STATE, > > -- > > diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c > index 97eb3544ab36..8bc4209b338b 100644 > --- a/drivers/crypto/ccp/sev-dev.c > +++ b/drivers/crypto/ccp/sev-dev.c > @@ -440,12 +440,20 @@ static int __sev_init_ex_locked(int *error) > return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error); > } > > +static inline int __sev_do_init_locked(int *psp_ret) > +{ > + if (sev_init_ex_buffer) > + return __sev_init_ex_locked(psp_ret); > + else > + > + return __sev_init_locked(psp_ret); > +} > + > static int __sev_platform_init_locked(int *error) > { > struct psp_device *psp = psp_master; > struct sev_device *sev; > - int rc = 0, psp_ret = SEV_RET_NO_FW_CALL; > - int (*init_function)(int *error); > + int rc = 0, psp_ret; > > if (!psp || !psp->sev_data) > return -ENODEV; > @@ -456,15 +464,12 @@ static int __sev_platform_init_locked(int *error) > return 0; > > if (sev_init_ex_buffer) { > - init_function = __sev_init_ex_locked; > rc = sev_read_init_ex_file(); > if (rc) > return rc; > - } else { > - init_function = __sev_init_locked; > } > > - rc = init_function(&psp_ret); > + rc = __sev_do_init_locked(&psp_ret); > if (rc && psp_ret == SEV_RET_SECURE_DATA_INVALID) { > /* > * Initialization command returned an integrity check failure > @@ -473,9 +478,12 @@ static int __sev_platform_init_locked(int *error) > * initialization function should succeed by replacing the state > * with a reset state. > */ > - dev_err(sev->dev, "SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state."); > - rc = init_function(&psp_ret); > + dev_err(sev->dev, > +"SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state."); > + > + rc = __sev_do_init_locked(&psp_ret); > } > + > if (error) > *error = psp_ret; > > diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h > index 1ad7f0a7e328..a9ed9e846cd2 100644 > --- a/include/uapi/linux/psp-sev.h > +++ b/include/uapi/linux/psp-sev.h > @@ -42,7 +42,7 @@ typedef enum { > * This is (u32)-1 since the firmware error code is represented as a > * 32-bit integer. > */ > - SEV_RET_NO_FW_CALL = 0xffffffff, > + SEV_RET_NO_FW_CALL = -1, > SEV_RET_SUCCESS = 0, > SEV_RET_INVALID_PLATFORM_STATE, > SEV_RET_INVALID_GUEST_STATE, > > -- > Regards/Gruss, > Boris. > > https://people.kernel.org/tglx/notes-about-netiquette
On Sat, Dec 03, 2022 at 10:58:39AM -0800, Dionna Amalie Glaze wrote: > It doesn't always overwrite psp_ret, such as the initial error checking. > The value remains uninitialized for -ENODEV, -EBUSY, -EINVAL. > Thus *error in __sev_platform_init_locked can be set to uninitialized > memory if psp_ret is not first initialized. Lemme see if I understand it correctly: you wanna signal that all early return cases in __sev_do_cmd_locked() are such that no firmware was called? I.e., everything before the first iowrite into the command buffer? But then the commit message says: "The PSP can return a "firmware error" code of -1 in circumstances where the PSP is not actually called." which is confusing. How can the PSP return something if it wasn't called? Or you mean those cases above where it would fail on some of the checks before issuing a SEV command? I think you do... So I see Tom has ACKed this but I have to ask: is the SEV spec not going to use -1 ever? Also, if this behavior is going to be user-visible, where are we documenting it? Especially if nothing in the kernel is looking at that value but only assigning it to a retval which gets looked at by userspace. Especially then this should be documented. Dunno, maybe somewhere in Documentation/x86/amd-memory-encryption.rst or maybe Tom would have a better idea. > That error points to the kernel copy of the user's argument struct, > which the ioctl always copies back. In the case of those error codes > then, without SEV_RET_NO_FW_CALL, user space will get uninitialized > kernel memory. Right, but having a return value which means "firmware wasn't called" sounds weird. Why does userspace care? I mean, you can just as well return any of the negative values -ENODEV, -EBUSY, -EINVAL too, depending on where you exit. Having three different retvals could tell you where exactly it failed, even. But the question remains: why does userspace needs to know that the failure happened and firmware wasn't called, as long as it is getting something negative to signal an error? Thx.
On Sat, Dec 3, 2022 at 11:37 AM Borislav Petkov <bp@alien8.de> wrote: > > On Sat, Dec 03, 2022 at 10:58:39AM -0800, Dionna Amalie Glaze wrote: > > It doesn't always overwrite psp_ret, such as the initial error checking. > > The value remains uninitialized for -ENODEV, -EBUSY, -EINVAL. > > Thus *error in __sev_platform_init_locked can be set to uninitialized > > memory if psp_ret is not first initialized. > > Lemme see if I understand it correctly: you wanna signal that all early > return cases in __sev_do_cmd_locked() are such that no firmware was > called? > > I.e., everything before the first iowrite into the command buffer? > > But then the commit message says: > > "The PSP can return a "firmware error" code of -1 in circumstances where > the PSP is not actually called." > > which is confusing. How can the PSP return something if it wasn't called? > > Or you mean those cases above where it would fail on some of the checks > before issuing a SEV command? I think you do... > > So I see Tom has ACKed this but I have to ask: is the SEV spec not going > to use -1 ever? > I'll confirm with Tom, since he's changing the GHCB spec for the throttling value. > Also, if this behavior is going to be user-visible, where are we > documenting it? Especially if nothing in the kernel is looking at > that value but only assigning it to a retval which gets looked at by > userspace. Especially then this should be documented. > > Dunno, maybe somewhere in Documentation/x86/amd-memory-encryption.rst or > maybe Tom would have a better idea. > Agreed it should be in both the Linux documentation and the GHCB spec. > > That error points to the kernel copy of the user's argument struct, > > which the ioctl always copies back. In the case of those error codes > > then, without SEV_RET_NO_FW_CALL, user space will get uninitialized > > kernel memory. > > Right, but having a return value which means "firmware wasn't called" > sounds weird. Why does userspace care? > Arguably it shouldn't ever get this value. We're just not very selective when we copy back the kernel copy of the ioctl argument. In all cases user space should treat the value as undefined, but still we don't want to leak uninitialized kernel stack values. Host driver: only on platform init, should just see the negative error value and not try to interpret the fw_err in the argument. Still the data is copied back and therefore should not be uninitialized kernel memory. Possible name: SEV_RET_UNDEFINED, or a return value -1 anyway with a comment that the argument is undefined. Guest driver: The host is issuing a guest request on behalf of the guest using patch 4/4 of this series. The guest is responsible for keeping the sequence number in sync with the PSP, so we want to track if the ghcb_hv_call completed successfully to know we should continue with the incremented IV. Otherwise we run the risk of the sequence numbers getting out of sync and we lock down the VMPCK. The guest driver actually sets exitinfo2 to an undocumented 0xff initial value just in case. =If the host doesn't write back a documented EXIT_INFO_2 value like invalid_len or throttled, then the kernel will emit a log with the initial value 0xff (or -1 after this patch). I've changed it to -1 to name the same kind of error across host and guest: the communication with the PSP didn't complete successfully, so the "error" value is not from the PSP. This value can also get returned to user space during a -ENOTTY result. We can call this NO_FW_CALL or UNDEFINED. I have no real preference. Whatever value we set initially, the VMM can overwrite exitinfo2 during the ghcb_hv_call. I'd rather that the "undefined" values were the same across both, because the guest is merely receiving a value from the host's PSP driver (or should be). It keeps the enum for return values a bit tidier and not concerned with whether the value is viewed from the host or guest. I can see an argument for not using the PSP header for its enum type and instead defining and documenting and using the separate the 0xff value elsewhere, but this seemed as good a place as any. > I mean, you can just as well return any of the negative values -ENODEV, > -EBUSY, -EINVAL too, depending on where you exit. Having three different > retvals could tell you where exactly it failed, even. > That's true, those values are already being returned to user space as the result of the ioctl. > But the question remains: why does userspace needs to know that the > failure happened and firmware wasn't called, as long as it is getting > something negative to signal an error? > I hope the above discussion is clear that it's purely a defined "undefined" because being pickier about what to copy_to_user during exceptional circumstances in order to not overwrite the user's fw_err value seems an unnecessary amount of code. > Thx. > > -- > Regards/Gruss, > Boris. > > https://people.kernel.org/tglx/notes-about-netiquette
On 12/5/22 11:05, Dionna Amalie Glaze wrote: > On Sat, Dec 3, 2022 at 11:37 AM Borislav Petkov <bp@alien8.de> wrote: >> >> On Sat, Dec 03, 2022 at 10:58:39AM -0800, Dionna Amalie Glaze wrote: >>> It doesn't always overwrite psp_ret, such as the initial error checking. >>> The value remains uninitialized for -ENODEV, -EBUSY, -EINVAL. >>> Thus *error in __sev_platform_init_locked can be set to uninitialized >>> memory if psp_ret is not first initialized. >> >> Lemme see if I understand it correctly: you wanna signal that all early >> return cases in __sev_do_cmd_locked() are such that no firmware was >> called? >> >> I.e., everything before the first iowrite into the command buffer? >> >> But then the commit message says: >> >> "The PSP can return a "firmware error" code of -1 in circumstances where >> the PSP is not actually called." >> >> which is confusing. How can the PSP return something if it wasn't called? >> >> Or you mean those cases above where it would fail on some of the checks >> before issuing a SEV command? I think you do... >> >> So I see Tom has ACKed this but I have to ask: is the SEV spec not going >> to use -1 ever? >> > > I'll confirm with Tom, since he's changing the GHCB spec for the > throttling value. The SEV API error codes are 16-bits in size, so you'll never see a -1. > >> Also, if this behavior is going to be user-visible, where are we >> documenting it? Especially if nothing in the kernel is looking at >> that value but only assigning it to a retval which gets looked at by >> userspace. Especially then this should be documented. >> >> Dunno, maybe somewhere in Documentation/x86/amd-memory-encryption.rst or >> maybe Tom would have a better idea. >> > > Agreed it should be in both the Linux documentation and the GHCB spec. Linux documentation, yes, GHCB spec, no. Thanks, Tom > >>> That error points to the kernel copy of the user's argument struct, >>> which the ioctl always copies back. In the case of those error codes >>> then, without SEV_RET_NO_FW_CALL, user space will get uninitialized >>> kernel memory. >> >> Right, but having a return value which means "firmware wasn't called" >> sounds weird. Why does userspace care? >> > > Arguably it shouldn't ever get this value. We're just not very > selective when we copy back the kernel copy of the ioctl argument. > In all cases user space should treat the value as undefined, but still > we don't want to leak uninitialized kernel stack values. > > Host driver: only on platform init, should just see the negative error > value and not try to interpret the fw_err in the argument. > Still the data is copied back and therefore should not be > uninitialized kernel memory. > Possible name: SEV_RET_UNDEFINED, or a return value -1 anyway with a > comment that the argument is undefined. > > Guest driver: The host is issuing a guest request on behalf of the > guest using patch 4/4 of this series. > The guest is responsible for keeping the sequence number in sync with > the PSP, so we want to track if the ghcb_hv_call completed > successfully to know we should continue with the incremented IV. > Otherwise we run the risk of the sequence numbers getting out of sync > and we lock down the VMPCK. > > The guest driver actually sets exitinfo2 to an undocumented 0xff > initial value just in case. > =If the host doesn't write back a documented EXIT_INFO_2 value like > invalid_len or throttled, then the kernel will emit a log with the > initial value 0xff (or -1 after this patch). > > I've changed it to -1 to name the same kind of error across host and > guest: the communication with the PSP didn't complete successfully, so > the "error" value is not from the PSP. > This value can also get returned to user space during a -ENOTTY result. > We can call this NO_FW_CALL or UNDEFINED. I have no real preference. > > Whatever value we set initially, the VMM can overwrite exitinfo2 > during the ghcb_hv_call. > I'd rather that the "undefined" values were the same across both, > because the guest is merely receiving a value from the host's PSP > driver (or should be). > It keeps the enum for return values a bit tidier and not concerned > with whether the value is viewed from the host or guest. > > I can see an argument for not using the PSP header for its enum type > and instead defining and documenting and using the separate the 0xff > value elsewhere, but this seemed as good a place as any. > > >> I mean, you can just as well return any of the negative values -ENODEV, >> -EBUSY, -EINVAL too, depending on where you exit. Having three different >> retvals could tell you where exactly it failed, even. >> > > That's true, those values are already being returned to user space as > the result of the ioctl. > >> But the question remains: why does userspace needs to know that the >> failure happened and firmware wasn't called, as long as it is getting >> something negative to signal an error? >> > > I hope the above discussion is clear that it's purely a defined > "undefined" because being pickier about what to copy_to_user during > exceptional circumstances in order to not overwrite the user's fw_err > value seems an unnecessary amount of code. > >> Thx. >> >> -- >> Regards/Gruss, >> Boris. >> >> https://people.kernel.org/tglx/notes-about-netiquette > > >
On Mon, Dec 05, 2022 at 09:05:19AM -0800, Dionna Amalie Glaze wrote: > Arguably it shouldn't ever get this value. We're just not very > selective when we copy back the kernel copy of the ioctl argument. > In all cases user space should treat the value as undefined, but still > we don't want to leak uninitialized kernel stack values. Absolutely. > I've changed it to -1 to name the same kind of error across host and > guest: the communication with the PSP didn't complete successfully, so > the "error" value is not from the PSP. > This value can also get returned to user space during a -ENOTTY result. > We can call this NO_FW_CALL or UNDEFINED. I have no real preference. Me neither as long as this is written down and agreed upon as a possible value and not leaking kernel stack. > Whatever value we set initially, the VMM can overwrite exitinfo2 > during the ghcb_hv_call. > I'd rather that the "undefined" values were the same across both, > because the guest is merely receiving a value from the host's PSP > driver (or should be). > It keeps the enum for return values a bit tidier and not concerned > with whether the value is viewed from the host or guest. Ack. ... > I hope the above discussion is clear that it's purely a defined > "undefined" because being pickier about what to copy_to_user during > exceptional circumstances in order to not overwrite the user's fw_err > value seems an unnecessary amount of code. Ok, I think we're on the same page. So pls document that NO_FW_CALL or so value and what it means and that thing should be taken care of. Thx.
diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 06fc7156c04f..97eb3544ab36 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -444,7 +444,7 @@ static int __sev_platform_init_locked(int *error) { struct psp_device *psp = psp_master; struct sev_device *sev; - int rc = 0, psp_ret = -1; + int rc = 0, psp_ret = SEV_RET_NO_FW_CALL; int (*init_function)(int *error); if (!psp || !psp->sev_data) diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 91b4c63d5cbf..1ad7f0a7e328 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -36,6 +36,13 @@ enum { * SEV Firmware status code */ typedef enum { + /* + * This error code is not in the SEV spec but is added to convey that + * there was an error that prevented the SEV Firmware from being called. + * This is (u32)-1 since the firmware error code is represented as a + * 32-bit integer. + */ + SEV_RET_NO_FW_CALL = 0xffffffff, SEV_RET_SUCCESS = 0, SEV_RET_INVALID_PLATFORM_STATE, SEV_RET_INVALID_GUEST_STATE,