Skip to contentSkip to conversation

tpm: use get_unaligned_be32 unaligned buffer access.

PATCH In review Tomas Winkler 3 participants 6 messages Nov 23, 2016 – Dec 3, 2016 TPM device driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: use get_unaligned_be32 unaligned buffer access.

Use get_unaligned_be32 as b32_to_cpu doesn't work correctly on all platforms for unaligned access.

The fix doesn't cover all the cases as also some cast structures have members on unaligned addresses.

---
 drivers/char/tpm/tpm-interface.c    | 4 ++--
 drivers/char/tpm/tpm-sysfs.c        | 2 +-
 drivers/char/tpm/tpm.h              | 1 +
 drivers/char/tpm/tpm_crb.c          | 4 ++--
 drivers/char/tpm/tpm_i2c_infineon.c | 5 +++--
 drivers/char/tpm/tpm_i2c_nuvoton.c  | 7 ++++---
 drivers/char/tpm/tpm_nsc.c          | 4 +---
 drivers/char/tpm/tpm_tis_core.c     | 4 ++--
 8 files changed, 16 insertions(+), 15 deletions(-)
drivers/char/tpm/tpm-interface.c +2 -2
index 769d8b0d31a3..4cf38d00d1b3 100644
@@ -353,8 +353,8 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
 	if (bufsiz > TPM_BUFSIZE)
 		bufsiz = TPM_BUFSIZE;
 
