Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1m5w5U-0002bw-PM for mharc-grub-devel@gnu.org; Tue, 20 Jul 2021 16:11:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58922) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5w5Q-0002XZ-Hs for grub-devel@gnu.org; Tue, 20 Jul 2021 16:11:30 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:47328 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5w5K-0001kB-2M for grub-devel@gnu.org; Tue, 20 Jul 2021 16:11:28 -0400 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16KK4PsF071089; Tue, 20 Jul 2021 16:11:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=subject : to : cc : references : from : message-id : date : in-reply-to : content-type : content-transfer-encoding : mime-version; s=pp1; bh=NvvgGu5eqlp2dSbN4rJnQfj8JdGfmZ0KECliI6dH5qw=; b=cN8q483LHHq+q2gyoPYlm4HfOsMBO9t9kiX4C1xvi/I+IrTHcppwuOTsIwEB3NnoAl+9 rfjPsKRYATp6OoZHqcR+Z7IY/DD3EUzHQk1+3Y1tx+bpUAKJxdlPzhZaEsgDYufdqr/n bPQHGP81KZtZrVbW2+TyDIcgD2YYGbdSPWSBHxDU/r0feemSkz9K+fNuWWrjWhpsqCO6 EZ2a5bDRayXbIrx8QwoZino+nuzolsIt8NecalL8eP8V9IdxHpdwUA29i2wv/DrvbNtl p+ZK//FPSsLxkki43peSx/luhpyHDFwFANFBqxRluTyPdDsztdjNcWVuphKiKfKqywnY ZQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 39x4fr9ay1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 20 Jul 2021 16:11:19 -0400 Received: from m0098420.ppops.net (m0098420.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 16KK4Sgv071517; Tue, 20 Jul 2021 16:11:18 -0400 Received: from ppma03dal.us.ibm.com (b.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.11]) by mx0b-001b2d01.pphosted.com with ESMTP id 39x4fr9axt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 20 Jul 2021 16:11:18 -0400 Received: from pps.filterd (ppma03dal.us.ibm.com [127.0.0.1]) by ppma03dal.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 16KK7DdU002546; Tue, 20 Jul 2021 20:11:18 GMT Received: from b03cxnp08028.gho.boulder.ibm.com (b03cxnp08028.gho.boulder.ibm.com [9.17.130.20]) by ppma03dal.us.ibm.com with ESMTP id 39upucve4r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 20 Jul 2021 20:11:17 +0000 Received: from b03ledav003.gho.boulder.ibm.com (b03ledav003.gho.boulder.ibm.com [9.17.130.234]) by b03cxnp08028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 16KKBGfW21692698 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 20 Jul 2021 20:11:16 GMT Received: from b03ledav003.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8A4CA6A051; Tue, 20 Jul 2021 20:11:16 +0000 (GMT) Received: from b03ledav003.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 115286A05D; Tue, 20 Jul 2021 20:11:15 +0000 (GMT) Received: from [9.47.158.152] (unknown [9.47.158.152]) by b03ledav003.gho.boulder.ibm.com (Postfix) with ESMTP; Tue, 20 Jul 2021 20:11:15 +0000 (GMT) Subject: Re: [PATCH] ibmvtpm: Add support for trusted boot using a vTPM 2.0 To: Daniel Kiper , Stefan Berger Cc: grub-devel@gnu.org, rashmica.g@gmail.com, alastair@d-silva.org, nayna@linux.ibm.com, dja@axtens.net, eric.snowberg@oracle.com References: <20210712190219.208790-1-stefanb@linux.vnet.ibm.com> <20210714161657.g2za2i3oyrwgekae@tomti.i.net-space.pl> From: Stefan Berger Message-ID: <39f8a035-563d-dfc1-83ff-b6ed2b0362fc@linux.ibm.com> Date: Tue, 20 Jul 2021 16:11:15 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 In-Reply-To: <20210714161657.g2za2i3oyrwgekae@tomti.i.net-space.pl> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US X-TM-AS-GCONF: 00 X-Proofpoint-GUID: yEE0njDnZ9P-H07nq1z_jTQhOti3Pmu_ X-Proofpoint-ORIG-GUID: 8WI1LAzkf1URMKY7dFGLE7oZ3L-I38iK Content-Transfer-Encoding: 8bit X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391, 18.0.790 definitions=2021-07-20_13:2021-07-19, 2021-07-20 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 lowpriorityscore=0 clxscore=1015 priorityscore=1501 mlxlogscore=999 malwarescore=0 impostorscore=0 phishscore=0 bulkscore=0 adultscore=0 suspectscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2107200128 Received-SPF: pass client-ip=148.163.158.5; envelope-from=stefanb@linux.ibm.com; helo=mx0a-001b2d01.pphosted.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, NICE_REPLY_A=-0.001, 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.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Jul 2021 20:11:30 -0000 On 7/14/21 12:16 PM, Daniel Kiper wrote: > Signed-off-by: Stefan Berger >> --- >> grub-core/Makefile.core.def | 8 ++ >> grub-core/commands/ieee1275/ibmvtpm.c | 118 ++++++++++++++++++++++++++ >> grub-core/kern/ieee1275/ibmvtpm.c | 62 ++++++++++++++ >> include/grub/ieee1275/ibmvtpm.h | 32 +++++++ >> 4 files changed, 220 insertions(+) >> create mode 100644 grub-core/commands/ieee1275/ibmvtpm.c >> create mode 100644 grub-core/kern/ieee1275/ibmvtpm.c >> create mode 100644 include/grub/ieee1275/ibmvtpm.h >> >> diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def >> index 3f3459b2c..e2a64f8ff 100644 >> --- a/grub-core/Makefile.core.def >> +++ b/grub-core/Makefile.core.def >> @@ -1166,6 +1166,14 @@ module = { >> enable = powerpc_ieee1275; >> }; >> >> +module = { >> + name = tpm; >> + common = commands/tpm.c; >> + common = kern/ieee1275/ibmvtpm.c; >> + ieee1275 = commands/ieee1275/ibmvtpm.c; > s/ieee1275/common/? It looks like this now: module = {   name = tpm;   common = commands/tpm.c;   ieee1275 = commands/ieee1275/ibmvtpm.c;   enable = powerpc_ieee1275; }; > >> + 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 000000000..9b06c76d9 >> --- /dev/null >> +++ b/grub-core/commands/ieee1275/ibmvtpm.c >> @@ -0,0 +1,118 @@ >> +/* >> + * GRUB -- GRand Unified Bootloader >> + * Copyright (C) 2021 IBM Corporation > Copyright (C) 2021 Free Software Foundation, Inc. > > Or both if you strongly need IBM... Doing both. > >> + * 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 >> +#include >> + >> +static grub_ieee1275_ihandle_t grub_tpm_ihandle; >> +static grub_uint8_t grub_tpm_version; >> + >> +static void grub_ieee1275_tpm_init (grub_ieee1275_ihandle_t *tpm_ihandle) > You can drop from static functions, variables, etc. names "grub_" prefix. Done. > >> +{ >> + static int init_called = 0; >> + >> + if (!init_called) { >> + init_called = 1; >> + grub_ieee1275_open ("/vdevice/vtpm", &grub_tpm_ihandle); >> + } > Incorrect formatting. It should be > > if (!init_called) > { > init_called = 1; > grub_ieee1275_open ("/vdevice/vtpm", &grub_tpm_ihandle); > } > > You can always refer to grub-core/kern/efi/sb.c to get formatting > examples for various pieces of the GRUB code. > > Please fix similar issues below too. Done. > >> + *tpm_ihandle = grub_tpm_ihandle; >> +} >> + >> +static grub_err_t >> +grub_tpm_get_tpm_version (grub_uint8_t *protocol_version) >> +{ >> + static int version_probed = 0; >> + grub_ieee1275_phandle_t vtpm; >> + char buffer[20]; >> + grub_ssize_t buffer_size; >> + >> + if (!version_probed) { >> + version_probed = 1; >> + if (!grub_ieee1275_finddevice ("/vdevice/vtpm", &vtpm) && >> + !grub_ieee1275_get_property (vtpm, "compatible", buffer, >> + sizeof buffer, &buffer_size) && >> + !grub_strcmp (buffer, "IBM,vtpm20")) { >> + grub_tpm_version = 2; >> + } >> + } >> + *protocol_version = grub_tpm_version; >> + >> + return 0; > You ignore this value later. > > I think the prototype of this function should be following: > grub_uint8_t get_tpm_version (void) > > Which means you should return the TPM version. Done > >> +} >> + >> +static grub_int8_t > static grub_err_t Done > >> +grub_tpm_handle_find (grub_ieee1275_ihandle_t *tpm_handle, >> + grub_uint8_t *protocol_version) >> +{ >> + grub_ieee1275_tpm_init (tpm_handle); >> + if (*tpm_handle == NULL) >> + return 0; > return GRUB_ERR_UNKNOWN_DEVICE; Done > >> + grub_tpm_get_tpm_version (protocol_version); >> + >> + return 1; > return GRUB_ERR_NONE; Done > >> +} >> + >> +static grub_err_t >> +grub_tpm2_log_event (grub_ieee1275_ihandle_t tpm_handle, unsigned char *buf, >> + grub_size_t size, grub_uint8_t pcr, >> + const char *description) >> +{ >> + static int error_displayed; >> + bool succ; > s/bool/grub_err_t/ > >> + >> + succ = grub_ieee1275_ibmvtpm_2hash_ext_log (tpm_handle, >> + pcr, EV_IPL, >> + description, >> + grub_strlen(description) + 1, >> + buf, size); >> + if (!succ && !error_displayed) { >> + error_displayed = 1; >> + grub_printf("2HASH-EXT-LOG failed: Firmware is likely too old.\n"); > s/grub_printf/grub_error/ > >> + } >> + >> + return 0; >> +} >> + >> +grub_err_t >> +grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr, >> + const char *description) >> +{ >> + grub_ieee1275_ihandle_t tpm_handle; >> + grub_uint8_t protocol_version = 0; > It seems to me that you have the same information in the grub_tpm_version > global variable. So, I think you should you use it and drop all instances > of protocol_version. Done > >> + /* Absence of a TPM isn't a failure. */ >> + if (!grub_tpm_handle_find (&tpm_handle, &protocol_version)) >> + return 0; >> + >> + grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n", >> + pcr, size, description); >> + >> + if (protocol_version == 2) >> + return grub_tpm2_log_event (tpm_handle, buf, size, pcr, description); >> + >> + return 0; >> +} >> diff --git a/grub-core/kern/ieee1275/ibmvtpm.c b/grub-core/kern/ieee1275/ibmvtpm.c >> new file mode 100644 >> index 000000000..525a792b4 >> --- /dev/null >> +++ b/grub-core/kern/ieee1275/ibmvtpm.c >> @@ -0,0 +1,62 @@ >> +/* ibmvtpm.c - Client interface to access the IBM vTPM */ >> +/* >> + * GRUB -- GRand Unified Bootloader >> + * 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 . >> + */ >> + >> +#include >> +#include >> +#include >> +#include >> + >> +bool > s/bool/grub_err_t/ All the other ones ( grub-core/kern/ieee1275/ieee1275.c ) actually have int. Using int now. > >> +grub_ieee1275_ibmvtpm_2hash_ext_log (grub_uint32_t ihandle, >> + grub_uint8_t pcrindex, >> + grub_uint32_t eventtype, >> + const char *description, >> + grub_size_t description_size, >> + void *buf, grub_size_t size) > I cannot see any reason to make this function part of the GRUB kernel. > I think it should be in grub-core/commands/ieee1275/ibmvtpm.c. Moved it. > >> +{ >> + struct tpm_2hash_ext_log > Please define this struct before the function, somewhere after includes. Done. > >> + { >> + struct grub_ieee1275_common_hdr common; >> + grub_ieee1275_cell_t method; >> + grub_ieee1275_cell_t ihandle; >> + grub_ieee1275_cell_t size; >> + void *buf; >> + grub_ieee1275_cell_t description_size; >> + const char *description; >> + grub_ieee1275_cell_t eventtype; >> + grub_ieee1275_cell_t pcrindex; >> + grub_ieee1275_cell_t catch_result; >> + grub_ieee1275_cell_t rc; >> + } > GRUB_PACKED? Per the other examples in the code this doesn't seem to be necessary. I also replaced void * and char * with the grub_ieee1275_cell_t. > >> + args; >> + >> + INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 2); >> + args.method = (grub_ieee1275_cell_t) "2hash-ext-log"; >> + args.ihandle = ihandle; >> + args.pcrindex = pcrindex; >> + args.eventtype = eventtype; >> + args.description = description; >> + args.description_size = description_size; >> + args.buf = buf; >> + args.size = (grub_ieee1275_cell_t) size; >> + >> + if (IEEE1275_CALL_ENTRY_FN (&args) == -1) >> + return false; >> + return !!args.rc; >> +} > Daniel Thanks.