Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nYin9-0006HN-W4 for mharc-grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:54 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50454) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nYin3-0006DB-Gl for grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:46 -0400 Received: from [2607:f8b0:4864:20::62b] (port=36635 helo=mail-pl1-x62b.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nYin1-0005JW-Iz for grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:45 -0400 Received: by mail-pl1-x62b.google.com with SMTP id m18so9056900plx.3 for ; Sun, 27 Mar 2022 23:23:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MTcqaDCD4Xz61gcGzTlFBZxgjxQvtbEpnaVFA7GXigs=; b=kOuEqO0UE4AMEeAaTdt8kT10aSdGSYOLzWkpR28dVT4DH9GH1ncT7BvIP2k69dgkKF K/tUKybKZY8XpgRxVTITGNAMbCVTjdswIPp5jwy/J+jJIL8UH7mKJ4GezoqaJ3A18EF9 VnZYYCT8R06V0vrsPwmwI2ThDFV7ja+1FouwE= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=MTcqaDCD4Xz61gcGzTlFBZxgjxQvtbEpnaVFA7GXigs=; b=IVIGKz2BLulmkYAfBNG6v2vQorh8SOvXIpOd5Y+H3CP3QSfemHkewf1QtWHvGr+U6B NvDnqNeorIBTn/nlMjXP3NYEabxCHOLOtSAFJldL56X0wrroltGjcbl1AR2lIBEuJOWg GoPx3QBcY+zE+FwlxKfZrZ7AuyWpfayM4fbYbuuoEkRklc0EfycMEc5TofcEK4r4jyDv 8mFDgUkJnvQZv7YkdAGH9oCzdyZ4hNYbvyQZ6N4NarIT23CgbW1X1QvezRQN+DJWQl56 93iMZ9Fn3xS035SS8Q0Yok3OSfUyMTrA2KIp3OP5L5qW2WkmtwMBnE7CSkgPclT4YdkS dddw== X-Gm-Message-State: AOAM532JKJiEzU/RwMuZ43t/7ch7o8Lz8kE4EIMahslajdpUmdGmP2EW b98zv6rtIJgZlLBdI8OxBnL7i1DRwKN8/A== X-Google-Smtp-Source: ABdhPJwiFbWEaN3QrqgF9n88LChriotvePcLo2kt5toy/yiVgDxQvyJCkAXOotxsMKH6udcjzE046Q== X-Received: by 2002:a17:902:7887:b0:154:4f3f:ec54 with SMTP id q7-20020a170902788700b001544f3fec54mr24484810pll.156.1648448621946; Sun, 27 Mar 2022 23:23:41 -0700 (PDT) Received: from localhost ([2001:4479:e000:e400:743e:bc3e:ec72:bf20]) by smtp.gmail.com with ESMTPSA id oo17-20020a17090b1c9100b001bf0ccc59c2sm20018530pjb.16.2022.03.27.23.23.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Mar 2022 23:23:41 -0700 (PDT) From: Daniel Axtens To: grub-devel@gnu.org Cc: leif@nuviainc.com, stefanb@linux.ibm.com, ps@pks.im, dkiper@net-space.pl, Eric Snowberg , Daniel Axtens Subject: [PATCH v2 15/15] ibmvtpm: Add support for trusted boot using a vTPM 2.0 Date: Mon, 28 Mar 2022 17:22:40 +1100 Message-Id: <20220328062240.878781-16-dja@axtens.net> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220328062240.878781-1-dja@axtens.net> References: <20220328062240.878781-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::62b (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=dja@axtens.net; helo=mail-pl1-x62b.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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, 28 Mar 2022 06:23:46 -0000 From: Stefan Berger Add support for trusted boot using a vTPM 2.0 on the IBM IEEE1275 PowerPC platform. With this patch grub now measures text and binary data into the TPM's PCRs 8 and 9 in the same way as the x86_64 platform does. This patch requires Daniel Axtens's patches for claiming more memory. For vTPM support to work on PowerVM, system driver levels 1010.30 or 1020.00 are required. Note: Previous versions of firmware levels with the 2hash-ext-log API call have a bug that, once this API call is invoked, has the effect of disabling the vTPM driver under Linux causing an error message to be displayed in the Linux kernel log. Those users will have to update their machines to the firmware levels mentioned above. Cc: Eric Snowberg Signed-off-by: Stefan Berger Signed-off-by: Daniel Axtens --- docs/grub.texi | 3 +- grub-core/Makefile.core.def | 7 ++ grub-core/commands/ieee1275/ibmvtpm.c | 152 ++++++++++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 3 + 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 grub-core/commands/ieee1275/ibmvtpm.c diff --git a/docs/grub.texi b/docs/grub.texi index caba8befb4ef..54063430c717 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -6084,7 +6084,8 @@ tpm module is loaded. As such it is recommended that the tpm module be built into @file{core.img} in order to avoid a potential gap in measurement between @file{core.img} being loaded and the tpm module being loaded. -Measured boot is currently only supported on EFI platforms. +Measured boot is currently only supported on EFI and IBM IEEE1275 PowerPC +platforms. @node Lockdown @section Lockdown when booting on a secure setup diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index ce7d79d38e5b..bbbbeaf43773 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1121,6 +1121,13 @@ module = { enable = powerpc_ieee1275; }; +module = { + name = tpm; + common = commands/tpm.c; + ieee1275 = commands/ieee1275/ibmvtpm.c; + enable = powerpc_ieee1275; +}; + module = { name = terminal; common = commands/terminal.c; diff --git a/grub-core/commands/ieee1275/ibmvtpm.c b/grub-core/commands/ieee1275/ibmvtpm.c new file mode 100644 index 000000000000..e68b8448bc00 --- /dev/null +++ b/grub-core/commands/ieee1275/ibmvtpm.c @@ -0,0 +1,152 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2021 Free Software Foundation, Inc. + * Copyright (C) 2021 IBM Corporation + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + * + * IBM vTPM support code. + */ + +#include +#include +#include +#include +#include +#include + +static grub_ieee1275_ihandle_t tpm_ihandle; +static grub_uint8_t tpm_version; + +#define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_ihandle_t)0) + +static void +tpm_get_tpm_version (void) +{ + grub_ieee1275_phandle_t vtpm; + char buffer[20]; + + if (!grub_ieee1275_finddevice ("/vdevice/vtpm", &vtpm) && + !grub_ieee1275_get_property (vtpm, "compatible", buffer, + sizeof (buffer), NULL) && + !grub_strcmp (buffer, "IBM,vtpm20")) + tpm_version = 2; +} + +static grub_err_t +tpm_init (void) +{ + static int init_success = 0; + + if (!init_success) + { + if (grub_ieee1275_open ("/vdevice/vtpm", &tpm_ihandle) < 0) { + tpm_ihandle = IEEE1275_IHANDLE_INVALID; + return GRUB_ERR_UNKNOWN_DEVICE; + } + + init_success = 1; + + tpm_get_tpm_version (); + } + + return GRUB_ERR_NONE; +} + +static int +ibmvtpm_2hash_ext_log (grub_uint8_t pcrindex, + grub_uint32_t eventtype, + const char *description, + grub_size_t description_size, + void *buf, grub_size_t size) +{ + struct tpm_2hash_ext_log + { + struct grub_ieee1275_common_hdr common; + grub_ieee1275_cell_t method; + grub_ieee1275_cell_t ihandle; + grub_ieee1275_cell_t size; + grub_ieee1275_cell_t buf; + grub_ieee1275_cell_t description_size; + grub_ieee1275_cell_t description; + grub_ieee1275_cell_t eventtype; + grub_ieee1275_cell_t pcrindex; + grub_ieee1275_cell_t catch_result; + grub_ieee1275_cell_t rc; + } + args; + + INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 2); + args.method = (grub_ieee1275_cell_t) "2hash-ext-log"; + args.ihandle = tpm_ihandle; + args.pcrindex = pcrindex; + args.eventtype = eventtype; + args.description = (grub_ieee1275_cell_t) description; + args.description_size = description_size; + args.buf = (grub_ieee1275_cell_t) buf; + args.size = (grub_ieee1275_cell_t) size; + + if (IEEE1275_CALL_ENTRY_FN (&args) == -1) + return -1; + + /* + * catch_result is set if firmware does not support 2hash-ext-log + * rc is GRUB_IEEE1275_CELL_FALSE (0) on failure + */ + if ((args.catch_result) || args.rc == GRUB_IEEE1275_CELL_FALSE) + return -1; + + return 0; +} + +static grub_err_t +tpm2_log_event (unsigned char *buf, + grub_size_t size, grub_uint8_t pcr, + const char *description) +{ + static int error_displayed = 0; + int err; + + err = ibmvtpm_2hash_ext_log (pcr, EV_IPL, + description, + grub_strlen(description) + 1, + buf, size); + if (err && !error_displayed) + { + error_displayed++; + return grub_error (GRUB_ERR_BAD_DEVICE, + "2HASH-EXT-LOG failed: Firmware is likely too old.\n"); + } + + return GRUB_ERR_NONE; +} + +grub_err_t +grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr, + const char *description) +{ + 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); + + if (tpm_version == 2) + return tpm2_log_event (buf, size, pcr, description); + + return GRUB_ERR_NONE; +} diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h index b5c916d1de58..5002195fce0d 100644 --- a/include/grub/ieee1275/ieee1275.h +++ b/include/grub/ieee1275/ieee1275.h @@ -24,6 +24,9 @@ #include #include +#define GRUB_IEEE1275_CELL_FALSE ((grub_ieee1275_cell_t) 0) +#define GRUB_IEEE1275_CELL_TRUE ((grub_ieee1275_cell_t) -1) + struct grub_ieee1275_mem_region { unsigned int start; -- 2.32.0