-	count = be32_to_cpu(*((__be32 *) (buf + 2)));
-	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+	count = get_unaligned_be32(buf + 2);
+	ordinal = get_unaligned_be32(buf + 6);
 	if (count == 0)
 		return -ENODATA;
 	if (count > bufsiz) {
drivers/char/tpm/tpm-sysfs.c +1 -1
index 848ad6580b46..2435d710b6af 100644
@@ -71,7 +71,7 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
 		    data[12], data[13], data[14], data[15],
 		    data[16], data[17], data[18], data[19],
 		    data[20], data[21], data[22], data[23],
-		    be32_to_cpu(*((__be32 *) (data + 24))));
+		    be32_to_cpup((__be32 *)(data + 24)));
 
 	for (i = 0; i < 256; i++) {
 		str += sprintf(str, "%02X ", data[i + 28]);
drivers/char/tpm/tpm.h +1 -0
index 1ae976894257..83dba0ff5ea0 100644
@@ -23,6 +23,7 @@
 #ifndef __TPM_H__
 #define __TPM_H__
 
+#include <asm/unaligned.h>
 #include <linux/module.h>
 #include <linux/delay.h>
 #include <linux/fs.h>
drivers/char/tpm/tpm_crb.c +2 -2
index 65040d74bb02..8067cfbfdbe2 100644
@@ -168,7 +168,7 @@ static u8 crb_status(struct tpm_chip *chip)
 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 {
 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
-	unsigned int expected;
+	u32 expected;
 
 	/* sanity check */
 	if (count < 6)
@@ -178,7 +178,7 @@ static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		return -EIO;
 
 	memcpy_fromio(buf, priv->rsp, 6);
-	expected = be32_to_cpup((__be32 *) &buf[2]);
+	expected = get_unaligned_be32(buf + 2);
 
 	if (expected > count)
 		return -EIO;
drivers/char/tpm/tpm_i2c_infineon.c +3 -2
index 62ee44e57ddc..2dd4e3bb14c7 100644
@@ -437,7 +437,8 @@ static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 {
 	int size = 0;
-	int expected, status;
+	u32 expected;
+	int status;
 
 	if (count < TPM_HEADER_SIZE) {
 		size = -EIO;
@@ -451,7 +452,7 @@ static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		goto out;
 	}
 
-	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	expected = get_unaligned_be32(buf + 2);
 	if ((size_t) expected > count) {
 		size = -EIO;
 		goto out;
drivers/char/tpm/tpm_i2c_nuvoton.c +4 -3
index e3a9155ee671..7ba9c435da4e 100644
@@ -273,7 +273,8 @@ static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 	struct device *dev = chip->dev.parent;
 	struct i2c_client *client = to_i2c_client(dev);
 	s32 rc;
-	int expected, status, burst_count, retries, size = 0;
+	int status, burst_count, retries, size = 0;
+	u32 expected;
 
 	if (count < TPM_HEADER_SIZE) {
 		i2c_nuvoton_ready(chip);    /* return to idle */
@@ -314,7 +315,7 @@ static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		 * convert number of expected bytes field from big endian 32 bit
 		 * to machine native
 		 */
-		expected = be32_to_cpu(*(__be32 *) (buf + 2));
+		expected = get_unaligned_be32(buf + 2);
 		if (expected > count) {
 			dev_err(dev, "%s() expected > count\n", __func__);
 			size = -EIO;
@@ -442,7 +443,7 @@ static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
 		i2c_nuvoton_ready(chip);
 		return rc;
 	}
-	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+	ordinal = get_unaligned_be32(buf + 6);
 	rc = i2c_nuvoton_wait_for_data_avail(chip,
 					     tpm_calc_ordinal_duration(chip,
 								       ordinal),
drivers/char/tpm/tpm_nsc.c +1 -3
index 9ff0e072c476..99a8ff6ea37d 100644
@@ -131,7 +131,6 @@ static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 	u8 *buffer = buf;
 	u8 data, *p;
 	u32 size;
-	__be32 *native_size;
 
 	if (count < 6)
 		return -EIO;
@@ -174,8 +173,7 @@ static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 		return -EIO;
 	}
 
-	native_size = (__force __be32 *) (buf + 2);
-	size = be32_to_cpu(*native_size);
+	size = get_unaligned_be32(buf + 2);
 
 	if (count < size)
 		return -EIO;
drivers/char/tpm/tpm_tis_core.c +2 -2
index 7993678954a2..5323c54dc917 100644
@@ -222,7 +222,7 @@ static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		goto out;
 	}
 
-	expected = be32_to_cpu(*(__be32 *) (buf + 2));
+	expected = get_unaligned_be32(buf + 2);
 	if (expected > count) {
 		size = -EIO;
 		goto out;
@@ -371,7 +371,7 @@ static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len)
 		goto out_err;
 
 	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
-		ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+		ordinal = get_unaligned_be32(buf + 6);
 
 		if (chip->flags & TPM_CHIP_FLAG_TPM2)
 			dur = tpm2_calc_ordinal_duration(chip, ordinal);
Signature · 1 line
2.7.4
#1 lore raw
Quoted 5 lines · Tomas Winkler

Use get_unaligned_be32 as b32_to_cpu doesn't work correctly on all platforms for unaligned access.

The fix doesn't cover all the cases as also some cast structures have members on unaligned addresses.

I think this is a good idea..

drivers/char/tpm/tpm-interface.c ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz, · line 357

Quoted 8 lines
drivers/char/tpm/tpm-interface.c +2 -2
@@ -353,8 +353,8 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
 	if (bufsiz > TPM_BUFSIZE)
 		bufsiz = TPM_BUFSIZE;
 
-	count = be32_to_cpu(*((__be32 *) (buf + 2)));
-	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+	count = get_unaligned_be32(buf + 2);
+	ordinal = get_unaligned_be32(buf + 6);

But lets fix this better and get rid of the constants too...

const tpm_input_header *hdr = buf;
count = be32_to_cpu(hdr->length);
ordinal = be32_to_cpu(hdr->ordinal);

Compiler will take care of unaligned for __packed.

drivers/char/tpm/tpm_crb.c static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count) · line 181

Quoted 6 lines
drivers/char/tpm/tpm_crb.c +1 -1
@@ -178,7 +178,7 @@ static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		return -EIO;
 
 	memcpy_fromio(buf, priv->rsp, 6);
-	expected = be32_to_cpup((__be32 *) &buf[2]);
+	expected = get_unaligned_be32(buf + 2);

Here too, except tpm_output_header (and is tpm1 and 2 the same here?)

drivers/char/tpm/tpm_i2c_infineon.c static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) · line 455

Quoted 6 lines
drivers/char/tpm/tpm_i2c_infineon.c +1 -1
@@ -451,7 +452,7 @@ static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		goto out;
 	}
 
-	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	expected = get_unaligned_be32(buf + 2);

Ditto

drivers/char/tpm/tpm_i2c_nuvoton.c static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count) · line 318

