From patchwork Wed May 10 23:54:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 9720783 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7396360231 for ; Wed, 10 May 2017 23:54:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8147827F7F for ; Wed, 10 May 2017 23:54:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 75E2A28637; Wed, 10 May 2017 23:54:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 08C1D27F7F for ; Wed, 10 May 2017 23:54:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751765AbdEJXyy (ORCPT ); Wed, 10 May 2017 19:54:54 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:50863 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754037AbdEJXyc (ORCPT ); Wed, 10 May 2017 19:54:32 -0400 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v4ANn1lA176227 for ; Wed, 10 May 2017 19:54:30 -0400 Received: from e34.co.us.ibm.com (e34.co.us.ibm.com [32.97.110.152]) by mx0b-001b2d01.pphosted.com with ESMTP id 2abp5ts0g3-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 10 May 2017 19:54:30 -0400 Received: from localhost by e34.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 10 May 2017 17:54:30 -0600 Received: from b03cxnp08025.gho.boulder.ibm.com (9.17.130.17) by e34.co.us.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 10 May 2017 17:54:27 -0600 Received: from b03ledav001.gho.boulder.ibm.com (b03ledav001.gho.boulder.ibm.com [9.17.130.232]) by b03cxnp08025.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v4ANsRsX9634086; Wed, 10 May 2017 16:54:27 -0700 Received: from b03ledav001.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 35ECC6E038; Wed, 10 May 2017 17:54:27 -0600 (MDT) Received: from sbct-3.watson.ibm.com (unknown [9.2.141.158]) by b03ledav001.gho.boulder.ibm.com (Postfix) with ESMTP id A498D6E035; Wed, 10 May 2017 17:54:26 -0600 (MDT) From: Stefan Berger To: tpmdd-devel@lists.sourceforge.net, linux-security-module@vger.kernel.org, jarkko.sakkinen@linux.intel.com Cc: jgunthorpe@obsidianresearch.com, linux-kernel@vger.kernel.org, Stefan Berger Subject: [PATCH v4 1/2] tpm: Refactor tpm_transmit pulling out tpm_transfer function Date: Wed, 10 May 2017 19:54:21 -0400 X-Mailer: git-send-email 2.5.5 In-Reply-To: <1494460462-29022-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1494460462-29022-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17051023-0016-0000-0000-000006B75D1E X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007044; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000210; SDB=6.00858879; UDB=6.00425616; IPR=6.00638355; BA=6.00005343; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00015403; XFM=3.00000015; UTC=2017-05-10 23:54:29 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17051023-0017-0000-0000-0000399618D4 Message-Id: <1494460462-29022-2-git-send-email-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-05-10_20:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1705100161 Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Refactor tpm_transmit and pull out code sending the command and receiving the response and put this into tpm_transfer. Signed-off-by: Stefan Berger --- drivers/char/tpm/tpm-interface.c | 121 +++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 48 deletions(-) diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c index 158c1db..263b6d1 100644 --- a/drivers/char/tpm/tpm-interface.c +++ b/drivers/char/tpm/tpm-interface.c @@ -370,67 +370,29 @@ static bool tpm_validate_command(struct tpm_chip *chip, } /** - * tmp_transmit - Internal kernel interface to transmit TPM commands. + * tmp_transfer - Send a TPM command to the TPM and receive response * * @chip: TPM chip to use * @buf: TPM command buffer + * @count: size of the TPM command * @bufsiz: length of the TPM command buffer - * @flags: tpm transmit flags - bitmap * * Return: - * 0 when the operation is successful. + * >0 when the operation is successful; returns response length * A negative number for system errors (errno). */ -ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, - u8 *buf, size_t bufsiz, unsigned int flags) +ssize_t tpm_transfer(struct tpm_chip *chip, u8 *buf, u32 count, size_t bufsiz) { - struct tpm_output_header *header = (void *)buf; int rc; + struct tpm_output_header *header = (void *)buf; + u32 ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); ssize_t len = 0; - u32 count, ordinal; unsigned long stop; - bool need_locality; - - if (!tpm_validate_command(chip, space, buf, bufsiz)) - return -EINVAL; - - if (bufsiz > TPM_BUFSIZE) - bufsiz = TPM_BUFSIZE; - - count = be32_to_cpu(*((__be32 *) (buf + 2))); - ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); - if (count == 0) - return -ENODATA; - if (count > bufsiz) { - dev_err(&chip->dev, - "invalid count value %x %zx\n", count, bufsiz); - return -E2BIG; - } - - if (!(flags & TPM_TRANSMIT_UNLOCKED)) - mutex_lock(&chip->tpm_mutex); - - if (chip->dev.parent) - pm_runtime_get_sync(chip->dev.parent); - - /* Store the decision as chip->locality will be changed. */ - need_locality = chip->locality == -1; - - if (need_locality && chip->ops->request_locality) { - rc = chip->ops->request_locality(chip, 0); - if (rc < 0) - goto out_no_locality; - chip->locality = rc; - } - - rc = tpm2_prepare_space(chip, space, ordinal, buf); - if (rc) - goto out; rc = chip->ops->send(chip, (u8 *) buf, count); if (rc < 0) { dev_err(&chip->dev, - "tpm_transmit: tpm_send: error %d\n", rc); + "tpm_transfer: tpm_send: error %d\n", rc); goto out; } @@ -467,18 +429,81 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, if (len < 0) { rc = len; dev_err(&chip->dev, - "tpm_transmit: tpm_recv: error %d\n", rc); + "tpm_transfer: tpm_recv: error %d\n", rc); goto out; } else if (len < TPM_HEADER_SIZE) { rc = -EFAULT; goto out; } - if (len != be32_to_cpu(header->length)) { + if (len != be32_to_cpu(header->length)) rc = -EFAULT; - goto out; + +out: + return rc ? rc : len; +} +EXPORT_SYMBOL_GPL(tpm_transfer); + +/** + * tmp_transmit - Internal kernel interface to transmit TPM commands. + * + * @chip: TPM chip to use + * @buf: TPM command buffer + * @bufsiz: length of the TPM command buffer + * @flags: tpm transmit flags - bitmap + * + * Return: + * 0 when the operation is successful. + * A negative number for system errors (errno). + */ +ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, + u8 *buf, size_t bufsiz, unsigned int flags) +{ + int rc; + ssize_t len = 0; + u32 count, ordinal; + bool need_locality; + + if (!tpm_validate_command(chip, space, buf, bufsiz)) + return -EINVAL; + + if (bufsiz > TPM_BUFSIZE) + bufsiz = TPM_BUFSIZE; + + count = be32_to_cpu(*((__be32 *) (buf + 2))); + ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); + if (count == 0) + return -ENODATA; + if (count > bufsiz) { + dev_err(&chip->dev, + "invalid count value %x %zx\n", count, bufsiz); + return -E2BIG; + } + + if (!(flags & TPM_TRANSMIT_UNLOCKED)) + mutex_lock(&chip->tpm_mutex); + + if (chip->dev.parent) + pm_runtime_get_sync(chip->dev.parent); + + /* Store the decision as chip->locality will be changed. */ + need_locality = chip->locality == -1; + + if (need_locality && chip->ops->request_locality) { + rc = chip->ops->request_locality(chip, 0); + if (rc < 0) + goto out_no_locality; + chip->locality = rc; } + rc = tpm2_prepare_space(chip, space, ordinal, buf); + if (rc) + goto out; + + len = tpm_transfer(chip, buf, count, bufsiz); + if (len < 0) + goto out; + rc = tpm2_commit_space(chip, space, ordinal, buf, &len); out: