From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B773C433EF for ; Thu, 16 Sep 2021 07:24:59 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5E55C60FA0 for ; Thu, 16 Sep 2021 07:24:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 5E55C60FA0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmx.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8C18582D95; Thu, 16 Sep 2021 09:24:56 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.b="KcyzbGyl"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 79C5C829FC; Thu, 16 Sep 2021 09:24:54 +0200 (CEST) Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 738CC829FC for ; Thu, 16 Sep 2021 09:24:50 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1631777088; bh=FyRPqGlNIwj/Mhe72Vg55q3Ul19yJbauhZ1Dl0TGNGg=; h=X-UI-Sender-Class:Subject:To:Cc:References:From:Date:In-Reply-To; b=KcyzbGylDKAq9rK3tAoiJNAGQ6FpGts1uqnj15YvlRxWP8e0KXiCDFmb7oJTh2H4/ xyWZbVmXYWCnC4PaE/0s7in2i/IOapZbW8mqDNp1ToVIEAK78N6AEKB3qv6a8f1opW K/EGwnWI5n1BZ9cs/EY61miLQnc956n1VLaKux7g= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.189] ([46.253.247.72]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MWici-1mOSRU1xyC-00X3CI; Thu, 16 Sep 2021 09:24:48 +0200 Subject: Re: [PATCH 2/3] efi_loader: add UEFI GPT measurement To: Masahisa Kojima Cc: Alexander Graf , Simon Glass , Bin Meng , Christian Gmeiner , u-boot@lists.denx.de, Ilias Apalodimas References: <20210915051546.500-1-masahisa.kojima@linaro.org> <20210915051546.500-3-masahisa.kojima@linaro.org> From: Heinrich Schuchardt Message-ID: Date: Thu, 16 Sep 2021 09:24:47 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <20210915051546.500-3-masahisa.kojima@linaro.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:6RihjVqMFsXwsoHkCik91xw9HvjDncVaTbtXiSkgrBw2zne4ArN yrr9f3F3IkkJ6PJ4Wz+LnTIhsy6fJpf80F9m9GhwO+DxqdWvNIOzoiOZ237Zye0IyXFUBbQ agQYGDSEKXDxVx7GUo6WECjMyEwa0Wz4U1D9sW5mDWGeyCU+gDTe8/6P/lL1kHAX4Q0vsmP Apit6aH4J9KBc8MoQc5Xg== X-UI-Out-Filterresults: notjunk:1;V03:K0:NplSo3+zD+8=:3qILfghxkTK1kopFiP0Qtj ucjw6sDl+giLNHNMOG0SNLaWTOn46H1ab6Csfqoo3yS4AvLpcdiUJk6zLfDIqYjr2m+N+S7N7 mcogqRK0PaUsbEbEfntSVtqjp2r57huQCQhp1q9eKsJDUlychkgKWTMDPy2g81EHiVTtkrBQk nrbIJhQ8hbO1uYVb07uowcdxht3VYi6VGzp7U8+we6YMWHwACWVyIKSP0pBFgJrTE6ASHUk5g d4KW1QKYx/u2ouPb76VjQgkzb736Wt5VBSPqKBmaEzRH8q8wx55FTgIrgemzkqY0PblAKavQN /Owh4xBtv4BAw0UJuwmJJswT9iL7jfYOr6cCUNtpg25jqEc3CwAu95/qFUE+i4D1JPa5TDUUE aAFSNSemZ+/bnhWAFPT+4kefLjeuoRr1YW1CwH3DvL2ttRUXop7UC7B5V7XYeYWczsVWqq9I7 SzD41ISi66rdY7CxubE6LA8+5ubr0/X0FRJeN/VSnGcjju2YE+CM4v/L7PbERIP1qOEvhQyzJ gQ90FRACWpDCM6T2B/HyekCE6oQGWGFMaeXrzZJ89gRXEtZHe+bjoZ7l6YVuFJNge3abNM5N0 pAIXxS4j2monAAo7FKjAaRmczyQLh2mPKVWxkh6DH9UdEnfLvkgDC7sECxJ5/Ap++HG4O6oxz 7T/5ncDzeusq9cYfM3B8kBDefIz+4oIyBCnSeM6UWtool6xXGFwH2yhcGj26p8/5RBHPMZA3e d9B4hXk+7ilrPNcqIt1OPaL4QTu1zMqBiBDEVgUerxItAtCRV0U1BrhrsZt8sTnaMN4D1Xabc so2h5NFb0WtjSXky58X3vuRl9txBKNVdw3rXxIuIOwcUXYiPz14ekEibT9OqSQUQDtULMJf/s fjx6HVTnAk6+oOx8tcvWtas/UZ9+qxlwMN/xD5HM6OTcmONBUvy3eIlCJfR6BQmX9vxq9+Ql6 LAwWNHIpte7/8vxLCOvtF9ayI/F+tjvHmE52xdLS1iQeEgpLEL1erI/98odwCKh4lFp+uGwMB m3bzCUKtiUlNnsgZsQNpMhWcrNGvmFLOEyCV3FQ6VH0uChJwXHcnYnvYqFDpSEJTNyQaZrCex k/jFJ9eUD2Q+Yc= X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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 On 9/15/21 7:15 AM, Masahisa Kojima wrote: > This commit adds the UEFI GPT disk partition topology > measurement required in TCG PC Client PFP Spec. Thanks for looking into the missing parts of TCG measurement in U-Boot. The requirements in the TCG PC Client PFP Spec are strange. It does not explicitly say how to handle multiple block devices. Did the authors never enjoy the pleasure of a second disk drive? With your implementation you would measure the GPT table of the SD-card from which you load Shim or GRUB but would not care about the GPT of the NVMe drive with your OS. A clarification by the TCG standards committee would be helpful. After this series "10.2.7 DEVICE_SECURITY_EVENT_DATA Structure" still needs to be covered. Best regards Heinrich > > Signed-off-by: Masahisa Kojima > --- > include/blk.h | 3 + > include/efi_loader.h | 2 +- > include/efi_tcg2.h | 12 +++ > lib/efi_loader/efi_boottime.c | 2 +- > lib/efi_loader/efi_tcg2.c | 175 +++++++++++++++++++++++++++++++++- > 5 files changed, 191 insertions(+), 3 deletions(-) > > diff --git a/include/blk.h b/include/blk.h > index 19bab081c2..f0cc7ca1a2 100644 > --- a/include/blk.h > +++ b/include/blk.h > @@ -45,6 +45,9 @@ enum if_type { > #define BLK_PRD_SIZE 20 > #define BLK_REV_SIZE 8 > > +#define PART_FORMAT_PCAT 0x1 > +#define PART_FORMAT_GPT 0x2 > + > /* > * Identifies the partition table type (ie. MBR vs GPT GUID) signature > */ > diff --git a/include/efi_loader.h b/include/efi_loader.h > index 13f0c24058..dbcc296e01 100644 > --- a/include/efi_loader.h > +++ b/include/efi_loader.h > @@ -503,7 +503,7 @@ efi_status_t efi_init_variables(void); > void efi_variables_boot_exit_notify(void); > efi_status_t efi_tcg2_notify_exit_boot_services_failed(void); > /* Measure efi application invocation */ > -efi_status_t efi_tcg2_measure_efi_app_invocation(void); > +efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_imag= e_obj *handle); > /* Measure efi application exit */ > efi_status_t efi_tcg2_measure_efi_app_exit(void); > /* Called by bootefi to initialize root node */ > diff --git a/include/efi_tcg2.h b/include/efi_tcg2.h > index da33f8a1d0..33257fa96b 100644 > --- a/include/efi_tcg2.h > +++ b/include/efi_tcg2.h > @@ -230,6 +230,18 @@ struct smbios_handoff_table_pointers2 { > struct efi_configuration_table table_entry[1]; > } __packed; > > +/** > + * struct tdUEFI_GPT_DATA - event log structure of industry standard ta= bles > + * @uefi_partition_header: gpt partition header > + * @number_of_partitions: the number of partition > + * @partitions: partition entries > + */ > +struct efi_gpt_data { > + gpt_header uefi_partition_header; > + u64 number_of_partitions; > + gpt_entry partitions[]; > +} __packed; > + > struct efi_tcg2_protocol { > efi_status_t (EFIAPI * get_capability)(struct efi_tcg2_protocol *this= , > struct efi_tcg2_boot_service_capability *capability); > diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime= .c > index 701e2212c8..bf5661e1ee 100644 > --- a/lib/efi_loader/efi_boottime.c > +++ b/lib/efi_loader/efi_boottime.c > @@ -3003,7 +3003,7 @@ efi_status_t EFIAPI efi_start_image(efi_handle_t i= mage_handle, > > if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) { > if (image_obj->image_type =3D=3D IMAGE_SUBSYSTEM_EFI_APPLICATION) { > - ret =3D efi_tcg2_measure_efi_app_invocation(); > + ret =3D efi_tcg2_measure_efi_app_invocation(image_obj); > if (ret !=3D EFI_SUCCESS) { > log_warning("tcg2 measurement fails(0x%lx)\n", > ret); > diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c > index 7f47998a55..35810615ed 100644 > --- a/lib/efi_loader/efi_tcg2.c > +++ b/lib/efi_loader/efi_tcg2.c > @@ -1525,12 +1525,181 @@ static void *search_smbios_table(void) > return NULL; > } > > +/** > + * search_gpt_dp_node() - search gpt device path node > + * > + * @device_path: device path > + * > + * Return: pointer to the gpt device path node > + */ > +static struct > +efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path= ) > +{ > + struct efi_device_path *dp =3D device_path; > + > + while (dp) { > + if (dp->type =3D=3D DEVICE_PATH_TYPE_MEDIA_DEVICE && > + dp->sub_type =3D=3D DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) { > + struct efi_device_path_hard_drive_path *hd_dp =3D > + (struct efi_device_path_hard_drive_path *)dp; > + > + if (hd_dp->partmap_type =3D=3D PART_FORMAT_GPT && > + hd_dp->signature_type =3D=3D SIG_TYPE_GUID) > + return dp; > + } > + dp =3D efi_dp_next(dp); > + } > + > + return NULL; > +} > + > +/** > + * tcg2_measure_gpt_table() - measure gpt table > + * > + * @dev: TPM device > + * @loaded_image: handle to the loaded image > + * > + * Return: status code > + */ > +static efi_status_t > +tcg2_measure_gpt_data(struct udevice *dev, > + struct efi_loaded_image_obj *loaded_image) > +{ > + efi_status_t ret; > + efi_handle_t handle; > + struct efi_handler *dp_handler; > + struct efi_device_path *orig_device_path; > + struct efi_device_path *device_path; > + struct efi_device_path *dp; > + struct efi_block_io *block_io; > + struct efi_gpt_data *event =3D NULL; > + efi_guid_t null_guid =3D NULL_GUID; > + gpt_header *orig_gpt_h =3D NULL; > + gpt_entry *orig_gpt_e =3D NULL; > + gpt_header *gpt_h =3D NULL; > + gpt_entry *entry =3D NULL; > + gpt_entry *gpt_e; > + u32 num_of_valid_entry =3D 0; > + u32 event_size; > + u32 i; > + u32 total_gpt_entry_size; > + > + ret =3D efi_search_protocol(&loaded_image->header, > + &efi_guid_loaded_image_device_path, > + &dp_handler); > + if (ret !=3D EFI_SUCCESS) > + return ret; > + > + orig_device_path =3D dp_handler->protocol_interface; > + device_path =3D efi_dp_dup(orig_device_path); > + if (!device_path) > + return EFI_OUT_OF_RESOURCES; > + > + dp =3D search_gpt_dp_node(device_path); > + if (!dp) { > + /* no GPT device path node found, skip GPT measurement */ > + ret =3D EFI_SUCCESS; > + goto out1; > + } > + > + /* read GPT header */ > + dp->type =3D DEVICE_PATH_TYPE_END; > + dp->sub_type =3D DEVICE_PATH_SUB_TYPE_END; > + dp =3D device_path; > + ret =3D EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_gui= d, > + &dp, &handle)); > + if (ret !=3D EFI_SUCCESS) > + goto out1; > + > + ret =3D EFI_CALL(efi_handle_protocol(handle, > + &efi_block_io_guid, (void **)&block_io)); > + if (ret !=3D EFI_SUCCESS) > + goto out1; > + > + orig_gpt_h =3D calloc(1, (block_io->media->block_size + block_io->medi= a->io_align)); > + if (!orig_gpt_h) { > + ret =3D EFI_OUT_OF_RESOURCES; > + goto out2; > + } > + > + gpt_h =3D (gpt_header *)ALIGN((uintptr_t)orig_gpt_h, block_io->media->= io_align); > + ret =3D block_io->read_blocks(block_io, block_io->media->media_id, 1, > + block_io->media->block_size, gpt_h); > + if (ret !=3D EFI_SUCCESS) > + goto out2; > + > + /* read GPT entry */ > + total_gpt_entry_size =3D gpt_h->num_partition_entries * > + gpt_h->sizeof_partition_entry; > + orig_gpt_e =3D calloc(1, total_gpt_entry_size + block_io->media->io_al= ign); > + entry =3D (void *)ALIGN((uintptr_t)orig_gpt_e, block_io->media->io_ali= gn); > + if (!entry) { > + ret =3D EFI_OUT_OF_RESOURCES; > + goto out2; > + } > + > + ret =3D block_io->read_blocks(block_io, block_io->media->media_id, > + gpt_h->partition_entry_lba, > + total_gpt_entry_size, entry); > + if (ret !=3D EFI_SUCCESS) > + goto out2; > + > + /* count valid GPT entry */ > + gpt_e =3D entry; > + for (i =3D 0; i < gpt_h->num_partition_entries; i++) { > + if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) > + num_of_valid_entry++; > + > + gpt_e =3D (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry); > + } > + > + /* prepare event data for measurement */ > + event_size =3D sizeof(struct efi_gpt_data) + > + (num_of_valid_entry * gpt_h->sizeof_partition_entry); > + event =3D calloc(1, event_size); > + if (!event) { > + ret =3D EFI_OUT_OF_RESOURCES; > + goto out2; > + } > + memcpy(event, gpt_h, sizeof(gpt_header)); > + put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions); > + > + /* copy valid GPT entry */ > + gpt_e =3D entry; > + num_of_valid_entry =3D 0; > + for (i =3D 0; i < gpt_h->num_partition_entries; i++) { > + if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) { > + memcpy((u8 *)event->partitions + > + (num_of_valid_entry * gpt_h->sizeof_partition_entry), > + gpt_e, gpt_h->sizeof_partition_entry); > + num_of_valid_entry++; > + } > + > + gpt_e =3D (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry); > + } > + > + ret =3D tcg2_measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *= )event); > + if (ret !=3D EFI_SUCCESS) > + goto out2; > + > +out2: > + EFI_CALL(efi_close_protocol((efi_handle_t)block_io, &efi_block_io_guid= , > + NULL, NULL)); > + free(orig_gpt_h); > + free(orig_gpt_e); > + free(event); > +out1: > + efi_free_pool(device_path); > + > + return ret; > +} > + > /** > * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation > * > * Return: status code > */ > -efi_status_t efi_tcg2_measure_efi_app_invocation(void) > +efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_imag= e_obj *handle) > { > efi_status_t ret; > u32 pcr_index; > @@ -1569,6 +1738,10 @@ efi_status_t efi_tcg2_measure_efi_app_invocation(= void) > goto out; > } > > + ret =3D tcg2_measure_gpt_data(dev, handle); > + if (ret !=3D EFI_SUCCESS) > + goto out; > + > tcg2_efi_app_invoked =3D true; > out: > return ret; >