Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp4890434imd; Tue, 30 Oct 2018 08:52:52 -0700 (PDT) X-Google-Smtp-Source: AJdET5elSqnHPf2wI61OZHjdRdkG22HGzhZ0YtFIUjqZ6lN/LX5tvCExxEEhEgw+YtpF1EWdt4gD X-Received: by 2002:a62:9f11:: with SMTP id g17-v6mr3482331pfe.144.1540914772211; Tue, 30 Oct 2018 08:52:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540914772; cv=none; d=google.com; s=arc-20160816; b=NSR8lGVDttHHn6HZUy9N+9scHE7DXdyszXDBI1OjOMa2l3P4zRiS9eYSgkDsqDgPpc YvYTN3Bb3juZd0b4dztZo04s4uDQokcfYjokld0mQ3w0Xgjd0+BGo9uTFDujff0viFOt kfXcCWGMk/aPuw73s6/NfHKlzJ94IRD9VbsYPIa6frdBTHNEsKdlzWWXp7K9EqZ4+pE7 sjwEK53lpPbgn0K6gOil0ClAdGZr672WC1qZGacy2l4IITLE02ZnMLwUK2RK1HLyisVE l4X/Kdl5o0jDwvLxrsg8FmlvToytvDKUx5g20dE0rCNGKbJpz5hz7C7qlnoZL/P2hSuU EqOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=J2WS77sQNtDkGNoZp5hsRXNLZ4lqem+GD4PjtBez3x4=; b=uVhlmZu4ZEVpizkbquc4icyqJrLrdxpD73Eiy2x4l0Sd6ywKxREbhXHvmoxhT+imW7 rb1+rGmVLN71VqLGV507QYDBysXDbpjLVpvrqYfDaXzWhC7jDSzSMsEVYBvFmxmJqPDp eAA90eM+0iPT59+HnnCCz9EfwsLojOirBI1IuzSs18ARAgpL8TBhcDt4lLPtjgc+/apo fNooQzOXfINVTKi7jVrXDPXuunn9V//cbTiflkJ4O6/ae3kCSkBqJGqzF3lufS8oMKhS ZMz45FGjGRiT4C2eiPLqFAKPZphANeTlogqP1PK+Kbm+P7jsNOe1+NVpOC3shijGLfNj 2kjQ== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g9-v6si22921273plo.328.2018.10.30.08.52.36; Tue, 30 Oct 2018 08:52:52 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727825AbeJaAp0 (ORCPT + 99 others); Tue, 30 Oct 2018 20:45:26 -0400 Received: from lhrrgout.huawei.com ([185.176.76.210]:32708 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726792AbeJaAp0 (ORCPT ); Tue, 30 Oct 2018 20:45:26 -0400 Received: from lhreml703-cah.china.huawei.com (unknown [172.18.7.106]) by Forcepoint Email with ESMTP id A0FA8A7E41F59; Tue, 30 Oct 2018 15:51:23 +0000 (GMT) Received: from roberto-HP-EliteDesk-800-G2-DM-65W.huawei.com (10.204.65.153) by smtpsuk.huawei.com (10.201.108.44) with Microsoft SMTP Server (TLS) id 14.3.408.0; Tue, 30 Oct 2018 15:51:15 +0000 From: Roberto Sassu To: , CC: , , , , Roberto Sassu Subject: [PATCH v3 5/5] tpm: ensure that output of PCR read contains the correct digest size Date: Tue, 30 Oct 2018 16:47:11 +0100 Message-ID: <20181030154711.2782-6-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181030154711.2782-1-roberto.sassu@huawei.com> References: <20181030154711.2782-1-roberto.sassu@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.204.65.153] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch ensures that the digest size returned by the TPM during a PCR read matches the size of the algorithm passed as argument to tpm2_pcr_read(). The check is performed after information about the PCR banks has been retrieved. Signed-off-by: Roberto Sassu --- drivers/char/tpm/tpm2-cmd.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c index 8e821e7b4674..477dcc30fc53 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -187,15 +187,28 @@ struct tpm2_pcr_read_out { int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, struct tpm_digest *digest_struct, u16 *digest_size_ptr) { + int i; int rc; struct tpm_buf buf; struct tpm2_pcr_read_out *out; u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0}; u16 digest_size; + u16 expected_digest_size = 0; if (pcr_idx >= TPM2_PLATFORM_PCR) return -EINVAL; + if (!digest_size_ptr) { + for (i = 0; i < ARRAY_SIZE(chip->active_banks) && + chip->active_banks[i].alg_id != digest_struct->alg_id; i++) + ; + + if (i == ARRAY_SIZE(chip->active_banks)) + return -EINVAL; + + expected_digest_size = chip->active_banks[i].digest_size; + } + rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ); if (rc) return rc; @@ -215,7 +228,8 @@ int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE]; digest_size = be16_to_cpu(out->digest_size); - if (digest_size > sizeof(digest_struct->digest)) { + if ((digest_size_ptr && digest_size > sizeof(digest_struct->digest)) || + (!digest_size_ptr && digest_size != expected_digest_size)) { rc = -EINVAL; goto out; } -- 2.17.1