tpm: add buffer function to point to returned parameters
authorJames Bottomley <James.Bottomley@HansenPartnership.com>
Mon, 29 Apr 2024 20:27:59 +0000 (16:27 -0400)
committerJarkko Sakkinen <jarkko@kernel.org>
Thu, 9 May 2024 19:30:51 +0000 (22:30 +0300)
Replace all instances of &buf.data[TPM_HEADER_SIZE] with a new
function tpm_buf_parameters() because encryption sessions change
where the return parameters are located in the buffer since if a
return session is present they're 4 bytes beyond the header with those
4 bytes giving the parameter length.  If there is no return session,
then they're in the usual place immediately after the header.

Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Tested-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
drivers/char/tpm/tpm-buf.c
include/linux/tpm.h

index cc14fa3fd8c2a307b185b02faad9f823cd7ee89c..6e4595cc98bcfa857a6d6207d635e7fab563f059 100644 (file)
@@ -221,3 +221,31 @@ u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset)
        return be32_to_cpu(value);
 }
 EXPORT_SYMBOL_GPL(tpm_buf_read_u32);
+
+static u16 tpm_buf_tag(struct tpm_buf *buf)
+{
+       struct tpm_header *head = (struct tpm_header *)buf->data;
+
+       return be16_to_cpu(head->tag);
+}
+
+/**
+ * tpm_buf_parameters - return the TPM response parameters area of the tpm_buf
+ * @buf: tpm_buf to use
+ *
+ * Where the parameters are located depends on the tag of a TPM
+ * command (it's immediately after the header for TPM_ST_NO_SESSIONS
+ * or 4 bytes after for TPM_ST_SESSIONS). Evaluate this and return a
+ * pointer to the first byte of the parameters area.
+ *
+ * @return: pointer to parameters area
+ */
+u8 *tpm_buf_parameters(struct tpm_buf *buf)
+{
+       int offset = TPM_HEADER_SIZE;
+
+       if (tpm_buf_tag(buf) == TPM2_ST_SESSIONS)
+               offset += 4;
+
+       return &buf->data[offset];
+}
index e8172f81c5625f79b19e82159aec5d74cf72d130..6be263509e819e12ea24613a8a9087537d074372 100644 (file)
@@ -344,6 +344,8 @@ u8 tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset);
 u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset);
 u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset);
 
+u8 *tpm_buf_parameters(struct tpm_buf *buf);
+
 /*
  * Check if TPM device is in the firmware upgrade mode.
  */