From patchwork Wed Nov 24 13:40:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruchika Gupta X-Patchwork-Id: 519347 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp10398663imj; Wed, 24 Nov 2021 05:41:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJxNNtFtz6QCWXQr0sp9cptT5z6LQ+uDIr+NqYA7Ti9fn6pgdVLKDbg+ubS+Py+MNBNZpcp2 X-Received: by 2002:aa7:d58c:: with SMTP id r12mr25438077edq.115.1637761269876; Wed, 24 Nov 2021 05:41:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637761269; cv=none; d=google.com; s=arc-20160816; b=e3yJhUAyrfeDdR399gtSfpjoI/i/dXPPn5JLU1Wn37K9lb1/45YvfiyHjZV63QhFew N18scd+5v4POhHC96yWVa4gKFvgETcNx3JaZDg3wRfZsnBW/yswYn5n2oVVv3BwIvlf+ KPRpK4iGXeLlTRd0Ga5hdv8M1IrUUSRPv931LJp9npUoCb/PTEer2LuaE7Yj8SJPHNxT QL3d3uXOHTGeQ26buV+c2cZ867blfkVAtg4MwLUXTr0iGBbOXtmRe5zKkj5U7+uaOFdB 6cuAMZ+Wa9yyYXsFwn5f25xJeCMmWzhPJ8Ecr8tjonjvZ+HoRpNWQ2xjHTZ0yBHTaAbF 8RjA== 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:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=sASvuCpnuj/7n+NeXG208UDPNlMmn4GJTQLYkZGwdW8=; b=vMvy378buG5lVwlrkE9cIB6yDUI7A5te3Objv30xRfNqdwStoIzatj6YJhI+jf0hur m8+I9cM7MLmkx9AGkkb5nBDeNb7Oy22Djf6hMLFK/RViQi1xs4djC1SYdxw/gCr6gBBF 6cJ80YnjavFp5rA6PU7L66bqCfyJtjNLv0Ln5aSARed1yq8CFINminiskQudZNH6PSNd O97b/Py56NPZ4V/x2ez7ScmZXborYIJvAhNqdaFD2eOWTIyWjqMRZtqe8bp+XUcNaBUW 007RVIaJuMkI11dhco7HoeS5WHqNzG4Hwh4H95VcBZtoPCxLmU+FHCG7W7NiLBqxNyGI tjgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=erjQMoAt; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id ar17si31306036ejc.497.2021.11.24.05.41.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 05:41:09 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=erjQMoAt; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 8687B82F7D; Wed, 24 Nov 2021 14:41:04 +0100 (CET) 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="erjQMoAt"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BB703812A2; Wed, 24 Nov 2021 14:41:01 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) (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 B57C380FAE for ; Wed, 24 Nov 2021 14:40:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ruchika.gupta@linaro.org Received: by mail-pg1-x52b.google.com with SMTP id g28so2182593pgg.3 for ; Wed, 24 Nov 2021 05:40:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sASvuCpnuj/7n+NeXG208UDPNlMmn4GJTQLYkZGwdW8=; b=erjQMoAtFb2yJI2Pg/IUlvMba8LLZdJ5S8E0GkMZYnN5xFdECpSIxrXm8g2kxs5DCE nRgGozioaz18KK3MfAEw0gsfkj6Kll/NdI1Mq1SWsLRHHWeUr7r4nPsMGPvH6mz3pt7t 1v3hEBXhTNiV7uKhJJ6d6tU8GGHYso3pSdvk5FBOIymWe3rrj93Wv5x1GEPGDl/ghSVK h6GMllD4V6yOyKytwCzDF3PlWyPSnOp4EPG87j9j/JbRJzESbq1LAYxZ4M3ridh/n9kz tRBFHMaHzD7zTF0X5s4RgVaDc0ric2qZ/AsKXYbWM4SKkon1nQykJ78W0YYcjYkWm3XO 6Dow== 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=sASvuCpnuj/7n+NeXG208UDPNlMmn4GJTQLYkZGwdW8=; b=nStxXM5jwEYbhl7up59LCaBLlbQ67awFAV3wLQDONrQa2W3QOgvMWxeWjxcvYyxtP6 0FM+A9heGSLexikHbzxx8v8gh3MILA2tAdyQFK6T3sgjrtXBjWjAlLLoyd1fznhuV5GE 8SFwTTpqgggjmcu7vOLPB4u00i80VFV+AS27edWl7n2vEBMRXN5bkyLU9+0TqOv3vg4J u8wiWvrTuwYfXLLj/R5ApVdqr/RwXa6bOcAehBwIApk3qofsGyW9k5pV/M1tR+mwypzf OUqii3dE2K+2uwBPzO5imgHqW/jMrm8j5GsEyjEoK+ZDf+XEPft7l+D9SL4EMjJB7/RV oE+A== X-Gm-Message-State: AOAM533idNTqOiPfhPYzNb8/NRDI3md2qRodghS8VzIFYvdaMKMsdPOx rgcuYbzTkP5qezPveTXhDlfqKyI37iaqDg== X-Received: by 2002:a62:ce4c:0:b0:4a4:d175:306f with SMTP id y73-20020a62ce4c000000b004a4d175306fmr5961826pfg.75.1637761254299; Wed, 24 Nov 2021 05:40:54 -0800 (PST) Received: from localhost.localdomain ([106.215.91.18]) by smtp.gmail.com with ESMTPSA id k19sm16864653pff.20.2021.11.24.05.40.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 05:40:53 -0800 (PST) From: Ruchika Gupta To: u-boot@lists.denx.de, ilias.apalodimas@linaro.org, xypron.glpk@gmx.de, agraf@csgraf.de, masahisa.kojima@linaro.org Cc: Ruchika Gupta Subject: [PATCH v4 1/3] efi_loader: Add check for event log passed from firmware Date: Wed, 24 Nov 2021 19:10:42 +0530 Message-Id: <20211124134044.454168-1-ruchika.gupta@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.37 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 Platforms may have support to measure their initial firmware components and pass the event log to u-boot. The event log address can be passed in property tpm_event_log_addr and tpm_event_log_size of the tpm node. Platforms may choose their own specific mechanism to do so. A weak function is added to check if even log has been passed to u-boot from earlier firmware components. If available, the eventlog is parsed to check for its correctness and further event logs are appended to the passed log. Signed-off-by: Ruchika Gupta --- v4: Add SCRTM version to log only if previous firmware doesn;t pass the eventlog v3: Return as soon as you detect error v2: Moved firmware eventlog code parsing to tcg2_get_fw_eventlog() lib/efi_loader/efi_tcg2.c | 334 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 317 insertions(+), 17 deletions(-) diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c index 8c1f22e337..1713cfff70 100644 --- a/lib/efi_loader/efi_tcg2.c +++ b/lib/efi_loader/efi_tcg2.c @@ -324,6 +324,45 @@ __weak efi_status_t platform_get_tpm2_device(struct udevice **dev) return EFI_NOT_FOUND; } +/** + * platform_get_eventlog() - retrieve the eventlog address and size + * + * This function retrieves the eventlog address and size if the underlying + * firmware has done some measurements and passed them. + * + * This function may be overridden based on platform specific method of + * passing the eventlog address and size. + * + * @dev: udevice + * @addr: eventlog address + * @sz: eventlog size + * Return: status code + */ +__weak efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, + u32 *sz) +{ + const u64 *basep; + const u32 *sizep; + + basep = dev_read_prop(dev, "tpm_event_log_addr", NULL); + if (!basep) + return EFI_NOT_FOUND; + + *addr = be64_to_cpup((__force __be64 *)basep); + + sizep = dev_read_prop(dev, "tpm_event_log_size", NULL); + if (!sizep) + return EFI_NOT_FOUND; + + *sz = be32_to_cpup((__force __be32 *)sizep); + if (*sz == 0) { + log_debug("event log empty\n"); + return EFI_NOT_FOUND; + } + + return EFI_SUCCESS; +} + /** * tpm2_get_max_command_size() - get the supported max command size * @@ -1181,6 +1220,250 @@ static const struct efi_tcg2_protocol efi_tcg2_protocol = { .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks, }; +/** + * parse_event_log_header() - Parse and verify the event log header fields + * + * @buffer: Pointer to the event header + * @size: Size of the eventlog + * @pos: Position in buffer after event log header + * + * Return: status code + */ +efi_status_t parse_event_log_header(void *buffer, u32 size, u32 *pos) +{ + struct tcg_pcr_event *event_header = (struct tcg_pcr_event *)buffer; + int i = 0; + + if (size < sizeof(*event_header)) + return EFI_COMPROMISED_DATA; + + if (get_unaligned_le32(&event_header->pcr_index) != 0 || + get_unaligned_le32(&event_header->event_type) != EV_NO_ACTION) + return EFI_COMPROMISED_DATA; + + for (i = 0; i < sizeof(event_header->digest); i++) { + if (event_header->digest[i] != 0) + return EFI_COMPROMISED_DATA; + } + + *pos += sizeof(*event_header); + + return EFI_SUCCESS; +} + +/** + * parse_specid_event() - Parse and verify the specID Event in the eventlog + * + * @dev: udevice + * @buffer: Pointer to the start of the eventlog + * @log_size: Size of the eventlog + * @pos: Offset in the evenlog where specID event starts + * + * Return: status code + * @pos Offset in the eventlog where the specID event ends + * @digest_list: list of digests in the event + */ +efi_status_t parse_specid_event(struct udevice *dev, void *buffer, u32 log_size, + u32 *pos, + struct tpml_digest_values *digest_list) +{ + struct tcg_efi_spec_id_event *spec_event; + struct tcg_pcr_event *event_header = (struct tcg_pcr_event *)buffer; + size_t spec_event_size; + u32 active = 0, supported = 0, pcr_count = 0, alg_count = 0; + u32 spec_active = 0; + u16 hash_alg, hash_sz; + u8 vendor_sz; + int err, i; + + /* Check specID event data */ + spec_event = (struct tcg_efi_spec_id_event *)((uintptr_t)buffer + *pos); + /* Check for signature */ + if (memcmp(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03, + sizeof(TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03))) { + log_err("specID Event: Signature mismatch\n"); + return EFI_COMPROMISED_DATA; + } + + if (spec_event->spec_version_minor != + TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 || + spec_event->spec_version_major != + TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2) + return EFI_COMPROMISED_DATA; + + if (spec_event->number_of_algorithms > MAX_HASH_COUNT || + spec_event->number_of_algorithms < 1) { + log_err("specID Event: Number of algorithms incorrect\n"); + return EFI_COMPROMISED_DATA; + } + + alg_count = spec_event->number_of_algorithms; + + err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_count); + if (err) + return EFI_DEVICE_ERROR; + + digest_list->count = 0; + /* + * We may need to worry about the order of algs in this structure as + * subsequent entries in event should be in same order + */ + for (i = 0; i < alg_count; i++) { + hash_alg = + get_unaligned_le16(&spec_event->digest_sizes[i].algorithm_id); + hash_sz = + get_unaligned_le16(&spec_event->digest_sizes[i].digest_size); + + if (!(supported & alg_to_mask(hash_alg))) { + log_err("specID Event: Unsupported algorithm\n"); + return EFI_COMPROMISED_DATA; + } + digest_list->digests[digest_list->count++].hash_alg = hash_alg; + + spec_active |= alg_to_mask(hash_alg); + } + + /* TCG spec expects the event log to have hashes for all active PCR's */ + if (spec_active != active) { + /* + * Previous stage bootloader should know all the active PCR's + * and use them in the Eventlog. + */ + log_err("specID Event: All active hash alg not present\n"); + return EFI_COMPROMISED_DATA; + } + + /* + * the size of the spec event and placement of vendor_info_size + * depends on supported algoriths + */ + spec_event_size = + offsetof(struct tcg_efi_spec_id_event, digest_sizes) + + alg_count * sizeof(spec_event->digest_sizes[0]); + + vendor_sz = *(uint8_t *)((uintptr_t)buffer + *pos + spec_event_size); + + spec_event_size += sizeof(vendor_sz) + vendor_sz; + *pos += spec_event_size; + + if (get_unaligned_le32(&event_header->event_size) != spec_event_size) { + log_err("specID event: header event size mismatch\n"); + /* Right way to handle this can be to call SetActive PCR's */ + return EFI_COMPROMISED_DATA; + } + + return EFI_SUCCESS; +} + +efi_status_t tcg2_parse_event(struct udevice *dev, void *buffer, u32 log_size, + u32 *offset, struct tpml_digest_values *digest_list, + u32 *pcr) +{ + struct tcg_pcr_event2 *event = NULL; + u32 event_type, count, size, event_size; + size_t pos; + + if (*offset > log_size) + return EFI_COMPROMISED_DATA; + + event = (struct tcg_pcr_event2 *)((uintptr_t)buffer + *offset); + + *pcr = get_unaligned_le32(&event->pcr_index); + + event_size = tcg_event_final_size(digest_list); + + if (*offset + event_size > log_size) { + log_err("Event exceeds log size\n"); + return EFI_COMPROMISED_DATA; + } + + event_type = get_unaligned_le32(&event->event_type); + + /* get the count */ + count = get_unaligned_le32(&event->digests.count); + if (count != digest_list->count) + return EFI_COMPROMISED_DATA; + + pos = offsetof(struct tcg_pcr_event2, digests); + pos += offsetof(struct tpml_digest_values, digests); + + for (int i = 0; i < digest_list->count; i++) { + u16 alg; + u16 hash_alg = digest_list->digests[i].hash_alg; + u8 *digest = (u8 *)&digest_list->digests[i].digest; + + alg = get_unaligned_le16((void *)((uintptr_t)event + pos)); + + if (alg != hash_alg) + return EFI_COMPROMISED_DATA; + + pos += offsetof(struct tpmt_ha, digest); + memcpy(digest, (void *)((uintptr_t)event + pos), alg_to_len(hash_alg)); + pos += alg_to_len(hash_alg); + } + + size = get_unaligned_le32((void *)((uintptr_t)event + pos)); + event_size += size; + pos += sizeof(u32); /* tcg_pcr_event2 event_size*/ + pos += size; + + /* make sure the calculated buffer is what we checked against */ + if (pos != event_size) + return EFI_COMPROMISED_DATA; + + if (pos > log_size) + return EFI_COMPROMISED_DATA; + + *offset += pos; + + return EFI_SUCCESS; +} + +efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer, + size_t *log_sz) +{ + struct tpml_digest_values digest_list; + void *buffer; + efi_status_t ret; + u32 pcr, pos; + u64 base; + u32 sz; + + ret = platform_get_eventlog(dev, &base, &sz); + if (ret != EFI_SUCCESS) + return ret; + + if (sz > TPM2_EVENT_LOG_SIZE) + return EFI_VOLUME_FULL; + + buffer = (void *)base; + pos = 0; + /* Parse the eventlog to check for its validity */ + ret = parse_event_log_header(buffer, sz, &pos); + if (ret || pos > sz) + return EFI_COMPROMISED_DATA; + + ret = parse_specid_event(dev, buffer, sz, &pos, &digest_list); + if (ret || pos > sz) { + log_err("Error parsing SPEC ID Event\n"); + return EFI_COMPROMISED_DATA; + } + + while (pos < sz) { + ret = tcg2_parse_event(dev, buffer, sz, &pos, &digest_list, + &pcr); + if (ret) { + log_err("Error parsing event\n"); + return ret; + } + } + + memcpy(log_buffer, buffer, sz); + *log_sz = sz; + + return ret; +} + /** * create_specid_event() - Create the first event in the eventlog * @@ -1340,6 +1623,12 @@ static efi_status_t efi_init_event_log(void) * last log entry */ memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE); + + /* + * The log header is defined to be in SHA1 event log entry format. + * Setup event header + */ + event_header = (struct tcg_pcr_event *)event_log.buffer; event_log.pos = 0; event_log.last_event_size = 0; event_log.get_event_called = false; @@ -1347,20 +1636,37 @@ static efi_status_t efi_init_event_log(void) event_log.truncated = false; /* - * The log header is defined to be in SHA1 event log entry format. - * Setup event header + * Check if earlier firmware have passed any eventlog. Different + * platforms can use different ways to do so */ - event_header = (struct tcg_pcr_event *)event_log.buffer; - put_unaligned_le32(0, &event_header->pcr_index); - put_unaligned_le32(EV_NO_ACTION, &event_header->event_type); - memset(&event_header->digest, 0, sizeof(event_header->digest)); - ret = create_specid_event(dev, (void *)((uintptr_t)event_log.buffer + sizeof(*event_header)), - &spec_event_size); + ret = tcg2_get_fw_eventlog(dev, event_log.buffer, &event_log.pos); + /* + * If earlier firmware hasn't passed any eventlog, go ahead and + * create the eventlog header + */ + if (ret == EFI_NOT_FOUND) { + put_unaligned_le32(0, &event_header->pcr_index); + put_unaligned_le32(EV_NO_ACTION, &event_header->event_type); + memset(&event_header->digest, 0, sizeof(event_header->digest)); + ret = create_specid_event(dev, + (void *)((uintptr_t)event_log.buffer + + sizeof(*event_header)), + &spec_event_size); + if (ret != EFI_SUCCESS) + goto free_pool; + put_unaligned_le32(spec_event_size, &event_header->event_size); + event_log.pos = spec_event_size + sizeof(*event_header); + event_log.last_event_size = event_log.pos; + + /* + * Add SCRTM version to the log if previous firmmware + * doesn't pass an eventlog + */ + ret = efi_append_scrtm_version(dev); + } + if (ret != EFI_SUCCESS) goto free_pool; - put_unaligned_le32(spec_event_size, &event_header->event_size); - event_log.pos = spec_event_size + sizeof(*event_header); - event_log.last_event_size = event_log.pos; ret = create_final_event(); if (ret != EFI_SUCCESS) @@ -1963,12 +2269,6 @@ efi_status_t efi_tcg2_register(void) if (ret != EFI_SUCCESS) goto fail; - ret = efi_append_scrtm_version(dev); - if (ret != EFI_SUCCESS) { - tcg2_uninit(); - goto fail; - } - ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol, (void *)&efi_tcg2_protocol); if (ret != EFI_SUCCESS) { From patchwork Wed Nov 24 13:40:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruchika Gupta X-Patchwork-Id: 519348 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp10398945imj; Wed, 24 Nov 2021 05:41:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJwD50OMIpb1lsT2F41rTxKbgV9XjxdYRHGKjtkaHS1v46q3sl+l2NKvng5nQC+LAtVKHyqw X-Received: by 2002:a17:906:fca3:: with SMTP id qw3mr20796728ejb.285.1637761281003; Wed, 24 Nov 2021 05:41:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637761280; cv=none; d=google.com; s=arc-20160816; b=qTAY+Bs4ReC0GfKka5/VEf5vVWs7z9LVXWOQHVquqRAI+uS6IM+5zkOp/znCaks9ry +hAC+rvGC8UHJeeFo05kdy9z+aUfYPqVKazBBQ4KQfa5hlz3eL24KvvbaOE1Uxb4CW2D E0/Yg/trWfAsLuPHM1FR/xEntsNGaqjBZFgWK6lNpCUr4dSJoU7taKElQ6B693C3IVnr UlCQ4n/6/L6AO/A3vhbs12soghSFKeemJoFctUsgvUOKLxFzhJrsGt45f0ilEdFDbcBZ KrNrDV3ms24Lq75RsIlVSdJvXP9eaKDz1TrHmFhemxVAIHePN2/K6coGa63vLUOnBcEz k1FQ== 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:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=uUdRelbQfmazWyOo91y1infRQ7s32CdKbFI6cG0rCsQ=; b=CwG3Gs7CFq6JRGvmqKD7rvdsKYM0euiIxoobsyXhh7NXaWxjXoAWJcZuVuOVaWEQlc yzSzYLyEBmLCt8skO7iFOyS4t6Bs4r1uAnavy8AWczzk3Zocu4upjaRhDdJL/KiuMzJx hdE13F8Nenk4bsi8pnhtJUXJ5xMIlbmN5S4Dz4hSjGXEfvuk7hkvMe03JR6vjk5piG/u pFMeKN5ahC9z0/9V1z18Ww8KUZrUj6PvA8un2Oabh8dxs1R9GQvKC2USK0UXiZ4xFXSJ Z1XN7EUWe0Tvr7otAU0SEsSaV1Q3htvA+AtgcqoMD7xBGbBgKmwRYHjBnTiltHaPeST9 G0WA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FBo+YgD3; 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 l18si48376245edb.148.2021.11.24.05.41.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 05:41:20 -0800 (PST) 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=FBo+YgD3; 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 6B18882FBC; Wed, 24 Nov 2021 14:41:14 +0100 (CET) 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="FBo+YgD3"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D469480FAE; Wed, 24 Nov 2021 14:41:07 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) (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 D55A980FAE for ; Wed, 24 Nov 2021 14:41:02 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ruchika.gupta@linaro.org Received: by mail-pg1-x52c.google.com with SMTP id 71so2175146pgb.4 for ; Wed, 24 Nov 2021 05:41:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uUdRelbQfmazWyOo91y1infRQ7s32CdKbFI6cG0rCsQ=; b=FBo+YgD3GkCtkIfLxvWHXGKzvfeZ0oP8Meov1GaASf9Qv2D3QNpZ8pWSwTAayQcSpN S7a7GWDnkeOP1UU9ZgtbF5k5/fcvriDn7ztjx1fRN6N6gI+goV/ki8Frpf9+FAToBrzV Oqk2uZRMKLXK6lU5C/rq3oCewNTMN9t+ua2ICVSMw5merdyqqOFey3Qp4UVeCzDhpg9X 9AHPx9sH5zY54b5kS/0SxnsdiWAg5WSSUJutpiGv4oafzf0cJnwbcCn9qP8Efxp0vxrD RzIbJs3n1tRLBvFI3CQsV1E3KgZGZ+V78s1T5d+Fx8/RA7HMYV0mOyO7dQw7bwmbwfkl 8iUw== 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=uUdRelbQfmazWyOo91y1infRQ7s32CdKbFI6cG0rCsQ=; b=421UbhH8NkhlcdfY1S8YQPlIGuPJa+CuK2M7YzkQgFrxvQC/BvWR1RN3rbTPozU9gP PeKq7h0+nUB0o8ZX6c6UMKXtC7G1bVlRDJEiwl+yW0FtCvAL+tT0OrICT7vDazocLfI8 oBF5aEQpfpBrZkg1gH5MrhtCt4xQmq5pIfODmcMW4JOTdlhj9jVPCdKP7yJ3VzFb8Tdo 3Uh63NJTgRSUe9f1LNem6CbkUBk+dliv/IrXPbFesC6SPzAg+kFRLC3VZTl+7e5TVFYJ Xeec7fMNhdDLeLNsWrElOAGN2cCYuNe8PdKP9Ti470KCj1pDonY9YUcVk7en9aVEJwql sxYw== X-Gm-Message-State: AOAM531481JpCjwBQ9mTa/rIrgqQgobLBeiJ5qgAA19aeV1ZHgRs+xGQ vhl9FlZXiXnWyDTHda48sFCbl4FrnCxNUQ== X-Received: by 2002:a05:6a00:ac6:b029:374:a33b:a74 with SMTP id c6-20020a056a000ac6b0290374a33b0a74mr6122316pfl.51.1637761260986; Wed, 24 Nov 2021 05:41:00 -0800 (PST) Received: from localhost.localdomain ([106.215.91.18]) by smtp.gmail.com with ESMTPSA id k19sm16864653pff.20.2021.11.24.05.40.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 05:41:00 -0800 (PST) From: Ruchika Gupta To: u-boot@lists.denx.de, ilias.apalodimas@linaro.org, xypron.glpk@gmx.de, agraf@csgraf.de, masahisa.kojima@linaro.org Cc: Ruchika Gupta Subject: [PATCH v4 2/3] tpm: use more algorithms than sha256 on pcr_read Date: Wed, 24 Nov 2021 19:10:43 +0530 Message-Id: <20211124134044.454168-2-ruchika.gupta@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211124134044.454168-1-ruchika.gupta@linaro.org> References: <20211124134044.454168-1-ruchika.gupta@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.37 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 The current tpm2_pcr_read is hardcoded using SHA256. Make the actual command to TPM configurable to use wider range of algorithms. The current command line is kept as is i.e limited to SHA-256 only. Signed-off-by: Ruchika Gupta Reviewed-by: Ilias Apalodimas --- v4: No change v3: No change v2: Change algorithm from u32 to u16 Add parameter description in function declaration cmd/tpm-v2.c | 3 ++- include/tpm-v2.h | 5 ++++- lib/tpm-v2.c | 12 ++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/tpm-v2.c b/cmd/tpm-v2.c index daae91100a..4ea5f9f094 100644 --- a/cmd/tpm-v2.c +++ b/cmd/tpm-v2.c @@ -151,7 +151,8 @@ static int do_tpm_pcr_read(struct cmd_tbl *cmdtp, int flag, int argc, data = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0); - rc = tpm2_pcr_read(dev, index, priv->pcr_select_min, data, &updates); + rc = tpm2_pcr_read(dev, index, priv->pcr_select_min, TPM2_ALG_SHA256, + data, TPM2_DIGEST_LEN, &updates); if (!rc) { printf("PCR #%u content (%u known updates):\n", index, updates); print_byte_string(data, TPM2_DIGEST_LEN); diff --git a/include/tpm-v2.h b/include/tpm-v2.h index ceff7d245e..4e9dd52cb6 100644 --- a/include/tpm-v2.h +++ b/include/tpm-v2.h @@ -512,13 +512,16 @@ u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data, * @dev TPM device * @idx Index of the PCR * @idx_min_sz Minimum size in bytes of the pcrSelect array + * @algorithm Algorithm used, defined in 'enum tpm2_algorithms' * @data Output buffer for contents of the named PCR + * @digest_len len of the data * @updates Optional out parameter: number of updates for this PCR * * @return code of the operation */ u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz, - void *data, unsigned int *updates); + u16 algorithm, void *data, u32 digest_len, + unsigned int *updates); /** * Issue a TPM2_GetCapability command. This implementation is limited diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c index 2e7b27bd6b..1bf627853a 100644 --- a/lib/tpm-v2.c +++ b/lib/tpm-v2.c @@ -254,7 +254,8 @@ u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data, } u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz, - void *data, unsigned int *updates) + u16 algorithm, void *data, u32 digest_len, + unsigned int *updates) { u8 idx_array_sz = max(idx_min_sz, DIV_ROUND_UP(idx, 8)); u8 command_v2[COMMAND_BUFFER_SIZE] = { @@ -264,7 +265,7 @@ u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz, /* TPML_PCR_SELECTION */ tpm_u32(1), /* Number of selections */ - tpm_u16(TPM2_ALG_SHA256), /* Algorithm of the hash */ + tpm_u16(algorithm), /* Algorithm of the hash */ idx_array_sz, /* Array size for selection */ /* bitmap(idx) Selected PCR bitmap */ }; @@ -283,10 +284,13 @@ u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz, if (ret) return ret; + if (digest_len > response_len) + return TPM_LIB_ERROR; + if (unpack_byte_string(response, response_len, "ds", 10, &counter, - response_len - TPM2_DIGEST_LEN, data, - TPM2_DIGEST_LEN)) + response_len - digest_len, data, + digest_len)) return TPM_LIB_ERROR; if (updates) From patchwork Wed Nov 24 13:40:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruchika Gupta X-Patchwork-Id: 519349 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp10399422imj; Wed, 24 Nov 2021 05:41:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJywXAG4kigyeaWiA1/+esqkFc3mRdmvPhadkJqvDBQsNtVms8rZBdqE6LTp0HKjpTMjZsWc X-Received: by 2002:a17:906:788:: with SMTP id l8mr19641081ejc.548.1637761301466; Wed, 24 Nov 2021 05:41:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637761301; cv=none; d=google.com; s=arc-20160816; b=pAgP3eA2lrY+ytFVeZPTNN9Gcx/zaoAsKsyFnIc4zAP7wtIVNcFZP0OT2lxHbrAbbk 4yFHY3NXTn1rcIsG5HGX6pQ0J81Jqa+o4FMdci0dSt+kEWuPZfMDb526Obnx/M6nnyZR xmZd95eG8hs5m8LFSyvwC5skwIMo1qJzrUpPWhbGUQZ1hCmI/rGedJEbfnRhj6UEtMo/ Efsf/AvP46uhSFqH3bFuVm35pQ0XEzyF49VZ7C3ihBsesMtheXxv3N9op9qa+xOlGu1n OtfWcc6N3S9PO8tcYupwkn7WVAdiAa2udTf10Fr8eJz+Y4CzNtTNO6fnF+vUKwYnLvZW 68Qg== 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:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=u5n3detrpfuCj6jSzTCHbV5QKT7ZgU0DGhUz203XtSs=; b=UwJqMD4ok2NlstQbmzCDwtLSO6vWKXCxhDs4oOYaNAcUhf4mJsXt51RCsmgj36QQzl fFTP7IZhqJxxNR0Nwe4IKRJS1ENKgmbfQlwELXKmiyKCoU8bFKSfqcYIIPIPxlf2yJ+u hOhflTF8toAt/h2Z0I1JEG+5wmp21TZ+qGFFMv++FvthsGmUoHs8o6py/Crty7kzy88U 3umQ2KFxzaKfUnpAUBzKEPFCxSNSWmVhSWZyTE90qyJxwJj9IGSb80znPQQGK/67ZFsj 9KYr7bWl4onl8OUK7a/PUNFiSVa86KTbZ/ycYX5l2a6KeA37YsaRqv6A2U/Pl3gWbg7m vl5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VTJoIN2g; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id bq24si33537772edb.290.2021.11.24.05.41.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 05:41:41 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VTJoIN2g; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 1E4C482FB0; Wed, 24 Nov 2021 14:41:40 +0100 (CET) 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="VTJoIN2g"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 37957830E7; Wed, 24 Nov 2021 14:41:38 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) (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 C451880FAE for ; Wed, 24 Nov 2021 14:41:33 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ruchika.gupta@linaro.org Received: by mail-pl1-x635.google.com with SMTP id u17so1880420plg.9 for ; Wed, 24 Nov 2021 05:41:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=u5n3detrpfuCj6jSzTCHbV5QKT7ZgU0DGhUz203XtSs=; b=VTJoIN2gckQcAM/g3b/dxW6+5Tq7eTLMiZWEiBFI8CvctFbH7Lg3J9axHY3ZxuJnQs /22uGfH74B5sF66Ufy0e17Py5qB/QEsYcAmVf1nMlMH+7oQW7D4eBGk86p+Sfpsqs5lf l/+6DMTdEeUMH9FEN1lMjwFTXhQhU689UWxUYymhm1LqT7VbjimeJoHuzMhF3Q5kwwpb Saq9K8O3yvrkceYJ9eyxhzx1nO13fkDVadquH9OC2fIxPZoigIhdaBXoqcuV+bHsB0i9 rl5JvYC5l7vc/KFYUaExRn13kOKEwISrasJFxwgRgEdGc15WG9G7tgYqpe3SD9KVO4ZE ArkQ== 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=u5n3detrpfuCj6jSzTCHbV5QKT7ZgU0DGhUz203XtSs=; b=a3uMwHi5Z39uNc6uQkZEMzDegoTvnhsk8ynuJ3oUeOIMgRDM7v4YmFpvIf04JFwShx 4PsNQDVde/a+k1G3DIL2p0BXqduhcEGQRX/UEqdZ//mY9bFEQaUiRyDuoMdt3il/L16U GBwJ1uk0NRq2SsjVzAtKp8ZhXoIzebXYUMHcIa7u3NQudFJ2qPrxscts0ROoyUwF2LNS Y+9/q8/ljTKoh+SYsIMNsIBrU56AemuwABTdjJgkqXpa+vdo9gRjSq+38tMdLm+wUeUm pOnYIJLydFkx8rv590WNxek5j40+28Uy5cB87x1OnHzfJBDof21MhyXmpBfbAldJ2Plz ofxw== X-Gm-Message-State: AOAM530SPITi+dm7WjkWGKvZb423B+xLFu6JxiaXlPUNdnuSDZxh5zTU LWySE+0ji/oGT+2pYFMgVQ0zbs6N+vFs6A== X-Received: by 2002:a17:902:b097:b0:141:ec7d:a055 with SMTP id p23-20020a170902b09700b00141ec7da055mr18754187plr.3.1637761291978; Wed, 24 Nov 2021 05:41:31 -0800 (PST) Received: from localhost.localdomain ([106.215.91.18]) by smtp.gmail.com with ESMTPSA id k19sm16864653pff.20.2021.11.24.05.41.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 05:41:31 -0800 (PST) From: Ruchika Gupta To: u-boot@lists.denx.de, ilias.apalodimas@linaro.org, xypron.glpk@gmx.de, agraf@csgraf.de, masahisa.kojima@linaro.org Cc: Ruchika Gupta Subject: [PATCH v4 3/3] efi_loader: Extend PCR's for firmware measurements Date: Wed, 24 Nov 2021 19:10:44 +0530 Message-Id: <20211124134044.454168-3-ruchika.gupta@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211124134044.454168-1-ruchika.gupta@linaro.org> References: <20211124134044.454168-1-ruchika.gupta@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.37 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 Firmwares before U-Boot may be capable of doing tpm measurements and passing them to U-Boot in the form of eventlog. However there may be scenarios where the firmwares don't have TPM driver and are not capable of extending the measurements in the PCRs. Based on TCG spec, if previous firnware has extended PCR's, PCR0 would not be 0. So, read the PCR0 to determine if the PCR's need to be extended as eventlog is parsed or not. Signed-off-by: Ruchika Gupta Reviewed-by: Ilias Apalodimas --- v4 : No change v3 : Rebase changes on top of changes made in first patch of series v2 : Removed check for PCR0 in eventlog lib/efi_loader/efi_tcg2.c | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c index 1713cfff70..08b02e9e1f 100644 --- a/lib/efi_loader/efi_tcg2.c +++ b/lib/efi_loader/efi_tcg2.c @@ -199,6 +199,43 @@ static efi_status_t tcg2_pcr_extend(struct udevice *dev, u32 pcr_index, return EFI_SUCCESS; } +/* tcg2_pcr_read - Read PCRs for a TPM2 device for a given tpml_digest_values + * + * @dev: device + * @digest_list: list of digest algorithms to extend + * + * @Return: status code + */ +static efi_status_t tcg2_pcr_read(struct udevice *dev, u32 pcr_index, + struct tpml_digest_values *digest_list) +{ + struct tpm_chip_priv *priv; + unsigned int updates, pcr_select_min; + u32 rc; + size_t i; + + priv = dev_get_uclass_priv(dev); + if (!priv) + return EFI_DEVICE_ERROR; + + pcr_select_min = priv->pcr_select_min; + + for (i = 0; i < digest_list->count; i++) { + u16 hash_alg = digest_list->digests[i].hash_alg; + u8 *digest = (u8 *)&digest_list->digests[i].digest; + + rc = tpm2_pcr_read(dev, pcr_index, pcr_select_min, + hash_alg, digest, alg_to_len(hash_alg), + &updates); + if (rc) { + EFI_PRINT("Failed to read PCR\n"); + return EFI_DEVICE_ERROR; + } + } + + return EFI_SUCCESS; +} + /* put_event - Append an agile event to an eventlog * * @pcr_index: PCR index @@ -1428,6 +1465,8 @@ efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer, u32 pcr, pos; u64 base; u32 sz; + bool extend_pcr = false; + int i; ret = platform_get_eventlog(dev, &base, &sz); if (ret != EFI_SUCCESS) @@ -1449,6 +1488,26 @@ efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer, return EFI_COMPROMISED_DATA; } + ret = tcg2_pcr_read(dev, 0, &digest_list); + if (ret) { + log_err("Error reading PCR 0\n"); + return ret; + } + + /* + * If PCR0 is 0, previous firmware didn't have the capability + * to extend the PCR. In this scenario, extend the PCR as + * the eventlog is parsed. + */ + for (i = 0; i < digest_list.count; i++) { + u8 buffer[TPM2_DIGEST_LEN] = { 0 }; + u16 hash_alg = digest_list.digests[i].hash_alg; + + if (!memcmp((u8 *)&digest_list.digests[i].digest, buffer, + alg_to_len(hash_alg))) + extend_pcr = true; + } + while (pos < sz) { ret = tcg2_parse_event(dev, buffer, sz, &pos, &digest_list, &pcr); @@ -1456,6 +1515,22 @@ efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer, log_err("Error parsing event\n"); return ret; } + if (extend_pcr) { + ret = tcg2_pcr_extend(dev, pcr, &digest_list); + if (ret != EFI_SUCCESS) { + log_err("Error in extending PCR\n"); + return ret; + } + + /* Clear the digest for next event */ + for (i = 0; i < digest_list.count; i++) { + u16 hash_alg = digest_list.digests[i].hash_alg; + u8 *digest = + (u8 *)&digest_list.digests[i].digest; + + memset(digest, 0, alg_to_len(hash_alg)); + } + } } memcpy(log_buffer, buffer, sz);