Skip to contentSkip to conversation

Encrypted Hibernation

PATCH In review Evan Green 8 participants 61 messages Nov 11, 2022 – Feb 7, 2023

Subsystems (8)
lore
Share & tools
lore

Evan Green

author
#0 lore raw

v5 cover Encrypted Hibernation

We are exploring enabling hibernation in some new scenarios. However, our security team has a few requirements, listed below:
1. The hibernate image must be encrypted with protection derived from both the platform (eg TPM) and user authentication data (eg password).
2. Hibernation must not be a vector by which a malicious userspace can escalate to the kernel.

Requirement #1 can be achieved solely with uswsusp, however requirement 2 necessitates mechanisms in the kernel to guarantee integrity of the hibernate image. The kernel needs a way to authenticate that it generated the hibernate image being loaded, and that the image has not been tampered with. Adding support for in-kernel AEAD encryption with a TPM-sealed key allows us to achieve both requirements with a single computation pass.

Matthew Garrett published a series [1] that aligns closely with this goal. His series utilized the fact that PCR23 is a resettable PCR that can be blocked from access by usermode. The TPM can create a sealed key tied to PCR23 in two ways. First, the TPM can attest to the value of PCR23 when the key was created, which the kernel can use on resume to verify that the kernel must have created the key (since it is the only one capable of modifying PCR23). It can also create a policy that enforces PCR23 be set to a specific value as a condition of unsealing the key, preventing usermode from unsealing the key by talking directly to the TPM.

This series adopts that primitive as a foundation, tweaking and building on it a bit. Where Matthew's series used the TPM-backed key to encrypt a hash of the image, this series uses the key directly as a gcm(aes) encryption key, which the kernel uses to encrypt and decrypt the hibernate image in chunks of 16 pages. This provides both encryption and integrity, which turns out to be a noticeable performance improvement over separate passes for encryption and hashing.

The series also introduces the concept of mixing user key material into the encryption key. This allows usermode to introduce key material based on unspecified external authentication data (in our case derived from something like the user password or PIN), without requiring usermode to do a separate encryption pass.

Matthew also documented issues his series had [2] related to generating fake images by booting alternate kernels without the PCR23 limiting. With access to PCR23 on the same machine, usermode can create fake hibernate images that are indistinguishable to the new kernel from genuine ones. His post outlines a solution that involves adding more PCRs into the creation data and policy, with some gyrations to make this work well on a standard PC.

Our approach would be similar: on our machines PCR 0 indicates whether the system is booted in secure/verified mode or developer mode. By adding PCR0 to the policy, we can reject hibernate images made in developer mode while in verified mode (or vice versa).

Additionally, mixing in the user authentication data limits both data exfiltration attacks (eg a stolen laptop) and forged hibernation image attacks to attackers that already know the authentication data (eg user's password). This, combined with our relatively sealed userspace (dm-verity on the rootfs), and some judicious clearing of the hibernate image (such as across an OS update) further reduce the risk of an online attack. The remaining attack space of a forgery from someone with physical access to the device and knowledge of the authentication data is out of scope for us, given that flipping to developer mode or reflashing RO firmware trivially achieves the same thing.

A couple of patches still need to be written on top of this series. The generalized functionality to OR in additional PCRs via Kconfig (like PCR 0 or 5) still needs to be added. We'll also need a patch that disallows unencrypted forms of resume from hibernation, to fully close the door to malicious userspace. However, I wanted to get this series out first and get reactions from upstream before continuing to add to it.

[1] https://patchwork.kernel.org/project/linux-pm/cover/[email protected]/
[2] https://mjg59.dreamwidth.org/58077.html

Changes in v5:
- Change to co-developed by Matthew (Kees)
- Change tags on RESTRICT_PCR patch (Kees)
- Rename to TCG_TPM2_RESTRICT_PCR
- Do nothing on TPM1.2 devices (Jarkko, Doug)
- Factored some math out to a helper function (Kees)
- Constified src in tpm2_key_encode().
- Make Matthew's tag match author
- Removed default n in Kconfig (Kees)
- Expanded commit message (Jarkko)
- Use Suggested-by tag instead of made up Sourced-from (Kees)
- ENCRYPTED_HIBERNATION should depend on TCG_TPM2_RESTRCT_PCR
- Remove pad struct member (Kees)
- Use a struct to access creation data (Kees)
- Build PCR bitmask programmatically in creation data (Kees)

Changes in v4:
- Open code tpm2_pcr_reset implementation in tpm-interface.c (Jarkko)
- Rename interface symbol to tpm2_pcr_reset, fix kerneldocs (Jarkko)
- Augment the commit message (Jarkko)
- Local ordering and whitespace changes (Jarkko)
- s/tpm_pcr_reset/tpm2_pcr_reset/ due to change in other patch
- Variable ordering and whitespace fixes (Jarkko)
- Add NULL check explanation in teardown (Jarkko)
- Change strlen+1 to sizeof for static buffer (Jarkko)
- Fix nr_allocated_banks loop overflow (found via KASAN)
- Local variable reordering (Jarkko)
- Local variable ordering (Jarkko)

Changes in v3:
- Unify tpm1/2_pcr_reset prototypes (Jarkko)
- Wait no, remove the TPM1 stuff altogether (Jarkko)
- Remove extra From tag and blank in commit msg (Jarkko).
- Split find_and_validate_cc() export to its own patch (Jarkko)
- Rename tpm_find_and_validate_cc() to tpm2_find_and_validate_cc().
- Fix up commit message (Jarkko)
- tpm2_find_and_validate_cc() was split (Jarkko)
- Simply fully restrict TPM1 since v2 failed to account for tunnelled transport sessions (Stefan and Jarkko).
- Fix SoB and -- note ordering (Kees)
- Add comments describing the TPM2 spec type names for the new fields in tpm2key.asn1 (Kees)
- Add len buffer checks in tpm2_key_encode() (Kees)
- Clarified creationpcrs documentation (Ben)
- Changed funky tag to suggested-by (Kees). Matthew, holler if you want something different.
- ENCRYPTED_HIBERNATION needs TRUSTED_KEYS builtin for key_type_trusted.
- Remove KEYS dependency since it's covered by TRUSTED_KEYS (Kees)
- Changed funky tag to Co-developed-by (Kees). Matthew, holler if you want something different.
- Changed funky tag to Co-developed-by (Kees)

Changes in v2:
- Fixed sparse warnings
- Adjust hash len by 2 due to new ASN.1 storage, and add underflow check.
- Rework load/create_kernel_key() to eliminate a label (Andrey)
- Call put_device() needed from calling tpm_default_chip().
- Add missing static on snapshot_encrypted_byte_count()
- Fold in only the used kernel key bytes to the user key.
- Make the user key length 32 (Eric)
- Use CRYPTO_LIB_SHA256 for less boilerplate (Eric)
- Fixed some sparse warnings
- Use CRYPTO_LIB_SHA256 to get rid of sha256_data() (Eric)
- Adjusted offsets due to new ASN.1 format, and added a creation data length check.
- Fix sparse warnings
- Fix session type comment (Andrey)
- Eliminate extra label in get/create_kernel_key() (Andrey)
- Call tpm_try_get_ops() before calling tpm2_flush_context().

Evan Green (10):
tpm: Add support for in-kernel resetting of PCRs
tpm: Export and rename tpm2_find_and_validate_cc()
tpm: Allow PCR 23 to be restricted to kernel-only use
security: keys: trusted: Include TPM2 creation data
security: keys: trusted: Verify creation data
PM: hibernate: Add kernel-based encryption
PM: hibernate: Use TPM-backed keys to encrypt image
PM: hibernate: Mix user key in encrypted hibernate
PM: hibernate: Verify the digest encryption key
PM: hibernate: seal the encryption key with a PCR policy

Matthew Garrett (1):
security: keys: trusted: Allow storage of PCR values in creation data

 Documentation/power/userland-swsusp.rst       |    8 +
 .../security/keys/trusted-encrypted.rst       |    6 +
 drivers/char/tpm/Kconfig                      |   12 +
 drivers/char/tpm/tpm-dev-common.c             |    6 +
 drivers/char/tpm/tpm-interface.c              |   47 +
 drivers/char/tpm/tpm.h                        |   15 +
 drivers/char/tpm/tpm2-cmd.c                   |   29 +-
 drivers/char/tpm/tpm2-space.c                 |    8 +-
 include/keys/trusted-type.h                   |    9 +
 include/linux/tpm.h                           |   19 +
 include/uapi/linux/suspend_ioctls.h           |   28 +-
 kernel/power/Kconfig                          |   15 +
 kernel/power/Makefile                         |    1 +
 kernel/power/power.h                          |    1 +
 kernel/power/snapenc.c                        | 1105 +++++++++++++++++
 kernel/power/snapshot.c                       |    5 +
 kernel/power/user.c                           |   44 +-
 kernel/power/user.h                           |  117 ++
 security/keys/trusted-keys/tpm2key.asn1       |   15 +-
 security/keys/trusted-keys/trusted_tpm1.c     |    9 +
 security/keys/trusted-keys/trusted_tpm2.c     |  355 +++++-
 21 files changed, 1797 insertions(+), 57 deletions(-)
 create mode 100644 kernel/power/snapenc.c
 create mode 100644 kernel/power/user.h
Signature · 1 line
2.38.1.431.g37b22c650d-goog
author
#1 lore raw

PATCH v5 1/11 tpm: Add support for in-kernel resetting of PCRs

Add an internal command for resetting a PCR. This will be used by the encrypted hibernation code to set PCR23 to a known value. The hibernation code will seal the hibernation key with a policy specifying PCR23 be set to this known value as a mechanism to ensure that the hibernation key is genuine. But to do this repeatedly, resetting the PCR is necessary as well.

---

Changes in v5:
- Change to co-developed by Matthew (Kees)

Changes in v4:
- Open code tpm2_pcr_reset implementation in tpm-interface.c (Jarkko)
- Rename interface symbol to tpm2_pcr_reset, fix kerneldocs (Jarkko)

Changes in v3:
- Unify tpm1/2_pcr_reset prototypes (Jarkko)
- Wait no, remove the TPM1 stuff altogether (Jarkko)
- Remove extra From tag and blank in commit msg (Jarkko).

 drivers/char/tpm/tpm-interface.c | 47 ++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm2-cmd.c      |  7 -----
 include/linux/tpm.h              | 14 ++++++++++
 3 files changed, 61 insertions(+), 7 deletions(-)
drivers/char/tpm/tpm-interface.c +47 -0
index 1621ce8187052c..886277b2654e3b 100644
@@ -342,6 +342,53 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 }
 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
 
+/**
+ * tpm2_pcr_reset - Reset the specified PCR
+ * @chip: A &struct tpm_chip instance, %NULL for the default chip
+ * @pcr_idx: The PCR to be reset
+ *
+ * Return: Same as with tpm_transmit_cmd(), or ENOTTY for TPM1 devices.
+ */
+int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx)
+{
+	struct tpm2_null_auth_area auth_area;
+	struct tpm_buf buf;
+	int rc;
+
+	chip = tpm_find_get_ops(chip);
+	if (!chip)
+		return -ENODEV;
+
+	if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
+		rc = -ENOTTY;
+		goto out;
+	}
+
+	rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_RESET);
+	if (rc)
+		goto out;
+
+	tpm_buf_append_u32(&buf, 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;
+
+	tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));
+	tpm_buf_append(&buf, (const unsigned char *)&auth_area,
+		       sizeof(auth_area));
+
+	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to reset a PCR");
+
+	tpm_buf_destroy(&buf);
+
+out:
+	tpm_put_ops(chip);
+	return rc;
+}
+EXPORT_SYMBOL_GPL(tpm2_pcr_reset);
+
 /**
  * tpm_send - send a TPM command
  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
drivers/char/tpm/tpm2-cmd.c +0 -7
index 65d03867e114c5..303ce2ea02a4b0 100644
@@ -216,13 +216,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
 	return rc;
 }
 
-struct tpm2_null_auth_area {
-	__be32  handle;
-	__be16  nonce_size;
-	u8  attributes;
-	__be16  auth_size;
-} __packed;
-
 /**
  * tpm2_pcr_extend() - extend a PCR value
  *
include/linux/tpm.h +14 -0
index dfeb25a0362dee..70134e6551745f 100644
@@ -219,6 +219,7 @@ enum tpm2_command_codes {
 	TPM2_CC_HIERARCHY_CONTROL       = 0x0121,
 	TPM2_CC_HIERARCHY_CHANGE_AUTH   = 0x0129,
 	TPM2_CC_CREATE_PRIMARY          = 0x0131,
+	TPM2_CC_PCR_RESET		= 0x013D,
 	TPM2_CC_SEQUENCE_COMPLETE       = 0x013E,
 	TPM2_CC_SELF_TEST	        = 0x0143,
 	TPM2_CC_STARTUP		        = 0x0144,
@@ -293,6 +294,13 @@ struct tpm_header {
 	};
 } __packed;
 
+struct tpm2_null_auth_area {
+	__be32  handle;
+	__be16  nonce_size;
+	u8  attributes;
+	__be16  auth_size;
+} __packed;
+
 /* A string buffer type for constructing TPM commands. This is based on the
  * ideas of string buffer code in security/keys/trusted.h but is heap based
  * in order to keep the stack usage minimal.
@@ -423,6 +431,7 @@ extern ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
 				size_t min_rsp_body_length, const char *desc);
 extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
 			struct tpm_digest *digest);
+extern int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx);
 extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 			  struct tpm_digest *digests);
 extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
@@ -440,6 +449,11 @@ static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
 	return -ENODEV;
 }
 
+static inline int tpm2_pcr_reset(struct tpm_chip *chip, int pcr_idx)
+{
+	return -ENODEV;
+}
+
 static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 				 struct tpm_digest *digests)
 {
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#2 lore raw

Re: [PATCH v5 01/11] tpm: Add support for in-kernel resetting of PCRs

drivers/char/tpm/tpm-interface.c int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, · line 346

Quoted 10 lines · Evan Green
drivers/char/tpm/tpm-interface.c +2 -0
index 1621ce8187052c..886277b2654e3b 100644
@@ -342,6 +342,53 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 }
 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
 
+/**
+ * tpm2_pcr_reset - Reset the specified PCR

Should this function be in drivers/char/tpm/tpm2-cmd.c instead of here?

drivers/char/tpm/tpm-interface.c int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, · line 352

Quoted 6 lines
drivers/char/tpm/tpm-interface.c +6 -0
+ * @chip: A &struct tpm_chip instance, %NULL for the default chip
+ * @pcr_idx: The PCR to be reset
+ *
+ * Return: Same as with tpm_transmit_cmd(), or ENOTTY for TPM1 devices.
+ */
+int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx)

The callers of this function assume it returns a negative errno value. But actually it can return positive TPM2_RC_* error codes as well. Probably you should make it only return negative errno values.

Quoted 16 lines
drivers/char/tpm/tpm-interface.c +16 -0
+{
+	struct tpm2_null_auth_area auth_area;
+	struct tpm_buf buf;
+	int rc;
+
+	chip = tpm_find_get_ops(chip);
+	if (!chip)
+		return -ENODEV;
+
+	if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
+		rc = -ENOTTY;
+		goto out;
+	}
+
+	rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_RESET);
+	if (rc)

drivers/char/tpm/tpm-interface.c int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, · line 378

Quoted 10 lines
drivers/char/tpm/tpm-interface.c +10 -0
+		goto out;
+
+	tpm_buf_append_u32(&buf, 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;
+
+	tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));

sizeof(struct tpm2_null_auth_area) => sizeof(auth_area)

drivers/char/tpm/tpm-interface.c int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, · line 379

