security: keys: trusted: fix lost handle flush
Subsystems (4)
James Bottomley
maintainerPATCH 1/1 security: keys: trusted: fix lost handle flush
The original code, before it was moved into security/keys/trusted-keys had a flush after the blob unseal. Without that flush, the volatile handles increase in the TPM until it becomes unusable and the system either has to be rebooted or the TPM volatile area manually flushed. Fix by adding back the lost flush, which we now have to export because of the relocation of the trusted key code may cause the consumer to be modular.
--- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm2-cmd.c | 1 + include/linux/tpm.h | 1 + security/keys/trusted-keys/trusted_tpm2.c | 1 + 4 files changed, 3 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm.h +0 -1
@@ -218,7 +218,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,int tpm2_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digests);int tpm2_get_random (struct tpm_chip *chip,u8 *dest,size_t max); -void tpm2_flush_context (struct tpm_chip *chip,u32 handle);ssize_t tpm2_get_tpm_pt (struct tpm_chip *chip,u32 property_id,u32 *value,const char *desc);
drivers/char/tpm/tpm2-cmd.c +1 -0
@@ -362,6 +362,7 @@ void tpm2_flush_context(struct tpm_chip *chip, u32 handle)tpm_transmit_cmd (chip, &buf,0 ,"flushing context" );tpm_buf_destroy (&buf); } +EXPORT_SYMBOL_GPL (tpm2_flush_context);struct tpm2_get_cap_out {u8 more_data;
include/linux/tpm.h +1 -0
@@ -403,6 +403,7 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,extern int tpm_send (struct tpm_chip *chip,void *cmd,size_t buflen);extern int tpm_get_random (struct tpm_chip *chip,u8 *data,size_t max);extern struct tpm_chip *tpm_default_chip (void ); +void tpm2_flush_context (struct tpm_chip *chip,u32 handle);#else static inline int tpm_is_tpm2 (struct tpm_chip *chip) {
security/keys/trusted-keys/trusted_tpm2.c +1 -0
@@ -309,6 +309,7 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,return rc; rc =tpm2_unseal_cmd (chip, payload, options, blob_handle); +tpm2_flush_context (chip, blob_handle);return rc; }
Signature · 1 line
2.16.4
Quoted 11 lines · James Bottomley
The original code, before it was moved into
security/keys/trusted-keyshad a flush after the blob unseal. Without that flush, the volatile handles increase in the TPM until it becomes unusable and the system either has to be rebooted or the TPM volatile area manually flushed. Fix by adding back the lost flush, which we now have to export because of the relocation of the trusted key code may cause the consumer to be modular.Signed-off-by James Bottomley <[email protected]>Fixes 2e19e10131a0 ("KEYS: trusted: Move TPM2 trusted keys code")
Quoted 58 lines
---
drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm2-cmd.c | 1 + include/linux/tpm.h | 1 + security/keys/trusted-keys/trusted_tpm2.c | 1 + 4 files changed, 3 insertions(+), 1 deletion(-)drivers/char/tpm/tpm.h +0
-1@@ -218,7 +218,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,int tpm2_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digests);int tpm2_get_random (struct tpm_chip *chip,u8 *dest,size_t max); -void tpm2_flush_context (struct tpm_chip *chip,u32 handle);ssize_t tpm2_get_tpm_pt (struct tpm_chip *chip,u32 property_id,u32 *value,const char *desc);drivers/char/tpm/tpm2-cmd.c +1
-0@@ -362,6 +362,7 @@ void tpm2_flush_context(struct tpm_chip *chip, u32 handle)tpm_transmit_cmd (chip, &buf,0 ,"flushing context" );tpm_buf_destroy (&buf); } +EXPORT_SYMBOL_GPL (tpm2_flush_context);struct tpm2_get_cap_out {u8 more_data;include/linux/tpm.h +1
-0@@ -403,6 +403,7 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,extern int tpm_send (struct tpm_chip *chip,void *cmd,size_t buflen);extern int tpm_get_random (struct tpm_chip *chip,u8 *data,size_t max);extern struct tpm_chip *tpm_default_chip (void ); +void tpm2_flush_context (struct tpm_chip *chip,u32 handle);#else static inline int tpm_is_tpm2 (struct tpm_chip *chip) {security/keys/trusted-keys/trusted_tpm2.c +1
-0@@ -309,6 +309,7 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,return rc; rc =tpm2_unseal_cmd (chip, payload, options, blob_handle); +tpm2_flush_context (chip, blob_handle);return rc; }Signature · 1 line
2.16.4
Quoted 9 lines · James Bottomley
The original code, before it was moved into
security/keys/trusted-keyshad a flush after the blob unseal. Without that flush, the volatile handles increase in the TPM until it becomes unusable and the system either has to be rebooted or the TPM volatile area manually flushed. Fix by adding back the lost flush, which we now have to export because of the relocation of the trusted key code may cause the consumer to be modular.
Quoted 3 lines
Signed-off-by James Bottomley <[email protected]>Fixes 2e19e10131a0 ("KEYS: trusted: Move TPM2 trusted keys code")
Overall looks good to me with following minor comment.
Quoted 36 lines
--- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm2-cmd.c | 1 + include/linux/tpm.h | 1 + security/keys/trusted-keys/trusted_tpm2.c | 1 + 4 files changed, 3 insertions(+), 1 deletion(-)drivers/char/tpm/tpm.h +0
-1@@ -218,7 +218,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,int tpm2_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digests);int tpm2_get_random (struct tpm_chip *chip,u8 *dest,size_t max); -void tpm2_flush_context (struct tpm_chip *chip,u32 handle);ssize_t tpm2_get_tpm_pt (struct tpm_chip *chip,u32 property_id,u32 *value,const char *desc);drivers/char/tpm/tpm2-cmd.c +1
-0@@ -362,6 +362,7 @@ void tpm2_flush_context(struct tpm_chip *chip, u32 handle)tpm_transmit_cmd (chip, &buf,0 ,"flushing context" );tpm_buf_destroy (&buf); } +EXPORT_SYMBOL_GPL (tpm2_flush_context);struct tpm2_get_cap_out {u8 more_data;include/linux/tpm.h +0
-0
include/linux/tpm.h extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, · line 406
Quoted 4 lines
include/linux/tpm.h +1
-0@@ -403,6 +403,7 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,extern int tpm_send (struct tpm_chip *chip,void *cmd,size_t buflen);extern int tpm_get_random (struct tpm_chip *chip,u8 *data,size_t max);extern struct tpm_chip *tpm_default_chip (void ); +void tpm2_flush_context (struct tpm_chip *chip,u32 handle);
Shouldn't this be declared as "extern" similar to other APIs? Also, I think we need "#else" part for this API as well.
-Sumit
Quoted 18 lines
include/linux/tpm.h +0
-0#else static inline int tpm_is_tpm2 (struct tpm_chip *chip) {security/keys/trusted-keys/trusted_tpm2.c +1
-1@@ -309,6 +309,7 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,return rc; rc =tpm2_unseal_cmd (chip, payload, options, blob_handle); +tpm2_flush_context (chip, blob_handle);return rc; } --2.16.4
James Bottomley
maintainerQuoted 50 lines · Sumit Garg
Quoted 9 lines · James Bottomley
The original code, before it was moved into
security/keys/trusted-keys had a flush after the blob unseal. Without that flush, the volatile handles increase in the TPM until it becomes unusable and the system either has to be rebooted or the TPM volatile area manually flushed. Fix by adding back the lost flush, which we now have to export because of the relocation of the trusted key code may cause the consumer to be modular.Quoted 4 lines
Signed-off-by James Bottomley <[email protected]om>
Fixes 2e19e10131a0 ("KEYS: trusted: Move TPM2 trusted keys code")Overall looks good to me with following minor comment.
Quoted 32 lines
--- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm2-cmd.c | 1 + include/linux/tpm.h | 1 + security/keys/trusted-keys/trusted_tpm2.c | 1 + 4 files changed, 3 insertions(+), 1 deletion(-)drivers/char/tpm/tpm.h +0
-1@@ -218,7 +218,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,int tpm2_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digests);int tpm2_get_random (struct tpm_chip *chip,u8 *dest,size_t max); -void tpm2_flush_context (struct tpm_chip *chip,u32 handle);ssize_t tpm2_get_tpm_pt (struct tpm_chip *chip,u32 property_id,u32 *value,const char *desc);drivers/char/tpm/tpm2- renamed from drivers/char/tpm/tpm2-cmd.c +0
-0
cmd.c
indexfdb457704aa7..13696deceae8100644drivers/char/tpm/tpm2-cmd.c +1
-0@@ -362,6 +362,7 @@ void tpm2_flush_context(struct tpm_chip *chip, u32 handle) tpm_transmit_cmd(chip, &buf, 0, "flushing context"); tpm_buf_destroy(&buf); } +EXPORT_SYMBOL_GPL (tpm2_flush_context);
Quoted 16 lines
Quoted 7 lines
drivers/char/tpm/tpm2-cmd.c +0
-0struct tpm2_get_cap_out {u8 more_data;include/linux/tpm.h +0
-0include/linux/tpm.h extern int tpm_pcr_extend(struct tpm_chip · line 409
Quoted 7 lines
include/linux/tpm.h +1
-0@@ -403,6 +403,7 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);extern int tpm_get_random (struct tpm_chip *chip,u8 *data,size_t max);extern struct tpm_chip *tpm_default_chip (void ); +void tpm2_flush_context (struct tpm_chip *chip,u32 handle);Shouldn't this be declared as "extern" similar to other APIs?
extern has no meaning for function declarations and our coding guide does say do not use it but I think it's advisory not mandatory so I've no objection to changing it if we prefer consistency over the style guide.
Quoted 1 line
Also, I think we need "#else" part for this API as well.
No, we shouldn't ... the #else part is only for functions which are called when the TPM isn't compiled in. That should never happen with tpm2_flush_context, so if it ever does we want the compile to break.
James
James Bottomley
maintainerQuoted 1 line · James Bottomley
On Fri, 2019-12-13 at 11:10 +0530, Sumit Garg wrote:
[...]
Quoted 5 lines
Quoted 1 line
Also, I think we need "#else" part for this API as well.
No, we shouldn't ... the #else part is only for functions which are called when the TPM isn't compiled in. That should never happen with
tpm2_flush_context, so if it ever does we want the compile to break.
Just on this bit, it looks like we've given insufficient thought to what it means to move TPM internals using code outside of the tpm directory. I think we really need two include/linux files, one tpm.h for external code that going to do stuff which it would do anyway even if a TPM weren't compiled in, like the PCR read and extend. The other tpm-internal.h for code that wants access to TPM internal functions like flushing and session handling and will take care itself of the case where the TPM isn't compiled in, like the trusted key code does through Kconfig dependencies. The test should be easy: if it was originally in drivers/char/tpm/tpm.h it belongs in include/linux/tpm- internals.h
James
Quoted 10 lines · James Bottomley
Quoted 1 line · James Bottomley
On Fri, 2019-12-13 at 11:10 +0530, Sumit Garg wrote:
[...]
Quoted 5 lines
Quoted 1 line
Also, I think we need "#else" part for this API as well.
No, we shouldn't ... the #else part is only for functions which are called when the TPM isn't compiled in. That should never happen with
tpm2_flush_context, so if it ever does we want the compile to break.
Quoted 12 lines
Just on this bit, it looks like we've given insufficient thought to what it means to move TPM internals using code outside of the tpm directory. I think we really need two
include/linuxfiles, onetpm.hfor external code that going to do stuff which it would do anyway even if a TPM weren't compiled in, like the PCR read and extend. The othertpm-internal.hfor code that wants access to TPM internal functions like flushing and session handling and will take care itself of the case where the TPM isn't compiled in, like the trusted key code does throughKconfigdependencies. The test should be easy: if it was originally indrivers/char/tpm/tpm.hit belongs ininclude/linux/tpm-internals.h
Your approach sounds good to me. But how about just moving the APIs that needs to be used independently of TPM compilation to include/linux/tpm-externals.h from drivers/char/tpm/tpm.h. As the initial thoughts while I was moving contents to drivers/char/tpm/tpm.h was to keep a consolidated view of TPM header for a particular user.
Quoted 1 line
Quoted 1 line
Quoted 1 line
+void
tpm2_flush_context(struct tpm_chip *chip, u32 handle);
I agree with you that the above API should remain as it is and should be moved out of the following check:
#if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
...
#else
...
#endif
-Sumit
Quoted 2 lines
James
James Bottomley
maintainerQuoted 27 lines · Sumit Garg
Quoted 12 lines · James Bottomley
Quoted 1 line · James Bottomley
On Fri, 2019-12-13 at 11:10 +0530, Sumit Garg wrote:
[...]
Quoted 6 lines
Quoted 1 line
Also, I think we need "#else" part for this API as well.
No, we shouldn't ... the #else part is only for functions which are called when the TPM isn't compiled in. That should never happen with
tpm2_flush_context, so if it ever does we want the compile to break.Quoted 12 lines
Just on this bit, it looks like we've given insufficient thought to what it means to move TPM internals using code outside of the tpm directory. I think we really need two
include/linuxfiles, onetpm.hfor external code that going to do stuff which it would do anyway even if a TPM weren't compiled in, like the PCR read and extend. The othertpm-internal.hfor code that wants access to TPM internal functions like flushing and session handling and will take care itself of the case where the TPM isn't compiled in, like the trusted key code does throughKconfigdependencies. The test should be easy: if it was originally indrivers/char/tpm/tpm.hit belongs ininclude/linux/tpm-internals.h
Quoted 6 lines
Your approach sounds good to me. But how about just moving the APIs that needs to be used independently of TPM compilation to
include/linux/tpm-externals.hfromdrivers/char/tpm/tpm.h. As the initial thoughts while I was moving contents todrivers/char/tpm/tpm.hwas to keep a consolidated view of TPM header for a particular user.
If we do that, we have to change every current user of tpm.h, because that file was originally only for the external users (mostly PCR extends). I'd rather avoid the churn and keep them using tpm.h, hence the tpm-internal.h proposal.
Quoted 10 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
+void
tpm2_flush_context(struct tpm_chip *chip, u32 handle);I agree with you that the above API should remain as it is and should be moved out of the following check:
#if
defined(CONFIG_TCG_TPM)||defined(CONFIG_TCG_TPM_MODULE)
...
#else
...
#endif
That merely changes where the compile breaks. If it's within it breaks on the actual offending file. If it's without, you don't find out until kernel link time. I'm reasonably ambivalent about this but my HA days have given me a preference for failing fast, so in the file itself.
James
Quoted 10 lines · James Bottomley
The original code, before it was moved into
security/keys/trusted-keyshad a flush after the blob unseal. Without that flush, the volatile handles increase in the TPM until it becomes unusable and the system either has to be rebooted or the TPM volatile area manually flushed. Fix by adding back the lost flush, which we now have to export because of the relocation of the trusted key code may cause the consumer to be modular.Signed-off-by James Bottomley <[email protected]>Fixes 2e19e10131a0 ("KEYS: trusted: Move TPM2 trusted keys code")
Will collect to my rc3 PR, thank you.
/Jarkko