Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp4167063imm; Mon, 14 May 2018 03:42:37 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoeEiPcZbTKo5ONaj1NPIeogq8GU/eoa1YIqMl5n658e7XRoHl8Be5Hjg6TaEewPEpezddj X-Received: by 2002:a62:32c6:: with SMTP id y189-v6mr9878423pfy.241.1526294556945; Mon, 14 May 2018 03:42:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526294556; cv=none; d=google.com; s=arc-20160816; b=sCUDgHG+qRE+YwhsIhV9/gIlxxrMscB+U2UTLudGAEzt5199euk1QB4V3F15/tohLi NgLpkk0mN2goRSaVmu0es40GTruQmNvjy4zfFCKtubKXzzeVET85cW4/oEN6DOhkwf/m 3UHx+9ftkumci4HkbixU5mLuee+1V9Ll3LY+rtUQrwmCDKUK/A9N6F9ultb1fQk4umvU QE3ZZOdRCg6inyF0VeLkgVkVEFjujHtcoW4BRke6Dg0v56UoRd69o1DK2tkx6ENl5FKN g3yx95DqTjMO+K3PqIXXrElmngV7xV+7aWwKma9PlMS5f2MX1zWv6k3FzdD316h9RbsN X9qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:organization:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=1EaSfclh7n6U5WiiwCdcfDeGBv1pWsxDQ2DEb4s18Kw=; b=eGsZ+Tm3aBjxdHQr+854q6kdSrMchZophrFVuiCK5PGmwl2uBRP5hqN50nV07ECkE8 1WZXOREBmo33IWg6UXpaO3S2MO8AT7mXAbKDME3FMDED4Rn6Dc6KyHXIy0AneMEEQX00 V9245OyeMCLEGzZxeJ+bOMCmuVtrAbtsgVg+MCe74UzhHQbCPVLccdmu7Xkrz6Kf8lss TxwPnExKQVQe/Q2yI5E54tjZmZUOVyDAWLThKKO0W28xFDInwKFsltGO8IdedzxmwMZe TGTRxA0OS/i+zY6etifK+w4m7dfGeDRvDlVXgNMPz8iQCDo4knVtk574xyqY/+TeZBIM 6pTw== 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 o4-v6si8835652plk.321.2018.05.14.03.42.22; Mon, 14 May 2018 03:42:36 -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 S1752169AbeENKmA (ORCPT + 99 others); Mon, 14 May 2018 06:42:00 -0400 Received: from mga14.intel.com ([192.55.52.115]:50656 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751954AbeENKl7 (ORCPT ); Mon, 14 May 2018 06:41:59 -0400 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 May 2018 03:41:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,399,1520924400"; d="scan'208";a="54121083" Received: from yixuanma-mobl.ccr.corp.intel.com (HELO localhost) ([10.249.254.136]) by fmsmga004.fm.intel.com with ESMTP; 14 May 2018 03:41:54 -0700 Date: Mon, 14 May 2018 13:41:53 +0300 From: Jarkko Sakkinen To: Nayna Jain Cc: linux-integrity@vger.kernel.org, zohar@linux.vnet.ibm.com, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, peterhuewe@gmx.de, tpmdd@selhorst.net, jgunthorpe@obsidianresearch.com Subject: Re: [PATCH v2] tpm: check selftest status before retrying full selftest Message-ID: <20180514104153.GB8228@linux.intel.com> References: <20180507153941.4952-1-nayna@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180507153941.4952-1-nayna@linux.vnet.ibm.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 07, 2018 at 09:09:41PM +0530, Nayna Jain wrote: > As per the TCG Specification[1][2], RC_COMMAND_CODE indicates that the TPM > command is not implemented or not supported. When RC_COMMAND_CODE is > returned in response to the partial selftest, this is not the case. TPM 2.0 > supports TPM2_GetTestResult[3], which can be used to check the selftest > status before sending the full selftest command. > > This patch implements the tpm2_get_selftest_result function to check the > selftest status when partial selftest returns RC_COMMAND_CODE. Cosmetic: parentheses when referring to functions. > This change results in finishing of the selftest much earlier compared to > the existing case where full selftest is immediately sent to retry. The > Pi's dmesg shows: the TPM selftest completed at 1.243864 secs compared > with the previous timestamp of 1.939667 secs. > > [1] As per the TCG Specification, Trusted Platform Module Library, > Part 2 - Structures, Section 6.6.3 and Section 4.18: > > "RC_COMMAND_CODE indicates the response code that is returned if the TPM is > unmarshalling a value that it expects to be a TPM_CC and the input value is > not in the table." > > [2] As per the TCG Specification, Trusted Platform Module Library, > Part 2 - Commands, Section 5.2: > > "The TPM shall successfully unmarshal a TPM_CC and verify that the command > is implemented (TPM_RC_COMMAND_CODE)." > > [3] As per the TCG Specification, Trusted Platform Module Library, > Part 2 - Commands, Section 10.4: > > "This command(TPM2_GetTestResult) returns manufacturer-specific information > regarding the results of a self-test and an indication of the test status." > > Signed-off-by: Nayna Jain > Tested-by: Mimi Zohar (on Pi with TPM 2.0) > --- > > Changelog v2: > * changed the subject and updated patch description > * removed the logs > > drivers/char/tpm/tpm.h | 2 ++ > drivers/char/tpm/tpm2-cmd.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 50 insertions(+) > > diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h > index af3bb87d3ea1..1de4240b52c4 100644 > --- a/drivers/char/tpm/tpm.h > +++ b/drivers/char/tpm/tpm.h > @@ -114,6 +114,7 @@ enum tpm2_return_codes { > TPM2_RC_FAILURE = 0x0101, > TPM2_RC_DISABLED = 0x0120, > TPM2_RC_COMMAND_CODE = 0x0143, > + TPM2_RC_NEEDS_TEST = 0x0153, > TPM2_RC_TESTING = 0x090A, /* RC_WARN */ > TPM2_RC_REFERENCE_H0 = 0x0910, > TPM2_RC_RETRY = 0x0922, > @@ -144,6 +145,7 @@ enum tpm2_command_codes { > TPM2_CC_FLUSH_CONTEXT = 0x0165, > TPM2_CC_GET_CAPABILITY = 0x017A, > TPM2_CC_GET_RANDOM = 0x017B, > + TPM2_CC_GET_TEST_RESULT = 0x017C, > TPM2_CC_PCR_READ = 0x017E, > TPM2_CC_PCR_EXTEND = 0x0182, > TPM2_CC_LAST = 0x018F, > diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c > index 96c77c8e7f40..4abba0ebe25b 100644 > --- a/drivers/char/tpm/tpm2-cmd.c > +++ b/drivers/char/tpm/tpm2-cmd.c > @@ -825,6 +825,50 @@ unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal) > EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration); > > /** > + * tpm2_get_selftest_result() - get the status of self tests > + * There should not be an empty line here. > + * @chip: TPM chip to use > + * > + * Return: If error return rc, else return the result of the self tests. > + * TPM_RC_NEEDS_TESTING: No self tests are done. Needs testing. > + * TPM_RC_TESTING: Self tests are in progress. > + * TPM_RC_SUCCESS: Self tests completed successfully. > + * TPM_RC_FAILURE: Self tests completed failure. > + * > + * This function can be used to check the status of self tests on the TPM. > + */ Description should Better to just have: Return: TPM return code, -errno otherwise There is a lot of variance in return values but this is the format where I would like the code base to dilate to. Describing TPM return codes one by one is not very maintainable in the long run. See: https://www.kernel.org/doc/Documentation/kernel-doc-nano-HOWTO.txt > +static int tpm2_get_selftest_result(struct tpm_chip *chip) > +{ > + struct tpm_buf buf; > + int rc; > + int test_result; > + uint16_t data_size; > + int len; > + const struct tpm_output_header *header; > + > + rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_TEST_RESULT); > + if (rc) > + return rc; > + > + len = tpm_transmit(chip, NULL, buf.data, PAGE_SIZE, 0); > + if (len < 0) > + return len; > + > + header = (struct tpm_output_header *)buf.data; > + > + rc = be32_to_cpu(header->return_code); > + if (rc) > + return rc; > + > + data_size = be16_to_cpup((__be16 *)&buf.data[TPM_HEADER_SIZE]); > + > + test_result = be32_to_cpup((__be32 *) > + (&buf.data[TPM_HEADER_SIZE + 2 + data_size])); > + > + return test_result; > +} > + > +/** > * tpm2_do_selftest() - ensure that all self tests have passed > * > * @chip: TPM chip to use > @@ -853,6 +897,10 @@ static int tpm2_do_selftest(struct tpm_chip *chip) > "attempting the self test"); > tpm_buf_destroy(&buf); > > + /* Check the selftest status */ What is the purpose of this comment eg what does it describe that isn't obvious? > + if (rc == TPM2_RC_COMMAND_CODE) > + rc = tpm2_get_selftest_result(chip); > + > if (rc == TPM2_RC_TESTING) > rc = TPM2_RC_SUCCESS; > if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS) > -- > 2.13.6 >