Quoted 6 lines
drivers/char/tpm/tpm_i2c_nuvoton.c +1 -1
@@ -314,7 +315,7 @@ static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		 * convert number of expected bytes field from big endian 32 bit
 		 * to machine native
 		 */
-		expected = be32_to_cpu(*(__be32 *) (buf + 2));
+		expected = get_unaligned_be32(buf + 2);

Ditto

drivers/char/tpm/tpm_i2c_nuvoton.c static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len) · line 446

Quoted 9 lines
drivers/char/tpm/tpm_i2c_nuvoton.c +1 -1
 		if (expected > count) {
 			dev_err(dev, "%s() expected > count\n", __func__);
 			size = -EIO;
@@ -442,7 +443,7 @@ static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
 		i2c_nuvoton_ready(chip);
 		return rc;
 	}
-	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+	ordinal = get_unaligned_be32(buf + 6);

Ditto

drivers/char/tpm/tpm_nsc.c static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count) · line 176

Quoted 7 lines
drivers/char/tpm/tpm_nsc.c +1 -2
@@ -174,8 +173,7 @@ static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 		return -EIO;
 	}
 
-	native_size = (__force __be32 *) (buf + 2);
-	size = be32_to_cpu(*native_size);
+	size = get_unaligned_be32(buf + 2);

Ditto

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count) · line 225

Quoted 9 lines
drivers/char/tpm/tpm_tis_core.c +2 -1
index 7993678954a2..5323c54dc917 100644
+++ b/drivers/char/tpm/tpm_tis_core.c
@@ -222,7 +222,7 @@ static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		goto out;
 	}
 
-	expected = be32_to_cpu(*(__be32 *) (buf + 2));
+	expected = get_unaligned_be32(buf + 2);

Ditto

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len) · line 374

Quoted 6 lines
drivers/char/tpm/tpm_tis_core.c +1 -1
@@ -371,7 +371,7 @@ static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len)
 		goto out_err;
 
 	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
-		ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+		ordinal = get_unaligned_be32(buf + 6);

Ditto

Jason

------------------------------------------------------------------------------

#2 lore raw
Quoted 21 lines · Tomas Winkler
Quoted 5 lines

Use get_unaligned_be32 as b32_to_cpu doesn't work correctly on all platforms for unaligned access.

The fix doesn't cover all the cases as also some cast structures have members on unaligned addresses.

I think this is a good idea..

Review ssize_t tpm_transmit(struct tpm_chip *chip, const u8

Quoted 1 line
(quoted diff) +0 -0
@@ -353,8 +353,8 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const u8

*buf, size_t bufsiz,

drivers/char/tpm/tpm-interface.c ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz, · line 357

Quoted 7 lines
drivers/char/tpm/tpm-interface.c +2 -2
 	if (bufsiz > TPM_BUFSIZE)
 		bufsiz = TPM_BUFSIZE;
 
-	count = be32_to_cpu(*((__be32 *) (buf + 2)));
-	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+	count = get_unaligned_be32(buf + 2);
+	ordinal = get_unaligned_be32(buf + 6);

But lets fix this better and get rid of the constants too...

Quoted 5 lines

const tpm_input_header *hdr = buf;
count = be32_to_cpu(hdr->length);
ordinal = be32_to_cpu(hdr->ordinal);

Compiler will take care of unaligned for __packed.

Yes, compiler takes care at performance penalty but probably we don't care about that much,
and readability is maybe more important here. What I've done is pretty much mechanical fix,
I agree this is better approach in this particular case.

Quoted 73 lines

Review static int crb_recv(struct tpm_chip *chip, u8 *buf,

Quoted 1 line
(quoted diff) +0 -0
@@ -178,7 +178,7 @@ static int crb_recv(struct tpm_chip *chip, u8 *buf,

size_t count)

drivers/char/tpm/tpm_crb.c static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count) · line 181

Quoted 5 lines
drivers/char/tpm/tpm_crb.c +1 -1
 		return -EIO;
 
 	memcpy_fromio(buf, priv->rsp, 6);
-	expected = be32_to_cpup((__be32 *) &buf[2]);
+	expected = get_unaligned_be32(buf + 2);

Here too, except tpm_output_header (and is tpm1 and 2 the same here?)

Review static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8

Quoted 1 line
(quoted diff) +0 -0
@@ -451,7 +452,7 @@ static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8

*buf, size_t count)

drivers/char/tpm/tpm_i2c_infineon.c static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) · line 455

Quoted 5 lines
drivers/char/tpm/tpm_i2c_infineon.c +1 -1
 		goto out;
 	}
 
-	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	expected = get_unaligned_be32(buf + 2);

Ditto

Review static int i2c_nuvoton_recv(struct tpm_chip *chip, u8

Quoted 1 line
(quoted diff) +0 -0
@@ -314,7 +315,7 @@ static int i2c_nuvoton_recv(struct tpm_chip *chip, u8

*buf, size_t count)

Review static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 · line 315

Quoted 1 line
(quoted diff) +0 -0
 		 * convert number of expected bytes field from big endian 32

bit

drivers/char/tpm/tpm_i2c_nuvoton.c static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count) · line 318

Quoted 4 lines
drivers/char/tpm/tpm_i2c_nuvoton.c +1 -1
 		 * to machine native
 		 */
-		expected = be32_to_cpu(*(__be32 *) (buf + 2));
+		expected = get_unaligned_be32(buf + 2);

Ditto

drivers/char/tpm/tpm_i2c_nuvoton.c static int i2c_nuvoton_send(struct tpm_chip *chip, u8

Quoted 4 lines
drivers/char/tpm/tpm_i2c_nuvoton.c +0 -0
 		if (expected > count) {
 			dev_err(dev, "%s() expected > count\n", __func__);
 			size = -EIO;
@@ -442,7 +443,7 @@ static int i2c_nuvoton_send(struct tpm_chip *chip, u8

*buf, size_t len)

drivers/char/tpm/tpm_i2c_nuvoton.c static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len) · line 446

Quoted 5 lines
drivers/char/tpm/tpm_i2c_nuvoton.c +1 -1
 		i2c_nuvoton_ready(chip);
 		return rc;
 	}
-	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+	ordinal = get_unaligned_be32(buf + 6);

Ditto

Review static int tpm_nsc_recv(struct tpm_chip *chip, u8 *

Quoted 1 line
(quoted diff) +0 -0
@@ -174,8 +173,7 @@ static int tpm_nsc_recv(struct tpm_chip *chip, u8 *

buf, size_t count)

drivers/char/tpm/tpm_nsc.c static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count) · line 176

Quoted 6 lines
drivers/char/tpm/tpm_nsc.c +1 -2
 		return -EIO;
 	}
 
-	native_size = (__force __be32 *) (buf + 2);
-	size = be32_to_cpu(*native_size);
+	size = get_unaligned_be32(buf + 2);

Ditto

Review static int tpm_tis_recv(struct tpm_chip *chip, u8

Quoted 5 lines
(quoted diff) +0 -0

b/drivers/char/tpm/tpm_tis_core.c index 7993678954a2..5323c54dc917 100644
+++ b/drivers/char/tpm/tpm_tis_core.c

(quoted diff) +0 -0
@@ -222,7 +222,7 @@ static int tpm_tis_recv(struct tpm_chip *chip, u8

*buf, size_t count)

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count) · line 225

Quoted 5 lines
drivers/char/tpm/tpm_tis_core.c +1 -1
 		goto out;
 	}
 
-	expected = be32_to_cpu(*(__be32 *) (buf + 2));
+	expected = get_unaligned_be32(buf + 2);

Ditto

Review static int tpm_tis_send_main(struct tpm_chip *chip,

Quoted 1 line
(quoted diff) +0 -0
@@ -371,7 +371,7 @@ static int tpm_tis_send_main(struct tpm_chip *chip,

u8 *buf, size_t len)

Quoted 2 lines
drivers/char/tpm/tpm_tis_core.c +0 -0
 		goto out_err;
 
Quoted 7 lines

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len) · line 374

Quoted 3 lines
drivers/char/tpm/tpm_tis_core.c +1 -1
 	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
-		ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+		ordinal = get_unaligned_be32(buf + 6);

Ditto

Jason

------------------------------------------------------------------------------

#3 lore raw
Quoted 21 lines · Tomas
Quoted 20 lines · Tomas Winkler
Quoted 5 lines

Use get_unaligned_be32 as b32_to_cpu doesn't work correctly on all platforms for unaligned access.

The fix doesn't cover all the cases as also some cast structures have members on unaligned addresses.

