Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1opcOA-00037D-OW for mharc-grub-devel@gnu.org; Mon, 31 Oct 2022 17:32:21 -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 1opcNx-0001kN-Dv for grub-devel@gnu.org; Mon, 31 Oct 2022 17:31:57 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1opcNv-00062o-Mq for grub-devel@gnu.org; Mon, 31 Oct 2022 17:31:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667251914; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZHdOBW8M49KJ6xPgpdtoOqtcfh29v+YFEF1EreEBXyA=; b=iMGyOthOP/guh2iWDvlizejOnJD5gHOJWSW9CgUWqA2T+KroTyljFUHIgmr+AsGLKONmBE QLKeLYKMHIXqpVbnA2h82+JAQUYMmtvFr71C8IfevYmsvvpIJXmdKjvivm/QOxMhpgGYhG 2w3NrIrLBaOYJTZaKwfMVP67kFo83m0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-590-bv_uLFUcPXqlRTgWFKMFpA-1; Mon, 31 Oct 2022 17:31:50 -0400 X-MC-Unique: bv_uLFUcPXqlRTgWFKMFpA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1A8D9185A7A3; Mon, 31 Oct 2022 21:31:50 +0000 (UTC) Received: from eesha.redhat.com (unknown [10.22.35.111]) by smtp.corp.redhat.com (Postfix) with ESMTP id 86C2B35429; Mon, 31 Oct 2022 21:31:49 +0000 (UTC) From: Robbie Harwood To: grub-devel@gnu.org Cc: Robbie Harwood , dkiper@net-space.pl, James.Bottomley@HansenPartnership.com, daniel.kiper@oracle.com, mathieu.trudel-lapierre@canonical.com, mtottenh@akamai.com, mathieu.tl@gmail.com, Javier Martinez Canillas Subject: [PATCH v3 1/1] tpm: Don't propagate measurement failures to the verifiers layer Date: Mon, 31 Oct 2022 17:31:40 -0400 Message-Id: <20221031213140.175237-2-rharwood@redhat.com> In-Reply-To: <20221031213140.175237-1-rharwood@redhat.com> References: <20221031213140.175237-1-rharwood@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Received-SPF: pass client-ip=170.10.133.124; envelope-from=rharwood@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.048, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=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, 31 Oct 2022 21:31:57 -0000 Currently if an EFI firmware fails to do a TPM measurement for a file, the error will be propagated to the verifiers framework which will prevent it to be opened. This mean that buggy firmwares will lead to the system not booting because files won't be allowed to be loaded. But a failure to do a TPM measurement isn't expected to be a fatal error that causes the system to be unbootable. To avoid this, don't return errors from .write and .verify_string callbacks and just print a debug message in the case of a TPM measurement failure. Add an environment variable (tpm_fail_fatal) to restore the previous behavior. Also-authored-by: Javier Martinez Canillas Signed-off-by: Javier Martinez Canillas Signed-off-by: Robbie Harwood --- docs/grub.texi | 9 +++++++++ grub-core/commands/tpm.c | 29 ++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/docs/grub.texi b/docs/grub.texi index 2d6cd83580..eb43d8970d 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -3318,6 +3318,7 @@ These variables have special meaning to GRUB. * theme:: * timeout:: * timeout_style:: +* tpm_fail_fatal:: @end menu @@ -3825,6 +3826,14 @@ displaying the menu. See the documentation of @samp{GRUB_TIMEOUT_STYLE} (@pxref{Simple configuration}) for details. +@node tpm_fail_fatal +@subsection tpm_fail_fatal + +If this variable is enabled, TPM measurements that fail will be treated +as fatal. Otherwise, they will merely be debug-logged and boot will +continue. + + @node Environment block @section The GRUB environment block diff --git a/grub-core/commands/tpm.c b/grub-core/commands/tpm.c index 2052c36eab..ca088055dd 100644 --- a/grub-core/commands/tpm.c +++ b/grub-core/commands/tpm.c @@ -18,6 +18,7 @@ * Core TPM support code. */ +#include #include #include #include @@ -26,6 +27,7 @@ #include #include #include +#include GRUB_MOD_LICENSE ("GPLv3+"); @@ -39,10 +41,27 @@ grub_tpm_verify_init (grub_file_t io, return GRUB_ERR_NONE; } +static inline bool +is_tpm_fail_fatal (void) +{ + const char *val = grub_env_get ("tpm_fail_fatal"); + + if (val == NULL || grub_strlen (val) < 1 || grub_strcmp (val, "0") == 0 || + grub_strcmp (val, "false") == 0) + return false; + return true; +} + static grub_err_t grub_tpm_verify_write (void *context, void *buf, grub_size_t size) { - return grub_tpm_measure (buf, size, GRUB_BINARY_PCR, context); + grub_err_t status = grub_tpm_measure (buf, size, GRUB_BINARY_PCR, context); + + if (status == GRUB_ERR_NONE) + return GRUB_ERR_NONE; + + grub_dprintf ("tpm", "Measuring buffer failed: %d\n", status); + return is_tpm_fail_fatal () ? status : GRUB_ERR_NONE; } static grub_err_t @@ -66,7 +85,7 @@ grub_tpm_verify_string (char *str, enum grub_verify_string_type type) } description = grub_malloc (grub_strlen (str) + grub_strlen (prefix) + 1); if (!description) - return grub_errno; + return GRUB_ERR_NONE; grub_memcpy (description, prefix, grub_strlen (prefix)); grub_memcpy (description + grub_strlen (prefix), str, grub_strlen (str) + 1); @@ -74,7 +93,11 @@ grub_tpm_verify_string (char *str, enum grub_verify_string_type type) grub_tpm_measure ((unsigned char *) str, grub_strlen (str), GRUB_STRING_PCR, description); grub_free (description); - return status; + if (status == GRUB_ERR_NONE) + return GRUB_ERR_NONE; + + grub_dprintf ("tpm", "Measuring string %s failed: %d\n", str, status); + return is_tpm_fail_fatal () ? status : GRUB_ERR_NONE; } struct grub_file_verifier grub_tpm_verifier = { -- 2.35.1