From patchwork Thu Nov 25 06:07:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 519355 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp297789imj; Wed, 24 Nov 2021 22:04:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJz9rS2d0vVX/wybhtf2qTBH3FGyggvvxmUMkySUzEjWP6ifYm0FhtN5cZgrBJXB88P1j+o3 X-Received: by 2002:a17:906:79c3:: with SMTP id m3mr28028406ejo.332.1637820271419; Wed, 24 Nov 2021 22:04:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637820271; cv=none; d=google.com; s=arc-20160816; b=Kr9A6qd4tJ2ES0wTm5rNic3ue6B/NWteeocfsBFmDYxwN6SDioUZs2XyeyHipMn5Um R5jIE/jk9eM4bH8d9+TFL3fOPMMBj5xHrvnqqMBgq3Ej17ZJdqiqh34qwRPS0GGgKlcD wrMmyNU5etVnk0+tBlOcsmoM32jrj5LX7jB/MhzLng5zN8mdvoVvl+XiJnMQ1FfxHAhB kSf5Vbpr8cWYsC7SKgRzqknny1odqsXX3llry39n7QpILhiqQ7TNXEtbCsNjRes8bY0e 1QQstTJjNEMOPM3PoOv+7R4w54Yj/vEehEo0qrJRYTdEindjBjiltlUrnnhYtYQJSdNg KZCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=kokg5KVuX1+rbU2S2NHMXslFna9SYMe2FesJLfdoYDA=; b=HZDrfvZQC5gxnFUu/AvF8GgVZDeKx1DqjZE7bp99hb7z6lJi3Psz3DtrlGboUCmDMH gKd44Tid2AIbOc3N2mVPy5nudq+iB3drJo4LWqHKmJ309BXK8b1jCAbXq2cgUfHCc+YX 9TiIFXVnruzJaQMAK6YYfoPm+5HsEnibXCb4NZEfMlxpaPZSG0OdKYe5Lztnfoqrao2+ PExUctP2mTlmDwvQZ2tWmMEdlMtUt6NlSK7KA5ROHYTurNlQPO14BI1uSHSJeCA9XTrs YPUG5klfBUPo6SrLQMMFEvu2GzDE7rNIl0/nz2QOL/8NFfnf0fF9ZVwfQktyCpvGXb+6 K6bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BkvSs8IV; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id i24si4130766edr.262.2021.11.24.22.04.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 22:04:31 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BkvSs8IV; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E35A780FAE; Thu, 25 Nov 2021 07:04:29 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="BkvSs8IV"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EF1F3811D2; Thu, 25 Nov 2021 07:04:27 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: 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_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6742280960 for ; Thu, 25 Nov 2021 07:04:24 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=masahisa.kojima@linaro.org Received: by mail-pf1-x42e.google.com with SMTP id r130so4946066pfc.1 for ; Wed, 24 Nov 2021 22:04:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=kokg5KVuX1+rbU2S2NHMXslFna9SYMe2FesJLfdoYDA=; b=BkvSs8IVcPE/O2MudfL7Zg14aUzxZDubJO0jfefvTuXPRY3TJl/atNDE92Z1DE/e/t xu6Jl1sO3xm9mGjaZA18Bl79CRIDqghnNapdqht/pFi5b2JnvaNjh13Ynzzwv2UhH9f6 CjCvF9uPN61wHjqkStDkTriFoVKxbm+JxGGys994clrXDVjvwjQmAdP+Bsj3p+cgSNAo VAEmlUD2nXH7FWXewtqHasi90jluWBZm143YsHqyIC9jVpseh6MCvobRZKUhIWd7XmPs HnsNhSsYTaKalnQyJAn3jPckvLbu+oGSI/26ZWIrmlLPNXkC4Ad94uUSE95D3JdTIkxu bgew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=kokg5KVuX1+rbU2S2NHMXslFna9SYMe2FesJLfdoYDA=; b=OMO4l61O9WyPt4lShKW9E9wERVtsB/GCtgJ0SNYTyDpb5Dg4XLHxiRNvLCXzJlrt/5 nn6t8pxZGnj9zAY01lSpmrRLsl/FKCT4TdlJ1AoMyvuI/OVyO9qb7dDpCsDVu6pAACbK 8idgTVaJMPml9W1Qn8/LceHqlVzcf3h8ab2BZG3oDov5RiyroshmVK3JDNDT6SqWG0TT +WVPyHdx671jXn6cpjN0y0gU3RElOU1tlQWk9c4mCmVfLZ2OdMxonPCRXG+SwVoxAnOW SVNKYNXIUbZOUpFXuw5qkl9ni3mgEBOqGkVMrdM1/17rrXo3hKy+bUKiAPON9SfHGVBl to6w== X-Gm-Message-State: AOAM531pbNLSJjJEkniEi4ipscBGJWMq1Ap/hcQnnprUzI/DkXgqCc1x cfmJIBIEgFT5Yx/rV6koHBhriBzd3L8G+w== X-Received: by 2002:aa7:99cf:0:b0:49f:9d3c:ac0f with SMTP id v15-20020aa799cf000000b0049f9d3cac0fmr12169697pfi.39.1637820262502; Wed, 24 Nov 2021 22:04:22 -0800 (PST) Received: from localhost.localdomain ([2400:2411:502:a100:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id j17sm1430510pfe.174.2021.11.24.22.04.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 22:04:22 -0800 (PST) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Ruchika Gupta , Masahisa Kojima , Alexander Graf Subject: [PATCH] efi_loader: check tcg2 protocol installation outside the TCG protocol Date: Thu, 25 Nov 2021 15:07:27 +0900 Message-Id: <20211125060727.26093-1-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.37 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean There are functions that calls tcg2_agile_log_append() outside of the TCG protocol invocation (e.g tcg2_measure_pe_image). These functions must to check that tcg2 protocol is installed. If not, measurement shall be skipped. Together with above change, this commit also removes the unnecessary tcg2_uninit() call in efi_tcg2_register(), refactors efi_tcg2_register() not to include the process that is not related to the tcg2 protocol registration. Signed-off-by: Masahisa Kojima --- include/efi_loader.h | 4 ++ lib/efi_loader/efi_setup.c | 3 ++ lib/efi_loader/efi_tcg2.c | 89 +++++++++++++++++++++++++++++++------- 3 files changed, 81 insertions(+), 15 deletions(-) diff --git a/include/efi_loader.h b/include/efi_loader.h index d52e399841..fe29c10906 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -525,6 +525,10 @@ efi_status_t efi_disk_register(void); efi_status_t efi_rng_register(void); /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */ efi_status_t efi_tcg2_register(void); +/* Called by efi_init_obj_list() to do the required setup for the measurement */ +efi_status_t efi_tcg2_setup_measurement(void); +/* Called by efi_init_obj_list() to do initial measurement */ +efi_status_t efi_tcg2_do_initial_measurement(void); /* measure the pe-coff image, extend PCR and add Event Log */ efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size, struct efi_loaded_image_obj *handle, diff --git a/lib/efi_loader/efi_setup.c b/lib/efi_loader/efi_setup.c index a2338d74af..781d10590d 100644 --- a/lib/efi_loader/efi_setup.c +++ b/lib/efi_loader/efi_setup.c @@ -271,6 +271,9 @@ efi_status_t efi_init_obj_list(void) ret = efi_tcg2_register(); if (ret != EFI_SUCCESS) goto out; + + efi_tcg2_setup_measurement(); + efi_tcg2_do_initial_measurement(); } /* Secure boot */ diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c index 8c1f22e337..46300cae79 100644 --- a/lib/efi_loader/efi_tcg2.c +++ b/lib/efi_loader/efi_tcg2.c @@ -153,6 +153,20 @@ static u16 alg_to_len(u16 hash_alg) return 0; } +/** + * is_tcg2_protocol_installed - chech whether tcg2 protocol is installed + * + * @Return: true if tcg2 protocol is installed, false if not + */ +bool is_tcg2_protocol_installed(void) +{ + struct efi_handler *handler; + efi_status_t ret; + + ret = efi_search_protocol(efi_root, &efi_guid_tcg2_protocol, &handler); + return ((ret == EFI_SUCCESS) ? true : false); +} + static u32 tcg_event_final_size(struct tpml_digest_values *digest_list) { u32 len; @@ -886,6 +900,9 @@ efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size, IMAGE_NT_HEADERS32 *nt; struct efi_handler *handler; + if (!is_tcg2_protocol_installed()) + return EFI_NOT_READY; + ret = platform_get_tpm2_device(&dev); if (ret != EFI_SUCCESS) return ret; @@ -1759,6 +1776,9 @@ efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *ha u32 event = 0; struct smbios_entry *entry; + if (!is_tcg2_protocol_installed()) + return EFI_NOT_READY; + if (tcg2_efi_app_invoked) return EFI_SUCCESS; @@ -1809,6 +1829,9 @@ efi_status_t efi_tcg2_measure_efi_app_exit(void) efi_status_t ret; struct udevice *dev; + if (!is_tcg2_protocol_installed()) + return EFI_NOT_READY; + ret = platform_get_tpm2_device(&dev); if (ret != EFI_SUCCESS) return ret; @@ -1833,6 +1856,11 @@ efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context) EFI_ENTRY("%p, %p", event, context); + if (!is_tcg2_protocol_installed()) { + ret = EFI_NOT_READY; + goto out; + } + event_log.ebs_called = true; ret = platform_get_tpm2_device(&dev); if (ret != EFI_SUCCESS) @@ -1863,6 +1891,9 @@ efi_status_t efi_tcg2_notify_exit_boot_services_failed(void) struct udevice *dev; efi_status_t ret; + if (!is_tcg2_protocol_installed()) + return EFI_NOT_READY; + ret = platform_get_tpm2_device(&dev); if (ret != EFI_SUCCESS) goto out; @@ -1932,6 +1963,45 @@ error: return ret; } +/** + * efi_tcg2_setup_measurement() - setup for the measurement + * + * Return: status code + */ +efi_status_t efi_tcg2_setup_measurement(void) +{ + efi_status_t ret; + struct efi_event *event; + + ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK, + efi_tcg2_notify_exit_boot_services, NULL, + NULL, &event); + + return ret; +} + +/** + * efi_tcg2_do_initial_measurement() - do initial measurement + * + * Return: status code + */ +efi_status_t efi_tcg2_do_initial_measurement(void) +{ + efi_status_t ret; + struct udevice *dev; + + ret = platform_get_tpm2_device(&dev); + if (ret != EFI_SUCCESS) + goto out; + + ret = tcg2_measure_secure_boot_variable(dev); + if (ret != EFI_SUCCESS) + goto out; + +out: + return ret; +} + /** * efi_tcg2_register() - register EFI_TCG2_PROTOCOL * @@ -1943,7 +2013,6 @@ efi_status_t efi_tcg2_register(void) { efi_status_t ret = EFI_SUCCESS; struct udevice *dev; - struct efi_event *event; u32 err; ret = platform_get_tpm2_device(&dev); @@ -1969,6 +2038,10 @@ efi_status_t efi_tcg2_register(void) goto fail; } + /* + * efi_add_protocol() is called at last on purpose. + * tcg2_uninit() does not uninstall the tcg2 protocol, but it is intended. + */ ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol, (void *)&efi_tcg2_protocol); if (ret != EFI_SUCCESS) { @@ -1976,20 +2049,6 @@ efi_status_t efi_tcg2_register(void) goto fail; } - ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK, - efi_tcg2_notify_exit_boot_services, NULL, - NULL, &event); - if (ret != EFI_SUCCESS) { - tcg2_uninit(); - goto fail; - } - - ret = tcg2_measure_secure_boot_variable(dev); - if (ret != EFI_SUCCESS) { - tcg2_uninit(); - goto fail; - } - return ret; fail: