Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1peCg5-0003hS-1k for mharc-grub-devel@gnu.org; Mon, 20 Mar 2023 06:23:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1peCg3-0003hJ-Ji for grub-devel@gnu.org; Mon, 20 Mar 2023 06:23:43 -0400 Received: from mail-vi1eur04on062b.outbound.protection.outlook.com ([2a01:111:f400:fe0e::62b] helo=EUR04-VI1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1peCg1-0002qF-Mu for grub-devel@gnu.org; Mon, 20 Mar 2023 06:23:43 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=L6YHSjkSUefWvXj9/8f5eLlGv8KQCLsz7hdOA/Viu/eKs4zQbWPlNsKE9ULUSG77rnwY1ms1BPYtTSlytbPlymPawxVwWKGOWfE9nFy6zeJtuI63bevled6zdN7FM0UHKL1y88DLiUY8AG6IIppxbBzihAzRoVzJL42/jUmzKaMcjfqWP6UA/7gdbls6auBRrl/WQgLhfjLUOXPb232pg2KNAwu7pCiM0qGHtM+xOhDrMRMs9UD2IrhKpYqVpFyQc2DXDE4HD2WeUEW7AsqxCTpC+hHlPaWb+2uN07BnGJ5TH8Tg68jkTCYYltFshGb8GKDNkCNdCXcvJQRdwBafFQ== 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=/bEJoAJYucWCjqZJY+2n5de+AuhhHQJWEui/3bdpjW8=; b=CMXuFwHL5+TXeQNoW57SAWi44aN4W7FHpag3LOsWbqneVE7sNnVQmmR/Cp/j1vc3rYYvruhJ7fRJelWDGEhVz7uY5wkM8xqTHRZSmKy4COYDUsw3FPdrO6u8vp5FPu96potLV5M3YTnCm1vJfo8GsIyZSqYJEhYgNc8nDA31s5k0cUPZV0DjzAEXotsydZt9KXQGAKviM2wbS/pKv1cw1O3LjeHTnCbVt9nwxRLT5FDc+XYiwicHHqlxLu2CKB2lwee+/YeOaiJh5scwAjctqUMjLNZjcbsPHrwKvt4Jt2LyZM479T747UkkAKdtiSIW41U0B8ipzKmbtvQmo/bv3A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/bEJoAJYucWCjqZJY+2n5de+AuhhHQJWEui/3bdpjW8=; b=zaautiViFbKW5w23UG7Ml2oFpY+1i/9BodSwR62dlxYWwStILMwgTXc8WgxblPzV/QBu3VeW3JmaUIA5B8/fmckYwEoxF7bfnK7bPnbgjZc5R0xEWzoMjdoXXUI8dd1V/N3Dk7Q28PeBiDOuLiiyWekTH/c0bZ2Ma5g4KY+vXrSfz1ADwbxtstJ1CE1Oy5/mwg2Z0jMoRXFRS+/gVIpA9i82lct8iH8BInh5yX7QMxY75Y1GD4VSmaakVkr3VPidEX57WyTeekd94fs5cnBYKAADbJ5GBxJ6S9oK71yY2JtJ/77dwLudJwb1eoI5AfGNohctmDmZavW0ncphaeA2Pw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Received: from DU2PR04MB9081.eurprd04.prod.outlook.com (2603:10a6:10:2f0::13) by DBBPR04MB7545.eurprd04.prod.outlook.com (2603:10a6:10:200::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.37; Mon, 20 Mar 2023 10:18:36 +0000 Received: from DU2PR04MB9081.eurprd04.prod.outlook.com ([fe80::f24e:f7d1:14f:b945]) by DU2PR04MB9081.eurprd04.prod.outlook.com ([fe80::f24e:f7d1:14f:b945%4]) with mapi id 15.20.6178.037; Mon, 20 Mar 2023 10:18:36 +0000 From: Michael Chang To: The development of GNU GRUB Subject: [PATCH v4] tpm: Disable tpm verifier if tpm is not present Date: Mon, 20 Mar 2023 18:18:26 +0800 Message-Id: <20230320101826.8539-1-mchang@suse.com> X-Mailer: git-send-email 2.39.2 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: TYCP286CA0128.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:2b6::18) To DU2PR04MB9081.eurprd04.prod.outlook.com (2603:10a6:10:2f0::13) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PR04MB9081:EE_|DBBPR04MB7545:EE_ X-MS-Office365-Filtering-Correlation-Id: 0b2e6dfe-2f51-459b-efd8-08db292c7773 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dQK/fepcENtpUzzTGn7wmwOxPR4VuLIn4KtLwNWvO8sfgIqtwH+qIRAPWqK2eZcVnXPe306hhncC/5cbcsNB6ot2rWOMkEjh+yibCKVAqgNfxtwMdF5J7IFdpp9WzLPto+TCP+bRDVAfCD39RlBWFX9mDxHBiu8/Q6rUXvGwjOfrZ5JYZZx0csebebm/8HKO+KLuexkWd8/K2zENfmgtwPUUbnbfimNa9jKBokVBPQb77brGtpTZjlWog9Kn1O3W6BRv+Wcv0IfrK5VJBi5hRS12g09yf+vNJKbb92XCNr34ai7rPyGc5rnlm1ham48wBpH4CqXN9niTvZ0qP1O+rY4CU36aq7tQqz8YUkG+fffr0/OGHlIP98ftweHhzjVhgghH9disiXdYfJY8cwF3TuUD3PxWLa2O2eekwsXgZnfSsq8D0vSBIVaFRe0X3WKcPDTgy4ERQAlLKld20QiuuiJmZBNTV2agq7EhA0AcJWJclgap5xRKzQCkuBXSN44eKRff8ccMSHrUOA/9zSx8wQ8St/L4M702h/KPYAcKUQe5Srvwq1jH/C257x4bN1XT3yqR8A5kIf/SU674hoOMpQ0f6M7qkT+G/Abs7teRekoLoVETIOgDhVN/znSBLJgzykARsbsDf4BuDpkN2aANDA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DU2PR04MB9081.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(39860400002)(396003)(346002)(366004)(376002)(136003)(451199018)(2616005)(186003)(6486002)(478600001)(6666004)(83380400001)(316002)(66476007)(6916009)(66556008)(66946007)(8676002)(6512007)(1076003)(6506007)(66899018)(41300700001)(5660300002)(8936002)(38100700002)(2906002)(86362001)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?RnMwR2BYXCC3gSNq9JvQ198RY2rjKf1qKXRAA2UNreOtQj7+CVwY5SqtAd1m?= =?us-ascii?Q?BRy232FcCRAuqHAHLIp53Tl23lA/QV+Pn0QuvsKbLQL9knjmc3wlrIDJRByl?= =?us-ascii?Q?yGf2R/D6uKwaFyWuU1MNBaOU5jWYox8XftRGtWg/0XsARHpMWHTSrlgX0T1Y?= =?us-ascii?Q?/cdmBuMPtDxeW7+9Yrz3GUyuto3V1ykuBDzeATCHR9QwCbZX1ymlImFApzx9?= =?us-ascii?Q?RdfcdtNfdNTn3jBy6PX6YlMtHlkYEde+2djiuCmJurcYMCSx8LldWSHpEK1N?= =?us-ascii?Q?83f4rpNvOpka6qfeJ62HG5sdy4W0p1lwkKlPDmSXrXXPPc/g/TdqKwSzaEUp?= =?us-ascii?Q?JguO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8FUcQhxt?= =?us-ascii?Q?27sZe6kpAJPLVi7xHop/KrHeuJVZE1Ef7ClUZAc2rhADhy4r570yHw5ay+ia?= =?us-ascii?Q?aXwy+KWsfBOrM1GiaB5kffz4Hb++Ma2rPjel6ZZ77ngxuckRGfyswsrxiu2m?= =?us-ascii?Q?dF6kGHJgO8MTFRyZ3Gr6FaqP8b6XtFVdPTcBJMi4ex5pRC0PLrZNOb3n6pfh?= =?us-ascii?Q?AxrvSLeaWEI5tbsexUN+FyNQe4vhd+tmeVCB1zTUeuDgkun3BnPhR0t+i6LJ?= =?us-ascii?Q?v8UreeDVRdAU8WG8C6JIlANjm/z7gyKsSiOQfzHZ1kdLbLFsw915yV56LWPb?= =?us-ascii?Q?ne6mAHzYtXODhrOYLddesiH9ZaHfZ90haJz6XBXpFr8UKvefiuZdZmrqXYgg?= =?us-ascii?Q?ggh6gBWIKrqAW+K+rX/ztJEk71sL3Aai2kmpsBbIYP49?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0b2e6dfe-2f51-459b-efd8-08db292c7773 X-MS-Exchange-CrossTenant-AuthSource: DU2PR04MB9081.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Mar 2023 10:18:36.6349 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 9FmdCNNPNRyos5hoj/DiLrjpUnco3OuC4xJB3CQzfffrRtxE9Sbyu1FrwqdghkAF X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR04MB7545 Received-SPF: pass client-ip=2a01:111:f400:fe0e::62b; envelope-from=MChang@suse.com; helo=EUR04-VI1-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Mar 2023 10:23:43 -0000 When the TPM module is loaded, the verifier reads the entire file into memory, measures and extends the hash, and uses the verified content as a backing buffer for disk files. However, this process can result in a high memory utilization cost per file operation, sometimes causing the system to run out of memory, which can lead to boot failure. To address this issue, previous patches have optimized memory management by dynamically allocating heap space to maximize memory usage and reduce the threat of memory exhaustion. But in some cases, problems may still arise, such as when large ISO images are mounted using loopback or when dealing with embedded systems with limited memory resources. Unfortunately, the current implementation of the TPM module doesn't allow for the elimination of the back buffer once it is loaded, even if no TPM device is present or the device has been explicitly disabled. This can lead to wasted memory. To solve this issue, a patch has been developed to detect the TPM status at the time of loading and skip verifier registration if the device is missing or deactivated. This prevents the allocation of memory for a back buffer, avoiding wasted memory when no real measure boot functionality is performed. This patch also provides users with the option to disable the TPM device to free up memory in scenarios where the system can't afford the high memory utilization cost. Signed-off-by: Michael Chang Signed-off-by: Stefan Berger --- grub-core/commands/efi/tpm.c | 37 +++++++++++++++++++++++++++ grub-core/commands/ieee1275/ibmvtpm.c | 20 +++++++-------- grub-core/commands/tpm.c | 11 ++++++++ include/grub/tpm.h | 1 + 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/grub-core/commands/efi/tpm.c b/grub-core/commands/efi/tpm.c index ae09c1bf8..e1f343fea 100644 --- a/grub-core/commands/efi/tpm.c +++ b/grub-core/commands/efi/tpm.c @@ -287,3 +287,40 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr, else return grub_tpm2_log_event (tpm_handle, buf, size, pcr, description); } + +int +grub_tpm_present (void) +{ + grub_efi_handle_t tpm_handle; + grub_efi_uint8_t protocol_version; + + if (!grub_tpm_handle_find (&tpm_handle, &protocol_version)) + return 0; + + if (protocol_version == 1) + { + grub_efi_tpm_protocol_t *tpm; + + tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid, + GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (!tpm) + { + grub_dprintf ("tpm", "Cannot open TPM protocol\n"); + return 0; + } + return grub_tpm1_present (tpm); + } + else + { + grub_efi_tpm2_protocol_t *tpm; + + tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid, + GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (!tpm) + { + grub_dprintf ("tpm", "Cannot open TPM protocol\n"); + return 0; + } + return grub_tpm2_present (tpm); + } +} diff --git a/grub-core/commands/ieee1275/ibmvtpm.c b/grub-core/commands/ieee1275/ibmvtpm.c index 239942d27..e01759c17 100644 --- a/grub-core/commands/ieee1275/ibmvtpm.c +++ b/grub-core/commands/ieee1275/ibmvtpm.c @@ -135,16 +135,6 @@ grub_err_t grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr, const char *description) { - /* - * Call tpm_init() 'late' rather than from GRUB_MOD_INIT() so that device nodes - * can be found. - */ - grub_err_t err = tpm_init (); - - /* Absence of a TPM isn't a failure. */ - if (err != GRUB_ERR_NONE) - return GRUB_ERR_NONE; - grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n", pcr, size, description); @@ -153,3 +143,13 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr, return GRUB_ERR_NONE; } + +int +grub_tpm_present (void) +{ + /* + * Call tpm_init() 'late' rather than from GRUB_MOD_INIT() so that device nodes + * can be found. + */ + return tpm_init() == GRUB_ERR_NONE; +} diff --git a/grub-core/commands/tpm.c b/grub-core/commands/tpm.c index 3437e8e03..3128bede0 100644 --- a/grub-core/commands/tpm.c +++ b/grub-core/commands/tpm.c @@ -103,10 +103,21 @@ struct grub_file_verifier grub_tpm_verifier = { GRUB_MOD_INIT (tpm) { + /* + * Even though this now calls ibmvtpm's grub_tpm_present() from + * GRUB_MOD_INIT(), it does seem to call it late enough in the initialization + * sequence so that whatever discovered 'device nodes' before this + * GRUB_MOD_INIT() is called, enables the ibmvtpm driver to see the device + * nodes. + */ + if (!grub_tpm_present()) + return; grub_verifier_register (&grub_tpm_verifier); } GRUB_MOD_FINI (tpm) { + if (!grub_tpm_present()) + return; grub_verifier_unregister (&grub_tpm_verifier); } diff --git a/include/grub/tpm.h b/include/grub/tpm.h index 5c285cbc5..c19fcbd0a 100644 --- a/include/grub/tpm.h +++ b/include/grub/tpm.h @@ -36,4 +36,5 @@ grub_err_t grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr, const char *description); +int grub_tpm_present (void); #endif -- 2.39.2