From patchwork Thu Dec 8 15:29:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Lendacky X-Patchwork-Id: 31417 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp262016wrr; Thu, 8 Dec 2022 07:31:56 -0800 (PST) X-Google-Smtp-Source: AA0mqf4LfkJ9K71oMWgjpxI51KD89h9pgeAhcmTtprdL3P8bDb/dYXbBd2KeeJsUOHuapt6UWvCN X-Received: by 2002:a17:906:30c3:b0:7ba:a674:22e4 with SMTP id b3-20020a17090630c300b007baa67422e4mr54732023ejb.279.1670513516403; Thu, 08 Dec 2022 07:31:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1670513516; cv=pass; d=google.com; s=arc-20160816; b=dl1TgQxx8at9ujHdexvcjorgyQAm13j47UDXe9lOUIGWU+5gT44+LEIEXkt1/iXqio rPT3NcQCUEoL0Frt+NBCUB+kp9dgcpFUj1/HiVmfgljjPRHn7BQCMeum6shfFT1y9g3g 5Ho5ppi9WbsGlMCBgFON5+fNyG09maaCvBHZxgVvx2g0QIh/DNEFEioCP1NE2Ol3TKXb mnvu/5ap2MheVPqhcF4pB2AaiUvtu7cfO4mj+hcKaee+IFxmuEw3zBZwOxqC7xnlQqkp JuYFgLopuPDwS9ZkshUdvHGBomyHWHl/e2ZB+b+7UWgbvhN1VJO8XUTxDt3VXVbwxGbs Krug== 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=cMbd6t318giH/5MNnxD31shZCKsR7hDf/YbWNyKT7SY=; b=MZmz4SpYXaAKwMWzDQkpSJeaUq0wvLwS8zcGjxLmYumhCp0Y3LXhHKxwbWNG8u3xzo YVsJXmoHonNglDm+dIpvHa+NVFN965fEFcbkvF2CrDuVHa40SckJSldpLk/ZBfGIQLGj PmyKJY0ZRJDzW1fRpDhH0izDLXxj33ei3psCzMxSsMeIMycFMi+126wHL5kzonOgwylI LvMlsJvI84diVXK2jKZkewHzb/eQCbbwmOqYP9poRUhwj7TuKgFq1Dz98cvjyvJJOeY5 NXYvZhFpE6odzLzAyaBjjfbGinAnQu7hPn/VZo7dL2BSlx94VQxKQU+xrsmczAK152iH uftg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b="dk1lwK/7"; 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::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q6-20020a1709064cc600b0078e063fc787si15144390ejt.433.2022.12.08.07.31.29; Thu, 08 Dec 2022 07:31:56 -0800 (PST) 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=@amd.com header.s=selector1 header.b="dk1lwK/7"; 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::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230345AbiLHP3v (ORCPT + 99 others); Thu, 8 Dec 2022 10:29:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230255AbiLHP3i (ORCPT ); Thu, 8 Dec 2022 10:29:38 -0500 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on20609.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e89::609]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A94C78682 for ; Thu, 8 Dec 2022 07:29:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cWd6yKXNqWgj3Y5l8wsQhfp4QyJmy/9D8jc/11bacgGru9uGf13/fQede8Fl2+08Jzj9rffSPFTgSHtfwC3ZeU4qImsLrW7GCi0mebHhwBltE3ksPJeNwrw1YqoEKnhpvaLHzqL+2x2YYLwznvBcd8R3d41Y/aruXwHpcAYOrsZlYiqOC3BPDYGgPayEl0AJazbsLOQvnNNP4Vy4w6zJ1qMAF//t568E8TIU+WP4P4R9BChVmQ6ZII0tWSpuNxsaYiVjHPgU7n9tzR0/pWLymhOUB0mGNozWEN+TzxZmeH3wQIh0EnQaOA+TH+finK5wTZBMd4hFwvFjpQTYPFofBg== 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=cMbd6t318giH/5MNnxD31shZCKsR7hDf/YbWNyKT7SY=; b=DZe/HB5jPX5a4ZkDsbsH09txBLZCrc/0w3lqgezRji7yCwN4Odd+onEe1FxBUc9H8QMTM8Z3t7kDVGw0N7N/UxoWlJ7MogzSITJ+2BGV/AScQ07eCckmsx6FDi8mc0k7hkq16+DQfApltORerCvnaJhNyaYSeiEaphPPFywmOALaF1Lj/zukSyfwHsTT1Ri6rvewaur/qqOZCO7T6MpOefpwMcTSOdo2gPaK1Nr7MqSg0mdiqgU5pgZtDd0SRDqdmq6L01OlZoPPJseIocDensTPlv74nrS/CSHobs3KNRSDeki6fSrh6VcOZ4baXxzq5o1Ll8Ctu1c+NvvXwDJ5kQ== 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 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=cMbd6t318giH/5MNnxD31shZCKsR7hDf/YbWNyKT7SY=; b=dk1lwK/7bTTdXFt5kaqJrQxFyq2fZp9RpdSf8G7s+zPmDSBiQ1Grwi7+HqgAQnWK9Uf4ep6q+IAwyPUUJVBLvDNYR6RtqVhaqCl0LxPvlu4ll0wLRYKySZE6J+OA89fbjDv2YxNAdu82qdFSeCc4j1AXJ7I0QcSzKScLJF8l8+I= Received: from BN8PR03CA0015.namprd03.prod.outlook.com (2603:10b6:408:94::28) by SN7PR12MB7417.namprd12.prod.outlook.com (2603:10b6:806:2a4::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.14; Thu, 8 Dec 2022 15:29:34 +0000 Received: from BN8NAM11FT059.eop-nam11.prod.protection.outlook.com (2603:10b6:408:94:cafe::69) by BN8PR03CA0015.outlook.office365.com (2603:10b6:408:94::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.14 via Frontend Transport; Thu, 8 Dec 2022 15:29:34 +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 BN8NAM11FT059.mail.protection.outlook.com (10.13.177.120) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5901.17 via Frontend Transport; Thu, 8 Dec 2022 15:29:33 +0000 Received: from tlendack-t1.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.2375.34; Thu, 8 Dec 2022 09:29:33 -0600 From: Tom Lendacky To: , CC: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "Kirill A. Shutemov" , "H. Peter Anvin" , Michael Roth , Joerg Roedel , Andy Lutomirski , Peter Zijlstra Subject: [PATCH v6 1/5] x86/sev: Fix calculation of end address based on number of pages Date: Thu, 8 Dec 2022 09:29:09 -0600 Message-ID: X-Mailer: git-send-email 2.38.1 In-Reply-To: References: <20221207014933.8435-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 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: BN8NAM11FT059:EE_|SN7PR12MB7417:EE_ X-MS-Office365-Filtering-Correlation-Id: eec0ada5-8866-4a83-3adc-08dad9310239 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4OnUdH6+8xRCuZxG5dG26GhdiaCGvRNBKx3CaWzqmu6d0z513piD5KgdZCguaQUY/VtpOt/uydkncnVXuWl9FacQtaHcQKBNdE5NlaHGq62P2RadGaYiqCtwF19kqRpUGuTKGhYX4HXdLXRtuaTl23sVf6hTWVh61SqaQg7WkxtgMS+cUL1NqTuqCvXfTIWhJEJ3W9sbjj3I4MdYUmxu3wqdOEravToEnp5jf8eHc61tPV5NmPc6fcKZ2j7T/SREscs7IDPxleTpVf+3Yp5n+xQp5a1DMJUkpyXEoSE1QHCKsf7VJigYSW05kb0fPqLIiPbcX9jqmKE5O5zN+FJgPLvplwS7YuxT+TDlK8v7DJ+ppN/ttI/k8lrtO5FggEagdlsVnPAg5hB4rG0inChNpxMM2jHWc5bKSzBLlW07VB3ihHfOg9np1KIgEWMJ5igjuB1jT1JYB4qoB3fL0RWrwjl9dD5cN+aOvrhP1w79d6HsIUYGynoemd1ANrJ0SWuJzGYuURryn2YSDdHjXH0N/6zBvG65wTHdT25jPH1Xw/RLtib4xR5UqGDACtrb47ycejwAOkWMx96WZzuqj5JCjo3VXPvZqPWFKMKewMrEB1wFKBFcHYErpe7OAOKv9Xu8ClgXSG+J+SqszPqGzqPl7vwHoBlavfkDvJDdn3EL6BKdJjPWUDkf5gh79lkdtzsVTlLvPPFMmskgqC02C7Jt7ndpxIZT2vmFXSxwitbhI7E= 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:(13230022)(4636009)(376002)(136003)(39860400002)(346002)(396003)(451199015)(40470700004)(36840700001)(46966006)(186003)(16526019)(110136005)(2616005)(26005)(336012)(316002)(36860700001)(7696005)(83380400001)(6666004)(4326008)(81166007)(7416002)(40460700003)(2906002)(8676002)(478600001)(41300700001)(82740400003)(36756003)(47076005)(70206006)(70586007)(54906003)(5660300002)(426003)(356005)(82310400005)(86362001)(8936002)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Dec 2022 15:29:33.9807 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: eec0ada5-8866-4a83-3adc-08dad9310239 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: BN8NAM11FT059.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7417 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS 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?1751660381009245792?= X-GMAIL-MSGID: =?utf-8?q?1751660381009245792?= When calculating an end address based on an unsigned int number of pages, any value greater than or equal to 0x100000 that is shift PAGE_SHIFT bits results in a 0 value, resulting in an invalid end address. Change the number of pages variable in various routines from an unsigned int to an unsigned long to calculate the end address correctly. Fixes: 5e5ccff60a29 ("x86/sev: Add helper for validating pages in early enc attribute changes") Fixes: dc3f3d2474b8 ("x86/mm: Validate memory when changing the C-bit") Signed-off-by: Tom Lendacky --- arch/x86/include/asm/sev.h | 16 ++++++++-------- arch/x86/kernel/sev.c | 14 +++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ebc271bb6d8e..a0a58c4122ec 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -187,12 +187,12 @@ static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) } void setup_ghcb(void); void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, - unsigned int npages); + unsigned long npages); void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, - unsigned int npages); + unsigned long npages); void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op); -void snp_set_memory_shared(unsigned long vaddr, unsigned int npages); -void snp_set_memory_private(unsigned long vaddr, unsigned int npages); +void snp_set_memory_shared(unsigned long vaddr, unsigned long npages); +void snp_set_memory_private(unsigned long vaddr, unsigned long npages); void snp_set_wakeup_secondary_cpu(void); bool snp_init(struct boot_params *bp); void __init __noreturn snp_abort(void); @@ -207,12 +207,12 @@ static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; } static inline void setup_ghcb(void) { } static inline void __init -early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } +early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned long npages) { } static inline void __init -early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } +early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned long npages) { } static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op) { } -static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) { } -static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npages) { } +static inline void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) { } +static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npages) { } static inline void snp_set_wakeup_secondary_cpu(void) { } static inline bool snp_init(struct boot_params *bp) { return false; } static inline void snp_abort(void) { } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index a428c62330d3..6b823f913c97 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -643,7 +643,7 @@ static u64 __init get_jump_table_addr(void) return ret; } -static void pvalidate_pages(unsigned long vaddr, unsigned int npages, bool validate) +static void pvalidate_pages(unsigned long vaddr, unsigned long npages, bool validate) { unsigned long vaddr_end; int rc; @@ -660,7 +660,7 @@ static void pvalidate_pages(unsigned long vaddr, unsigned int npages, bool valid } } -static void __init early_set_pages_state(unsigned long paddr, unsigned int npages, enum psc_op op) +static void __init early_set_pages_state(unsigned long paddr, unsigned long npages, enum psc_op op) { unsigned long paddr_end; u64 val; @@ -699,7 +699,7 @@ static void __init early_set_pages_state(unsigned long paddr, unsigned int npage } void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, - unsigned int npages) + unsigned long npages) { /* * This can be invoked in early boot while running identity mapped, so @@ -721,7 +721,7 @@ void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long padd } void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, - unsigned int npages) + unsigned long npages) { /* * This can be invoked in early boot while running identity mapped, so @@ -877,7 +877,7 @@ static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); } -static void set_pages_state(unsigned long vaddr, unsigned int npages, int op) +static void set_pages_state(unsigned long vaddr, unsigned long npages, int op) { unsigned long vaddr_end, next_vaddr; struct snp_psc_desc *desc; @@ -902,7 +902,7 @@ static void set_pages_state(unsigned long vaddr, unsigned int npages, int op) kfree(desc); } -void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) +void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) { if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return; @@ -912,7 +912,7 @@ void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) set_pages_state(vaddr, npages, SNP_PAGE_STATE_SHARED); } -void snp_set_memory_private(unsigned long vaddr, unsigned int npages) +void snp_set_memory_private(unsigned long vaddr, unsigned long npages) { if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return;