From patchwork Sun Nov 5 11:34:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 10042105 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B6B6560247 for ; Sun, 5 Nov 2017 11:34:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 98D6329305 for ; Sun, 5 Nov 2017 11:34:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8C4A3294C4; Sun, 5 Nov 2017 11:34:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3C16429305 for ; Sun, 5 Nov 2017 11:34:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751409AbdKELe3 (ORCPT ); Sun, 5 Nov 2017 06:34:29 -0500 Received: from mail.skyhub.de ([5.9.137.197]:55438 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750751AbdKELe1 (ORCPT ); Sun, 5 Nov 2017 06:34:27 -0500 X-Virus-Scanned: Nedap ESD1 at mail.skyhub.de Received: from mail.skyhub.de ([127.0.0.1]) by localhost (blast.alien8.de [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id ay1cnH6re5eX; Sun, 5 Nov 2017 12:34:26 +0100 (CET) Received: from pd.tnic (p2003008C2F261400D19A513C02A1D3AF.dip0.t-ipconnect.de [IPv6:2003:8c:2f26:1400:d19a:513c:2a1:d3af]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id CF0311EC02FF; Sun, 5 Nov 2017 12:34:25 +0100 (CET) Date: Sun, 5 Nov 2017 12:34:12 +0100 From: Borislav Petkov To: Brijesh Singh Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini , Radim =?utf-8?B?S3LEjW3DocWZ?= , Herbert Xu , Gary Hook , Tom Lendacky , linux-crypto@vger.kernel.org Subject: Re: [Part2 PATCH v7 20/38] crypto: ccp: Implement SEV_PDH_CERT_EXPORT ioctl command Message-ID: <20171105113412.2xtuifkbmxwrf3ji@pd.tnic> References: <20171101211623.71496-1-brijesh.singh@amd.com> <20171101211623.71496-21-brijesh.singh@amd.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20171101211623.71496-21-brijesh.singh@amd.com> User-Agent: NeoMutt/20170609 (1.8.3) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Wed, Nov 01, 2017 at 04:16:05PM -0500, Brijesh Singh wrote: > The SEV_PDH_CERT_EXPORT command can be used to export the PDH and its > certificate chain. The command is defined in SEV spec section 5.10. ... > --- > drivers/crypto/ccp/psp-dev.c | 98 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 98 insertions(+) Fixes ontop: * !input.cert_chain_address test was repeated. I saw that by aligning them vertically, i.e., after making it more readable, the repetition became obvious. * Do the lengths checks first and the access_ok after, in each PDH and cert chain test. * Do the checks first and the allocations after, not interleaved. * Comments are sentences which should end with a '.' (hunk below contains also that &psp_master->cmd_buf change but you're going to remove that arg anyway). diff --git a/drivers/crypto/ccp/psp-dev.c b/drivers/crypto/ccp/psp-dev.c index 09f69e573d00..d3fdcce61e6d 100644 --- a/drivers/crypto/ccp/psp-dev.c +++ b/drivers/crypto/ccp/psp-dev.c @@ -455,14 +455,22 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp) if (!data) return -ENOMEM; - /* Userspace wants to query the certificate length */ - if (!input.pdh_cert_address || !input.pdh_cert_len || - !input.cert_chain_address || !input.cert_chain_address) + /* Userspace wants to query the certificate length. */ + if (!input.pdh_cert_address || + !input.pdh_cert_len || + !input.cert_chain_address) goto cmd; - /* allocate a physically contiguous buffer to store the PDH blob */ - if (!access_ok(VERIFY_WRITE, input.pdh_cert_address, input.pdh_cert_len) || - (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE)) { + /* allocate a physically contiguous buffer to store the PDH blob. */ + if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) || + !access_ok(VERIFY_WRITE, input.pdh_cert_address, input.pdh_cert_len)) { + ret = -EFAULT; + goto e_free; + } + + /* allocate a physically contiguous buffer to store the cert chain blob. */ + if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) || + !access_ok(VERIFY_WRITE, input.cert_chain_address, input.cert_chain_len)) { ret = -EFAULT; goto e_free; } @@ -476,13 +484,6 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp) data->pdh_cert_address = __psp_pa(pdh_blob); data->pdh_cert_len = input.pdh_cert_len; - /* allocate a physically contiguous buffer to store the cert chain blob */ - if (!access_ok(VERIFY_WRITE, input.cert_chain_address, input.cert_chain_len) || - (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE)) { - ret = -EFAULT; - goto e_free_pdh; - } - cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL); if (!cert_blob) { ret = -ENOMEM; @@ -493,18 +494,16 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp) data->cert_chain_len = input.cert_chain_len; cmd: - /* If platform is not in INIT state then transition it to INIT */ + /* If platform is not in INIT state then transition it to INIT. */ if (psp_master->sev_state != SEV_STATE_INIT) { - ret = __sev_platform_init_locked(psp_master->sev_init, &argp->error); + ret = __sev_platform_init_locked(&psp_master->cmd_buf, &argp->error); if (ret) goto e_free_cert; } ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error); - /* - * If we query the length, FW responded with expected data - */ + /* If we query the length, FW responded with expected data. */ input.cert_chain_len = data->cert_chain_len; input.pdh_cert_len = data->pdh_cert_len;