Re: [PATCH 1/9] tpm: Add support for in-kernel resetting of PCRs

2021-02-19 Thread Jarkko Sakkinen
On Sat, Feb 20, 2021 at 01:32:47AM +, Matthew Garrett wrote:

Perhaps, prepend with a paragraph elaborating the background just a bit:

"When entering to hibernation, PCR 23 will be used to store a known value.
This value defines a policy for the decryption of the key used to encrypt
the hibernate image."

At minimum that. You can of course edit this however you see fit.

> Add an internal command for resetting a PCR.
> 
> Signed-off-by: Matthew Garrett 
> ---
>  drivers/char/tpm/tpm-interface.c | 28 +
>  drivers/char/tpm/tpm.h   |  2 ++
>  drivers/char/tpm/tpm1-cmd.c  | 34 ++
>  drivers/char/tpm/tpm2-cmd.c  | 36 
>  include/linux/tpm.h  |  7 +++
>  5 files changed, 107 insertions(+)
> 
> diff --git a/drivers/char/tpm/tpm-interface.c 
> b/drivers/char/tpm/tpm-interface.c
> index 1621ce818705..17b8643ee109 100644
> --- a/drivers/char/tpm/tpm-interface.c
> +++ b/drivers/char/tpm/tpm-interface.c
> @@ -342,6 +342,34 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
>  }
>  EXPORT_SYMBOL_GPL(tpm_pcr_extend);
>  
> +/**
> + * tpm_pcr_reset - reset the specified PCR
> + * @chip:a  tpm_chip instance, %NULL for the default chip
> + * @pcr_idx: the PCR to be reset
> + *
> + * Return: same as with tpm_transmit_cmd()
> + */
> +int tpm_pcr_reset(struct tpm_chip *chip, u32 pcr_idx)
> +{
> + int rc;
> +
> + chip = tpm_find_get_ops(chip);
> + if (!chip)
> + return -ENODEV;
> +
> + if (chip->flags & TPM_CHIP_FLAG_TPM2) {
> + rc = tpm2_pcr_reset(chip, pcr_idx);
> + goto out;
> + }
> +
> + rc = tpm1_pcr_reset(chip, pcr_idx, "attempting to reset a PCR");
> +
> +out:
> + tpm_put_ops(chip);
> + return rc;
> +}
> +EXPORT_SYMBOL_GPL(tpm_pcr_reset);
> +
>  /**
>   * tpm_send - send a TPM command
>   * @chip:a  tpm_chip instance, %NULL for the default chip
> diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
> index 947d1db0a5cc..746f7696bdc0 100644
> --- a/drivers/char/tpm/tpm.h
> +++ b/drivers/char/tpm/tpm.h
> @@ -176,6 +176,7 @@ int tpm1_get_timeouts(struct tpm_chip *chip);
>  unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
>  int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
>   const char *log_msg);
> +int tpm1_pcr_reset(struct tpm_chip *chip, u32 pcr_idx, const char *log_msg);
>  int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
>  ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
>   const char *desc, size_t min_cap_length);
> @@ -220,6 +221,7 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
> struct tpm_digest *digest, u16 *digest_size_ptr);
>  int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
>   struct tpm_digest *digests);
> +int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx);
>  int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
>  ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
>   u32 *value, const char *desc);
> diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
> index ca7158fa6e6c..36990e9d2dc1 100644
> --- a/drivers/char/tpm/tpm1-cmd.c
> +++ b/drivers/char/tpm/tpm1-cmd.c
> @@ -478,6 +478,40 @@ int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, 
> const u8 *hash,
>   return rc;
>  }
>  
> +struct tpm_pcr_selection {
> + u16 size_of_select;
> + u8  pcr_select[3];
> +} __packed;
> +
> +#define TPM_ORD_PCR_RESET 200
> +int tpm1_pcr_reset(struct tpm_chip *chip, u32 pcr_idx, const char *log_msg)
> +{
> + struct tpm_pcr_selection selection;
> + struct tpm_buf buf;
> + int i, rc;
> + char tmp;
> +
> + rc = tpm_buf_init(, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_RESET);
> + if (rc)
> + return rc;
> +
> + selection.size_of_select = 3;
> +
> + for (i = 0; i < selection.size_of_select; i++) {
> + tmp = 0;
> + if (pcr_idx / 3 == i) {
> + pcr_idx -= i * 8;
> + tmp |= 1 << pcr_idx;
> + }
> + selection.pcr_select[i] = tmp;
> + }
> + tpm_buf_append(, (u8 *), sizeof(selection));
> +
> + rc = tpm_transmit_cmd(chip, , sizeof(selection), log_msg);
> + tpm_buf_destroy();
> + return rc;
> +}
> +
>  #define TPM_ORD_GET_CAP 101
>  ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
>   const char *desc, size_t min_cap_length)
> diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
> index eff1f12d981a..9609ae8086c6 100644
> --- a/drivers/char/tpm/tpm2-cmd.c
> +++ b/drivers/char/tpm/tpm2-cmd.c
> @@ -269,6 +269,42 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
>   return rc;
>  }
>  
> +/**
> + * tpm2_pcr_reset() - reset a PCR
> + *
> + * @chip:TPM chip to use.
> 

[PATCH 1/9] tpm: Add support for in-kernel resetting of PCRs

2021-02-19 Thread Matthew Garrett
Add an internal command for resetting a PCR.

Signed-off-by: Matthew Garrett 
---
 drivers/char/tpm/tpm-interface.c | 28 +
 drivers/char/tpm/tpm.h   |  2 ++
 drivers/char/tpm/tpm1-cmd.c  | 34 ++
 drivers/char/tpm/tpm2-cmd.c  | 36 
 include/linux/tpm.h  |  7 +++
 5 files changed, 107 insertions(+)

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index 1621ce818705..17b8643ee109 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -342,6 +342,34 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 }
 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
 
+/**
+ * tpm_pcr_reset - reset the specified PCR
+ * @chip:  a  tpm_chip instance, %NULL for the default chip
+ * @pcr_idx:   the PCR to be reset
+ *
+ * Return: same as with tpm_transmit_cmd()
+ */
+int tpm_pcr_reset(struct tpm_chip *chip, u32 pcr_idx)
+{
+   int rc;
+
+   chip = tpm_find_get_ops(chip);
+   if (!chip)
+   return -ENODEV;
+
+   if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+   rc = tpm2_pcr_reset(chip, pcr_idx);
+   goto out;
+   }
+
+   rc = tpm1_pcr_reset(chip, pcr_idx, "attempting to reset a PCR");
+
+out:
+   tpm_put_ops(chip);
+   return rc;
+}
+EXPORT_SYMBOL_GPL(tpm_pcr_reset);
+
 /**
  * tpm_send - send a TPM command
  * @chip:  a  tpm_chip instance, %NULL for the default chip
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 947d1db0a5cc..746f7696bdc0 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -176,6 +176,7 @@ int tpm1_get_timeouts(struct tpm_chip *chip);
 unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
 int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
const char *log_msg);
+int tpm1_pcr_reset(struct tpm_chip *chip, u32 pcr_idx, const char *log_msg);
 int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
const char *desc, size_t min_cap_length);
@@ -220,6 +221,7 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
  struct tpm_digest *digest, u16 *digest_size_ptr);
 int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
struct tpm_digest *digests);
+int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx);
 int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
u32 *value, const char *desc);
diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
index ca7158fa6e6c..36990e9d2dc1 100644
--- a/drivers/char/tpm/tpm1-cmd.c
+++ b/drivers/char/tpm/tpm1-cmd.c
@@ -478,6 +478,40 @@ int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, 
const u8 *hash,
return rc;
 }
 
+struct tpm_pcr_selection {
+   u16 size_of_select;
+   u8  pcr_select[3];
+} __packed;
+
+#define TPM_ORD_PCR_RESET 200
+int tpm1_pcr_reset(struct tpm_chip *chip, u32 pcr_idx, const char *log_msg)
+{
+   struct tpm_pcr_selection selection;
+   struct tpm_buf buf;
+   int i, rc;
+   char tmp;
+
+   rc = tpm_buf_init(, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_RESET);
+   if (rc)
+   return rc;
+
+   selection.size_of_select = 3;
+
+   for (i = 0; i < selection.size_of_select; i++) {
+   tmp = 0;
+   if (pcr_idx / 3 == i) {
+   pcr_idx -= i * 8;
+   tmp |= 1 << pcr_idx;
+   }
+   selection.pcr_select[i] = tmp;
+   }
+   tpm_buf_append(, (u8 *), sizeof(selection));
+
+   rc = tpm_transmit_cmd(chip, , sizeof(selection), log_msg);
+   tpm_buf_destroy();
+   return rc;
+}
+
 #define TPM_ORD_GET_CAP 101
 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
const char *desc, size_t min_cap_length)
diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index eff1f12d981a..9609ae8086c6 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -269,6 +269,42 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
return rc;
 }
 
+/**
+ * tpm2_pcr_reset() - reset a PCR
+ *
+ * @chip:  TPM chip to use.
+ * @pcr_idx:   index of the PCR.
+ *
+ * Return: Same as with tpm_transmit_cmd.
+ */
+int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx)
+{
+   struct tpm_buf buf;
+   struct tpm2_null_auth_area auth_area;
+   int rc;
+
+   rc = tpm_buf_init(, TPM2_ST_SESSIONS, TPM2_CC_PCR_RESET);
+   if (rc)
+   return rc;
+
+   tpm_buf_append_u32(, pcr_idx);
+
+   auth_area.handle = cpu_to_be32(TPM2_RS_PW);
+   auth_area.nonce_size = 0;
+   auth_area.attributes = 0;
+   auth_area.auth_size = 0;
+
+