From patchwork Fri Nov 26 01:31:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 519676 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp1686041imj; Thu, 25 Nov 2021 17:27:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJzyi0sxyq+6NWcfpVDEcIjC5e9LsexqDu4+Bdr6R1OGMxPE/FI+i+zX4/anT4KK96Amiti/ X-Received: by 2002:a05:6402:2686:: with SMTP id w6mr45017974edd.141.1637890070832; Thu, 25 Nov 2021 17:27:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637890070; cv=none; d=google.com; s=arc-20160816; b=WwAEv6LYMvUrZPB608mUjuQdxnQ3bMQyZjVDbrYriLPUnGyqjw4hJMCWar0FeDG3Vy +FHBQfeZt9FeBUDI9HVugPQNUvx+hD9upLmuldKJ6IYR7AgWRq9K9159aP2Onrl5e7f1 mam/97SOG+gmU3GPJ3uD3hoNq0/60A9cVtfydVUd3iHVq+E6SpL4t0QKX+yOslgjWI5i NH0EOCG5NC80tQ3I6zay068VPWMjEDVmnCglxmjCTaFr9Tsh7moWfDnKBcO4gw8cAc/P z+jRpLvqG1WODzWcv8Fo/1VHx/7NloumTyzrEK2y+rBW6eT9H5PJasRPT9K9fo2m3/AV Efpw== 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=6uaT2wJERNGoeN1F/+3R0NxKD41ouz/c+LcaLzdaMw0=; b=ewggypZiNb3HEHEZhwIFtUFnDL1uAg7qO0N3+t+5KbmyyICNqlgxZ55U4aW3onEgNN NPVrXChoJrydpAj8uMzhGy4rb+3FT6dLSsEagCPTB3cXU8lc9D/2BVASLjHqNfjsHXDW 4oO1V0BH/nSqnOXAi2BE4XZQ988jFcHXkjmQckZ2bCBNzAZcsSz92HZeqlN/g7OWcmVN IDiqbsyCvPr7j/5ru1JLIS24XofnaPxBY3TUJhR8wf3cb3csLBDJT11giJ+AgXYRWW9O PKSsiGWgymA5JPslUA4sCr4gpwtzFHmdOk5HNu/5BpW44Ux8y1sZOnfMsIGz2ygF4znQ 4gjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KTf8BDJr; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id yc12si8433245ejb.695.2021.11.25.17.27.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Nov 2021 17:27:50 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KTf8BDJr; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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 A318D81DB4; Fri, 26 Nov 2021 02:27:46 +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="KTf8BDJr"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 56D4182FB4; Fri, 26 Nov 2021 02:27:44 +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-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) (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 6B54F81294 for ; Fri, 26 Nov 2021 02:27:40 +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-pj1-x102e.google.com with SMTP id h24so6190839pjq.2 for ; Thu, 25 Nov 2021 17:27:40 -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=6uaT2wJERNGoeN1F/+3R0NxKD41ouz/c+LcaLzdaMw0=; b=KTf8BDJrVpbbDYZn7hM+JAZX3ZXupZwUxMnEoWcyUb+bAVNobustj3iuQ79173R2a4 YDIo1ikcHDn385c0UMn5XLPtm2DF/mo4P2L4J2YHK6BlB1GqbcLCQ8dCPqKjRFOyIjwQ ZM/riWpJWqEAzjkUuy0T/7TiZqRFCW45zfYuWXXHiCcBE+WKoinSaoLhIG55dawYxokT I5x1kceRT7z6ptxuhVf26ZGml/uOwi2SlKxqrAJXMrYy0D9NbIUdscj0nooAF8jtwXzJ 25C5Q1pvKyL5aR3f2m4q26ujR707/jU2Prcw3Jcg8m3xJLB+A63pXDi/Pqm8nrLXRFvW s3bA== 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=6uaT2wJERNGoeN1F/+3R0NxKD41ouz/c+LcaLzdaMw0=; b=wM17QTTBp5G30K4Zz1y8Ho+ZCx8BOlycmfp5nQv3VyVkF9l6eJq9T2N7Ml4st3Sf8J RxQ4zrSYEwcuTIlwZ6fkauxMB3tLOWBebkFS1zGQJsB6BR5MtmOT5iRb9F9A/toK0Nvd xMhCab0jIIB2GDE/rxDl7xM796O5gOTj7QHeju1eWmrdz6uGTyV+Ex61DL+n+dvyJpKo diSR2eNBgnXk4u9HWVVGcOyNUJTvNMpc5dhJRMbrcgm4gSg6HdNmxEDMHjUm3j9tdTvG jOHpN+qgh9QF1i9tuLrqkJbZIjJv9IftMwgX7n75g66ODVKeY8W3Uq9P2zRCuof0gXwa mi4A== X-Gm-Message-State: AOAM532W7uN3Qg3lzQi0LX4F6E8YzqtFfRh7H9NbK1YRYATMgvb6p6tW nJg5cV3Deyt1U1BBsJ6lBWMyotlB+0BpbQ== X-Received: by 2002:a17:90b:4b8d:: with SMTP id lr13mr11760968pjb.239.1637890058441; Thu, 25 Nov 2021 17:27:38 -0800 (PST) Received: from localhost.localdomain ([2400:2411:502:a100:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id md6sm3802503pjb.22.2021.11.25.17.27.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Nov 2021 17:27:37 -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 v3] efi_loader: check tcg2 protocol installation outside the TCG protocol Date: Fri, 26 Nov 2021 10:31:16 +0900 Message-Id: <20211126013116.3781-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 --- Changes in v3: - add static qualifier to is_tcg2_protocol_installed() - simplify is_tcg2_protocol_installed() return handling Changes in v2: - rebased on top of the TF-A eventlog handover support 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 b44eed0ec9..2196af49a6 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 + */ +static 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); +} + static u32 tcg_event_final_size(struct tpml_digest_values *digest_list) { u32 len; @@ -962,6 +976,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; @@ -2140,6 +2157,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; @@ -2190,6 +2210,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; @@ -2214,6 +2237,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) @@ -2244,6 +2272,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; @@ -2313,6 +2344,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 * @@ -2324,7 +2394,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); @@ -2344,6 +2413,10 @@ efi_status_t efi_tcg2_register(void) if (ret != EFI_SUCCESS) 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) { @@ -2351,20 +2424,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: