Skip to contentSkip to conversation

tpm_tis_spi: Account for SPI header when allocating TPM SPI xfer buffer

PATCH Superseded Matthew R. Ochs 2 participants 6 messages May 21, 2024 – May 22, 2024

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

PATCH 1/1 tpm_tis_spi: Account for SPI header when allocating TPM SPI xfer buffer

The TPM SPI transfer mechanism uses MAX_SPI_FRAMESIZE for computing the maximum transfer length and the size of the transfer buffer. As such, it does not account for the 4 bytes of header that prepends the SPI data frame. This can result in out-of-bounds accesses and was confirmed with KASAN.

Introduce MAX_SPI_BUFSIZE to account for the header and use to allocate the transfer buffer.

Fixes a86a42ac2bd6 ("tpm_tis_spi: Add hardware wait polling")
---
 drivers/char/tpm/tpm_tis_spi_main.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm_tis_spi_main.c +3 -1
index 3f9eaf27b41b..ba50eaead9d8 100644
@@ -37,6 +37,8 @@
 #include "tpm_tis_spi.h"
 
 #define MAX_SPI_FRAMESIZE 64
+#define MAX_SPI_HDRSIZE 4
+#define MAX_SPI_BUFSIZE (MAX_SPI_HDRSIZE + MAX_SPI_FRAMESIZE)
 
 /*
  * TCG SPI flow control is documented in section 6.4 of the spec[1]. In short,
@@ -247,7 +249,7 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
 		     int irq, const struct tpm_tis_phy_ops *phy_ops)
 {
-	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
+	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_BUFSIZE, GFP_KERNEL);
 	if (!phy->iobuf)
 		return -ENOMEM;
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 21 lines · Matthew R. Ochs

The TPM SPI transfer mechanism uses MAX_SPI_FRAMESIZE for computing the maximum transfer length and the size of the transfer buffer. As such, it does not account for the 4 bytes of header that prepends the SPI data frame. This can result in out-of-bounds accesses and was confirmed with KASAN.

Introduce MAX_SPI_BUFSIZE to account for the header and use to allocate the transfer buffer.

Fixes a86a42ac2bd6 ("tpm_tis_spi: Add hardware wait polling")
---
 drivers/char/tpm/tpm_tis_spi_main.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm_tis_spi_main.c +0 -0
index 3f9eaf27b41b..ba50eaead9d8 100644

drivers/char/tpm/tpm_tis_spi_main.c line 41

Quoted 5 lines
drivers/char/tpm/tpm_tis_spi_main.c +2 -0
@@ -37,6 +37,8 @@
 #include "tpm_tis_spi.h"
 
 #define MAX_SPI_FRAMESIZE 64
+#define MAX_SPI_HDRSIZE 4
+#define MAX_SPI_BUFSIZE (MAX_SPI_HDRSIZE + MAX_SPI_FRAMESIZE)

"MAX_" prefix does not make sense in the new entries.

drivers/char/tpm/tpm_tis_spi_main.c static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr, · line 252

Quoted 9 lines
drivers/char/tpm/tpm_tis_spi_main.c +1 -1
 /*
  * TCG SPI flow control is documented in section 6.4 of the spec[1]. In short,
@@ -247,7 +249,7 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
 		     int irq, const struct tpm_tis_phy_ops *phy_ops)
 {
-	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
+	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_BUFSIZE, GFP_KERNEL);

It would better to open code here "SPI_HDRSIZE + MAX_SPI_FRAMESIZE".

I.e. less cross-referencing and documents better what is going on at the call site.

drivers/char/tpm/tpm_tis_spi_main.c static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr, · line 255

Quoted 3 lines
drivers/char/tpm/tpm_tis_spi_main.c +0 -0
 	if (!phy->iobuf)
 		return -ENOMEM;
 

BR, Jarkko

author
#2 lore raw
Quoted 13 lines · Jarkko Sakkinen

drivers/char/tpm/tpm_tis_spi_main.c static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr, · line 252

Quoted 9 lines

/*
* TCG SPI flow control is documented in section 6.4 of the spec[1]. In short,

drivers/char/tpm/tpm_tis_spi_main.c +1 -1
@@ -247,7 +249,7 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
 		     int irq, const struct tpm_tis_phy_ops *phy_ops)
 {
-	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
+	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_BUFSIZE, GFP_KERNEL);

It would better to open code here "SPI_HDRSIZE + MAX_SPI_FRAMESIZE".

Quoted 2 lines

I.e. less cross-referencing and documents better what is going on at the call site.

Sure, will make this change in a v2.

-matt

maintainer
#3 lore raw
Quoted 13 lines · Matt Ochs
Quoted 13 lines · Jarkko Sakkinen

drivers/char/tpm/tpm_tis_spi_main.c static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr, · line 252

Quoted 9 lines

/*
* TCG SPI flow control is documented in section 6.4 of the spec[1]. In short,

drivers/char/tpm/tpm_tis_spi_main.c +1 -1
@@ -247,7 +249,7 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
 		     int irq, const struct tpm_tis_phy_ops *phy_ops)
 {
-	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
+	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_BUFSIZE, GFP_KERNEL);

It would better to open code here "SPI_HDRSIZE + MAX_SPI_FRAMESIZE".

Quoted 4 lines
Quoted 2 lines

I.e. less cross-referencing and documents better what is going on at the call site.

Sure, will make this change in a v2.

Yeah, and thanks for spotting the bug and fixing it! Looking forward to the final fix.

BR, Jarkko

#4 lore raw

PATCH v2 1/1 tpm_tis_spi: Account for SPI header when allocating TPM SPI xfer buffer

In reply to Matthew R. Ochs

The TPM SPI transfer mechanism uses MAX_SPI_FRAMESIZE for computing the maximum transfer length and the size of the transfer buffer. As such, it does not account for the 4 bytes of header that prepends the SPI data frame. This can result in out-of-bounds accesses and was confirmed with KASAN.

Introduce SPI_HDRSIZE to account for the header and use to allocate the transfer buffer.

Fixes a86a42ac2bd6 ("tpm_tis_spi: Add hardware wait polling")

---
v2: Removed MAX_SPI_BUFSIZE in favor of open coding the buffer allocation

---
 drivers/char/tpm/tpm_tis_spi_main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm_tis_spi_main.c +2 -1
index 3f9eaf27b41b..c9eca24bbad4 100644
@@ -37,6 +37,7 @@
 #include "tpm_tis_spi.h"
 
 #define MAX_SPI_FRAMESIZE 64
+#define SPI_HDRSIZE 4
 
 /*
  * TCG SPI flow control is documented in section 6.4 of the spec[1]. In short,
@@ -247,7 +248,7 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
 		     int irq, const struct tpm_tis_phy_ops *phy_ops)
 {
-	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
+	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
 	if (!phy->iobuf)
 		return -ENOMEM;
 
Signature · 1 line
2.25.1
maintainer Reviewed-by on v2 1/1
#5 lore raw
Quoted 31 lines · Matthew R. Ochs

The TPM SPI transfer mechanism uses MAX_SPI_FRAMESIZE for computing the maximum transfer length and the size of the transfer buffer. As such, it does not account for the 4 bytes of header that prepends the SPI data frame. This can result in out-of-bounds accesses and was confirmed with KASAN.

Introduce SPI_HDRSIZE to account for the header and use to allocate the transfer buffer.

Fixes a86a42ac2bd6 ("tpm_tis_spi: Add hardware wait polling")

---
v2: Removed MAX_SPI_BUFSIZE in favor of open coding the buffer allocation

---
 drivers/char/tpm/tpm_tis_spi_main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm_tis_spi_main.c +1 -0
index 3f9eaf27b41b..c9eca24bbad4 100644
@@ -37,6 +37,7 @@
 #include "tpm_tis_spi.h"
 
 #define MAX_SPI_FRAMESIZE 64
+#define SPI_HDRSIZE 4
 
 /*
  * TCG SPI flow control is documented in section 6.4 of the spec[1]. In short,

drivers/char/tpm/tpm_tis_spi_main.c static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr, · line 254

Quoted 8 lines
drivers/char/tpm/tpm_tis_spi_main.c +1 -1
@@ -247,7 +248,7 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
 		     int irq, const struct tpm_tis_phy_ops *phy_ops)
 {
-	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
+	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
 	if (!phy->iobuf)
 		return -ENOMEM;
 

Thanks, this is much better. Now when reading the code after months go by, it is easy to see why the metrics for buffer size are what they are.

Given that it is a bug fix, I can put this to rc2 pull request. Thanks for finding and fixing the bug.

BR, Jarkko