Skip to content

Commit bcfff83

Browse files
robertosassuJarkko Sakkinen
authored andcommitted
tpm: dynamically allocate the allocated_banks array
This patch renames active_banks (member of tpm_chip) to allocated_banks, stores the number of allocated PCR banks in nr_allocated_banks (new member of tpm_chip), and replaces the static array with a pointer to a dynamically allocated array. tpm2_get_pcr_allocation() determines if a PCR bank is allocated by checking the mask in the TPML_PCR_SELECTION structure returned by the TPM for TPM2_Get_Capability(). If a bank is not allocated, the TPM returns that bank in TPML_PCR_SELECTION, with all bits in the mask set to zero. In this case, the bank is not included in chip->allocated_banks, to avoid that TPM driver users unnecessarily calculate a digest for that bank. One PCR bank with algorithm set to SHA1 is always allocated for TPM 1.x. As a consequence of the introduction of nr_allocated_banks, tpm_pcr_extend() does not check anymore if the algorithm stored in tpm_chip is equal to zero. Signed-off-by: Roberto Sassu <[email protected]> Tested-by: Jarkko Sakkinen <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent 47a6c28 commit bcfff83

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

drivers/char/tpm/tpm-chip.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ static void tpm_dev_release(struct device *dev)
266266
kfree(chip->log.bios_event_log);
267267
kfree(chip->work_space.context_buf);
268268
kfree(chip->work_space.session_buf);
269+
kfree(chip->allocated_banks);
269270
kfree(chip);
270271
}
271272

drivers/char/tpm/tpm-interface.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,25 +318,27 @@ EXPORT_SYMBOL_GPL(tpm_pcr_read);
318318
int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
319319
{
320320
int rc;
321-
struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
322-
u32 count = 0;
321+
struct tpm2_digest *digest_list;
323322
int i;
324323

325324
chip = tpm_find_get_ops(chip);
326325
if (!chip)
327326
return -ENODEV;
328327

329328
if (chip->flags & TPM_CHIP_FLAG_TPM2) {
330-
memset(digest_list, 0, sizeof(digest_list));
329+
digest_list = kcalloc(chip->nr_allocated_banks,
330+
sizeof(*digest_list), GFP_KERNEL);
331+
if (!digest_list)
332+
return -ENOMEM;
331333

332-
for (i = 0; i < ARRAY_SIZE(chip->active_banks) &&
333-
chip->active_banks[i] != TPM2_ALG_ERROR; i++) {
334-
digest_list[i].alg_id = chip->active_banks[i];
334+
for (i = 0; i < chip->nr_allocated_banks; i++) {
335+
digest_list[i].alg_id = chip->allocated_banks[i];
335336
memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
336-
count++;
337337
}
338338

339-
rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
339+
rc = tpm2_pcr_extend(chip, pcr_idx, chip->nr_allocated_banks,
340+
digest_list);
341+
kfree(digest_list);
340342
tpm_put_ops(chip);
341343
return rc;
342344
}

drivers/char/tpm/tpm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ struct tpm_chip {
257257
const struct attribute_group *groups[3];
258258
unsigned int groups_cnt;
259259

260-
u16 active_banks[7];
260+
u32 nr_allocated_banks;
261+
u16 *allocated_banks;
261262
#ifdef CONFIG_ACPI
262263
acpi_handle acpi_dev_handle;
263264
char ppi_version[TPM_PPI_VERSION_LEN + 1];

drivers/char/tpm/tpm1-cmd.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,16 @@ int tpm1_auto_startup(struct tpm_chip *chip)
696696
goto out;
697697
}
698698

699+
chip->allocated_banks = kcalloc(1, sizeof(*chip->allocated_banks),
700+
GFP_KERNEL);
701+
if (!chip->allocated_banks) {
702+
rc = -ENOMEM;
703+
goto out;
704+
}
705+
706+
chip->allocated_banks[0] = TPM2_ALG_SHA1;
707+
chip->nr_allocated_banks = 1;
708+
699709
return rc;
700710
out:
701711
if (rc > 0)

drivers/char/tpm/tpm2-cmd.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
234234
int i;
235235
int j;
236236

237-
if (count > ARRAY_SIZE(chip->active_banks))
237+
if (count > chip->nr_allocated_banks)
238238
return -EINVAL;
239239

240240
rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
@@ -808,8 +808,10 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
808808
void *marker;
809809
void *end;
810810
void *pcr_select_offset;
811-
unsigned int count;
812811
u32 sizeof_pcr_selection;
812+
u32 nr_possible_banks;
813+
u32 nr_alloc_banks = 0;
814+
u16 hash_alg;
813815
u32 rsp_len;
814816
int rc;
815817
int i = 0;
@@ -826,11 +828,14 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
826828
if (rc)
827829
goto out;
828830

829-
count = be32_to_cpup(
831+
nr_possible_banks = be32_to_cpup(
830832
(__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
831833

832-
if (count > ARRAY_SIZE(chip->active_banks)) {
833-
rc = -ENODEV;
834+
chip->allocated_banks = kcalloc(nr_possible_banks,
835+
sizeof(*chip->allocated_banks),
836+
GFP_KERNEL);
837+
if (!chip->allocated_banks) {
838+
rc = -ENOMEM;
834839
goto out;
835840
}
836841

@@ -839,7 +844,7 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
839844
rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
840845
end = &buf.data[rsp_len];
841846

842-
for (i = 0; i < count; i++) {
847+
for (i = 0; i < nr_possible_banks; i++) {
843848
pcr_select_offset = marker +
844849
offsetof(struct tpm2_pcr_selection, size_of_select);
845850
if (pcr_select_offset >= end) {
@@ -848,17 +853,23 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
848853
}
849854

850855
memcpy(&pcr_selection, marker, sizeof(pcr_selection));
851-
chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
856+
hash_alg = be16_to_cpu(pcr_selection.hash_alg);
857+
858+
pcr_select_offset = memchr_inv(pcr_selection.pcr_select, 0,
859+
pcr_selection.size_of_select);
860+
if (pcr_select_offset) {
861+
chip->allocated_banks[nr_alloc_banks] = hash_alg;
862+
nr_alloc_banks++;
863+
}
864+
852865
sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
853866
sizeof(pcr_selection.size_of_select) +
854867
pcr_selection.size_of_select;
855868
marker = marker + sizeof_pcr_selection;
856869
}
857870

871+
chip->nr_allocated_banks = nr_alloc_banks;
858872
out:
859-
if (i < ARRAY_SIZE(chip->active_banks))
860-
chip->active_banks[i] = TPM2_ALG_ERROR;
861-
862873
tpm_buf_destroy(&buf);
863874

864875
return rc;

0 commit comments

Comments
 (0)