I think this is a good idea..

Review ssize_t tpm_transmit(struct tpm_chip *chip, const u8

Quoted 1 line
(quoted diff) +0 -0
@@ -353,8 +353,8 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const u8

*buf, size_t bufsiz,

drivers/char/tpm/tpm-interface.c ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz, · line 357

Quoted 7 lines
drivers/char/tpm/tpm-interface.c +2 -2
 	if (bufsiz > TPM_BUFSIZE)
 		bufsiz = TPM_BUFSIZE;
 
-	count = be32_to_cpu(*((__be32 *) (buf + 2)));
-	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+	count = get_unaligned_be32(buf + 2);
+	ordinal = get_unaligned_be32(buf + 6);

But lets fix this better and get rid of the constants too...

Quoted 8 lines
Quoted 5 lines

const tpm_input_header *hdr = buf;
count = be32_to_cpu(hdr->length);
ordinal = be32_to_cpu(hdr->ordinal);

Compiler will take care of unaligned for __packed.

Yes, compiler takes care at performance penalty but probably we don't care about that much,

Hmm? get_unaligned_be32 boils down to the same __packed construct.

As is today we must be hitting the in-kernel unaligned access trap (eg on ARM) which is *very* expensive so this is a very worthwhile fix ...

Jason

------------------------------------------------------------------------------

#4 lore raw
In reply to Tomas Winkler
Quoted 7 lines · Tomas Winkler

Use get_unaligned_be32 as b32_to_cpu doesn't work correctly on all platforms for unaligned access.

The fix doesn't cover all the cases as also some cast structures have members on unaligned addresses.

This looks good to me with the exceptio that was pointed out by Jason. CC the next version to the linux-security-module. I talked with James about this at LPC and since this list does not have too many active reviewers it makes sense to cycle all the non-trivial changes through that list.

/Jarkko

Quoted 175 lines
---
 drivers/char/tpm/tpm-interface.c    | 4 ++--
 drivers/char/tpm/tpm-sysfs.c        | 2 +-
 drivers/char/tpm/tpm.h              | 1 +
 drivers/char/tpm/tpm_crb.c          | 4 ++--
 drivers/char/tpm/tpm_i2c_infineon.c | 5 +++--
 drivers/char/tpm/tpm_i2c_nuvoton.c  | 7 ++++---
 drivers/char/tpm/tpm_nsc.c          | 4 +---
 drivers/char/tpm/tpm_tis_core.c     | 4 ++--
 8 files changed, 16 insertions(+), 15 deletions(-)
drivers/char/tpm/tpm-interface.c +2 -2
index 769d8b0d31a3..4cf38d00d1b3 100644
@@ -353,8 +353,8 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
 	if (bufsiz > TPM_BUFSIZE)
 		bufsiz = TPM_BUFSIZE;
 
-	count = be32_to_cpu(*((__be32 *) (buf + 2)));
-	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+	count = get_unaligned_be32(buf + 2);
+	ordinal = get_unaligned_be32(buf + 6);
 	if (count == 0)
 		return -ENODATA;
 	if (count > bufsiz) {
drivers/char/tpm/tpm-sysfs.c +1 -1
index 848ad6580b46..2435d710b6af 100644
@@ -71,7 +71,7 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
 		    data[12], data[13], data[14], data[15],
 		    data[16], data[17], data[18], data[19],
 		    data[20], data[21], data[22], data[23],
-		    be32_to_cpu(*((__be32 *) (data + 24))));
+		    be32_to_cpup((__be32 *)(data + 24)));
 
 	for (i = 0; i < 256; i++) {
 		str += sprintf(str, "%02X ", data[i + 28]);
drivers/char/tpm/tpm.h +1 -0
index 1ae976894257..83dba0ff5ea0 100644
@@ -23,6 +23,7 @@
 #ifndef __TPM_H__
 #define __TPM_H__
 
+#include <asm/unaligned.h>
 #include <linux/module.h>
 #include <linux/delay.h>
 #include <linux/fs.h>
drivers/char/tpm/tpm_crb.c +2 -2
index 65040d74bb02..8067cfbfdbe2 100644
@@ -168,7 +168,7 @@ static u8 crb_status(struct tpm_chip *chip)
 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 {
 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
-	unsigned int expected;
+	u32 expected;
 
 	/* sanity check */
 	if (count < 6)
@@ -178,7 +178,7 @@ static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		return -EIO;
 
 	memcpy_fromio(buf, priv->rsp, 6);
-	expected = be32_to_cpup((__be32 *) &buf[2]);
+	expected = get_unaligned_be32(buf + 2);
 
 	if (expected > count)
 		return -EIO;
drivers/char/tpm/tpm_i2c_infineon.c +3 -2
index 62ee44e57ddc..2dd4e3bb14c7 100644
@@ -437,7 +437,8 @@ static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 {
 	int size = 0;
-	int expected, status;
+	u32 expected;
+	int status;
 
 	if (count < TPM_HEADER_SIZE) {
 		size = -EIO;
@@ -451,7 +452,7 @@ static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		goto out;
 	}
 
-	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	expected = get_unaligned_be32(buf + 2);
 	if ((size_t) expected > count) {
 		size = -EIO;
 		goto out;
drivers/char/tpm/tpm_i2c_nuvoton.c +4 -3
index e3a9155ee671..7ba9c435da4e 100644
@@ -273,7 +273,8 @@ static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 	struct device *dev = chip->dev.parent;
 	struct i2c_client *client = to_i2c_client(dev);
 	s32 rc;
-	int expected, status, burst_count, retries, size = 0;
+	int status, burst_count, retries, size = 0;
+	u32 expected;
 
 	if (count < TPM_HEADER_SIZE) {
 		i2c_nuvoton_ready(chip);    /* return to idle */
@@ -314,7 +315,7 @@ static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		 * convert number of expected bytes field from big endian 32 bit
 		 * to machine native
 		 */
-		expected = be32_to_cpu(*(__be32 *) (buf + 2));
+		expected = get_unaligned_be32(buf + 2);
 		if (expected > count) {
 			dev_err(dev, "%s() expected > count\n", __func__);
 			size = -EIO;
@@ -442,7 +443,7 @@ static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
 		i2c_nuvoton_ready(chip);
 		return rc;
 	}
-	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+	ordinal = get_unaligned_be32(buf + 6);
 	rc = i2c_nuvoton_wait_for_data_avail(chip,
 					     tpm_calc_ordinal_duration(chip,
 								       ordinal),
drivers/char/tpm/tpm_nsc.c +1 -3
index 9ff0e072c476..99a8ff6ea37d 100644
@@ -131,7 +131,6 @@ static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 	u8 *buffer = buf;
 	u8 data, *p;
 	u32 size;
-	__be32 *native_size;
 
 	if (count < 6)
 		return -EIO;
@@ -174,8 +173,7 @@ static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 		return -EIO;
 	}
 
-	native_size = (__force __be32 *) (buf + 2);
-	size = be32_to_cpu(*native_size);
+	size = get_unaligned_be32(buf + 2);
 
 	if (count < size)
 		return -EIO;
drivers/char/tpm/tpm_tis_core.c +2 -2
index 7993678954a2..5323c54dc917 100644
@@ -222,7 +222,7 @@ static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		goto out;
 	}
 
-	expected = be32_to_cpu(*(__be32 *) (buf + 2));
+	expected = get_unaligned_be32(buf + 2);
 	if (expected > count) {
 		size = -EIO;
 		goto out;
@@ -371,7 +371,7 @@ static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len)
 		goto out_err;
 
 	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
-		ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+		ordinal = get_unaligned_be32(buf + 6);
 
 		if (chip->flags & TPM_CHIP_FLAG_TPM2)
 			dur = tpm2_calc_ordinal_duration(chip, ordinal);
Signature · 1 line
2.7.4
#5 lore raw

Hey,

Quoted 7 lines · Jarkko Sakkinen
Quoted 6 lines · Tomas Winkler

Use get_unaligned_be32 as b32_to_cpu doesn't work correctly on all platforms for unaligned access.

The fix doesn't cover all the cases as also some cast structures have members on unaligned addresses.

Quoted 7 lines
Quoted 1 line

This looks good to me with the exceptio that was pointed out by Jason. CC the next version to the linux-security-module. I talked with James about this at LPC and since this list does not have too many active reviewers it makes sense to cycle all the non-trivial changes through that list.

Are you planning to send an updated version of this. Should be a no-brainer to apply.

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot