From patchwork Fri Nov 4 23:00:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 15856 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp688140wru; Fri, 4 Nov 2022 16:05:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5y3p56TS7DI0MWTsjLr3Unyzo+tgpidOScJ+VpdHmhUJh7NpK7Iza8gmfAJG/YQt4LxCbK X-Received: by 2002:aa7:da84:0:b0:463:7873:119a with SMTP id q4-20020aa7da84000000b004637873119amr26815536eds.329.1667603102394; Fri, 04 Nov 2022 16:05:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667603102; cv=none; d=google.com; s=arc-20160816; b=sfqwmLq6R6NP7j0v+Q1EK/Nzauw0tXGVZvI4Bn8dg7AL15jSjap8Nef9wPmTC4GlJv VXoLMFlm0FVSgFqkN94f8gG6gZOHmLPH2Z7QpTW78C/Yfs4Mzgr3ZmMsYbBud+SOVELB vu57OJGUlFaNZI+DiO+OJ5xN3F53/7AuauEMKK5Qmam7Vt20GmneGDxdluMwubLoUSv/ j+gm8Ko4lqm8dBtjWFegLsx8Xyhm+K2/mrZV5ysGLnQjUgaNRgfLkk+V5/5jchuY/edF 7MqYfUb39Wi5LY7MT7TrxuBOAJ7Hlg6F1K9djBlUf0d5R02iNVzTykaJKtfvAu/KuK79 FrxA== 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=cdK/VRyCPwKThqqRcg2usy4G2FHLW3I2lPiI0d9S9hA=; b=NSvDgJ9vBEkvh1H+nd9C2UpT6Aw24wCT00d2QvpwTh0NdHdj68O2Ba0azDmvEIc3RO 9HvHF+Wx95EFxfmZLUEtW+cVr3vCDPm1BXSk4wG9PRDiiQ8pEF4KvyA6pISf6VB5U81r D0qkuZSSORJEu02lx7XWrKMI8QqqI5OmZ8a6cWGTx9DFw7wr0j1yIieXdIFAWZnCoHPX OvsWMuqE2bjYzeePpJW25igM50FFymR4KUYDOp6V1NlKiqAXR6NV82hEgxvIyf3C5KXd gkCeCPsFgpp2z0ri3iE9VVRQoLkc82FvFmIetFsQQmJadUrVXC2oBl45h3KT62mp4r7j IfmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=C7B2GiAC; 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 u18-20020a170906b11200b00780f0b45416si264624ejy.622.2022.11.04.16.04.38; Fri, 04 Nov 2022 16:05:02 -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=C7B2GiAC; 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 S229662AbiKDXB3 (ORCPT + 99 others); Fri, 4 Nov 2022 19:01:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbiKDXBM (ORCPT ); Fri, 4 Nov 2022 19:01:12 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D589142F6A for ; Fri, 4 Nov 2022 16:01:10 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id t3-20020a170902e84300b00186ab03043dso4504759plg.20 for ; Fri, 04 Nov 2022 16:01:10 -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=cdK/VRyCPwKThqqRcg2usy4G2FHLW3I2lPiI0d9S9hA=; b=C7B2GiACclUr4bSg9eCOuhvdwXQYkHErdquyDfYepWM9YWiqvNxBWnp29ohwvvLYN4 GnDjmvJX5GD3n177DQptp7GYjzQGqISK8F8yU4xsUd6iy1ZGj6NqGpebAGtNt4RFPK4T IE+V7+os77Wmpj6D91LoVxk5YMR3Y2p5d1/WWEUo7glcCyu66lPXC3r5o0LW3+IAsmDg nWh0zX9F1uzSo0YmRLBjGB6e7YoRxPB4FeH4lheU7UbCfRTDwws40b9ax/2P5LCCVBLX sXJq81RnQjUEaF9nSzceSAXsNNUQ+G7bOnIXJ4broNDCJEzpfGZJGFeYkBVSri4SSF35 BcCA== 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=cdK/VRyCPwKThqqRcg2usy4G2FHLW3I2lPiI0d9S9hA=; b=MW2Yl6cXZNNH8RFX8OJH+IP+zxm+kJVdrtW7+tZoM7YZLctE+3cf/Xu36RVAQrILus QzK9aAt8/QK0/3ru0Ks5VQO3O9U0aVZUXG2t2zcoJFqiRQ3JtR+fq2r7TzCZdTTB77tW WzzaWG0ONhv6pwekfgSXtVu3JjwU7toshlfSopBl8pZ/s4keV2jNO7yFIOy1YucWNlYv +KlgEQ5VoYqUlQREiSeToTFdjK9Fbd+Ndxb1G8ZnXAsNYhD4PRy7It1zQXCyCgTavY26 JPbE36rySvBYR1okWJcu7u1kkIyuUzxN5/tiFqRv7lFD9SbmyIR0zckKyTGBS1nl5uG1 oBwg== X-Gm-Message-State: ACrzQf3tgJS8SanrNWaFgyOjoHg89yTdhouHy6xWZA8u7u+17gQTvzoB rBDuKpxzHoRPuarjIzKS0qk9CqpOA36XRC2KpF9bRN/gke3bO2SKe1wJCywvxFvTcqJVjVQd8pR R/o6TmoDBo26N5hdNODuC7f4bssHWz786goaBqoITcZX1yR2KNHE2xep/bHHU0wH91HWEBdXWrD 6DipELZA4= X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a17:90b:1881:b0:213:1f29:2d0a with SMTP id mn1-20020a17090b188100b002131f292d0amr396847pjb.154.1667602869918; Fri, 04 Nov 2022 16:01:09 -0700 (PDT) Date: Fri, 4 Nov 2022 23:00:39 +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-4-dionnaglaze@google.com> Subject: [PATCH v8 3/4] virt: sev-guest: Remove err in handle_guest_request From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Dionna Glaze , Tom Lendacky , Paolo Bonzini , Joerg Roedel , Peter Gonda , Thomas Gleixner , Dave Hansen , Borislav Petkov , Haowen Bai , Liam Merwick , Yang Yingliang 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748608590633127700?= X-GMAIL-MSGID: =?utf-8?q?1748608590633127700?= 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. The fw_err argument is better to just pass through to snp_issue_guest_request, so we do that by passing along the ioctl argument. This removes the need for an argument to handle_guest_request. Cc: Tom Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Peter Gonda Cc: Thomas Gleixner Cc: Dave Hansen Cc: Borislav Petkov Cc: Haowen Bai Cc: Liam Merwick Cc: Yang Yingliang Fixes: fce96cf04430 ("virt: Add SEV-SNP guest driver") Signed-off-by: Dionna Glaze Reviewed-by: Peter Gonda Reviewed-by: Tom Lendacky --- drivers/virt/coco/sev-guest/sev-guest.c | 37 ++++++++++++------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index d08ff87c2dac..5615d349b378 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -320,11 +320,11 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 return __enc_payload(snp_dev, req, payload, sz); } -static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, int msg_ver, +static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, + struct snp_guest_request_ioctl *arg, u8 type, void *req_buf, size_t req_sz, void *resp_buf, - u32 resp_sz, __u64 *fw_err) + u32 resp_sz) { - unsigned long err; u64 seqno; int rc; @@ -336,12 +336,14 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); /* Encrypt the userspace provided payload */ - rc = enc_payload(snp_dev, seqno, msg_ver, type, req_buf, req_sz); + rc = enc_payload(snp_dev, seqno, arg->msg_version, type, req_buf, + req_sz); if (rc) return rc; /* Call firmware to process the request */ - rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err); + rc = snp_issue_guest_request(exit_code, &snp_dev->input, + &arg->fw_err); /* * If the extended guest request fails due to having to small of a @@ -349,23 +351,21 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in * extended data request. */ if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST && - err == SNP_GUEST_REQ_INVALID_LEN) { + arg->fw_err == SNP_GUEST_REQ_INVALID_LEN) { const unsigned int certs_npages = snp_dev->input.data_npages; exit_code = SVM_VMGEXIT_GUEST_REQUEST; - rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err); + rc = snp_issue_guest_request(exit_code, &snp_dev->input, + &arg->fw_err); - err = SNP_GUEST_REQ_INVALID_LEN; + arg->fw_err = SNP_GUEST_REQ_INVALID_LEN; snp_dev->input.data_npages = certs_npages; } - if (fw_err) - *fw_err = err; - if (rc) { dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err: %llu\n", - rc, *fw_err); + rc, arg->fw_err); goto disable_vmpck; } @@ -412,9 +412,9 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io if (!resp) return -ENOMEM; - rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_version, + rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg, SNP_MSG_REPORT_REQ, &req, sizeof(req), resp->data, - resp_len, &arg->fw_err); + resp_len); if (rc) goto e_free; @@ -452,9 +452,8 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque if (copy_from_user(&req, (void __user *)arg->req_data, sizeof(req))) return -EFAULT; - rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_version, - SNP_MSG_KEY_REQ, &req, sizeof(req), buf, resp_len, - &arg->fw_err); + rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg, + SNP_MSG_KEY_REQ, &req, sizeof(req), buf, resp_len); if (rc) return rc; @@ -514,9 +513,9 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques return -ENOMEM; snp_dev->input.data_npages = npages; - ret = handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg->msg_version, + ret = handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg, SNP_MSG_REPORT_REQ, &req.data, - sizeof(req.data), resp->data, resp_len, &arg->fw_err); + sizeof(req.data), resp->data, resp_len); /* If certs length is invalid then copy the returned length */ if (arg->fw_err == SNP_GUEST_REQ_INVALID_LEN) {