Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6832976imu; Wed, 14 Nov 2018 07:37:50 -0800 (PST) X-Google-Smtp-Source: AJdET5cQtD6s77nZZISU7ERlWxDXAQ8LXJ7kLBCwLtIDEi7Lt1wdKBXYh1cOtA4EC6dPwss5Yefi X-Received: by 2002:aa7:818a:: with SMTP id g10-v6mr2431013pfi.153.1542209870802; Wed, 14 Nov 2018 07:37:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542209870; cv=none; d=google.com; s=arc-20160816; b=g/R7K2+ypj/J7Eq69AHllWU0eaJqODU18p2/cXOFLYy0TuNJbt4xBWEVVRinWNmVLP 3LP/CYniCub+qQ69hdtHhPI0LPT/+sTI7sqUMFWO3aGB3yoo1QxxsuV6l4EQQRoExbjs 4l9c9PwWRVDUbzONZial09xoUdQ0hSKZ/Ni0/4Vk9s3IyRjwbE2JUIl1dAfK10yKDmht CXU/6RbX4BpFGjehX+apsogZMpBkQRgABjTyFTW60vtKTwEI7FjSdORcBqfrYwwG65X8 eVNEIKxtlSdDnG2EEqRyW4YJe3e4LhvIz17zsicAheMeWDsZYH9Wq5qyP8IkAgW5our4 b5Ow== 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=jE24mCuNmI6o2Fu9U0crq/WmTecGQ3c+jOobzv5JNCw=; b=lbYH000wge9hwaHdx0IgMfB8TQo8sOJzUzdwbxN08y6iEQp3YT/oi1mB/r77ApKDBZ wlQxZvjKhM2PJrJF0hGw0bSZ0CS6FtrqK1L4Vt5ciegPC/dGlUM/6XPpYFoAT4Z75pIm y2BNKAMaDFCJ0NQwXCMsDftuASL8jk7XyBsgilLRfhcb0OZqo4+ynqYFTxvG1gCO0RYT dqr0jzDw5EBqObsXSNbS+2gHfUpklQ4D1Jk33Dl050K/sEzTRFbGZIt2xSGk2GZFc5Gm rT4ex8axpgl/xKS6VL29e4d05ubCb8y+UI6S9p2FfkJ7DLA3R46vl5aTsKaR0sIYAiuF UwSQ== 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 y16-v6si23703649pgh.383.2018.11.14.07.37.35; Wed, 14 Nov 2018 07:37:50 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387628AbeKOBka (ORCPT + 99 others); Wed, 14 Nov 2018 20:40:30 -0500 Received: from lhrrgout.huawei.com ([185.176.76.210]:32770 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727731AbeKOBka (ORCPT ); Wed, 14 Nov 2018 20:40:30 -0500 Received: from LHREML714-CAH.china.huawei.com (unknown [172.18.7.107]) by Forcepoint Email with ESMTP id 780692FF2BF5; Wed, 14 Nov 2018 15:36:43 +0000 (GMT) Received: from roberto-HP-EliteDesk-800-G2-DM-65W.huawei.com (10.204.65.153) by smtpsuk.huawei.com (10.201.108.37) with Microsoft SMTP Server (TLS) id 14.3.408.0; Wed, 14 Nov 2018 15:36:39 +0000 From: Roberto Sassu To: , , , CC: , , , , Roberto Sassu , Subject: [PATCH v5 6/7] tpm: ensure that the output of PCR read contains the correct digest size Date: Wed, 14 Nov 2018 16:31:07 +0100 Message-ID: <20181114153108.12907-7-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181114153108.12907-1-roberto.sassu@huawei.com> References: <20181114153108.12907-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 protects against data corruption that could happen in the bus, by checking that that the digest size returned by the TPM during a PCR read matches the size of the algorithm passed to tpm2_pcr_read(). This check is performed after information about the PCR banks has been retrieved. Signed-off-by: Roberto Sassu Reviewed-by: Jarkko Sakkinen Cc: stable@vger.kernel.org --- drivers/char/tpm/tpm2-cmd.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c index acaaab72ef2e..974465f04b78 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -179,15 +179,29 @@ struct tpm2_pcr_read_out { int tpm2_pcr_read(struct tpm_chip *chip, u32 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 < chip->nr_allocated_banks && + chip->allocated_banks[i].alg_id != digest_struct->alg_id; + i++) + ; + + if (i == chip->nr_allocated_banks) + return -EINVAL; + + expected_digest_size = chip->allocated_banks[i].digest_size; + } + rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ); if (rc) return rc; @@ -207,7 +221,8 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 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 > sizeof(digest_struct->digest) || + (!digest_size_ptr && digest_size != expected_digest_size)) { rc = -EINVAL; goto out; } -- 2.17.1