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: 879b589210a9 ("tpm: retrieve digest size of unknown algorithms with PCR read")
Reported-by: Michal Suchanek <msuchanek@suse.de>
Signed-off-by: Nayna Jain <nayna@linux.ibm.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
Tested-by: Sachin Sant <sachinp@linux.vnet.ibm.com>
Tested-by: Michal Suchánek <msuchanek@suse.de>
Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>

authored by Nayna Jain and committed by Jarkko Sakkinen fa4f99c0 1e5ac630

Changed files
+47 -17
drivers
+20
drivers/char/tpm/tpm-chip.c
··· 554 554 return hwrng_register(&chip->hwrng); 555 555 } 556 556 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 + 557 571 /* 558 572 * tpm_chip_register() - create a character device for the TPM chip 559 573 * @chip: TPM chip to use. ··· 587 573 if (rc) 588 574 return rc; 589 575 rc = tpm_auto_startup(chip); 576 + if (rc) { 577 + tpm_chip_stop(chip); 578 + return rc; 579 + } 580 + 581 + rc = tpm_get_pcr_allocation(chip); 590 582 tpm_chip_stop(chip); 591 583 if (rc) 592 584 return rc;
+2
drivers/char/tpm/tpm.h
··· 394 394 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap, 395 395 const char *desc, size_t min_cap_length); 396 396 int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max); 397 + int tpm1_get_pcr_allocation(struct tpm_chip *chip); 397 398 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal); 398 399 int tpm_pm_suspend(struct device *dev); 399 400 int tpm_pm_resume(struct device *dev); ··· 450 449 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, 451 450 u32 *value, const char *desc); 452 451 452 + ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip); 453 453 int tpm2_auto_startup(struct tpm_chip *chip); 454 454 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type); 455 455 unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
+24 -12
drivers/char/tpm/tpm1-cmd.c
··· 699 699 goto out; 700 700 } 701 701 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 - 714 702 return rc; 715 703 out: 716 704 if (rc > 0) ··· 767 779 return rc; 768 780 } 769 781 782 + /** 783 + * tpm1_get_pcr_allocation() - initialize the allocated bank 784 + * @chip: TPM chip to use. 785 + * 786 + * The function initializes the SHA1 allocated bank to extend PCR 787 + * 788 + * Return: 789 + * * 0 on success, 790 + * * < 0 on error. 791 + */ 792 + int tpm1_get_pcr_allocation(struct tpm_chip *chip) 793 + { 794 + chip->allocated_banks = kcalloc(1, sizeof(*chip->allocated_banks), 795 + GFP_KERNEL); 796 + if (!chip->allocated_banks) 797 + return -ENOMEM; 798 + 799 + chip->allocated_banks[0].alg_id = TPM_ALG_SHA1; 800 + chip->allocated_banks[0].digest_size = hash_digest_size[HASH_ALGO_SHA1]; 801 + chip->allocated_banks[0].crypto_id = HASH_ALGO_SHA1; 802 + chip->nr_allocated_banks = 1; 803 + 804 + return 0; 805 + }
+1 -5
drivers/char/tpm/tpm2-cmd.c
··· 840 840 u8 pcr_select[3]; 841 841 } __packed; 842 842 843 - static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) 843 + ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) 844 844 { 845 845 struct tpm2_pcr_selection pcr_selection; 846 846 struct tpm_buf buf; ··· 1039 1039 if (rc) 1040 1040 goto out; 1041 1041 } 1042 - 1043 - rc = tpm2_get_pcr_allocation(chip); 1044 - if (rc) 1045 - goto out; 1046 1042 1047 1043 rc = tpm2_get_cc_attrs_tbl(chip); 1048 1044