Quoted 1 line
drivers/char/tpm/tpm-interface.c +1 -0
+	tpm_buf_append(&buf, (const unsigned char *)&auth_area,

unsigned char => u8

Also, since the code to append a "null" authorization area appears in both tpm2_pcr_reset() and tpm2_pcr_extend(), perhaps it should be refactored into a helper function?

Quoted 13 lines
include/linux/tpm.h +1 -0
index dfeb25a0362dee..70134e6551745f 100644
@@ -219,6 +219,7 @@ enum tpm2_command_codes {
 	TPM2_CC_HIERARCHY_CONTROL       = 0x0121,
 	TPM2_CC_HIERARCHY_CHANGE_AUTH   = 0x0129,
 	TPM2_CC_CREATE_PRIMARY          = 0x0131,
+	TPM2_CC_PCR_RESET		= 0x013D,
 	TPM2_CC_SEQUENCE_COMPLETE       = 0x013E,
 	TPM2_CC_SELF_TEST	        = 0x0143,
 	TPM2_CC_STARTUP		        = 0x0144,

include/linux/tpm.h struct tpm_header { · line 302

Quoted 9 lines
include/linux/tpm.h +6 -0
@@ -293,6 +294,13 @@ struct tpm_header {
 	};
 } __packed;
 
+struct tpm2_null_auth_area {
+	__be32  handle;
+	__be16  nonce_size;
+	u8  attributes;
+	__be16  auth_size;
+} __packed;

struct tpm2_null_auth_area is only used by code in drivers/char/tpm/, so should its declaration go in the internal header drivers/char/tpm/tpm.h instead?

- Eric

maintainer Reviewed-by on v5 1/11
#3 lore raw

Re: [PATCH v5 01/11] tpm: Add support for in-kernel resetting of PCRs

In reply to Evan Green
Quoted 156 lines · Evan Green

Add an internal command for resetting a PCR. This will be used by the encrypted hibernation code to set PCR23 to a known value. The hibernation code will seal the hibernation key with a policy specifying PCR23 be set to this known value as a mechanism to ensure that the hibernation key is genuine. But to do this repeatedly, resetting the PCR is necessary as well.

---

Changes in v5:
- Change to co-developed by Matthew (Kees)

Changes in v4:
- Open code tpm2_pcr_reset implementation in tpm-interface.c (Jarkko)
- Rename interface symbol to tpm2_pcr_reset, fix kerneldocs (Jarkko)

Changes in v3:
- Unify tpm1/2_pcr_reset prototypes (Jarkko)
- Wait no, remove the TPM1 stuff altogether (Jarkko)
- Remove extra From tag and blank in commit msg (Jarkko).

 drivers/char/tpm/tpm-interface.c | 47 ++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm2-cmd.c      |  7 -----
 include/linux/tpm.h              | 14 ++++++++++
 3 files changed, 61 insertions(+), 7 deletions(-)
drivers/char/tpm/tpm-interface.c +47 -0
index 1621ce8187052c..886277b2654e3b 100644
@@ -342,6 +342,53 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 }
 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
 
+/**
+ * tpm2_pcr_reset - Reset the specified PCR
+ * @chip: A &struct tpm_chip instance, %NULL for the default chip
+ * @pcr_idx: The PCR to be reset
+ *
+ * Return: Same as with tpm_transmit_cmd(), or ENOTTY for TPM1 devices.
+ */
+int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx)
+{
+	struct tpm2_null_auth_area auth_area;
+	struct tpm_buf buf;
+	int rc;
+
+	chip = tpm_find_get_ops(chip);
+	if (!chip)
+		return -ENODEV;
+
+	if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
+		rc = -ENOTTY;
+		goto out;
+	}
+
+	rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_RESET);
+	if (rc)
+		goto out;
+
+	tpm_buf_append_u32(&buf, 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;
+
+	tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));
+	tpm_buf_append(&buf, (const unsigned char *)&auth_area,
+		       sizeof(auth_area));
+
+	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to reset a PCR");
+
+	tpm_buf_destroy(&buf);
+
+out:
+	tpm_put_ops(chip);
+	return rc;
+}
+EXPORT_SYMBOL_GPL(tpm2_pcr_reset);
+
 /**
  * tpm_send - send a TPM command
  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
drivers/char/tpm/tpm2-cmd.c +0 -7
index 65d03867e114c5..303ce2ea02a4b0 100644
@@ -216,13 +216,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
 	return rc;
 }
 
-struct tpm2_null_auth_area {
-	__be32  handle;
-	__be16  nonce_size;
-	u8  attributes;
-	__be16  auth_size;
-} __packed;
-
 /**
  * tpm2_pcr_extend() - extend a PCR value
  *
include/linux/tpm.h +14 -0
index dfeb25a0362dee..70134e6551745f 100644
@@ -219,6 +219,7 @@ enum tpm2_command_codes {
 	TPM2_CC_HIERARCHY_CONTROL       = 0x0121,
 	TPM2_CC_HIERARCHY_CHANGE_AUTH   = 0x0129,
 	TPM2_CC_CREATE_PRIMARY          = 0x0131,
+	TPM2_CC_PCR_RESET		= 0x013D,
 	TPM2_CC_SEQUENCE_COMPLETE       = 0x013E,
 	TPM2_CC_SELF_TEST	        = 0x0143,
 	TPM2_CC_STARTUP		        = 0x0144,
@@ -293,6 +294,13 @@ struct tpm_header {
 	};
 } __packed;
 
+struct tpm2_null_auth_area {
+	__be32  handle;
+	__be16  nonce_size;
+	u8  attributes;
+	__be16  auth_size;
+} __packed;
+
 /* A string buffer type for constructing TPM commands. This is based on the
  * ideas of string buffer code in security/keys/trusted.h but is heap based
  * in order to keep the stack usage minimal.
@@ -423,6 +431,7 @@ extern ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
 				size_t min_rsp_body_length, const char *desc);
 extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
 			struct tpm_digest *digest);
+extern int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx);
 extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 			  struct tpm_digest *digests);
 extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
@@ -440,6 +449,11 @@ static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
 	return -ENODEV;
 }
 
+static inline int tpm2_pcr_reset(struct tpm_chip *chip, int pcr_idx)
+{
+	return -ENODEV;
+}
+
 static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 				 struct tpm_digest *digests)
 {
Signature · 1 line
2.38.1.431.g37b22c650d-goog

BR, Jarkko

maintainer
#4 lore raw

Re: [PATCH v5 01/11] tpm: Add support for in-kernel resetting of PCRs

Quoted 146 lines · Jarkko Sakkinen
Quoted 145 lines · Evan Green

Add an internal command for resetting a PCR. This will be used by the encrypted hibernation code to set PCR23 to a known value. The hibernation code will seal the hibernation key with a policy specifying PCR23 be set to this known value as a mechanism to ensure that the hibernation key is genuine. But to do this repeatedly, resetting the PCR is necessary as well.

---

Changes in v5:
- Change to co-developed by Matthew (Kees)

Changes in v4:
- Open code tpm2_pcr_reset implementation in tpm-interface.c (Jarkko)
- Rename interface symbol to tpm2_pcr_reset, fix kerneldocs (Jarkko)

Changes in v3:
- Unify tpm1/2_pcr_reset prototypes (Jarkko)
- Wait no, remove the TPM1 stuff altogether (Jarkko)
- Remove extra From tag and blank in commit msg (Jarkko).

 drivers/char/tpm/tpm-interface.c | 47 ++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm2-cmd.c      |  7 -----
 include/linux/tpm.h              | 14 ++++++++++
 3 files changed, 61 insertions(+), 7 deletions(-)
drivers/char/tpm/tpm-interface.c +47 -0
index 1621ce8187052c..886277b2654e3b 100644
@@ -342,6 +342,53 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 }
 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
 
+/**
+ * tpm2_pcr_reset - Reset the specified PCR
+ * @chip: A &struct tpm_chip instance, %NULL for the default chip
+ * @pcr_idx: The PCR to be reset
+ *
+ * Return: Same as with tpm_transmit_cmd(), or ENOTTY for TPM1 devices.
+ */
+int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx)
+{
+	struct tpm2_null_auth_area auth_area;
+	struct tpm_buf buf;
+	int rc;
+
+	chip = tpm_find_get_ops(chip);
+	if (!chip)
+		return -ENODEV;
+
+	if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
+		rc = -ENOTTY;
+		goto out;
+	}
+
+	rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_RESET);
+	if (rc)
+		goto out;
+
+	tpm_buf_append_u32(&buf, 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;
+
+	tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));
+	tpm_buf_append(&buf, (const unsigned char *)&auth_area,
+		       sizeof(auth_area));
+
+	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to reset a PCR");
+
+	tpm_buf_destroy(&buf);
+
+out:
+	tpm_put_ops(chip);
+	return rc;
+}
+EXPORT_SYMBOL_GPL(tpm2_pcr_reset);
+
 /**
  * tpm_send - send a TPM command
  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
drivers/char/tpm/tpm2-cmd.c +0 -7
index 65d03867e114c5..303ce2ea02a4b0 100644
@@ -216,13 +216,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
 	return rc;
 }
 
-struct tpm2_null_auth_area {
-	__be32  handle;
-	__be16  nonce_size;
-	u8  attributes;
-	__be16  auth_size;
-} __packed;
-
 /**
  * tpm2_pcr_extend() - extend a PCR value
  *
include/linux/tpm.h +9 -0
index dfeb25a0362dee..70134e6551745f 100644
@@ -219,6 +219,7 @@ enum tpm2_command_codes {
 	TPM2_CC_HIERARCHY_CONTROL       = 0x0121,
 	TPM2_CC_HIERARCHY_CHANGE_AUTH   = 0x0129,
 	TPM2_CC_CREATE_PRIMARY          = 0x0131,
+	TPM2_CC_PCR_RESET		= 0x013D,
 	TPM2_CC_SEQUENCE_COMPLETE       = 0x013E,
 	TPM2_CC_SELF_TEST	        = 0x0143,
 	TPM2_CC_STARTUP		        = 0x0144,
@@ -293,6 +294,13 @@ struct tpm_header {
 	};
 } __packed;
 
+struct tpm2_null_auth_area {
+	__be32  handle;
+	__be16  nonce_size;
+	u8  attributes;
+	__be16  auth_size;
+} __packed;
+
 /* A string buffer type for constructing TPM commands. This is based on the
  * ideas of string buffer code in security/keys/trusted.h but is heap based
  * in order to keep the stack usage minimal.
@@ -423,6 +431,7 @@ extern ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
 				size_t min_rsp_body_length, const char *desc);
 extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
 			struct tpm_digest *digest);
+extern int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx);
 extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 			  struct tpm_digest *digests);
 extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
@@ -440,6 +449,11 @@ static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
 	return -ENODEV;
 }
 
Quoted 13 lines
Quoted 11 lines
include/linux/tpm.h +5 -0
+static inline int tpm2_pcr_reset(struct tpm_chip *chip, int pcr_idx)
+{
+	return -ENODEV;
+}
+
 static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 				 struct tpm_digest *digests)
 {
Signature · 1 line
2.38.1.431.g37b22c650d-goog

Please ignore, wrong patch, sorry.

BR, Jarkko

author
#5 lore raw

PATCH v5 2/11 tpm: Export and rename tpm2_find_and_validate_cc()

In reply to Evan Green

Export tpm_find_and_validate_cc() since it will be needed by an upcoming change allowing access to certain PCRs to be restricted to the kernel. In order to export it consistently, and because it's a tpm2-only function, rename it to tpm2_find_and_validate_cc().

---

(no changes since v3)

Changes in v3:
- Split find_and_validate_cc() export to its own patch (Jarkko)
- Rename tpm_find_and_validate_cc() to tpm2_find_and_validate_cc().

 drivers/char/tpm/tpm.h        | 3 +++
 drivers/char/tpm/tpm2-space.c | 8 ++++----
 2 files changed, 7 insertions(+), 4 deletions(-)
drivers/char/tpm/tpm.h +3 -0
index 24ee4e1cc452a0..f1e0f490176f01 100644
@@ -231,6 +231,9 @@ int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
 int tpm2_init_space(struct tpm_space *space, unsigned int buf_size);
 void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space);
 void tpm2_flush_space(struct tpm_chip *chip);
+int tpm2_find_and_validate_cc(struct tpm_chip *chip,
+			      struct tpm_space *space,
+			      const void *cmd, size_t len);
 int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
 		       size_t cmdsiz);
 int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, void *buf,
drivers/char/tpm/tpm2-space.c +4 -4
index ffb35f0154c16c..ca34cc006e7f8d 100644
@@ -262,9 +262,9 @@ static int tpm2_map_command(struct tpm_chip *chip, u32 cc, u8 *cmd)
 	return 0;
 }
 
-static int tpm_find_and_validate_cc(struct tpm_chip *chip,
-				    struct tpm_space *space,
-				    const void *cmd, size_t len)
+int tpm2_find_and_validate_cc(struct tpm_chip *chip,
+			      struct tpm_space *space,
+			      const void *cmd, size_t len)
 {
 	const struct tpm_header *header = (const void *)cmd;
 	int i;
@@ -306,7 +306,7 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
 	if (!space)
 		return 0;
 
-	cc = tpm_find_and_validate_cc(chip, space, cmd, cmdsiz);
+	cc = tpm2_find_and_validate_cc(chip, space, cmd, cmdsiz);
 	if (cc < 0)
 		return cc;
 
Signature · 1 line
2.38.1.431.g37b22c650d-goog
author
#6 lore raw

PATCH v5 3/11 tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to Evan Green

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled restricts usermode's ability to extend or reset PCR 23.

Under certain circumstances it might be desirable to enable the creation of TPM-backed secrets that are only accessible to the kernel. In an ideal world this could be achieved by using TPM localities, but these don't appear to be available on consumer systems. An alternative is to simply block userland from modifying one of the resettable PCRs, leaving it available to the kernel. If the kernel ensures that no userland can access the TPM while it is carrying out work, it can reset PCR 23, extend it to an arbitrary value, create or load a secret, and then reset the PCR again. Even if userland somehow obtains the sealed material, it will be unable to unseal it since PCR 23 will never be in the appropriate state.

This Kconfig is only properly supported for systems with TPM2 devices. For systems with TPM1 devices, having this Kconfig enabled completely restricts usermode's access to the TPM. TPM1 contains support for tunnelled transports, which usermode could use to smuggle commands through that this Kconfig is attempting to restrict.

---

Changes in v5:
- Change tags on RESTRICT_PCR patch (Kees)
- Rename to TCG_TPM2_RESTRICT_PCR
- Do nothing on TPM1.2 devices (Jarkko, Doug)

Changes in v4:
- Augment the commit message (Jarkko)

Changes in v3:
- Fix up commit message (Jarkko)
- tpm2_find_and_validate_cc() was split (Jarkko)
- Simply fully restrict TPM1 since v2 failed to account for tunnelled transport sessions (Stefan and Jarkko).

Changes in v2:
- Fixed sparse warnings

 drivers/char/tpm/Kconfig          | 12 ++++++++++++
 drivers/char/tpm/tpm-dev-common.c |  6 ++++++
 drivers/char/tpm/tpm.h            | 12 ++++++++++++
 drivers/char/tpm/tpm2-cmd.c       | 22 ++++++++++++++++++++++
 4 files changed, 52 insertions(+)
drivers/char/tpm/Kconfig +12 -0
index 927088b2c3d3f2..e6d3aa9f6c694f 100644
@@ -211,4 +211,16 @@ config TCG_FTPM_TEE
 	  This driver proxies for firmware TPM running in TEE.
 
 source "drivers/char/tpm/st33zp24/Kconfig"
+
+config TCG_TPM2_RESTRICT_PCR
+	bool "Restrict userland access to PCR 23 on TPM2 devices"
+	depends on TCG_TPM
+	help
+	  If set, block userland from extending or resetting PCR 23 on TPM2.0
+	  and later systems. This allows the PCR to be restricted to in-kernel
+	  use, preventing userland from being able to make use of data sealed to
+	  the TPM by the kernel. This is required for secure hibernation
+	  support, but should be left disabled if any userland may require
+	  access to PCR23. This is a TPM2-only feature, enabling this on a TPM1
+	  machine is effectively a no-op.
 endif # TCG_TPM
drivers/char/tpm/tpm-dev-common.c +6 -0
index dc4c0a0a512903..66d15a2a967443 100644
@@ -198,6 +198,12 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
 	priv->response_read = false;
 	*off = 0;
 
+	if (priv->chip->flags & TPM_CHIP_FLAG_TPM2) {
+		ret = tpm2_cmd_restricted(priv->chip, priv->data_buffer, size);
+		if (ret)
+			goto out;
+	}
+
 	/*
 	 * If in nonblocking mode schedule an async job to send
 	 * the command return the size.
drivers/char/tpm/tpm.h +12 -0
index f1e0f490176f01..7fb746d210f59d 100644
@@ -245,4 +245,16 @@ void tpm_bios_log_setup(struct tpm_chip *chip);
 void tpm_bios_log_teardown(struct tpm_chip *chip);
 int tpm_dev_common_init(void);
 void tpm_dev_common_exit(void);
+
+#ifdef CONFIG_TCG_TPM2_RESTRICT_PCR
+#define TPM_RESTRICTED_PCR 23
+
+int tpm2_cmd_restricted(struct tpm_chip *chip, u8 *buffer, size_t size);
+#else
+static inline int tpm2_cmd_restricted(struct tpm_chip *chip, u8 *buffer,
+				      size_t size)
+{
+	return 0;
+}
+#endif
 #endif
drivers/char/tpm/tpm2-cmd.c +22 -0
index 303ce2ea02a4b0..3bc5546fddc792 100644
@@ -778,3 +778,25 @@ int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
 
 	return -1;
 }
+
+#ifdef CONFIG_TCG_TPM2_RESTRICT_PCR
+int tpm2_cmd_restricted(struct tpm_chip *chip, u8 *buffer, size_t size)
+{
+	int cc = tpm2_find_and_validate_cc(chip, NULL, buffer, size);
+	__be32 *handle;
+
+	switch (cc) {
+	case TPM2_CC_PCR_EXTEND:
+	case TPM2_CC_PCR_RESET:
+		if (size < (TPM_HEADER_SIZE + sizeof(u32)))
+			return -EINVAL;
+
+		handle = (__be32 *)&buffer[TPM_HEADER_SIZE];
+		if (be32_to_cpu(*handle) == TPM_RESTRICTED_PCR)
+			return -EPERM;
+		break;
+	}
+
+	return 0;
+}
+#endif
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#7 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 1 line · Evan Green

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled

TCG_TPM_RESTRICT_PCR => TCG_TPM2_RESTRICT_PCR

Quoted 2 lines

For systems with TPM1 devices, having this Kconfig enabled completely restricts usermode's access to the TPM.

This doesn't appear to actually be the case.

drivers/char/tpm/Kconfig config TCG_FTPM_TEE · line 217

Quoted 3 lines
drivers/char/tpm/Kconfig +3 -0
+config TCG_TPM2_RESTRICT_PCR
+	bool "Restrict userland access to PCR 23 on TPM2 devices"
+	depends on TCG_TPM

I assume you also considered making this a once-settable sysctl, or similar? I guess this kconfig is fine for now, but IMO it does violate the concept of "kernel provides mechanism, not policy".

Quoted 14 lines
drivers/char/tpm/tpm2-cmd.c +6 -0
index 303ce2ea02a4b0..3bc5546fddc792 100644
@@ -778,3 +778,25 @@ int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
 
 	return -1;
 }
+
+#ifdef CONFIG_TCG_TPM2_RESTRICT_PCR
+int tpm2_cmd_restricted(struct tpm_chip *chip, u8 *buffer, size_t size)
+{
+	int cc = tpm2_find_and_validate_cc(chip, NULL, buffer, size);
+	__be32 *handle;

drivers/char/tpm/tpm2-cmd.c int tpm2_find_cc(struct tpm_chip *chip, u32 cc) · line 796

Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c +10 -0
+
+	switch (cc) {
+	case TPM2_CC_PCR_EXTEND:
+	case TPM2_CC_PCR_RESET:
+		if (size < (TPM_HEADER_SIZE + sizeof(u32)))
+			return -EINVAL;
+
+		handle = (__be32 *)&buffer[TPM_HEADER_SIZE];
+		if (be32_to_cpu(*handle) == TPM_RESTRICTED_PCR)
+			return -EPERM;

get_unaligned_be32((__be32 *)&buffer[TPM_HEADER_SIZE]), to avoid an unaligned memory access.

drivers/char/tpm/tpm2-cmd.c int tpm2_find_cc(struct tpm_chip *chip, u32 cc) · line 800

Quoted 4 lines
drivers/char/tpm/tpm2-cmd.c +4 -0
+		break;
+	}
+
+	return 0;

So, if tpm2_find_and_validate_cc() returns an error code, the command is *not* restricted, even if it uses one of the forbidden command codes. Are you sure there are no loopholes here?

- Eric

#8 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to Evan Green
Quoted 2 lines · Evan Green

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled restricts usermode's ability to extend or reset PCR 23.

Could I re ask the question here that I asked of Matthew's patch set:

https://lore.kernel.org/all/[email protected]/

Which was could we use an NVRAM index in the TPM instead of a PCR? The reason for asking was that PCRs are rather precious and might get more so now that Lennart has some grand scheme for using more of them in his unified boot project. Matthew promised to play with the idea but never got back to the patch set to say whether he investigated this or not.

James

maintainer
#9 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 6 lines · James Bottomley
Quoted 2 lines · Evan Green

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled restricts usermode's ability to extend or reset PCR 23.

Could I re ask the question here that I asked of Matthew's patch set:

Quoted 7 lines

https://lore.kernel.org/all/[email protected]/

Which was could we use an NVRAM index in the TPM instead of a PCR? The reason for asking was that PCRs are rather precious and might get more so now that Lennart has some grand scheme for using more of them in his unified boot project. Matthew promised to play with the idea but never got back to the patch set to say whether he investigated this or not.

Even for PCR case it would be better to have it configurable through kernel command-line, including a disabled state, which would the default.

This would be backwards compatible, and if designed properly, could more easily extended for NV index later on.

BR, Jarkko

#10 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 17 lines · Jarkko Sakkinen
Quoted 7 lines · James Bottomley
Quoted 2 lines · Evan Green

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled restricts usermode's ability to extend or reset PCR 23.

Could I re ask the question here that I asked of Matthew's patch set:

Quoted 8 lines

https://lore.kernel.org/all/[email protected]/

Which was could we use an NVRAM index in the TPM instead of a PCR? The reason for asking was that PCRs are rather precious and might get more so now that Lennart has some grand scheme for using more of them in his unified boot project.  Matthew promised to play with the idea but never got back to the patch set to say whether he investigated this or not.

Quoted 6 lines

Even for PCR case it would be better to have it configurable through kernel command-line, including a disabled state, which would the default.

This would be backwards compatible, and if designed properly, could more easily extended for NV index later on.

Um how? The observation is in the above referenced email is that PCR23 is reserved in the TCG literature for application usage. If any application is actually using PCR23 based on that spec then revoking access to user space will cause it to break. This is an ABI change which is not backwards compatible. You can call it a distro problem if it's command line configurable, but the default would be what most distros take, so it's rather throwing them under the bus if there is an application using it.

Of course, if no application is actually using PCR23, then it's probably OK to use it in the kernel and make it invisible to user space, but no evidence about this has actually been presented.

James

#11 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

On Sun, Nov 27, 2022 at 11:41:26AM -0500, James Bottomley wrote:

Good afternoon, I hope the week is going well for everyone.

Quoted 34 lines · Jarkko Sakkinen
Quoted 17 lines · James Bottomley
Quoted 7 lines · Evan Green
Quoted 2 lines

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled restricts usermode's ability to extend or reset PCR 23.

Could I re ask the question here that I asked of Matthew's patch set:

Quoted 8 lines

https://lore.kernel.org/all/[email protected]/

Which was could we use an NVRAM index in the TPM instead of a PCR??? The reason for asking was that PCRs are rather precious and might get more so now that Lennart has some grand scheme for using more of them in his unified boot project.?? Matthew promised to play with the idea but never got back to the patch set to say whether he investigated this or not.

Quoted 6 lines

Even for PCR case it would be better to have it configurable through kernel command-line, including a disabled state, which would the default.

This would be backwards compatible, and if designed properly, could more easily extended for NV index later on.

Um how? The observation is in the above referenced email is that PCR23 is reserved in the TCG literature for application usage. If any application is actually using PCR23 based on that spec then revoking access to user space will cause it to break. This is an ABI change which is not backwards compatible. You can call it a distro problem if it's command line configurable, but the default would be what most distros take, so it's rather throwing them under the bus if there is an application using it.

Quoted 3 lines

Of course, if no application is actually using PCR23, then it's probably OK to use it in the kernel and make it invisible to user space, but no evidence about this has actually been presented.

If there isn't, there will be in in the next week or so, if we can stay on schedule. Otherwise, I fear that Casey Schaufler, who I believe is holding his breath, may turn irretrievably blue.... :-)

The Trust Orchestration System, Quixote, that we are releasing for Linux uses PCR23 to generate an attestation of the functional state value for an internally modeled security domain.

TSEM, the LSM based kernel component in all of this, supports the ability to implement multiple 'domains', nee namespaces, each of which can have a security modeling function attached to it. Each internally modeled domain has to have the ability to independently attest the functional value of the security model implemented for the domain/namespace.

We have found, and I believe others will find that, particularly the resettable registers, are too precious to be constrained from general usage. We actually just finished lifting the PCR23 extension functionality out of the TSEM driver and into userspace because having it in the kernel was too constraining.

With respect to making the behavior a command-line option. We've slogged through 2+ years of conversations with sizable players who have indicated that if the 'distys' don't implement something, it isn't a relevant Linux technology, so a command-line option poses a barrier to innovation.

Quoted 1 line

James

Have a good day.

As always,
Dr. Greg

The Quixote Project - Flailing at the Travails of Cybersecurity

#12 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 35 lines · Dr. Greg

On Sun, Nov 27, 2022 at 11:41:26AM -0500, James Bottomley wrote:

Good afternoon, I hope the week is going well for everyone.

Quoted 31 lines · Jarkko Sakkinen
Quoted 15 lines · James Bottomley
Quoted 6 lines · Evan Green
Quoted 2 lines

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled restricts usermode's ability to extend or reset PCR 23.

Could I re ask the question here that I asked of Matthew's patch set:

Quoted 8 lines

https://lore.kernel.org/all/[email protected]/

Which was could we use an NVRAM index in the TPM instead of a PCR??? The reason for asking was that PCRs are rather precious and might get more so now that Lennart has some grand scheme for using more of them in his unified boot project.?? Matthew promised to play with the idea but never got back to the patch set to say whether he investigated this or not.

Quoted 6 lines

Even for PCR case it would be better to have it configurable through kernel command-line, including a disabled state, which would the default.

This would be backwards compatible, and if designed properly, could more easily extended for NV index later on.

Um how? The observation is in the above referenced email is that PCR23 is reserved in the TCG literature for application usage. If any application is actually using PCR23 based on that spec then revoking access to user space will cause it to break. This is an ABI change which is not backwards compatible. You can call it a distro problem if it's command line configurable, but the default would be what most distros take, so it's rather throwing them under the bus if there is an application using it.

Quoted 6 lines
Quoted 3 lines

Of course, if no application is actually using PCR23, then it's probably OK to use it in the kernel and make it invisible to user space, but no evidence about this has actually been presented.

If there isn't, there will be in in the next week or so, if we can stay on schedule. Otherwise, I fear that Casey Schaufler, who I believe is holding his breath, may turn irretrievably blue.... :-)

Sorry to disappoint, but my supply of apoplexy is firmly rooted elsewhere for the time being. :-( Also, you overestimate my interest in things TPM related.

Quoted 10 lines

The Trust Orchestration System, Quixote, that we are releasing for Linux uses PCR23 to generate an attestation of the functional state value for an internally modeled security domain.

TSEM, the LSM based kernel component in all of this, supports the ability to implement multiple 'domains', nee namespaces, each of which can have a security modeling function attached to it. Each internally modeled domain has to have the ability to independently attest the functional value of the security model implemented for the domain/namespace.

I am very interested to see TSEM. I have heard nothing of it to date.

Quoted 19 lines

We have found, and I believe others will find that, particularly the resettable registers, are too precious to be constrained from general usage. We actually just finished lifting the PCR23 extension functionality out of the TSEM driver and into userspace because having it in the kernel was too constraining.

With respect to making the behavior a command-line option. We've slogged through 2+ years of conversations with sizable players who have indicated that if the 'distys' don't implement something, it isn't a relevant Linux technology, so a command-line option poses a barrier to innovation.

Quoted 1 line

James

Have a good day.

As always,
Dr. Greg

The Quixote Project - Flailing at the Travails of Cybersecurity

#13 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

On Wed, Nov 30, 2022 at 01:34:28PM -0800, Casey Schaufler wrote:

Good evening to everyone.

Quoted 6 lines · Dr. Greg
Quoted 5 lines · James Bottomley
Quoted 3 lines

Of course, if no application is actually using PCR23, then it's probably OK to use it in the kernel and make it invisible to user space, but no evidence about this has actually been presented.

Quoted 7 lines
Quoted 3 lines

If there isn't, there will be in in the next week or so, if we can stay on schedule. Otherwise, I fear that Casey Schaufler, who I believe is holding his breath, may turn irretrievably blue.... :-)

Sorry to disappoint, but my supply of apoplexy is firmly rooted elsewhere for the time being. :-( Also, you overestimate my interest in things TPM related.

I was being too clever by half, my comment had nothing to do with your interest, or lack thereof about TPM's.... :-)

I had replied to one of the threads where LSM stacking and IMA integration issues were being discussed and I commented that TSEM may contribute to those conversations. You had replied back and said that sending teasers was unfair, I was suggesting with my comment that you were holding your breath waiting for the release of TSEM.... :-)

On a related note to this thread, a major component of Quixote/TSEM is the notion of raising the question and opportunity for shaping what TPM's should be when they grow up, given the limited resources they bring to the table, let alone the notion that they are about retrospective rather than prospective trust.

Quoted 2 lines

I am very interested to see TSEM. I have heard nothing of it to date.

Hardly anyone has, small team, very focused, working in a deep dive for the last couple of years to bring this forward.

Hopefully it will prove of interest and utility, I don't believe there is a reference in the literature to an equivalent approach.

Have a good evening.

As always,
Dr. Greg

The Quixote Project - Flailing at the Travails of Cybersecurity

#14 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to James Bottomley
Quoted 7 lines · James Bottomley
Quoted 2 lines · Evan Green

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled restricts usermode's ability to extend or reset PCR 23.

Could I re ask the question here that I asked of Matthew's patch set:

Quoted 7 lines

https://lore.kernel.org/all/[email protected]/

Which was could we use an NVRAM index in the TPM instead of a PCR? The reason for asking was that PCRs are rather precious and might get more so now that Lennart has some grand scheme for using more of them in his unified boot project. Matthew promised to play with the idea but never got back to the patch set to say whether he investigated this or not.

Is there any way to get key creation data to include NV indexes? If not, no, we can't use NVRAM.

#15 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 11 lines · Matthew Garrett
Quoted 9 lines · James Bottomley
Quoted 2 lines · Evan Green

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled restricts usermode's ability to extend or reset PCR 23.

Could I re ask the question here that I asked of Matthew's patch set:

https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 5 lines

Which was could we use an NVRAM index in the TPM instead of a PCR? The reason for asking was that PCRs are rather precious and might get more so now that Lennart has some grand scheme for using more of them in his unified boot project. Matthew promised to play with the idea but never got back to the patch set to say whether he investigated this or not.

Is there any way to get key creation data to include NV indexes?

Not that I am aware of and the spec seems to be a no.

Quoted 1 line

If not, no, we can't use NVRAM.

What's the use case of using the creation data and ticket in this context? Who gets the
creationData and the ticket?
Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

#16 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the
creationData and the ticket?
Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

#17 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

James

#18 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 29 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

Quoted 5 lines

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

I also usually stick to features within the PTP spec[1], which includes the locality support.

+2 for locality, I responded somewhere that I also support locality. I was thinking more of TPM2_PolicyLocality I didn't realize that's within the creationData. I was thinking more along the lines of, can I wield the key over "did my locality create it". I'm not sure what other protections are on the key,are there any protections preventing them from
wielding it and using it to sign something nefarious?

1. https://trustedcomputinggroup.org/wp-content/uploads/TCG_PC_Client_Platform_TPM_Profile_PTP_Specification_Family_2.0_Revision_1.3v22.pdf

Quoted 2 lines

James

#19 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to James Bottomley
Quoted 9 lines · James Bottomley

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

Quoted 4 lines

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

It's not a question of TPM support, it's a question of platform support. Intel chipsets that don't support TXT simply don't forward requests with non-0 locality. Every Windows-sticker laptop since 2014 has shipped with a TPM, but the number that ship with TXT support is a very small percentage of that. I agree that locality is the obvious solution for a whole bunch of problems, but it's just not usable in the generic case.

#20 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 16 lines · Matthew Garrett
Quoted 9 lines · James Bottomley

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

Quoted 4 lines

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Quoted 7 lines

It's not a question of TPM support, it's a question of platform support. Intel chipsets that don't support TXT simply don't forward requests with non-0 locality. Every Windows-sticker laptop since 2014 has shipped with a TPM, but the number that ship with TXT support is a very small percentage of that. I agree that locality is the obvious solution for a whole bunch of problems, but it's just not usable in the generic case.

Instead of walling off a PCR, why not wall off an NV Index PCR and use a policy?

#21 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to Matthew Garrett
Quoted 16 lines · Matthew Garrett
Quoted 9 lines · James Bottomley

Can we go back again to why you can't use locality?  It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality.  However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

Quoted 4 lines

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Quoted 7 lines

It's not a question of TPM support, it's a question of platform support. Intel chipsets that don't support TXT simply don't forward requests with non-0 locality. Every Windows-sticker laptop since 2014 has shipped with a TPM, but the number that ship with TXT support is a very small percentage of that. I agree that locality is the obvious solution for a whole bunch of problems, but it's just not usable in the generic case.

How sure are you of this statement? Of all the Laptops I have with TPM2 (a sample size of 2), my old Dell XPS-13 (a 9350 bought in 2016 with a TPM 1.2 that was firmware upgraded to 2.0) has a Nuvoton TIS TPM that doesn't respond on any locality other than 0. However, my more modern Inspiron 13 2-in-1 (a 7391 from 2019 recently bought refurbished) has an Intel PTT TPM using the CRB interface and responds fine on locality 1 and also indicates that locality in the creation data. Neither of these laptops has TXT nor the SMX extensions, so that would seem to indicate your statement above isn't universal.

James

maintainer
#22 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to James Bottomley
Quoted 28 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

Quoted 4 lines

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Kernel could try to use locality 2 and use locality 0 as fallback.

BR, Jarkko

#23 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 30 lines · Jarkko Sakkinen
Quoted 28 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

Quoted 6 lines
Quoted 4 lines

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Kernel could try to use locality 2 and use locality 0 as fallback.

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

I was informed that all 5 localities should be supported starting with Gen 7 Kaby Lake launched in 2016. Don't know if this is still "too new".

Quoted 2 lines

BR, Jarkko

#24 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

On Mon, Jan 23, 2023 at 11:48:25AM -0600, William Roberts wrote:

Good morning, I hope the week is going well for everyone.

Quoted 36 lines · Jarkko Sakkinen
Quoted 35 lines · James Bottomley
Quoted 32 lines · Matthew Garrett
Quoted 11 lines
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Quoted 4 lines
Quoted 1 line

Kernel could try to use locality 2 and use locality 0 as fallback.

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

Indeed, I was going to mention that. Falling back means that the security guarantee is lost, perhaps silently and lost on the owner of the system, if they are not paying attention to things like the boot logs.

One of the persistent challenges with these hardware security technologies is that they need to be ubiquitous to be useful, something that has historically plagued all of these technologies.

Quoted 3 lines

I was informed that all 5 localities should be supported starting with Gen 7 Kaby Lake launched in 2016. Don't know if this is still "too new".

It will be necessary, and important, to differentiate between 'supported' and 'available'.

Historically, security features have been SKU'ified, in other words, made available only on specific SKU's, even when the platform writ large has the necessary support. These SKU's are designed to be directed at various verticals or OEM's who are perceived to be willing to pay more for enhanced security.

I've had conversations on whether or not hardware technologies would be available and the conversation usually ends with the equivalent of:
"Show us the business case for supporting this."

Which translates, roughly, into how much money are we going to make if we offer this.

Unfortunately, without being ubiquitous, as you note for a long period of time, there is no development interest, which in turn translates into no market 'pull'. A rather troublesome dilemma for security innovation.

As always,
Dr. Greg

The Quixote Project - Flailing at the Travails of Cybersecurity

#25 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to William Roberts
Quoted 37 lines · William Roberts
Quoted 35 lines · Jarkko Sakkinen
Quoted 32 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality?  It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality.  However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Quoted 4 lines
Quoted 1 line

Kernel could try to use locality 2 and use locality 0 as fallback.

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

No, I think it would be good enough: locality 0 means anyone (including the kernel on a machine which doesn't function correctly) could have created this key. Locality 2 would mean only the kernel could have created this key.

By the time the kernel boots and before it loads the hibernation image it will know the answer to the question "does my TPM support locality 2", so it can use that in its security assessment: if the kernel supports locality 2 and the key wasn't created in locality 2 then assume an attack. Obviously, if the kernel doesn't support locality 2 then the hibernation resume has to accept any old key, but that's the same as the situation today.

Quoted 3 lines

I was informed that all 5 localities should be supported starting with Gen 7 Kaby Lake launched in 2016. Don't know if this is still "too new".

It's probably good enough. Current laptops which can't use locality 2 are in the same position as now, but newer ones can provide more security guarantees.

There is, however, another wrinkle: can Kaby Lake be persuaded, though bios settings perhaps, to shut off the non zero localities? This would allow for a downgrade attack where you shut off locality 2 then present a forged locality 0 key and hibernation image; the kernel will think, because it can't access locality 2, that it's in a reduced security environment so the key might be OK. We could fix this by requiring Kaby Lake and beyond to have locality 2 and refusing to hibernate if it can't be accessed and building "is this Kaby lake or beyond" into the check for should I have locality 2, but this is getting complex and error prone.

James

#26 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 41 lines · James Bottomley
Quoted 39 lines · William Roberts
Quoted 36 lines · Jarkko Sakkinen
Quoted 28 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

Quoted 4 lines

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Kernel could try to use locality 2 and use locality 0 as fallback.

Quoted 7 lines
Quoted 2 lines

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

No, I think it would be good enough: locality 0 means anyone (including the kernel on a machine which doesn't function correctly) could have created this key. Locality 2 would mean only the kernel could have created this key.

That's exactly what I was saying, for this feature to be functional 2 localities need to be supported.

Quoted 9 lines

By the time the kernel boots and before it loads the hibernation image it will know the answer to the question "does my TPM support locality 2", so it can use that in its security assessment: if the kernel supports locality 2 and the key wasn't created in locality 2 then assume an attack. Obviously, if the kernel doesn't support locality 2 then the hibernation resume has to accept any old key, but that's the same as the situation today.

Yep, we had this conversation offline on a thread, i'm in agreement here as well.

Quoted 10 lines
Quoted 3 lines

I was informed that all 5 localities should be supported starting with Gen 7 Kaby Lake launched in 2016. Don't know if this is still "too new".

It's probably good enough. Current laptops which can't use locality 2 are in the same position as now, but newer ones can provide more security guarantees.

There is, however, another wrinkle: can Kaby Lake be persuaded, though bios settings perhaps, to shut off the non zero localities?

I have no idea, and I don't have one handy, but I can ask around.

Quoted 12 lines

This would
allow for a downgrade attack where you shut off locality 2 then present a forged locality 0 key and hibernation image; the kernel will think, because it can't access locality 2, that it's in a reduced security environment so the key might be OK. We could fix this by requiring Kaby Lake and beyond to have locality 2 and refusing to hibernate if it can't be accessed and building "is this Kaby lake or beyond" into the check for should I have locality 2, but this is getting complex and error prone.

James

maintainer
#27 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to James Bottomley
Quoted 48 lines · James Bottomley
Quoted 37 lines · William Roberts
Quoted 35 lines · Jarkko Sakkinen
Quoted 32 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality?  It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality.  However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Quoted 4 lines
Quoted 1 line

Kernel could try to use locality 2 and use locality 0 as fallback.

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

No, I think it would be good enough: locality 0 means anyone (including the kernel on a machine which doesn't function correctly) could have created this key. Locality 2 would mean only the kernel could have created this key.

Quoted 7 lines

By the time the kernel boots and before it loads the hibernation image it will know the answer to the question "does my TPM support locality 2", so it can use that in its security assessment: if the kernel supports locality 2 and the key wasn't created in locality 2 then assume an attack. Obviously, if the kernel doesn't support locality 2 then the hibernation resume has to accept any old key, but that's the same as the situation today.

This sounds otherwise great to me but why bother even allowing a machine with no-locality TPM to be involved with hibernate? Simply detect locality support during driver initialization and disallow sealed hibernation (or whatever the feature was called) if localities were not detected.

I get supporting old hardware with old features but it does not make sense to maintain new features with hardware, which clearly does not scale, right?

BR, Jarkko

#28 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 64 lines · Jarkko Sakkinen
Quoted 48 lines · James Bottomley
Quoted 37 lines · William Roberts
Quoted 35 lines · Jarkko Sakkinen
Quoted 32 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Quoted 4 lines
Quoted 1 line

Kernel could try to use locality 2 and use locality 0 as fallback.

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

No, I think it would be good enough: locality 0 means anyone (including the kernel on a machine which doesn't function correctly) could have created this key. Locality 2 would mean only the kernel could have created this key.

Quoted 7 lines

By the time the kernel boots and before it loads the hibernation image it will know the answer to the question "does my TPM support locality 2", so it can use that in its security assessment: if the kernel supports locality 2 and the key wasn't created in locality 2 then assume an attack. Obviously, if the kernel doesn't support locality 2 then the hibernation resume has to accept any old key, but that's the same as the situation today.

This sounds otherwise great to me but why bother even allowing a machine with no-locality TPM to be involved with hibernate? Simply detect locality support during driver initialization and disallow sealed hibernation (or whatever the feature was called) if localities were not detected.

Quoted 5 lines

I get supporting old hardware with old features but it does not make sense to maintain new features with hardware, which clearly does not scale, right?

BR, Jarkko

Here's a thought, what if we had a static/cmd line configurable no-auth NV Index and writelocked it with the expected key information, name or something. I guess the problem is atomicity with write/lock, but can't the kernel lock out all other users?

An attacker would need to issue tpm2_startup, which in this case would DOS the kernel in both scenarios. If an attacker already wrote and locked the NV index, that would also be a DOS. If they already wrote it, the kernel simply writes whatever they want. Is there an attack I am missing?

I guess the issue here would be setup, since creating the NV index requires hierarchy auth, does the kernel have platform auth or is that already shut down by firmware (I can't recall)? A null hierarchy volatile lockable index would be nice for this, too bad that doesn't exist.

maintainer
#29 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 81 lines · William Roberts
Quoted 64 lines · Jarkko Sakkinen
Quoted 48 lines · James Bottomley
Quoted 37 lines · William Roberts
Quoted 35 lines · Jarkko Sakkinen
Quoted 32 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Quoted 4 lines
Quoted 1 line

Kernel could try to use locality 2 and use locality 0 as fallback.

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

No, I think it would be good enough: locality 0 means anyone (including the kernel on a machine which doesn't function correctly) could have created this key. Locality 2 would mean only the kernel could have created this key.

Quoted 7 lines

By the time the kernel boots and before it loads the hibernation image it will know the answer to the question "does my TPM support locality 2", so it can use that in its security assessment: if the kernel supports locality 2 and the key wasn't created in locality 2 then assume an attack. Obviously, if the kernel doesn't support locality 2 then the hibernation resume has to accept any old key, but that's the same as the situation today.

This sounds otherwise great to me but why bother even allowing a machine with no-locality TPM to be involved with hibernate? Simply detect locality support during driver initialization and disallow sealed hibernation (or whatever the feature was called) if localities were not detected.

Quoted 5 lines

I get supporting old hardware with old features but it does not make sense to maintain new features with hardware, which clearly does not scale, right?

BR, Jarkko

Here's a thought, what if we had a static/cmd line configurable no-auth NV Index and writelocked it with the expected key information, name or something. I guess the problem is atomicity with write/lock, but can't the kernel lock out all other users?

An attacker would need to issue tpm2_startup, which in this case would DOS the kernel in both scenarios. If an attacker already wrote and locked the NV index, that would also be a DOS. If they already wrote it, the kernel simply writes whatever they want. Is there an attack I am missing?

Quoted 4 lines

I guess the issue here would be setup, since creating the NV index requires hierarchy auth, does the kernel have platform auth or is that already shut down by firmware (I can't recall)? A null hierarchy volatile lockable index would be nice for this, too bad that doesn't exist.

How do you see this would better when compared to finding a way to use locality, which could potentially be made to somewhat simple to setup (practically zero config)?

BR, Jarkko

#30 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 83 lines · Jarkko Sakkinen
Quoted 81 lines · William Roberts
Quoted 64 lines · Jarkko Sakkinen
Quoted 48 lines · James Bottomley
Quoted 37 lines · William Roberts
Quoted 35 lines · Jarkko Sakkinen
Quoted 32 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Quoted 4 lines
Quoted 1 line

Kernel could try to use locality 2 and use locality 0 as fallback.

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

No, I think it would be good enough: locality 0 means anyone (including the kernel on a machine which doesn't function correctly) could have created this key. Locality 2 would mean only the kernel could have created this key.

Quoted 7 lines

By the time the kernel boots and before it loads the hibernation image it will know the answer to the question "does my TPM support locality 2", so it can use that in its security assessment: if the kernel supports locality 2 and the key wasn't created in locality 2 then assume an attack. Obviously, if the kernel doesn't support locality 2 then the hibernation resume has to accept any old key, but that's the same as the situation today.

This sounds otherwise great to me but why bother even allowing a machine with no-locality TPM to be involved with hibernate? Simply detect locality support during driver initialization and disallow sealed hibernation (or whatever the feature was called) if localities were not detected.

Quoted 5 lines

I get supporting old hardware with old features but it does not make sense to maintain new features with hardware, which clearly does not scale, right?

BR, Jarkko

Here's a thought, what if we had a static/cmd line configurable no-auth NV Index and writelocked it with the expected key information, name or something. I guess the problem is atomicity with write/lock, but can't the kernel lock out all other users?

An attacker would need to issue tpm2_startup, which in this case would DOS the kernel in both scenarios. If an attacker already wrote and locked the NV index, that would also be a DOS. If they already wrote it, the kernel simply writes whatever they want. Is there an attack I am missing?

Quoted 9 lines
Quoted 4 lines

I guess the issue here would be setup, since creating the NV index requires hierarchy auth, does the kernel have platform auth or is that already shut down by firmware (I can't recall)? A null hierarchy volatile lockable index would be nice for this, too bad that doesn't exist.

How do you see this would better when compared to finding a way to use locality, which could potentially be made to somewhat simple to setup (practically zero config)?

I never said it was better, I said here is a thought for discussion. If we had to support older hardware (I could care less about things that don't support localities, but some might not), this could be an avenue to support them without walling off a PCR. I pointed out the downsides, and argument could be made that when localities is not supported then walling off PCR23 is the better approach if older hardware is an issue. This all hinges on do we care about things that don't support multiple localities. I don't, im for if you have locality support you get the feature else you don't.

Quoted 1 line

BR, Jarkko

maintainer
#31 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 94 lines · William Roberts
Quoted 83 lines · Jarkko Sakkinen
Quoted 81 lines · William Roberts
Quoted 64 lines · Jarkko Sakkinen
Quoted 48 lines · James Bottomley
Quoted 37 lines · William Roberts
Quoted 35 lines · Jarkko Sakkinen
Quoted 32 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Quoted 4 lines
Quoted 1 line

Kernel could try to use locality 2 and use locality 0 as fallback.

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

No, I think it would be good enough: locality 0 means anyone (including the kernel on a machine which doesn't function correctly) could have created this key. Locality 2 would mean only the kernel could have created this key.

Quoted 7 lines

By the time the kernel boots and before it loads the hibernation image it will know the answer to the question "does my TPM support locality 2", so it can use that in its security assessment: if the kernel supports locality 2 and the key wasn't created in locality 2 then assume an attack. Obviously, if the kernel doesn't support locality 2 then the hibernation resume has to accept any old key, but that's the same as the situation today.

This sounds otherwise great to me but why bother even allowing a machine with no-locality TPM to be involved with hibernate? Simply detect locality support during driver initialization and disallow sealed hibernation (or whatever the feature was called) if localities were not detected.

Quoted 5 lines

I get supporting old hardware with old features but it does not make sense to maintain new features with hardware, which clearly does not scale, right?

BR, Jarkko

Here's a thought, what if we had a static/cmd line configurable no-auth NV Index and writelocked it with the expected key information, name or something. I guess the problem is atomicity with write/lock, but can't the kernel lock out all other users?

An attacker would need to issue tpm2_startup, which in this case would DOS the kernel in both scenarios. If an attacker already wrote and locked the NV index, that would also be a DOS. If they already wrote it, the kernel simply writes whatever they want. Is there an attack I am missing?

Quoted 9 lines
Quoted 4 lines

I guess the issue here would be setup, since creating the NV index requires hierarchy auth, does the kernel have platform auth or is that already shut down by firmware (I can't recall)? A null hierarchy volatile lockable index would be nice for this, too bad that doesn't exist.

How do you see this would better when compared to finding a way to use locality, which could potentially be made to somewhat simple to setup (practically zero config)?

Quoted 9 lines

I never said it was better, I said here is a thought for discussion. If we had to support older hardware (I could care less about things that don't support localities, but some might not), this could be an avenue to support them without walling off a PCR. I pointed out the downsides, and argument could be made that when localities is not supported then walling off PCR23 is the better approach if older hardware is an issue. This all hinges on do we care about things that don't support multiple localities. I don't, im for if you have locality support you get the feature else you don't.

Probably does not make much sense to care for this feature.

BR, Jarkko

maintainer
#32 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to William Roberts
Quoted 36 lines · William Roberts
Quoted 35 lines · Jarkko Sakkinen
Quoted 32 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Quoted 8 lines
Quoted 1 line

Kernel could try to use locality 2 and use locality 0 as fallback.

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

I was informed that all 5 localities should be supported starting with Gen 7 Kaby Lake launched in 2016. Don't know if this is still "too new".

What about having opt-in flag that distributions can then enable?

BR, Jarkko

maintainer
#33 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 39 lines · Jarkko Sakkinen
Quoted 38 lines · William Roberts
Quoted 36 lines · Jarkko Sakkinen
Quoted 28 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

Quoted 4 lines

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Kernel could try to use locality 2 and use locality 0 as fallback.

Quoted 8 lines
Quoted 6 lines

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

I was informed that all 5 localities should be supported starting with Gen 7 Kaby Lake launched in 2016. Don't know if this is still "too new".

What about having opt-in flag that distributions can then enable?

This is more intrusive but still worth of consideration: add opt-in kernel command-line flag for no locality. I.e. require locality support unless explicitly stated otherwise.

I'd presume that legacy production cases are a rarity but really is something that is beyond me, and could potentially draw wrong conclusions.

BR, Jarkko

#34 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

Quoted 48 lines · Jarkko Sakkinen
Quoted 46 lines · Jarkko Sakkinen
Quoted 44 lines · William Roberts
Quoted 30 lines · Jarkko Sakkinen
Quoted 28 lines · James Bottomley
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · William Roberts

What's the use case of using the creation data and ticket in this context? Who gets the creationData and the ticket? Could a user supplied outsideInfo work? IIRC I saw some patches flying around where the sessions will get encrypted and presumably correctly as well. This would allow the transfer of that outsideInfo, like the NV Index PCR value to be included and integrity protected by the session HMAC.

Quoted 6 lines

The goal is to ensure that the key was generated by the kernel. In the absence of the creation data, an attacker could generate a hibernation image using their own key and trick the kernel into resuming arbitrary code. We don't have any way to pass secret data from the hibernate kernel to the resume kernel, so I don't think there's any easy way to do it with outsideinfo.

Can we go back again to why you can't use locality? It's exactly designed for this since locality is part of creation data. Currently everything only uses locality 0, so it's impossible for anyone on Linux to produce a key with anything other than 0 in the creation data for locality. However, the dynamic launch people are proposing that the Kernel should use Locality 2 for all its operations, which would allow you to distinguish a key created by the kernel from one created by a user by locality.

Quoted 6 lines
Quoted 4 lines

I think the previous objection was that not all TPMs implement locality, but then not all laptops have TPMs either, so if you ever come across one which has a TPM but no locality, it's in a very similar security boat to one which has no TPM.

Kernel could try to use locality 2 and use locality 0 as fallback.

I don't think that would work for Matthew, they need something reliable to indicate key provenance.

I was informed that all 5 localities should be supported starting with Gen 7 Kaby Lake launched in 2016. Don't know if this is still "too new".

Quoted 9 lines
Quoted 1 line

What about having opt-in flag that distributions can then enable?

This is more intrusive but still worth of consideration: add opt-in kernel command-line flag for no locality. I.e. require locality support unless explicitly stated otherwise.

I'd presume that legacy production cases are a rarity but really is something that is beyond me, and could potentially draw wrong conclusions.

One thing that was never answered for me, is that there was nowhere safe to store some information about the expected key or a secret. That would be the most obvious solution, so I am assuming that's a no.

#35 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to Matthew Garrett
Quoted 11 lines · Matthew Garrett
Quoted 9 lines · James Bottomley
Quoted 2 lines · Evan Green

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled restricts usermode's ability to extend or reset PCR 23.

Could I re ask the question here that I asked of Matthew's patch set:

https://lore.kernel.org/all/[email protected]/

Quoted 8 lines
Quoted 5 lines

Which was could we use an NVRAM index in the TPM instead of a PCR? The reason for asking was that PCRs are rather precious and might get more so now that Lennart has some grand scheme for using more of them in his unified boot project. Matthew promised to play with the idea but never got back to the patch set to say whether he investigated this or not.

Is there any way to get key creation data to include NV indexes? If not, no, we can't use NVRAM.

No theirs not, but there's room for qualifyingData. So some background on this is they use the PCR value to verify it's the right key. Since it's added by the TPM within the trust boundary its an unforgeable value, unlike the qdata.

I think there are better ways to verify it's the right key, i.e. the ability to wield the key, or if you have some ability to remember state, you
could verify the name which is cryptgraphically bound to the private key and thus TPM2_Load will fail. From what I understand they have no ability to remember state as their verifying and executing a resume kernel, but I am not well versed in that area of the kernel. It makes me think that they are checking against a hardcoded known PCR23 state and rolling it to prevent other keys from being generated. I would consider policy locality for controlling who can use the key and couple with policynv if revoking long lasting keys is a need. If they are ephemeral, theirs the NULL hierarchy.

A lot of this is conjecture, as Matthew just stopped responding. Perhaps they are away or busy, but I just wanted to weigh in on this.

maintainer
#36 lore raw

Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

In reply to Evan Green
Quoted 2 lines · Evan Green

Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled restricts usermode's ability to extend or reset PCR 23.

TCG_TPM_KERNEL_PCR would be a more descriptive name, and the description should be less abstract, e.g.

"Introduce TCG_TPM_RESTRICT_PCR to Kconfig. If enabled, filter out TPM2_CC_PCR_{EXTEND, RESET} concerning PCR 23 in tpm_common_write()."

Quoted 11 lines

Under certain circumstances it might be desirable to enable the creation of TPM-backed secrets that are only accessible to the kernel. In an ideal world this could be achieved by using TPM localities, but these don't appear to be available on consumer systems. An alternative is to simply block userland from modifying one of the resettable PCRs, leaving it available to the kernel. If the kernel ensures that no userland can access the TPM while it is carrying out work, it can reset PCR 23, extend it to an arbitrary value, create or load a secret, and then reset the PCR again. Even if userland somehow obtains the sealed material, it will be unable to unseal it since PCR 23 will never be in the appropriate state.

This should be the first paragraph (motivation).

Quoted 84 lines

This Kconfig is only properly supported for systems with TPM2 devices. For systems with TPM1 devices, having this Kconfig enabled completely restricts usermode's access to the TPM. TPM1 contains support for tunnelled transports, which usermode could use to smuggle commands through that this Kconfig is attempting to restrict.

---

Changes in v5:
- Change tags on RESTRICT_PCR patch (Kees)
- Rename to TCG_TPM2_RESTRICT_PCR
- Do nothing on TPM1.2 devices (Jarkko, Doug)

Changes in v4:
- Augment the commit message (Jarkko)

Changes in v3:
- Fix up commit message (Jarkko)
- tpm2_find_and_validate_cc() was split (Jarkko)
- Simply fully restrict TPM1 since v2 failed to account for tunnelled transport sessions (Stefan and Jarkko).

Changes in v2:
- Fixed sparse warnings

 drivers/char/tpm/Kconfig          | 12 ++++++++++++
 drivers/char/tpm/tpm-dev-common.c |  6 ++++++
 drivers/char/tpm/tpm.h            | 12 ++++++++++++
 drivers/char/tpm/tpm2-cmd.c       | 22 ++++++++++++++++++++++
 4 files changed, 52 insertions(+)
drivers/char/tpm/Kconfig +12 -0
index 927088b2c3d3f2..e6d3aa9f6c694f 100644
@@ -211,4 +211,16 @@ config TCG_FTPM_TEE
 	  This driver proxies for firmware TPM running in TEE.
 
 source "drivers/char/tpm/st33zp24/Kconfig"
+
+config TCG_TPM2_RESTRICT_PCR
+	bool "Restrict userland access to PCR 23 on TPM2 devices"
+	depends on TCG_TPM
+	help
+	  If set, block userland from extending or resetting PCR 23 on TPM2.0
+	  and later systems. This allows the PCR to be restricted to in-kernel
+	  use, preventing userland from being able to make use of data sealed to
+	  the TPM by the kernel. This is required for secure hibernation
+	  support, but should be left disabled if any userland may require
+	  access to PCR23. This is a TPM2-only feature, enabling this on a TPM1
+	  machine is effectively a no-op.
 endif # TCG_TPM
drivers/char/tpm/tpm-dev-common.c +6 -0
index dc4c0a0a512903..66d15a2a967443 100644
@@ -198,6 +198,12 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
 	priv->response_read = false;
 	*off = 0;
 
+	if (priv->chip->flags & TPM_CHIP_FLAG_TPM2) {
+		ret = tpm2_cmd_restricted(priv->chip, priv->data_buffer, size);
+		if (ret)
+			goto out;
+	}
+
 	/*
 	 * If in nonblocking mode schedule an async job to send
 	 * the command return the size.
drivers/char/tpm/tpm.h +2 -0
index f1e0f490176f01..7fb746d210f59d 100644
@@ -245,4 +245,16 @@ void tpm_bios_log_setup(struct tpm_chip *chip);
 void tpm_bios_log_teardown(struct tpm_chip *chip);
 int tpm_dev_common_init(void);
 void tpm_dev_common_exit(void);
+
+#ifdef CONFIG_TCG_TPM2_RESTRICT_PCR

drivers/char/tpm/tpm.h void tpm_bios_log_setup(struct tpm_chip *chip); · line 259

Quoted 10 lines
drivers/char/tpm/tpm.h +10 -0
+#define TPM_RESTRICTED_PCR 23
+
+int tpm2_cmd_restricted(struct tpm_chip *chip, u8 *buffer, size_t size);
+#else
+static inline int tpm2_cmd_restricted(struct tpm_chip *chip, u8 *buffer,
+				      size_t size)
+{
+	return 0;
+}
+#endif

Why do you need to export this? That was not discussed in the commit message.

The function name is quite undescriptive IMHO.

Quoted 6 lines
drivers/char/tpm/tpm.h +0 -0
 #endif
drivers/char/tpm/tpm2-cmd.c +0 -0
index 303ce2ea02a4b0..3bc5546fddc792 100644

drivers/char/tpm/tpm2-cmd.c int tpm2_find_cc(struct tpm_chip *chip, u32 cc) · line 785

Quoted 8 lines
drivers/char/tpm/tpm2-cmd.c +5 -0
@@ -778,3 +778,25 @@ int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
 
 	return -1;
 }
+
+#ifdef CONFIG_TCG_TPM2_RESTRICT_PCR
+int tpm2_cmd_restricted(struct tpm_chip *chip, u8 *buffer, size_t size)
+{
+	int cc = tpm2_find_and_validate_cc(chip, NULL, buffer, size);

Please discuss this call in the commit message.

Quoted 20 lines
drivers/char/tpm/tpm2-cmd.c +17 -0
+	__be32 *handle;
+
+	switch (cc) {
+	case TPM2_CC_PCR_EXTEND:
+	case TPM2_CC_PCR_RESET:
+		if (size < (TPM_HEADER_SIZE + sizeof(u32)))
+			return -EINVAL;
+
+		handle = (__be32 *)&buffer[TPM_HEADER_SIZE];
+		if (be32_to_cpu(*handle) == TPM_RESTRICTED_PCR)
+			return -EPERM;
+		break;
+	}
+
+	return 0;
+}
+#endif
Signature · 1 line
2.38.1.431.g37b22c650d-goog

BR, Jarkko

author
#37 lore raw

PATCH v5 4/11 security: keys: trusted: Include TPM2 creation data

In reply to Evan Green

In addition to the private key and public key, the TPM2_Create command may also return creation data, a creation hash, and a creation ticket. These fields allow the TPM to attest to the contents of a specified set of PCRs at the time the trusted key was created. Encrypted hibernation will use this to ensure that PCRs settable only by the kernel were set properly at the time of creation, indicating this is an authentic hibernate key.

Encode these additional parameters into the ASN.1 created to represent the key blob. The new fields are made optional so that they don't bloat key blobs which don't need them, and to ensure interoperability with old blobs.

---

Changes in v5:
- Factored some math out to a helper function (Kees)
- Constified src in tpm2_key_encode().

Changes in v3:
- Fix SoB and -- note ordering (Kees)
- Add comments describing the TPM2 spec type names for the new fields in tpm2key.asn1 (Kees)
- Add len buffer checks in tpm2_key_encode() (Kees)

This is a replacement for Matthew's original patch here:
https://patchwork.kernel.org/patch/12096489/

That patch was written before the exported key format was switched to ASN.1. This patch accomplishes the same thing (saving, loading, and getting pointers to the creation data) while utilizing the new ASN.1 format.

---
 include/keys/trusted-type.h               |   8 +
 security/keys/trusted-keys/tpm2key.asn1   |  15 +-
 security/keys/trusted-keys/trusted_tpm2.c | 253 +++++++++++++++++++---
 3 files changed, 245 insertions(+), 31 deletions(-)
include/keys/trusted-type.h +8 -0
index 4eb64548a74f1a..209086fed240a5 100644
@@ -22,15 +22,23 @@
 #define MAX_BLOB_SIZE			512
 #define MAX_PCRINFO_SIZE		64
 #define MAX_DIGEST_SIZE			64
+#define MAX_CREATION_DATA		412
+#define MAX_TK				76
 
 struct trusted_key_payload {
 	struct rcu_head rcu;
 	unsigned int key_len;
 	unsigned int blob_len;
+	unsigned int creation_len;
+	unsigned int creation_hash_len;
+	unsigned int tk_len;
 	unsigned char migratable;
 	unsigned char old_format;
 	unsigned char key[MAX_KEY_SIZE + 1];
 	unsigned char blob[MAX_BLOB_SIZE];
+	unsigned char *creation;
+	unsigned char *creation_hash;
+	unsigned char *tk;
 };
 
 struct trusted_key_options {
security/keys/trusted-keys/tpm2key.asn1 +14 -1
index f57f869ad60068..608f8d9ca95fa8 100644
@@ -7,5 +7,18 @@ TPMKey ::= SEQUENCE {
 	emptyAuth	[0] EXPLICIT BOOLEAN OPTIONAL,
 	parent		INTEGER ({tpm2_key_parent}),
 	pubkey		OCTET STRING ({tpm2_key_pub}),
-	privkey		OCTET STRING ({tpm2_key_priv})
+	privkey		OCTET STRING ({tpm2_key_priv}),
+	---
+	--- A TPM2B_CREATION_DATA struct as returned from the TPM2_Create command.
+	---
+	creationData	[1] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_creation_data}),
+	---
+	--- A TPM2B_DIGEST of the creationHash as returned from the TPM2_Create
+	--- command.
+	---
+	creationHash	[2] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_creation_hash}),
+	---
+	--- A TPMT_TK_CREATION ticket as returned from the TPM2_Create command.
+	---
+	creationTk	[3] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_creation_tk})
 	}
security/keys/trusted-keys/trusted_tpm2.c +223 -30

355 lines not on this page · raw message

Signature · 1 line
2.38.1.431.g37b22c650d-goog
#38 lore raw

Re: [PATCH v5 04/11] security: keys: trusted: Include TPM2 creation data

Quoted 14 lines · Evan Green
security/keys/trusted-keys/tpm2key.asn1 +5 -1
index f57f869ad60068..608f8d9ca95fa8 100644
@@ -7,5 +7,18 @@ TPMKey ::= SEQUENCE {
 	emptyAuth	[0] EXPLICIT BOOLEAN OPTIONAL,
 	parent		INTEGER ({tpm2_key_parent}),
 	pubkey		OCTET STRING ({tpm2_key_pub}),
-	privkey		OCTET STRING ({tpm2_key_priv})
+	privkey		OCTET STRING ({tpm2_key_priv}),
+	---
+	--- A TPM2B_CREATION_DATA struct as returned from the TPM2_Create command.
+	---
+	creationData	[1] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_creation_data}),

security/keys/trusted-keys/tpm2key.asn1 TPMKey ::= SEQUENCE { · line 24

Quoted 10 lines
security/keys/trusted-keys/tpm2key.asn1 +9 -0
+	---
+	--- A TPM2B_DIGEST of the creationHash as returned from the TPM2_Create
+	--- command.
+	---
+	creationHash	[2] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_creation_hash}),
+	---
+	--- A TPMT_TK_CREATION ticket as returned from the TPM2_Create command.
+	---
+	creationTk	[3] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_creation_tk})
 	}

The commit that added this file claimed:

"The benefit of the ASN.1 format is that it's a standard and thus the exported key can be used by userspace tools (openssl_tpm2_engine, openconnect and tpm2-tss-engine"

Are these new fields in compliance with whatever standard that was referring to?

Or was that just referring to ASN.1 itself?

security/keys/trusted-keys/tpm2key.asn1

Quoted 7 lines
security/keys/trusted-keys/tpm2key.asn1 +7 -0
+/* Helper function to advance past a __be16 length + buffer safely */
+static const u8 *get_sized_section(const u8 *src, const u8 *end, u16 *len)
+{
+	u32 length;
+
+	if (src + sizeof(u16) > end)
+		return NULL;

'end - src < sizeof(u16)', so the pointer isn't advanced past the end.

security/keys/trusted-keys/tpm2key.asn1

Quoted 12 lines
security/keys/trusted-keys/tpm2key.asn1 +12 -0
+
+	/* Include the size field in the returned section length. */
+	length = get_unaligned_be16(src) + sizeof(u16);
+	*len = length;
+	if (*len != length)
+		return NULL;
+
+	src += *len;
+	if (src > end)
+		return NULL;
+
+	return src;

Similarly:

	if (end - src < *len)
		return NULL;

	return src + *len;

security/keys/trusted-keys/tpm2key.asn1

Quoted 6 lines
security/keys/trusted-keys/tpm2key.asn1 +6 -0
+		/*
+		 * The creation ticket (TPMT_TK_CREATION) consists of a 2 byte
+		 * tag, 4 byte handle, and then a TPM2B_DIGEST, which is a 2
+		 * byte length followed by data.
+		 */
+		if (src + 8 > end)

end - src < 8

And actually it really should be 6 instead of 8, to match the code below. get_sized_section() already validates that there are at least 2 more bytes.

Quoted 29 lines
security/keys/trusted-keys/tpm2key.asn1 +17 -3
+			return -EINVAL;
+
+		creation_tk = src;
+		src = get_sized_section(src + 6, end, &creation_tk_len);
+		if (!src)
+			return -EINVAL;
+
+		creation_tk_len += 6;
+
+	} else {
+		creation_data_len = 0;
+		creation_data = NULL;
+	}
 
 	if (!scratch)
 		return -ENOMEM;
@@ -63,26 +125,81 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
 	}
 
 	/*
-	 * Assume both octet strings will encode to a 2 byte definite length
+	 * Assume each octet string will encode to a 2 byte definite length.
+	 * Each optional octet string consumes one extra byte.
 	 *
-	 * Note: For a well behaved TPM, this warning should never
-	 * trigger, so if it does there's something nefarious going on
+	 * Note: For a well behaved TPM, this warning should never trigger, so
+	 * if it does there's something nefarious going on
 	 */

security/keys/trusted-keys/tpm2key.asn1 static int tpm2_key_encode(struct trusted_key_payload *payload, · line 140

Quoted 10 lines
security/keys/trusted-keys/tpm2key.asn1 +7 -3
-	if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
-		 "BUG: scratch buffer is too small"))
-		return -EINVAL;
+	if (WARN(work - scratch + pub_len + priv_len + creation_data_len +
+		 creation_hash_len + creation_tk_len + (7 * 5) + 3 >
+		 SCRATCH_SIZE,
+		 "BUG: scratch buffer is too small")) {
+		rc = -EINVAL;
+		goto err;
+	}

This appears to be fixing a memory leak in the error case.

The same memory leak also still appears above in:

	if (WARN(IS_ERR(w), "BUG: Boolean failed to encode"))
		return PTR_ERR(w);

Maybe both should be fixed in a separate patch.

security/keys/trusted-keys/tpm2key.asn1 static int tpm2_key_encode(struct trusted_key_payload *payload, · line 150

Quoted 10 lines
security/keys/trusted-keys/tpm2key.asn1 +10 -0
+		work2 = asn1_encode_octet_string(scratch2,
+						 end_work2,
+						 creation_data,
+						 creation_data_len);
+
+		work = asn1_encode_tag(work,
+				       end_work,
+				       1,
+				       scratch2,
+				       work2 - scratch2);

There's no helper function to do these two steps together?

security/keys/trusted-keys/tpm2key.asn1 static int tpm2_key_encode(struct trusted_key_payload *payload, · line 160

Quoted 12 lines
security/keys/trusted-keys/tpm2key.asn1 +8 -2
+
-	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
-		return PTR_ERR(work1);
+	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed")) {
+		rc = PTR_ERR(work1);
+		goto err;
+	}
 
 	return work1 - payload->blob;
+err:
+	kfree(scratch);
+	return rc;

Is this another memory leak fix that is unrelated to the functionality added by this patch?

Also, isn't 'scratch' still being leaked in the success case?

Quoted 14 lines
security/keys/trusted-keys/tpm2key.asn1 +3 -3
 static int tpm2_key_decode(struct trusted_key_payload *payload,
-			   struct trusted_key_options *options,
-			   u8 **buf)
+			   struct trusted_key_options *options)
 {
+	u64 data_len;
 	int ret;
 	struct tpm2_key_context ctx;
-	u8 *blob;
+	u8 *blob, *buf;
 
 	memset(&ctx, 0, sizeof(ctx));
 

security/keys/trusted-keys/tpm2key.asn1 static int tpm2_key_decode(struct trusted_key_payload *payload, · line 235

Quoted 6 lines
security/keys/trusted-keys/tpm2key.asn1 +2 -1
@@ -108,21 +231,57 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,
 	if (ret < 0)
 		return ret;
 
-	if (ctx.priv_len + ctx.pub_len > MAX_BLOB_SIZE)
+	data_len = ctx.priv_len + ctx.pub_len + ctx.creation_data_len +
+		   ctx.creation_hash_len + ctx.creation_tk_len;

It's unclear why 'data_len' is a u64, given that the value assigned to it always fits in a u32. Perhaps you intended to do the additions with 64-bit numbers so that they can't overflow.

But shouldn't the lengths already be bounded by size of the ASN.1 blob before even reaching here, anyway?

security/keys/trusted-keys/tpm2key.asn1 static int tpm2_key_decode(struct trusted_key_payload *payload, · line 242

Quoted 9 lines
security/keys/trusted-keys/tpm2key.asn1 +4 -2
+
+	if (data_len > MAX_BLOB_SIZE)
 		return -EINVAL;
 
-	blob = kmalloc(ctx.priv_len + ctx.pub_len + 4, GFP_KERNEL);
-	if (!blob)
+	buf = kmalloc(data_len + 4, GFP_KERNEL);
+	if (!buf)
 		return -ENOMEM;

It's unclear what the '+ 4' is for.

Quoted 13 lines
security/keys/trusted-keys/tpm2key.asn1 +1 -1
@@ -229,6 +424,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 		      struct trusted_key_options *options)
 {
 	int blob_len = 0;
+	unsigned int offset;
 	struct tpm_buf buf;
 	u32 hash;
 	u32 flags;
@@ -317,13 +513,14 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 		rc = -E2BIG;
 		goto out;
 	}
-	if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) {

security/keys/trusted-keys/tpm2key.asn1 int tpm2_seal_trusted(struct tpm_chip *chip, · line 524

Quoted 10 lines
security/keys/trusted-keys/tpm2key.asn1 +3 -1
+	offset = TPM_HEADER_SIZE + 4;
+	if (tpm_buf_length(&buf) < offset + blob_len) {
 		rc = -EFAULT;
 		goto out;
 	}
 
 	blob_len = tpm2_key_encode(payload, options,
-				   &buf.data[TPM_HEADER_SIZE + 4],
+				   &buf.data[offset],
 				   blob_len);

This hunk of the patch doesn't seem to serve any purpose.

- Eric

#39 lore raw

Re: [PATCH v5 04/11] security: keys: trusted: Include TPM2 creation data

Quoted 35 lines · Eric Biggers
Quoted 23 lines · Evan Green
security/keys/trusted-keys/tpm2key.asn1 +3 -1
index f57f869ad60068..608f8d9ca95fa8 100644
@@ -7,5 +7,18 @@ TPMKey ::= SEQUENCE {
         emptyAuth       [0] EXPLICIT BOOLEAN OPTIONAL,
         parent          INTEGER ({tpm2_key_parent}),
         pubkey          OCTET STRING ({tpm2_key_pub}),
-       privkey         OCTET STRING ({tpm2_key_priv})
+       privkey         OCTET STRING ({tpm2_key_priv}),
+       ---
+       --- A TPM2B_CREATION_DATA struct as returned from the

TPM2_Create command.
+       ---
+       creationData    [1] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_creation_data}),
+       ---
+       --- A TPM2B_DIGEST of the creationHash as returned from the TPM2_Create
+       --- command.
+       ---
+       creationHash    [2] EXPLICIT OCTET STRING OPTIONAL

Quoted 8 lines

({tpm2_key_creation_hash}),
+       ---
+       --- A TPMT_TK_CREATION ticket as returned from the TPM2_Create command.
+       ---
+       creationTk      [3] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_creation_tk})
        }

