Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp177820imu; Thu, 8 Nov 2018 06:49:11 -0800 (PST) X-Google-Smtp-Source: AJdET5f6plkVcGq8UaUrnatCPSnOpcfJJw5DyBOamVWgdl0rx/NjpVtIUeetHtuWXENNvisfpSQ+ X-Received: by 2002:a62:ce8e:: with SMTP id y136-v6mr4828823pfg.201.1541688551305; Thu, 08 Nov 2018 06:49:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541688551; cv=none; d=google.com; s=arc-20160816; b=qIZswy2BVNiBVe142hREOHZYKa0BHlzn3Bw+0d7T7p8h0j8BilDRYgkAU7ECD0Si6C q86HJz+jJha074EsuS0djPjUPemuTHg9AoUUyYJhOEJl0Vggm0VjVsJyCVpdl/GQ8huT 7a8nZXdFBoBCNEC6qFvTTF9vcwqXa+6bBI8KohR0Xpuk6LHDP6lD5Gwz17WdhpqrwYFW OxVl6m9pPJUDmoQOx+uMxzvyISz+skuPDnB8w/lNI9zwPhcCpUHk09gRHo2lyo0ECN3t IASF2N3po6qIUb42TZzSWE6qxW3PeEx1huJOW7cHCAr9Vvy+D9NhZp6T8bGWugtd3rHj OGCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=kzy7+RGkCoaxHJ2GRGBnnQczm5YeNvTDYe3BYKv44jo=; b=QeXmDFa4yU9tfQScgS6rC5VqfFScfFkUKLrzSZKeUBKCgDuKtykjZOM83r8Pc7WCvP r2NQmTJKkg4C++QrlA56nK1Hc3Lip0lyktr1bAApG3kHZKdo2SKRTv3L+isOzkCTlwpv gprPmUYPMf4JvF5wdZl0+XEPl9xPZU/baY7WBdy7TY9DcznJ+h93tljQAaOzcKM0gfq5 4icfa+OgQAmWrIFzNGM6+5c/SGeCjkR2QHv00MpXif+MBAh55bviO1EKaS29oyiFpWnF N1lRFlPYoUQcZlOLdckIyqnGotvfpBQ3WsDP0lTk7CqpzFuO3y/1fTkRjXrxkY7CpWY+ uxvA== 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 m61-v6si4539033plb.311.2018.11.08.06.48.55; Thu, 08 Nov 2018 06:49:11 -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 S1727763AbeKIAXk (ORCPT + 99 others); Thu, 8 Nov 2018 19:23:40 -0500 Received: from lhrrgout.huawei.com ([185.176.76.210]:32725 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726801AbeKIAXj (ORCPT ); Thu, 8 Nov 2018 19:23:39 -0500 Received: from LHREML714-CAH.china.huawei.com (unknown [172.18.7.107]) by Forcepoint Email with ESMTP id D10F05BF24125; Thu, 8 Nov 2018 14:47:46 +0000 (GMT) Received: from [10.204.65.142] (10.204.65.142) by smtpsuk.huawei.com (10.201.108.37) with Microsoft SMTP Server (TLS) id 14.3.408.0; Thu, 8 Nov 2018 14:47:40 +0000 Subject: Re: [PATCH v4 6/6] tpm: ensure that the output of PCR read contains the correct digest size To: Jarkko Sakkinen CC: , , , , References: <20181106150159.1136-1-roberto.sassu@huawei.com> <20181106150159.1136-7-roberto.sassu@huawei.com> <20181108140814.GF8922@linux.intel.com> From: Roberto Sassu Message-ID: <353ac2d0-591f-f950-f3ad-18e6a4f6085c@huawei.com> Date: Thu, 8 Nov 2018 15:47:39 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20181108140814.GF8922@linux.intel.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.204.65.142] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/8/2018 3:08 PM, Jarkko Sakkinen wrote: > On Tue, Nov 06, 2018 at 04:01:59PM +0100, Roberto Sassu wrote: >> 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 as argument to tpm2_pcr_read(). >> >> This 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 e2d5b84286a7..3b0b5b032901 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 < chip->nr_active_banks && >> + chip->active_banks[i].alg_id != digest_struct->alg_id; i++) >> + ; > > Not sure if the semicolon should be in its own line. scripts/Lindent suggests: for (i = 0; i < chip->nr_active_banks && chip->active_banks[i].alg_id != digest_struct->alg_id; i++) ; but this is not accepted by scripts/checkpatch.pl (there are no warnings/errors on the patch I sent). >> + >> + if (i == chip->nr_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 > sizeof(digest_struct->digest) || >> + (!digest_size_ptr && digest_size != expected_digest_size)) { >> rc = -EINVAL; >> goto out; >> } >> -- >> 2.17.1 >> > > Please add > > Cc: stable@vger.kernel.org. > > /Jarkko > -- HUAWEI TECHNOLOGIES Duesseldorf GmbH, HRB 56063 Managing Director: Bo PENG, Jian LI, Yanli SHI