Skip to content

Commit a6831a8

Browse files
committed
Merge tag 'tpmdd-next-20190805' of git://git.infradead.org/users/jjs/linux-tpmdd
Pull tpm fixes from Jarkko Sakkinen: "Two bug fixes that did not make into my first pull request" * tag 'tpmdd-next-20190805' of git://git.infradead.org/users/jjs/linux-tpmdd: tpm: tpm_ibm_vtpm: Fix unallocated banks tpm: Fix null pointer dereference on chip register error path
2 parents 62d1716 + fa4f99c commit a6831a8

File tree

4 files changed

+63
-24
lines changed

4 files changed

+63
-24
lines changed

drivers/char/tpm/tpm-chip.c

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ static int tpm_go_idle(struct tpm_chip *chip)
7777
return chip->ops->go_idle(chip);
7878
}
7979

80+
static void tpm_clk_enable(struct tpm_chip *chip)
81+
{
82+
if (chip->ops->clk_enable)
83+
chip->ops->clk_enable(chip, true);
84+
}
85+
86+
static void tpm_clk_disable(struct tpm_chip *chip)
87+
{
88+
if (chip->ops->clk_enable)
89+
chip->ops->clk_enable(chip, false);
90+
}
91+
8092
/**
8193
* tpm_chip_start() - power on the TPM
8294
* @chip: a TPM chip to use
@@ -89,22 +101,20 @@ int tpm_chip_start(struct tpm_chip *chip)
89101
{
90102
int ret;
91103

92-
if (chip->ops->clk_enable)
93-
chip->ops->clk_enable(chip, true);
104+
tpm_clk_enable(chip);
94105

95106
if (chip->locality == -1) {
96107
ret = tpm_request_locality(chip);
97108
if (ret) {
98-
chip->ops->clk_enable(chip, false);
109+
tpm_clk_disable(chip);
99110
return ret;
100111
}
101112
}
102113

103114
ret = tpm_cmd_ready(chip);
104115
if (ret) {
105116
tpm_relinquish_locality(chip);
106-
if (chip->ops->clk_enable)
107-
chip->ops->clk_enable(chip, false);
117+
tpm_clk_disable(chip);
108118
return ret;
109119
}
110120

@@ -124,8 +134,7 @@ void tpm_chip_stop(struct tpm_chip *chip)
124134
{
125135
tpm_go_idle(chip);
126136
tpm_relinquish_locality(chip);
127-
if (chip->ops->clk_enable)
128-
chip->ops->clk_enable(chip, false);
137+
tpm_clk_disable(chip);
129138
}
130139
EXPORT_SYMBOL_GPL(tpm_chip_stop);
131140

@@ -545,6 +554,20 @@ static int tpm_add_hwrng(struct tpm_chip *chip)
545554
return hwrng_register(&chip->hwrng);
546555
}
547556

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+
548571
/*
549572
* tpm_chip_register() - create a character device for the TPM chip
550573
* @chip: TPM chip to use.
@@ -564,6 +587,12 @@ int tpm_chip_register(struct tpm_chip *chip)
564587
if (rc)
565588
return rc;
566589
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);
567596
tpm_chip_stop(chip);
568597
if (rc)
569598
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)