Enable hibernation when Lockdown is enabled
Subsystems (8)
cover Enable hibernation when Lockdown is enabled
Lockdown in integrity mode aims to ensure that arbitrary code cannot end up in ring 0 without owner approval. The combination of module signing and secure boot gets most of the way there, but various other features are disabled by lockdown in order to avoid more convoluted approaches that would enable unwanted code to end up in kernel space. One of these is hibernation, since at present the kernel performs no verification of the code it's resuming. If hibernation were permitted, an attacker with root (but not kernel) privileges could disable swap, write a valid hibernation image containing code of their own choosing to the swap partition, and then reboot. On reboot, the kernel would happily resume the provided code.
This patchset aims to provide a secure implementation of hibernation. It is based on the presumption that simply storing a key in-kernel is insufficient, since if Lockdown is merely in integrity (rather than confidentiality) mode we assume that root is able to read kernel memory and so would be able to obtain these secrets. It also aims to be unspoofable - an attacker should not be able to write out a hibernation image using cryptographic material under their control.
TPMs can be used to generate key material that is encrypted with a key that does not leave the TPM. This means that we can generate an AES key, encrypt the image hash with it, encrypt it with a TPM-backed key, and store the encrypted key in the hibernation image. On resume, we pass the key back to the TPM, receive the unencrypted AES key, and use that to validate the image.
However, this is insufficient. Nothing prevents anyone else with access to the TPM asking it to decrypt the key. We need to be able to distinguish between accesses that come from the kernel directly and accesses that come from userland.
TPMs have several Platform Configuration Registers (PCRs) which are used for different purposes. PCRs are initialised to a known value, and cannot be modified directly by the platform. Instead, the platform can provide a hash of some data to the TPM. The TPM combines the existing PCR value with the new hash, and stores the hash of this combination in the PCR. Most PCRs can only be extended, which means that the only way to achieve a specific value for a TPM is to perform the same series of writes.
When secrets are encrypted by the TPM, they can be accompanied by a policy that describes the state the TPM must be in in order for it to decrypt them. If the TPM is not in this state, it will refuse to decrypt the material even if it is otherwise capable of doing so. This allows keys to be tied to specific system state - if the system is not in that state, the TPM will not grant access.
PCR 23 is special in that it can be reset on demand. This patchset re-purposes PCR 23 and blocks userland's ability to extend or reset it. The kernel is then free to impose policy by resetting PCR 23 to a known starting point, extending it with a known value, encrypting key material with a policy that ties it to PCR 23, and then resetting it. Even if userland has access to the encrypted blob, it cannot decrypt it since it has no way to force PCR 23 to be in the same state.
So. During hibernation we freeze userland. We then reset PCR 23 and extend it to a known value. We generate a key, use it and then encrypt it with the TPM. When we encrypt it, we define a policy which states that the TPM must have the same PCR 23 value as it presently does. We also store the current PCR 23 value in the key metadata. On resume, we again freeze userland, reset PCR 23 and extend it to the same value. We decrypt the key, and verify from the metadata that it was created when PCR 23 had the expected value. If so, we use it to decrypt the hash used to verify the hibernation image and ensure that the image matches it. If everything looks good, we resume. If not, we return to userland. Either way, we reset PCR 23 before any userland code runs again.
This all works on my machine, but it's imperfect - there's a meaningful performance hit on resume forced by reading all the blocks in-order, and it probably makes more sense to do that after reads are complete instead but I wanted to get the other components of this out for review first. It's also not ideal from a security perspective until there's more ecosystem integration - we need a kernel to be able to assert to a signed bootloader that it implements this, since otherwise an attacker can simply downgrade to a kernel that doesn't implement this policy and gain access to PCR 23 themselves. There's ongoing work in the UEFI bootloader space that would make this possible.
PATCH 1/9 tpm: Add support for in-kernel resetting of PCRs
Add an internal command for resetting a PCR.
--- drivers/char/tpm/tpm-interface.c | 28 +++++++++++++++++++++++++ drivers/char/tpm/tpm.h | 2 ++ drivers/char/tpm/tpm1-cmd.c | 34 ++++++++++++++++++++++++++++++ drivers/char/tpm/tpm2-cmd.c | 36 ++++++++++++++++++++++++++++++++ include/linux/tpm.h | 7 +++++++ 5 files changed, 107 insertions(+)
drivers/char/tpm/tpm-interface.c +28 -0
@@ -342,6 +342,34 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, }EXPORT_SYMBOL_GPL (tpm_pcr_extend); +/** +* tpm_pcr_reset - reset the specified PCR +* @chip: a &struct tpm_chip instance, %NULL for the default chip +* @pcr_idx: the PCR to be reset +* +* Return: same as with tpm_transmit_cmd() +*/ +int tpm_pcr_reset (struct tpm_chip *chip,u32 pcr_idx) +{ +int rc; + + chip =tpm_find_get_ops (chip); +if (!chip) +return -ENODEV ; + +if (chip->flags &TPM_CHIP_FLAG_TPM2 ) { + rc =tpm2_pcr_reset (chip, pcr_idx); + goto out; + } + + rc =tpm1_pcr_reset (chip, pcr_idx,"attempting to reset a PCR" ); + +out: +tpm_put_ops (chip); +return rc; +} +EXPORT_SYMBOL_GPL (tpm_pcr_reset); +/** * tpm_send - send a TPM command * @chip: a &struct tpm_chip instance, %NULL for the default chip
drivers/char/tpm/tpm.h +2 -0
@@ -176,6 +176,7 @@ int tpm1_get_timeouts(struct tpm_chip *chip);unsigned long tpm1_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm1_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,const u8 *hash,const char *log_msg); +int tpm1_pcr_reset (struct tpm_chip *chip,u32 pcr_idx,const char *log_msg);int tpm1_pcr_read (struct tpm_chip *chip,u32 pcr_idx,u8 *res_buf);ssize_t tpm1_getcap (struct tpm_chip *chip,u32 subcap_id,cap_t *cap,const char *desc,size_t min_cap_length);
@@ -220,6 +221,7 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digest, u16 *digest_size_ptr);int tpm2_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digests); +int tpm2_pcr_reset (struct tpm_chip *chip,u32 pcr_idx);int tpm2_get_random (struct tpm_chip *chip,u8 *dest,size_t max);ssize_t tpm2_get_tpm_pt (struct tpm_chip *chip,u32 property_id,u32 *value,const char *desc);
drivers/char/tpm/tpm1-cmd.c +34 -0
@@ -478,6 +478,40 @@ int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,return rc; } +struct tpm_pcr_selection { +u16 size_of_select; +u8 pcr_select[3 ]; +} __packed; + +#define TPM_ORD_PCR_RESET 200 +int tpm1_pcr_reset (struct tpm_chip *chip,u32 pcr_idx,const char *log_msg) +{ +struct tpm_pcr_selection selection; +struct tpm_buf buf; +int i, rc; +char tmp; + + rc =tpm_buf_init (&buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_PCR_RESET ); +if (rc) +return rc; + + selection.size_of_select =3 ; + +for (i =0 ; i < selection.size_of_select; i++) { + tmp =0 ; +if (pcr_idx /3 == i) { + pcr_idx -= i *8 ; + tmp |=1 << pcr_idx; + } + selection.pcr_select[i] = tmp; + } +tpm_buf_append (&buf, (u8 *)&selection,sizeof (selection)); + + rc =tpm_transmit_cmd (chip, &buf,sizeof (selection), log_msg); +tpm_buf_destroy (&buf); +return rc; +} +#define TPM_ORD_GET_CAP 101ssize_t tpm1_getcap (struct tpm_chip *chip,u32 subcap_id,cap_t *cap,const char *desc,size_t min_cap_length)
drivers/char/tpm/tpm2-cmd.c +36 -0
@@ -269,6 +269,42 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,return rc; } +/** +* tpm2_pcr_reset() - reset a PCR +* +* @chip: TPM chip to use. +* @pcr_idx: index of the PCR. +* +* Return: Same as with tpm_transmit_cmd. +*/ +int tpm2_pcr_reset (struct tpm_chip *chip,u32 pcr_idx) +{ +struct tpm_buf buf; +struct tpm2_null_auth_area auth_area; +int rc; + + rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_PCR_RESET ); +if (rc) +return rc; + +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); + +return rc; +} +struct tpm2_get_random_out {__be16 size;u8 buffer[TPM_MAX_RNG_DATA ];
include/linux/tpm.h +7 -0
@@ -211,6 +211,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,
@@ -399,6 +400,7 @@ static inline u32 tpm2_rc_value(u32 rc)extern int tpm_is_tpm2 (struct tpm_chip *chip);extern int tpm_pcr_read (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digest); +extern int tpm_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);
@@ -417,6 +419,11 @@ static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,return -ENODEV ; } +static inline int tpm_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.30.0.617.g56c4b15f3c-goog
Jarkko Sakkinen
maintainerRe: [PATCH 1/9] tpm: Add support for in-kernel resetting of PCRs
On Sat, Feb 20, 2021 at 01:32:47AM +0000, Matthew Garrett wrote:
Perhaps, prepend with a paragraph elaborating the background just a bit:
"When entering to hibernation, PCR 23 will be used to store a known value. This value defines a policy for the decryption of the key used to encrypt the hibernate image."
At minimum that. You can of course edit this however you see fit.
Quoted 198 lines
Add an internal command for resetting a PCR.
Signed-off-by Matthew Garrett <[email protected]>--- drivers/char/tpm/tpm-interface.c | 28 +++++++++++++++++++++++++ drivers/char/tpm/tpm.h | 2 ++ drivers/char/tpm/tpm1-cmd.c | 34 ++++++++++++++++++++++++++++++ drivers/char/tpm/tpm2-cmd.c | 36 ++++++++++++++++++++++++++++++++ include/linux/tpm.h | 7 +++++++ 5 files changed, 107 insertions(+)drivers/char/tpm/tpm-interface.c +28
-0@@ -342,6 +342,34 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, }EXPORT_SYMBOL_GPL (tpm_pcr_extend); +/** +* tpm_pcr_reset - reset the specified PCR +* @chip: a &struct tpm_chip instance, %NULL for the default chip +* @pcr_idx: the PCR to be reset +* +* Return: same as with tpm_transmit_cmd() +*/ +int tpm_pcr_reset (struct tpm_chip *chip,u32 pcr_idx) +{ +int rc; + + chip =tpm_find_get_ops (chip); +if (!chip) +return -ENODEV ; + +if (chip->flags &TPM_CHIP_FLAG_TPM2 ) { + rc =tpm2_pcr_reset (chip, pcr_idx); + goto out; + } + + rc =tpm1_pcr_reset (chip, pcr_idx,"attempting to reset a PCR" ); + +out: +tpm_put_ops (chip); +return rc; +} +EXPORT_SYMBOL_GPL (tpm_pcr_reset); +/** * tpm_send - send a TPM command * @chip: a &struct tpm_chip instance, %NULL for the default chip drivers/char/tpm/tpm.h +2
-0@@ -176,6 +176,7 @@ int tpm1_get_timeouts(struct tpm_chip *chip);unsigned long tpm1_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm1_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,const u8 *hash,const char *log_msg); +int tpm1_pcr_reset (struct tpm_chip *chip,u32 pcr_idx,const char *log_msg);int tpm1_pcr_read (struct tpm_chip *chip,u32 pcr_idx,u8 *res_buf);ssize_t tpm1_getcap (struct tpm_chip *chip,u32 subcap_id,cap_t *cap,const char *desc,size_t min_cap_length);@@ -220,6 +221,7 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digest, u16 *digest_size_ptr);int tpm2_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digests); +int tpm2_pcr_reset (struct tpm_chip *chip,u32 pcr_idx);int tpm2_get_random (struct tpm_chip *chip,u8 *dest,size_t max);ssize_t tpm2_get_tpm_pt (struct tpm_chip *chip,u32 property_id,u32 *value,const char *desc);drivers/char/tpm/tpm1-cmd.c +34
-0@@ -478,6 +478,40 @@ int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,return rc; } +struct tpm_pcr_selection { +u16 size_of_select; +u8 pcr_select[3 ]; +} __packed; + +#define TPM_ORD_PCR_RESET 200 +int tpm1_pcr_reset (struct tpm_chip *chip,u32 pcr_idx,const char *log_msg) +{ +struct tpm_pcr_selection selection; +struct tpm_buf buf; +int i, rc; +char tmp; + + rc =tpm_buf_init (&buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_PCR_RESET ); +if (rc) +return rc; + + selection.size_of_select =3 ; + +for (i =0 ; i < selection.size_of_select; i++) { + tmp =0 ; +if (pcr_idx /3 == i) { + pcr_idx -= i *8 ; + tmp |=1 << pcr_idx; + } + selection.pcr_select[i] = tmp; + } +tpm_buf_append (&buf, (u8 *)&selection,sizeof (selection)); + + rc =tpm_transmit_cmd (chip, &buf,sizeof (selection), log_msg); +tpm_buf_destroy (&buf); +return rc; +} +#define TPM_ORD_GET_CAP 101ssize_t tpm1_getcap (struct tpm_chip *chip,u32 subcap_id,cap_t *cap,const char *desc,size_t min_cap_length)drivers/char/tpm/tpm2-cmd.c +36
-0@@ -269,6 +269,42 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,return rc; } +/** +* tpm2_pcr_reset() - reset a PCR +* +* @chip: TPM chip to use. +* @pcr_idx: index of the PCR. +* +* Return: Same as with tpm_transmit_cmd. +*/ +int tpm2_pcr_reset (struct tpm_chip *chip,u32 pcr_idx) +{ +struct tpm_buf buf; +struct tpm2_null_auth_area auth_area; +int rc; + + rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_PCR_RESET ); +if (rc) +return rc; + +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); + +return rc; +} +struct tpm2_get_random_out {__be16 size;u8 buffer[TPM_MAX_RNG_DATA ];include/linux/tpm.h +7
-0@@ -211,6 +211,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,@@ -399,6 +400,7 @@ static inline u32 tpm2_rc_value(u32 rc)extern int tpm_is_tpm2 (struct tpm_chip *chip);extern int tpm_pcr_read (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digest); +extern int tpm_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);@@ -417,6 +419,11 @@ static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,return -ENODEV ; } +static inline int tpm_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.30.0.617.g56c4b15f3c-goog
/Jarkko
PATCH 2/9 tpm: Allow PCR 23 to be restricted to kernel-only use
In reply to Matthew GarrettUnder 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.
--- drivers/char/tpm/Kconfig | 10 +++++++++ drivers/char/tpm/tpm-dev-common.c | 8 +++++++ drivers/char/tpm/tpm.h | 21 +++++++++++++++++++ drivers/char/tpm/tpm1-cmd.c | 35 +++++++++++++++++++++++++++++++ drivers/char/tpm/tpm2-cmd.c | 22 +++++++++++++++++++ drivers/char/tpm/tpm2-space.c | 2 +- 6 files changed, 97 insertions(+), 1 deletion(-)
drivers/char/tpm/Kconfig +10 -0
@@ -190,4 +190,14 @@ config TCG_FTPM_TEE This driver proxies for firmware TPM running in TEE. source "drivers/char/tpm/st33zp24/Kconfig" + +config TCG_TPM_RESTRICT_PCR + bool "Restrict userland access to PCR 23" + depends on TCG_TPM + help + If set, block userland from extending or resetting PCR 23. This + allows it 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. endif # TCG_TPM
drivers/char/tpm/tpm-dev-common.c +8 -0
@@ -193,6 +193,14 @@ 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); +else + ret =tpm1_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 +21 -0
@@ -232,6 +232,8 @@ void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);unsigned long tpm2_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm2_probe (struct tpm_chip *chip);int tpm2_get_cc_attrs_tbl (struct tpm_chip *chip); +int tpm_find_and_validate_cc (struct tpm_chip *chip,struct tpm_space *space, +const void *buf,size_t bufsiz);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);
@@ -245,4 +247,23 @@ 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_TPM_RESTRICT_PCR +#define TPM_RESTRICTED_PCR 23 + +int tpm1_cmd_restricted (struct tpm_chip *chip,u8 *buffer,size_t size); +int tpm2_cmd_restricted (struct tpm_chip *chip,u8 *buffer,size_t size); +#else +static inline int tpm1_cmd_restricted (struct tpm_chip *chip,u8 *buffer, +size_t size) +{ +return 0 ; +} + +static inline int tpm2_cmd_restricted (struct tpm_chip *chip,u8 *buffer, +size_t size) +{ +return 0 ; +} +#endif #endif
drivers/char/tpm/tpm1-cmd.c +35 -0
@@ -840,3 +840,38 @@ int tpm1_get_pcr_allocation(struct tpm_chip *chip)return 0 ; } + +#ifdef CONFIG_TCG_TPM_RESTRICT_PCR +int tpm1_cmd_restricted (struct tpm_chip *chip,u8 *buffer,size_t size) +{ +struct tpm_header *header = (struct tpm_header *)buffer; +char len, offset; +u32 *pcr; +int pos; + +switch (be32_to_cpu (header->ordinal)) { +case TPM_ORD_PCR_EXTEND : +if (size < (TPM_HEADER_SIZE +sizeof (u32))) +return -EINVAL ; + pcr = (u32 *)&buffer[TPM_HEADER_SIZE ]; +if (be32_to_cpu (*pcr) ==TPM_RESTRICTED_PCR ) +return -EPERM ; +break ; +case TPM_ORD_PCR_RESET : +if (size < (TPM_HEADER_SIZE +1 )) +return -EINVAL ; + len = buffer[TPM_HEADER_SIZE ]; +if (size < (TPM_HEADER_SIZE +1 + len)) +return -EINVAL ; + offset =TPM_RESTRICTED_PCR /3 ; +if (len < offset) +break ; + pos =TPM_HEADER_SIZE +1 + offset; +if (buffer[pos] & (1 << (TPM_RESTRICTED_PCR -2 * offset))) +return -EPERM ; +break ; + } + +return 0 ; +} +#endif
drivers/char/tpm/tpm2-cmd.c +22 -0
@@ -795,3 +795,25 @@ int tpm2_find_cc(struct tpm_chip *chip, u32 cc)return -1 ; } + +#ifdef CONFIG_TCG_TPM_RESTRICT_PCR +int tpm2_cmd_restricted (struct tpm_chip *chip,u8 *buffer,size_t size) +{ +int cc =tpm_find_and_validate_cc (chip,NULL , buffer, size); +u32 *handle; + +switch (cc) { +case TPM2_CC_PCR_EXTEND : +case TPM2_CC_PCR_RESET : +if (size < (TPM_HEADER_SIZE +sizeof (u32))) +return -EINVAL ; + + handle = (u32 *)&buffer[TPM_HEADER_SIZE ]; +if (be32_to_cpu (*handle) ==TPM_RESTRICTED_PCR ) +return -EPERM ; +break ; + } + +return 0 ; +} +#endif
drivers/char/tpm/tpm2-space.c +1 -1
@@ -262,7 +262,7 @@ 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, +int tpm_find_and_validate_cc (struct tpm_chip *chip,struct tpm_space *space,const void *cmd,size_t len) {
Signature · 1 line
2.30.0.617.g56c4b15f3c-goog
Jarkko Sakkinen
maintainerRe: [PATCH 2/9] tpm: Allow PCR 23 to be restricted to kernel-only use
Quoted 11 lines · Matthew Garrett
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.
Does this leave room to use them *if* they are available? Not saying that this patch set must support them, but neither would like to disclude them from unforseeable future.
Quoted 36 lines
Signed-off-by Matthew Garrett <[email protected]>--- drivers/char/tpm/Kconfig | 10 +++++++++ drivers/char/tpm/tpm-dev-common.c | 8 +++++++ drivers/char/tpm/tpm.h | 21 +++++++++++++++++++ drivers/char/tpm/tpm1-cmd.c | 35 +++++++++++++++++++++++++++++++ drivers/char/tpm/tpm2-cmd.c | 22 +++++++++++++++++++ drivers/char/tpm/tpm2-space.c | 2 +- 6 files changed, 97 insertions(+), 1 deletion(-)drivers/char/tpm/Kconfig +10
-0@@ -190,4 +190,14 @@ config TCG_FTPM_TEE This driver proxies for firmware TPM running in TEE. source "drivers/char/tpm/st33zp24/Kconfig" + +config TCG_TPM_RESTRICT_PCR + bool "Restrict userland access to PCR 23" + depends on TCG_TPM + help + If set, block userland from extending or resetting PCR 23. This + allows it 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. endif # TCG_TPMdrivers/char/tpm/tpm-dev-common.c +0
-0@@ -193,6 +193,14 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf, priv->response_read = false;
drivers/char/tpm/tpm-dev-common.c ssize_t tpm_common_write(struct file *file, const char __user *buf, · line 203
Quoted 10 lines
drivers/char/tpm/tpm-dev-common.c +8
-0*off =0 ; +if (priv->chip->flags &TPM_CHIP_FLAG_TPM2 ) + ret =tpm2_cmd_restricted (priv->chip, priv->data_buffer, size); +else + ret =tpm1_cmd_restricted (priv->chip, priv->data_buffer, size); + +if (ret) + goto out; +
I have to admit my knowledge is limited here. I'm not sure how widely is 23 used by the pre-existing user space in the wild.
Quoted 130 lines
drivers/char/tpm/tpm-dev-common.c +0
-0/* * If in nonblocking mode schedule an async job to send * the command return the size. drivers/char/tpm/tpm.h +21
-0@@ -232,6 +232,8 @@ void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);unsigned long tpm2_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm2_probe (struct tpm_chip *chip);int tpm2_get_cc_attrs_tbl (struct tpm_chip *chip); +int tpm_find_and_validate_cc (struct tpm_chip *chip,struct tpm_space *space, +const void *buf,size_t bufsiz);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);@@ -245,4 +247,23 @@ 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_TPM_RESTRICT_PCR +#define TPM_RESTRICTED_PCR 23 + +int tpm1_cmd_restricted (struct tpm_chip *chip,u8 *buffer,size_t size); +int tpm2_cmd_restricted (struct tpm_chip *chip,u8 *buffer,size_t size); +#else +static inline int tpm1_cmd_restricted (struct tpm_chip *chip,u8 *buffer, +size_t size) +{ +return 0 ; +} + +static inline int tpm2_cmd_restricted (struct tpm_chip *chip,u8 *buffer, +size_t size) +{ +return 0 ; +} +#endif #endif drivers/char/tpm/tpm1-cmd.c +35
-0@@ -840,3 +840,38 @@ int tpm1_get_pcr_allocation(struct tpm_chip *chip)return 0 ; } + +#ifdef CONFIG_TCG_TPM_RESTRICT_PCR +int tpm1_cmd_restricted (struct tpm_chip *chip,u8 *buffer,size_t size) +{ +struct tpm_header *header = (struct tpm_header *)buffer; +char len, offset; +u32 *pcr; +int pos; + +switch (be32_to_cpu (header->ordinal)) { +case TPM_ORD_PCR_EXTEND : +if (size < (TPM_HEADER_SIZE +sizeof (u32))) +return -EINVAL ; + pcr = (u32 *)&buffer[TPM_HEADER_SIZE ]; +if (be32_to_cpu (*pcr) ==TPM_RESTRICTED_PCR ) +return -EPERM ; +break ; +case TPM_ORD_PCR_RESET : +if (size < (TPM_HEADER_SIZE +1 )) +return -EINVAL ; + len = buffer[TPM_HEADER_SIZE ]; +if (size < (TPM_HEADER_SIZE +1 + len)) +return -EINVAL ; + offset =TPM_RESTRICTED_PCR /3 ; +if (len < offset) +break ; + pos =TPM_HEADER_SIZE +1 + offset; +if (buffer[pos] & (1 << (TPM_RESTRICTED_PCR -2 * offset))) +return -EPERM ; +break ; + } + +return 0 ; +} +#endif drivers/char/tpm/tpm2-cmd.c +22
-0@@ -795,3 +795,25 @@ int tpm2_find_cc(struct tpm_chip *chip, u32 cc)return -1 ; } + +#ifdef CONFIG_TCG_TPM_RESTRICT_PCR +int tpm2_cmd_restricted (struct tpm_chip *chip,u8 *buffer,size_t size) +{ +int cc =tpm_find_and_validate_cc (chip,NULL , buffer, size); +u32 *handle; + +switch (cc) { +case TPM2_CC_PCR_EXTEND : +case TPM2_CC_PCR_RESET : +if (size < (TPM_HEADER_SIZE +sizeof (u32))) +return -EINVAL ; + + handle = (u32 *)&buffer[TPM_HEADER_SIZE ]; +if (be32_to_cpu (*handle) ==TPM_RESTRICTED_PCR ) +return -EPERM ; +break ; + } + +return 0 ; +} +#endif drivers/char/tpm/tpm2-space.c +1
-1@@ -262,7 +262,7 @@ 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, +int tpm_find_and_validate_cc (struct tpm_chip *chip,struct tpm_space *space,const void *cmd,size_t len) {Signature · 1 line
2.30.0.617.g56c4b15f3c-goog
What are the consumer use cases anyway? Why wouldn't locality based solution make sense to those use cases where this makes sense.
Finally, where does hibernate make sense? :-)
This comes more down to on how lay out the requirements.
/Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH 2/9] tpm: Allow PCR 23 to be restricted to kernel-only use
In reply to Matthew GarrettQuoted 1 line · Matthew Garrett
+#define
TPM_RESTRICTED_PCR23
As stupid it may sound, I'd just change this to:
#define TPM_PCR_23 23
It documents to the code that we are dealing with PCR 23, which just a plain number doesn't. By naming it as TPM_RESTRICED_TPM you have to unnecessarily xref to its definition. It obfuscates rather than clarifies anything important.
/Jarkko
Re: [PATCH 2/9] tpm: Allow PCR 23 to be restricted to kernel-only use
In reply to Matthew GarrettQuoted 5 lines · Matthew Garrett
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.
I don't understand this ... the localities seem to work fine on all the systems I have ... is this some embedded thing?
Quoted 7 lines
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 seems a bit arbitrary: You're removing this PCR from user space accessibility, but PCR 23 is defined as "Application Support" how can we be sure no application will actually want to use it (and then fail)?
Since PCRs are very scarce, why not use a NV index instead. They're still a bounded resource, but most TPMs have far more of them than they do PCRs, and the address space is much bigger so picking a nice arbitrary 24 bit value reduces the chance of collisions.
James
Re: [PATCH 2/9] tpm: Allow PCR 23 to be restricted to kernel-only use
Quoted 9 lines · James Bottomley
Quoted 5 lines · Matthew Garrett
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.
I don't understand this ... the localities seem to work fine on all the systems I have ... is this some embedded thing?
I haven't made it work on an HP Z440 or a Lenovo P520. So now I'm wondering whether having chipsets with TXT support (even if it's turned off) confuse this point. Sigh. I'd really prefer to use localities than a PCR, so if it works on client platforms I'd be inclined to say we'll do a self-test and go for that, and workstation vendors can just recommend their customers use UPSes or something.
Quoted 11 lines
Quoted 7 lines
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 seems a bit arbitrary: You're removing this PCR from user space accessibility, but PCR 23 is defined as "Application Support" how can we be sure no application will actually want to use it (and then fail)?
Absolutely no way of guaranteeing that, and enabling this option is certainly an ABI break.
Quoted 4 lines
Since PCRs are very scarce, why not use a NV index instead. They're still a bounded resource, but most TPMs have far more of them than they do PCRs, and the address space is much bigger so picking a nice arbitrary 24 bit value reduces the chance of collisions.
How many write cycles do we expect the NV to survive? But I'll find a client system with a TPM and play with locality support there - maybe we can just avoid this problem anyway.
PATCH 3/9 security: keys: trusted: Parse out individual components of the key blob
In reply to Matthew GarrettPerforming any sort of state validation of a sealed TPM blob requires being able to access the individual members in the response. Parse the blob sufficiently to be able to stash pointers to each member, along with the length.
--- include/keys/trusted-type.h | 8 +++ security/keys/trusted-keys/trusted_tpm2.c | 67 ++++++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-)
include/keys/trusted-type.h +8 -0
@@ -16,14 +16,22 @@#define MAX_BLOB_SIZE 512#define MAX_PCRINFO_SIZE 64#define MAX_DIGEST_SIZE 64 +#define MAX_CREATION_DATA 412 +#define MAX_TK 76struct 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 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/trusted_tpm2.c +65 -2
98 lines not on this page · raw message
Signature · 1 line
2.30.0.617.g56c4b15f3c-goog
Jarkko Sakkinen
maintainerRe: [PATCH 3/9] security: keys: trusted: Parse out individual components of the key blob
Quoted 6 lines · Matthew Garrett
Performing any sort of state validation of a sealed TPM blob requires being able to access the individual members in the response. Parse the blob sufficiently to be able to stash pointers to each member, along with the length.
Signed-off-by Matthew Garrett <[email protected]>
I'll just say LGTM for now. Did not see anything obviously wrong in the code change (and does make sense to nitpick minor things just yet).
Need to understand the whole use case just a little bit better.
/Jarkko
Re: [PATCH 3/9] security: keys: trusted: Parse out individual components of the key blob
Quoted 6 lines · Jarkko Sakkinen
Quoted 5 lines · Matthew Garrett
Performing any sort of state validation of a sealed TPM blob requires being able to access the individual members in the response. Parse the blob sufficiently to be able to stash pointers to each member, along with the length.
Quoted 7 lines
Quoted 1 line
Signed-off-by Matthew Garrett <[email protected]>I'll just say LGTM for now. Did not see anything obviously wrong in the code change (and does make sense to nitpick minor things just yet).
Need to understand the whole use case just a little bit better.
I wrote this up with some more detail at
https://mjg59.dreamwidth.org/55845.html - it seemed longer than appropriate for a commit message, but if you'd like more detail somewhere I can certainly add it.
Jarkko Sakkinen
maintainerRe: [PATCH 3/9] security: keys: trusted: Parse out individual components of the key blob
Quoted 13 lines · Matthew Garrett
Quoted 12 lines · Jarkko Sakkinen
Quoted 6 lines · Matthew Garrett
Performing any sort of state validation of a sealed TPM blob requires being able to access the individual members in the response. Parse the blob sufficiently to be able to stash pointers to each member, along with the length.
Signed-off-by Matthew Garrett <[email protected]>I'll just say LGTM for now. Did not see anything obviously wrong in the code change (and does make sense to nitpick minor things just yet).
Quoted 6 lines
Quoted 1 line
Need to understand the whole use case just a little bit better.
I wrote this up with some more detail at
https://mjg59.dreamwidth.org/55845.html - it seemed longer than appropriate for a commit message, but if you'd like more detail somewhere I can certainly add it.
Thanks (bookmarked). I'll read it before reviewing +1 version.
Requiring a config flag is something that slows down adoption in the stock kernels.
Since we are talking about hibernate the decision whether to have this feature set, does not have to be something that needs to be changed dynamically to a running system.
So: maybe the best compromise would be to have it kernel command line option? That way it's easier feature to adapt (e.g. with GRUB configuration) and to enable in the kernel.
/Jarkko
PATCH 4/9 security: keys: trusted: Store the handle of a loaded key
In reply to Matthew GarrettCertain in-kernel operations using a trusted key (such as creation certification) require knowledge of the handle it's loaded at. Keep a copy of that in the payload.
--- include/keys/trusted-type.h | 1 + security/keys/trusted-keys/trusted_tpm2.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-)
include/keys/trusted-type.h +1 -0
@@ -21,6 +21,7 @@struct trusted_key_payload {struct rcu_head rcu; +unsigned int blob_handle;unsigned int key_len;unsigned int blob_len;unsigned int creation_len;
security/keys/trusted-keys/trusted_tpm2.c +4 -2
@@ -272,11 +272,13 @@ static int tpm2_load_cmd(struct tpm_chip *chip, } rc =tpm_send (chip, buf.data,tpm_buf_length (&buf)); -if (!rc) +if (!rc) { *blob_handle =be32_to_cpup ( (__be32 *) &buf.data[TPM_HEADER_SIZE ]); -else + payload->blob_handle = *blob_handle; + }else { goto out; + } rc =tpm2_unpack_blob (payload); out:
Signature · 1 line
2.30.0.617.g56c4b15f3c-goog
Jarkko Sakkinen
maintainerRe: [PATCH 4/9] security: keys: trusted: Store the handle of a loaded key
Quoted 5 lines · Matthew Garrett
Certain in-kernel operations using a trusted key (such as creation certification) require knowledge of the handle it's loaded at. Keep a copy of that in the payload.
Signed-off-by Matthew Garrett <[email protected]>
This looks good to me as well *as a code change*.
/Jarkko
Quoted 41 lines
--- include/keys/trusted-type.h | 1 + security/keys/trusted-keys/trusted_tpm2.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-)include/keys/trusted-type.h +1
-0@@ -21,6 +21,7 @@struct trusted_key_payload {struct rcu_head rcu; +unsigned int blob_handle;unsigned int key_len;unsigned int blob_len;unsigned int creation_len;security/keys/trusted-keys/trusted_tpm2.c +4
-2@@ -272,11 +272,13 @@ static int tpm2_load_cmd(struct tpm_chip *chip, } rc =tpm_send (chip, buf.data,tpm_buf_length (&buf)); -if (!rc) +if (!rc) { *blob_handle =be32_to_cpup ( (__be32 *) &buf.data[TPM_HEADER_SIZE ]); -else + payload->blob_handle = *blob_handle; + }else { goto out; + } rc =tpm2_unpack_blob (payload); out:Signature · 1 line
2.30.0.617.g56c4b15f3c-goog
PATCH 5/9 security: keys: trusted: Allow storage of PCR values in creation data
In reply to Matthew GarrettWhen 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.
--- .../security/keys/trusted-encrypted.rst | 4 +++ 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, 37 insertions(+), 2 deletions(-)
Documentation/security/keys/trusted-encrypted.rst +4 -0
@@ -72,6 +72,10 @@ 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 PCR values to be + included in the PCR creation data. The bit corresponding + to each PCR should be 1 to be included, 0 to be ignored. + 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
@@ -47,6 +47,7 @@ struct trusted_key_options {uint32_t policydigest_len;unsigned char policydigest[MAX_DIGEST_SIZE ];uint32_t policyhandle; +uint32_t creation_pcrs; };extern struct key_type key_type_trusted;
security/keys/trusted-keys/trusted_tpm1.c +9 -0
@@ -709,6 +709,7 @@ enum { Opt_hash, Opt_policydigest, Opt_policyhandle, + Opt_creationpcrs, }; static const match_table_t key_tokens = {
@@ -724,6 +725,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} };
@@ -834,6 +836,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
@@ -124,7 +124,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,unsigned int offset;struct tpm_buf buf;u32 hash; -int i; +int i, j;int rc;for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) {
@@ -181,7 +181,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.30.0.617.g56c4b15f3c-goog
Jarkko Sakkinen
maintainerRe: [PATCH 5/9] security: keys: trusted: Allow storage of PCR values in creation data
Quoted 9 lines · Matthew Garrett
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.
Signed-off-by Matthew Garrett <[email protected]>
LGTM too.
Something popped into mind: could we make PCR 23 reservation dynamic instead of a config option.
E.g. if the user space uses it, then it's dirty and hibernate will fail. I really dislike the static compilation time firewall on it.
/Jarkko
Re: [PATCH 5/9] security: keys: trusted: Allow storage of PCR values in creation data
Quoted 5 lines · Jarkko Sakkinen
Something popped into mind: could we make PCR 23 reservation dynamic instead of a config option.
E.g. if the user space uses it, then it's dirty and hibernate will fail. I really dislike the static compilation time firewall on it.
I don't know the threat model here, but couldn't hibernation then be blocked by userspace using PCR 23 in some way (thus becoming a Denial of Service)? Are elevated permissions required to use PCR values?
--Ben
Re: [PATCH 5/9] security: keys: trusted: Allow storage of PCR values in creation data
In reply to Jarkko SakkinenQuoted 5 lines · Jarkko Sakkinen
Something popped into mind: could we make PCR 23 reservation dynamic instead of a config option.
E.g. if the user space uses it, then it's dirty and hibernate will fail. I really dislike the static compilation time firewall on it.
We can fail hibernation if userland hasn't flagged things, but the concern is that if you hibernate with PCR 23 blocking enabled and then reboot with the blocking disabled, userland can obtain the blob from the hibernation image, extend PCR 23, modify the image and use the key they've recovered to make it look legitimate, enable PCR 23 blocking again and then resume into their own code.
PATCH 6/9 pm: hibernate: Optionally store and verify a hash of the image
In reply to Matthew GarrettCalculate and store a cryptographically secure hash of the hibernation image if SF_VERIFY_IMAGE is set in the hibernation flags. This allows detection of a corrupt image, but has the disadvantage that it requires the blocks be read in in linear order.
--- kernel/power/power.h | 1 + kernel/power/swap.c | 131 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 110 insertions(+), 22 deletions(-)
kernel/power/power.h +1 -0
@@ -168,6 +168,7 @@ extern int swsusp_swap_in_use(void);#define SF_PLATFORM_MODE 1#define SF_NOCOMPRESS_MODE 2#define SF_CRC32_MODE 4 +#define SF_VERIFY_IMAGE 8/* kernel/power/hibernate.c */ extern int swsusp_check (void );
kernel/power/swap.c +109 -22
316 lines not on this page · raw message
Signature · 1 line
2.30.0.617.g56c4b15f3c-goog
Re: [PATCH 6/9] pm: hibernate: Optionally store and verify a hash of the image
Quoted 125 lines · Matthew Garrett
Calculate and store a cryptographically secure hash of the hibernation image if
SF_VERIFY_IMAGEis set in the hibernation flags. This allows detection of a corrupt image, but has the disadvantage that it requires the blocks be read in in linear order.Signed-off-by Matthew Garrett <[email protected]>--- kernel/power/power.h | 1 + kernel/power/swap.c | 131 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 110 insertions(+), 22 deletions(-)kernel/power/power.h +1
-0@@ -168,6 +168,7 @@ extern int swsusp_swap_in_use(void);#define SF_PLATFORM_MODE 1#define SF_NOCOMPRESS_MODE 2#define SF_CRC32_MODE 4 +#define SF_VERIFY_IMAGE 8/* kernel/power/hibernate.c */ extern int swsusp_check (void );kernel/power/swap.c +34
-2@@ -31,6 +31,8 @@#include <linux/kthread.h> #include <linux/crc32.h> #include <linux/ktime.h> +#include <crypto/hash.h> +#include <crypto/sha2.h> #include "power.h" @@ -95,17 +97,20 @@ struct swap_map_page_list {struct swap_map_handle {struct swap_map_page *cur;struct swap_map_page_list *maps; +struct shash_desc *desc;sector_t cur_swap;sector_t first_sector;unsigned int k;unsigned long reqd_free_pages;u32 crc32; +u8 digest[SHA256_DIGEST_SIZE ]; };struct swsusp_header {char reserved[PAGE_SIZE -20 -sizeof (sector_t) -sizeof (int ) - -sizeof (u32)]; +sizeof (u32) -SHA256_DIGEST_SIZE ];u32 crc32; +u8 digest[SHA256_DIGEST_SIZE ];sector_t image;unsigned int flags;/* Flags to pass to the "boot" kernel */ char orig_sig[10 ];@@ -305,6 +310,9 @@ static blk_status_t hib_wait_io(struct hib_bio_batch *hb)* We are relying on the behavior of blk_plug that a thread with * a plug will flush the plug list before sleeping. */ +if (!hb) +return 0 ; +wait_event (hb->wait,atomic_read (&hb->count) ==0 );return blk_status_to_errno (hb->error); }@@ -327,6 +335,8 @@ static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags) swsusp_header->flags = flags;if (flags &SF_CRC32_MODE ) swsusp_header->crc32 = handle->crc32; +memcpy (swsusp_header->digest, handle->digest, +SHA256_DIGEST_SIZE ); error =hib_submit_io (REQ_OP_WRITE ,REQ_SYNC , swsusp_resume_block, swsusp_header,NULL ); }else {@@ -417,6 +427,7 @@ static void release_swap_writer(struct swap_map_handle *handle)static int get_swap_writer (struct swap_map_handle *handle) {int ret; +struct crypto_shash *tfm; ret =swsusp_swap_check ();if (ret) {@@ -437,7 +448,28 @@ static int get_swap_writer(struct swap_map_handle *handle) handle->k =0 ; handle->reqd_free_pages =reqd_free_pages (); handle->first_sector = handle->cur_swap; + + tfm =crypto_alloc_shash ("sha256" ,0 ,0 ); +if (IS_ERR (tfm)) { + ret = -EINVAL ; + goto err_rel; + } + handle->desc =kmalloc (sizeof (struct shash_desc ) + +crypto_shash_descsize (tfm),GFP_KERNEL ); +if (!handle->desc) { + ret = -ENOMEM ; + goto err_rel; + } + + handle->desc->tfm = tfm; + + ret =crypto_shash_init (handle->desc); +if (ret !=0 ) + goto err_free; +return 0 ; +err_free: +kfree (handle->desc); err_rel:release_swap_writer (handle); err_close:@@ -446,7 +478,7 @@ static int get_swap_writer(struct swap_map_handle *handle) }static int swap_write_page (struct swap_map_handle *handle,void *buf, -struct hib_bio_batch *hb) +struct hib_bio_batch *hb,bool hash) {int error =0 ;sector_t offset;
kernel/power/swap.c static int swap_write_page(struct swap_map_handle *handle, void *buf, · line 489
Quoted 4 lines
kernel/power/swap.c +1
-0@@ -454,6 +486,7 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf,if (!handle->cur)return -EINVAL ; offset =alloc_swapdev_block (root_swap); +crypto_shash_update (handle->desc, buf,PAGE_SIZE );
Was this supposed to be conditionalized behind the new parameter, ie:
if (hash) crypto_shash_update()? If so, the same comment would apply to the read as well.
PATCH 7/9 pm: hibernate: Optionally use TPM-backed keys to protect image integrity
In reply to Matthew GarrettA plain hash protects the hibernation image against accidental modification, but in the face of an active attack the hash can simply be updated to match the new image. Generate a random AES key and seal this with the TPM, and use it to encrypt the hash. On resume, the key can be unsealed and used to decrypt the hash. By setting PCR 23 to a specific value we can verify that the key used was generated by the kernel during hibernation and prevent an attacker providing their own key.
--- kernel/power/Kconfig | 15 ++ kernel/power/Makefile | 1 + kernel/power/hibernate.c | 11 +- kernel/power/swap.c | 99 +++---------- kernel/power/swap.h | 38 +++++ kernel/power/tpm.c | 294 +++++++++++++++++++++++++++++++++++++++ kernel/power/tpm.h | 37 +++++ 7 files changed, 417 insertions(+), 78 deletions(-) create mode 100644 kernel/power/swap.h create mode 100644 kernel/power/tpm.c create mode 100644 kernel/power/tpm.h
kernel/power/Kconfig +15 -0
@@ -92,6 +92,21 @@ config HIBERNATION_SNAPSHOT_DEV If in doubt, say Y. +config SECURE_HIBERNATION + bool "Implement secure hibernation support" + depends on HIBERNATION && TCG_TPM + select KEYS + select TRUSTED_KEYS + select CRYPTO + select CRYPTO_SHA256 + select CRYPTO_AES + select TCG_TPM_RESTRICT_PCR + help + Use a TPM-backed key to securely determine whether a hibernation + image was written out by the kernel and has not been tampered with. + This requires a TCG-compliant TPM2 device, which is present on most + modern hardware. + config PM_STD_PARTITION string "Default resume partition" depends on HIBERNATION
kernel/power/Makefile +1 -0
@@ -12,6 +12,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_SECURE_HIBERNATION) += tpm.o obj-$(CONFIG_PM_AUTOSLEEP) += autosleep.o obj-$(CONFIG_PM_WAKELOCKS) += wakelock.o
kernel/power/hibernate.c +9 -2
@@ -34,6 +34,7 @@#include <trace/events/power.h> #include "power.h" +#include "tpm.h" static int nocompress;
@@ -81,7 +82,11 @@ void hibernate_release(void)bool hibernation_available (void ) { -return nohibernate ==0 && !security_locked_down (LOCKDOWN_HIBERNATION ); +if (security_locked_down (LOCKDOWN_HIBERNATION ) && + !secure_hibernation_available ()) +return false; + +return nohibernate ==0 ; }/**
@@ -752,7 +757,9 @@ int hibernate(void) flags |=SF_NOCOMPRESS_MODE ; else flags |= SF_CRC32_MODE; - +#ifdef CONFIG_SECURE_HIBERNATION + flags |=SF_VERIFY_IMAGE ; +#endif pm_pr_dbg ("Writing hibernation image.\n" ); error =swsusp_write (flags);swsusp_free ();
kernel/power/swap.c +23 -76
178 lines not on this page · raw message
kernel/power/swap.h new file +38 -0
@@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include <keys/trusted-type.h> +#include <crypto/sha2.h> + +#ifndef _POWER_SWAP_H +#define _POWER_SWAP_H 1 +/** +* The swap_map_handle structure is used for handling swap in +* a file-alike way +*/ + +struct swap_map_handle { +struct swap_map_page *cur; +struct swap_map_page_list *maps; +struct shash_desc *desc; +sector_t cur_swap; +sector_t first_sector; +unsigned int k; +unsigned long reqd_free_pages; +u32 crc32; +u8 digest[SHA256_DIGEST_SIZE ]; +}; + +struct swsusp_header { +char reserved[PAGE_SIZE -20 -sizeof (sector_t) -sizeof (int ) - +sizeof (u32) -SHA256_DIGEST_SIZE -MAX_BLOB_SIZE - +sizeof (u32)]; +u32 blob_len; +u8 blob[MAX_BLOB_SIZE ]; +u8 digest[SHA256_DIGEST_SIZE ]; +u32 crc32; +sector_t image; +unsigned int flags;/* Flags to pass to the "boot" kernel */ +char orig_sig[10 ]; +char sig[10 ]; +} __packed; + +#endif /* _POWER_SWAP_H */
kernel/power/tpm.c new file +294 -0
294 lines not on this page · raw message
kernel/power/tpm.h new file +37 -0
@@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include "swap.h" + +#ifndef _POWER_TPM_H +#define _POWER_TPM_H + +#ifdef CONFIG_SECURE_HIBERNATION +int secure_hibernation_available (void ); +int swsusp_encrypt_digest (struct swsusp_header *header); +int swsusp_decrypt_digest (struct swsusp_header *header); +int swsusp_digest_setup (struct swap_map_handle *handle); +void swsusp_digest_update (struct swap_map_handle *handle,char *buf, +size_t size); +void swsusp_digest_final (struct swap_map_handle *handle); +#else +static inline int secure_hibernation_available (void ) +{ +return -ENODEV ; +}; +static inline int swsusp_encrypt_digest (struct swsusp_header *header) +{ +return 0 ; +} +static inline int swsusp_decrypt_digest (struct swsusp_header *header) +{ +return 0 ; +} +static inline int swsusp_digest_setup (struct swap_map_handle *handle) +{ +return 0 ; +} +static inline void swsusp_digest_update (struct swap_map_handle *handle, +char *buf,size_t size) {}; +static inline void swsusp_digest_final (struct swap_map_handle *handle) {}; +#endif + +#endif /* _POWER_TPM_H */
Signature · 1 line
2.30.0.617.g56c4b15f3c-goog
Randy Dunlap
reviewerRe: [PATCH 7/9] pm: hibernate: Optionally use TPM-backed keys to protect image integrity
Hi--
Quoted 12 lines · Matthew Garrett
kernel/power/Kconfig +4
-0@@ -92,6 +92,21 @@ config HIBERNATION_SNAPSHOT_DEV If in doubt, say Y. +config SECURE_HIBERNATION + bool "Implement secure hibernation support" + depends on HIBERNATION && TCG_TPM + select KEYS
kernel/power/Kconfig config HIBERNATION_SNAPSHOT_DEV · line 108
Quoted 10 lines
kernel/power/Kconfig +10
-0+ select TRUSTED_KEYS + select CRYPTO + select CRYPTO_SHA256 + select CRYPTO_AES + select TCG_TPM_RESTRICT_PCR + help + Use a TPM-backed key to securely determine whether a hibernation + image was written out by the kernel and has not been tampered with. + This requires a TCG-compliant TPM2 device, which is present on most + modern hardware.
Please follow coding-style for Kconfig files:
from Documentation/process/coding-style.rst, section 10):
For all of the Kconfig* configuration files throughout the source tree, the indentation is somewhat different. Lines under a ``config`` definition are indented with one tab, while help text is indented an additional two spaces.
Also, one feature should not be responsible for enabling other "subsystems," such as KEYS and CRYPTO. They should instead be listed as dependencies.
Signature · 1 line
~Randy
Re: [PATCH 7/9] pm: hibernate: Optionally use TPM-backed keys to protect image integrity
Quoted 4 lines · Randy Dunlap
For all of the Kconfig* configuration files throughout the source tree, the indentation is somewhat different. Lines under a ``config`` definition are indented with one tab, while help text is indented an additional two spaces.
Whoops, I've no idea how I screwed that up. I'll fix for V2, thanks!
Quoted 2 lines
Also, one feature should not be responsible for enabling other "subsystems," such as KEYS and CRYPTO. They should instead be listed as dependencies.
ACK.
PATCH 8/9 pm: hibernate: Verify the digest encryption key
In reply to Matthew GarrettWe 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 ask the TPM to certify that the creation data is accurate and then make sure that the PCR information in the blob 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.
--- include/linux/tpm.h | 1 + kernel/power/tpm.c | 150 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 148 insertions(+), 3 deletions(-)
include/linux/tpm.h +1 -0
@@ -216,6 +216,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,
kernel/power/tpm.c +147 -3
194 lines not on this page · raw message
Signature · 1 line
2.30.0.617.g56c4b15f3c-goog
PATCH 9/9 pm: hibernate: seal the encryption key with a PCR policy
In reply to Matthew GarrettThe 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.
--- include/linux/tpm.h | 4 ++ kernel/power/tpm.c | 161 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 160 insertions(+), 5 deletions(-)
include/linux/tpm.h +4 -0
@@ -225,18 +225,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/tpm.c +156 -5
223 lines not on this page · raw message
Signature · 1 line
2.30.0.617.g56c4b15f3c-goog
Does anyone know if this series is abandoned, or is Matthew planning to do another spin? Email to [email protected] bounces.
-Evan
Jarkko Sakkinen
maintainerQuoted 4 lines · Evan Green
Does anyone know if this series is abandoned, or is Matthew planning to do another spin? Email to [email protected] bounces.
-Evan
Good question.
It could be that because James' patches did not end up to 5.12, but 5.13 instead, Matthew has just put this into hold for a while.
I mean the review comments I gave, were relatively cosmetic.
If I was the author, that's at least I might have done...
/Jarkko
Jarkko Sakkinen
maintainerQuoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Evan Green
Does anyone know if this series is abandoned, or is Matthew planning to do another spin? Email to [email protected] bounces.
-Evan
Quoted 8 lines
Good question.
It could be that because James' patches did not end up to 5.12, but 5.13 instead, Matthew has just put this into hold for a while.
I mean the review comments I gave, were relatively cosmetic.
If I was the author, that's at least I might have done...
Few months can pass surprisingly fast if you have all sorts of stuff going on :-)
/Jarkko
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Jarkko Sakkinen
Quoted 3 lines · Evan Green
Does anyone know if this series is abandoned, or is Matthew planning to do another spin? Email to [email protected] bounces.
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
-Evan
Good question.
It could be that because James' patches did not end up to 5.12, but 5.13 instead, Matthew has just put this into hold for a while.
Ah, thanks for the context. Which patches are those?
Quoted 7 lines
Quoted 4 lines
I mean the review comments I gave, were relatively cosmetic.
If I was the author, that's at least I might have done...
Few months can pass surprisingly fast if you have all sorts of stuff going on :-)
Hehe or an entire year can blur by if you're not careful.
Quoted 2 lines
/Jarkko