From patchwork Wed Feb 27 20:26:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Garrett X-Patchwork-Id: 10832311 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 40ED613B5 for ; Wed, 27 Feb 2019 20:27:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 35B152EB87 for ; Wed, 27 Feb 2019 20:27:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 29B652EB55; Wed, 27 Feb 2019 20:27:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 287E12EB8F for ; Wed, 27 Feb 2019 20:27:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730353AbfB0U1H (ORCPT ); Wed, 27 Feb 2019 15:27:07 -0500 Received: from mail-pl1-f201.google.com ([209.85.214.201]:45740 "EHLO mail-pl1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730342AbfB0U1G (ORCPT ); Wed, 27 Feb 2019 15:27:06 -0500 Received: by mail-pl1-f201.google.com with SMTP id e2so13247075pln.12 for ; Wed, 27 Feb 2019 12:27:05 -0800 (PST) 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=jrd68x/FfHXCt7CWtZJCh8NI2YSDkYhEoPHIj9aDd98=; b=Mr+4Z61H4ihzxA34NS2wOMwksF4TBqoI5xQEP3vt/c9ak352gyuXdgBhX3eTgAZgEi LtDnaLQZ+o0xtLsn7yeYUSGlYcsjOA8NmRfeEZiqmnr64eDpKkmm+cTGCxZuvUdJOkJa mTZ+GczaudDIuNrq0SlU9uwBmIubvXSOpgoHU5vx+ITprhq9EkieilaV3We3ID21//d/ 1EeBA28sRf03O4oGVKmQIJX9WGBk19mcSqCZFgDcv4RZLDnjcWorDnSzAs1rJyxoIE+d FIok+G2VB41l3Wrl9TDwwchEUfl9SO1lFDQXzsHzLwk9F6QUkNjGJUocIW469A3jojsR ljog== 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=jrd68x/FfHXCt7CWtZJCh8NI2YSDkYhEoPHIj9aDd98=; b=iy0rNfACqcYlJW5obd0H3U32NYBzk4F+TpW7tRNwp5k9EMkt+ifKyvChg5Tk++tR5k pcm0sLEeMtsjKkPd+gvHdqarl06/xDxU6oL00a+GrQCnWxM1x4CwOSDbUIPNBirrk1sA Fl1gh+lzcgrGYGSF+RRjBFuh7FL8KHWW/V8xueIVwIqK5Me9IsBf42ptaHawZtWieGn1 RqyNhhztxNCGSekhaVVg1V7DmNVTsv+q7lor7DVxy58bH+2wfVi8GGi44bQijG2Ztafw 45gK+hM9b85Pzhgtqe1UHhTL9V7y+NtiAsLW1SZ+FfOSBijnbTSCy96iUc7FBkzOazrD kykA== X-Gm-Message-State: AHQUAubDRbGtZEMwoHeSSFRjs28EcM9TXj5vbI5fy98mlVthmocL0JCR pjwF9VTIs66JgFqMhlJDd5bVJqlxGij5kySc3hqzV0feeIclkKTIX88glp9G/sjcZ+OJw2LaqsN hvHJTtcUlel46FFHkhvDeq9QvRc+pTbPrgupnNpLK9T6GGSyqjAiupNmyGfSL5ZwCYFcVF+5k1+ pQwZBYCvsL/EkbFTJg/6E= X-Google-Smtp-Source: AHgI3IZgRw3yl6qrdUSq8bs/1lWfV5Ug2/XKiOGV829PVXdXG1iGG/yH0d2eMrA/FwlVVnkUA+8RsIZPCKCwrNQJb4tZXQ== X-Received: by 2002:a17:902:8ec4:: with SMTP id x4mr1289915plo.124.1551299225370; Wed, 27 Feb 2019 12:27:05 -0800 (PST) Date: Wed, 27 Feb 2019 12:26:55 -0800 In-Reply-To: <20190227202658.197113-1-matthewgarrett@google.com> Message-Id: <20190227202658.197113-2-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190227202658.197113-1-matthewgarrett@google.com> X-Mailer: git-send-email 2.21.0.352.gf09ad66450-goog Subject: [PATCH V5 1/4] tpm: Abstract crypto agile event size calculations 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, Matthew Garrett Sender: linux-integrity-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Matthew Garrett We need to calculate the size of crypto agile events in multiple locations, including in the EFI boot stub. The easiest way to do this is to put it in a header file as an inline and leave a wrapper to ensure we don't end up with multiple copies of it embedded in the existing code. Signed-off-by: Matthew Garrett --- drivers/char/tpm/eventlog/tpm2.c | 47 +--------------------- include/linux/tpm_eventlog.h | 68 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 46 deletions(-) diff --git a/drivers/char/tpm/eventlog/tpm2.c b/drivers/char/tpm/eventlog/tpm2.c index f824563fc28d..1a977bdd3bd2 100644 --- a/drivers/char/tpm/eventlog/tpm2.c +++ b/drivers/char/tpm/eventlog/tpm2.c @@ -40,52 +40,7 @@ static size_t calc_tpm2_event_size(struct tcg_pcr_event2_head *event, struct tcg_pcr_event *event_header) { - struct tcg_efi_specid_event_head *efispecid; - struct tcg_event_field *event_field; - void *marker; - void *marker_start; - u32 halg_size; - size_t size; - u16 halg; - int i; - int j; - - marker = event; - marker_start = marker; - marker = marker + sizeof(event->pcr_idx) + sizeof(event->event_type) - + sizeof(event->count); - - efispecid = (struct tcg_efi_specid_event_head *)event_header->event; - - /* Check if event is malformed. */ - if (event->count > efispecid->num_algs) - return 0; - - for (i = 0; i < event->count; i++) { - halg_size = sizeof(event->digests[i].alg_id); - memcpy(&halg, marker, halg_size); - marker = marker + halg_size; - for (j = 0; j < efispecid->num_algs; j++) { - if (halg == efispecid->digest_sizes[j].alg_id) { - marker += - efispecid->digest_sizes[j].digest_size; - break; - } - } - /* Algorithm without known length. Such event is unparseable. */ - if (j == efispecid->num_algs) - return 0; - } - - event_field = (struct tcg_event_field *)marker; - marker = marker + sizeof(event_field->event_size) - + event_field->event_size; - size = marker - marker_start; - - if ((event->event_type == 0) && (event_field->event_size == 0)) - return 0; - - return size; + return __calc_tpm2_event_size(event, event_header); } static void *tpm2_bios_measurements_start(struct seq_file *m, loff_t *pos) diff --git a/include/linux/tpm_eventlog.h b/include/linux/tpm_eventlog.h index 81519f163211..6a86144e13f1 100644 --- a/include/linux/tpm_eventlog.h +++ b/include/linux/tpm_eventlog.h @@ -112,4 +112,72 @@ struct tcg_pcr_event2_head { struct tpm_digest digests[]; } __packed; +/** + * __calc_tpm2_event_size - calculate the size of a TPM2 event log entry + * @event: Pointer to the event whose size should be calculated + * @event_header: Pointer to the initial event containing the digest lengths + * + * The TPM2 event log format can contain multiple digests corresponding to + * separate PCR banks, and also contains a variable length of the data that + * was measured. This requires knowledge of how long each digest type is, + * and this information is contained within the first event in the log. + * + * We calculate the length by examining the number of events, and then looking + * at each event in turn to determine how much space is used for events in + * total. Once we've done this we know the offset of the data length field, + * and can calculate the total size of the event. + * + * Return: size of the event on success, <0 on failure + */ + +static inline int __calc_tpm2_event_size(struct tcg_pcr_event2_head *event, + struct tcg_pcr_event *event_header) +{ + struct tcg_efi_specid_event_head *efispecid; + struct tcg_event_field *event_field; + void *marker; + void *marker_start; + u32 halg_size; + size_t size; + u16 halg; + int i; + int j; + + marker = event; + marker_start = marker; + marker = marker + sizeof(event->pcr_idx) + sizeof(event->event_type) + + sizeof(event->count); + + efispecid = (struct tcg_efi_specid_event_head *)event_header->event; + + /* Check if event is malformed. */ + if (event->count > efispecid->num_algs) + return 0; + + for (i = 0; i < event->count; i++) { + halg_size = sizeof(event->digests[i].alg_id); + memcpy(&halg, marker, halg_size); + marker = marker + halg_size; + for (j = 0; j < efispecid->num_algs; j++) { + if (halg == efispecid->digest_sizes[j].alg_id) { + marker += + efispecid->digest_sizes[j].digest_size; + break; + } + } + /* Algorithm without known length. Such event is unparseable. */ + if (j == efispecid->num_algs) + return 0; + } + + event_field = (struct tcg_event_field *)marker; + marker = marker + sizeof(event_field->event_size) + + event_field->event_size; + size = marker - marker_start; + + if ((event->event_type == 0) && (event_field->event_size == 0)) + return 0; + + return size; +} #endif