The commit that added this file claimed:

Quoted 8 lines

        "The benefit of the ASN.1 format is that it's a standard and thus the
        exported key can be used by userspace tools (openssl_tpm2_engine,
        openconnect and tpm2-tss-engine"

Are these new fields in compliance with whatever standard that was referring to?

Not really, no. The current use case (and draft standard) is already using [1] for policies and [2] for importable keys:

https://git.kernel.org/pub/scm/linux/kernel/git/jejb/openssl_tpm2_engine.git/tree/doc/draft-bottomley-tpm2-keys.xml

I'm actually planning to use [3] for signed policies. There's no reason why you can't use [4] though. Since the creation data, hash and ticket are likely used as a job lot, it strikes me they should be a single numbered optional sequence instead of individually numbered, since you're unlikely to have one without the others.

James

author
#40 lore raw

Re: [PATCH v5 04/11] security: keys: trusted: Include TPM2 creation data

Quoted 49 lines · James Bottomley
Quoted 35 lines · Eric Biggers
Quoted 30 lines · Evan Green
security/keys/trusted-keys/tpm2key.asn1 +3 -1
index f57f869ad60068..608f8d9ca95fa8 100644
@@ -7,5 +7,18 @@ TPMKey ::= SEQUENCE {
        emptyAuth       [0] EXPLICIT BOOLEAN OPTIONAL,
        parent          INTEGER ({tpm2_key_parent}),
        pubkey          OCTET STRING ({tpm2_key_pub}),
-       privkey         OCTET STRING ({tpm2_key_priv})
+       privkey         OCTET STRING ({tpm2_key_priv}),
+       ---
+       --- A TPM2B_CREATION_DATA struct as returned from the

TPM2_Create command.

security/keys/trusted-keys/tpm2key.asn1 +2 -0
+       ---
+       creationData    [1] EXPLICIT OCTET STRING OPTIONAL

({tpm2_key_creation_data}),

security/keys/trusted-keys/tpm2key.asn1 +2 -0
+       ---
+       --- A TPM2B_DIGEST of the creationHash as returned from the

TPM2_Create

security/keys/trusted-keys/tpm2key.asn1 +3 -0
+       --- command.
+       ---
+       creationHash    [2] EXPLICIT OCTET STRING OPTIONAL

({tpm2_key_creation_hash}),

security/keys/trusted-keys/tpm2key.asn1 +2 -0
+       ---
+       --- A TPMT_TK_CREATION ticket as returned from the

TPM2_Create command.

security/keys/trusted-keys/tpm2key.asn1 +2 -0
+       ---
+       creationTk      [3] EXPLICIT OCTET STRING OPTIONAL

({tpm2_key_creation_tk})

Quoted 1 line
        }

The commit that added this file claimed:

Quoted 8 lines

"The benefit of the ASN.1 format is that it's a standard and
thus the
exported key can be used by userspace tools
(openssl_tpm2_engine,
openconnect and tpm2-tss-engine"

Are these new fields in compliance with whatever standard that was referring to?

Not really, no. The current use case (and draft standard) is already using [1] for policies and [2] for importable keys:

Quoted 7 lines

https://git.kernel.org/pub/scm/linux/kernel/git/jejb/openssl_tpm2_engine.git/tree/doc/draft-bottomley-tpm2-keys.xml

I'm actually planning to use [3] for signed policies. There's no reason why you can't use [4] though. Since the creation data, hash and ticket are likely used as a job lot, it strikes me they should be a single numbered optional sequence instead of individually numbered, since you're unlikely to have one without the others.

Thanks, I was hoping James might pipe up and tell me what to do. Grouping them as a single numbered optional sequence sounds reasonable to me. Is your draft too far along to squeeze this in? If it is and I'm on my own to draft up and submit this, I would definitely appreciate any pointers on getting started you might have.

I notice the draft and the code seem to be out of alignment. I'm unfamiliar with this process, is the idea to get through all the iterations and land the standard, then fix up the code? What happens to existing data handed out in the old format?

-Evan

#41 lore raw

Re: [PATCH v5 04/11] security: keys: trusted: Include TPM2 creation data

Quoted 60 lines · Evan Green
Quoted 50 lines · James Bottomley
Quoted 36 lines · Eric Biggers
Quoted 24 lines · Evan Green
security/keys/trusted-keys/tpm2key.asn1 +3 -1
index f57f869ad60068..608f8d9ca95fa8 100644
@@ -7,5 +7,18 @@ TPMKey ::= SEQUENCE {
         emptyAuth       [0] EXPLICIT BOOLEAN OPTIONAL,
         parent          INTEGER ({tpm2_key_parent}),
         pubkey          OCTET STRING ({tpm2_key_pub}),
-       privkey         OCTET STRING ({tpm2_key_priv})
+       privkey         OCTET STRING ({tpm2_key_priv}),
+       ---
+       --- A TPM2B_CREATION_DATA struct as returned from the

TPM2_Create command.
+       ---
+       creationData    [1] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_creation_data}),
+       ---
+       --- A TPM2B_DIGEST of the creationHash as returned from the
TPM2_Create
+       --- command.
+       ---
+       creationHash    [2] EXPLICIT OCTET STRING OPTIONAL

Quoted 8 lines

({tpm2_key_creation_hash}),
+       ---
+       --- A TPMT_TK_CREATION ticket as returned from the TPM2_Create command.
+       ---
+       creationTk      [3] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_creation_tk})
        }

The commit that added this file claimed:

Quoted 8 lines

        "The benefit of the ASN.1 format is that it's a standard and thus the
        exported key can be used by userspace tools (openssl_tpm2_engine,
        openconnect and tpm2-tss-engine"

Are these new fields in compliance with whatever standard that was referring to?

Not really, no.  The current use case (and draft standard) is already using [1] for policies and [2] for importable keys:

Quoted 7 lines

https://git.kernel.org/pub/scm/linux/kernel/git/jejb/openssl_tpm2_engine.git/tree/doc/draft-bottomley-tpm2-keys.xml

I'm actually planning to use [3] for signed policies.  There's no reason why you can't use [4] though.  Since the creation data, hash and ticket are likely used as a job lot, it strikes me they should be a single numbered optional sequence instead of individually numbered, since you're unlikely to have one without the others.

Quoted 3 lines

Thanks, I was hoping James might pipe up and tell me what to do. Grouping them as a single numbered optional sequence sounds reasonable to me. Is your draft too far along to squeeze this in?

Not at all. The draft only becomes frozen once I submit it to the IETF which, so far thanks to lack of any reviewers I haven't done (That's why I was also thinking of adding signed policies).

Quoted 4 lines

If it is and I'm on my own to draft up and submit this, I would
definitely appreciate any pointers on getting started you might have.

I notice the draft and the code seem to be out of alignment.

The kernel code is out of alignment just because development moves a bit slowly. Policy based keys were submitted a long time ago as part of the original move to interoperable sealed keys based on ASN.1:

https://lore.kernel.org/all/[email protected]/

But eventually the policy part was split out and forgotten about. I think the only complete implementation of the draft standard is the openssl_tpm2_engine.

Quoted 3 lines

I'm unfamiliar with this process, is the idea to get through all the
iterations and land the standard, then fix up the code? What happens to existing data handed out in the old format?

No, it doesn't matter at all. That's the whole point of using ASN.1 explicit optionals: the ASN.1 is always backwards compatible. If I ever submit the draft, there'll have to be a new RFC to add new explicit optionals, but keys conforming to the old RFC will still be valid under the new one.

Of course, since openssl_tpm2_engine is the complete reference implementation that means I'll have to add the creation PCRs implementation to it ... unless you'd like to do it?

Regards,

James

author
#42 lore raw

Re: [PATCH v5 04/11] security: keys: trusted: Include TPM2 creation data

Quoted 89 lines · James Bottomley
Quoted 60 lines · Evan Green
Quoted 50 lines · James Bottomley
Quoted 36 lines · Eric Biggers
Quoted 31 lines · Evan Green
security/keys/trusted-keys/tpm2key.asn1 +3 -1
index f57f869ad60068..608f8d9ca95fa8 100644
@@ -7,5 +7,18 @@ TPMKey ::= SEQUENCE {
        emptyAuth       [0] EXPLICIT BOOLEAN OPTIONAL,
        parent          INTEGER ({tpm2_key_parent}),
        pubkey          OCTET STRING ({tpm2_key_pub}),
-       privkey         OCTET STRING ({tpm2_key_priv})
+       privkey         OCTET STRING ({tpm2_key_priv}),
+       ---
+       --- A TPM2B_CREATION_DATA struct as returned from the

TPM2_Create command.

security/keys/trusted-keys/tpm2key.asn1 +2 -0
+       ---
+       creationData    [1] EXPLICIT OCTET STRING OPTIONAL

({tpm2_key_creation_data}),

security/keys/trusted-keys/tpm2key.asn1 +2 -0
+       ---
+       --- A TPM2B_DIGEST of the creationHash as returned from

the
TPM2_Create

security/keys/trusted-keys/tpm2key.asn1 +3 -0
+       --- command.
+       ---
+       creationHash    [2] EXPLICIT OCTET STRING OPTIONAL

({tpm2_key_creation_hash}),

security/keys/trusted-keys/tpm2key.asn1 +2 -0
+       ---
+       --- A TPMT_TK_CREATION ticket as returned from the

TPM2_Create command.

security/keys/trusted-keys/tpm2key.asn1 +2 -0
+       ---
+       creationTk      [3] EXPLICIT OCTET STRING OPTIONAL

({tpm2_key_creation_tk})

Quoted 1 line
        }

The commit that added this file claimed:

Quoted 8 lines

"The benefit of the ASN.1 format is that it's a standard
and thus the
exported key can be used by userspace tools
(openssl_tpm2_engine,
openconnect and tpm2-tss-engine"

Are these new fields in compliance with whatever standard that was referring to?

Not really, no. The current use case (and draft standard) is already using [1] for policies and [2] for importable keys:

Quoted 7 lines

https://git.kernel.org/pub/scm/linux/kernel/git/jejb/openssl_tpm2_engine.git/tree/doc/draft-bottomley-tpm2-keys.xml

I'm actually planning to use [3] for signed policies. There's no reason why you can't use [4] though. Since the creation data, hash and ticket are likely used as a job lot, it strikes me they should be a single numbered optional sequence instead of individually numbered, since you're unlikely to have one without the others.

Quoted 3 lines

Thanks, I was hoping James might pipe up and tell me what to do. Grouping them as a single numbered optional sequence sounds reasonable to me. Is your draft too far along to squeeze this in?

Not at all. The draft only becomes frozen once I submit it to the IETF which, so far thanks to lack of any reviewers I haven't done (That's why I was also thinking of adding signed policies).

Quoted 4 lines

If it is and I'm on my own to draft up and submit this, I would
definitely appreciate any pointers on getting started you might have.

I notice the draft and the code seem to be out of alignment.

The kernel code is out of alignment just because development moves a bit slowly. Policy based keys were submitted a long time ago as part of the original move to interoperable sealed keys based on ASN.1:

https://lore.kernel.org/all/[email protected]/

But eventually the policy part was split out and forgotten about. I think the only complete implementation of the draft standard is the openssl_tpm2_engine.

Quoted 3 lines

I'm unfamiliar with this process, is the idea to get through all the
iterations and land the standard, then fix up the code? What happens to existing data handed out in the old format?

Quoted 5 lines

No, it doesn't matter at all. That's the whole point of using ASN.1 explicit optionals: the ASN.1 is always backwards compatible. If I ever submit the draft, there'll have to be a new RFC to add new explicit optionals, but keys conforming to the old RFC will still be valid under the new one.

Ah I see, with the optionals in mind things do line up again.

Quoted 4 lines

Of course, since openssl_tpm2_engine is the complete reference implementation that means I'll have to add the creation PCRs implementation to it ... unless you'd like to do it?

I am willing to help as I'm the one making the mess. How does it sequence along with your draft submission (before, after, simultaneous)?

#43 lore raw

Re: [PATCH v5 04/11] security: keys: trusted: Include TPM2 creation data

Quoted 2 lines · Evan Green

On Mon, Nov 14, 2022 at 8:56 AM James Bottomley <[email protected]> wrote:

[...]

Quoted 7 lines
Quoted 3 lines

Of course, since openssl_tpm2_engine is the complete reference implementation that means I'll have to add the creation PCRs implementation to it ... unless you'd like to do it?

I am willing to help as I'm the one making the mess. How does it sequence along with your draft submission (before, after, simultaneous)?

At the moment, just send patches. The openssl_tpm2_engine is developed on a groups.io mailing list:

https://groups.io/g/openssl-tpm2-engine/

You need an IETF specific tool (xml2rfc) to build the rfc from the xml, but it's available in most distros as python3-xml2rfc. If you don't want to learn the IETF XML I can help you code up the patch to add that to the draft spec.

Regards,

James

#44 lore raw

Re: [PATCH v5 04/11] security: keys: trusted: Include TPM2 creation data

Quoted 16 lines · James Bottomley
Quoted 3 lines · Evan Green

On Mon, Nov 14, 2022 at 8:56 AM James Bottomley
<[email protected]>
wrote:

[...]

Quoted 7 lines
Quoted 3 lines

Of course, since openssl_tpm2_engine is the complete reference implementation that means I'll have to add the creation PCRs implementation to it ... unless you'd like to do it?

I am willing to help as I'm the one making the mess. How does it sequence along with your draft submission (before, after, simultaneous)?

At the moment, just send patches.  The openssl_tpm2_engine is developed on a groups.io mailing list:

Quoted 6 lines

https://groups.io/g/openssl-tpm2-engine/

You need an IETF specific tool (xml2rfc) to build the rfc from the xml, but it's available in most distros as python3-xml2rfc.  If you don't want to learn the IETF XML I can help you code up the patch to add that to the draft spec.

Just as a heads up, the patch series implementing signed policy (and thus taking option [3]) is on the mailing list for review:

https://groups.io/g/openssl-tpm2-engine/message/296

With apologies for the awful lack of threading in the groups.io interface.

So you don't have to build the RFC yourself, I published the proposed update on my website:

https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.txt

If you want to use option [4] for the creation data, it's available.

Regards,

James

author
#45 lore raw

Re: [PATCH v5 04/11] security: keys: trusted: Include TPM2 creation data

Quoted 33 lines · James Bottomley
Quoted 16 lines · James Bottomley
Quoted 3 lines · Evan Green

On Mon, Nov 14, 2022 at 8:56 AM James Bottomley
<[email protected]>
wrote:

[...]

Quoted 7 lines
Quoted 3 lines

Of course, since openssl_tpm2_engine is the complete reference implementation that means I'll have to add the creation PCRs implementation to it ... unless you'd like to do it?

I am willing to help as I'm the one making the mess. How does it sequence along with your draft submission (before, after, simultaneous)?

At the moment, just send patches. The openssl_tpm2_engine is developed on a groups.io mailing list:

Quoted 6 lines

https://groups.io/g/openssl-tpm2-engine/

You need an IETF specific tool (xml2rfc) to build the rfc from the xml, but it's available in most distros as python3-xml2rfc. If you don't want to learn the IETF XML I can help you code up the patch to add that to the draft spec.

Just as a heads up, the patch series implementing signed policy (and thus taking option [3]) is on the mailing list for review:

https://groups.io/g/openssl-tpm2-engine/message/296

With apologies for the awful lack of threading in the groups.io interface.

Quoted 7 lines

So you don't have to build the RFC yourself, I published the proposed update on my website:

https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.txt

If you want to use option [4] for the creation data, it's available.

Perfect, thanks James!
-Evan

author
#46 lore raw

PATCH v5 5/11 security: keys: trusted: Allow storage of PCR values in creation data

In reply to Evan Green

From: Matthew Garrett <[email protected]>

When TPMs generate keys, they can also generate some information describing the state of the PCRs at creation time. This data can then later be certified by the TPM, allowing verification of the PCR values. This allows us to determine the state of the system at the time a key was generated. Add an additional argument to the trusted key creation options, allowing the user to provide the set of PCRs that should have their values incorporated into the creation data.

---

Changes in v5:
- Make Matthew's tag match author

Changes in v3:
- Clarified creationpcrs documentation (Ben)

 .../security/keys/trusted-encrypted.rst       |  6 +++++
 include/keys/trusted-type.h                   |  1 +
 security/keys/trusted-keys/trusted_tpm1.c     |  9 +++++++
 security/keys/trusted-keys/trusted_tpm2.c     | 25 +++++++++++++++++--
 4 files changed, 39 insertions(+), 2 deletions(-)
Documentation/security/keys/trusted-encrypted.rst +6 -0
index 9bc9db8ec6517c..a1872964fe862f 100644
@@ -199,6 +199,12 @@ Usage::
        policyhandle= handle to an authorization policy session that defines the
                      same policy and with the same hash algorithm as was used to
                      seal the key.
+       creationpcrs= hex integer representing the set of PCRs to be
+                     included in the creation data. For each bit set, the
+                     corresponding PCR will be included in the key creation
+                     data. Bit 0 corresponds to PCR0. Currently only the first
+                     PC standard 24 PCRs are supported on the currently active
+                     bank. Leading zeroes are optional. TPM2 only.
 
 "keyctl print" returns an ascii hex copy of the sealed key, which is in standard
 TPM_STORED_DATA format.  The key length for new keys are always in bytes.
include/keys/trusted-type.h +1 -0
index 209086fed240a5..8523d41507b2a4 100644
@@ -54,6 +54,7 @@ struct trusted_key_options {
 	uint32_t policydigest_len;
 	unsigned char policydigest[MAX_DIGEST_SIZE];
 	uint32_t policyhandle;
+	uint32_t creation_pcrs;
 };
 
 struct trusted_key_ops {
security/keys/trusted-keys/trusted_tpm1.c +9 -0
index aa108bea6739b3..2975827c01bec0 100644
@@ -713,6 +713,7 @@ enum {
 	Opt_hash,
 	Opt_policydigest,
 	Opt_policyhandle,
+	Opt_creationpcrs,
 };
 
 static const match_table_t key_tokens = {
@@ -725,6 +726,7 @@ static const match_table_t key_tokens = {
 	{Opt_hash, "hash=%s"},
 	{Opt_policydigest, "policydigest=%s"},
 	{Opt_policyhandle, "policyhandle=%s"},
+	{Opt_creationpcrs, "creationpcrs=%s"},
 	{Opt_err, NULL}
 };
 
@@ -858,6 +860,13 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 				return -EINVAL;
 			opt->policyhandle = handle;
 			break;
+		case Opt_creationpcrs:
+			if (!tpm2)
+				return -EINVAL;
+			res = kstrtoint(args[0].from, 16, &opt->creation_pcrs);
+			if (res < 0)
+				return -EINVAL;
+			break;
 		default:
 			return -EINVAL;
 		}
security/keys/trusted-keys/trusted_tpm2.c +23 -2
index ff2aede8986236..3d84c3d41bdee1 100644
@@ -428,7 +428,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	struct tpm_buf buf;
 	u32 hash;
 	u32 flags;
-	int i;
+	int i, j;
 	int rc;
 
 	for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
@@ -497,7 +497,28 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	tpm_buf_append_u16(&buf, 0);
 
 	/* creation PCR */
-	tpm_buf_append_u32(&buf, 0);
+	if (options->creation_pcrs) {
+		/* One bank */
+		tpm_buf_append_u32(&buf, 1);
+		/* Which bank to use */
+		tpm_buf_append_u16(&buf, hash);
+		/* Length of the PCR bitmask */
+		tpm_buf_append_u8(&buf, 3);
+		/* PCR bitmask */
+		for (i = 0; i < 3; i++) {
+			char tmp = 0;
+
+			for (j = 0; j < 8; j++) {
+				char bit = (i * 8) + j;
+
+				if (options->creation_pcrs & (1 << bit))
+					tmp |= (1 << j);
+			}
+			tpm_buf_append_u8(&buf, tmp);
+		}
+	} else {
+		tpm_buf_append_u32(&buf, 0);
+	}
 
 	if (buf.flags & TPM_BUF_OVERFLOW) {
 		rc = -E2BIG;
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#47 lore raw

Re: [PATCH v5 05/11] security: keys: trusted: Allow storage of PCR values in creation data

Documentation/security/keys/trusted-encrypted.rst Usage:: · line 207

Quoted 6 lines · Evan Green
Documentation/security/keys/trusted-encrypted.rst +6 -0
+       creationpcrs= hex integer representing the set of PCRs to be
+                     included in the creation data. For each bit set, the
+                     corresponding PCR will be included in the key creation
+                     data. Bit 0 corresponds to PCR0. Currently only the first
+                     PC standard 24 PCRs are supported on the currently active
+                     bank. Leading zeroes are optional. TPM2 only.

What does "currently active bank" mean?

security/keys/trusted-keys/trusted_tpm2.c int tpm2_seal_trusted(struct tpm_chip *chip, · line 518

Quoted 12 lines
security/keys/trusted-keys/trusted_tpm2.c +12 -0
+		/* PCR bitmask */
+		for (i = 0; i < 3; i++) {
+			char tmp = 0;
+
+			for (j = 0; j < 8; j++) {
+				char bit = (i * 8) + j;
+
+				if (options->creation_pcrs & (1 << bit))
+					tmp |= (1 << j);
+			}
+			tpm_buf_append_u8(&buf, tmp);
+		}

Why not just:

	tpm_buf_append_u8(&buf, options->creation_pcrs);
	tpm_buf_append_u8(&buf, options->creation_pcrs >> 8);
	tpm_buf_append_u8(&buf, options->creation_pcrs >> 16);

Also what if bit 24 or above is set? Should an error be returned?

- Eric

author
#48 lore raw

PATCH v5 6/11 security: keys: trusted: Verify creation data

In reply to Evan Green

If a loaded key contains creation data, ask the TPM to verify that creation data. This allows users like encrypted hibernate to know that the loaded and parsed creation data has not been tampered with.

---
Source material for this change is at:
https://patchwork.kernel.org/project/linux-pm/patch/[email protected]/

(no changes since v3)

Changes in v3:
- Changed funky tag to suggested-by (Kees). Matthew, holler if you want something different.

Changes in v2:
- Adjust hash len by 2 due to new ASN.1 storage, and add underflow check.

 include/linux/tpm.h                       |  1 +
 security/keys/trusted-keys/trusted_tpm2.c | 77 ++++++++++++++++++++++-
 2 files changed, 77 insertions(+), 1 deletion(-)
include/linux/tpm.h +1 -0
index 70134e6551745f..9c2ee3e30ffa5d 100644
@@ -224,6 +224,7 @@ enum tpm2_command_codes {
 	TPM2_CC_SELF_TEST	        = 0x0143,
 	TPM2_CC_STARTUP		        = 0x0144,
 	TPM2_CC_SHUTDOWN	        = 0x0145,
+	TPM2_CC_CERTIFYCREATION	        = 0x014A,
 	TPM2_CC_NV_READ                 = 0x014E,
 	TPM2_CC_CREATE		        = 0x0153,
 	TPM2_CC_LOAD		        = 0x0157,
security/keys/trusted-keys/trusted_tpm2.c +76 -1

90 lines not on this page · raw message

Signature · 1 line
2.38.1.431.g37b22c650d-goog
#49 lore raw

Re: [PATCH v5 06/11] security: keys: trusted: Verify creation data

Quoted 5 lines · Evan Green

security: keys: trusted: Verify creation data

If a loaded key contains creation data, ask the TPM to verify that creation data. This allows users like encrypted hibernate to know that the loaded and parsed creation data has not been tampered with.

I don't understand what the purpose of this is.

I thought that the way to "seal" a key to a TPM PCR is to include the PCR in the "policy".

Are you doing that too? What is the purpose of using the "creation data"?

security/keys/trusted-keys/trusted_tpm2.c static int tpm2_unseal_cmd(struct tpm_chip *chip, · line 767

Quoted 6 lines
security/keys/trusted-keys/trusted_tpm2.c +6 -0
+	/* Auth */
+	tpm_buf_append_u32(&buf, 9);
+	tpm_buf_append_u32(&buf, TPM2_RS_PW);
+	tpm_buf_append_u16(&buf, 0);
+	tpm_buf_append_u8(&buf, 0);
+	tpm_buf_append_u16(&buf, 0);

This is struct tpm2_null_auth_area, so this is another place that could take advantage of a new helper function to append it.

security/keys/trusted-keys/trusted_tpm2.c static int tpm2_unseal_cmd(struct tpm_chip *chip, · line 780

Quoted 9 lines
security/keys/trusted-keys/trusted_tpm2.c +9 -0
+	/* Creation data hash */
+	if (payload->creation_hash_len < 2) {
+		rc = -EINVAL;
+		goto out;
+	}
+
+	tpm_buf_append_u16(&buf, payload->creation_hash_len - 2);
+	tpm_buf_append(&buf, payload->creation_hash + 2,
+		       payload->creation_hash_len - 2);

So the first two bytes of creation_hash are a redundant length field that needs to be ignored here? Perhaps tpm2_key_encode() shouldn't include that redundant length field?

security/keys/trusted-keys/trusted_tpm2.c static int tpm2_unseal_cmd(struct tpm_chip *chip, · line 790

Quoted 10 lines
security/keys/trusted-keys/trusted_tpm2.c +10 -0
+
+	/* signature scheme */
+	tpm_buf_append_u16(&buf, TPM_ALG_NULL);
+
+	/* creation ticket */
+	tpm_buf_append(&buf, payload->tk, payload->tk_len);
+
+	rc = tpm_transmit_cmd(chip, &buf, 6, "certifying creation data");
+	if (rc)
+		goto out;

This is another instance of the bug where a positive TPM2_RC_* code is being returned from a function that is supposed to return a negative errno value.

- Eric

author
#50 lore raw

PATCH v5 7/11 PM: hibernate: Add kernel-based encryption

In reply to Evan Green

Enabling the kernel to be able to do encryption and integrity checks on the hibernate image prevents a malicious userspace from escalating to kernel execution via hibernation resume. As a first step toward this, add the scaffolding needed for the kernel to do AEAD encryption on the hibernate image, giving us both secrecy and integrity.

We currently hardwire the encryption to be gcm(aes) in 16-page chunks. This strikes a balance between minimizing the authentication tag overhead on storage, and keeping a modest sized staging buffer. With this chunk size, we'd generate 2MB of authentication tag data on an 8GB hiberation image.

The encryption currently sits on top of the core snapshot functionality, wired up only if requested in the user space software suspend (uswsusp) path. This change adds a new ioctl, SNAPSHOT_ENABLE_ENCRYPTION, that is used in both the read (suspend) and write (resume) modes to enable this new encryption functionality. The ioctl also sends or receives the kernel encryption key used to access this specific hibernate image. For now this is passed in plaintext, in a subsequent commit it will become sealed via the TPM to provide confidentiality and integrity of the key itself. This mechanism could potentially be lowered into the common snapshot code given a mechanism to stitch the key contents into the image itself.

To avoid forcing usermode to deal with sequencing the auth tags in with the data, we stitch the auth tags in to the snapshot after each chunk of pages. This complicates the read and write functions, as we roll through the flow of (for read) 1) fill the staging buffer with encrypted data,
2) feed the data pages out to user mode, 3) feed the tag out to user mode. To avoid having each syscall return a small and variable amount of data, the encrypted versions of read and write operate in a loop, allowing an arbitrary amount of data through per syscall.

One alternative that would simplify things here would be a streaming interface to AEAD. Then we could just stream the entire hibernate image through directly, and handle a single tag at the end. However there is a school of thought that suggests a streaming interface to AEAD represents a loaded footgun, as it tempts the caller to act on the decrypted but not yet verified data, defeating the purpose of AEAD.

With this change alone, we don't actually protect ourselves from malicious userspace at all, since we kindly hand the key in plaintext to usermode. In later changes, we'll seal the key with the TPM before handing it back to usermode, so they can't decrypt or tamper with the key themselves.

---

Changes in v5:
- Removed default n in Kconfig (Kees)
- Expanded commit message (Jarkko)

Changes in v4:
- Local ordering and whitespace changes (Jarkko)

 Documentation/power/userland-swsusp.rst |   8 +
 include/uapi/linux/suspend_ioctls.h     |  15 +-
 kernel/power/Kconfig                    |  12 +
 kernel/power/Makefile                   |   1 +
 kernel/power/snapenc.c                  | 492 ++++++++++++++++++++++++
 kernel/power/user.c                     |  40 +-
 kernel/power/user.h                     | 103 +++++
 7 files changed, 659 insertions(+), 12 deletions(-)
 create mode 100644 kernel/power/snapenc.c
 create mode 100644 kernel/power/user.h
Documentation/power/userland-swsusp.rst +8 -0
index 1cf62d80a9ca10..f759915a78ce98 100644
@@ -115,6 +115,14 @@ SNAPSHOT_S2RAM
 	to resume the system from RAM if there's enough battery power or restore
 	its state on the basis of the saved suspend image otherwise)
 
+SNAPSHOT_ENABLE_ENCRYPTION
+	Enables encryption of the hibernate image within the kernel. Upon suspend
+	(ie when the snapshot device was opened for reading), returns a blob
+	representing the random encryption key the kernel created to encrypt the
+	hibernate image with. Upon resume (ie when the snapshot device was opened
+	for writing), receives a blob from usermode containing the key material
+	previously returned during hibernate.
+
 The device's read() operation can be used to transfer the snapshot image from
 the kernel.  It has the following limitations:
 
include/uapi/linux/suspend_ioctls.h +14 -1
index bcce04e21c0dce..b73026ef824bb9 100644
@@ -13,6 +13,18 @@ struct resume_swap_area {
 	__u32 dev;
 } __attribute__((packed));
 
+#define USWSUSP_KEY_NONCE_SIZE 16
+
+/*
+ * This structure is used to pass the kernel's hibernate encryption key in
+ * either direction.
+ */
+struct uswsusp_key_blob {
+	__u32 blob_len;
+	__u8 blob[512];
+	__u8 nonce[USWSUSP_KEY_NONCE_SIZE];
+} __attribute__((packed));
+
 #define SNAPSHOT_IOC_MAGIC	'3'
 #define SNAPSHOT_FREEZE			_IO(SNAPSHOT_IOC_MAGIC, 1)
 #define SNAPSHOT_UNFREEZE		_IO(SNAPSHOT_IOC_MAGIC, 2)
@@ -29,6 +41,7 @@ struct resume_swap_area {
 #define SNAPSHOT_PREF_IMAGE_SIZE	_IO(SNAPSHOT_IOC_MAGIC, 18)
 #define SNAPSHOT_AVAIL_SWAP_SIZE	_IOR(SNAPSHOT_IOC_MAGIC, 19, __kernel_loff_t)
 #define SNAPSHOT_ALLOC_SWAP_PAGE	_IOR(SNAPSHOT_IOC_MAGIC, 20, __kernel_loff_t)
-#define SNAPSHOT_IOC_MAXNR	20
+#define SNAPSHOT_ENABLE_ENCRYPTION	_IOWR(SNAPSHOT_IOC_MAGIC, 21, struct uswsusp_key_blob)
+#define SNAPSHOT_IOC_MAXNR	21
 
 #endif /* _LINUX_SUSPEND_IOCTLS_H */
kernel/power/Kconfig +12 -0
index 60a1d3051cc79a..2bde64bddae403 100644
@@ -92,6 +92,18 @@ config HIBERNATION_SNAPSHOT_DEV
 
 	  If in doubt, say Y.
 
+config ENCRYPTED_HIBERNATION
+	bool "Encryption support for userspace snapshots"
+	depends on HIBERNATION_SNAPSHOT_DEV
+	depends on CRYPTO_AEAD2=y
+	help
+	  Enable support for kernel-based encryption of hibernation snapshots
+	  created by uswsusp tools.
+
+	  Say N if userspace handles the image encryption.
+
+	  If in doubt, say N.
+
 config PM_STD_PARTITION
 	string "Default resume partition"
 	depends on HIBERNATION
kernel/power/Makefile +1 -0
index 874ad834dc8daf..7be08f2e0e3b68 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_SUSPEND)		+= suspend.o
 obj-$(CONFIG_PM_TEST_SUSPEND)	+= suspend_test.o
 obj-$(CONFIG_HIBERNATION)	+= hibernate.o snapshot.o swap.o
 obj-$(CONFIG_HIBERNATION_SNAPSHOT_DEV) += user.o
+obj-$(CONFIG_ENCRYPTED_HIBERNATION) += snapenc.o
 obj-$(CONFIG_PM_AUTOSLEEP)	+= autosleep.o
 obj-$(CONFIG_PM_WAKELOCKS)	+= wakelock.o
 
kernel/power/snapenc.c new file +492 -0

492 lines not on this page · raw message

kernel/power/user.c +29 -11
index 3a4e70366f354c..bba5cdbd2c0239 100644
@@ -25,19 +25,10 @@
 #include <linux/uaccess.h>
 
 #include "power.h"
+#include "user.h"
 
 static bool need_wait;
-
-static struct snapshot_data {
-	struct snapshot_handle handle;
-	int swap;
-	int mode;
-	bool frozen;
-	bool ready;
-	bool platform_support;
-	bool free_bitmaps;
-	dev_t dev;
-} snapshot_state;
+struct snapshot_data snapshot_state;
 
 int is_hibernate_resume_dev(dev_t dev)
 {
@@ -122,6 +113,7 @@ static int snapshot_release(struct inode *inode, struct file *filp)
 	} else if (data->free_bitmaps) {
 		free_basic_memory_bitmaps();
 	}
+	snapshot_teardown_encryption(data);
 	pm_notifier_call_chain(data->mode == O_RDONLY ?
 			PM_POST_HIBERNATION : PM_POST_RESTORE);
 	hibernate_release();
@@ -146,6 +138,12 @@ static ssize_t snapshot_read(struct file *filp, char __user *buf,
 		res = -ENODATA;
 		goto Unlock;
 	}
+
+	if (snapshot_encryption_enabled(data)) {
+		res = snapshot_read_encrypted(data, buf, count, offp);
+		goto Unlock;
+	}
+
 	if (!pg_offp) { /* on page boundary? */
 		res = snapshot_read_next(&data->handle);
 		if (res <= 0)
@@ -182,6 +180,11 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf,
 
 	data = filp->private_data;
 
+	if (snapshot_encryption_enabled(data)) {
+		res = snapshot_write_encrypted(data, buf, count, offp);
+		goto unlock;
+	}
+
 	if (!pg_offp) {
 		res = snapshot_write_next(&data->handle);
 		if (res <= 0)
@@ -317,6 +320,12 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 		break;
 
 	case SNAPSHOT_ATOMIC_RESTORE:
+		if (snapshot_encryption_enabled(data)) {
+			error = snapshot_finalize_decrypted_image(data);
+			if (error)
+				break;
+		}
+
 		snapshot_write_finalize(&data->handle);
 		if (data->mode != O_WRONLY || !data->frozen ||
 		    !snapshot_image_loaded(&data->handle)) {
@@ -352,6 +361,8 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 		}
 		size = snapshot_get_image_size();
 		size <<= PAGE_SHIFT;
+		if (snapshot_encryption_enabled(data))
+			size = snapshot_get_encrypted_image_size(size);
 		error = put_user(size, (loff_t __user *)arg);
 		break;
 
@@ -409,6 +420,13 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 		error = snapshot_set_swap_area(data, (void __user *)arg);
 		break;
 
+	case SNAPSHOT_ENABLE_ENCRYPTION:
+		if (data->mode == O_RDONLY)
+			error = snapshot_get_encryption_key(data, (void __user *)arg);
+		else
+			error = snapshot_set_encryption_key(data, (void __user *)arg);
+		break;
+
 	default:
 		error = -ENOTTY;
 
kernel/power/user.h new file +103 -0

103 lines not on this page · raw message

Signature · 1 line
2.38.1.431.g37b22c650d-goog
#51 lore raw

Re: [PATCH v5 07/11] PM: hibernate: Add kernel-based encryption

Quoted 5 lines · Evan Green

Enabling the kernel to be able to do encryption and integrity checks on the hibernate image prevents a malicious userspace from escalating to kernel execution via hibernation resume. As a first step toward this, add the scaffolding needed for the kernel to do AEAD encryption on the hibernate image, giving us both secrecy and integrity.

"Authenticity" would be more accurate than "integrity".

It is a common mistake, though...

include/uapi/linux/suspend_ioctls.h struct resume_swap_area { · line 26

Quoted 11 lines
include/uapi/linux/suspend_ioctls.h +11 -0
+#define USWSUSP_KEY_NONCE_SIZE 16
+
+/*
+ * This structure is used to pass the kernel's hibernate encryption key in
+ * either direction.
+ */
+struct uswsusp_key_blob {
+	__u32 blob_len;
+	__u8 blob[512];
+	__u8 nonce[USWSUSP_KEY_NONCE_SIZE];
+} __attribute__((packed));

What is the purpose of the nonce here?

It seems to be associated with the key? That doesn't make sense.

Also, please don't use __attribute__((packed)) on structures unless a specific byte layout is needed (e.g. due to the struct being stored on-disk).

kernel/power/Kconfig config HIBERNATION_SNAPSHOT_DEV · line 98

Quoted 4 lines
kernel/power/Kconfig +4 -0
+config ENCRYPTED_HIBERNATION
+	bool "Encryption support for userspace snapshots"
+	depends on HIBERNATION_SNAPSHOT_DEV
+	depends on CRYPTO_AEAD2=y

"gcm(aes)" from the crypto API is being used, so CRYPTO_GCM and CRYPTO_AES are needed to ensure that is available.

Quoted 5 lines
kernel/power/snapenc.c new file +5 -0
+/* Encrypt more data from the snapshot into the staging area. */
+static int snapshot_encrypt_refill(struct snapshot_data *data)
+{
+	struct aead_request *req = data->aead_req;
+	u8 nonce[GCM_AES_IV_SIZE];

kernel/power/snapenc.c line 28

Quoted 10 lines
kernel/power/snapenc.c new file +10 -0
+	DECLARE_CRYPTO_WAIT(wait);
+	size_t total = 0;
+	int pg_idx;
+	int res;
+
+	/*
+	 * The first buffer is the associated data, set to the offset to prevent
+	 * attacks that rearrange chunks.
+	 */
+	sg_set_buf(&data->sg[0], &data->crypt_total, sizeof(data->crypt_total));

Wouldn't it be simpler to use implicit nonces? I.e. make each nonce be the offset of the page, instead of a value stored alongside the ciphertext? Then there would be no need to include the offset of the page in the AAD.

kernel/power/snapenc.c line 48

Quoted 7 lines
kernel/power/snapenc.c new file +7 -0
+		/*
+		 * Copy the page into the staging area. A future optimization
+		 * could potentially skip this copy for lowmem pages.
+		 */
+		memcpy(buf, data_of(data->handle), PAGE_SIZE);
+		sg_set_buf(&data->sg[1 + pg_idx], buf, PAGE_SIZE);
+		total += PAGE_SIZE;

This is a bit ugly. It means that highmem pages in the snapshot get copied twice, first from the snapshot page to the static variable 'buffer', and then to a page in snapshot_data::crypt_pages. And lowmem pages in the snapshot get copied once, into snapshot_data::crypt_pages.

It should be possible to encrypt directly from the snapshot page in both cases, saving all the copies.

I suppose it is fine as-is for now, but I wonder if there's a clean way to implement the zero-copy method.

kernel/power/snapenc.c line 62

Quoted 13 lines
kernel/power/snapenc.c new file +13 -0
+	}
+
+	sg_set_buf(&data->sg[1 + pg_idx], &data->auth_tag, SNAPSHOT_AUTH_TAG_SIZE);
+	aead_request_set_callback(req, 0, crypto_req_done, &wait);
+	/*
+	 * Use incrementing nonces for each chunk, since a 64 bit value won't
+	 * roll into re-use for any given hibernate image.
+	 */
+	memcpy(&nonce[0], &data->nonce_low, sizeof(data->nonce_low));
+	memcpy(&nonce[sizeof(data->nonce_low)],
+	       &data->nonce_high,
+	       sizeof(nonce) - sizeof(data->nonce_low));
+	data->nonce_low += 1;

Hmm, so you *are* using incrementing nonces. In that case there's no need to store them alongside the ciphertext or to include them in the AAD.

Also, since it's being assumed that the counter doesn't overflow 64 bits, there should be no need for 'nonce_high' to exist.

Quoted 9 lines
kernel/power/snapenc.c new file +9 -0
+	/* Set up the encryption transform */
+	data->aead_tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
+	if (IS_ERR(data->aead_tfm)) {
+		rc = PTR_ERR(data->aead_tfm);
+		data->aead_tfm = NULL;
+		return rc;
+	}
+
+	rc = -ENOMEM;

kernel/power/snapenc.c line 364

Quoted 10 lines
kernel/power/snapenc.c new file +10 -0
+	data->aead_req = aead_request_alloc(data->aead_tfm, GFP_KERNEL);
+	if (data->aead_req == NULL)
+		goto setup_fail;
+
+	/* Allocate the staging area */
+	for (i = 0; i < CHUNK_SIZE; i++) {
+		data->crypt_pages[i] = (void *)__get_free_page(GFP_ATOMIC);
+		if (data->crypt_pages[i] == NULL)
+			goto setup_fail;
+	}

The GFP_ATOMIC above should be GFP_KERNEL, given that the same function uses GFP_KERNEL already.

Quoted 14 lines
kernel/power/snapenc.c new file +14 -0
+int snapshot_get_encryption_key(struct snapshot_data *data,
+				struct uswsusp_key_blob __user *key)
+{
+	u8 aead_key[SNAPSHOT_ENCRYPTION_KEY_SIZE];
+	u8 nonce[USWSUSP_KEY_NONCE_SIZE];
+	int rc;
+
+	/* Don't pull a random key from a world that can be reset. */
+	if (data->ready)
+		return -EPIPE;
+
+	rc = snapshot_setup_encryption_common(data);
+	if (rc)
+		return rc;

kernel/power/snapenc.c line 407

Quoted 10 lines
kernel/power/snapenc.c new file +10 -0
+
+	/* Build a random starting nonce. */
+	get_random_bytes(nonce, sizeof(nonce));
+	memcpy(&data->nonce_low, &nonce[0], sizeof(data->nonce_low));
+	memcpy(&data->nonce_high, &nonce[8], sizeof(data->nonce_high));
+	/* Build a random key */
+	get_random_bytes(aead_key, sizeof(aead_key));
+	rc = crypto_aead_setkey(data->aead_tfm, aead_key, sizeof(aead_key));
+	if (rc)
+		goto fail;

Why not just start the nonce at 0? It's a new key.

kernel/power/snapenc.c line 420

Quoted 7 lines
kernel/power/snapenc.c new file +7 -0
+	rc = copy_to_user(&key->blob, &aead_key, sizeof(aead_key));
+	if (rc)
+		goto fail;
+
+	rc = copy_to_user(&key->nonce, &nonce, sizeof(nonce));
+	if (rc)
+		goto fail;

This is not handling the return value of copy_to_user() correctly.

Quoted 8 lines
kernel/power/snapenc.c new file +8 -0
+int snapshot_set_encryption_key(struct snapshot_data *data,
+				struct uswsusp_key_blob __user *key)
+{
+	struct uswsusp_key_blob blob;
+	int rc;
+
+	/* It's too late if data's been pushed in. */
+	if (data->handle.cur)

kernel/power/snapenc.c line 446

Quoted 10 lines
kernel/power/snapenc.c new file +10 -0
+		return -EPIPE;
+
+	rc = snapshot_setup_encryption_common(data);
+	if (rc)
+		return rc;
+
+	/* Load the key from user mode. */
+	rc = copy_from_user(&blob, key, sizeof(struct uswsusp_key_blob));
+	if (rc)
+		goto crypto_setup_fail;

Likewise, this is not handling the return value of copy_from_user() correctly.

kernel/power/snapenc.c line 451

Quoted 5 lines
kernel/power/snapenc.c new file +5 -0
+
+	if (blob.blob_len != sizeof(struct uswsusp_key_blob)) {
+		rc = -EINVAL;
+		goto crypto_setup_fail;
+	}

Later in the patch series this changes to:

	if (blob.blob_len > sizeof(key->blob))

So the semantics of the blob_len field changes from "size of the struct" to
"size of a field in the struct". That's confusing. Which one did you intend?

kernel/power/snapenc.c line 473

Quoted 4 lines
kernel/power/snapenc.c new file +4 -0
+loff_t snapshot_get_encrypted_image_size(loff_t raw_size)
+{
+	loff_t pages = raw_size >> PAGE_SHIFT;
+	loff_t chunks = (pages + (CHUNK_SIZE - 1)) / CHUNK_SIZE;

DIV_ROUND_UP(pages, CHUNK_SIZE)

Quoted 8 lines
kernel/power/snapenc.c new file +8 -0
+	/*
+	 * The encrypted size is the normal size, plus a stitched in
+	 * authentication tag for every chunk of pages.
+	 */
+	return raw_size + (chunks * SNAPSHOT_AUTH_TAG_SIZE);
+}
+
+int snapshot_finalize_decrypted_image(struct snapshot_data *data)

kernel/power/snapenc.c line 491

Quoted 10 lines
kernel/power/snapenc.c new file +10 -0
+{
+	int rc;
+
+	if (data->crypt_offset != 0) {
+		rc = snapshot_decrypt_drain(data);
+		if (rc)
+			return rc;
+	}
+
+	return 0;

There's no need for the 'rc' variable:

        if (data->crypt_offset != 0)
                return snapshot_decrypt_drain(data);
        return 0;
Quoted 13 lines
kernel/power/user.c +1 -2
index 3a4e70366f354c..bba5cdbd2c0239 100644
@@ -25,19 +25,10 @@
 #include <linux/uaccess.h>
 
 #include "power.h"
+#include "user.h"
 
 static bool need_wait;
-
-static struct snapshot_data {

kernel/power/user.c line 31

Quoted 10 lines
kernel/power/user.c +1 -9
-	struct snapshot_handle handle;
-	int swap;
-	int mode;
-	bool frozen;
-	bool ready;
-	bool platform_support;
-	bool free_bitmaps;
-	dev_t dev;
-} snapshot_state;
+struct snapshot_data snapshot_state;

Why is this variable made non-static? It's still only used in this file.

kernel/power/user.h line 7

Quoted 13 lines
kernel/power/user.h new file +7 -0
new file mode 100644
index 00000000000000..ac429782abff85
@@ -0,0 +1,103 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/crypto.h>
+#include <crypto/aead.h>
+#include <crypto/aes.h>
+
+#define SNAPSHOT_ENCRYPTION_KEY_SIZE AES_KEYSIZE_128

Why use AES-128 instead of AES-256?

kernel/power/user.h line 8

Quoted 1 line
kernel/power/user.h new file +1 -0
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)

#ifdef CONFIG_ENCRYPTED_HIBERNATION

Quoted 29 lines
kernel/power/user.h new file +29 -0
+#else
+
+ssize_t snapshot_read_encrypted(struct snapshot_data *data,
+				char __user *buf, size_t count, loff_t *offp)
+{
+	return -ENOTTY;
+}
+
+ssize_t snapshot_write_encrypted(struct snapshot_data *data,
+				 const char __user *buf, size_t count,
+				 loff_t *offp)
+{
+	return -ENOTTY;
+}
+
+static void snapshot_teardown_encryption(struct snapshot_data *data) {}
+static int snapshot_get_encryption_key(struct snapshot_data *data,
+				       struct uswsusp_key_blob __user *key)
+{
+	return -ENOTTY;
+}
+
+static int snapshot_set_encryption_key(struct snapshot_data *data,
+				       struct uswsusp_key_blob __user *key)
+{
+	return -ENOTTY;
+}
+
+static loff_t snapshot_get_encrypted_image_size(loff_t raw_size)

kernel/power/user.h line 101

Quoted 10 lines
kernel/power/user.h new file +10 -0
+{
+	return raw_size;
+}
+
+static int snapshot_finalize_decrypted_image(struct snapshot_data *data)
+{
+	return -ENOTTY;
+}
+
+#define snapshot_encryption_enabled(data) (0)

The above stubs should be 'static inline' functions.

- Eric

author
#52 lore raw

PATCH v5 8/11 PM: hibernate: Use TPM-backed keys to encrypt image

In reply to Evan Green

When using encrypted hibernate images, have the TPM create a key for us and seal it. By handing back a sealed blob instead of the raw key, we prevent usermode from being able to decrypt and tamper with the hibernate image on a different machine.

We'll also go through the motions of having PCR23 set to a known value at the time of key creation and unsealing. Currently there's nothing that enforces the contents of PCR23 as a condition to unseal the key blob, that will come in a later change.

---
Matthew's incarnation of this patch is at:
https://patchwork.kernel.org/project/linux-pm/patch/[email protected]/

Changes in v5:
- Use Suggested-by tag instead of made up Sourced-from (Kees)
- ENCRYPTED_HIBERNATION should depend on TCG_TPM2_RESTRCT_PCR

Changes in v4:
- s/tpm_pcr_reset/tpm2_pcr_reset/ due to change in other patch
- Variable ordering and whitespace fixes (Jarkko)
- Add NULL check explanation in teardown (Jarkko)
- Change strlen+1 to sizeof for static buffer (Jarkko)
- Fix nr_allocated_banks loop overflow (found via KASAN)

Changes in v3:
- ENCRYPTED_HIBERNATION needs TRUSTED_KEYS builtin for key_type_trusted.
- Remove KEYS dependency since it's covered by TRUSTED_KEYS (Kees)

Changes in v2:
- Rework load/create_kernel_key() to eliminate a label (Andrey)
- Call put_device() needed from calling tpm_default_chip().

 kernel/power/Kconfig   |   2 +
 kernel/power/snapenc.c | 211 +++++++++++++++++++++++++++++++++++++++--
 kernel/power/user.h    |   1 +
 3 files changed, 205 insertions(+), 9 deletions(-)
kernel/power/Kconfig +2 -0
index 2bde64bddae403..420024f46992b2 100644
@@ -96,6 +96,8 @@ config ENCRYPTED_HIBERNATION
 	bool "Encryption support for userspace snapshots"
 	depends on HIBERNATION_SNAPSHOT_DEV
 	depends on CRYPTO_AEAD2=y
+	depends on TCG_TPM2_RESTRICT_PCR
+	depends on TRUSTED_KEYS=y
 	help
 	  Enable support for kernel-based encryption of hibernation snapshots
 	  created by uswsusp tools.
kernel/power/snapenc.c +202 -9

275 lines not on this page · raw message

kernel/power/user.h +1 -0
index ac429782abff85..6c86fb64ebe13e 100644
@@ -31,6 +31,7 @@ struct snapshot_data {
 	uint64_t crypt_total;
 	uint64_t nonce_low;
 	uint64_t nonce_high;
+	struct key *key;
 #endif
 
 };
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#53 lore raw

Re: [PATCH v5 08/11] PM: hibernate: Use TPM-backed keys to encrypt image

Quoted 4 lines · Evan Green

We'll also go through the motions of having PCR23 set to a known value at the time of key creation and unsealing. Currently there's nothing that enforces the contents of PCR23 as a condition to unseal the key blob, that will come in a later change.

This patch doesn't really make sense without the later patch "PM: hibernate: seal the encryption key with a PCR policy".

Maybe they should be merged into one patch?

kernel/power/snapenc.c line 22

Quoted 6 lines
kernel/power/snapenc.c +6 -0
+/* sha256("To sleep, perchance to dream") */
+static struct tpm_digest known_digest = { .alg_id = TPM_ALG_SHA256,
+	.digest = {0x92, 0x78, 0x3d, 0x79, 0x2d, 0x00, 0x31, 0xb0, 0x55, 0xf9,
+		   0x1e, 0x0d, 0xce, 0x83, 0xde, 0x1d, 0xc4, 0xc5, 0x8e, 0x8c,
+		   0xf1, 0x22, 0x38, 0x6c, 0x33, 0xb1, 0x14, 0xb7, 0xec, 0x05,
+		   0x5f, 0x49}};

This can be const.

Quoted 8 lines
kernel/power/snapenc.c +8 -0
+static int snapshot_create_kernel_key(struct snapshot_data *data)
+{
+	/* Create a key sealed by the SRK. */
+	char *keyinfo = "new\t32\tkeyhandle=0x81000000";
+	const struct cred *cred = current_cred();
+	struct tpm_digest *digests = NULL;
+	struct key *key = NULL;
+	struct tpm_chip *chip;

kernel/power/snapenc.c static int snapshot_setup_encryption_common(struct snapshot_data *data) · line 421

Quoted 10 lines
kernel/power/snapenc.c +10 -0
+	int ret, i;
+
+	chip = tpm_default_chip();
+	if (!chip)
+		return -ENODEV;
+
+	if (!(tpm_is_tpm2(chip))) {
+		ret = -ENODEV;
+		goto out_dev;
+	}

tpm_is_tpm2() can return an error value.

Quoted 6 lines
kernel/power/snapenc.c +6 -0
+	digests = kcalloc(chip->nr_allocated_banks, sizeof(struct tpm_digest),
+			  GFP_KERNEL);
+	if (!digests) {
+		ret = -ENOMEM;
+		goto out;
+	}

kernel/power/snapenc.c static int snapshot_setup_encryption_common(struct snapshot_data *data) · line 442

Quoted 10 lines
kernel/power/snapenc.c +10 -0
+
+	for (i = 0; i < chip->nr_allocated_banks; i++) {
+		digests[i].alg_id = chip->allocated_banks[i].alg_id;
+		if (digests[i].alg_id == known_digest.alg_id)
+			memcpy(&digests[i], &known_digest, sizeof(known_digest));
+	}
+
+	ret = tpm_pcr_extend(chip, 23, digests);
+	if (ret != 0)
+		goto out;

So, PCR 23 will not actually be extended properly if there is not a SHA-256 bank? Wouldn't it make more sense to return an error in that case?

kernel/power/snapenc.c int snapshot_get_encryption_key(struct snapshot_data *data, · line 532

Quoted 5 lines
kernel/power/snapenc.c +5 -0
+static int snapshot_load_kernel_key(struct snapshot_data *data,
+				    struct uswsusp_key_blob *blob)
+{
+
+	char *keytemplate = "load\t%s\tkeyhandle=0x81000000";

Make this const.

kernel/power/snapenc.c int snapshot_get_encryption_key(struct snapshot_data *data, · line 576

Quoted 8 lines
kernel/power/snapenc.c +8 -0
+	blobstring = kmalloc(blob->blob_len * 2, GFP_KERNEL);
+	if (!blobstring) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	bin2hex(blobstring, blob->blob, blob->blob_len);
+	keyinfo = kasprintf(GFP_KERNEL, keytemplate, blobstring);

There's no need for bin2hex(). Just use the %*phN format specifier instead:

	const char *keytemplate = "load\t%*phN\tkeyhandle=0x81000000";

	keyinfo = kasprintf(GFP_KERNEL, keytemplate, blob->blob_len, blob->blob);

- Eric

author
#54 lore raw

PATCH v5 9/11 PM: hibernate: Mix user key in encrypted hibernate

In reply to Evan Green

Usermode may have their own data protection requirements when it comes to encrypting the hibernate image. For example, users may want a policy where the hibernate image is protected by a key derived both from platform-level security as well as authentication data (such as a password or PIN). This way, even if the platform is compromised (ie a stolen laptop), sensitive data cannot be exfiltrated via the hibernate image without additional data (like the user's password).

The kernel is already doing the encryption, but will be protecting its key with the TPM alone. Allow usermode to mix in key content of their own for the data portion of the hibernate image, so that the image encryption key is determined both by a TPM-backed secret and user-defined data.

To mix the user key in, we hash the kernel key followed by the user key, and use the resulting hash as the new key. This allows usermode to mix in its key material without giving it too much control over what key is actually driving the encryption (which might be used to attack the secret kernel key).

Limiting this to the data portion allows the kernel to receive the page map and prepare its giant allocation even if this user key is not yet available (ie the user has not yet finished typing in their password). Once the user key becomes available, the data portion can be pushed through to the kernel as well. This enables "preloading" scenarios, where the hibernate image is loaded off of disk while the additional key material (eg password) is being collected.

One annoyance of the "preloading" scheme is that hibernate image memory is effectively double-allocated: first by the usermode process pulling encrypted contents off of disk and holding it, and second by the kernel in its giant allocation in prepare_image(). An interesting future optimization would be to allow the kernel to accept and store encrypted page data before the user key is available. This would remove the double allocation problem, as usermode could push the encrypted pages loaded from disk immediately without storing them. The kernel could defer decryption of the data until the user key is available, while still knowing the correct page locations to store the encrypted data in.

---

Changes in v5:
- Remove pad struct member (Kees)

Changes in v2:
- Add missing static on snapshot_encrypted_byte_count()
- Fold in only the used kernel key bytes to the user key.
- Make the user key length 32 (Eric)
- Use CRYPTO_LIB_SHA256 for less boilerplate (Eric)

 include/uapi/linux/suspend_ioctls.h |  17 ++-
 kernel/power/Kconfig                |   1 +
 kernel/power/power.h                |   1 +
 kernel/power/snapenc.c              | 166 ++++++++++++++++++++++++++--
 kernel/power/snapshot.c             |   5 +
 kernel/power/user.c                 |   4 +
 kernel/power/user.h                 |  13 +++
 7 files changed, 195 insertions(+), 12 deletions(-)
include/uapi/linux/suspend_ioctls.h +15 -2
index b73026ef824bb9..7612874608bae4 100644
@@ -14,6 +14,7 @@ struct resume_swap_area {
 } __attribute__((packed));
 
 #define USWSUSP_KEY_NONCE_SIZE 16
+#define USWSUSP_USER_KEY_SIZE 32
 
 /*
  * This structure is used to pass the kernel's hibernate encryption key in
@@ -22,9 +23,20 @@ struct resume_swap_area {
 struct uswsusp_key_blob {
 	__u32 blob_len;
 	__u8 blob[512];
-	__u8 nonce[USWSUSP_KEY_NONCE_SIZE];
+	__u8 nonce[USWSUSP_KEY_NONCE_SIZE] __nonstring;
 } __attribute__((packed));
 
+/*
+ * Allow user mode to fold in key material for the data portion of the hibernate
+ * image.
+ */
+struct uswsusp_user_key {
+	/* Kernel returns the metadata size. */
+	__kernel_loff_t meta_size;
+	__u32 key_len;
+	__u8 key[USWSUSP_USER_KEY_SIZE] __nonstring;
+};
+
 #define SNAPSHOT_IOC_MAGIC	'3'
 #define SNAPSHOT_FREEZE			_IO(SNAPSHOT_IOC_MAGIC, 1)
 #define SNAPSHOT_UNFREEZE		_IO(SNAPSHOT_IOC_MAGIC, 2)
@@ -42,6 +54,7 @@ struct uswsusp_key_blob {
 #define SNAPSHOT_AVAIL_SWAP_SIZE	_IOR(SNAPSHOT_IOC_MAGIC, 19, __kernel_loff_t)
 #define SNAPSHOT_ALLOC_SWAP_PAGE	_IOR(SNAPSHOT_IOC_MAGIC, 20, __kernel_loff_t)
 #define SNAPSHOT_ENABLE_ENCRYPTION	_IOWR(SNAPSHOT_IOC_MAGIC, 21, struct uswsusp_key_blob)
-#define SNAPSHOT_IOC_MAXNR	21
+#define SNAPSHOT_SET_USER_KEY		_IOWR(SNAPSHOT_IOC_MAGIC, 22, struct uswsusp_user_key)
+#define SNAPSHOT_IOC_MAXNR	22
 
 #endif /* _LINUX_SUSPEND_IOCTLS_H */
kernel/power/Kconfig +1 -0
index 420024f46992b2..5c1f8f3f7482d7 100644
@@ -98,6 +98,7 @@ config ENCRYPTED_HIBERNATION
 	depends on CRYPTO_AEAD2=y
 	depends on TCG_TPM2_RESTRICT_PCR
 	depends on TRUSTED_KEYS=y
+	select CRYPTO_LIB_SHA256
 	help
 	  Enable support for kernel-based encryption of hibernation snapshots
 	  created by uswsusp tools.
kernel/power/power.h +1 -0
index b4f43394320961..5955e5cf692302 100644
@@ -151,6 +151,7 @@ struct snapshot_handle {
 
 extern unsigned int snapshot_additional_pages(struct zone *zone);
 extern unsigned long snapshot_get_image_size(void);
+extern unsigned long snapshot_get_meta_page_count(void);
 extern int snapshot_read_next(struct snapshot_handle *handle);
 extern int snapshot_write_next(struct snapshot_handle *handle);
 extern void snapshot_write_finalize(struct snapshot_handle *handle);
kernel/power/snapenc.c +156 -10

241 lines not on this page · raw message

kernel/power/snapshot.c +5 -0
index c20ca5fb9adc87..d8a30f3eaaf4c6 100644
@@ -2083,6 +2083,11 @@ unsigned long snapshot_get_image_size(void)
 	return nr_copy_pages + nr_meta_pages + 1;
 }
 
+unsigned long snapshot_get_meta_page_count(void)
+{
+	return nr_meta_pages + 1;
+}
+
 static int init_header(struct swsusp_info *info)
 {
 	memset(info, 0, sizeof(struct swsusp_info));
kernel/power/user.c +4 -0
index bba5cdbd2c0239..a66e32c9596da8 100644
@@ -427,6 +427,10 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 			error = snapshot_set_encryption_key(data, (void __user *)arg);
 		break;
 
+	case SNAPSHOT_SET_USER_KEY:
+		error = snapshot_set_user_key(data, (void __user *)arg);
+		break;
+
 	default:
 		error = -ENOTTY;
 
kernel/power/user.h +13 -0
index 6c86fb64ebe13e..d75fd287b4c3de 100644
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 
 #include <linux/crypto.h>
+#include <linux/suspend_ioctls.h>
 #include <crypto/aead.h>
 #include <crypto/aes.h>
 
@@ -32,6 +33,9 @@ struct snapshot_data {
 	uint64_t nonce_low;
 	uint64_t nonce_high;
 	struct key *key;
+	u8 user_key[USWSUSP_USER_KEY_SIZE] __nonstring;
+	bool user_key_valid;
+	uint64_t meta_size;
 #endif
 
 };
@@ -55,6 +59,9 @@ int snapshot_get_encryption_key(struct snapshot_data *data,
 int snapshot_set_encryption_key(struct snapshot_data *data,
 				struct uswsusp_key_blob __user *key);
 
+int snapshot_set_user_key(struct snapshot_data *data,
+	struct uswsusp_user_key __user *key);
+
 loff_t snapshot_get_encrypted_image_size(loff_t raw_size);
 
 int snapshot_finalize_decrypted_image(struct snapshot_data *data);
@@ -89,6 +96,12 @@ static int snapshot_set_encryption_key(struct snapshot_data *data,
 	return -ENOTTY;
 }
 
+static int snapshot_set_user_key(struct snapshot_data *data,
+	struct uswsusp_user_key __user *key)
+{
+	return -ENOTTY;
+}
+
 static loff_t snapshot_get_encrypted_image_size(loff_t raw_size)
 {
 	return raw_size;
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#55 lore raw

Re: [PATCH v5 09/11] PM: hibernate: Mix user key in encrypted hibernate

Quoted 3 lines · Evan Green

Limiting this to the data portion allows the kernel to receive the page map and prepare its giant allocation even if this user key is not yet available (ie the user has not yet finished typing in their password).

What is meant by the "data portion"?

Quoted 10 lines
kernel/power/snapenc.c +10 -0
+int snapshot_set_user_key(struct snapshot_data *data,
+	struct uswsusp_user_key __user *key)
+{
+	struct uswsusp_user_key user_key;
+	unsigned int key_len;
+	int rc;
+	loff_t size;
+
+	/*
+	 * Return the metadata size, the number of bytes that can be fed in before

kernel/power/snapenc.c int snapshot_set_encryption_key(struct snapshot_data *data, · line 787

Quoted 10 lines
kernel/power/snapenc.c +10 -0
+	 * the user data key is needed at resume time.
+	 */
+	size = snapshot_get_meta_data_size();
+	rc = put_user(size, &key->meta_size);
+	if (rc)
+		return rc;
+
+	rc = copy_from_user(&user_key, key, sizeof(struct uswsusp_user_key));
+	if (rc)
+		return rc;

This isn't correctly checking the return value of copy_from_user().

kernel/power/snapenc.c int snapshot_set_encryption_key(struct snapshot_data *data, · line 793

Quoted 6 lines
kernel/power/snapenc.c +6 -0
+
+	BUILD_BUG_ON(sizeof(data->user_key) < sizeof(user_key.key));
+
+	key_len = min_t(__u32, user_key.key_len, sizeof(data->user_key));
+	if (key_len < 8)
+		return -EINVAL;

Shouldn't -EINVAL also be returned if key_len is too large?

- Eric

author
#56 lore raw

PATCH v5 10/11 PM: hibernate: Verify the digest encryption key

In reply to Evan Green

We want to ensure that the key used to encrypt the digest was created by the kernel during hibernation. To do this we request that the TPM include information about the value of PCR 23 at the time of key creation in the sealed blob. On resume, we can make sure that the PCR information in the creation data blob (already certified by the TPM to be accurate) corresponds to the expected value. Since only the kernel can touch PCR 23, if an attacker generates a key themselves the value of PCR 23 will have been different, allowing us to reject the key and boot normally instead of resuming.

---
Matthew's original version of this patch is here:
https://patchwork.kernel.org/project/linux-pm/patch/[email protected]/

I moved the TPM2_CC_CERTIFYCREATION code into a separate change in the trusted key code because the blob_handle was being flushed and was no longer valid for use in CC_CERTIFYCREATION after the key was loaded. As an added benefit of moving the certification into the trusted keys code, we can drop the other patch from the original series that squirrelled the blob_handle away.

Changes in v5:
- Use a struct to access creation data (Kees)
- Build PCR bitmask programmatically in creation data (Kees)

Changes in v4:
- Local variable reordering (Jarkko)

Changes in v3:
- Changed funky tag to Co-developed-by (Kees). Matthew, holler if you want something different.

Changes in v2:
- Fixed some sparse warnings
- Use CRYPTO_LIB_SHA256 to get rid of sha256_data() (Eric)
- Adjusted offsets due to new ASN.1 format, and added a creation data length check.

 kernel/power/snapenc.c | 122 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 120 insertions(+), 2 deletions(-)
kernel/power/snapenc.c +120 -2
index 0b38642628f7ce..f32c7347a330a4 100644
@@ -22,6 +22,12 @@ static struct tpm_digest known_digest = { .alg_id = TPM_ALG_SHA256,
 		   0xf1, 0x22, 0x38, 0x6c, 0x33, 0xb1, 0x14, 0xb7, 0xec, 0x05,
 		   0x5f, 0x49}};
 
+/* sha256(sha256(empty_pcr | known_digest)) */
+static const char expected_digest[] = {0x2f, 0x96, 0xf2, 0x1b, 0x70, 0xa9, 0xe8,
+	0x42, 0x25, 0x8e, 0x66, 0x07, 0xbe, 0xbc, 0xe3, 0x1f, 0x2c, 0x84, 0x4a,
+	0x3f, 0x85, 0x17, 0x31, 0x47, 0x9a, 0xa5, 0x53, 0xbb, 0x23, 0x0c, 0x32,
+	0xf3};
+
 /* Derive a key from the kernel and user keys for data encryption. */
 static int snapshot_use_user_key(struct snapshot_data *data)
 {
@@ -491,7 +497,7 @@ static int snapshot_setup_encryption_common(struct snapshot_data *data)
 static int snapshot_create_kernel_key(struct snapshot_data *data)
 {
 	/* Create a key sealed by the SRK. */
-	char *keyinfo = "new\t32\tkeyhandle=0x81000000";
+	char *keyinfo = "new\t32\tkeyhandle=0x81000000\tcreationpcrs=0x00800000";
 	const struct cred *cred = current_cred();
 	struct tpm_digest *digests = NULL;
 	struct key *key = NULL;
@@ -612,17 +618,57 @@ int snapshot_get_encryption_key(struct snapshot_data *data,
 	return rc;
 }
 
+/* Currently only PCR23 is included in the creation data. */
+#define SNAPSHOT_KEY_PCR_COUNT 1
+
+/* The standard set of 24 PCRs takes 3 bytes to represent as a bitmask. */
+#define SNAPSHOT_KEY_PCR_SELECTION_BYTES 3
+
+/*
+ * The TPM loves to return variable length structures. This is the form of
+ * TPM2B_CREATION_DATA expected and verified for the snapshot key.
+ */
+struct snapshot_key_creation_data {
+	__be16 size;
+	/* TPMS_CREATION_DATA, the hashed portion */
+	struct {
+		/* TPML_PCR_SELECTION */
+		struct {
+			__be32 count;
+			/* TPMS_PCR_SELECTION */
+			struct {
+				__be16 hash_algo;
+				u8 size;
+				u8 select[SNAPSHOT_KEY_PCR_SELECTION_BYTES];
+			} __packed pcr_selections;
+		} __packed pcr_select;
+
+		/* TPM2B_DIGEST */
+		struct {
+			__be16 size;
+			u8 digest[SHA256_DIGEST_SIZE];
+		} __packed pcr_digest[SNAPSHOT_KEY_PCR_COUNT];
+
+		/* ... additional fields not verified ... */
+	} creation;
+} __packed;
+
 static int snapshot_load_kernel_key(struct snapshot_data *data,
 				    struct uswsusp_key_blob *blob)
 {
 
 	char *keytemplate = "load\t%s\tkeyhandle=0x81000000";
+	struct snapshot_key_creation_data *creation;
 	const struct cred *cred = current_cred();
+	struct trusted_key_payload *payload;
+	char certhash[SHA256_DIGEST_SIZE];
 	struct tpm_digest *digests = NULL;
+	unsigned int creation_hash_length;
 	char *blobstring = NULL;
 	struct key *key = NULL;
 	struct tpm_chip *chip;
 	char *keyinfo = NULL;
+	u32 pcr_selection = 0;
 	int i, ret;
 
 	chip = tpm_default_chip();
@@ -640,8 +686,10 @@ static int snapshot_load_kernel_key(struct snapshot_data *data,
 
 	digests = kcalloc(chip->nr_allocated_banks, sizeof(struct tpm_digest),
 			  GFP_KERNEL);
-	if (!digests)
+	if (!digests) {
+		ret = -ENOMEM;
 		goto out;
+	}
 
 	for (i = 0; i < chip->nr_allocated_banks; i++) {
 		digests[i].alg_id = chip->allocated_banks[i].alg_id;
@@ -681,6 +729,76 @@ static int snapshot_load_kernel_key(struct snapshot_data *data,
 	if (ret != 0)
 		goto out;
 
+	/* Verify the creation hash matches the creation data. */
+	payload = key->payload.data[0];
+	creation = (struct snapshot_key_creation_data *)payload->creation;
+	if (!creation || !payload->creation_hash ||
+	    (payload->creation_len < sizeof(*creation)) ||
+	    (payload->creation_hash_len - 2 != SHA256_DIGEST_SIZE)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	creation_hash_length =
+		payload->creation_len -
+		offsetof(struct snapshot_key_creation_data, creation);
+
+	sha256((const u8 *)&creation->creation, creation_hash_length, certhash);
+	if (memcmp(payload->creation_hash + sizeof(__be16), certhash, SHA256_DIGEST_SIZE) != 0) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* We now know that the creation data is authentic - parse it */
+
+	/* Verify TPML_PCR_SELECTION.count */
+	if (be32_to_cpu(creation->creation.pcr_select.count) !=
+	    SNAPSHOT_KEY_PCR_COUNT) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Verify the PCRs are SHA256. */
+	if (be16_to_cpu(creation->creation.pcr_select.pcr_selections.hash_algo) !=
+	    TPM_ALG_SHA256) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Gather the PCR selection bitmask. */
+	if (creation->creation.pcr_select.pcr_selections.size !=
+	    SNAPSHOT_KEY_PCR_SELECTION_BYTES) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	for (i = SNAPSHOT_KEY_PCR_SELECTION_BYTES - 1; i >= 0; i--) {
+		pcr_selection <<= 8;
+		pcr_selection |=
+			creation->creation.pcr_select.pcr_selections.select[i];
+	}
+
+	/* Verify PCR 23 is selected. */
+	if (pcr_selection != (1 << 23)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Verify the first and only PCR hash is the expected size. */
+	if (be16_to_cpu(creation->creation.pcr_digest[0].size) !=
+	    SHA256_DIGEST_SIZE) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Verify PCR 23 contained the expected value when the key was created. */
+	if (memcmp(&creation->creation.pcr_digest[0].digest, expected_digest,
+		   SHA256_DIGEST_SIZE) != 0) {
+
+		ret = -EINVAL;
+		goto out;
+	}
+
 	data->key = key;
 	key = NULL;
 
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#57 lore raw

Re: [PATCH v5 10/11] PM: hibernate: Verify the digest encryption key

Quoted 1 line · Evan Green

We want to ensure that the key used to encrypt the digest

What is meant by "the key used to encrypt the digest"?

Quoted 9 lines

was created by
the kernel during hibernation. To do this we request that the TPM include information about the value of PCR 23 at the time of key creation in the sealed blob. On resume, we can make sure that the PCR information in the creation data blob (already certified by the TPM to be accurate) corresponds to the expected value. Since only the kernel can touch PCR 23, if an attacker generates a key themselves the value of PCR 23 will have been different, allowing us to reject the key and boot normally instead of resuming.

It seems that PCR 23 is already included in the authorization policy for the key, so why is this extra part needed?

- Eric

author
#58 lore raw

PATCH v5 11/11 PM: hibernate: seal the encryption key with a PCR policy

In reply to Evan Green

The key blob is not secret, and by default the TPM will happily unseal it regardless of system state. We can protect against that by sealing the secret with a PCR policy - if the current PCR state doesn't match, the TPM will refuse to release the secret. For now let's just seal it to PCR 23. In the long term we may want a more flexible policy around this, such as including PCR 7 for PCs or 0 for Chrome OS.

---

(no changes since v4)

Changes in v4:
- Local variable ordering (Jarkko)

Changes in v3:
- Changed funky tag to Co-developed-by (Kees)

Changes in v2:
- Fix sparse warnings
- Fix session type comment (Andrey)
- Eliminate extra label in get/create_kernel_key() (Andrey)
- Call tpm_try_get_ops() before calling tpm2_flush_context().

 include/linux/tpm.h    |   4 +
 kernel/power/snapenc.c | 166 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 165 insertions(+), 5 deletions(-)
include/linux/tpm.h +4 -0
index 9c2ee3e30ffa5d..252a8a92a7ff5b 100644
@@ -233,18 +233,22 @@ enum tpm2_command_codes {
 	TPM2_CC_CONTEXT_LOAD	        = 0x0161,
 	TPM2_CC_CONTEXT_SAVE	        = 0x0162,
 	TPM2_CC_FLUSH_CONTEXT	        = 0x0165,
+	TPM2_CC_START_AUTH_SESSION      = 0x0176,
 	TPM2_CC_VERIFY_SIGNATURE        = 0x0177,
 	TPM2_CC_GET_CAPABILITY	        = 0x017A,
 	TPM2_CC_GET_RANDOM	        = 0x017B,
 	TPM2_CC_PCR_READ	        = 0x017E,
+	TPM2_CC_POLICY_PCR              = 0x017F,
 	TPM2_CC_PCR_EXTEND	        = 0x0182,
 	TPM2_CC_EVENT_SEQUENCE_COMPLETE = 0x0185,
 	TPM2_CC_HASH_SEQUENCE_START     = 0x0186,
+	TPM2_CC_POLICY_GET_DIGEST       = 0x0189,
 	TPM2_CC_CREATE_LOADED           = 0x0191,
 	TPM2_CC_LAST		        = 0x0193, /* Spec 1.36 */
 };
 
 enum tpm2_permanent_handles {
+	TPM2_RH_NULL		= 0x40000007,
 	TPM2_RS_PW		= 0x40000009,
 };
 
kernel/power/snapenc.c +161 -5

231 lines not on this page · raw message

Signature · 1 line
2.38.1.431.g37b22c650d-goog
#59 lore raw

Re: [PATCH v5 11/11] PM: hibernate: seal the encryption key with a PCR policy

Quoted 11 lines · Evan Green
kernel/power/snapenc.c +11 -0
+static int tpm_setup_policy(struct tpm_chip *chip, int *session_handle)
+{
+	struct tpm_header *head;
+	struct tpm_buf buf;
+	char nonce[32] = {0x00};
+	int rc;
+
+	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS,
+			  TPM2_CC_START_AUTH_SESSION);
+	if (rc)
+		return rc;

kernel/power/snapenc.c void snapshot_teardown_encryption(struct snapshot_data *data) · line 466

Quoted 10 lines
kernel/power/snapenc.c +10 -0
+
+	/* Decrypt key */
+	tpm_buf_append_u32(&buf, TPM2_RH_NULL);
+
+	/* Auth entity */
+	tpm_buf_append_u32(&buf, TPM2_RH_NULL);
+
+	/* Nonce - blank is fine here */
+	tpm_buf_append_u16(&buf, sizeof(nonce));
+	tpm_buf_append(&buf, nonce, sizeof(nonce));

In general, hardcoded nonces are a huge red flag. If it's fine here, it would be helpful to leave a comment explaining why that is.

kernel/power/snapenc.c void snapshot_teardown_encryption(struct snapshot_data *data) · line 482

Quoted 3 lines
kernel/power/snapenc.c +3 -0
+	rc = tpm_send(chip, buf.data, tpm_buf_length(&buf));
+	if (rc)
+		goto out;

This is another instance of the bug where TPM2_RC_* codes are being returned from a function that is expected to return -errno values.

kernel/power/snapenc.c void snapshot_teardown_encryption(struct snapshot_data *data) · line 491

Quoted 1 line
kernel/power/snapenc.c +1 -0
+	*session_handle = be32_to_cpu(*(__be32 *)&buf.data[10]);

get_unaligned_be32, to avoid an unaligned memory access.

Quoted 33 lines
kernel/power/snapenc.c +17 -1
@@ -497,11 +602,16 @@ static int snapshot_setup_encryption_common(struct snapshot_data *data)
 static int snapshot_create_kernel_key(struct snapshot_data *data)
 {
 	/* Create a key sealed by the SRK. */
-	char *keyinfo = "new\t32\tkeyhandle=0x81000000\tcreationpcrs=0x00800000";
+	const char *keytemplate =
+		"new\t32\tkeyhandle=0x81000000\tcreationpcrs=0x00800000\tpolicydigest=%s";
 	const struct cred *cred = current_cred();
 	struct tpm_digest *digests = NULL;
+	char policy[SHA256_DIGEST_SIZE];
+	char *policydigest = NULL;
+	int session_handle = -1;
 	struct key *key = NULL;
 	struct tpm_chip *chip;
+	char *keyinfo = NULL;
 	int ret, i;
 
 	chip = tpm_default_chip();
@@ -534,6 +644,28 @@ static int snapshot_create_kernel_key(struct snapshot_data *data)
 	if (ret != 0)
 		goto out;
 
+	policydigest = kmalloc(SHA256_DIGEST_SIZE * 2 + 1, GFP_KERNEL);
+	if (!policydigest) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	ret = tpm_setup_policy(chip, &session_handle);
+	if (ret != 0)
+		goto out;
+
+	ret = tpm_policy_get_digest(chip, session_handle, policy);

kernel/power/snapenc.c static int snapshot_create_kernel_key(struct snapshot_data *data) · line 667

Quoted 10 lines
kernel/power/snapenc.c +10 -0
+	if (ret != 0)
+		goto out;
+
+	bin2hex(policydigest, policy, SHA256_DIGEST_SIZE);
+	policydigest[SHA256_DIGEST_SIZE * 2] = '\0';
+	keyinfo = kasprintf(GFP_KERNEL, keytemplate, policydigest);
+	if (!keyinfo) {
+		ret = -ENOMEM;
+		goto out;
+	}

With the %*phN format specifier, there would be no need for bin2hex().

- Eric

author
#60 lore raw
In reply to Evan Green

Hello, it's me again!

Quoted 72 lines · Evan Green

We are exploring enabling hibernation in some new scenarios. However, our security team has a few requirements, listed below:
1. The hibernate image must be encrypted with protection derived from both the platform (eg TPM) and user authentication data (eg password).
2. Hibernation must not be a vector by which a malicious userspace can escalate to the kernel.

Requirement #1 can be achieved solely with uswsusp, however requirement 2 necessitates mechanisms in the kernel to guarantee integrity of the hibernate image. The kernel needs a way to authenticate that it generated the hibernate image being loaded, and that the image has not been tampered with. Adding support for in-kernel AEAD encryption with a TPM-sealed key allows us to achieve both requirements with a single computation pass.

Matthew Garrett published a series [1] that aligns closely with this goal. His series utilized the fact that PCR23 is a resettable PCR that can be blocked from access by usermode. The TPM can create a sealed key tied to PCR23 in two ways. First, the TPM can attest to the value of PCR23 when the key was created, which the kernel can use on resume to verify that the kernel must have created the key (since it is the only one capable of modifying PCR23). It can also create a policy that enforces PCR23 be set to a specific value as a condition of unsealing the key, preventing usermode from unsealing the key by talking directly to the TPM.

This series adopts that primitive as a foundation, tweaking and building on it a bit. Where Matthew's series used the TPM-backed key to encrypt a hash of the image, this series uses the key directly as a gcm(aes) encryption key, which the kernel uses to encrypt and decrypt the hibernate image in chunks of 16 pages. This provides both encryption and integrity, which turns out to be a noticeable performance improvement over separate passes for encryption and hashing.

The series also introduces the concept of mixing user key material into the encryption key. This allows usermode to introduce key material based on unspecified external authentication data (in our case derived from something like the user password or PIN), without requiring usermode to do a separate encryption pass.

Matthew also documented issues his series had [2] related to generating fake images by booting alternate kernels without the PCR23 limiting. With access to PCR23 on the same machine, usermode can create fake hibernate images that are indistinguishable to the new kernel from genuine ones. His post outlines a solution that involves adding more PCRs into the creation data and policy, with some gyrations to make this work well on a standard PC.

Our approach would be similar: on our machines PCR 0 indicates whether the system is booted in secure/verified mode or developer mode. By adding PCR0 to the policy, we can reject hibernate images made in developer mode while in verified mode (or vice versa).

Additionally, mixing in the user authentication data limits both data exfiltration attacks (eg a stolen laptop) and forged hibernation image attacks to attackers that already know the authentication data (eg user's password). This, combined with our relatively sealed userspace (dm-verity on the rootfs), and some judicious clearing of the hibernate image (such as across an OS update) further reduce the risk of an online attack. The remaining attack space of a forgery from someone with physical access to the device and knowledge of the authentication data is out of scope for us, given that flipping to developer mode or reflashing RO firmware trivially achieves the same thing.

A couple of patches still need to be written on top of this series. The generalized functionality to OR in additional PCRs via Kconfig (like PCR 0 or 5) still needs to be added. We'll also need a patch that disallows unencrypted forms of resume from hibernation, to fully close the door to malicious userspace. However, I wanted to get this series out first and get reactions from upstream before continuing to add to it.

Quoted 3 lines

[1] https://patchwork.kernel.org/project/linux-pm/cover/[email protected]/
[2] https://mjg59.dreamwidth.org/58077.html

Doug found a practical problem with this design. The security of this mechanism depends on the kernel being able to prevent usermode from manipulating PCR23. While this series has managed to add that gating to the standard /dev/tpm interface, at least on ChromeOS, there are still many "dangerous toys" lying around that might allow a malicious root to communicate directly with the TPM. This raw access could allow usermode to extend PCR23 manually and forge malicious hibernate images that appear genuine. Examples of raw access include 1) i2cget -F, 2) unbinding the driver and binding i2c-dev instead, 3) using /dev/mem to manipulate the i2c controller registers directly, and 4) my favorite, remuxing the i2c pins to GPIO and bitbanging.

We did some brainstorming and came up with a pivot that has the benefits of 1) reusing a decent chunk of this series, 2) not taking PCR23 away from usermode (which based on other comments seemed like it might not fly anyway), and 3) pushing the TPM interaction back down into usermode. The new element we take advantage of is that our early userspace is still considered trusted, as we sign the rootfs and protect it with dm-verity.

The idea is to have early userspace ask the TPM to create a sealed key bound to a (non-resettable) PCR. We then save the blob to disk, extend the PCR (to prevent future unsealings in this boot), and push the key material up to the kernel for use as a "hibernate seed". The kernel will hold this seed in memory, and at hibernate time will use it to encrypt a randomly generated "bulk key". The bulk key is then used to encrypt the main hibernate image. So on disk at hibernate, we have 1) the encrypted hibernate image, protected by the bulk key, 2) the bulk key, protected by the seed, and finally 3) the seed, a TPM-protected key blob that can only be unsealed when a PCR is set to its boot value. In our own userspace implementation we'd seal this against a firmware PCR as well, to differentiate between Verified mode and Developer mode.

At resume time, early userspace would find the blob, successfully unseal it (because the PCRs had reset back to the value that matches the policy), and push the recovered seed to the kernel. It can then push the encrypted bulk key and encrypted hibernate image. On our systems, this works fine as the PCRs seem to always reset across hibernate. Is that true generally as well?

So my plan for the next spin of this series looks something like:
* Drop the tpm: and security: subsystem patches
* Keep the gist of the PM: patches as is, but instead of the TPM stuff...
* Introduce two new sysfs files, one to allow usermode to save the
seed into kernel memory, and another to lock out future changes to the hibernate seed (until the next reboot).
* Use the hibernate seed to encrypt a randomly generated bulk key,
which is then used to encrypt the main hibernate image.
* Keep the "PM: mix user key in" patch, as we still need the image to
be encrypted with a key based on user authentication data, which this new mechanism alone doesn't provide.

Anyone have any big objections to that plan, or see new gaping holes in the idea? In the end I think it's actually a little nicer, as it decouples all of the TPM-specific machinery from the concept of secure hibernate, as well as not trying to police PCR access. Casey and Greg, I'm going to guess you don't want to be CCed on the next spin, given that I'm dropping the notion of taking PCR23 away from userspace. Please holler if you would like to be CCed.

-Evan