From patchwork Wed Jul 14 13:00:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 476741 Delivered-To: patch@linaro.org Received: by 2002:a02:c94a:0:0:0:0:0 with SMTP id u10csp545045jao; Wed, 14 Jul 2021 05:59:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwHWuTEOOYnRo50hsXZ1Lg9jbY+aWuY1LP4EU3bj/QELtGHoXMzXqIcC3Q999L2ueEi6BR9 X-Received: by 2002:a05:6402:1a3c:: with SMTP id be28mr13400260edb.15.1626267591920; Wed, 14 Jul 2021 05:59:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626267591; cv=none; d=google.com; s=arc-20160816; b=JO1N03sLkIWXaNSoL8qMAmJgHQ4lz3GN1i2nIp1gRI0cL35+M7zp6aFNro/1tQmfEV z6jrAy5E+T7bjHuSjG036MSnSmENyZVl7+iorYUY1KX/RmLHA3LhOu+V1jvGr4uRaf47 2igiU40PRjLSX70dv7qpYdeZDiUw3A3Ll1gva5a2RPca80EfXOq2CvgD/A2ao0BSXAjZ FFkjFJVDlCY333q7YHm+9cbGbT8kgnoL3xqIRThU+x+k8cz+JR9Gl4aTN/Iie+FDhjq1 7JpZdXDU5bH9j071fowxuqen4VC6QsXjKzk8/hgl3Ah4OthG+FSKA7GeR6bC1WXb5FTE HUrw== 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:references:in-reply-to :message-id:date:subject:to:from:dkim-signature; bh=8Hsn+V+guE2lypZsedX3RNba7aS8U/05Sz1tivzJWJA=; b=lJdm5a/ZbVp0lUx4QDyK+6UIGRisk4ELweMcG3WbdYMdFh+hmOd/caLfznU4zWLGlo Hl0Kq6y/oCzneIau4hifGT0TZHJwBfLdeShVSOPeYE6RBzAHLnNhDpRPgUBFi+GOIjei qHU6WYgd8YnOXIhPCY6Nvibv3gDER+E6GZeJkhAUECPMwKQFar0sG5rYdVaJ0sIilTq9 ofnsZzlo5vv9wMt3wL5tvSD97r/YN2PXO5E+QdhSGz3Ls1M5KBGWJV8cwwNp4iUVLE5y LSDUP/3xpFYNoCoHUNzLw00iwFEK4vARuPzztD0uitUv5VVX+Z962YW3nXCmfEdQHt7C JXZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="cG/FeU7T"; 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 l1si2902438ejo.68.2021.07.14.05.59.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 05:59:51 -0700 (PDT) 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="cG/FeU7T"; 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 23AA482039; Wed, 14 Jul 2021 14:59:37 +0200 (CEST) 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="cG/FeU7T"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 92B6082000; Wed, 14 Jul 2021 14:59:32 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) (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 EADAF81E53 for ; Wed, 14 Jul 2021 14:59:26 +0200 (CEST) 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-x102d.google.com with SMTP id g6-20020a17090adac6b029015d1a9a6f1aso3563848pjx.1 for ; Wed, 14 Jul 2021 05:59:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=8Hsn+V+guE2lypZsedX3RNba7aS8U/05Sz1tivzJWJA=; b=cG/FeU7TLy1GkFm9TINfK/itZ9O1oQcjjhS70FJ8V9N5L6jGc46quut21CGLGk9xsF tF0fctyOkBisngYOeW95heLhtHgIVJSCibGb9bffAXguMZdAN8saVw8QLfqLwYTcS2+d Qa+WL1EIDVKfr4MwmT1/LxTAYcXcnv6/NCACPob0Sm3Bc7dsFVz5XvoQ1a1Of2v39uro DD8gbuCYwBG8spDZD0aa7VAz4eczr6dmMOPJJ++s0PNIFoAHGioq7qjauazGYXlaXSe9 fVIwot6urN2vmSe6yoyibvBuY04++XUi+Z6WI3R6uPcqk4mqYi/Ui+NvCNNZ0VEpOU6f lMog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=8Hsn+V+guE2lypZsedX3RNba7aS8U/05Sz1tivzJWJA=; b=e1TNBFl2OhSZVYqgAPXEed35246s5tPykgdkSVpUr8uPZ7Fh+F++SmR/s2T+PQqtn7 NnuRGNG++U/wmxecmW0fIeVRxAgLSdI2B0F0wgfBU3bskZN9igPEDzNR5HLkg1ztamdu ac/crYy5LkMHo+gsYDaA5SgMt/Nepn5ujPeAS+1yI8l1vDuyArmPZgKHM7TP8otmaIse vxtRu+ijtfb1+uKt0kAhUUSRaxOPz3061KDfBiXJ9EHM6C+ZZJeAt6Kjt+jI11alw05E BRXAOMSZL7gHNeoOxnLlH16oYjU8J5iZSsnWuvmSkjARMG4++NdQSH08gYHYQ/jmEPLM 3K4Q== X-Gm-Message-State: AOAM531KDX80UtxvI4RQl6dvJx+s84xzjW39gtvlW8O/huJnhBTWKSnV CLJroIwRrV7qWl9OR+wsekvReg== X-Received: by 2002:a17:90a:4404:: with SMTP id s4mr3712075pjg.218.1626267565338; Wed, 14 Jul 2021 05:59:25 -0700 (PDT) Received: from localhost.localdomain ([2400:2411:502:a100:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id m21sm2787509pfo.159.2021.07.14.05.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 05:59:24 -0700 (PDT) From: Masahisa Kojima To: Heinrich Schuchardt , Alexander Graf , Ilias Apalodimas , Simon Glass , Masahisa Kojima , Dhananjay Phadke , u-boot@lists.denx.de Subject: [PATCH v2 2/6] efi_loader: add secure boot variable measurement Date: Wed, 14 Jul 2021 22:00:02 +0900 Message-Id: <20210714130006.17837-3-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210714130006.17837-1-masahisa.kojima@linaro.org> References: <20210714130006.17837-1-masahisa.kojima@linaro.org> 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 TCG PC Client PFP spec requires to measure the secure boot policy before validating the UEFI image. This commit adds the secure boot variable measurement of "SecureBoot", "PK", "KEK", "db" and "dbx". Note that this implementation assumes that secure boot variables are pre-configured and not be set/updated in runtime. Signed-off-by: Masahisa Kojima --- Changes in v2: - missing null check for getting variable data - some minor fix for readability include/efi_tcg2.h | 20 ++++++ lib/efi_loader/efi_tcg2.c | 139 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) -- 2.17.1 diff --git a/include/efi_tcg2.h b/include/efi_tcg2.h index bcfb98168a..8d7b77c087 100644 --- a/include/efi_tcg2.h +++ b/include/efi_tcg2.h @@ -142,6 +142,26 @@ struct efi_tcg2_final_events_table { struct tcg_pcr_event2 event[]; }; +/** + * struct tdUEFI_VARIABLE_DATA + * @variable_name: The vendorGUID parameter in the + * GetVariable() API. + * @unicode_name_length: The length in CHAR16 of the Unicode name of + * the variable. + * @variable_data_length: The size of the variable data. + * @unicode_name: The CHAR16 unicode name of the variable + * without NULL-terminator. + * @variable_data: The data parameter of the efi variable + * in the GetVariable() API. + */ +struct efi_tcg2_uefi_variable_data { + efi_guid_t variable_name; + u64 unicode_name_length; + u64 variable_data_length; + u16 unicode_name[1]; + u8 variable_data[1]; +}; + 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_tcg2.c b/lib/efi_loader/efi_tcg2.c index 1319a8b378..12db6f6b7c 100644 --- a/lib/efi_loader/efi_tcg2.c +++ b/lib/efi_loader/efi_tcg2.c @@ -78,6 +78,19 @@ static const struct digest_info hash_algo_list[] = { }, }; +struct variable_info { + u16 *name; + const efi_guid_t *guid; +}; + +static struct variable_info secure_variables[] = { + {L"SecureBoot", &efi_global_variable_guid}, + {L"PK", &efi_global_variable_guid}, + {L"KEK", &efi_global_variable_guid}, + {L"db", &efi_guid_image_security_database}, + {L"dbx", &efi_guid_image_security_database}, +}; + #define MAX_HASH_COUNT ARRAY_SIZE(hash_algo_list) /** @@ -1264,6 +1277,39 @@ free_pool: return ret; } +/** + * tcg2_measure_event() - common function to add event log and extend PCR + * + * @dev: TPM device + * @pcr_index: PCR index + * @event_type: type of event added + * @size: event size + * @event: event data + * + * Return: status code + */ +static efi_status_t EFIAPI +tcg2_measure_event(struct udevice *dev, u32 pcr_index, u32 event_type, + u32 size, u8 event[]) +{ + struct tpml_digest_values digest_list; + efi_status_t ret; + + ret = tcg2_create_digest(event, size, &digest_list); + if (ret != EFI_SUCCESS) + goto out; + + ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); + if (ret != EFI_SUCCESS) + goto out; + + ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, + size, event); + +out: + return ret; +} + /** * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the * eventlog and extend the PCRs @@ -1294,6 +1340,92 @@ out: return ret; } +/** + * tcg2_measure_variable() - add variable event log and extend PCR + * + * @dev: TPM device + * @pcr_index: PCR index + * @event_type: type of event added + * @var_name: variable name + * @guid: guid + * @data_size: variable data size + * @data: variable data + * + * Return: status code + */ +static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index, + u32 event_type, u16 *var_name, + const efi_guid_t *guid, + efi_uintn_t data_size, u8 *data) +{ + u32 event_size; + efi_status_t ret; + struct efi_tcg2_uefi_variable_data *event; + + event_size = sizeof(event->variable_name) + + sizeof(event->unicode_name_length) + + sizeof(event->variable_data_length) + + (u16_strlen(var_name) * sizeof(u16)) + data_size; + event = malloc(event_size); + if (!event) + return EFI_OUT_OF_RESOURCES; + + guidcpy(&event->variable_name, guid); + event->unicode_name_length = u16_strlen(var_name); + event->variable_data_length = data_size; + memcpy(event->unicode_name, var_name, + (event->unicode_name_length * sizeof(u16))); + memcpy((u16 *)event->unicode_name + event->unicode_name_length, + data, data_size); + ret = tcg2_measure_event(dev, pcr_index, event_type, event_size, + (u8 *)event); + free(event); + return ret; +} + +/** + * tcg2_measure_secure_boot_variable() - measure secure boot variables + * + * @dev: TPM device + * + * Return: status code + */ +static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev) +{ + u8 *data; + efi_uintn_t data_size; + u32 count, i; + efi_status_t ret; + + count = ARRAY_SIZE(secure_variables); + for (i = 0; i < count; i++) { + data = efi_get_var(secure_variables[i].name, + secure_variables[i].guid, + &data_size); + if (data == NULL) { + log_info("%ls not found\n", secure_variables[i].name); + continue; + } + + ret = tcg2_measure_variable(dev, 7, + EV_EFI_VARIABLE_DRIVER_CONFIG, + secure_variables[i].name, + secure_variables[i].guid, + data_size, (u8 *)data); + free(data); + if (ret != EFI_SUCCESS) + goto error; + } + + /* + * TODO: add DBT and DBR measurement support when u-boot supports + * these variables. + */ + +error: + return ret; +} + /** * efi_tcg2_register() - register EFI_TCG2_PROTOCOL * @@ -1328,6 +1460,13 @@ efi_status_t efi_tcg2_register(void) tcg2_uninit(); goto fail; } + + ret = tcg2_measure_secure_boot_variable(dev); + if (ret != EFI_SUCCESS) { + tcg2_uninit(); + goto fail; + } + return ret; fail: