Received: by 10.192.165.148 with SMTP id m20csp3227321imm; Mon, 7 May 2018 08:43:23 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqBrCDqYnaG6FfvuKf3VDyWS9Fyz768xHluznJ4TU7dEa9V5ho69foZp85iq/u9FbaqJsC7 X-Received: by 2002:a24:a54:: with SMTP id 81-v6mr1859927itw.44.1525707803285; Mon, 07 May 2018 08:43:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525707803; cv=none; d=google.com; s=arc-20160816; b=Lwd/M3yv1ZcTJrMQtDn2cPOSP4JI67yvpvDKI24/oMqp1VWDIzX2Bf+cNuYj4oONnY WneKsXe0lSiGpbdS3Aaxwzlgmy8BOIBSAZWaytmVafnbItMwId14D/V1YPCTknBg+Ddn PlTQvTtmlZhd8TcsT84i4LGRJlnWdrdmRY9Y79hWbkPqIYPo3RWLSvbt9wdjApitxgID hdH6XtMJQyZ/EAwjYaNUxMvNwnOQyiur8zOHLcIPME2VRLPeWkjOi9bIurnTjulaZtaA VgOkF+FWVOeUGgx8W5WPUUgSyOCu0V5mo3jHMA4KwejGiyfu9lgYgP/4mnog+se6hX9W 5cwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=BRSY4OR6kklfOD62NDbOwDzGsf+X4qlWc5c+Pjw1yUA=; b=jd1MlVACj+Cb5WNXhmqyUSVRKk9I+6Ky1WturPGEKcwCYfuS8/iGLPF/uWXcP9gLB3 WwlAr8C9yj/u35qeQWdeTlfxSj/fNztiIMukz4KDCktjrOoPwF5Oao++maXgMToNr1Me M2vEa3NV2fy9ZmJVWcs9ikpdsd6D5BLUhvRQz1a2Tv6sZlmOeG63gQfrWdrLvjNmeZvT 5OO8PNBUxxBt8kZJDMnD/QCX6chhDjgt/j5mJKGpa6bBd5FZdJ2ytFZVVNW8+ud2WGMa r4dFMMqK6lQeU3Geb780cJCw3UiMwa3mVemWkgmB0LL065ZCB4ULr4Cyoral2plUZKkE n3yw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 10-v6si6773375ion.87.2018.05.07.08.43.10; Mon, 07 May 2018 08:43:23 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752595AbeEGPli (ORCPT + 99 others); Mon, 7 May 2018 11:41:38 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:58058 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752174AbeEGPlg (ORCPT ); Mon, 7 May 2018 11:41:36 -0400 Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w47FbEbv120083 for ; Mon, 7 May 2018 11:41:35 -0400 Received: from e06smtp15.uk.ibm.com (e06smtp15.uk.ibm.com [195.75.94.111]) by mx0b-001b2d01.pphosted.com with ESMTP id 2htr6j4c2n-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 07 May 2018 11:41:35 -0400 Received: from localhost by e06smtp15.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 7 May 2018 16:41:33 +0100 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp15.uk.ibm.com (192.168.101.145) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 7 May 2018 16:41:31 +0100 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w47FfUOq7668002; Mon, 7 May 2018 15:41:30 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AA5E352047; Mon, 7 May 2018 15:31:51 +0100 (BST) Received: from swastik.in.ibm.com (unknown [9.124.208.89]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id 52E9152041; Mon, 7 May 2018 15:31:47 +0100 (BST) From: Nayna Jain To: linux-integrity@vger.kernel.org Cc: zohar@linux.vnet.ibm.com, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, peterhuewe@gmx.de, jarkko.sakkinen@linux.intel.com, tpmdd@selhorst.net, jgunthorpe@obsidianresearch.com, Nayna Jain Subject: [PATCH v2] tpm: check selftest status before retrying full selftest Date: Mon, 7 May 2018 21:09:41 +0530 X-Mailer: git-send-email 2.13.6 X-TM-AS-GCONF: 00 x-cbid: 18050715-0020-0000-0000-0000041A0319 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18050715-0021-0000-0000-000042AF3742 Message-Id: <20180507153941.4952-1-nayna@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-05-07_07:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1805070158 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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 + * + * @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. + */ +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 */ + 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