Prepare TPM driver for Trenchboot
Subsystems (5)
Jarkko Sakkinen
maintainerv7 cover Prepare TPM driver for Trenchboot
Overview
========
Start preparing TPM driver for Trenchboot by decoupling tpm_buf from driver and memory allocation.
Backlog (of stuff that will end up to a follow-up patch sets):
1. Static flat duration table for TPM 1.2. Such feature already landed in v6.17 for TPM 2.0. We probably can figure out some sane roof values for the legacy standard, right? This allows to share more code "via data".
2. Not a blocker for sending subsequent versions of Trenchboot patch set but we could look at slicing tpm_tis code at some point of future. It could be even that redundant parts could be "merged" after the feature lands but this is neither a promise (depends on how things look overall).
This patch set does not do a lot heavy-lifting but is beefed enough that it is better not fatten it up too much anymore. Sane buffering is a great starting point.
v7:
- Fixed reported issues.
v6:
- Fixed a memory leak that I found trom trusted keys.
v5:
- Order sobs correctly.
- Fixed a minor glitch in 9/9.
v4:
- PCR patch has been removed because it does not fly without Trenchboot context.
v3:
- I think 6.19 is a better goal for this and thus expanded the series to be a generic Trenchboot enablers series. This version also consolidates my two separate ongoing series.
v2:
- While including fixes from v1, this patch set has a refocus in order to do minimal changes to make code base more compatible Trenchboot.
Jarkko Sakkinen (11):
tpm: Cap the number of PCR banks
tpm: Use -EPERM as fallback error code in tpm_ret_to_err
KEYS: trusted: remove redundant instance of tpm2_hash_map
KEYS: trusted: Fix memory leak in tpm2_load()
KEYS: trusted: Use tpm_ret_to_err() in trusted_tpm2
tpm2-sessions: Remove 'attributes' from tpm_buf_append_auth
tpm2-sessions: Unmask tpm_buf_append_hmac_session()
KEYS: trusted: Open code tpm2_buf_append()
tpm-buf: unify TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW
tpm-buf: Remove chip parameter from tpm_buf_append_handle
tpm-buf: Enable managed and stack allocations.
drivers/char/tpm/tpm-buf.c | 143 ++++++---- drivers/char/tpm/tpm-chip.c | 13 +- drivers/char/tpm/tpm-sysfs.c | 21 +- drivers/char/tpm/tpm.h | 2 - drivers/char/tpm/tpm1-cmd.c | 185 +++++------- drivers/char/tpm/tpm2-cmd.c | 332 +++++++++++----------- drivers/char/tpm/tpm2-sessions.c | 129 ++++----- drivers/char/tpm/tpm2-space.c | 44 ++- drivers/char/tpm/tpm_vtpm_proxy.c | 30 +- include/linux/tpm.h | 74 ++--- security/keys/trusted-keys/trusted_tpm1.c | 36 +-- security/keys/trusted-keys/trusted_tpm2.c | 291 ++++++++----------- 12 files changed, 584 insertions(+), 716 deletions(-)
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v7 1/11 tpm: Cap the number of PCR banks
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation() does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.
---
v7:
- In Ryzen desktop there is total three banks so yep, eight is probably much safer bet than four banks. Fixed the commit message as per remark from Jonathan:
https://lore.kernel.org/linux-integrity/[email protected]/#t
And with that added also reviewed-by.
v6
- No changes.
v5:
- No changes.
v4:
- Revert spurious changes from include/linux/tpm.h.
- Increase TPM2_MAX_BANKS to 8.
- Rename TPM2_MAX_BANKS as TPM2_MAX_PCR_BANKS for the sake of clarity. v3:
- Wrote a more clear commit message.
- Fixed pr_err() message.
v2:
- A new patch.
--- drivers/char/tpm/tpm-chip.c | 13 +++++++++---- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 25 ------------------------- drivers/char/tpm/tpm2-cmd.c | 8 +++----- include/linux/tpm.h | 8 +++++--- 5 files changed, 17 insertions(+), 38 deletions(-)
drivers/char/tpm/tpm-chip.c +9 -4
@@ -559,14 +559,19 @@ static int tpm_add_hwrng(struct tpm_chip *chip)static int tpm_get_pcr_allocation (struct tpm_chip *chip) { -int rc; +int rc =0 ;if (tpm_is_firmware_upgrade (chip))return 0 ; - rc = (chip->flags &TPM_CHIP_FLAG_TPM2 ) ? -tpm2_get_pcr_allocation (chip) : -tpm1_get_pcr_allocation (chip); +if (!(chip->flags &TPM_CHIP_FLAG_TPM2 )) { + chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; + chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; + chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; + chip->nr_allocated_banks =1 ; + }else { + rc =tpm2_get_pcr_allocation (chip); + }if (rc >0 )return -ENODEV ;
drivers/char/tpm/tpm.h +0 -1
@@ -252,7 +252,6 @@ 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);int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max); -int tpm1_get_pcr_allocation (struct tpm_chip *chip);unsigned long tpm_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm_pm_suspend (struct device *dev);int tpm_pm_resume (struct device *dev);
drivers/char/tpm/tpm1-cmd.c +0 -25
@@ -786,28 +786,3 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)return rc; } - -/** -* tpm1_get_pcr_allocation() - initialize the allocated bank -* @chip: TPM chip to use. -* -* The function initializes the SHA1 allocated bank to extend PCR -* -* Return: -* * 0 on success, -* * < 0 on error. -*/ -int tpm1_get_pcr_allocation (struct tpm_chip *chip) -{ - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; - chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; - chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; - chip->nr_allocated_banks =1 ; - -return 0 ; -}
drivers/char/tpm/tpm2-cmd.c +3 -5
@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }
include/linux/tpm.h +5 -3
@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;
@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {
@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];
Signature · 1 line
2.52.0
Re: [PATCH v7 01/11] tpm: Cap the number of PCR banks
Quoted 7 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>
Sorry, I realized that you are expecting me to review.
I have a couple of questions:
- Could you explain better how out of bounds would occur, since one could check the number of PCR banks?
- Is dynamic allocation that bad? And if yes, why?
- Couldn't you just check that the number of available PCR banks is below the threshold you like and keep dynamic allocation?
- Is removing tpm1_get_pcr_allocation() improving code readability?
Thanks
Roberto
Quoted 158 lines
Fixes bcfff8384f6c ("tpm: dynamically allocate the allocated_banks array")Reviewed-by Jonathan McDowell <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- In Ryzen desktop there is total three banks so yep, eight is probably much safer bet than four banks. Fixed the commit message as per remark from Jonathan:https://lore.kernel.org/linux-integrity/[email protected]/#t
And with that added also reviewed-by.
v6
- No changes.
v5:
- No changes.
v4:
- Revert spurious changes frominclude/linux/tpm.h.
- IncreaseTPM2_MAX_BANKSto 8.
- RenameTPM2_MAX_BANKSasTPM2_MAX_PCR_BANKSfor the sake of clarity. v3:
- Wrote a more clear commit message.
- Fixedpr_err()message.
v2:
- A new patch.--- drivers/char/tpm/tpm-chip.c | 13 +++++++++---- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 25 ------------------------- drivers/char/tpm/tpm2-cmd.c | 8 +++----- include/linux/tpm.h | 8 +++++--- 5 files changed, 17 insertions(+), 38 deletions(-)drivers/char/tpm/tpm-chip.c +9
-4@@ -559,14 +559,19 @@ static int tpm_add_hwrng(struct tpm_chip *chip)static int tpm_get_pcr_allocation (struct tpm_chip *chip) { -int rc; +int rc =0 ;if (tpm_is_firmware_upgrade (chip))return 0 ; - rc = (chip->flags &TPM_CHIP_FLAG_TPM2 ) ? -tpm2_get_pcr_allocation (chip) : -tpm1_get_pcr_allocation (chip); +if (!(chip->flags &TPM_CHIP_FLAG_TPM2 )) { + chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; + chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; + chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; + chip->nr_allocated_banks =1 ; + }else { + rc =tpm2_get_pcr_allocation (chip); + }if (rc >0 )return -ENODEV ;drivers/char/tpm/tpm.h +0
-1@@ -252,7 +252,6 @@ 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);int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max); -int tpm1_get_pcr_allocation (struct tpm_chip *chip);unsigned long tpm_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm_pm_suspend (struct device *dev);int tpm_pm_resume (struct device *dev);drivers/char/tpm/tpm1-cmd.c +0
-25@@ -786,28 +786,3 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)return rc; } - -/** -* tpm1_get_pcr_allocation() - initialize the allocated bank -* @chip: TPM chip to use. -* -* The function initializes the SHA1 allocated bank to extend PCR -* -* Return: -* * 0 on success, -* * < 0 on error. -*/ -int tpm1_get_pcr_allocation (struct tpm_chip *chip) -{ - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; - chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; - chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; - chip->nr_allocated_banks =1 ; - -return 0 ; -}drivers/char/tpm/tpm2-cmd.c +3
-5@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }include/linux/tpm.h +5
-3@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];
Jarkko Sakkinen
maintainerRe: [PATCH v7 01/11] tpm: Cap the number of PCR banks
Quoted 11 lines · Roberto Sassu
Quoted 7 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Sorry, I realized that you are expecting me to review.
Quoted 7 lines
I have a couple of questions:
- Could you explain better how out of bounds would occur, since one could check the number of PCR banks?
- Is dynamic allocation that bad? And if yes, why?
- Couldn't you just check that the number of available PCR banks is below the threshold you like and keep dynamic allocation?
- Is removingtpm1_get_pcr_allocation()improving code readability?
nr_possible_banks is read from external source i.e., neither kernel nor CPU fully control its value. This causes *uncontrolled* dynamic allocation. Thus, it must be capped to some value.
Quoted 4 lines
Thanks
Roberto
BR, Jarkko
Re: [PATCH v7 01/11] tpm: Cap the number of PCR banks
Quoted 20 lines · Jarkko Sakkinen
Quoted 11 lines · Roberto Sassu
Quoted 7 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Sorry, I realized that you are expecting me to review.
Quoted 7 lines
I have a couple of questions:
- Could you explain better how out of bounds would occur, since one could check the number of PCR banks?
- Is dynamic allocation that bad? And if yes, why?
- Couldn't you just check that the number of available PCR banks is below the threshold you like and keep dynamic allocation?
- Is removingtpm1_get_pcr_allocation()improving code readability?
Quoted 3 lines
nr_possible_banksis read from external source i.e., neither kernel nor CPU fully control its value. This causes *uncontrolled* dynamic allocation. Thus, it must be capped to some value.
Sure, I'm fine with capping. Isn't that enough?
Thanks
Roberto
Quoted 5 lines
Quoted 3 lines
Thanks
Roberto
BR, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v7 01/11] tpm: Cap the number of PCR banks
Quoted 21 lines · Roberto Sassu
Quoted 20 lines · Jarkko Sakkinen
Quoted 18 lines · Roberto Sassu
Quoted 7 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Sorry, I realized that you are expecting me to review.
I have a couple of questions:
- Could you explain better how out of bounds would occur, since one could check the number of PCR banks?
- Is dynamic allocation that bad? And if yes, why?
- Couldn't you just check that the number of available PCR banks is below the threshold you like and keep dynamic allocation?
- Is removingtpm1_get_pcr_allocation()improving code readability?
Quoted 5 lines
Quoted 3 lines
nr_possible_banksis read from external source i.e., neither kernel nor CPU fully control its value. This causes *uncontrolled* dynamic allocation. Thus, it must be capped to some value.Sure, I'm fine with capping. Isn't that enough?
It makes sense to make the whole memory allocation then infallible, especially since it does not have much effect on diff. And it has not significant effect on memory usage either.
But I do see one completely spurious and actually unintended change that I spotted: tpm1_get_pcr_allocation. It there's no intention doing this it has just carried over the series.
I reverted that part, which make it look like a proper bug fix:
drivers/char/tpm/tpm1-cmd.c +0 -5
@@ -799,11 +799,6 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)*/ int tpm1_get_pcr_allocation (struct tpm_chip *chip) { - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ;
drivers/char/tpm/tpm2-cmd.c +3 -5
@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }
include/linux/tpm.h +5 -3
@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;
@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {
@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];
Quoted 11 lines
Thanks
Roberto
Quoted 5 lines
Quoted 3 lines
Thanks
Roberto
BR, Jarkko
BR, Jarkko
Roberto Sassu
Reviewed-byRe: [PATCH v7 01/11] tpm: Cap the number of PCR banks
Quoted 26 lines · Jarkko Sakkinen
Quoted 25 lines · Roberto Sassu
Quoted 23 lines · Jarkko Sakkinen
Quoted 11 lines · Roberto Sassu
Quoted 7 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Sorry, I realized that you are expecting me to review.
Quoted 7 lines
I have a couple of questions:
- Could you explain better how out of bounds would occur, since one could check the number of PCR banks?
- Is dynamic allocation that bad? And if yes, why?
- Couldn't you just check that the number of available PCR banks is below the threshold you like and keep dynamic allocation?
- Is removingtpm1_get_pcr_allocation()improving code readability?
nr_possible_banksis read from external source i.e., neither kernel nor CPU fully control its value. This causes *uncontrolled* dynamic allocation. Thus, it must be capped to some value.
Quoted 5 lines
Quoted 1 line
Sure, I'm fine with capping. Isn't that enough?
It makes sense to make the whole memory allocation then infallible, especially since it does not have much effect on diff. And it has not significant effect on memory usage either.
Ok. In that case (even if it does not get in):
Roberto
Quoted 89 lines
But I do see one completely spurious and actually unintended change that I spotted:
tpm1_get_pcr_allocation. It there's no intention doing this it has just carried over the series.I reverted that part, which make it look like a proper bug fix:
drivers/char/tpm/tpm1-cmd.c +0
-5@@ -799,11 +799,6 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)*/ int tpm1_get_pcr_allocation (struct tpm_chip *chip) { - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ;drivers/char/tpm/tpm2-cmd.c +3
-5@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }include/linux/tpm.h +5
-3@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];Quoted 13 lines
Thanks
Roberto
Quoted 5 lines
Quoted 3 lines
Thanks
Roberto
BR, Jarkko
BR, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v7 01/11] tpm: Cap the number of PCR banks
Quoted 29 lines · Roberto Sassu
Quoted 28 lines · Jarkko Sakkinen
Quoted 26 lines · Roberto Sassu
Quoted 20 lines · Jarkko Sakkinen
Quoted 11 lines · Roberto Sassu
Quoted 7 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Sorry, I realized that you are expecting me to review.
Quoted 7 lines
I have a couple of questions:
- Could you explain better how out of bounds would occur, since one could check the number of PCR banks?
- Is dynamic allocation that bad? And if yes, why?
- Couldn't you just check that the number of available PCR banks is below the threshold you like and keep dynamic allocation?
- Is removingtpm1_get_pcr_allocation()improving code readability?Quoted 3 lines
nr_possible_banksis read from external source i.e., neither kernel nor CPU fully control its value. This causes *uncontrolled* dynamic allocation. Thus, it must be capped to some value.Sure, I'm fine with capping. Isn't that enough?
Quoted 7 lines
Quoted 3 lines
It makes sense to make the whole memory allocation then infallible, especially since it does not have much effect on diff. And it has not significant effect on memory usage either.
Ok. In that case (even if it does not get in):
Reviewed-by Roberto Sassu <[email protected]>
Thank you :-) It's not a serious but categorilly it is still a bug, and these types guards of them help to detect more serious bugs that cause weird values to passed on to otherwise legit code (and that makes it also utilitarily useful).
BR, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v7 01/11] tpm: Cap the number of PCR banks
In reply to Roberto SassuQuoted 29 lines · Roberto Sassu
Quoted 28 lines · Jarkko Sakkinen
Quoted 26 lines · Roberto Sassu
Quoted 20 lines · Jarkko Sakkinen
Quoted 11 lines · Roberto Sassu
Quoted 7 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Sorry, I realized that you are expecting me to review.
Quoted 7 lines
I have a couple of questions:
- Could you explain better how out of bounds would occur, since one could check the number of PCR banks?
- Is dynamic allocation that bad? And if yes, why?
- Couldn't you just check that the number of available PCR banks is below the threshold you like and keep dynamic allocation?
- Is removingtpm1_get_pcr_allocation()improving code readability?Quoted 3 lines
nr_possible_banksis read from external source i.e., neither kernel nor CPU fully control its value. This causes *uncontrolled* dynamic allocation. Thus, it must be capped to some value.Sure, I'm fine with capping. Isn't that enough?
Quoted 7 lines
Quoted 3 lines
It makes sense to make the whole memory allocation then infallible, especially since it does not have much effect on diff. And it has not significant effect on memory usage either.
Ok. In that case (even if it does not get in):
Reviewed-by Roberto Sassu <[email protected]>
https://lore.kernel.org/linux-integrity/[email protected]/T/#u
I renewed the PR as I had to drop one patch so it's there in place :-)
Thanks again for reviewed-by.
BR, Jarkko
Re: [PATCH v7 01/11] tpm: Cap the number of PCR banks
In reply to Jarkko SakkinenQuoted 168 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Fixes bcfff8384f6c ("tpm: dynamically allocate the allocated_banks array")Reviewed-by Jonathan McDowell <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- In Ryzen desktop there is total three banks so yep, eight is probably much safer bet than four banks. Fixed the commit message as per remark from Jonathan:https://lore.kernel.org/linux-integrity/[email protected]/#t
And with that added also reviewed-by.
v6
- No changes.
v5:
- No changes.
v4:
- Revert spurious changes frominclude/linux/tpm.h.
- IncreaseTPM2_MAX_BANKSto 8.
- RenameTPM2_MAX_BANKSasTPM2_MAX_PCR_BANKSfor the sake of clarity. v3:
- Wrote a more clear commit message.
- Fixedpr_err()message.
v2:
- A new patch.--- drivers/char/tpm/tpm-chip.c | 13 +++++++++---- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 25 ------------------------- drivers/char/tpm/tpm2-cmd.c | 8 +++----- include/linux/tpm.h | 8 +++++--- 5 files changed, 17 insertions(+), 38 deletions(-)drivers/char/tpm/tpm-chip.c +9
-4@@ -559,14 +559,19 @@ static int tpm_add_hwrng(struct tpm_chip *chip)static int tpm_get_pcr_allocation (struct tpm_chip *chip) { -int rc; +int rc =0 ;if (tpm_is_firmware_upgrade (chip))return 0 ; - rc = (chip->flags &TPM_CHIP_FLAG_TPM2 ) ? -tpm2_get_pcr_allocation (chip) : -tpm1_get_pcr_allocation (chip); +if (!(chip->flags &TPM_CHIP_FLAG_TPM2 )) { + chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; + chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; + chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; + chip->nr_allocated_banks =1 ; + }else { + rc =tpm2_get_pcr_allocation (chip); + }if (rc >0 )return -ENODEV ;drivers/char/tpm/tpm.h +0
-1@@ -252,7 +252,6 @@ 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);int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max); -int tpm1_get_pcr_allocation (struct tpm_chip *chip);unsigned long tpm_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm_pm_suspend (struct device *dev);int tpm_pm_resume (struct device *dev);drivers/char/tpm/tpm1-cmd.c +0
-25@@ -786,28 +786,3 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)return rc; } - -/** -* tpm1_get_pcr_allocation() - initialize the allocated bank -* @chip: TPM chip to use. -* -* The function initializes the SHA1 allocated bank to extend PCR -* -* Return: -* * 0 on success, -* * < 0 on error. -*/ -int tpm1_get_pcr_allocation (struct tpm_chip *chip) -{ - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; - chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; - chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; - chip->nr_allocated_banks =1 ; - -return 0 ; -}drivers/char/tpm/tpm2-cmd.c +3
-5@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }include/linux/tpm.h +5
-3@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];Signature · 1 line
2.52.0
Hi Jarkko Sakkinen,
Greetings!
I used Syzkaller and found that there is KASAN: invalid-free in tpm_dev_release in linux-tpmdd branch tpmdd-next-6.19-rc1-v3.
After bisection and the first bad commit is:
"83f6ace27d21 tpm: Cap the number of PCR banks
"
All detailed into can be found at:
https://github.com/laifryiee/syzkaller_logs/tree/main/251202_193851_tpm_dev_release
Syzkaller repro code:
https://github.com/laifryiee/syzkaller_logs/tree/main/251202_193851_tpm_dev_release/repro.c
Syzkaller repro syscall steps:
https://github.com/laifryiee/syzkaller_logs/tree/main/251202_193851_tpm_dev_release/repro.prog
Syzkaller report:
https://github.com/laifryiee/syzkaller_logs/tree/main/251202_193851_tpm_dev_release/repro.report
Kconfig(make olddefconfig):
https://github.com/laifryiee/syzkaller_logs/tree/main/251202_193851_tpm_dev_release/kconfig_origin
Bisect info:
https://github.com/laifryiee/syzkaller_logs/tree/main/251202_193851_tpm_dev_release/bisect_info.log
bzImage:
https://github.com/laifryiee/syzkaller_logs/raw/refs/heads/main/251202_193851_tpm_dev_release/bzImage_tpmdd-next-6.19-rc1-v3
Issue dmesg:
https://github.com/laifryiee/syzkaller_logs/blob/main/251202_193851_tpm_dev_release/tpmdd-next-6.19-rc1-v3_dmesg.log
"
[ 18.405712] ================================================================== [ 18.406210] BUG: KASAN: invalid-free in tpm_dev_release+0xdc/0x110 [ 18.406657] Free of addr ff11000011572d38 by task repro/729 [ 18.407037] [ 18.407156] CPU: 1 UID: 0 PID: 729 Comm: repro Not tainted 6.18.0-tpmdd-next-6+ #1 PREEMPT(voluntary) [ 18.407167] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.q4 [ 18.407173] Call Trace: [ 18.407177] <TASK> [ 18.407180] dump_stack_lvl+0xea/0x150 [ 18.407197] print_report+0xce/0x610 [ 18.407210] ? kasan_complete_mode_report_info+0x40/0x200 [ 18.407220] ? tpm_dev_release+0xdc/0x110 [ 18.407232] kasan_report_invalid_free+0xa7/0xd0 [ 18.407245] ? tpm_dev_release+0xdc/0x110 [ 18.407259] ? tpm_dev_release+0xdc/0x110 [ 18.407271] check_slab_allocation+0x128/0x140 [ 18.407281] __kasan_slab_pre_free+0xd/0x20 [ 18.407291] kfree+0x119/0x620 [ 18.407300] ? tpm_dev_release+0xdc/0x110 [ 18.407311] ? do_wait_intr_irq+0x1e0/0x230 [ 18.407326] ? __pfx_tpm_dev_release+0x10/0x10 [ 18.407340] tpm_dev_release+0xdc/0x110 [ 18.407351] ? tpm_dev_release+0xdc/0x110 [ 18.407363] device_release+0xb6/0x260 [ 18.407374] kobject_put+0x22d/0x550 [ 18.407383] put_device+0x29/0x40 [ 18.407391] vtpm_proxy_fops_release+0x152/0x1e0 [ 18.407403] ? __pfx_vtpm_proxy_fops_release+0x10/0x10 [ 18.407412] __fput+0x41f/0xb70 [ 18.407435] ____fput+0x22/0x30 [ 18.407449] task_work_run+0x19e/0x2b0 [ 18.407467] ? __pfx_task_work_run+0x10/0x10 [ 18.407480] ? __sanitizer_cov_trace_const_cmp4+0x1a/0x20 [ 18.407490] ? switch_task_namespaces+0xdd/0x130 [ 18.407502] do_exit+0x893/0x28c0 [ 18.407519] ? do_group_exit+0x1d8/0x2c0 [ 18.407532] ? __pfx_do_exit+0x10/0x10 [ 18.407545] ? __this_cpu_preempt_check+0x21/0x30 [ 18.407555] ? _raw_spin_unlock_irq+0x2c/0x60 [ 18.407571] ? lockdep_hardirqs_on+0x89/0x110 [ 18.407589] do_group_exit+0xe4/0x2c0 [ 18.407602] __x64_sys_exit_group+0x4d/0x60 [ 18.407615] x64_sys_call+0x21a2/0x21b0 [ 18.407627] do_syscall_64+0x6d/0x450 [ 18.407641] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 18.407650] RIP: 0033:0x7f1331118a4d [ 18.407658] Code: Unable to access opcode bytes at 0x7f1331118a23. [ 18.407662] RSP: 002b:00007ffec868c668 EFLAGS: 00000246 ORIG_RAX: 00000000000000e7 [ 18.407671] RAX: ffffffffffffffda RBX: 00007f13311f69e0 RCX: 00007f1331118a4d [ 18.407677] RDX: 00000000000000e7 RSI: ffffffffffffff80 RDI: 0000000000000000 [ 18.407683] RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000020 [ 18.407688] R10: 00007ffec868c510 R11: 0000000000000246 R12: 00007f13311f69e0 [ 18.407694] R13: 00007f13311fbf00 R14: 0000000000000001 R15: 00007f13311fbee8 [ 18.407707] </TASK> [ 18.407710] [ 18.423610] Allocated by task 729: [ 18.423853] kasan_save_stack+0x2c/0x60 [ 18.424122] kasan_save_track+0x18/0x40 [ 18.424392] kasan_save_alloc_info+0x3c/0x50 [ 18.424690] __kasan_kmalloc+0x88/0xa0 [ 18.424954] __kmalloc_cache_noprof+0x2b1/0x840 [ 18.425271] tpm_chip_alloc+0x56/0x4c0 [ 18.425540] vtpmx_fops_ioctl+0x2d0/0x830 [ 18.425819] __x64_sys_ioctl+0x1bf/0x220 [ 18.426100] x64_sys_call+0x1280/0x21b0 [ 18.426366] do_syscall_64+0x6d/0x450 [ 18.426630] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 18.426980] [ 18.427098] The buggy address belongs to the object at ff11000011572000 [ 18.427098] which belongs to the cache kmalloc-4k of size 4096 [ 18.427931] The buggy address is located 3384 bytes inside of [ 18.427931] 3528-byte region [ff11000011572000, ff11000011572dc8) [ 18.428720] [ 18.428836] The buggy address belongs to the physical page: [ 18.429210] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x11570 [ 18.429744] head: order:3 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0 [ 18.430258] anon flags: 0xfffffc0000040(head|node=0|zone=1|lastcpupid=0x1fffff) [ 18.430754] page_type: f5(slab) [ 18.430982] raw: 000fffffc0000040 ff1100000ac38140 0000000000000000 dead000000000001 [ 18.431505] raw: 0000000000000000 0000000080040004 00000000f5000000 0000000000000000 [ 18.432025] head: 000fffffc0000040 ff1100000ac38140 0000000000000000 dead000000000001 [ 18.432549] head: 0000000000000000 0000000080040004 00000000f5000000 0000000000000000 [ 18.433077] head: 000fffffc0000003 ffd4000000455c01 00000000ffffffff 00000000ffffffff [ 18.433605] head: ffffffffffffffff 0000000000000000 00000000ffffffff 0000000000000008 [ 18.434126] page dumped because: kasan: bad access detected [ 18.434504] [ 18.434621] Memory state around the buggy address: [ 18.434950] ff11000011572c00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 18.435438] ff11000011572c80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 18.435925] >ff11000011572d00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 18.436399] ^ [ 18.436744] ff11000011572d80: 00 00 00 00 00 00 00 00 00 fc fc fc fc fc fc fc [ 18.437229] ff11000011572e00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc [ 18.437709] ==================================================================
"
Hope this cound be insightful to you.
Regards,
Yi Lai
---
If you don't need the following environment to reproduce the problem or if you already have one reproduced environment, please ignore the following information.
How to reproduce:
git clone https://gitlab.com/xupengfe/repro_vm_env.git
cd repro_vm_env
tar -xvf repro_vm_env.tar.gz
cd repro_vm_env; ./start3.sh // it needs qemu-system-x86_64 and I used v7.1.0
// start3.sh will load bzImage_2241ab53cbb5cdb08a6b2d4688feb13971058f65 v6.2-rc5 kernel
// You could change the bzImage_xxx as you want
// Maybe you need to remove line "-drive if=pflash,format=raw,readonly=on,file=./OVMF_CODE.fd \" for different qemu version
You could use below command to log in, there is no password for root.
ssh -p 10023 root@localhost
After login vm(virtual machine) successfully, you could transfer reproduced binary to the vm by below way, and reproduce the problem in vm:
gcc -pthread -o repro repro.c
scp -P 10023 repro root@localhost:/root/
Get the bzImage for target kernel:
Please use target kconfig and copy it to kernel_src/.config make olddefconfig
make -jx bzImage //x should equal or less than cpu num your pc has
Fill the bzImage file into above start3.sh to load the target kernel in vm.
Tips:
If you already have qemu-system-x86_64, please ignore below info.
If you want to install qemu v7.1.0 version:
git clone https://github.com/qemu/qemu.git
cd qemu
git checkout -f v7.1.0
mkdir build
cd build
yum install -y ninja-build.x86_64
yum -y install libslirp-devel.x86_64
../configure --target-list=x86_64-softmmu --enable-kvm --enable-vnc --enable-gtk --enable-sdl --enable-usb-redir --enable-slirp make
make install
Jarkko Sakkinen
maintainerRe: [PATCH v7 01/11] tpm: Cap the number of PCR banks
Quoted 172 lines · Yi
Quoted 168 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Fixes bcfff8384f6c ("tpm: dynamically allocate the allocated_banks array")Reviewed-by Jonathan McDowell <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- In Ryzen desktop there is total three banks so yep, eight is probably much safer bet than four banks. Fixed the commit message as per remark from Jonathan:https://lore.kernel.org/linux-integrity/[email protected]/#t
And with that added also reviewed-by.
v6
- No changes.
v5:
- No changes.
v4:
- Revert spurious changes frominclude/linux/tpm.h.
- IncreaseTPM2_MAX_BANKSto 8.
- RenameTPM2_MAX_BANKSasTPM2_MAX_PCR_BANKSfor the sake of clarity. v3:
- Wrote a more clear commit message.
- Fixedpr_err()message.
v2:
- A new patch.--- drivers/char/tpm/tpm-chip.c | 13 +++++++++---- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 25 ------------------------- drivers/char/tpm/tpm2-cmd.c | 8 +++----- include/linux/tpm.h | 8 +++++--- 5 files changed, 17 insertions(+), 38 deletions(-)drivers/char/tpm/tpm-chip.c +9
-4@@ -559,14 +559,19 @@ static int tpm_add_hwrng(struct tpm_chip *chip)static int tpm_get_pcr_allocation (struct tpm_chip *chip) { -int rc; +int rc =0 ;if (tpm_is_firmware_upgrade (chip))return 0 ; - rc = (chip->flags &TPM_CHIP_FLAG_TPM2 ) ? -tpm2_get_pcr_allocation (chip) : -tpm1_get_pcr_allocation (chip); +if (!(chip->flags &TPM_CHIP_FLAG_TPM2 )) { + chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; + chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; + chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; + chip->nr_allocated_banks =1 ; + }else { + rc =tpm2_get_pcr_allocation (chip); + }if (rc >0 )return -ENODEV ;drivers/char/tpm/tpm.h +0
-1@@ -252,7 +252,6 @@ 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);int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max); -int tpm1_get_pcr_allocation (struct tpm_chip *chip);unsigned long tpm_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm_pm_suspend (struct device *dev);int tpm_pm_resume (struct device *dev);drivers/char/tpm/tpm1-cmd.c +0
-25@@ -786,28 +786,3 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)return rc; } - -/** -* tpm1_get_pcr_allocation() - initialize the allocated bank -* @chip: TPM chip to use. -* -* The function initializes the SHA1 allocated bank to extend PCR -* -* Return: -* * 0 on success, -* * < 0 on error. -*/ -int tpm1_get_pcr_allocation (struct tpm_chip *chip) -{ - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; - chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; - chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; - chip->nr_allocated_banks =1 ; - -return 0 ; -}drivers/char/tpm/tpm2-cmd.c +3
-5@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }include/linux/tpm.h +5
-3@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];Signature · 1 line
2.52.0Hi Jarkko Sakkinen,
Quoted 7 lines
Greetings!
I used Syzkaller and found that there is KASAN: invalid-free in
tpm_dev_releasein linux-tpmdd branch tpmdd-next-6.19-rc1-v3.After bisection and the first bad commit is:
"83f6ace27d21tpm: Cap the number of PCR banks
Thank you. I updated the patch in my branch:
Possible to re-test (I can add your tested-by afterwards)?
BR, Jarkko
Re: [PATCH v7 01/11] tpm: Cap the number of PCR banks
Quoted 177 lines · Jarkko Sakkinen
Quoted 176 lines · Yi
Quoted 168 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Fixes bcfff8384f6c ("tpm: dynamically allocate the allocated_banks array")Reviewed-by Jonathan McDowell <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- In Ryzen desktop there is total three banks so yep, eight is probably much safer bet than four banks. Fixed the commit message as per remark from Jonathan:https://lore.kernel.org/linux-integrity/[email protected]/#t
And with that added also reviewed-by.
v6
- No changes.
v5:
- No changes.
v4:
- Revert spurious changes frominclude/linux/tpm.h.
- IncreaseTPM2_MAX_BANKSto 8.
- RenameTPM2_MAX_BANKSasTPM2_MAX_PCR_BANKSfor the sake of clarity. v3:
- Wrote a more clear commit message.
- Fixedpr_err()message.
v2:
- A new patch.--- drivers/char/tpm/tpm-chip.c | 13 +++++++++---- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 25 ------------------------- drivers/char/tpm/tpm2-cmd.c | 8 +++----- include/linux/tpm.h | 8 +++++--- 5 files changed, 17 insertions(+), 38 deletions(-)drivers/char/tpm/tpm-chip.c +9
-4@@ -559,14 +559,19 @@ static int tpm_add_hwrng(struct tpm_chip *chip)static int tpm_get_pcr_allocation (struct tpm_chip *chip) { -int rc; +int rc =0 ;if (tpm_is_firmware_upgrade (chip))return 0 ; - rc = (chip->flags &TPM_CHIP_FLAG_TPM2 ) ? -tpm2_get_pcr_allocation (chip) : -tpm1_get_pcr_allocation (chip); +if (!(chip->flags &TPM_CHIP_FLAG_TPM2 )) { + chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; + chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; + chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; + chip->nr_allocated_banks =1 ; + }else { + rc =tpm2_get_pcr_allocation (chip); + }if (rc >0 )return -ENODEV ;drivers/char/tpm/tpm.h +0
-1@@ -252,7 +252,6 @@ 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);int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max); -int tpm1_get_pcr_allocation (struct tpm_chip *chip);unsigned long tpm_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm_pm_suspend (struct device *dev);int tpm_pm_resume (struct device *dev);drivers/char/tpm/tpm1-cmd.c +0
-25@@ -786,28 +786,3 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)return rc; } - -/** -* tpm1_get_pcr_allocation() - initialize the allocated bank -* @chip: TPM chip to use. -* -* The function initializes the SHA1 allocated bank to extend PCR -* -* Return: -* * 0 on success, -* * < 0 on error. -*/ -int tpm1_get_pcr_allocation (struct tpm_chip *chip) -{ - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; - chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; - chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; - chip->nr_allocated_banks =1 ; - -return 0 ; -}drivers/char/tpm/tpm2-cmd.c +3
-5@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }include/linux/tpm.h +5
-3@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];Signature · 1 line
2.52.0Hi Jarkko Sakkinen,
Greetings!
I used Syzkaller and found that there is KASAN: invalid-free in
tpm_dev_releasein linux-tpmdd branch tpmdd-next-6.19-rc1-v3.
Quoted 8 lines
Quoted 3 lines
After bisection and the first bad commit is:
"83f6ace27d21tpm: Cap the number of PCR banksThank you. I updated the patch in my branch:
Re-test using the latest branch. Issue cannot be reproduced using the same binary.
Regards,
Yi Lai
Quoted 3 lines
Possible to re-test (I can add your tested-by afterwards)?
BR, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v7 01/11] tpm: Cap the number of PCR banks
Quoted 184 lines · Yi
Quoted 183 lines · Jarkko Sakkinen
Quoted 172 lines · Yi
Quoted 168 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Fixes bcfff8384f6c ("tpm: dynamically allocate the allocated_banks array")Reviewed-by Jonathan McDowell <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- In Ryzen desktop there is total three banks so yep, eight is probably much safer bet than four banks. Fixed the commit message as per remark from Jonathan:https://lore.kernel.org/linux-integrity/[email protected]/#t
And with that added also reviewed-by.
v6
- No changes.
v5:
- No changes.
v4:
- Revert spurious changes frominclude/linux/tpm.h.
- IncreaseTPM2_MAX_BANKSto 8.
- RenameTPM2_MAX_BANKSasTPM2_MAX_PCR_BANKSfor the sake of clarity. v3:
- Wrote a more clear commit message.
- Fixedpr_err()message.
v2:
- A new patch.--- drivers/char/tpm/tpm-chip.c | 13 +++++++++---- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 25 ------------------------- drivers/char/tpm/tpm2-cmd.c | 8 +++----- include/linux/tpm.h | 8 +++++--- 5 files changed, 17 insertions(+), 38 deletions(-)drivers/char/tpm/tpm-chip.c +9
-4@@ -559,14 +559,19 @@ static int tpm_add_hwrng(struct tpm_chip *chip)static int tpm_get_pcr_allocation (struct tpm_chip *chip) { -int rc; +int rc =0 ;if (tpm_is_firmware_upgrade (chip))return 0 ; - rc = (chip->flags &TPM_CHIP_FLAG_TPM2 ) ? -tpm2_get_pcr_allocation (chip) : -tpm1_get_pcr_allocation (chip); +if (!(chip->flags &TPM_CHIP_FLAG_TPM2 )) { + chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; + chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; + chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; + chip->nr_allocated_banks =1 ; + }else { + rc =tpm2_get_pcr_allocation (chip); + }if (rc >0 )return -ENODEV ;drivers/char/tpm/tpm.h +0
-1@@ -252,7 +252,6 @@ 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);int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max); -int tpm1_get_pcr_allocation (struct tpm_chip *chip);unsigned long tpm_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm_pm_suspend (struct device *dev);int tpm_pm_resume (struct device *dev);drivers/char/tpm/tpm1-cmd.c +0
-25@@ -786,28 +786,3 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)return rc; } - -/** -* tpm1_get_pcr_allocation() - initialize the allocated bank -* @chip: TPM chip to use. -* -* The function initializes the SHA1 allocated bank to extend PCR -* -* Return: -* * 0 on success, -* * < 0 on error. -*/ -int tpm1_get_pcr_allocation (struct tpm_chip *chip) -{ - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; - chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; - chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; - chip->nr_allocated_banks =1 ; - -return 0 ; -}drivers/char/tpm/tpm2-cmd.c +3
-5@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }include/linux/tpm.h +5
-3@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];Signature · 1 line
2.52.0Hi Jarkko Sakkinen,
Quoted 7 lines
Greetings!
I used Syzkaller and found that there is KASAN: invalid-free in
tpm_dev_releasein linux-tpmdd branch tpmdd-next-6.19-rc1-v3.After bisection and the first bad commit is:
"83f6ace27d21tpm: Cap the number of PCR banksThank you. I updated the patch in my branch:
Quoted 8 lines
Quoted 2 lines
Re-test using the latest branch. Issue cannot be reproduced using the same binary.
Regards,
Yi Lai
Thank you!
BR, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v7 01/11] tpm: Cap the number of PCR banks
In reply to Jarkko SakkinenQuoted 181 lines · Jarkko Sakkinen
Quoted 172 lines · Yi
Quoted 168 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Fixes bcfff8384f6c ("tpm: dynamically allocate the allocated_banks array")Reviewed-by Jonathan McDowell <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- In Ryzen desktop there is total three banks so yep, eight is probably much safer bet than four banks. Fixed the commit message as per remark from Jonathan:https://lore.kernel.org/linux-integrity/[email protected]/#t
And with that added also reviewed-by.
v6
- No changes.
v5:
- No changes.
v4:
- Revert spurious changes frominclude/linux/tpm.h.
- IncreaseTPM2_MAX_BANKSto 8.
- RenameTPM2_MAX_BANKSasTPM2_MAX_PCR_BANKSfor the sake of clarity. v3:
- Wrote a more clear commit message.
- Fixedpr_err()message.
v2:
- A new patch.--- drivers/char/tpm/tpm-chip.c | 13 +++++++++---- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 25 ------------------------- drivers/char/tpm/tpm2-cmd.c | 8 +++----- include/linux/tpm.h | 8 +++++--- 5 files changed, 17 insertions(+), 38 deletions(-)drivers/char/tpm/tpm-chip.c +9
-4@@ -559,14 +559,19 @@ static int tpm_add_hwrng(struct tpm_chip *chip)static int tpm_get_pcr_allocation (struct tpm_chip *chip) { -int rc; +int rc =0 ;if (tpm_is_firmware_upgrade (chip))return 0 ; - rc = (chip->flags &TPM_CHIP_FLAG_TPM2 ) ? -tpm2_get_pcr_allocation (chip) : -tpm1_get_pcr_allocation (chip); +if (!(chip->flags &TPM_CHIP_FLAG_TPM2 )) { + chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; + chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; + chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; + chip->nr_allocated_banks =1 ; + }else { + rc =tpm2_get_pcr_allocation (chip); + }if (rc >0 )return -ENODEV ;drivers/char/tpm/tpm.h +0
-1@@ -252,7 +252,6 @@ 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);int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max); -int tpm1_get_pcr_allocation (struct tpm_chip *chip);unsigned long tpm_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm_pm_suspend (struct device *dev);int tpm_pm_resume (struct device *dev);drivers/char/tpm/tpm1-cmd.c +0
-25@@ -786,28 +786,3 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)return rc; } - -/** -* tpm1_get_pcr_allocation() - initialize the allocated bank -* @chip: TPM chip to use. -* -* The function initializes the SHA1 allocated bank to extend PCR -* -* Return: -* * 0 on success, -* * < 0 on error. -*/ -int tpm1_get_pcr_allocation (struct tpm_chip *chip) -{ - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; - chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; - chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; - chip->nr_allocated_banks =1 ; - -return 0 ; -}drivers/char/tpm/tpm2-cmd.c +3
-5@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }include/linux/tpm.h +5
-3@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];Signature · 1 line
2.52.0Hi Jarkko Sakkinen,
Quoted 7 lines
Greetings!
I used Syzkaller and found that there is KASAN: invalid-free in
tpm_dev_releasein linux-tpmdd branch tpmdd-next-6.19-rc1-v3.After bisection and the first bad commit is:
"83f6ace27d21tpm: Cap the number of PCR banks
Quoted 5 lines
Thank you. I updated the patch in my branch:
Possible to re-test (I can add your tested-by afterwards)?
I mean tested-by AND reported-by.
BR, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v7 01/11] tpm: Cap the number of PCR banks
Quoted 182 lines · Jarkko Sakkinen
Quoted 181 lines · Jarkko Sakkinen
Quoted 179 lines · Yi
Quoted 168 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to eight banks so that out of bounds values coming from external I/O cause on only limited harm.Cc Roberto Sassu <[email protected]>Fixes bcfff8384f6c ("tpm: dynamically allocate the allocated_banks array")Reviewed-by Jonathan McDowell <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- In Ryzen desktop there is total three banks so yep, eight is probably much safer bet than four banks. Fixed the commit message as per remark from Jonathan:https://lore.kernel.org/linux-integrity/[email protected]/#t
And with that added also reviewed-by.
v6
- No changes.
v5:
- No changes.
v4:
- Revert spurious changes frominclude/linux/tpm.h.
- IncreaseTPM2_MAX_BANKSto 8.
- RenameTPM2_MAX_BANKSasTPM2_MAX_PCR_BANKSfor the sake of clarity. v3:
- Wrote a more clear commit message.
- Fixedpr_err()message.
v2:
- A new patch.--- drivers/char/tpm/tpm-chip.c | 13 +++++++++---- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 25 ------------------------- drivers/char/tpm/tpm2-cmd.c | 8 +++----- include/linux/tpm.h | 8 +++++--- 5 files changed, 17 insertions(+), 38 deletions(-)drivers/char/tpm/tpm-chip.c +9
-4@@ -559,14 +559,19 @@ static int tpm_add_hwrng(struct tpm_chip *chip)static int tpm_get_pcr_allocation (struct tpm_chip *chip) { -int rc; +int rc =0 ;if (tpm_is_firmware_upgrade (chip))return 0 ; - rc = (chip->flags &TPM_CHIP_FLAG_TPM2 ) ? -tpm2_get_pcr_allocation (chip) : -tpm1_get_pcr_allocation (chip); +if (!(chip->flags &TPM_CHIP_FLAG_TPM2 )) { + chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; + chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; + chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; + chip->nr_allocated_banks =1 ; + }else { + rc =tpm2_get_pcr_allocation (chip); + }if (rc >0 )return -ENODEV ;drivers/char/tpm/tpm.h +0
-1@@ -252,7 +252,6 @@ 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);int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max); -int tpm1_get_pcr_allocation (struct tpm_chip *chip);unsigned long tpm_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm_pm_suspend (struct device *dev);int tpm_pm_resume (struct device *dev);drivers/char/tpm/tpm1-cmd.c +0
-25@@ -786,28 +786,3 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)return rc; } - -/** -* tpm1_get_pcr_allocation() - initialize the allocated bank -* @chip: TPM chip to use. -* -* The function initializes the SHA1 allocated bank to extend PCR -* -* Return: -* * 0 on success, -* * < 0 on error. -*/ -int tpm1_get_pcr_allocation (struct tpm_chip *chip) -{ - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; - chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; - chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; - chip->nr_allocated_banks =1 ; - -return 0 ; -}drivers/char/tpm/tpm2-cmd.c +3
-5@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }include/linux/tpm.h +5
-3@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];Signature · 1 line
2.52.0Hi Jarkko Sakkinen,
Greetings!
I used Syzkaller and found that there is KASAN: invalid-free in
tpm_dev_releasein linux-tpmdd branch tpmdd-next-6.19-rc1-v3.After bisection and the first bad commit is:
"83f6ace27d21tpm: Cap the number of PCR banks
Quoted 7 lines
Quoted 5 lines
Thank you. I updated the patch in my branch:
Possible to re-test (I can add your tested-by afterwards)?
I mean tested-by AND reported-by.
Duh. Only tested-by since the patch is not yet in mainline (sorry) :-)
BR, Jarkko
Jarkko Sakkinen
maintainerPATCH v7 2/11 tpm: Use -EPERM as fallback error code in tpm_ret_to_err
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Using -EFAULT as the tpm_ret_to_err() fallback error code causes makes it incompatible on how trusted keys transmute TPM return codes.
Change the fallback as -EPERM in order to gain compatibility with trusted keys. In addition, map TPM_RC_HASH to -EINVAL in order to be compatible with tpm2_seal_trusted() return values.
---
v6:
- No changes.
v5:
- No changes.
v4:
- No changes.
v3:
- Removed fixes tag as it hardly categorizes as a bug fix. v2:
- Split trusted_tpm2 change to a separate patch.
--- include/linux/tpm.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/tpm.h +3 -1
@@ -456,8 +456,10 @@ static inline ssize_t tpm_ret_to_err(ssize_t ret)return 0 ;case TPM2_RC_SESSION_MEMORY :return -ENOMEM ; +case TPM2_RC_HASH : +return -EINVAL ;default : -return -EFAULT ; +return -EPERM ; } }
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v7 3/11 KEYS: trusted: remove redundant instance of tpm2_hash_map
In reply to Jarkko SakkinenTrusted keys duplicates tpm2_hash_map from TPM driver internals. Implement and export tpm2_find_hash_alg() in order to address this glitch, and replace redundant code block with a call this new function.
---
v7:
- A new patch.
--- drivers/char/tpm/tpm2-cmd.c | 19 +++++++++++++++-- include/linux/tpm.h | 7 ++----- security/keys/trusted-keys/trusted_tpm2.c | 25 +++++------------------ 3 files changed, 24 insertions(+), 27 deletions(-)
drivers/char/tpm/tpm2-cmd.c +17 -2
@@ -18,7 +18,10 @@ static bool disable_pcr_integrity;module_param (disable_pcr_integrity, bool,0444 );MODULE_PARM_DESC (disable_pcr_integrity,"Disable integrity protection of TPM2_PCR_Extend" ); -static struct tpm2_hash tpm2_hash_map[] = { +static struct { +unsigned int crypto_id; +unsigned int alg_id; +} tpm2_hash_map[] = { {HASH_ALGO_SHA1 ,TPM_ALG_SHA1 }, {HASH_ALGO_SHA256 ,TPM_ALG_SHA256 }, {HASH_ALGO_SHA384 ,TPM_ALG_SHA384 },
@@ -26,6 +29,18 @@ static struct tpm2_hash tpm2_hash_map[] = { {HASH_ALGO_SM3_256, TPM_ALG_SM3_256}, }; +int tpm2_find_hash_alg (unsigned int crypto_id) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) +if (crypto_id == tpm2_hash_map[i].crypto_id) +return tpm2_hash_map[i].alg_id; + +return -EINVAL ; +} +EXPORT_SYMBOL_GPL (tpm2_find_hash_alg); +int tpm2_get_timeouts (struct tpm_chip *chip) { chip->timeout_a =msecs_to_jiffies (TPM2_TIMEOUT_A );
@@ -490,7 +505,7 @@ static int tpm2_init_bank_info(struct tpm_chip *chip, u32 bank_index)for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) {enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id; -if (bank->alg_id != tpm2_hash_map[i].tpm_id) +if (bank->alg_id != tpm2_hash_map[i].alg_id)continue ; bank->digest_size = hash_digest_size[crypto_algo];
include/linux/tpm.h +2 -5
@@ -410,11 +410,6 @@ enum tpm2_session_attributes { TPM2_SA_AUDIT = BIT(7), }; -struct tpm2_hash { -unsigned int crypto_id; -unsigned int tpm_id; -}; -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal);void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal);int tpm_buf_init_sized (struct tpm_buf *buf);
@@ -465,6 +460,7 @@ static inline ssize_t tpm_ret_to_err(ssize_t ret)#if defined(CONFIG_TCG_TPM ) || defined(CONFIG_TCG_TPM_MODULE ) +unsigned int tpm2_alg_to_crypto_id (unsigned int alg_id);extern int tpm_is_tpm2 (struct tpm_chip *chip);extern __must_check int tpm_try_get_ops (struct tpm_chip *chip);extern void tpm_put_ops (struct tpm_chip *chip);
@@ -477,6 +473,7 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,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); +int tpm2_find_hash_alg (unsigned int crypto_id);static inline void tpm_buf_append_empty_auth (struct tpm_buf *buf,u32 handle) {
security/keys/trusted-keys/trusted_tpm2.c +5 -20
@@ -18,14 +18,6 @@#include "tpm2key.asn1.h" -static struct tpm2_hash tpm2_hash_map[] = { - {HASH_ALGO_SHA1 ,TPM_ALG_SHA1 }, - {HASH_ALGO_SHA256 ,TPM_ALG_SHA256 }, - {HASH_ALGO_SHA384 ,TPM_ALG_SHA384 }, - {HASH_ALGO_SHA512 ,TPM_ALG_SHA512 }, - {HASH_ALGO_SM3_256 ,TPM_ALG_SM3_256 }, -}; -static u32 tpm2key_oid[] = {2 ,23 ,133 ,10 ,1 ,5 }; static int tpm2_key_encode(struct trusted_key_payload *payload,
@@ -244,24 +236,17 @@ int tpm2_seal_trusted(struct tpm_chip *chip,off_t offset =TPM_HEADER_SIZE ;struct tpm_buf buf, sized;int blob_len =0 ; -u32 hash; +int hash;u32 flags; -int i;int rc; -for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) { -if (options->hash == tpm2_hash_map[i].crypto_id) { - hash = tpm2_hash_map[i].tpm_id; -break ; - } - } - -if (i ==ARRAY_SIZE (tpm2_hash_map)) -return -EINVAL ; -if (!options->keyhandle)return -EINVAL ; + hash =tpm2_find_hash_alg (options->hash); +if (hash) +return hash; + rc =tpm_try_get_ops (chip);if (rc)return rc;
Signature · 1 line
2.52.0
Re: [PATCH v7 03/11] KEYS: trusted: remove redundant instance of tpm2_hash_map
Quoted 75 lines · Jarkko Sakkinen
Trusted keys duplicates
tpm2_hash_mapfrom TPM driver internals. Implement and exporttpm2_find_hash_alg()in order to address this glitch, and replace redundant code block with a call this new function.Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- A new patch.
---drivers/char/tpm/tpm2-cmd.c | 19 +++++++++++++++-- include/linux/tpm.h | 7 ++----- security/keys/trusted-keys/trusted_tpm2.c | 25 +++++------------------ 3 files changed, 24 insertions(+), 27 deletions(-)drivers/char/tpm/tpm2-cmd.c +17
-2@@ -18,7 +18,10 @@ static bool disable_pcr_integrity;module_param (disable_pcr_integrity, bool,0444 );MODULE_PARM_DESC (disable_pcr_integrity,"Disable integrity protection of TPM2_PCR_Extend" ); -static struct tpm2_hash tpm2_hash_map[] = { +static struct { +unsigned int crypto_id; +unsigned int alg_id; +} tpm2_hash_map[] = { {HASH_ALGO_SHA1 ,TPM_ALG_SHA1 }, {HASH_ALGO_SHA256 ,TPM_ALG_SHA256 }, {HASH_ALGO_SHA384 ,TPM_ALG_SHA384 },@@ -26,6 +29,18 @@ static struct tpm2_hash tpm2_hash_map[] = { {HASH_ALGO_SM3_256, TPM_ALG_SM3_256}, }; +int tpm2_find_hash_alg (unsigned int crypto_id) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) +if (crypto_id == tpm2_hash_map[i].crypto_id) +return tpm2_hash_map[i].alg_id; + +return -EINVAL ; +} +EXPORT_SYMBOL_GPL (tpm2_find_hash_alg); +int tpm2_get_timeouts (struct tpm_chip *chip) { chip->timeout_a =msecs_to_jiffies (TPM2_TIMEOUT_A );@@ -490,7 +505,7 @@ static int tpm2_init_bank_info(struct tpm_chip *chip, u32 bank_index)for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) {enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id; -if (bank->alg_id != tpm2_hash_map[i].tpm_id) +if (bank->alg_id != tpm2_hash_map[i].alg_id)continue ; bank->digest_size = hash_digest_size[crypto_algo];include/linux/tpm.h +0
-5@@ -410,11 +410,6 @@ enum tpm2_session_attributes { TPM2_SA_AUDIT = BIT(7), }; -struct tpm2_hash { -unsigned int crypto_id; -unsigned int tpm_id; -}; -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal);void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal);int tpm_buf_init_sized (struct tpm_buf *buf);
include/linux/tpm.h static inline ssize_t tpm_ret_to_err(ssize_t ret) · line 466
Quoted 7 lines
include/linux/tpm.h +1
-0@@ -465,6 +460,7 @@ static inline ssize_t tpm_ret_to_err(ssize_t ret)#if defined(CONFIG_TCG_TPM ) || defined(CONFIG_TCG_TPM_MODULE ) +unsigned int tpm2_alg_to_crypto_id (unsigned int alg_id);extern int tpm_is_tpm2 (struct tpm_chip *chip);extern __must_check int tpm_try_get_ops (struct tpm_chip *chip);extern void tpm_put_ops (struct tpm_chip *chip);
This looks like an errant chunk? I can't see tpm2_alg_to_crypto_id defined or used?
Quoted 59 lines
include/linux/tpm.h +1
-0@@ -477,6 +473,7 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,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); +int tpm2_find_hash_alg (unsigned int crypto_id);static inline void tpm_buf_append_empty_auth (struct tpm_buf *buf,u32 handle) {security/keys/trusted-keys/trusted_tpm2.c +5
-20@@ -18,14 +18,6 @@#include "tpm2key.asn1.h" -static struct tpm2_hash tpm2_hash_map[] = { - {HASH_ALGO_SHA1 ,TPM_ALG_SHA1 }, - {HASH_ALGO_SHA256 ,TPM_ALG_SHA256 }, - {HASH_ALGO_SHA384 ,TPM_ALG_SHA384 }, - {HASH_ALGO_SHA512 ,TPM_ALG_SHA512 }, - {HASH_ALGO_SM3_256 ,TPM_ALG_SM3_256 }, -}; -static u32 tpm2key_oid[] = {2 ,23 ,133 ,10 ,1 ,5 }; static int tpm2_key_encode(struct trusted_key_payload *payload,@@ -244,24 +236,17 @@ int tpm2_seal_trusted(struct tpm_chip *chip,off_t offset =TPM_HEADER_SIZE ;struct tpm_buf buf, sized;int blob_len =0 ; -u32 hash; +int hash;u32 flags; -int i;int rc; -for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) { -if (options->hash == tpm2_hash_map[i].crypto_id) { - hash = tpm2_hash_map[i].tpm_id; -break ; - } - } - -if (i ==ARRAY_SIZE (tpm2_hash_map)) -return -EINVAL ; -if (!options->keyhandle)return -EINVAL ; + hash =tpm2_find_hash_alg (options->hash); +if (hash) +return hash; + rc =tpm_try_get_ops (chip);if (rc)return rc;Signature · 1 line
2.52.0
J.
Signature · 1 line
Design a system any fool can use, and only a fool will want to use it.
Jarkko Sakkinen
maintainerRe: [PATCH v7 03/11] KEYS: trusted: remove redundant instance of tpm2_hash_map
Quoted 79 lines · Jonathan McDowell
Quoted 78 lines · Jarkko Sakkinen
Trusted keys duplicates
tpm2_hash_mapfrom TPM driver internals. Implement and exporttpm2_find_hash_alg()in order to address this glitch, and replace redundant code block with a call this new function.Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- A new patch.
---drivers/char/tpm/tpm2-cmd.c | 19 +++++++++++++++-- include/linux/tpm.h | 7 ++----- security/keys/trusted-keys/trusted_tpm2.c | 25 +++++------------------ 3 files changed, 24 insertions(+), 27 deletions(-)drivers/char/tpm/tpm2-cmd.c +17
-2@@ -18,7 +18,10 @@ static bool disable_pcr_integrity;module_param (disable_pcr_integrity, bool,0444 );MODULE_PARM_DESC (disable_pcr_integrity,"Disable integrity protection of TPM2_PCR_Extend" ); -static struct tpm2_hash tpm2_hash_map[] = { +static struct { +unsigned int crypto_id; +unsigned int alg_id; +} tpm2_hash_map[] = { {HASH_ALGO_SHA1 ,TPM_ALG_SHA1 }, {HASH_ALGO_SHA256 ,TPM_ALG_SHA256 }, {HASH_ALGO_SHA384 ,TPM_ALG_SHA384 },@@ -26,6 +29,18 @@ static struct tpm2_hash tpm2_hash_map[] = { {HASH_ALGO_SM3_256, TPM_ALG_SM3_256}, }; +int tpm2_find_hash_alg (unsigned int crypto_id) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) +if (crypto_id == tpm2_hash_map[i].crypto_id) +return tpm2_hash_map[i].alg_id; + +return -EINVAL ; +} +EXPORT_SYMBOL_GPL (tpm2_find_hash_alg); +int tpm2_get_timeouts (struct tpm_chip *chip) { chip->timeout_a =msecs_to_jiffies (TPM2_TIMEOUT_A );@@ -490,7 +505,7 @@ static int tpm2_init_bank_info(struct tpm_chip *chip, u32 bank_index)for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) {enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id; -if (bank->alg_id != tpm2_hash_map[i].tpm_id) +if (bank->alg_id != tpm2_hash_map[i].alg_id)continue ; bank->digest_size = hash_digest_size[crypto_algo];include/linux/tpm.h +0
-5@@ -410,11 +410,6 @@ enum tpm2_session_attributes { TPM2_SA_AUDIT = BIT(7), }; -struct tpm2_hash { -unsigned int crypto_id; -unsigned int tpm_id; -}; -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal);void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal);int tpm_buf_init_sized (struct tpm_buf *buf);@@ -465,6 +460,7 @@ static inline ssize_t tpm_ret_to_err(ssize_t ret)#if defined(CONFIG_TCG_TPM ) || defined(CONFIG_TCG_TPM_MODULE )
Quoted 7 lines
include/linux/tpm.h static inline ssize_t tpm_ret_to_err(ssize_t ret) · line 466
Quoted 4 lines
include/linux/tpm.h +1
-0+unsigned int tpm2_alg_to_crypto_id (unsigned int alg_id);extern int tpm_is_tpm2 (struct tpm_chip *chip);extern __must_check int tpm_try_get_ops (struct tpm_chip *chip);extern void tpm_put_ops (struct tpm_chip *chip);This looks like an errant chunk? I can't see
tpm2_alg_to_crypto_iddefined or used?
It is! Thanks, will remove.
Quoted 51 lines
Quoted 51 lines
include/linux/tpm.h +1
-0@@ -477,6 +473,7 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,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); +int tpm2_find_hash_alg (unsigned int crypto_id);static inline void tpm_buf_append_empty_auth (struct tpm_buf *buf,u32 handle) {security/keys/trusted-keys/trusted_tpm2.c +1
-20@@ -18,14 +18,6 @@#include "tpm2key.asn1.h" -static struct tpm2_hash tpm2_hash_map[] = { - {HASH_ALGO_SHA1 ,TPM_ALG_SHA1 }, - {HASH_ALGO_SHA256 ,TPM_ALG_SHA256 }, - {HASH_ALGO_SHA384 ,TPM_ALG_SHA384 }, - {HASH_ALGO_SHA512 ,TPM_ALG_SHA512 }, - {HASH_ALGO_SM3_256 ,TPM_ALG_SM3_256 }, -}; -static u32 tpm2key_oid[] = {2 ,23 ,133 ,10 ,1 ,5 }; static int tpm2_key_encode(struct trusted_key_payload *payload,@@ -244,24 +236,17 @@ int tpm2_seal_trusted(struct tpm_chip *chip,off_t offset =TPM_HEADER_SIZE ;struct tpm_buf buf, sized;int blob_len =0 ; -u32 hash; +int hash;u32 flags; -int i;int rc; -for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) { -if (options->hash == tpm2_hash_map[i].crypto_id) { - hash = tpm2_hash_map[i].tpm_id; -break ; - } - } - -if (i ==ARRAY_SIZE (tpm2_hash_map)) -return -EINVAL ; -if (!options->keyhandle)return -EINVAL ;
Quoted 14 lines
Quoted 9 lines
security/keys/trusted-keys/trusted_tpm2.c +4
-0+ hash =tpm2_find_hash_alg (options->hash); +if (hash) +return hash; + rc =tpm_try_get_ops (chip);if (rc)return rc;Signature · 1 line
2.52.0J.
Signature · 1 line
Design a system any fool can use, and only a fool will want to use it.
BR, Jarkko
Jarkko Sakkinen
maintainerPATCH v7 4/11 KEYS: trusted: Fix memory leak in tpm2_load()
In reply to Jarkko Sakkinentpm2_load() allocates a blob indirectly via tpm2_key_decode() but it is not freed in all failure paths. Address this with a scope-based cleanup helper __free(). For legacy blobs, the implicit de-allocation is gets disable by no_free_ptr().
---
v7:
- Fix compiler warning.
v6:
- A new patch in this version.
--- security/keys/trusted-keys/trusted_tpm2.c | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +12 -12
@@ -98,9 +98,8 @@ struct tpm2_key_context {u32 priv_len; }; -static int tpm2_key_decode (struct trusted_key_payload *payload, -struct trusted_key_options *options, -u8 **buf) +static void *tpm2_key_decode (struct trusted_key_payload *payload, +struct trusted_key_options *options) {int ret;struct tpm2_key_context ctx;
@@ -111,16 +110,15 @@ static int tpm2_key_decode(struct trusted_key_payload *payload, ret =asn1_ber_decoder (&tpm2key_decoder, &ctx, payload->blob, payload->blob_len);if (ret <0 ) -return ret; +return ERR_PTR (ret);if (ctx.priv_len + ctx.pub_len >MAX_BLOB_SIZE ) -return -EINVAL ; +return ERR_PTR (-EINVAL ); blob =kmalloc (ctx.priv_len + ctx.pub_len +4 ,GFP_KERNEL );if (!blob) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); - *buf = blob; options->keyhandle = ctx.parent;memcpy (blob, ctx.priv, ctx.priv_len);
@@ -128,7 +126,7 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,memcpy (blob, ctx.pub, ctx.pub_len); -return 0 ; +return blob; } int tpm2_key_parent(void *context, size_t hdrlen,
@@ -372,6 +370,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,struct trusted_key_options *options, u32 *blob_handle) { + u8 *blob_ref __free(kfree) = NULL;struct tpm_buf buf;unsigned int private_len;unsigned int public_len;
@@ -380,11 +379,14 @@ static int tpm2_load_cmd(struct tpm_chip *chip,int rc;u32 attrs; - rc =tpm2_key_decode (payload, options, &blob); -if (rc) { + blob =tpm2_key_decode (payload, options); +if (IS_ERR (blob)) {/* old form */ blob = payload->blob; payload->old_format =1 ; + }else { +/* Bind to cleanup: */ + blob_ref = blob; }/* new format carries keyhandle but old format doesn't */
@@ -449,8 +451,6 @@ static int tpm2_load_cmd(struct tpm_chip *chip, (__be32 *) &buf.data[TPM_HEADER_SIZE]); out: -if (blob != payload->blob) -kfree (blob);tpm_buf_destroy (&buf);if (rc >0 )
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v7 5/11 KEYS: trusted: Use tpm_ret_to_err() in trusted_tpm2
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Use tpm_ret_to_err() to transmute TPM return codes in trusted_tpm2.
---
v6:
- No changes.
v5:
- No changes.
v4:
- No changes.
v3:
- No changes.
v2:
- New patch split out from the fix.
--- security/keys/trusted-keys/trusted_tpm2.c | 26 ++++++----------------- 1 file changed, 7 insertions(+), 19 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +7 -19
@@ -331,25 +331,19 @@ int tpm2_seal_trusted(struct tpm_chip *chip, } blob_len =tpm2_key_encode (payload, options, &buf.data[offset], blob_len); +if (blob_len <0 ) + rc = blob_len; out:tpm_buf_destroy (&sized);tpm_buf_destroy (&buf); -if (rc >0 ) { -if (tpm2_rc_value (rc) ==TPM2_RC_HASH ) - rc = -EINVAL ; -else - rc = -EPERM ; - } -if (blob_len <0 ) - rc = blob_len; -else +if (!rc) payload->blob_len = blob_len; out_put:tpm_put_ops (chip); -return rc; +return tpm_ret_to_err (rc); }/**
@@ -453,10 +447,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip, out:tpm_buf_destroy (&buf); -if (rc >0 ) - rc = -EPERM ; - -return rc; +return tpm_ret_to_err (rc); }/**
@@ -519,8 +510,6 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,tpm_buf_fill_hmac_session (chip, &buf); rc =tpm_transmit_cmd (chip, &buf,6 ,"unsealing" ); rc =tpm_buf_check_hmac_response (chip, &buf, rc); -if (rc >0 ) - rc = -EPERM ;if (!rc) { data_len = be16_to_cpup(
@@ -553,7 +542,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, out:tpm_buf_destroy (&buf); -return rc; +return tpm_ret_to_err (rc); }/**
@@ -585,6 +574,5 @@ int tpm2_unseal_trusted(struct tpm_chip *chip, out:tpm_put_ops (chip); - -return rc; +return tpm_ret_to_err (rc); }
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v7 6/11 tpm2-sessions: Remove 'attributes' from tpm_buf_append_auth
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
In a previous bug fix, 'attributes' was added by mistake to tpm_buf_append_auth(). Remove the parameter.
---
v6:
- No changes.
v5:
- No changes.
v4:
- No changes.
v3:
- No changes
v2:
- Uncorrupt the patch.
--- drivers/char/tpm/tpm2-cmd.c | 2 +- drivers/char/tpm/tpm2-sessions.c | 5 ++--- include/linux/tpm.h | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-)
drivers/char/tpm/tpm2-cmd.c +1 -1
@@ -206,7 +206,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,tpm_buf_append_hmac_session (chip, &buf,0 ,NULL ,0 ); }else {tpm_buf_append_handle (chip, &buf, pcr_idx); -tpm_buf_append_auth (chip, &buf,0 ,NULL ,0 ); +tpm_buf_append_auth (chip, &buf,NULL ,0 ); }tpm_buf_append_u32 (&buf, chip->nr_allocated_banks);
drivers/char/tpm/tpm2-sessions.c +2 -3
@@ -266,7 +266,7 @@ void tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,EXPORT_SYMBOL_GPL (tpm_buf_append_name);void tpm_buf_append_auth (struct tpm_chip *chip,struct tpm_buf *buf, -u8 attributes,u8 *passphrase,int passphrase_len) +u8 *passphrase,int passphrase_len) {/* offset tells us where the sessions area begins */ int offset = buf->handles *4 +TPM_HEADER_SIZE ;
@@ -327,8 +327,7 @@ void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf,#endif if (!tpm2_chip_auth (chip)) { -tpm_buf_append_auth (chip, buf, attributes, passphrase, - passphrase_len); +tpm_buf_append_auth (chip, buf, passphrase, passphrase_len);return ; }
include/linux/tpm.h +1 -1
@@ -531,7 +531,7 @@ void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf, u8 attributes, u8 *passphrase, int passphraselen);void tpm_buf_append_auth (struct tpm_chip *chip,struct tpm_buf *buf, -u8 attributes,u8 *passphrase,int passphraselen); +u8 *passphrase,int passphraselen); static inline void tpm_buf_append_hmac_session_opt(struct tpm_chip *chip, struct tpm_buf *buf, u8 attributes,
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v7 7/11 tpm2-sessions: Unmask tpm_buf_append_hmac_session()
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Open code tpm_buf_append_hmac_session_opt() in order to unmask the code paths in the call sites of tpm_buf_append_hmac_session().
---
v6:
- No changes.
v5:
- No changes.
v4:
- Fixed typo in short summary.
v3:
- No changes.
v2:
- Uncorrupt the patch.
--- drivers/char/tpm/tpm2-cmd.c | 14 +++++++++++--- include/linux/tpm.h | 23 ----------------------- security/keys/trusted-keys/trusted_tpm2.c | 12 ++++++++++-- 3 files changed, 21 insertions(+), 28 deletions(-)
drivers/char/tpm/tpm2-cmd.c +11 -3
@@ -272,9 +272,17 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) do { tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_GET_RANDOM); - tpm_buf_append_hmac_session_opt(chip, &buf, TPM2_SA_ENCRYPT - | TPM2_SA_CONTINUE_SESSION, - NULL, 0); + if (tpm2_chip_auth(chip)) { + tpm_buf_append_hmac_session(chip, &buf, + TPM2_SA_ENCRYPT | + TPM2_SA_CONTINUE_SESSION, + NULL, 0); + } else { + offset = buf.handles * 4 + TPM_HEADER_SIZE; + head = (struct tpm_header *)buf.data; + if (tpm_buf_length(&buf) == offset) + head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS); + } tpm_buf_append_u16(&buf, num_bytes); tpm_buf_fill_hmac_session(chip, &buf); err = tpm_transmit_cmd(chip, &buf,
include/linux/tpm.h +0 -23
@@ -532,29 +532,6 @@ void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf, int passphraselen);void tpm_buf_append_auth (struct tpm_chip *chip,struct tpm_buf *buf,u8 *passphrase,int passphraselen); -static inline void tpm_buf_append_hmac_session_opt (struct tpm_chip *chip, -struct tpm_buf *buf, -u8 attributes, -u8 *passphrase, -int passphraselen) -{ -struct tpm_header *head; -int offset; - -if (tpm2_chip_auth (chip)) { -tpm_buf_append_hmac_session (chip, buf, attributes, passphrase, passphraselen); - }else { - offset = buf->handles *4 +TPM_HEADER_SIZE ; - head = (struct tpm_header *)buf->data; - -/* -* If the only sessions are optional, the command tag must change to -* TPM2_ST_NO_SESSIONS. -*/ -if (tpm_buf_length (buf) == offset) - head->tag =cpu_to_be16 (TPM2_ST_NO_SESSIONS ); - } -}#ifdef CONFIG_TCG_TPM2_HMAC
security/keys/trusted-keys/trusted_tpm2.c +10 -2
@@ -467,8 +467,10 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, struct trusted_key_options *options, u32 blob_handle) { +struct tpm_header *head;struct tpm_buf buf;u16 data_len; +int offset;u8 *data;int rc;
@@ -503,8 +505,14 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,tpm2_buf_append_auth (&buf, options->policyhandle,NULL /* nonce */ ,0 ,0 , options->blobauth, options->blobauth_len); -tpm_buf_append_hmac_session_opt (chip, &buf,TPM2_SA_ENCRYPT , -NULL ,0 ); +if (tpm2_chip_auth (chip)) { +tpm_buf_append_hmac_session (chip, &buf,TPM2_SA_ENCRYPT ,NULL ,0 ); + }else { + offset = buf.handles *4 +TPM_HEADER_SIZE ; + head = (struct tpm_header *)buf.data; +if (tpm_buf_length (&buf) == offset) + head->tag =cpu_to_be16 (TPM2_ST_NO_SESSIONS ); + } }tpm_buf_fill_hmac_session (chip, &buf);
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v7 8/11 KEYS: trusted: Open code tpm2_buf_append()
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
tpm2_buf_append_auth() has only single call site and most of its parameters are redundant. Open code it to the call site. Remove illegit FIXME comment as there is no categorized bug and replace it with more sane comment about implementation (i.e. "non-opionated inline comment").
---
v6:
- No changes.
v5:
- No changes.
v4:
- No changes.
v3:
- No changes.
v2:
- No changes.
--- security/keys/trusted-keys/trusted_tpm2.c | 51 ++++------------------- 1 file changed, 9 insertions(+), 42 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +9 -42
@@ -188,36 +188,6 @@ int tpm2_key_priv(void *context, size_t hdrlen,return 0 ; } -/** -* tpm2_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer. -* -* @buf: an allocated tpm_buf instance -* @session_handle: session handle -* @nonce: the session nonce, may be NULL if not used -* @nonce_len: the session nonce length, may be 0 if not used -* @attributes: the session attributes -* @hmac: the session HMAC or password, may be NULL if not used -* @hmac_len: the session HMAC or password length, maybe 0 if not used -*/ -static void tpm2_buf_append_auth (struct tpm_buf *buf,u32 session_handle, -const u8 *nonce,u16 nonce_len, -u8 attributes, -const u8 *hmac,u16 hmac_len) -{ -tpm_buf_append_u32 (buf,9 + nonce_len + hmac_len); -tpm_buf_append_u32 (buf, session_handle); -tpm_buf_append_u16 (buf, nonce_len); - -if (nonce && nonce_len) -tpm_buf_append (buf, nonce, nonce_len); - -tpm_buf_append_u8 (buf, attributes); -tpm_buf_append_u16 (buf, hmac_len); - -if (hmac && hmac_len) -tpm_buf_append (buf, hmac, hmac_len); -} -/** * tpm2_seal_trusted() - seal the payload of a trusted key *
@@ -492,19 +462,16 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, options->blobauth_len); }else {/* -* FIXME: The policy session was generated outside the -* kernel so we don't known the nonce and thus can't -* calculate a HMAC on it. Therefore, the user can -* only really use TPM2_PolicyPassword and we must -* send down the plain text password, which could be -* intercepted. We can still encrypt the returned -* key, but that's small comfort since the interposer -* could repeat our actions with the exfiltrated -* password. +* The policy session is generated outside the kernel, and thus +* the password will end up being unencrypted on the bus, as +* HMAC nonce cannot be calculated for it. */ -tpm2_buf_append_auth (&buf, options->policyhandle, -NULL /* nonce */ ,0 ,0 , - options->blobauth, options->blobauth_len); +tpm_buf_append_u32 (&buf,9 + options->blobauth_len); +tpm_buf_append_u32 (&buf, options->policyhandle); +tpm_buf_append_u16 (&buf,0 ); +tpm_buf_append_u8 (&buf,0 ); +tpm_buf_append_u16 (&buf, options->blobauth_len); +tpm_buf_append (&buf, options->blobauth, options->blobauth_len);if (tpm2_chip_auth (chip)) {tpm_buf_append_hmac_session (chip, &buf,TPM2_SA_ENCRYPT ,NULL ,0 ); }else {
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v7 9/11 tpm-buf: unify TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Unify TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW into TPM_BUF_INVALID, given that they are identical. The only difference are the log messages.
In addition, add a missing TPM_BUF_INVALID check to tpm_buf_append_handle() following the pattern from other functions in tpm-buf.c.
---
v6:
- Change short summary to something more descriptive. v5:
- No changes.
v4:
- No changes.
v3:
- No changes.
v2:
- A new patch.
--- drivers/char/tpm/tpm-buf.c | 14 ++++++++------ include/linux/tpm.h | 8 +++----- security/keys/trusted-keys/trusted_tpm2.c | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-)
drivers/char/tpm/tpm-buf.c +8 -6
@@ -104,13 +104,12 @@ EXPORT_SYMBOL_GPL(tpm_buf_length);*/ void tpm_buf_append (struct tpm_buf *buf,const u8 *new_data,u16 new_length) { -/* Return silently if overflow has already happened. */ -if (buf->flags &TPM_BUF_OVERFLOW ) +if (buf->flags &TPM_BUF_INVALID )return ;if ((buf->length + new_length) >PAGE_SIZE ) {WARN (1 ,"tpm_buf: write overflow\n" ); - buf->flags |=TPM_BUF_OVERFLOW ; + buf->flags |=TPM_BUF_INVALID ;return ; }
@@ -157,8 +156,12 @@ EXPORT_SYMBOL_GPL(tpm_buf_append_u32);*/ void tpm_buf_append_handle (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle) { +if (buf->flags &TPM_BUF_INVALID ) +return ; +if (buf->flags &TPM_BUF_TPM2B ) {dev_err (&chip->dev,"Invalid buffer type (TPM2B)\n" ); + buf->flags |=TPM_BUF_INVALID ;return ; }
@@ -177,14 +180,13 @@ static void tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void {off_t next_offset; -/* Return silently if overflow has already happened. */ -if (buf->flags &TPM_BUF_BOUNDARY_ERROR ) +if (buf->flags &TPM_BUF_INVALID )return ; next_offset = *offset + count;if (next_offset > buf->length) {WARN (1 ,"tpm_buf: read out of boundary\n" ); - buf->flags |=TPM_BUF_BOUNDARY_ERROR ; + buf->flags |=TPM_BUF_INVALID ;return ; }
include/linux/tpm.h +3 -5
@@ -369,12 +369,10 @@ struct tpm_header { } __packed;enum tpm_buf_flags { -/* the capacity exceeded: */ -TPM_BUF_OVERFLOW =BIT (0 ),/* TPM2B format: */ -TPM_BUF_TPM2B =BIT (1 ), -/* read out of boundary: */ -TPM_BUF_BOUNDARY_ERROR =BIT (2 ), +TPM_BUF_TPM2B =BIT (0 ), +/* The buffer is in invalid and unusable state: */ +TPM_BUF_INVALID =BIT (1 ), };/*
security/keys/trusted-keys/trusted_tpm2.c +3 -3
@@ -278,7 +278,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,/* creation PCR */ tpm_buf_append_u32 (&buf,0 ); -if (buf.flags &TPM_BUF_OVERFLOW ) { +if (buf.flags &TPM_BUF_INVALID ) { rc = -E2BIG ;tpm2_end_auth_session (chip); goto out;
@@ -291,7 +291,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip, goto out; blob_len =tpm_buf_read_u32 (&buf, &offset); -if (blob_len >MAX_BLOB_SIZE || buf.flags &TPM_BUF_BOUNDARY_ERROR ) { +if (blob_len >MAX_BLOB_SIZE || buf.flags &TPM_BUF_INVALID ) { rc = -E2BIG ; goto out; }
@@ -401,7 +401,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,tpm_buf_append (&buf, blob, blob_len); -if (buf.flags &TPM_BUF_OVERFLOW ) { +if (buf.flags &TPM_BUF_INVALID ) { rc = -E2BIG ;tpm2_end_auth_session (chip); goto out;
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v7 10/11 tpm-buf: Remove chip parameter from tpm_buf_append_handle
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Remove @chip from tpm_buf_append_handle() in order to decouple it from driver. This allows tpm-buf.c to be compiled as part of early boot code, thus providing a mechanism to build and parse TPM data.
E.g., Trenchboot can use this to manage TPM2_PcrExtend protocol data as part of its early boot blob simply by compiling and linking the object file.
---
v6:
- Rewrote the commit message.
v5:
- No changes.
v4:
- No changes.
v3:
- No changes.
v2:
- No changes.
--- drivers/char/tpm/tpm-buf.c | 5 ++--- drivers/char/tpm/tpm2-cmd.c | 2 +- drivers/char/tpm/tpm2-sessions.c | 2 +- include/linux/tpm.h | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-)
drivers/char/tpm/tpm-buf.c +2 -3
@@ -147,20 +147,19 @@ EXPORT_SYMBOL_GPL(tpm_buf_append_u32);/** * tpm_buf_append_handle() - Add a handle -* @chip: &tpm_chip instance * @buf: &tpm_buf instance * @handle: a TPM object handle * * Add a handle to the buffer, and increase the count tracking the number of * handles in the command buffer. Works only for command buffers. */ -void tpm_buf_append_handle (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle) +void tpm_buf_append_handle (struct tpm_buf *buf,u32 handle) {if (buf->flags &TPM_BUF_INVALID )return ;if (buf->flags &TPM_BUF_TPM2B ) { -dev_err (&chip->dev,"Invalid buffer type (TPM2B)\n" ); +WARN (1 ,"tpm-buf: invalid type: TPM2B\n" ); buf->flags |=TPM_BUF_INVALID ;return ; }
drivers/char/tpm/tpm2-cmd.c +1 -1
@@ -205,7 +205,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,tpm_buf_append_name (chip, &buf, pcr_idx,NULL );tpm_buf_append_hmac_session (chip, &buf,0 ,NULL ,0 ); }else { -tpm_buf_append_handle (chip, &buf, pcr_idx); +tpm_buf_append_handle (&buf, pcr_idx);tpm_buf_append_auth (chip, &buf,NULL ,0 ); }
drivers/char/tpm/tpm2-sessions.c +1 -1
@@ -232,7 +232,7 @@ void tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,#endif if (!tpm2_chip_auth (chip)) { -tpm_buf_append_handle (chip, buf, handle); +tpm_buf_append_handle (buf, handle);return ; }
include/linux/tpm.h +1 -1
@@ -421,7 +421,7 @@ void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value);u8 tpm_buf_read_u8 (struct tpm_buf *buf,off_t *offset);u16 tpm_buf_read_u16 (struct tpm_buf *buf,off_t *offset);u32 tpm_buf_read_u32 (struct tpm_buf *buf,off_t *offset); -void tpm_buf_append_handle (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle); +void tpm_buf_append_handle (struct tpm_buf *buf,u32 handle);/* * Check if TPM device is in the firmware upgrade mode.
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v7 11/11 tpm-buf: Enable managed and stack allocations.
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Decouple kzalloc from buffer creation, so that a managed allocation can be done trivially:
struct tpm_buf *buf __free(kfree) buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL); if (!buf) return -ENOMEM; tpm_buf_init(buf, TPM_BUFSIZE);
Alternatively, stack allocations are also possible:
u8 buf_data[512]; struct tpm_buf *buf = (struct tpm_buf *)buf_data; tpm_buf_init(buf, sizeof(buf_data));
Given that every single tpm_transmit_cmd() call site needs to be changed, place command names from TCG 1.2 and 2.0 specifications to the @dest parameter, which will e.g., help tracing bugs.
---
v7:
- Addressed issues in:
https://lore.kernel.org/linux-integrity/yynqxoqux5whcbsnticikhwmupqh57xfbll5egzkn42kj7gkaf@s4kwxfmto5ia/
v6
- Update commit message.
v5:
- There was a spurious change in tpm2_seal_trusted() error code handling introduce by this patch.
v4:
- Since every single tpm_transmit_cmd() call site needs to be changed anyhow, use 'dest' parameter more structured and actually useful way, and pick the string TCG 1.2 and 2.0 specifications.
- tpm1-cmd: Remove useless rc declarations and repliace them with trivial "return tpm_transmit_cmd" statement.
- Reverted spurious changes in include/linux/tpm.h.
- Use concisely TPM_BUFSIZE instead of PAGE_SIZE.
v3:
- A new patch from the earlier series with more scoped changes and less abstract commit message.
--- drivers/char/tpm/tpm-buf.c | 124 +++++---- drivers/char/tpm/tpm-sysfs.c | 21 +- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 162 +++++------- drivers/char/tpm/tpm2-cmd.c | 299 ++++++++++------------ drivers/char/tpm/tpm2-sessions.c | 122 +++++---- drivers/char/tpm/tpm2-space.c | 44 ++-- drivers/char/tpm/tpm_vtpm_proxy.c | 30 +-- include/linux/tpm.h | 20 +- security/keys/trusted-keys/trusted_tpm1.c | 36 +-- security/keys/trusted-keys/trusted_tpm2.c | 173 ++++++------- 11 files changed, 488 insertions(+), 544 deletions(-)
drivers/char/tpm/tpm-buf.c +78 -46
@@ -7,82 +7,109 @@#include <linux/module.h> #include <linux/tpm.h> -/** -* tpm_buf_init() - Allocate and initialize a TPM command -* @buf: A &tpm_buf -* @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS -* @ordinal: A command ordinal -* -* Return: 0 or -ENOMEM -*/ -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) +static void __tpm_buf_size_invariant (struct tpm_buf *buf,u16 buf_size) { - buf->data = (u8 *)__get_free_page (GFP_KERNEL ); -if (!buf->data) -return -ENOMEM ; - -tpm_buf_reset (buf, tag, ordinal); -return 0 ; +u32 buf_size_2 = (u32 )buf->capacity + (u32 )sizeof (*buf); + +if (!buf->capacity) { +if (buf_size >TPM_BUFSIZE ) { +WARN (1 ,"%s: size overflow: %u\n" , __func__, buf_size); + buf->flags |=TPM_BUF_INVALID ; + } + }else { +if (buf_size != buf_size_2) { +WARN (1 ,"%s: size mismatch: %u != %u\n" , __func__, buf_size, + buf_size_2); + buf->flags |=TPM_BUF_INVALID ; + } + } } -EXPORT_SYMBOL_GPL (tpm_buf_init); -/** -* tpm_buf_reset() - Initialize a TPM command -* @buf: A &tpm_buf -* @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS -* @ordinal: A command ordinal -*/ -void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal) +static void __tpm_buf_reset (struct tpm_buf *buf,u16 buf_size,u16 tag,u32 ordinal) {struct tpm_header *head = (struct tpm_header *)buf->data; +__tpm_buf_size_invariant (buf, buf_size); + +if (buf->flags &TPM_BUF_INVALID ) +return ; +WARN_ON (tag !=TPM_TAG_RQU_COMMAND && tag !=TPM2_ST_NO_SESSIONS && tag !=TPM2_ST_SESSIONS && tag !=0 ); buf->flags =0 ; buf->length =sizeof (*head); + buf->capacity = buf_size -sizeof (*buf); + buf->handles =0 ; head->tag =cpu_to_be16 (tag); head->length =cpu_to_be32 (sizeof (*head)); head->ordinal =cpu_to_be32 (ordinal); +} + +static void __tpm_buf_reset_sized (struct tpm_buf *buf,u16 buf_size) +{ +__tpm_buf_size_invariant (buf, buf_size); + +if (buf->flags &TPM_BUF_INVALID ) +return ; + + buf->flags =TPM_BUF_TPM2B ; + buf->length =2 ; + buf->capacity = buf_size -sizeof (*buf); buf->handles =0 ; + buf->data[0 ] =0 ; + buf->data[1 ] =0 ; } -EXPORT_SYMBOL_GPL (tpm_buf_reset);/** -* tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer -* @buf: A @tpm_buf -* -* Return: 0 or -ENOMEM +* tpm_buf_init() - Initialize a TPM command +* @buf: A &tpm_buf +* @buf_size: Size of the buffer. */ -int tpm_buf_init_sized (struct tpm_buf *buf) +void tpm_buf_init (struct tpm_buf *buf,u16 buf_size) { - buf->data = (u8 *)__get_free_page (GFP_KERNEL ); -if (!buf->data) -return -ENOMEM ; +memset (buf,0 , buf_size); +__tpm_buf_reset (buf, buf_size,TPM_TAG_RQU_COMMAND ,0 ); +} +EXPORT_SYMBOL_GPL (tpm_buf_init); -tpm_buf_reset_sized (buf); -return 0 ; +/** +* tpm_buf_init_sized() - Initialize a sized buffer +* @buf: A &tpm_buf +* @buf_size: Size of the buffer. +*/ +void tpm_buf_init_sized (struct tpm_buf *buf,u16 buf_size) +{ +memset (buf,0 , buf_size); +__tpm_buf_reset_sized (buf, buf_size); }EXPORT_SYMBOL_GPL (tpm_buf_init_sized);/** -* tpm_buf_reset_sized() - Initialize a sized buffer +* tpm_buf_reset() - Re-initialize a TPM command * @buf: A &tpm_buf +* @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS +* @ordinal: A command ordinal */ -void tpm_buf_reset_sized (struct tpm_buf *buf) +void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal) { - buf->flags =TPM_BUF_TPM2B ; - buf->length =2 ; - buf->data[0 ] =0 ; - buf->data[1 ] =0 ; +u16 buf_size = buf->capacity +sizeof (*buf); + +__tpm_buf_reset (buf, buf_size, tag, ordinal); } -EXPORT_SYMBOL_GPL (tpm_buf_reset_sized); +EXPORT_SYMBOL_GPL (tpm_buf_reset); -void tpm_buf_destroy (struct tpm_buf *buf) +/** +* tpm_buf_reset_sized() - Re-initialize a sized buffer +* @buf: A &tpm_buf +*/ +void tpm_buf_reset_sized (struct tpm_buf *buf) { -free_page ((unsigned long )buf->data); +u16 buf_size = buf->capacity +sizeof (*buf); + +__tpm_buf_reset_sized (buf, buf_size); } -EXPORT_SYMBOL_GPL (tpm_buf_destroy); +EXPORT_SYMBOL_GPL (tpm_buf_reset_sized);/** * tpm_buf_length() - Return the number of bytes consumed by the data
@@ -90,8 +117,11 @@ EXPORT_SYMBOL_GPL(tpm_buf_destroy);* * Return: The number of bytes consumed by the buffer */ -u32 tpm_buf_length (struct tpm_buf *buf) +u16 tpm_buf_length (struct tpm_buf *buf) { +if (buf->flags &TPM_BUF_INVALID ) +return 0 ; +return buf->length; }EXPORT_SYMBOL_GPL (tpm_buf_length);
@@ -104,10 +134,12 @@ EXPORT_SYMBOL_GPL(tpm_buf_length);*/ void tpm_buf_append (struct tpm_buf *buf,const u8 *new_data,u16 new_length) { +u32 total_length = (u32 )buf->length + (u32 )new_length; +if (buf->flags &TPM_BUF_INVALID )return ; -if ((buf->length + new_length) >PAGE_SIZE ) { +if (total_length > (u32 )buf->capacity) {WARN (1 ,"tpm_buf: write overflow\n" ); buf->flags |=TPM_BUF_INVALID ;return ;
drivers/char/tpm/tpm-sysfs.c +10 -11
@@ -32,28 +32,29 @@ struct tpm_readpubek_out {static ssize_t pubek_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct tpm_buf tpm_buf;struct tpm_readpubek_out *out;int i;char *str = buf;struct tpm_chip *chip =to_tpm_chip (dev);char anti_replay[20 ]; + struct tpm_buf *tpm_buf __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!tpm_buf) +return -ENOMEM ; +memset (&anti_replay,0 ,sizeof (anti_replay));if (tpm_try_get_ops (chip))return 0 ; -if (tpm_buf_init (&tpm_buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_READPUBEK )) - goto out_ops; - -tpm_buf_append (&tpm_buf, anti_replay,sizeof (anti_replay)); +tpm_buf_init (tpm_buf,TPM_BUFSIZE ); +tpm_buf_reset (tpm_buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_READPUBEK ); +tpm_buf_append (tpm_buf, anti_replay,sizeof (anti_replay)); -if (tpm_transmit_cmd (chip, &tpm_buf,READ_PUBEK_RESULT_MIN_BODY_SIZE , -"attempting to read the PUBEK" )) - goto out_buf; +if (tpm_transmit_cmd (chip, tpm_buf,READ_PUBEK_RESULT_MIN_BODY_SIZE ,"TPM_ReadPubek" )) + goto out_ops; - out = (struct tpm_readpubek_out *)&tpm_buf.data[10 ]; + out = (struct tpm_readpubek_out *)&tpm_buf->data[10 ]; str +=sprintf (str, "Algorithm: %4ph\n"
@@ -71,8 +72,6 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,for (i =0 ; i <256 ; i +=16 ) str +=sprintf (str,"%16ph\n" , &out->modulus[i]); -out_buf: -tpm_buf_destroy (&tpm_buf); out_ops:tpm_put_ops (chip);return str - buf;
drivers/char/tpm/tpm.h +0 -1
@@ -32,7 +32,6 @@#endif #define TPM_MINOR 224/* officially assigned */ -#define TPM_BUFSIZE 4096#define TPM_NUM_DEVICES 65536#define TPM_RETRY 50
drivers/char/tpm/tpm1-cmd.c +68 -94
271 lines not on this page · raw message
drivers/char/tpm/tpm2-cmd.c +136 -163
535 lines not on this page · raw message
drivers/char/tpm/tpm2-sessions.c +58 -64
224 lines not on this page · raw message
drivers/char/tpm/tpm2-space.c +18 -26
106 lines not on this page · raw message
drivers/char/tpm/tpm_vtpm_proxy.c +13 -17
@@ -395,40 +395,36 @@ static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip *chip, u8 status)static int vtpm_proxy_request_locality (struct tpm_chip *chip,int locality) { -struct tpm_buf buf;int rc;const struct tpm_header *header;struct proxy_dev *proxy_dev =dev_get_drvdata (&chip->dev); + struct tpm_buf *buf __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!buf) +return -ENOMEM ; + +tpm_buf_init (buf,TPM_BUFSIZE );if (chip->flags &TPM_CHIP_FLAG_TPM2 ) - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS , -TPM2_CC_SET_LOCALITY ); +tpm_buf_reset (buf,TPM2_ST_SESSIONS ,TPM2_CC_SET_LOCALITY );else - rc =tpm_buf_init (&buf,TPM_TAG_RQU_COMMAND , -TPM_ORD_SET_LOCALITY ); -if (rc) -return rc; -tpm_buf_append_u8 (&buf, locality); +tpm_buf_reset (buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_SET_LOCALITY ); + +tpm_buf_append_u8 (buf, locality); proxy_dev->state |=STATE_DRIVER_COMMAND ; - rc =tpm_transmit_cmd (chip, &buf,0 ,"attempting to set locality" ); + rc =tpm_transmit_cmd (chip, buf,0 ,"attempting to set locality" ); proxy_dev->state &= ~STATE_DRIVER_COMMAND ; -if (rc <0 ) { - locality = rc; - goto out; - } +if (rc <0 ) +return rc; - header = (const struct tpm_header *)buf.data; + header = (const struct tpm_header *)buf->data; rc =be32_to_cpu (header->return_code);if (rc) locality =-1 ; -out: -tpm_buf_destroy (&buf); -return locality; }
include/linux/tpm.h +11 -9
@@ -25,7 +25,8 @@#include <crypto/hash_info.h> #include <crypto/aes.h> -#define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ +#define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ +#define TPM_BUFSIZE 4096#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE#define TPM2_MAX_PCR_BANKS 8
@@ -376,13 +377,15 @@ enum tpm_buf_flags { };/* -* A string buffer type for constructing TPM commands. +* A buffer for constructing and parsing TPM commands, responses and sized +* (TPM2B) buffers. */ struct tpm_buf { -u32 flags; -u32 length; -u8 *data; +u8 flags;u8 handles; +u16 length; +u16 capacity; +u8 data[]; };enum tpm2_object_attributes {
@@ -408,12 +411,11 @@ enum tpm2_session_attributes { TPM2_SA_AUDIT = BIT(7), }; -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal); +void tpm_buf_init (struct tpm_buf *buf,u16 buf_size); +void tpm_buf_init_sized (struct tpm_buf *buf,u16 buf_size);void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal); -int tpm_buf_init_sized (struct tpm_buf *buf);void tpm_buf_reset_sized (struct tpm_buf *buf); -void tpm_buf_destroy (struct tpm_buf *buf); -u32 tpm_buf_length (struct tpm_buf *buf); +u16 tpm_buf_length (struct tpm_buf *buf);void tpm_buf_append (struct tpm_buf *buf,const u8 *new_data,u16 new_length);void tpm_buf_append_u8 (struct tpm_buf *buf,const u8 value);void tpm_buf_append_u16 (struct tpm_buf *buf,const u16 value);
security/keys/trusted-keys/trusted_tpm1.c +18 -18
@@ -310,9 +310,10 @@ static int TSS_checkhmac2(unsigned char *buffer,* For key specific tpm requests, we will generate and send our * own TPM command packets using the drivers send function. */ -static int trusted_tpm_send (unsigned char *cmd,size_t buflen) +static int trusted_tpm_send (void *cmd,size_t cmd_len) { -struct tpm_buf buf; +u8 buf_data[512 ]; +struct tpm_buf *buf = (struct tpm_buf *)buf_data;int rc;if (!chip)
@@ -322,11 +323,10 @@ static int trusted_tpm_send(unsigned char *cmd, size_t buflen)if (rc)return rc; - buf.flags =0 ; - buf.length = buflen; - buf.data = cmd; +tpm_buf_init (buf,sizeof (buf_data)); +tpm_buf_append (buf, cmd, cmd_len);dump_tpm_buf (cmd); - rc =tpm_transmit_cmd (chip, &buf,4 ,"sending data" ); + rc =tpm_transmit_cmd (chip, buf,4 ,"sending data" );dump_tpm_buf (cmd);if (rc >0 )
@@ -624,23 +624,23 @@ static int tpm_unseal(struct tpm_buf *tb,static int key_seal (struct trusted_key_payload *p,struct trusted_key_options *o) { -struct tpm_buf tb;int ret; - ret =tpm_buf_init (&tb,0 ,0 ); -if (ret) -return ret; + struct tpm_buf *tb __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!tb) +return -ENOMEM ; + +tpm_buf_init (tb,TPM_BUFSIZE );/* include migratable flag at end of sealed key */ p->key[p->key_len] = p->migratable; - ret =tpm_seal (&tb, o->keytype, o->keyhandle, o->keyauth, + ret =tpm_seal (tb, o->keytype, o->keyhandle, o->keyauth, p->key, p->key_len +1 , p->blob, &p->blob_len, o->blobauth, o->pcrinfo, o->pcrinfo_len);if (ret <0 )pr_info ("srkseal failed (%d)\n" , ret); -tpm_buf_destroy (&tb);return ret; }
@@ -650,14 +650,15 @@ static int key_seal(struct trusted_key_payload *p,static int key_unseal (struct trusted_key_payload *p,struct trusted_key_options *o) { -struct tpm_buf tb;int ret; - ret =tpm_buf_init (&tb,0 ,0 ); -if (ret) -return ret; + struct tpm_buf *tb __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!tb) +return -ENOMEM ; + +tpm_buf_init (tb,TPM_BUFSIZE ); - ret =tpm_unseal (&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len, + ret =tpm_unseal (tb, o->keyhandle, o->keyauth, p->blob, p->blob_len, o->blobauth, p->key, &p->key_len);if (ret <0 )pr_info ("srkunseal failed (%d)\n" , ret);
@@ -665,7 +666,6 @@ static int key_unseal(struct trusted_key_payload *p,/* pull migratable flag out of sealed key */ p->migratable = p->key[--p->key_len]; -tpm_buf_destroy (&tb);return ret; }
security/keys/trusted-keys/trusted_tpm2.c +78 -95
289 lines not on this page · raw message
Signature · 1 line
2.52.0