Message ID | 20231030063652.68675-6-nikunj@amd.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp2018190vqb; Sun, 29 Oct 2023 23:38:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IErPf/MKEeZq8j0OYlf0aFQRlf6evWLcNsAD5+fayzMulfiS9Qq8S2T2wKGJcz9+zkf+IP4 X-Received: by 2002:a05:6359:2e01:b0:169:92d:64c5 with SMTP id ro1-20020a0563592e0100b00169092d64c5mr6620997rwb.32.1698647922533; Sun, 29 Oct 2023 23:38:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698647922; cv=pass; d=google.com; s=arc-20160816; b=XkHu791ixc6nTj1nZy2TdJyE8rO00P5RLBoW5VHaBE/b5PLFLNa9VbNAZ1TzfSAmsk 9Z71BhcpWXlIlX/Dkq/+rsnQpyjM5Qf0amSSPVNSr8S+Xqm8jU9dPWN18shxoxGbUkdV Wq+mNp7B/xh4JkgZO6RDRrWNSL5eMmmlM2MikEEkPw+BKMFzXFhaPMtXaB8A9DO/QXV6 zDCkmAxyT2lZ0dgiyE0yFez7KbbjKf4srURxVxerJd+/oF1pwLadIXVfegV7S71Bnym6 dPOXhis0PU+uE4RxDQhtzTQ2yk9JHOsKnye+0TXw6thp3t8HGGvEOB89OxBRauNjDAwO vIig== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=H5C9YlLNWvr9aZipyWk6dgDei/vUf8ig2OfXDucLg9I=; fh=GrBYTfaCmKSBoJ0OQBiloTDpWB/NcGHvBdTqN4MlWVs=; b=d2mA8502psLZY2fc+1/1gsr5CX4lyRk1PYWamcHkSMc4B7zV4MMOYgNeXw1sjF/6YV 5zMaAvQ3HpzonNjZm7simhksfV3ozkarn5eAmMdwOyPFiIbE1Cp124TCubrrunvddCId VulbAFuG9RsNpUt/3DB/UoEvPwD62LviuxEiRJ59roMDCa3iJEVjsbQ26xWBDwTpIMDf purhQWKGQOgDM4Em02GKhQUxi4qSPzL5iBPYRYB+1ptQadpDY7OWG/nqmQ3enVTwykRQ hSpsDERPmS6Md1N73dGBKqZYXP1Vx9Aa51c2b6/mmvp89XLS+NGCNPhyIZRdZK0b2zqi GCZw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=VL9YDlKp; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id 76-20020a63024f000000b005b999968b85si899508pgc.666.2023.10.29.23.38.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 23:38:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=VL9YDlKp; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 353E180C034A; Sun, 29 Oct 2023 23:38:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231824AbjJ3Gi3 (ORCPT <rfc822;chrisjones.unixmen@gmail.com> + 32 others); Mon, 30 Oct 2023 02:38:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231743AbjJ3GiX (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 30 Oct 2023 02:38:23 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D2D9109; Sun, 29 Oct 2023 23:38:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GCSrC4Csnv/MoH1Re/cL3xh75R2zpptWlZEiY4sRqIDzOhKLk2yKxUAfHYD4IIt1Ei/rnyoj4qG2pQYiqVpLMy38+ERT6HVJGRIGufcJ9AAiICaqeSIKL4ff3oRhqGUxFSJPmzMgCtdsWzVEYdbLgI5OOgZPimHelW5objEs8UMh3g58NH4JAvHBeKrZ9aY8Wnb9kiCZkrVhnSZT6ubnCuqiDXd5ZbBxm6KjuFuT6O4Ugvl2rPFBUebQ9lf46TCWiawzYqSas5VDfo+bMu90VMzsIrimAwipvcG02rKnuLgauSADJNqM4XJNm2ZL2D79ij9EA6KF5ckjMxcQXZ+r6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=H5C9YlLNWvr9aZipyWk6dgDei/vUf8ig2OfXDucLg9I=; b=lSoxohTKZhAxCFItCLIn6JQZHaOTOHZGa1T5iyNI6cT7ddeo5W7NsMXddWZb/KmyohfZbBuJK/pu2HDNztImtq+a2GsRNnYhs4MheRsJpvqZOqh6foJH7MPbj8pdqULJmO1UbEbdTZD8hA0WSneGxqCnQNcU8AeCUJ0mp1RyUhHua+Vt98r337L7AqKPU7Y+gRrrFmF+ghkQwzkxaReStL069yD/4ZzHTpOfi1/vLfUX6nQqaEnIW9u+1iRjcRRWzt+2fNSffldRMdv+dzuNpy77cFqIl+PNyk21ZbJVXkH8gD3Nv3axohdZXe0xP3+zHgq8pqrKj3c9w3oedreeIg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=H5C9YlLNWvr9aZipyWk6dgDei/vUf8ig2OfXDucLg9I=; b=VL9YDlKpElcWVbfctLJtIBzZnG4gvOpuWh60bVA8P/2PhMG4YTEPDfusCVa6FXFqD+GM+5E+DOC/0CzQGE0XYuHJmTQ4lmuO8rSpa9RlkTQXEo/WNxgkab2i17PrIT7Icak+noTjCpdQvtITebEmthM64Ris+4STnbKx3wPTL+4= Received: from MN2PR20CA0065.namprd20.prod.outlook.com (2603:10b6:208:235::34) by DM4PR12MB6112.namprd12.prod.outlook.com (2603:10b6:8:aa::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.22; Mon, 30 Oct 2023 06:38:14 +0000 Received: from BL6PEPF0001AB73.namprd02.prod.outlook.com (2603:10b6:208:235:cafe::6a) by MN2PR20CA0065.outlook.office365.com (2603:10b6:208:235::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.28 via Frontend Transport; Mon, 30 Oct 2023 06:38:14 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BL6PEPF0001AB73.mail.protection.outlook.com (10.167.242.166) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6933.22 via Frontend Transport; Mon, 30 Oct 2023 06:38:14 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Mon, 30 Oct 2023 01:38:09 -0500 From: Nikunj A Dadhania <nikunj@amd.com> To: <linux-kernel@vger.kernel.org>, <thomas.lendacky@amd.com>, <x86@kernel.org>, <kvm@vger.kernel.org> CC: <bp@alien8.de>, <mingo@redhat.com>, <tglx@linutronix.de>, <dave.hansen@linux.intel.com>, <dionnaglaze@google.com>, <pgonda@google.com>, <seanjc@google.com>, <pbonzini@redhat.com>, <nikunj@amd.com> Subject: [PATCH v5 05/14] virt: sev-guest: Add vmpck_id to snp_guest_dev struct Date: Mon, 30 Oct 2023 12:06:43 +0530 Message-ID: <20231030063652.68675-6-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231030063652.68675-1-nikunj@amd.com> References: <20231030063652.68675-1-nikunj@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL6PEPF0001AB73:EE_|DM4PR12MB6112:EE_ X-MS-Office365-Filtering-Correlation-Id: 1725621f-884c-4c5d-5310-08dbd912cb16 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: X2gSu3C29TmlVWgwd88BoUPP5hG5dkERh02HGNft0GNxTgDIzvSc2RCmD1cKMtb0ntyEYiajToW0hZQBLlDiaL7CvDseb+q6RrtTNeEw9Y0PVHgsJ/nG2kiywugggZM1Xtys2UT2I3+JpPGxcHEi/gZz/O4Uy22r8pTk0nzUxfszFM6ASuEEVyBuJEx+i/p65dU136jobyx2mmAQW4ZyVulRt+dOV/ZBreaZ+AlxMqXfFugDyuUERw0SJ84KIXFdoTWg32DlXw7y/3f6IYWKdYcp8XREbrzAT7SDG0RjOlYL+3anONfUxe46i37KKn95Ttx5Te8NDu3bLV2x0vWZGGODfzUGRw05yNYkFLWrBrCxU0emeOqSlVRY1NYM8iGTD5FNxRob9gJ2+RK/YiCenL7j/+99hweTmGo9aRLN0KU6vRvYxcyXGUQkhuBPPxDs/NyHppiY96yjkpxpFqtJc3O6Gb0Rbtq6bH2DD+JpKF2YmSlPQs+t/oypX0SzIpoef6PaYTgOnMuasJH4Hlehd2z/drE9z3Lup8LVGwsX2MQn3LbyviRu8Z7nuAWDKk05vBXw1BSV9OxRXAxE910L2zuE9FoiaGu+yXxoncCFFyh2/nOPRklfUzuK0fdXS6HUq55oXp5kn+YvUREeBI90GlRwGVcaUfEnvOeXz8ozDJKKFfcLr4YxpSGSlSxplTy5h8l0MDR8rq2l1APrrbaoTgugXhnSg/lkA0PP9JCIBsd7nuGlyZ+21nBIcnbU9e6sWl7Ib5X5dhg0QobY8utD5g== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(39860400002)(396003)(136003)(346002)(376002)(230922051799003)(186009)(82310400011)(1800799009)(64100799003)(451199024)(40470700004)(46966006)(36840700001)(2906002)(83380400001)(7416002)(47076005)(36860700001)(81166007)(426003)(40480700001)(336012)(5660300002)(1076003)(356005)(2616005)(82740400003)(41300700001)(110136005)(316002)(70586007)(54906003)(40460700003)(6666004)(7696005)(8676002)(16526019)(26005)(478600001)(70206006)(4326008)(8936002)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Oct 2023 06:38:14.2465 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1725621f-884c-4c5d-5310-08dbd912cb16 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BL6PEPF0001AB73.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6112 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=no 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 29 Oct 2023 23:38:41 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781161443844476288 X-GMAIL-MSGID: 1781161443844476288 |
Series |
Add Secure TSC support for SNP guests
|
|
Commit Message
Nikunj A. Dadhania
Oct. 30, 2023, 6:36 a.m. UTC
Drop vmpck and os_area_msg_seqno pointers so that secret page layout
does not need to be exposed to the sev-guest driver after the rework.
Instead, add helper APIs to access vmpck and os_area_msg_seqno when
needed.
Also, change function is_vmpck_empty() to snp_is_vmpck_empty() in
preparation for moving to sev.c.
Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
---
drivers/virt/coco/sev-guest/sev-guest.c | 85 ++++++++++++-------------
1 file changed, 42 insertions(+), 43 deletions(-)
Comments
On Sun, Oct 29, 2023 at 11:38 PM Nikunj A Dadhania <nikunj@amd.com> wrote: > > Drop vmpck and os_area_msg_seqno pointers so that secret page layout > does not need to be exposed to the sev-guest driver after the rework. > Instead, add helper APIs to access vmpck and os_area_msg_seqno when > needed. > > Also, change function is_vmpck_empty() to snp_is_vmpck_empty() in > preparation for moving to sev.c. > > Signed-off-by: Nikunj A Dadhania <nikunj@amd.com> > --- > drivers/virt/coco/sev-guest/sev-guest.c | 85 ++++++++++++------------- > 1 file changed, 42 insertions(+), 43 deletions(-) > > diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c > index 5801dd52ffdf..4dd094c73e2f 100644 > --- a/drivers/virt/coco/sev-guest/sev-guest.c > +++ b/drivers/virt/coco/sev-guest/sev-guest.c > @@ -50,8 +50,7 @@ struct snp_guest_dev { > > struct snp_secrets_page_layout *layout; > struct snp_req_data input; > - u32 *os_area_msg_seqno; > - u8 *vmpck; > + unsigned int vmpck_id; > }; > > static u32 vmpck_id; > @@ -61,14 +60,22 @@ MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP. > /* Mutex to serialize the shared buffer access and command handling. */ > static DEFINE_MUTEX(snp_cmd_mutex); > > -static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) > +static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev) > { > - char zero_key[VMPCK_KEY_LEN] = {0}; > + return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN; > +} > > - if (snp_dev->vmpck) > - return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN); > +static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev) > +{ > + return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id; > +} > > - return true; > +static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev) > +{ > + char zero_key[VMPCK_KEY_LEN] = {0}; > + u8 *key = snp_get_vmpck(snp_dev); > + > + return !memcmp(key, zero_key, VMPCK_KEY_LEN); > } > > /* > @@ -90,20 +97,22 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) > */ > static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) > { > + u8 *key = snp_get_vmpck(snp_dev); > + > dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n", > - vmpck_id); > - memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN); > - snp_dev->vmpck = NULL; > + snp_dev->vmpck_id); > + memzero_explicit(key, VMPCK_KEY_LEN); > } We disable the VMPCK because we believe the guest to be under attack, but this only clears a single key. Shouldn't we clear all VMPCK keys in the secrets page for good measure? If at VMPCK > 0, most likely the 0..VMPCK-1 keys have been zeroed by whatever was prior in the boot stack, but still better to be safe. > > static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev) > { > + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); > u64 count; > > lockdep_assert_held(&snp_dev->cmd_mutex); > > /* Read the current message sequence counter from secrets pages */ > - count = *snp_dev->os_area_msg_seqno; > + count = *os_area_msg_seqno; > > return count + 1; > } > @@ -131,11 +140,13 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev) > > static void snp_inc_msg_seqno(struct snp_guest_dev *snp_dev) > { > + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); > + > /* > * The counter is also incremented by the PSP, so increment it by 2 > * and save in secrets page. > */ > - *snp_dev->os_area_msg_seqno += 2; > + *os_area_msg_seqno += 2; > } > > static inline struct snp_guest_dev *to_snp_dev(struct file *file) > @@ -145,15 +156,22 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) > return container_of(dev, struct snp_guest_dev, misc); > } > > -static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) > +static struct aesgcm_ctx *snp_init_crypto(struct snp_guest_dev *snp_dev) > { > struct aesgcm_ctx *ctx; > + u8 *key; > + > + if (snp_is_vmpck_empty(snp_dev)) { > + pr_err("SNP: vmpck id %d is null\n", snp_dev->vmpck_id); > + return NULL; > + } > > ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); > if (!ctx) > return NULL; > > - if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) { > + key = snp_get_vmpck(snp_dev); > + if (aesgcm_expandkey(ctx, key, VMPCK_KEY_LEN, AUTHTAG_LEN)) { > pr_err("SNP: crypto init failed\n"); > kfree(ctx); > return NULL; > @@ -586,7 +604,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long > mutex_lock(&snp_dev->cmd_mutex); > > /* Check if the VMPCK is not empty */ > - if (is_vmpck_empty(snp_dev)) { > + if (snp_is_vmpck_empty(snp_dev)) { > dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n"); > mutex_unlock(&snp_dev->cmd_mutex); > return -ENOTTY; > @@ -656,32 +674,14 @@ static const struct file_operations snp_guest_fops = { > .unlocked_ioctl = snp_guest_ioctl, > }; > > -static u8 *get_vmpck(int id, struct snp_secrets_page_layout *layout, u32 **seqno) > +bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) > { > - u8 *key = NULL; > + if (WARN_ON(vmpck_id > 3)) > + return false; The vmpck_id is an int for some reason, so < 0 is also a problem. Can we not use unsigned int? > > - switch (id) { > - case 0: > - *seqno = &layout->os_area.msg_seqno_0; > - key = layout->vmpck0; > - break; > - case 1: > - *seqno = &layout->os_area.msg_seqno_1; > - key = layout->vmpck1; > - break; > - case 2: > - *seqno = &layout->os_area.msg_seqno_2; > - key = layout->vmpck2; > - break; > - case 3: > - *seqno = &layout->os_area.msg_seqno_3; > - key = layout->vmpck3; > - break; > - default: > - break; > - } > + dev->vmpck_id = vmpck_id; > > - return key; > + return true; > } > > static int __init sev_guest_probe(struct platform_device *pdev) > @@ -713,14 +713,14 @@ static int __init sev_guest_probe(struct platform_device *pdev) > goto e_unmap; > > ret = -EINVAL; > - snp_dev->vmpck = get_vmpck(vmpck_id, layout, &snp_dev->os_area_msg_seqno); > - if (!snp_dev->vmpck) { > + snp_dev->layout = layout; > + if (!snp_assign_vmpck(snp_dev, vmpck_id)) { > dev_err(dev, "invalid vmpck id %d\n", vmpck_id); > goto e_unmap; > } > > /* Verify that VMPCK is not zero. */ > - if (is_vmpck_empty(snp_dev)) { > + if (snp_is_vmpck_empty(snp_dev)) { > dev_err(dev, "vmpck id %d is null\n", vmpck_id); > goto e_unmap; > } > @@ -728,7 +728,6 @@ static int __init sev_guest_probe(struct platform_device *pdev) > mutex_init(&snp_dev->cmd_mutex); > platform_set_drvdata(pdev, snp_dev); > snp_dev->dev = dev; > - snp_dev->layout = layout; > > /* Allocate the shared page used for the request and response message. */ > snp_dev->request = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); > @@ -744,7 +743,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) > goto e_free_response; > > ret = -EIO; > - snp_dev->ctx = snp_init_crypto(snp_dev->vmpck, VMPCK_KEY_LEN); > + snp_dev->ctx = snp_init_crypto(snp_dev); > if (!snp_dev->ctx) > goto e_free_cert_data; > > -- > 2.34.1 >
On 10/30/23 11:16, Dionna Amalie Glaze wrote: > On Sun, Oct 29, 2023 at 11:38 PM Nikunj A Dadhania <nikunj@amd.com> wrote: >> >> Drop vmpck and os_area_msg_seqno pointers so that secret page layout >> does not need to be exposed to the sev-guest driver after the rework. >> Instead, add helper APIs to access vmpck and os_area_msg_seqno when >> needed. >> >> Also, change function is_vmpck_empty() to snp_is_vmpck_empty() in >> preparation for moving to sev.c. >> >> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com> >> --- >> drivers/virt/coco/sev-guest/sev-guest.c | 85 ++++++++++++------------- >> 1 file changed, 42 insertions(+), 43 deletions(-) >> >> diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c >> index 5801dd52ffdf..4dd094c73e2f 100644 >> --- a/drivers/virt/coco/sev-guest/sev-guest.c >> +++ b/drivers/virt/coco/sev-guest/sev-guest.c >> @@ -50,8 +50,7 @@ struct snp_guest_dev { >> >> struct snp_secrets_page_layout *layout; >> struct snp_req_data input; >> - u32 *os_area_msg_seqno; >> - u8 *vmpck; >> + unsigned int vmpck_id; >> }; >> >> static u32 vmpck_id; >> @@ -61,14 +60,22 @@ MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP. >> /* Mutex to serialize the shared buffer access and command handling. */ >> static DEFINE_MUTEX(snp_cmd_mutex); >> >> -static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) >> +static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev) >> { >> - char zero_key[VMPCK_KEY_LEN] = {0}; >> + return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN; >> +} >> >> - if (snp_dev->vmpck) >> - return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN); >> +static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev) >> +{ >> + return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id; >> +} >> >> - return true; >> +static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev) >> +{ >> + char zero_key[VMPCK_KEY_LEN] = {0}; >> + u8 *key = snp_get_vmpck(snp_dev); >> + >> + return !memcmp(key, zero_key, VMPCK_KEY_LEN); >> } >> >> /* >> @@ -90,20 +97,22 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) >> */ >> static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) >> { >> + u8 *key = snp_get_vmpck(snp_dev); >> + >> dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n", >> - vmpck_id); >> - memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN); >> - snp_dev->vmpck = NULL; >> + snp_dev->vmpck_id); >> + memzero_explicit(key, VMPCK_KEY_LEN); >> } > > We disable the VMPCK because we believe the guest to be under attack, > but this only clears a single key. Shouldn't we clear all VMPCK keys > in the secrets page for good measure? If at VMPCK > 0, most likely the > 0..VMPCK-1 keys have been zeroed by whatever was prior in the boot > stack, but still better to be safe. Doing that would be a separate patch series and isn't appropriate here. Thanks, Tom > >> >> static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev) >> { >> + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); >> u64 count; >> >> lockdep_assert_held(&snp_dev->cmd_mutex); >> >> /* Read the current message sequence counter from secrets pages */ >> - count = *snp_dev->os_area_msg_seqno; >> + count = *os_area_msg_seqno; >> >> return count + 1; >> } >> @@ -131,11 +140,13 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev) >> >> static void snp_inc_msg_seqno(struct snp_guest_dev *snp_dev) >> { >> + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); >> + >> /* >> * The counter is also incremented by the PSP, so increment it by 2 >> * and save in secrets page. >> */ >> - *snp_dev->os_area_msg_seqno += 2; >> + *os_area_msg_seqno += 2; >> } >> >> static inline struct snp_guest_dev *to_snp_dev(struct file *file) >> @@ -145,15 +156,22 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) >> return container_of(dev, struct snp_guest_dev, misc); >> } >> >> -static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) >> +static struct aesgcm_ctx *snp_init_crypto(struct snp_guest_dev *snp_dev) >> { >> struct aesgcm_ctx *ctx; >> + u8 *key; >> + >> + if (snp_is_vmpck_empty(snp_dev)) { >> + pr_err("SNP: vmpck id %d is null\n", snp_dev->vmpck_id); >> + return NULL; >> + } >> >> ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); >> if (!ctx) >> return NULL; >> >> - if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) { >> + key = snp_get_vmpck(snp_dev); >> + if (aesgcm_expandkey(ctx, key, VMPCK_KEY_LEN, AUTHTAG_LEN)) { >> pr_err("SNP: crypto init failed\n"); >> kfree(ctx); >> return NULL; >> @@ -586,7 +604,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long >> mutex_lock(&snp_dev->cmd_mutex); >> >> /* Check if the VMPCK is not empty */ >> - if (is_vmpck_empty(snp_dev)) { >> + if (snp_is_vmpck_empty(snp_dev)) { >> dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n"); >> mutex_unlock(&snp_dev->cmd_mutex); >> return -ENOTTY; >> @@ -656,32 +674,14 @@ static const struct file_operations snp_guest_fops = { >> .unlocked_ioctl = snp_guest_ioctl, >> }; >> >> -static u8 *get_vmpck(int id, struct snp_secrets_page_layout *layout, u32 **seqno) >> +bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) >> { >> - u8 *key = NULL; >> + if (WARN_ON(vmpck_id > 3)) >> + return false; > > The vmpck_id is an int for some reason, so < 0 is also a problem. Can > we not use unsigned int? > >> >> - switch (id) { >> - case 0: >> - *seqno = &layout->os_area.msg_seqno_0; >> - key = layout->vmpck0; >> - break; >> - case 1: >> - *seqno = &layout->os_area.msg_seqno_1; >> - key = layout->vmpck1; >> - break; >> - case 2: >> - *seqno = &layout->os_area.msg_seqno_2; >> - key = layout->vmpck2; >> - break; >> - case 3: >> - *seqno = &layout->os_area.msg_seqno_3; >> - key = layout->vmpck3; >> - break; >> - default: >> - break; >> - } >> + dev->vmpck_id = vmpck_id; >> >> - return key; >> + return true; >> } >> >> static int __init sev_guest_probe(struct platform_device *pdev) >> @@ -713,14 +713,14 @@ static int __init sev_guest_probe(struct platform_device *pdev) >> goto e_unmap; >> >> ret = -EINVAL; >> - snp_dev->vmpck = get_vmpck(vmpck_id, layout, &snp_dev->os_area_msg_seqno); >> - if (!snp_dev->vmpck) { >> + snp_dev->layout = layout; >> + if (!snp_assign_vmpck(snp_dev, vmpck_id)) { >> dev_err(dev, "invalid vmpck id %d\n", vmpck_id); >> goto e_unmap; >> } >> >> /* Verify that VMPCK is not zero. */ >> - if (is_vmpck_empty(snp_dev)) { >> + if (snp_is_vmpck_empty(snp_dev)) { >> dev_err(dev, "vmpck id %d is null\n", vmpck_id); >> goto e_unmap; >> } >> @@ -728,7 +728,6 @@ static int __init sev_guest_probe(struct platform_device *pdev) >> mutex_init(&snp_dev->cmd_mutex); >> platform_set_drvdata(pdev, snp_dev); >> snp_dev->dev = dev; >> - snp_dev->layout = layout; >> >> /* Allocate the shared page used for the request and response message. */ >> snp_dev->request = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); >> @@ -744,7 +743,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) >> goto e_free_response; >> >> ret = -EIO; >> - snp_dev->ctx = snp_init_crypto(snp_dev->vmpck, VMPCK_KEY_LEN); >> + snp_dev->ctx = snp_init_crypto(snp_dev); >> if (!snp_dev->ctx) >> goto e_free_cert_data; >> >> -- >> 2.34.1 >> > >
On 10/30/23 01:36, Nikunj A Dadhania wrote: > Drop vmpck and os_area_msg_seqno pointers so that secret page layout > does not need to be exposed to the sev-guest driver after the rework. > Instead, add helper APIs to access vmpck and os_area_msg_seqno when > needed. > > Also, change function is_vmpck_empty() to snp_is_vmpck_empty() in > preparation for moving to sev.c. > > Signed-off-by: Nikunj A Dadhania <nikunj@amd.com> With the fix to the snp_assign_vmpck() to change the int to an unsigned int as requested by Dionna... Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com> > --- > drivers/virt/coco/sev-guest/sev-guest.c | 85 ++++++++++++------------- > 1 file changed, 42 insertions(+), 43 deletions(-) > > diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c > index 5801dd52ffdf..4dd094c73e2f 100644 > --- a/drivers/virt/coco/sev-guest/sev-guest.c > +++ b/drivers/virt/coco/sev-guest/sev-guest.c > @@ -50,8 +50,7 @@ struct snp_guest_dev { > > struct snp_secrets_page_layout *layout; > struct snp_req_data input; > - u32 *os_area_msg_seqno; > - u8 *vmpck; > + unsigned int vmpck_id; > }; > > static u32 vmpck_id; > @@ -61,14 +60,22 @@ MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP. > /* Mutex to serialize the shared buffer access and command handling. */ > static DEFINE_MUTEX(snp_cmd_mutex); > > -static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) > +static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev) > { > - char zero_key[VMPCK_KEY_LEN] = {0}; > + return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN; > +} > > - if (snp_dev->vmpck) > - return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN); > +static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev) > +{ > + return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id; > +} > > - return true; > +static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev) > +{ > + char zero_key[VMPCK_KEY_LEN] = {0}; > + u8 *key = snp_get_vmpck(snp_dev); > + > + return !memcmp(key, zero_key, VMPCK_KEY_LEN); > } > > /* > @@ -90,20 +97,22 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) > */ > static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) > { > + u8 *key = snp_get_vmpck(snp_dev); > + > dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n", > - vmpck_id); > - memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN); > - snp_dev->vmpck = NULL; > + snp_dev->vmpck_id); > + memzero_explicit(key, VMPCK_KEY_LEN); > } > > static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev) > { > + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); > u64 count; > > lockdep_assert_held(&snp_dev->cmd_mutex); > > /* Read the current message sequence counter from secrets pages */ > - count = *snp_dev->os_area_msg_seqno; > + count = *os_area_msg_seqno; > > return count + 1; > } > @@ -131,11 +140,13 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev) > > static void snp_inc_msg_seqno(struct snp_guest_dev *snp_dev) > { > + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); > + > /* > * The counter is also incremented by the PSP, so increment it by 2 > * and save in secrets page. > */ > - *snp_dev->os_area_msg_seqno += 2; > + *os_area_msg_seqno += 2; > } > > static inline struct snp_guest_dev *to_snp_dev(struct file *file) > @@ -145,15 +156,22 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) > return container_of(dev, struct snp_guest_dev, misc); > } > > -static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) > +static struct aesgcm_ctx *snp_init_crypto(struct snp_guest_dev *snp_dev) > { > struct aesgcm_ctx *ctx; > + u8 *key; > + > + if (snp_is_vmpck_empty(snp_dev)) { > + pr_err("SNP: vmpck id %d is null\n", snp_dev->vmpck_id); > + return NULL; > + } > > ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); > if (!ctx) > return NULL; > > - if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) { > + key = snp_get_vmpck(snp_dev); > + if (aesgcm_expandkey(ctx, key, VMPCK_KEY_LEN, AUTHTAG_LEN)) { > pr_err("SNP: crypto init failed\n"); > kfree(ctx); > return NULL; > @@ -586,7 +604,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long > mutex_lock(&snp_dev->cmd_mutex); > > /* Check if the VMPCK is not empty */ > - if (is_vmpck_empty(snp_dev)) { > + if (snp_is_vmpck_empty(snp_dev)) { > dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n"); > mutex_unlock(&snp_dev->cmd_mutex); > return -ENOTTY; > @@ -656,32 +674,14 @@ static const struct file_operations snp_guest_fops = { > .unlocked_ioctl = snp_guest_ioctl, > }; > > -static u8 *get_vmpck(int id, struct snp_secrets_page_layout *layout, u32 **seqno) > +bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) > { > - u8 *key = NULL; > + if (WARN_ON(vmpck_id > 3)) > + return false; > > - switch (id) { > - case 0: > - *seqno = &layout->os_area.msg_seqno_0; > - key = layout->vmpck0; > - break; > - case 1: > - *seqno = &layout->os_area.msg_seqno_1; > - key = layout->vmpck1; > - break; > - case 2: > - *seqno = &layout->os_area.msg_seqno_2; > - key = layout->vmpck2; > - break; > - case 3: > - *seqno = &layout->os_area.msg_seqno_3; > - key = layout->vmpck3; > - break; > - default: > - break; > - } > + dev->vmpck_id = vmpck_id; > > - return key; > + return true; > } > > static int __init sev_guest_probe(struct platform_device *pdev) > @@ -713,14 +713,14 @@ static int __init sev_guest_probe(struct platform_device *pdev) > goto e_unmap; > > ret = -EINVAL; > - snp_dev->vmpck = get_vmpck(vmpck_id, layout, &snp_dev->os_area_msg_seqno); > - if (!snp_dev->vmpck) { > + snp_dev->layout = layout; > + if (!snp_assign_vmpck(snp_dev, vmpck_id)) { > dev_err(dev, "invalid vmpck id %d\n", vmpck_id); > goto e_unmap; > } > > /* Verify that VMPCK is not zero. */ > - if (is_vmpck_empty(snp_dev)) { > + if (snp_is_vmpck_empty(snp_dev)) { > dev_err(dev, "vmpck id %d is null\n", vmpck_id); > goto e_unmap; > } > @@ -728,7 +728,6 @@ static int __init sev_guest_probe(struct platform_device *pdev) > mutex_init(&snp_dev->cmd_mutex); > platform_set_drvdata(pdev, snp_dev); > snp_dev->dev = dev; > - snp_dev->layout = layout; > > /* Allocate the shared page used for the request and response message. */ > snp_dev->request = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); > @@ -744,7 +743,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) > goto e_free_response; > > ret = -EIO; > - snp_dev->ctx = snp_init_crypto(snp_dev->vmpck, VMPCK_KEY_LEN); > + snp_dev->ctx = snp_init_crypto(snp_dev); > if (!snp_dev->ctx) > goto e_free_cert_data; >
On 10/30/2023 10:42 PM, Tom Lendacky wrote: > On 10/30/23 11:16, Dionna Amalie Glaze wrote: >> On Sun, Oct 29, 2023 at 11:38 PM Nikunj A Dadhania <nikunj@amd.com> wrote: >>> @@ -656,32 +674,14 @@ static const struct file_operations snp_guest_fops = { >>> .unlocked_ioctl = snp_guest_ioctl, >>> }; >>> >>> -static u8 *get_vmpck(int id, struct snp_secrets_page_layout *layout, u32 **seqno) >>> +bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) >>> { >>> - u8 *key = NULL; >>> + if (WARN_ON(vmpck_id > 3)) >>> + return false; >> >> The vmpck_id is an int for some reason, so < 0 is also a problem. Can >> we not use unsigned int? Yes, I will update that in my next revision, Thanks Nikunj
diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 5801dd52ffdf..4dd094c73e2f 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -50,8 +50,7 @@ struct snp_guest_dev { struct snp_secrets_page_layout *layout; struct snp_req_data input; - u32 *os_area_msg_seqno; - u8 *vmpck; + unsigned int vmpck_id; }; static u32 vmpck_id; @@ -61,14 +60,22 @@ MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP. /* Mutex to serialize the shared buffer access and command handling. */ static DEFINE_MUTEX(snp_cmd_mutex); -static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) +static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev) { - char zero_key[VMPCK_KEY_LEN] = {0}; + return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN; +} - if (snp_dev->vmpck) - return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN); +static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev) +{ + return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id; +} - return true; +static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev) +{ + char zero_key[VMPCK_KEY_LEN] = {0}; + u8 *key = snp_get_vmpck(snp_dev); + + return !memcmp(key, zero_key, VMPCK_KEY_LEN); } /* @@ -90,20 +97,22 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) */ static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) { + u8 *key = snp_get_vmpck(snp_dev); + dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n", - vmpck_id); - memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN); - snp_dev->vmpck = NULL; + snp_dev->vmpck_id); + memzero_explicit(key, VMPCK_KEY_LEN); } static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev) { + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); u64 count; lockdep_assert_held(&snp_dev->cmd_mutex); /* Read the current message sequence counter from secrets pages */ - count = *snp_dev->os_area_msg_seqno; + count = *os_area_msg_seqno; return count + 1; } @@ -131,11 +140,13 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev) static void snp_inc_msg_seqno(struct snp_guest_dev *snp_dev) { + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); + /* * The counter is also incremented by the PSP, so increment it by 2 * and save in secrets page. */ - *snp_dev->os_area_msg_seqno += 2; + *os_area_msg_seqno += 2; } static inline struct snp_guest_dev *to_snp_dev(struct file *file) @@ -145,15 +156,22 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) +static struct aesgcm_ctx *snp_init_crypto(struct snp_guest_dev *snp_dev) { struct aesgcm_ctx *ctx; + u8 *key; + + if (snp_is_vmpck_empty(snp_dev)) { + pr_err("SNP: vmpck id %d is null\n", snp_dev->vmpck_id); + return NULL; + } ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); if (!ctx) return NULL; - if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) { + key = snp_get_vmpck(snp_dev); + if (aesgcm_expandkey(ctx, key, VMPCK_KEY_LEN, AUTHTAG_LEN)) { pr_err("SNP: crypto init failed\n"); kfree(ctx); return NULL; @@ -586,7 +604,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long mutex_lock(&snp_dev->cmd_mutex); /* Check if the VMPCK is not empty */ - if (is_vmpck_empty(snp_dev)) { + if (snp_is_vmpck_empty(snp_dev)) { dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n"); mutex_unlock(&snp_dev->cmd_mutex); return -ENOTTY; @@ -656,32 +674,14 @@ static const struct file_operations snp_guest_fops = { .unlocked_ioctl = snp_guest_ioctl, }; -static u8 *get_vmpck(int id, struct snp_secrets_page_layout *layout, u32 **seqno) +bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) { - u8 *key = NULL; + if (WARN_ON(vmpck_id > 3)) + return false; - switch (id) { - case 0: - *seqno = &layout->os_area.msg_seqno_0; - key = layout->vmpck0; - break; - case 1: - *seqno = &layout->os_area.msg_seqno_1; - key = layout->vmpck1; - break; - case 2: - *seqno = &layout->os_area.msg_seqno_2; - key = layout->vmpck2; - break; - case 3: - *seqno = &layout->os_area.msg_seqno_3; - key = layout->vmpck3; - break; - default: - break; - } + dev->vmpck_id = vmpck_id; - return key; + return true; } static int __init sev_guest_probe(struct platform_device *pdev) @@ -713,14 +713,14 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_unmap; ret = -EINVAL; - snp_dev->vmpck = get_vmpck(vmpck_id, layout, &snp_dev->os_area_msg_seqno); - if (!snp_dev->vmpck) { + snp_dev->layout = layout; + if (!snp_assign_vmpck(snp_dev, vmpck_id)) { dev_err(dev, "invalid vmpck id %d\n", vmpck_id); goto e_unmap; } /* Verify that VMPCK is not zero. */ - if (is_vmpck_empty(snp_dev)) { + if (snp_is_vmpck_empty(snp_dev)) { dev_err(dev, "vmpck id %d is null\n", vmpck_id); goto e_unmap; } @@ -728,7 +728,6 @@ static int __init sev_guest_probe(struct platform_device *pdev) mutex_init(&snp_dev->cmd_mutex); platform_set_drvdata(pdev, snp_dev); snp_dev->dev = dev; - snp_dev->layout = layout; /* Allocate the shared page used for the request and response message. */ snp_dev->request = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); @@ -744,7 +743,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_free_response; ret = -EIO; - snp_dev->ctx = snp_init_crypto(snp_dev->vmpck, VMPCK_KEY_LEN); + snp_dev->ctx = snp_init_crypto(snp_dev); if (!snp_dev->ctx) goto e_free_cert_data;