Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp165714ybm; Mon, 20 May 2019 13:56:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqwRuC2lcpkUEXfMn50B5nYLhWyeWmoQZhx0+pjBoD1+4dF4uvOxmsMaUyX8FSef3DrjPfse X-Received: by 2002:a17:902:c01:: with SMTP id 1mr39241292pls.142.1558385803112; Mon, 20 May 2019 13:56:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558385803; cv=none; d=google.com; s=arc-20160816; b=T2rt7fU3/Xh/ljOJbfwXChEps132TcRwH3WHOLJXa5e/TKe2Vo3sd+ra8YU8peEGWC 0ViX5zARtyYuovPhKMnrMQ6ST+utrIZ0LUEFCF5AC60MJAa1qE93a6UitmL57fgGz7bN BsD5krtPBGw9h+qVNEQZGHRB48w/ay7ccLBMtH3GkgpK26EhuHBTuIEeoQLjkuJ6NiO8 PcT9BU+/qASn2v/Dfh7fcprw8uT/kERCHKkLvQ+n31NolWOJTxMXQr7e1egshZrKQ8Hu X/xWj9/Opuy9zxhzxB+dOig7YDi/jZwilNr0nNNkA2mN7mjkqcMV1Z1o1jqu0OAUWVUD vNYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=2S/gHsQaO08L1k/kaLAgP/iaI6c/d06OZBDkdH1Ekx0=; b=iQsUGzOoza3ljCo9McQqJtcYiXtK5DHIQOxLCrBO2D08oegzdALKZ38YmbOvykFn+m yvOmO7T3+BYISHN4GSXyLQPmB+/Ev40u8O0mjUqeWUZO/3KbmSE7v2uIXKxa0z79RQug DCW7zH4S1OWgvTk7TPzpqmGqirQ3gsMaWWfzQYmQOdoi0uw89q8hjcf40UIYcnDUIcSP 6i+0hRsxJlwZtyrEkt6Gm5cqxt48Ra1BABI/k0lugzEOElrNW5Kkd7arVTF1vnwnFlzV z/4KxpLwIVOs2QgqSTPTnz8v1jpVfmy5LSAkOAdHLz1Q+NRmIZQ8iY+b7BLhI0QDMZvz MfuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=bn7WHiEN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j6si14403549pfi.184.2019.05.20.13.56.27; Mon, 20 May 2019 13:56:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=bn7WHiEN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726998AbfETUzS (ORCPT + 99 others); Mon, 20 May 2019 16:55:18 -0400 Received: from mail-pl1-f202.google.com ([209.85.214.202]:48813 "EHLO mail-pl1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726985AbfETUzR (ORCPT ); Mon, 20 May 2019 16:55:17 -0400 Received: by mail-pl1-f202.google.com with SMTP id f7so9817361plm.15 for ; Mon, 20 May 2019 13:55:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=2S/gHsQaO08L1k/kaLAgP/iaI6c/d06OZBDkdH1Ekx0=; b=bn7WHiENp+Jgwq76sRokv6v0WVMG7P/MdlQ0xYiJJeuGCup45bH514LUOM4BaBFOuw hIy6lnvtRfymw2xIt/dCmLB5QS6fGxTOlZYBEXpo0fOw05p1vzBfpNRMnKilxoy8UJmN RundMf4Zidiv5pdMqiDJvIqlfmZe4p5XNL2Bn1ZR7Dqmvkh6yYJEVYefZZAvfG0DAdhn WVNDbeYz1YoQ3AbvTdCoacQlkFj8rWvYmubnWoAplDxM0LIxIKMh5grd4dEY/YsLNvRf rvu7FgRa2sS22ddDAmespjdjtj/wHQO2czYud2euSGrg99uYS6zQvwMyGrquZxSrhL+P d4xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=2S/gHsQaO08L1k/kaLAgP/iaI6c/d06OZBDkdH1Ekx0=; b=Wgbv9IjqCKf5sarU86MCSPOaCQqEcksT+nK8X4AX3M59V52giXtszlo8sfdgtCgMNH Q22ufpMLWDDNraG32QyET1Vdb2PcHp+RZqYEVAIPHWm2RH710YZgEWpheL601ubtJi1c lyNG0P/FbjGZUTb9KdJVFbtKqUHD1HXB8Qt9amXRcPgeAsh6GBLrIwXqC4BJjdBF0kCn 2Xp9ilhltvOURbyoLYzom5W2nc4tjq7IhjSZHWa1kHXDB15HD6SGgPoBljUlmRag/czD OBMiQXWOKE5E0y1uZOJzMu6q0VaXcADsWb1pbLvlpa27Kqx+1dKlSQbMvk4GCfeUZVcS Dv4A== X-Gm-Message-State: APjAAAVzsMqfLmIdqCkf9Zt6Zopnd6/Np0yxhti6iapcnUQzartC/ZyI kqvAyG+FTebUbqxkhDQHLBOfwaALNb/n8qd9K0VeCQ== X-Received: by 2002:a63:1061:: with SMTP id 33mr51272273pgq.328.1558385715691; Mon, 20 May 2019 13:55:15 -0700 (PDT) Date: Mon, 20 May 2019 13:55:01 -0700 In-Reply-To: <20190520205501.177637-1-matthewgarrett@google.com> Message-Id: <20190520205501.177637-5-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190520205501.177637-1-matthewgarrett@google.com> X-Mailer: git-send-email 2.21.0.1020.gf2820cf01a-goog Subject: [PATCH V7 4/4] efi: Attempt to get the TCG2 event log in the boot stub From: Matthew Garrett To: linux-integrity@vger.kernel.org Cc: peterhuewe@gmx.de, jarkko.sakkinen@linux.intel.com, jgg@ziepe.ca, roberto.sassu@huawei.com, linux-efi@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, tweek@google.com, bsz@semihalf.com, Matthew Garrett Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Garrett Right now we only attempt to obtain the SHA1-only event log. The protocol also supports a crypto agile log format, which contains digests for all algorithms in use. Attempt to obtain this first, and fall back to obtaining the older format if the system doesn't support it. This is lightly complicated by the event sizes being variable (as we don't know in advance which algorithms are in use), and the interface giving us back a pointer to the start of the final entry rather than a pointer to the end of the log - as a result, we need to parse the final entry to figure out its length in order to know how much data to copy up to the OS. Signed-off-by: Matthew Garrett --- drivers/firmware/efi/libstub/tpm.c | 50 ++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/drivers/firmware/efi/libstub/tpm.c b/drivers/firmware/efi/libstub/tpm.c index 5bd04f75d8d6..6b3b507a54eb 100644 --- a/drivers/firmware/efi/libstub/tpm.c +++ b/drivers/firmware/efi/libstub/tpm.c @@ -57,7 +57,7 @@ void efi_enable_reset_attack_mitigation(efi_system_table_t *sys_table_arg) #endif -static void efi_retrieve_tpm2_eventlog_1_2(efi_system_table_t *sys_table_arg) +void efi_retrieve_tpm2_eventlog(efi_system_table_t *sys_table_arg) { efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID; efi_guid_t linux_eventlog_guid = LINUX_EFI_TPM_EVENT_LOG_GUID; @@ -67,6 +67,7 @@ static void efi_retrieve_tpm2_eventlog_1_2(efi_system_table_t *sys_table_arg) unsigned long first_entry_addr, last_entry_addr; size_t log_size, last_entry_size; efi_bool_t truncated; + int version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2; void *tcg2_protocol = NULL; status = efi_call_early(locate_protocol, &tcg2_guid, NULL, @@ -74,14 +75,20 @@ static void efi_retrieve_tpm2_eventlog_1_2(efi_system_table_t *sys_table_arg) if (status != EFI_SUCCESS) return; - status = efi_call_proto(efi_tcg2_protocol, get_event_log, tcg2_protocol, - EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2, - &log_location, &log_last_entry, &truncated); - if (status != EFI_SUCCESS) - return; + status = efi_call_proto(efi_tcg2_protocol, get_event_log, + tcg2_protocol, version, &log_location, + &log_last_entry, &truncated); + + if (status != EFI_SUCCESS || !log_location) { + version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2; + status = efi_call_proto(efi_tcg2_protocol, get_event_log, + tcg2_protocol, version, &log_location, + &log_last_entry, &truncated); + if (status != EFI_SUCCESS || !log_location) + return; + + } - if (!log_location) - return; first_entry_addr = (unsigned long) log_location; /* @@ -96,8 +103,23 @@ static void efi_retrieve_tpm2_eventlog_1_2(efi_system_table_t *sys_table_arg) * We need to calculate its size to deduce the full size of * the logs. */ - last_entry_size = sizeof(struct tcpa_event) + - ((struct tcpa_event *) last_entry_addr)->event_size; + if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) { + /* + * The TCG2 log format has variable length entries, + * and the information to decode the hash algorithms + * back into a size is contained in the first entry - + * pass a pointer to the final entry (to calculate its + * size) and the first entry (so we know how long each + * digest is) + */ + last_entry_size = + __calc_tpm2_event_size((void *)last_entry_addr, + (void *)(long)log_location, + false); + } else { + last_entry_size = sizeof(struct tcpa_event) + + ((struct tcpa_event *) last_entry_addr)->event_size; + } log_size = log_last_entry - log_location + last_entry_size; } @@ -114,7 +136,7 @@ static void efi_retrieve_tpm2_eventlog_1_2(efi_system_table_t *sys_table_arg) memset(log_tbl, 0, sizeof(*log_tbl) + log_size); log_tbl->size = log_size; - log_tbl->version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2; + log_tbl->version = version; memcpy(log_tbl->log, (void *) first_entry_addr, log_size); status = efi_call_early(install_configuration_table, @@ -126,9 +148,3 @@ static void efi_retrieve_tpm2_eventlog_1_2(efi_system_table_t *sys_table_arg) err_free: efi_call_early(free_pool, log_tbl); } - -void efi_retrieve_tpm2_eventlog(efi_system_table_t *sys_table_arg) -{ - /* Only try to retrieve the logs in 1.2 format. */ - efi_retrieve_tpm2_eventlog_1_2(sys_table_arg); -} -- 2.21.0.1020.gf2820cf01a-goog