Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp224463imm; Tue, 15 May 2018 00:20:01 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoLTcYPqOSzqFao4NnxN84bCZZ7RlnDKXtcJQR7a79eMo7sxt8p0ogS5eojFEWhTL2ZEvr3 X-Received: by 2002:a65:4102:: with SMTP id w2-v6mr11129392pgp.31.1526368800991; Tue, 15 May 2018 00:20:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526368800; cv=none; d=google.com; s=arc-20160816; b=A9I5sx5WXmX2NrBMIfqnoiCcM6fUphxAzwIFtL+Qb7XnW4SthvGGc6P26+hfmOSgBA XSBXtYSsOTq4NGXEpscvy+GTOF2fB+iNZHOOe0b/N1I+k+8oLcUW2Ke+u0N+wGy/eUzO RDvlmBUHGw6V21wXe6v2hX58l3fQNmyHGvu0mmnsUwmXJjABEFLXoFm4hb9IAbuYvEIi 7ZtRjQu3EGvy6q4FcKpWIRGKg95mb0x9tS+CdCNzkvw2nrGjIQgQXI4GWp+VD2/rVDVg 27G9n+3tzKhIErDjRanXSgylPAz/s6rGiF5mv78fUJMH5ZY2TFsZi7zpuxBdYAIf/NfB qYvw== 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=2zRI6MFBkGySI3+hH8wGoZ8g14gHMS/6lfYJ09DY3qw=; b=LbJtJ7PtAl8SLlWWD0/Aiv+8hMfs3Asuz+vZyjipy2XkJ5K7Gg4Pii7wAMJ3JzUnCG SZ0dy9hYTloxwQShcJsbJ3gTy/T3OPyVABo/uIxfEymup1tgqfXpwtw4Y+CUBL3BSiwZ F4an5t4hHzSSxRDYseMhjXJWL86NUtF6Fhbj0hFmDMmNh4iLmQgRYyURKsIP2lvgVEiC eVPpOtMDBeXKL+EIFQ11KhuP7vL13vT3KM5wTeY1y+uRB2h/5/CrTYIy4i2aVQeYeyU+ QqQ0m7jGSh4i1b2ALKlTx3lNnwqI3nS82IBtubdSnTn/PomeoENPnMLN52l3dcSmkqCM V/Tw== 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 h188-v6si8838802pgc.53.2018.05.15.00.19.46; Tue, 15 May 2018 00:20:00 -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 S1752231AbeEOHTh (ORCPT + 99 others); Tue, 15 May 2018 03:19:37 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:54832 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752135AbeEOHTg (ORCPT ); Tue, 15 May 2018 03:19:36 -0400 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w4F7JZlT045415 for ; Tue, 15 May 2018 03:19:35 -0400 Received: from e06smtp10.uk.ibm.com (e06smtp10.uk.ibm.com [195.75.94.106]) by mx0b-001b2d01.pphosted.com with ESMTP id 2hypw5gqqu-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 15 May 2018 03:19:18 -0400 Received: from localhost by e06smtp10.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 15 May 2018 08:19:17 +0100 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp10.uk.ibm.com (192.168.101.140) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 15 May 2018 08:19:13 +0100 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w4F7JDx850724874; Tue, 15 May 2018 07:19:13 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D89334C04A; Tue, 15 May 2018 08:11:04 +0100 (BST) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id EA3D14C050; Tue, 15 May 2018 08:11:01 +0100 (BST) Received: from swastik.in.ibm.com (unknown [9.79.186.180]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 15 May 2018 08:11:01 +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 v3] tpm: check selftest status before retrying full selftest Date: Tue, 15 May 2018 12:47:12 +0530 X-Mailer: git-send-email 2.13.6 X-TM-AS-GCONF: 00 x-cbid: 18051507-0040-0000-0000-00000439E165 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18051507-0041-0000-0000-0000263EEF39 Message-Id: <20180515071712.9331-1-nayna@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-05-15_01:,, 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=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1805150077 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org TPM 2.0 supports TPM2_GetTestResult[1], which can be used to check the selftest status. This patch implements the tpm2_get_selftest_result() function to check the selftest status before trying full selftest. [1] As per 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) Tested-by: Stefan Berger (With QEMU with swtpm TPM 2.0) --- Changelog: v3: * modified to check the selftest status before full selftest command is run * fixed the patch description * includes Jarkko's feedbacks v2: * changed the subject and updated patch description * removed the logs drivers/char/tpm/tpm.h | 2 ++ drivers/char/tpm/tpm2-cmd.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 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..811bcf221208 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -825,6 +825,43 @@ 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 selftest + * @chip: TPM chip to use + * + * Return: TPM return code, errno otherwise + */ +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 @@ -857,6 +894,13 @@ static int tpm2_do_selftest(struct tpm_chip *chip) rc = TPM2_RC_SUCCESS; if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS) return rc; + + rc = tpm2_get_selftest_result(chip); + dev_dbg(&chip->dev, "self test result is %04x\n", rc); + + if (rc == TPM2_RC_TESTING || rc == TPM2_RC_SUCCESS) + return TPM2_RC_SUCCESS; + } return rc; -- 2.13.6