Skip to content

Commit faf07e6

Browse files
committed
tpm: Cap the number of PCR banks
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: stable@vger.kernel.org # v5.10+ Fixes: bcfff83 ("tpm: dynamically allocate the allocated_banks array") Tested-by: Lai Yi <yi1.lai@linux.intel.com> Reviewed-by: Jonathan McDowell <noodles@meta.com> Reviewed-by: Roberto Sassu <roberto.sassu@huawei.com> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@opinsys.com>
1 parent 020a0d8 commit faf07e6

4 files changed

Lines changed: 8 additions & 14 deletions

File tree

drivers/char/tpm/tpm-chip.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ static void tpm_dev_release(struct device *dev)
246246

247247
kfree(chip->work_space.context_buf);
248248
kfree(chip->work_space.session_buf);
249-
kfree(chip->allocated_banks);
250249
#ifdef CONFIG_TCG_TPM2_HMAC
251250
kfree(chip->auth);
252251
#endif

drivers/char/tpm/tpm1-cmd.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,6 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)
799799
*/
800800
int tpm1_get_pcr_allocation(struct tpm_chip *chip)
801801
{
802-
chip->allocated_banks = kcalloc(1, sizeof(*chip->allocated_banks),
803-
GFP_KERNEL);
804-
if (!chip->allocated_banks)
805-
return -ENOMEM;
806-
807802
chip->allocated_banks[0].alg_id = TPM_ALG_SHA1;
808803
chip->allocated_banks[0].digest_size = hash_digest_size[HASH_ALGO_SHA1];
809804
chip->allocated_banks[0].crypto_id = HASH_ALGO_SHA1;

drivers/char/tpm/tpm2-cmd.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
550550

551551
nr_possible_banks = be32_to_cpup(
552552
(__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
553-
554-
chip->allocated_banks = kcalloc(nr_possible_banks,
555-
sizeof(*chip->allocated_banks),
556-
GFP_KERNEL);
557-
if (!chip->allocated_banks) {
553+
if (nr_possible_banks > TPM2_MAX_PCR_BANKS) {
554+
pr_err("tpm: out of bank capacity: %u > %u\n",
555+
nr_possible_banks, TPM2_MAX_PCR_BANKS);
558556
rc = -ENOMEM;
559557
goto out;
560558
}

include/linux/tpm.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
#include <crypto/aes.h>
2727

2828
#define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */
29-
#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
29+
30+
#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
31+
#define TPM2_MAX_PCR_BANKS 8
3032

3133
struct tpm_chip;
3234
struct trusted_key_payload;
@@ -68,7 +70,7 @@ enum tpm2_curves {
6870

6971
struct tpm_digest {
7072
u16 alg_id;
71-
u8 digest[TPM_MAX_DIGEST_SIZE];
73+
u8 digest[TPM2_MAX_DIGEST_SIZE];
7274
} __packed;
7375

7476
struct tpm_bank_info {
@@ -189,7 +191,7 @@ struct tpm_chip {
189191
unsigned int groups_cnt;
190192

191193
u32 nr_allocated_banks;
192-
struct tpm_bank_info *allocated_banks;
194+
struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS];
193195
#ifdef CONFIG_ACPI
194196
acpi_handle acpi_dev_handle;
195197
char ppi_version[TPM_PPI_VERSION_LEN + 1];

0 commit comments

Comments
 (0)