Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2369902imu; Sat, 5 Jan 2019 23:27:26 -0800 (PST) X-Google-Smtp-Source: ALg8bN5HizN2u1IO6G37w7b+FMiXDvTcKrLtt9j6GL5tLfZ2ZD2bzghwn5pQw1+MKDNbinnRXGOt X-Received: by 2002:a62:ca05:: with SMTP id n5mr58735162pfg.154.1546759646166; Sat, 05 Jan 2019 23:27:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546759646; cv=none; d=google.com; s=arc-20160816; b=u3BgNZJdFBGSBahVnH5PchjWM6IgHfzQl8kQMopfq1cir0EjKewjimfZ8sQyHKZXg8 6S9fNhi+nZQZOz0huhCizyDwQSufUB6Yc79aUq0yChNbbxFuAYKDLGeTjOKFR18a4Za+ vUNRMP2m/pKludJ98EZfHLhkldpsBmjbrmKHrKWu18mtFQZhKW7oOtiUNjkX/ADjz8LO lGiIUe5bQorfQpxfaMgEL6uXkexWoZWCrUXnvbgZzfRi8zJFqIRlpHYAltace8vNoJGM fWGbOjw40T3YglK7oJn258R7g3tJmBX1ayJ2f8Sd47pbCfT9I9xNpQ5cB69XgmWd7XCt VjsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=AHkDN9Qsw8xusCZkbb9WmaKHLm47Mx9BTKeL66fuKUA=; b=TGc0h7t53VvXsHnAxgXlPzj+fGVrgRxG+JuU+nGtbaRT1ezC/H/xi0PN4CPpsdmAAY r8QHnpx4/6vyfU0nnemNZb5r7Czu+l9x4naZWndLCMCgBIAjLkOPUaa4r+PpbWuimP9c G2vjZfy+9dqcoSjLFWVjvvZw5qxttu6XS9lyUsXKIltClB0sBvz/VoaLhuAm58Abx7jy x6FdswhwGjZq79aNsHJefmPk3aXkzB1ew+5J70aeQMlDRJbq7uoXL6gtKsrSQ1+gMIyA FD87o7HjqmTYc1EGwtFX4oE96UDTHxwhE2lXeXzhcmzVyxU6V7RpuXMfexz1/34srhPA LQ5A== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c3si55786837pls.73.2019.01.05.23.27.10; Sat, 05 Jan 2019 23:27:26 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726448AbfAFHYm (ORCPT + 99 others); Sun, 6 Jan 2019 02:24:42 -0500 Received: from out30-130.freemail.mail.aliyun.com ([115.124.30.130]:55089 "EHLO out30-130.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726250AbfAFHYl (ORCPT ); Sun, 6 Jan 2019 02:24:41 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R121e4;CH=green;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e01424;MF=zhang.jia@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0THdglfm_1546759399; Received: from localhost(mailfrom:zhang.jia@linux.alibaba.com fp:SMTPD_---0THdglfm_1546759399) by smtp.aliyun-inc.com(127.0.0.1); Sun, 06 Jan 2019 15:23:20 +0800 From: Jia Zhang To: jarkko.sakkinen@linux.intel.com, peterhuewe@gmx.de, jgg@ziepe.ca, tweek@google.com Cc: linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, zhang.jia@linux.alibaba.com Subject: [PATCH 1/2] tpm/eventlog/tpm1: Simplify walking over *pos measurements Date: Sun, 6 Jan 2019 15:23:18 +0800 Message-Id: <1546759399-45360-1-git-send-email-zhang.jia@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The sanity check would be easier, especially for the first read of binary_bios_measurements from the beginning. Signed-off-by: Jia Zhang --- drivers/char/tpm/eventlog/tpm1.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/drivers/char/tpm/eventlog/tpm1.c b/drivers/char/tpm/eventlog/tpm1.c index 58c8478..4cf8303 100644 --- a/drivers/char/tpm/eventlog/tpm1.c +++ b/drivers/char/tpm/eventlog/tpm1.c @@ -74,7 +74,7 @@ /* returns pointer to start of pos. entry of tcg log */ static void *tpm1_bios_measurements_start(struct seq_file *m, loff_t *pos) { - loff_t i; + loff_t i = 0; struct tpm_chip *chip = m->private; struct tpm_bios_log *log = &chip->log; void *addr = log->bios_event_log; @@ -83,38 +83,29 @@ static void *tpm1_bios_measurements_start(struct seq_file *m, loff_t *pos) u32 converted_event_size; u32 converted_event_type; - /* read over *pos measurements */ - for (i = 0; i < *pos; i++) { + do { event = addr; + /* check if current entry is valid */ + if (addr + sizeof(struct tcpa_event) >= limit) + return NULL; + converted_event_size = do_endian_conversion(event->event_size); converted_event_type = do_endian_conversion(event->event_type); - if ((addr + sizeof(struct tcpa_event)) < limit) { - if ((converted_event_type == 0) && - (converted_event_size == 0)) - return NULL; - addr += (sizeof(struct tcpa_event) + - converted_event_size); - } - } - - /* now check if current entry is valid */ - if ((addr + sizeof(struct tcpa_event)) >= limit) - return NULL; - - event = addr; + if (((converted_event_type == 0) && (converted_event_size == 0)) + || ((addr + sizeof(struct tcpa_event) + converted_event_size) + >= limit)) + return NULL; - converted_event_size = do_endian_conversion(event->event_size); - converted_event_type = do_endian_conversion(event->event_type); + if (i++ == *pos) + break; - if (((converted_event_type == 0) && (converted_event_size == 0)) - || ((addr + sizeof(struct tcpa_event) + converted_event_size) - >= limit)) - return NULL; + addr += (sizeof(struct tcpa_event) + converted_event_size); + } while (1); return addr; } -- 1.8.3.1