Skip to contentSkip to conversation

tpm: Define TPM2_SPACE_BUFFER_SIZE to replace the use of PAGE_SIZE

PATCH In review Jarkko Sakkinen 2 participants 3 messages Jun 26, 2020 – Jul 2, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: Define TPM2_SPACE_BUFFER_SIZE to replace the use of PAGE_SIZE

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).

Fixes 745b361e989a ("tpm: infrastructure for TPM spaces")
---
 drivers/char/tpm/tpm2-space.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
drivers/char/tpm/tpm2-space.c +16 -11
index 982d341d8837..9bef646093d1 100644
@@ -15,6 +15,8 @@
 #include <asm/unaligned.h>
 #include "tpm.h"
 
+#define TPM2_SPACE_BUFFER_SIZE		16384 /* 16 kB */
+
 enum tpm2_handle_types {
 	TPM2_HT_HMAC_SESSION	= 0x02000000,
 	TPM2_HT_POLICY_SESSION	= 0x03000000,
@@ -40,11 +42,11 @@ static void tpm2_flush_sessions(struct tpm_chip *chip, struct tpm_space *space)
 
 int tpm2_init_space(struct tpm_space *space)
 {
-	space->context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	space->context_buf = kzalloc(TPM2_SPACE_BUFFER_SIZE, GFP_KERNEL);
 	if (!space->context_buf)
 		return -ENOMEM;
 
-	space->session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	space->session_buf = kzalloc(TPM2_SPACE_BUFFER_SIZE, GFP_KERNEL);
 	if (space->session_buf == NULL) {
 		kfree(space->context_buf);
 		return -ENOMEM;
@@ -311,8 +313,10 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
 	       sizeof(space->context_tbl));
 	memcpy(&chip->work_space.session_tbl, &space->session_tbl,
 	       sizeof(space->session_tbl));
-	memcpy(chip->work_space.context_buf, space->context_buf, PAGE_SIZE);
-	memcpy(chip->work_space.session_buf, space->session_buf, PAGE_SIZE);
+	memcpy(chip->work_space.context_buf, space->context_buf,
+	       TPM2_SPACE_BUFFER_SIZE);
+	memcpy(chip->work_space.session_buf, space->session_buf,
+	       TPM2_SPACE_BUFFER_SIZE);
 
 	rc = tpm2_load_space(chip);
 	if (rc) {
@@ -492,8 +496,8 @@ static int tpm2_save_space(struct tpm_chip *chip)
 			continue;
 
 		rc = tpm2_save_context(chip, space->context_tbl[i],
-				       space->context_buf, PAGE_SIZE,
-				       &offset);
+				       space->context_buf,
+				       TPM2_SPACE_BUFFER_SIZE, &offset);
 		if (rc == -ENOENT) {
 			space->context_tbl[i] = 0;
 			continue;
@@ -509,9 +513,8 @@ static int tpm2_save_space(struct tpm_chip *chip)
 			continue;
 
 		rc = tpm2_save_context(chip, space->session_tbl[i],
-				       space->session_buf, PAGE_SIZE,
-				       &offset);
-
+				       space->session_buf,
+				       TPM2_SPACE_BUFFER_SIZE, &offset);
 		if (rc == -ENOENT) {
 			/* handle error saving session, just forget it */
 			space->session_tbl[i] = 0;
@@ -557,8 +560,10 @@ int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
 	       sizeof(space->context_tbl));
 	memcpy(&space->session_tbl, &chip->work_space.session_tbl,
 	       sizeof(space->session_tbl));
-	memcpy(space->context_buf, chip->work_space.context_buf, PAGE_SIZE);
-	memcpy(space->session_buf, chip->work_space.session_buf, PAGE_SIZE);
+	memcpy(space->context_buf, chip->work_space.context_buf,
+	       TPM2_SPACE_BUFFER_SIZE);
+	memcpy(space->session_buf, chip->work_space.session_buf,
+	       TPM2_SPACE_BUFFER_SIZE);
 
 	return 0;
 out:
Signature · 1 line
2.25.1
#1 lore raw
Quoted 78 lines · Jarkko Sakkinen

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).

Fixes 745b361e989a ("tpm: infrastructure for TPM spaces")
---
  drivers/char/tpm/tpm2-space.c | 27 ++++++++++++++++-----------
  1 file changed, 16 insertions(+), 11 deletions(-)
drivers/char/tpm/tpm2-space.c +12 -9
index 982d341d8837..9bef646093d1 100644
@@ -15,6 +15,8 @@
  #include <asm/unaligned.h>
  #include "tpm.h"
 
+#define TPM2_SPACE_BUFFER_SIZE		16384 /* 16 kB */
+
  enum tpm2_handle_types {
  	TPM2_HT_HMAC_SESSION	= 0x02000000,
  	TPM2_HT_POLICY_SESSION	= 0x03000000,
@@ -40,11 +42,11 @@ static void tpm2_flush_sessions(struct tpm_chip *chip, struct tpm_space *space)
 
  int tpm2_init_space(struct tpm_space *space)
  {
-	space->context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	space->context_buf = kzalloc(TPM2_SPACE_BUFFER_SIZE, GFP_KERNEL);
  	if (!space->context_buf)
  		return -ENOMEM;
 
-	space->session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	space->session_buf = kzalloc(TPM2_SPACE_BUFFER_SIZE, GFP_KERNEL);
  	if (space->session_buf == NULL) {
  		kfree(space->context_buf);
  		return -ENOMEM;
@@ -311,8 +313,10 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
  	       sizeof(space->context_tbl));
  	memcpy(&chip->work_space.session_tbl, &space->session_tbl,
  	       sizeof(space->session_tbl));
-	memcpy(chip->work_space.context_buf, space->context_buf, PAGE_SIZE);
-	memcpy(chip->work_space.session_buf, space->session_buf, PAGE_SIZE);
+	memcpy(chip->work_space.context_buf, space->context_buf,
+	       TPM2_SPACE_BUFFER_SIZE);
+	memcpy(chip->work_space.session_buf, space->session_buf,
+	       TPM2_SPACE_BUFFER_SIZE);
 
  	rc = tpm2_load_space(chip);
  	if (rc) {
@@ -492,8 +496,8 @@ static int tpm2_save_space(struct tpm_chip *chip)
  			continue;
 
  		rc = tpm2_save_context(chip, space->context_tbl[i],
-				       space->context_buf, PAGE_SIZE,
-				       &offset);
+				       space->context_buf,
+				       TPM2_SPACE_BUFFER_SIZE, &offset);
  		if (rc == -ENOENT) {
  			space->context_tbl[i] = 0;
  			continue;
@@ -509,9 +513,8 @@ static int tpm2_save_space(struct tpm_chip *chip)
  			continue;
 
  		rc = tpm2_save_context(chip, space->session_tbl[i],
-				       space->session_buf, PAGE_SIZE,
-				       &offset);
-
+				       space->session_buf,
+				       TPM2_SPACE_BUFFER_SIZE, &offset);
  		if (rc == -ENOENT) {
  			/* handle error saving session, just forget it */
  			space->session_tbl[i] = 0;

drivers/char/tpm/tpm2-space.c int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, · line 566

Quoted 9 lines
drivers/char/tpm/tpm2-space.c +4 -2
@@ -557,8 +560,10 @@ int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
  	       sizeof(space->context_tbl));
  	memcpy(&space->session_tbl, &chip->work_space.session_tbl,
  	       sizeof(space->session_tbl));
-	memcpy(space->context_buf, chip->work_space.context_buf, PAGE_SIZE);
-	memcpy(space->session_buf, chip->work_space.session_buf, PAGE_SIZE);
+	memcpy(space->context_buf, chip->work_space.context_buf,
+	       TPM2_SPACE_BUFFER_SIZE);
+	memcpy(space->session_buf, chip->work_space.session_buf,
+	       TPM2_SPACE_BUFFER_SIZE);

work_space.session_buf and context_buf also need allocation changes, otherwise we read from a smaller buffer here. See comment to other patch about tpm-chip.c .

Quoted 3 lines
drivers/char/tpm/tpm2-space.c +0 -0
  	return 0;
  out:
#2 lore raw
Quoted 79 lines · Stefan Berger
Quoted 78 lines · Jarkko Sakkinen

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).

Fixes 745b361e989a ("tpm: infrastructure for TPM spaces")
---
  drivers/char/tpm/tpm2-space.c | 27 ++++++++++++++++-----------
  1 file changed, 16 insertions(+), 11 deletions(-)
drivers/char/tpm/tpm2-space.c +13 -11
index 982d341d8837..9bef646093d1 100644
@@ -15,6 +15,8 @@
  #include <asm/unaligned.h>
  #include "tpm.h"
+#define TPM2_SPACE_BUFFER_SIZE		16384 /* 16 kB */
+
  enum tpm2_handle_types {
  	TPM2_HT_HMAC_SESSION	= 0x02000000,
  	TPM2_HT_POLICY_SESSION	= 0x03000000,
@@ -40,11 +42,11 @@ static void tpm2_flush_sessions(struct tpm_chip *chip, struct tpm_space *space)
  int tpm2_init_space(struct tpm_space *space)
  {
-	space->context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	space->context_buf = kzalloc(TPM2_SPACE_BUFFER_SIZE, GFP_KERNEL);
  	if (!space->context_buf)
  		return -ENOMEM;
-	space->session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	space->session_buf = kzalloc(TPM2_SPACE_BUFFER_SIZE, GFP_KERNEL);
  	if (space->session_buf == NULL) {
  		kfree(space->context_buf);
  		return -ENOMEM;
@@ -311,8 +313,10 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
  	       sizeof(space->context_tbl));
  	memcpy(&chip->work_space.session_tbl, &space->session_tbl,
  	       sizeof(space->session_tbl));
-	memcpy(chip->work_space.context_buf, space->context_buf, PAGE_SIZE);
-	memcpy(chip->work_space.session_buf, space->session_buf, PAGE_SIZE);
+	memcpy(chip->work_space.context_buf, space->context_buf,
+	       TPM2_SPACE_BUFFER_SIZE);
+	memcpy(chip->work_space.session_buf, space->session_buf,
+	       TPM2_SPACE_BUFFER_SIZE);
  	rc = tpm2_load_space(chip);
  	if (rc) {
@@ -492,8 +496,8 @@ static int tpm2_save_space(struct tpm_chip *chip)
  			continue;
  		rc = tpm2_save_context(chip, space->context_tbl[i],
-				       space->context_buf, PAGE_SIZE,
-				       &offset);
+				       space->context_buf,
+				       TPM2_SPACE_BUFFER_SIZE, &offset);
  		if (rc == -ENOENT) {
  			space->context_tbl[i] = 0;
  			continue;
@@ -509,9 +513,8 @@ static int tpm2_save_space(struct tpm_chip *chip)
  			continue;
  		rc = tpm2_save_context(chip, space->session_tbl[i],
-				       space->session_buf, PAGE_SIZE,
-				       &offset);
-
+				       space->session_buf,
+				       TPM2_SPACE_BUFFER_SIZE, &offset);
  		if (rc == -ENOENT) {
  			/* handle error saving session, just forget it */
  			space->session_tbl[i] = 0;
@@ -557,8 +560,10 @@ int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
  	       sizeof(space->context_tbl));
  	memcpy(&space->session_tbl, &chip->work_space.session_tbl,
  	       sizeof(space->session_tbl));
-	memcpy(space->context_buf, chip->work_space.context_buf, PAGE_SIZE);
-	memcpy(space->session_buf, chip->work_space.session_buf, PAGE_SIZE);
+	memcpy(space->context_buf, chip->work_space.context_buf,
Quoted 8 lines

drivers/char/tpm/tpm2-space.c int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, · line 566

Quoted 3 lines
drivers/char/tpm/tpm2-space.c +3 -0
+	       TPM2_SPACE_BUFFER_SIZE);
+	memcpy(space->session_buf, chip->work_space.session_buf,
+	       TPM2_SPACE_BUFFER_SIZE);

work_space.session_buf and context_buf also need allocation changes, otherwise we read from a smaller buffer here. See comment to other patch about tpm-chip.c .

Thank you, a good catch. I'll send an update.

/Jarkko