tpm: return a TPM_RC_COMMAND_CODE response if command is not implemented
authorJavier Martinez Canillas <javierm@redhat.com>
Thu, 30 Nov 2017 07:39:07 +0000 (08:39 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 12 Apr 2018 10:32:16 +0000 (12:32 +0200)
[ Upstream commit 095531f891e627e408606f2da4008d3d53e6748a ]

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 that response as an 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 not implemented and the TPM responds with an error.

Instead of returning an -EINVAL errno code when the tpm_validate_command()
function fails, synthesize a TPM command response so user-space can get a
TPM_RC_COMMAND_CODE as expected when a chip doesn't implement the command.

The TPM only sets 12 of the 32 bits in the TPM_RC response, so the TSS and
TAB specifications define that higher layers in the stack should use some
of the unused 20 bits to specify from which level of the stack the error
is coming from.

Since the TPM_RC_COMMAND_CODE response code is sent by the kernel resource
manager, set the error level to the TAB/RM layer so user-space is aware of
this.

Suggested-by: Jason Gunthorpe <jgg@ziepe.ca>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: William Roberts <william.c.roberts@intel.com>
Reviewed-by: Philip Tricca <philip.b.tricca@intel.com>
Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Sasha Levin <alexander.levin@microsoft.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/char/tpm/tpm-interface.c
drivers/char/tpm/tpm.h

index 5294442505cb5c7c2dcb34303596fd9a8236618c..0f1dc35e7078c2d4a3f7acffb207563a34f7f443 100644 (file)
@@ -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 |
+                                                 TSS2_RESMGR_TPM_RC_LAYER);
+               return bufsiz;
+       }
 
        if (bufsiz > TPM_BUFSIZE)
                bufsiz = TPM_BUFSIZE;
index 2d5466a72e40f82b3272b857b74a1f822f82b966..0b5b499f726ad2551c359211010043e8e3a46667 100644 (file)
@@ -93,12 +93,17 @@ enum tpm2_structures {
        TPM2_ST_SESSIONS        = 0x8002,
 };
 
+/* Indicates from what layer of the software stack the error comes from */
+#define TSS2_RC_LAYER_SHIFT     16
+#define TSS2_RESMGR_TPM_RC_LAYER (11 << TSS2_RC_LAYER_SHIFT)
+
 enum tpm2_return_codes {
        TPM2_RC_SUCCESS         = 0x0000,
        TPM2_RC_HASH            = 0x0083, /* RC_FMT1 */
        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,
 };