From patchwork Wed Nov 22 09:26:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Martinez Canillas X-Patchwork-Id: 10069969 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 A91E260595 for ; Wed, 22 Nov 2017 09:26:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A0C6829C20 for ; Wed, 22 Nov 2017 09:26:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9550929C22; Wed, 22 Nov 2017 09:26:48 +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 7649029C20 for ; Wed, 22 Nov 2017 09:26:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751280AbdKVJ03 (ORCPT ); Wed, 22 Nov 2017 04:26:29 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:39200 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751273AbdKVJ01 (ORCPT ); Wed, 22 Nov 2017 04:26:27 -0500 Received: by mail-wr0-f195.google.com with SMTP id 11so10530207wrb.6 for ; Wed, 22 Nov 2017 01:26:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=89lZalBgQaatXnYLiGr/Pt0SpXP7HV2as6743CONn5A=; b=pPZTBtVe8pObval29f1yDxrhoXh2YewgJ6vm8dAnKArj+gjVW56WN2zwnSjchXkUag GO8cpZC4jtVhjDEdy6HYVEl5xGV3QA6pF3yY3EjaODJl7lr6M1z2sp+73RRYoB7bcUBK o+oq7vzNh40++mApUwJ/l2jzuIQFrU2PvF7qfhwpjBfj2YqgehrkZI1Ui/CDJwujifyZ xPntJBo+50KugNQm05BviYAhWD1zovHLztBuZpgmcNqtfAciaBtq7Oz6p0u/K7pSixpn /DoGHfFkdMjs52E9+93fkF1+yyWEt766Y3uPBqoN23/6x62exQLp1dJ2rVClgsYLOKK5 VSEg== X-Gm-Message-State: AJaThX5H+N5kBdm36/C24fNgW/unLs43vz518JtaaEyNdEYlKjop+1Jy pd3ztNvVeO7zZlx3GoTtIfl1DM81DRY= X-Google-Smtp-Source: AGs4zMY+oQxcngE4DSUVLcP6u23Jl2SXR2+ktWaDiN+5q9kLN1Lev1gKdYoNEBBiG1j9YT3BSOk3eg== X-Received: by 10.223.142.49 with SMTP id n46mr16520264wrb.279.1511342785596; Wed, 22 Nov 2017 01:26:25 -0800 (PST) Received: from [10.201.33.27] ([149.6.131.75]) by smtp.gmail.com with ESMTPSA id 128sm3945627wmi.28.2017.11.22.01.26.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Nov 2017 01:26:25 -0800 (PST) Subject: Re: [RFC PATCH] tpm: don't return -EINVAL if TPM command validation fails To: "Roberts, William C" , Jarkko Sakkinen Cc: "linux-kernel@vger.kernel.org" , Peter Huewe , "Tricca, Philip B" , Jason Gunthorpe , "linux-integrity@vger.kernel.org" References: <20171117100724.19257-1-javierm@redhat.com> <20171120231512.6wpqgcggfta3am7m@linux.intel.com> <7c148cf0-2403-55cf-1633-ff326d5c6f7b@redhat.com> <20171121123006.esr7yxs5lvorlfjf@linux.intel.com> <476DC76E7D1DF2438D32BFADF679FC563F4BFC0B@ORSMSX115.amr.corp.intel.com> From: Javier Martinez Canillas Message-ID: <600d6778-f155-ea51-64dc-e0041c6943d3@redhat.com> Date: Wed, 22 Nov 2017 10:26:24 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <476DC76E7D1DF2438D32BFADF679FC563F4BFC0B@ORSMSX115.amr.corp.intel.com> Content-Language: en-US Sender: linux-integrity-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On 11/21/2017 09:29 PM, Roberts, William C wrote: [snip] >>> >>> Do you agree with Jason's suggestion to send a synthesized TPM command >>> in the that the command isn't supported? >> >> Nope. > > We should update the elf loader to make sure that ELF files don't contain > Incorrect instructions. We shouldn't have this type of policy in the driver > considering that the tpm is designed to handle it. Obviously you disagree, > just understand you're wrong :-P > I think the sandbox is correct and makes sense to only send well constructed commands to the TPM. So my RFC patch breaking the sandbox is clearly wrong. I still do believe that both interfaces (/dev/tpm and /dev/tpmrm) should be consistent if possible though. In other words, I don't see the value of not behaving as expected by the spec if this doesn't have security implications as is the case with the approach suggested by Jason. And the implementation for sending the synthesized response is also trivial. The other option that's fixing this in user-space will be a workaround, since it would either be to check for TPM_RC_SUCCESS instead of TPM_RC_COMMAND_CODE or make the SAPI library infer that a -EINVAL error means that a command isn't supported and return a TPM_RC_COMMAND_CODE to the caller. For completeness, I'll share my patch implementing what Jason suggested, even when is quite likely that Jarkko won't like it since he has a strong opinion on this: From 145b6891a68b32ae803a4b0abd3d35679ed6b2a1 Mon Sep 17 00:00:00 2001 From: Javier Martinez Canillas Date: Tue, 21 Nov 2017 12:32:15 +0100 Subject: [RFCv2 PATCH] tpm: return a TPM_RC_COMMAND_CODE response if command isn't implemented According to the TPM Library Specification, a TPM device must do a command header validation before processing and return a TPM_RC_COMMAND_CODE code if the command is not implemented. So user-space will expect to handle the response cods as error. But if the in-kernel resource manager is used (/dev/tpmrm?), an -EINVAL errno code is returned instead if the command isn't implemented. This confuses userspace since it doesn't expect that error value. This also isn't consistent with the behavior when not using TPM spaces and accessing the TPM directly (/dev/tpm?). In this case, the command is sent to the TPM even when implemented and userspace gets an error from the TPM. Instead of returning an -EINVAL errno code when the tpm_validate_command() function fails, synthesize a TPM command response so userspace can get a TPM_RC_COMMAND_CODE as expected when a chip doesn't implement the command. Suggested-by: Jason Gunthorpe Signed-off-by: Javier Martinez Canillas --- Changes since RFCv1: - Don't send not validated commands to the TPM, instead return a synthesized response with the correct TPM return code (suggested by Jason Gunthorpe). drivers/char/tpm/tpm-interface.c | 28 ++++++++++++++++++++-------- drivers/char/tpm/tpm.h | 1 + 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c index ebe0a1d36d8c..b8d01897c0ba 100644 --- a/drivers/char/tpm/tpm-interface.c +++ b/drivers/char/tpm/tpm-interface.c @@ -328,7 +328,7 @@ unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, } EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); -static bool tpm_validate_command(struct tpm_chip *chip, +static int tpm_validate_command(struct tpm_chip *chip, struct tpm_space *space, const u8 *cmd, size_t len) @@ -340,10 +340,10 @@ static bool tpm_validate_command(struct tpm_chip *chip, unsigned int nr_handles; if (len < TPM_HEADER_SIZE) - return false; + return -EINVAL; if (!space) - return true; + return 0; if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) { cc = be32_to_cpu(header->ordinal); @@ -352,7 +352,7 @@ static bool tpm_validate_command(struct tpm_chip *chip, if (i < 0) { dev_dbg(&chip->dev, "0x%04X is an invalid command\n", cc); - return false; + return -EOPNOTSUPP; } attrs = chip->cc_attrs_tbl[i]; @@ -362,11 +362,11 @@ static bool tpm_validate_command(struct tpm_chip *chip, goto err_len; } - return true; + return 0; err_len: dev_dbg(&chip->dev, "%s: insufficient command length %zu", __func__, len); - return false; + return -EINVAL; } /** @@ -391,8 +391,20 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, unsigned long stop; bool need_locality; - if (!tpm_validate_command(chip, space, buf, bufsiz)) - return -EINVAL; + rc = tpm_validate_command(chip, space, buf, bufsiz); + if (rc == -EINVAL) + return rc; + /* + * If the command is not implemented by the TPM, synthesize a + * response with a TPM2_RC_COMMAND_CODE return for user-space. + */ + if (rc == -EOPNOTSUPP) { + header->length = cpu_to_be32(sizeof(*header)); + header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS); + header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE); + + return bufsiz; + } if (bufsiz > TPM_BUFSIZE) bufsiz = TPM_BUFSIZE; diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h index c1866cc02e30..40818fa59b05 100644 --- a/drivers/char/tpm/tpm.h +++ b/drivers/char/tpm/tpm.h @@ -100,6 +100,7 @@ enum tpm2_return_codes { TPM2_RC_HANDLE = 0x008B, TPM2_RC_INITIALIZE = 0x0100, /* RC_VER1 */ TPM2_RC_DISABLED = 0x0120, + TPM2_RC_COMMAND_CODE = 0x0143, TPM2_RC_TESTING = 0x090A, /* RC_WARN */ TPM2_RC_REFERENCE_H0 = 0x0910, };