Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7980653imu; Tue, 4 Dec 2018 00:28:36 -0800 (PST) X-Google-Smtp-Source: AFSGD/VEB2NWTAhkty7Od+1qhCL7KFBO8MaMDCuXa9fQIrRX1kY2oouccsmK8nNP5pfO3nlDPBhr X-Received: by 2002:a63:b218:: with SMTP id x24mr15546269pge.223.1543912116435; Tue, 04 Dec 2018 00:28:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543912116; cv=none; d=google.com; s=arc-20160816; b=OnfBUml3s3ImReSepA7o45egkaRffnJ5Va4Zmbb6aUOlXy6V0H3p1lnZm1V6VCI8pd 2UumR/WzNPPBZCXdaueT5a6DRiGdFK0vrbsym6oGzQUxR4kdpjT3nxA4b/QAGFaHOYD9 En1UY3lJTi2J2WCG/4sEzf9JXS79geEEdMcT/L011yudMGUIUNgmDWl9Fcg+ox2480iG kaaqi2XTxe2/ryu5mgfEoCOn+gcny/RvfDyiWYVQcmXuMtEiDaZ5zfhX44IFeDKFp5RW ObS1xUtaOWvnAGDi7JsYhUM3Pf4dzAZbPA3Wl0lHsm10348qVwWc2UDOf23QU5DjJXRk Txpw== 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=HMw/tKCCZ/AshbMToNABDFEpDY27a+EODXf2Wmt0mE0=; b=jykvmGRbcsIv65LqEnL9uCr5rECEm+xyueF7i+U3kW3HLMCPjboy/sVZwVu+9tLbVv mAbYYMIcU9l8pSfdTIXYJ4LgKRcQjgRNzy26Q4EJ/7CM7flT2OLTucO8ErTUqfM4oCv6 EhzH2eBsRbOrduKZhPFrCznvKhI3dqCtfxkiJB+qGnFWuSSBdnbivH93J943Nwd6FS1P LP18v8k9+4DAjuPBrcRP55578SZvRjCYoNqYQpo9UOPgwzqxpNDPMQxelV0fFQifRzIz qzPbl76LhGii0bRAsfbz+kf0lgviJ1XFi+SJSlkeCQbTCAsCtEGmDdt1sE+LnwVBj/BP SMpg== 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 n4si13951920pgv.512.2018.12.04.00.28.21; Tue, 04 Dec 2018 00:28:36 -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 S1726126AbeLDI1i (ORCPT + 99 others); Tue, 4 Dec 2018 03:27:38 -0500 Received: from lhrrgout.huawei.com ([185.176.76.210]:32796 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726005AbeLDI1h (ORCPT ); Tue, 4 Dec 2018 03:27:37 -0500 Received: from lhreml701-cah.china.huawei.com (unknown [172.18.7.108]) by Forcepoint Email with ESMTP id 14BAFAD65D6BF; Tue, 4 Dec 2018 08:27:34 +0000 (GMT) Received: from roberto-HP-EliteDesk-800-G2-DM-65W.huawei.com (10.204.65.153) by smtpsuk.huawei.com (10.201.108.42) with Microsoft SMTP Server (TLS) id 14.3.408.0; Tue, 4 Dec 2018 08:27:27 +0000 From: Roberto Sassu To: , , , CC: , , , , Roberto Sassu Subject: [PATCH v6 6/7] tpm: ensure that the output of PCR read contains the correct digest size Date: Tue, 4 Dec 2018 09:21:37 +0100 Message-ID: <20181204082138.24600-7-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181204082138.24600-1-roberto.sassu@huawei.com> References: <20181204082138.24600-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 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 --- 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 a804f0b7126a..d6d29480348c 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -179,15 +179,28 @@ struct tpm2_pcr_read_out { int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digest, 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->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 +220,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->digest)) { + if (digest_size > sizeof(digest->digest) || + (!digest_size_ptr && digest_size != expected_digest_size)) { rc = -EINVAL; goto out; } -- 2.17.1