Skip to content

Commit fa4f99c

Browse files
naynajainJarkko Sakkinen
authored andcommitted
tpm: tpm_ibm_vtpm: Fix unallocated banks
The nr_allocated_banks and allocated banks are initialized as part of tpm_chip_register. Currently, this is done as part of auto startup function. However, some drivers, like the ibm vtpm driver, do not run auto startup during initialization. This results in uninitialized memory issue and causes a kernel panic during boot. This patch moves the pcr allocation outside the auto startup function into tpm_chip_register. This ensures that allocated banks are initialized in any case. Fixes: 879b589 ("tpm: retrieve digest size of unknown algorithms with PCR read") Reported-by: Michal Suchanek <[email protected]> Signed-off-by: Nayna Jain <[email protected]> Reviewed-by: Mimi Zohar <[email protected]> Tested-by: Sachin Sant <[email protected]> Tested-by: Michal Suchánek <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent 1e5ac63 commit fa4f99c

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

drivers/char/tpm/tpm-chip.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,20 @@ static int tpm_add_hwrng(struct tpm_chip *chip)
554554
return hwrng_register(&chip->hwrng);
555555
}
556556

557+
static int tpm_get_pcr_allocation(struct tpm_chip *chip)
558+
{
559+
int rc;
560+
561+
rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
562+
tpm2_get_pcr_allocation(chip) :
563+
tpm1_get_pcr_allocation(chip);
564+
565+
if (rc > 0)
566+
return -ENODEV;
567+
568+
return rc;
569+
}
570+
557571
/*
558572
* tpm_chip_register() - create a character device for the TPM chip
559573
* @chip: TPM chip to use.
@@ -573,6 +587,12 @@ int tpm_chip_register(struct tpm_chip *chip)
573587
if (rc)
574588
return rc;
575589
rc = tpm_auto_startup(chip);
590+
if (rc) {
591+
tpm_chip_stop(chip);
592+
return rc;
593+
}
594+
595+
rc = tpm_get_pcr_allocation(chip);
576596
tpm_chip_stop(chip);
577597
if (rc)
578598
return rc;

drivers/char/tpm/tpm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
394394
ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
395395
const char *desc, size_t min_cap_length);
396396
int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
397+
int tpm1_get_pcr_allocation(struct tpm_chip *chip);
397398
unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
398399
int tpm_pm_suspend(struct device *dev);
399400
int tpm_pm_resume(struct device *dev);
@@ -449,6 +450,7 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,
449450
ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
450451
u32 *value, const char *desc);
451452

453+
ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip);
452454
int tpm2_auto_startup(struct tpm_chip *chip);
453455
void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
454456
unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);

drivers/char/tpm/tpm1-cmd.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -699,18 +699,6 @@ int tpm1_auto_startup(struct tpm_chip *chip)
699699
goto out;
700700
}
701701

702-
chip->allocated_banks = kcalloc(1, sizeof(*chip->allocated_banks),
703-
GFP_KERNEL);
704-
if (!chip->allocated_banks) {
705-
rc = -ENOMEM;
706-
goto out;
707-
}
708-
709-
chip->allocated_banks[0].alg_id = TPM_ALG_SHA1;
710-
chip->allocated_banks[0].digest_size = hash_digest_size[HASH_ALGO_SHA1];
711-
chip->allocated_banks[0].crypto_id = HASH_ALGO_SHA1;
712-
chip->nr_allocated_banks = 1;
713-
714702
return rc;
715703
out:
716704
if (rc > 0)
@@ -779,3 +767,27 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)
779767
return rc;
780768
}
781769

770+
/**
771+
* tpm1_get_pcr_allocation() - initialize the allocated bank
772+
* @chip: TPM chip to use.
773+
*
774+
* The function initializes the SHA1 allocated bank to extend PCR
775+
*
776+
* Return:
777+
* * 0 on success,
778+
* * < 0 on error.
779+
*/
780+
int tpm1_get_pcr_allocation(struct tpm_chip *chip)
781+
{
782+
chip->allocated_banks = kcalloc(1, sizeof(*chip->allocated_banks),
783+
GFP_KERNEL);
784+
if (!chip->allocated_banks)
785+
return -ENOMEM;
786+
787+
chip->allocated_banks[0].alg_id = TPM_ALG_SHA1;
788+
chip->allocated_banks[0].digest_size = hash_digest_size[HASH_ALGO_SHA1];
789+
chip->allocated_banks[0].crypto_id = HASH_ALGO_SHA1;
790+
chip->nr_allocated_banks = 1;
791+
792+
return 0;
793+
}

drivers/char/tpm/tpm2-cmd.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ struct tpm2_pcr_selection {
840840
u8 pcr_select[3];
841841
} __packed;
842842

843-
static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
843+
ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
844844
{
845845
struct tpm2_pcr_selection pcr_selection;
846846
struct tpm_buf buf;
@@ -1040,10 +1040,6 @@ int tpm2_auto_startup(struct tpm_chip *chip)
10401040
goto out;
10411041
}
10421042

1043-
rc = tpm2_get_pcr_allocation(chip);
1044-
if (rc)
1045-
goto out;
1046-
10471043
rc = tpm2_get_cc_attrs_tbl(chip);
10481044

10491045
out:

0 commit comments

Comments
 (0)