Skip to content

Commit 6c4e79d

Browse files
author
Jarkko Sakkinen
committed
tpm: Unify the mismatching TPM space buffer sizes
The size of the buffers for storing context's and sessions can vary from arch to arch as PAGE_SIZE can be anything between 4 kB and 256 kB (the maximum for PPC64). Define a fixed buffer size set to 16 kB. This should be enough for most use with three handles (that is how many we allow at the moment). Parametrize the buffer size while doing this, so that it is easier to revisit this later on if required. Cc: [email protected] Reported-by: Stefan Berger <[email protected]> Fixes: 745b361 ("tpm: infrastructure for TPM spaces") Reviewed-by: Jerry Snitselaar <[email protected]> Tested-by: Stefan Berger <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent 7f3d176 commit 6c4e79d

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

drivers/char/tpm/tpm-chip.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,8 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
386386
chip->cdev.owner = THIS_MODULE;
387387
chip->cdevs.owner = THIS_MODULE;
388388

389-
chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
390-
if (!chip->work_space.context_buf) {
391-
rc = -ENOMEM;
392-
goto out;
393-
}
394-
chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
395-
if (!chip->work_space.session_buf) {
389+
rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
390+
if (rc) {
396391
rc = -ENOMEM;
397392
goto out;
398393
}

drivers/char/tpm/tpm.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ enum tpm_addr {
5959

6060
#define TPM_TAG_RQU_COMMAND 193
6161

62+
/* TPM2 specific constants. */
63+
#define TPM2_SPACE_BUFFER_SIZE 16384 /* 16 kB */
64+
6265
struct stclear_flags_t {
6366
__be16 tag;
6467
u8 deactivated;
@@ -228,7 +231,7 @@ unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
228231
int tpm2_probe(struct tpm_chip *chip);
229232
int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip);
230233
int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
231-
int tpm2_init_space(struct tpm_space *space);
234+
int tpm2_init_space(struct tpm_space *space, unsigned int buf_size);
232235
void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space);
233236
void tpm2_flush_space(struct tpm_chip *chip);
234237
int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,

drivers/char/tpm/tpm2-space.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,21 @@ static void tpm2_flush_sessions(struct tpm_chip *chip, struct tpm_space *space)
3838
}
3939
}
4040

41-
int tpm2_init_space(struct tpm_space *space)
41+
int tpm2_init_space(struct tpm_space *space, unsigned int buf_size)
4242
{
43-
space->context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
43+
space->context_buf = kzalloc(buf_size, GFP_KERNEL);
4444
if (!space->context_buf)
4545
return -ENOMEM;
4646

47-
space->session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
47+
space->session_buf = kzalloc(buf_size, GFP_KERNEL);
4848
if (space->session_buf == NULL) {
4949
kfree(space->context_buf);
50+
/* Prevent caller getting a dangling pointer. */
51+
space->context_buf = NULL;
5052
return -ENOMEM;
5153
}
5254

55+
space->buf_size = buf_size;
5356
return 0;
5457
}
5558

@@ -311,8 +314,10 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
311314
sizeof(space->context_tbl));
312315
memcpy(&chip->work_space.session_tbl, &space->session_tbl,
313316
sizeof(space->session_tbl));
314-
memcpy(chip->work_space.context_buf, space->context_buf, PAGE_SIZE);
315-
memcpy(chip->work_space.session_buf, space->session_buf, PAGE_SIZE);
317+
memcpy(chip->work_space.context_buf, space->context_buf,
318+
space->buf_size);
319+
memcpy(chip->work_space.session_buf, space->session_buf,
320+
space->buf_size);
316321

317322
rc = tpm2_load_space(chip);
318323
if (rc) {
@@ -492,7 +497,7 @@ static int tpm2_save_space(struct tpm_chip *chip)
492497
continue;
493498

494499
rc = tpm2_save_context(chip, space->context_tbl[i],
495-
space->context_buf, PAGE_SIZE,
500+
space->context_buf, space->buf_size,
496501
&offset);
497502
if (rc == -ENOENT) {
498503
space->context_tbl[i] = 0;
@@ -509,9 +514,8 @@ static int tpm2_save_space(struct tpm_chip *chip)
509514
continue;
510515

511516
rc = tpm2_save_context(chip, space->session_tbl[i],
512-
space->session_buf, PAGE_SIZE,
517+
space->session_buf, space->buf_size,
513518
&offset);
514-
515519
if (rc == -ENOENT) {
516520
/* handle error saving session, just forget it */
517521
space->session_tbl[i] = 0;
@@ -557,8 +561,10 @@ int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
557561
sizeof(space->context_tbl));
558562
memcpy(&space->session_tbl, &chip->work_space.session_tbl,
559563
sizeof(space->session_tbl));
560-
memcpy(space->context_buf, chip->work_space.context_buf, PAGE_SIZE);
561-
memcpy(space->session_buf, chip->work_space.session_buf, PAGE_SIZE);
564+
memcpy(space->context_buf, chip->work_space.context_buf,
565+
space->buf_size);
566+
memcpy(space->session_buf, chip->work_space.session_buf,
567+
space->buf_size);
562568

563569
return 0;
564570
out:

drivers/char/tpm/tpmrm-dev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static int tpmrm_open(struct inode *inode, struct file *file)
2121
if (priv == NULL)
2222
return -ENOMEM;
2323

24-
rc = tpm2_init_space(&priv->space);
24+
rc = tpm2_init_space(&priv->space, TPM2_SPACE_BUFFER_SIZE);
2525
if (rc) {
2626
kfree(priv);
2727
return -ENOMEM;

include/linux/tpm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ struct tpm_space {
9696
u8 *context_buf;
9797
u32 session_tbl[3];
9898
u8 *session_buf;
99+
u32 buf_size;
99100
};
100101

101102
struct tpm_bios_log {

0 commit comments

Comments
 (0)