Skip to contentSkip to conversation

tpm: fix type issues in tpm_getcap()

PATCH In review Jarkko Sakkinen 2 participants 10 messages Feb 1, 2017 – Feb 3, 2017 TPM device driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: fix type issues in tpm_getcap()

There are two type issues associated with tpm_getcap().

You must not do arithmetic with __be32 or __le32 types because sometimes it results incorrect results. Calculations must be done only with data that is in CPU byte order. This commit migrates tpm_getcap() to struct tpm_buf in order to sort out these issues.

The second issue is with struct cap_t as the size of the type bool is assumed to be one byte. This commit sorts out the issue by changing the type to u8.

---
v2:
- Use struct tpm_buf.
- Merge the type change of 'owned' to this patch.

 drivers/char/tpm/tpm-interface.c | 33 ++++++++++++++++++---------------
 drivers/char/tpm/tpm.h           | 15 +--------------
 2 files changed, 19 insertions(+), 29 deletions(-)
drivers/char/tpm/tpm-interface.c +18 -15
index 423938e..7af1e8c 100644
@@ -480,31 +480,34 @@ static const struct tpm_input_header tpm_getcap_header = {
 ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
 		   const char *desc, size_t min_cap_length)
 {
-	struct tpm_cmd_t tpm_cmd;
+	struct tpm_buf buf;
 	int rc;
 
-	tpm_cmd.header.in = tpm_getcap_header;
+	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
+	if (rc)
+		return rc;
+
 	if (subcap_id == TPM_CAP_VERSION_1_1 ||
 	    subcap_id == TPM_CAP_VERSION_1_2) {
-		tpm_cmd.params.getcap_in.cap = cpu_to_be32(subcap_id);
-		/*subcap field not necessary */
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
-		tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
+		tpm_buf_append_u32(&buf, subcap_id);
+		tpm_buf_append_u32(&buf, 0);
 	} else {
 		if (subcap_id == TPM_CAP_FLAG_PERM ||
 		    subcap_id == TPM_CAP_FLAG_VOL)
-			tpm_cmd.params.getcap_in.cap =
-				cpu_to_be32(TPM_CAP_FLAG);
+			tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
 		else
-			tpm_cmd.params.getcap_in.cap =
-				cpu_to_be32(TPM_CAP_PROP);
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
-		tpm_cmd.params.getcap_in.subcap = cpu_to_be32(subcap_id);
+			tpm_buf_append_u32(&buf, TPM_CAP_PROP);
+
+		tpm_buf_append_u32(&buf, 4);
+		tpm_buf_append_u32(&buf, subcap_id);
 	}
-	rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
-			      min_cap_length, 0, desc);
+
+	rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, min_cap_length, 0,
+			      desc);
 	if (!rc)
-		*cap = tpm_cmd.params.getcap_out.cap;
+		*cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
+
+	tpm_buf_destroy(&buf);
 	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_getcap);
drivers/char/tpm/tpm.h +1 -14
index bff37be..709e7d4 100644
@@ -281,7 +281,7 @@ struct permanent_flags_t {
 typedef union {
 	struct	permanent_flags_t perm_flags;
 	struct	stclear_flags_t	stclear_flags;
-	bool	owned;
+	u8	owned;
 	__be32	num_pcrs;
 	struct	tpm_version_t	tpm_version;
 	struct	tpm_version_1_2_t tpm_version_1_2;
@@ -307,17 +307,6 @@ enum tpm_sub_capabilities {
 	TPM_CAP_PROP_TIS_DURATION = 0x120,
 };
 
-struct	tpm_getcap_params_in {
-	__be32	cap;
-	__be32	subcap_size;
-	__be32	subcap;
-} __packed;
-
-struct	tpm_getcap_params_out {
-	__be32	cap_size;
-	cap_t	cap;
-} __packed;
-
 struct	tpm_readpubek_params_out {
 	u8	algorithm[4];
 	u8	encscheme[2];
@@ -367,10 +356,8 @@ struct tpm_startup_in {
 } __packed;
 
 typedef union {
-	struct	tpm_getcap_params_out getcap_out;
 	struct	tpm_readpubek_params_out readpubek_out;
 	u8	readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
-	struct	tpm_getcap_params_in getcap_in;
 	struct	tpm_pcrread_in	pcrread_in;
 	struct	tpm_pcrread_out	pcrread_out;
 	struct	tpm_pcrextend_in pcrextend_in;
Signature · 6 lines
2.9.3


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
#1 lore raw
Quoted 7 lines · Jarkko Sakkinen

There are two type issues associated with tpm_getcap().

You must not do arithmetic with __be32 or __le32 types because sometimes it results incorrect results. Calculations must be done only with data that is in CPU byte order. This commit migrates tpm_getcap() to struct tpm_buf in order to sort out these issues.

Quoted 5 lines

The second issue is with struct cap_t as the size of the type bool is assumed to be one byte. This commit sorts out the issue by changing the type to u8.

This is the only mandatory fix for the next pull request and 4.11.

/Jarkko

Quoted 107 lines

---
v2:
- Use struct tpm_buf.
- Merge the type change of 'owned' to this patch.

 drivers/char/tpm/tpm-interface.c | 33 ++++++++++++++++++---------------
 drivers/char/tpm/tpm.h           | 15 +--------------
 2 files changed, 19 insertions(+), 29 deletions(-)
drivers/char/tpm/tpm-interface.c +18 -15
index 423938e..7af1e8c 100644
@@ -480,31 +480,34 @@ static const struct tpm_input_header tpm_getcap_header = {
 ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
 		   const char *desc, size_t min_cap_length)
 {
-	struct tpm_cmd_t tpm_cmd;
+	struct tpm_buf buf;
 	int rc;
 
-	tpm_cmd.header.in = tpm_getcap_header;
+	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
+	if (rc)
+		return rc;
+
 	if (subcap_id == TPM_CAP_VERSION_1_1 ||
 	    subcap_id == TPM_CAP_VERSION_1_2) {
-		tpm_cmd.params.getcap_in.cap = cpu_to_be32(subcap_id);
-		/*subcap field not necessary */
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
-		tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
+		tpm_buf_append_u32(&buf, subcap_id);
+		tpm_buf_append_u32(&buf, 0);
 	} else {
 		if (subcap_id == TPM_CAP_FLAG_PERM ||
 		    subcap_id == TPM_CAP_FLAG_VOL)
-			tpm_cmd.params.getcap_in.cap =
-				cpu_to_be32(TPM_CAP_FLAG);
+			tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
 		else
-			tpm_cmd.params.getcap_in.cap =
-				cpu_to_be32(TPM_CAP_PROP);
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
-		tpm_cmd.params.getcap_in.subcap = cpu_to_be32(subcap_id);
+			tpm_buf_append_u32(&buf, TPM_CAP_PROP);
+
+		tpm_buf_append_u32(&buf, 4);
+		tpm_buf_append_u32(&buf, subcap_id);
 	}
-	rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
-			      min_cap_length, 0, desc);
+
+	rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, min_cap_length, 0,
+			      desc);
 	if (!rc)
-		*cap = tpm_cmd.params.getcap_out.cap;
+		*cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
+
+	tpm_buf_destroy(&buf);
 	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_getcap);
drivers/char/tpm/tpm.h +1 -14
index bff37be..709e7d4 100644
@@ -281,7 +281,7 @@ struct permanent_flags_t {
 typedef union {
 	struct	permanent_flags_t perm_flags;
 	struct	stclear_flags_t	stclear_flags;
-	bool	owned;
+	u8	owned;
 	__be32	num_pcrs;
 	struct	tpm_version_t	tpm_version;
 	struct	tpm_version_1_2_t tpm_version_1_2;
@@ -307,17 +307,6 @@ enum tpm_sub_capabilities {
 	TPM_CAP_PROP_TIS_DURATION = 0x120,
 };
 
-struct	tpm_getcap_params_in {
-	__be32	cap;
-	__be32	subcap_size;
-	__be32	subcap;
-} __packed;
-
-struct	tpm_getcap_params_out {
-	__be32	cap_size;
-	cap_t	cap;
-} __packed;
-
 struct	tpm_readpubek_params_out {
 	u8	algorithm[4];
 	u8	encscheme[2];
@@ -367,10 +356,8 @@ struct tpm_startup_in {
 } __packed;
 
 typedef union {
-	struct	tpm_getcap_params_out getcap_out;
 	struct	tpm_readpubek_params_out readpubek_out;
 	u8	readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
-	struct	tpm_getcap_params_in getcap_in;
 	struct	tpm_pcrread_in	pcrread_in;
 	struct	tpm_pcrread_out	pcrread_out;
 	struct	tpm_pcrextend_in pcrextend_in;
Signature · 1 line
2.9.3
#2 lore raw
Quoted 8 lines · Jarkko Sakkinen
Quoted 7 lines · Jarkko Sakkinen

There are two type issues associated with tpm_getcap().

You must not do arithmetic with __be32 or __le32 types because sometimes it results incorrect results. Calculations must be done only with data that is in CPU byte order. This commit migrates tpm_getcap() to struct tpm_buf in order to sort out these issues.

Quoted 7 lines
Quoted 5 lines

The second issue is with struct cap_t as the size of the type bool is assumed to be one byte. This commit sorts out the issue by changing the type to u8.

This is the only mandatory fix for the next pull request and 4.11.

I mean from the reported sparse errors.

/Jarkko

#3 lore raw
In reply to Jarkko Sakkinen
Quoted 25 lines · Jarkko Sakkinen

There are two type issues associated with tpm_getcap().

You must not do arithmetic with __be32 or __le32 types because sometimes it results incorrect results. Calculations must be done only with data that is in CPU byte order. This commit migrates tpm_getcap() to struct tpm_buf in order to sort out these issues.

The second issue is with struct cap_t as the size of the type bool is assumed to be one byte. This commit sorts out the issue by changing the type to u8.

---
v2:
- Use struct tpm_buf.
- Merge the type change of 'owned' to this patch.

 drivers/char/tpm/tpm-interface.c | 33 ++++++++++++++++++---------------
 drivers/char/tpm/tpm.h           | 15 +--------------
 2 files changed, 19 insertions(+), 29 deletions(-)
drivers/char/tpm/tpm-interface.c +0 -0
index 423938e..7af1e8c 100644

drivers/char/tpm/tpm-interface.c static const struct tpm_input_header tpm_getcap_header = { · line 481

Quoted 2 lines
drivers/char/tpm/tpm-interface.c +0 -0
@@ -480,31 +480,34 @@ static const struct tpm_input_header tpm_getcap_header = {
 ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
 		   const char *desc, size_t min_cap_length)

Last two parameters are in the wrong order (different than tpm_transmit_cmd).

/Jarkko

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

#4 lore raw
In reply to Jarkko Sakkinen
Quoted 7 lines · Jarkko Sakkinen

There are two type issues associated with tpm_getcap().

You must not do arithmetic with __be32 or __le32 types because sometimes it results incorrect results. Calculations must be done only with data that is in CPU byte order. This commit migrates tpm_getcap() to struct tpm_buf in order to sort out these issues.

Quoted 5 lines

The second issue is with struct cap_t as the size of the type bool is assumed to be one byte. This commit sorts out the issue by changing the type to u8.

Wwould it be possible for anyone to peer test this so I could move forward creating the pull request?

/Jarkko

Quoted 107 lines

---
v2:
- Use struct tpm_buf.
- Merge the type change of 'owned' to this patch.

 drivers/char/tpm/tpm-interface.c | 33 ++++++++++++++++++---------------
 drivers/char/tpm/tpm.h           | 15 +--------------
 2 files changed, 19 insertions(+), 29 deletions(-)
drivers/char/tpm/tpm-interface.c +18 -15
index 423938e..7af1e8c 100644
@@ -480,31 +480,34 @@ static const struct tpm_input_header tpm_getcap_header = {
 ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
 		   const char *desc, size_t min_cap_length)
 {
-	struct tpm_cmd_t tpm_cmd;
+	struct tpm_buf buf;
 	int rc;
 
-	tpm_cmd.header.in = tpm_getcap_header;
+	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
+	if (rc)
+		return rc;
+
 	if (subcap_id == TPM_CAP_VERSION_1_1 ||
 	    subcap_id == TPM_CAP_VERSION_1_2) {
-		tpm_cmd.params.getcap_in.cap = cpu_to_be32(subcap_id);
-		/*subcap field not necessary */
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
-		tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
+		tpm_buf_append_u32(&buf, subcap_id);
+		tpm_buf_append_u32(&buf, 0);
 	} else {
 		if (subcap_id == TPM_CAP_FLAG_PERM ||
 		    subcap_id == TPM_CAP_FLAG_VOL)
-			tpm_cmd.params.getcap_in.cap =
-				cpu_to_be32(TPM_CAP_FLAG);
+			tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
 		else
-			tpm_cmd.params.getcap_in.cap =
-				cpu_to_be32(TPM_CAP_PROP);
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
-		tpm_cmd.params.getcap_in.subcap = cpu_to_be32(subcap_id);
+			tpm_buf_append_u32(&buf, TPM_CAP_PROP);
+
+		tpm_buf_append_u32(&buf, 4);
+		tpm_buf_append_u32(&buf, subcap_id);
 	}
-	rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
-			      min_cap_length, 0, desc);
+
+	rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, min_cap_length, 0,
+			      desc);
 	if (!rc)
-		*cap = tpm_cmd.params.getcap_out.cap;
+		*cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
+
+	tpm_buf_destroy(&buf);
 	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_getcap);
drivers/char/tpm/tpm.h +1 -14
index bff37be..709e7d4 100644
@@ -281,7 +281,7 @@ struct permanent_flags_t {
 typedef union {
 	struct	permanent_flags_t perm_flags;
 	struct	stclear_flags_t	stclear_flags;
-	bool	owned;
+	u8	owned;
 	__be32	num_pcrs;
 	struct	tpm_version_t	tpm_version;
 	struct	tpm_version_1_2_t tpm_version_1_2;
@@ -307,17 +307,6 @@ enum tpm_sub_capabilities {
 	TPM_CAP_PROP_TIS_DURATION = 0x120,
 };
 
-struct	tpm_getcap_params_in {
-	__be32	cap;
-	__be32	subcap_size;
-	__be32	subcap;
-} __packed;
-
-struct	tpm_getcap_params_out {
-	__be32	cap_size;
-	cap_t	cap;
-} __packed;
-
 struct	tpm_readpubek_params_out {
 	u8	algorithm[4];
 	u8	encscheme[2];
@@ -367,10 +356,8 @@ struct tpm_startup_in {
 } __packed;
 
 typedef union {
-	struct	tpm_getcap_params_out getcap_out;
 	struct	tpm_readpubek_params_out readpubek_out;
 	u8	readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
-	struct	tpm_getcap_params_in getcap_in;
 	struct	tpm_pcrread_in	pcrread_in;
 	struct	tpm_pcrread_out	pcrread_out;
 	struct	tpm_pcrextend_in pcrextend_in;
Signature · 1 line
2.9.3
#5 lore raw
In reply to Jarkko Sakkinen

drivers/char/tpm/tpm-interface.c static const struct tpm_input_header tpm_getcap_header = {

Quoted 25 lines · Jarkko Sakkinen

There are two type issues associated with tpm_getcap().

You must not do arithmetic with __be32 or __le32 types because sometimes it results incorrect results. Calculations must be done only with data that is in CPU byte order. This commit migrates tpm_getcap() to struct tpm_buf in order to sort out these issues.

The second issue is with struct cap_t as the size of the type bool is assumed to be one byte. This commit sorts out the issue by changing the type to u8.

---
v2:
- Use struct tpm_buf.
- Merge the type change of 'owned' to this patch.

  drivers/char/tpm/tpm-interface.c | 33 ++++++++++++++++++---------------
  drivers/char/tpm/tpm.h           | 15 +--------------
  2 files changed, 19 insertions(+), 29 deletions(-)
drivers/char/tpm/tpm-interface.c +0 -0
index 423938e..7af1e8c 100644
@@ -480,31 +480,34 @@ static const struct tpm_input_header tpm_getcap_header = {

Is tpm_getcap_header still needed ?

Thanks & Regards,
- Nayna

Quoted 92 lines
drivers/char/tpm/tpm-interface.c +18 -15
  ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
  		   const char *desc, size_t min_cap_length)
  {
-	struct tpm_cmd_t tpm_cmd;
+	struct tpm_buf buf;
  	int rc;
 
-	tpm_cmd.header.in = tpm_getcap_header;
+	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
+	if (rc)
+		return rc;
+
  	if (subcap_id == TPM_CAP_VERSION_1_1 ||
  	    subcap_id == TPM_CAP_VERSION_1_2) {
-		tpm_cmd.params.getcap_in.cap = cpu_to_be32(subcap_id);
-		/*subcap field not necessary */
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
-		tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
+		tpm_buf_append_u32(&buf, subcap_id);
+		tpm_buf_append_u32(&buf, 0);
  	} else {
  		if (subcap_id == TPM_CAP_FLAG_PERM ||
  		    subcap_id == TPM_CAP_FLAG_VOL)
-			tpm_cmd.params.getcap_in.cap =
-				cpu_to_be32(TPM_CAP_FLAG);
+			tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
  		else
-			tpm_cmd.params.getcap_in.cap =
-				cpu_to_be32(TPM_CAP_PROP);
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
-		tpm_cmd.params.getcap_in.subcap = cpu_to_be32(subcap_id);
+			tpm_buf_append_u32(&buf, TPM_CAP_PROP);
+
+		tpm_buf_append_u32(&buf, 4);
+		tpm_buf_append_u32(&buf, subcap_id);
  	}
-	rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
-			      min_cap_length, 0, desc);
+
+	rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, min_cap_length, 0,
+			      desc);
  	if (!rc)
-		*cap = tpm_cmd.params.getcap_out.cap;
+		*cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
+
+	tpm_buf_destroy(&buf);
  	return rc;
  }
  EXPORT_SYMBOL_GPL(tpm_getcap);
drivers/char/tpm/tpm.h +1 -14
index bff37be..709e7d4 100644
@@ -281,7 +281,7 @@ struct permanent_flags_t {
  typedef union {
  	struct	permanent_flags_t perm_flags;
  	struct	stclear_flags_t	stclear_flags;
-	bool	owned;
+	u8	owned;
  	__be32	num_pcrs;
  	struct	tpm_version_t	tpm_version;
  	struct	tpm_version_1_2_t tpm_version_1_2;
@@ -307,17 +307,6 @@ enum tpm_sub_capabilities {
  	TPM_CAP_PROP_TIS_DURATION = 0x120,
  };
 
-struct	tpm_getcap_params_in {
-	__be32	cap;
-	__be32	subcap_size;
-	__be32	subcap;
-} __packed;
-
-struct	tpm_getcap_params_out {
-	__be32	cap_size;
-	cap_t	cap;
-} __packed;
-
  struct	tpm_readpubek_params_out {
  	u8	algorithm[4];
  	u8	encscheme[2];
@@ -367,10 +356,8 @@ struct tpm_startup_in {
  } __packed;
 
  typedef union {
-	struct	tpm_getcap_params_out getcap_out;
  	struct	tpm_readpubek_params_out readpubek_out;
  	u8	readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
-	struct	tpm_getcap_params_in getcap_in;
  	struct	tpm_pcrread_in	pcrread_in;
  	struct	tpm_pcrread_out	pcrread_out;
  	struct	tpm_pcrextend_in pcrextend_in;
#6 lore raw
Quoted 23 lines · Nayna
Quoted 20 lines · Jarkko Sakkinen

There are two type issues associated with tpm_getcap().

You must not do arithmetic with __be32 or __le32 types because sometimes it results incorrect results. Calculations must be done only with data that is in CPU byte order. This commit migrates tpm_getcap() to struct tpm_buf in order to sort out these issues.

The second issue is with struct cap_t as the size of the type bool is assumed to be one byte. This commit sorts out the issue by changing the type to u8.

---
v2:
- Use struct tpm_buf.
- Merge the type change of 'owned' to this patch.

  drivers/char/tpm/tpm-interface.c | 33 ++++++++++++++++++---------------
  drivers/char/tpm/tpm.h           | 15 +--------------
  2 files changed, 19 insertions(+), 29 deletions(-)
Quoted 7 lines

drivers/char/tpm/tpm-interface.c static const struct tpm_input_header tpm_getcap_header = {

Quoted 5 lines
drivers/char/tpm/tpm-interface.c +0 -0
index 423938e..7af1e8c 100644
@@ -480,31 +480,34 @@ static const struct tpm_input_header tpm_getcap_header = {

Is tpm_getcap_header still needed ?

Definitely not, thanks. Are you able to try this out so I could move forward with pull request?

/Jarkko

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

#7 lore raw
Quoted 32 lines · Jarkko Sakkinen
Quoted 23 lines · Nayna
Quoted 20 lines · Jarkko Sakkinen

There are two type issues associated with tpm_getcap().

You must not do arithmetic with __be32 or __le32 types because sometimes it results incorrect results. Calculations must be done only with data that is in CPU byte order. This commit migrates tpm_getcap() to struct tpm_buf in order to sort out these issues.

The second issue is with struct cap_t as the size of the type bool is assumed to be one byte. This commit sorts out the issue by changing the type to u8.

---
v2:
- Use struct tpm_buf.
- Merge the type change of 'owned' to this patch.

   drivers/char/tpm/tpm-interface.c | 33 ++++++++++++++++++---------------
   drivers/char/tpm/tpm.h           | 15 +--------------
   2 files changed, 19 insertions(+), 29 deletions(-)
Quoted 7 lines

drivers/char/tpm/tpm-interface.c static const struct tpm_input_header tpm_getcap_header = {

Quoted 5 lines
drivers/char/tpm/tpm-interface.c +0 -0
index 423938e..7af1e8c 100644
@@ -480,31 +480,34 @@ static const struct tpm_input_header tpm_getcap_header = {

Is tpm_getcap_header still needed ?

Quoted 2 lines

Definitely not, thanks. Are you able to try this out so I could move forward with pull request?

Yes, I tried it out and see a problem, I already replied that in one other mail. The tag and ordinal are converted to BE twice, once in #define and again in tpm_buf_init.

Thanks & Regards,
- Nayna

Quoted 3 lines

/Jarkko

#8 lore raw
Quoted 38 lines · Nayna
Quoted 32 lines · Jarkko Sakkinen
Quoted 23 lines · Nayna
Quoted 20 lines · Jarkko Sakkinen

There are two type issues associated with tpm_getcap().

You must not do arithmetic with __be32 or __le32 types because sometimes it results incorrect results. Calculations must be done only with data that is in CPU byte order. This commit migrates tpm_getcap() to struct tpm_buf in order to sort out these issues.

The second issue is with struct cap_t as the size of the type bool is assumed to be one byte. This commit sorts out the issue by changing the type to u8.

---
v2:
- Use struct tpm_buf.
- Merge the type change of 'owned' to this patch.

   drivers/char/tpm/tpm-interface.c | 33 ++++++++++++++++++---------------
   drivers/char/tpm/tpm.h           | 15 +--------------
   2 files changed, 19 insertions(+), 29 deletions(-)
Quoted 7 lines

drivers/char/tpm/tpm-interface.c static const struct tpm_input_header tpm_getcap_header = {

Quoted 5 lines
drivers/char/tpm/tpm-interface.c +0 -0
index 423938e..7af1e8c 100644
@@ -480,31 +480,34 @@ static const struct tpm_input_header tpm_getcap_header = {

Is tpm_getcap_header still needed ?

Quoted 2 lines

Definitely not, thanks. Are you able to try this out so I could move forward with pull request?

Quoted 7 lines

Yes, I tried it out and see a problem, I already replied that in one other mail.
The tag and ordinal are converted to BE twice, once in #define and again in tpm_buf_init.

Thanks & Regards,
- Nayna

Hmm... I wonder why it seemed to work when I tried it. I must have done BuildRoot image build without this patch [1]. Hmm... I just take Stefans patch that only changes 'owned' to u8 and refine this after the pull request. Fixing the other sparse errors is not as high priority for the release.

[1] http://git.infradead.org/users/jjs/buildroot-tpmdd.git

/Jarkko

#9 lore raw
In reply to Jarkko Sakkinen
Quoted 25 lines · Jarkko Sakkinen

There are two type issues associated with tpm_getcap().

You must not do arithmetic with __be32 or __le32 types because sometimes it results incorrect results. Calculations must be done only with data that is in CPU byte order. This commit migrates tpm_getcap() to struct tpm_buf in order to sort out these issues.

The second issue is with struct cap_t as the size of the type bool is assumed to be one byte. This commit sorts out the issue by changing the type to u8.

---
v2:
- Use struct tpm_buf.
- Merge the type change of 'owned' to this patch.

  drivers/char/tpm/tpm-interface.c | 33 ++++++++++++++++++---------------
  drivers/char/tpm/tpm.h           | 15 +--------------
  2 files changed, 19 insertions(+), 29 deletions(-)
drivers/char/tpm/tpm-interface.c +0 -0
index 423938e..7af1e8c 100644

drivers/char/tpm/tpm-interface.c static const struct tpm_input_header tpm_getcap_header = { · line 486

Quoted 9 lines
drivers/char/tpm/tpm-interface.c +2 -2
@@ -480,31 +480,34 @@ static const struct tpm_input_header tpm_getcap_header = {
  ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
  		   const char *desc, size_t min_cap_length)
  {
-	struct tpm_cmd_t tpm_cmd;
+	struct tpm_buf buf;
  	int rc;
 
-	tpm_cmd.header.in = tpm_getcap_header;
+	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);

I think there is a problem here of twice converting to be32 for both tag and ordinal.

Both TPM_ORD_GET_CAP and TPM_TAG_RQU_COMMAND are already defined as:
#define TPM_ORD_GET_CAP cpu_to_be32(101)
#define TPM_TAG_RQU_COMMAND cpu_to_be16(193)

and again converted to BE in tpm_buf_init().

Thanks & Regards,
- Nayna

Quoted 83 lines
drivers/char/tpm/tpm-interface.c +16 -13
+	if (rc)
+		return rc;
+
  	if (subcap_id == TPM_CAP_VERSION_1_1 ||
  	    subcap_id == TPM_CAP_VERSION_1_2) {
-		tpm_cmd.params.getcap_in.cap = cpu_to_be32(subcap_id);
-		/*subcap field not necessary */
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
-		tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
+		tpm_buf_append_u32(&buf, subcap_id);
+		tpm_buf_append_u32(&buf, 0);
  	} else {
  		if (subcap_id == TPM_CAP_FLAG_PERM ||
  		    subcap_id == TPM_CAP_FLAG_VOL)
-			tpm_cmd.params.getcap_in.cap =
-				cpu_to_be32(TPM_CAP_FLAG);
+			tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
  		else
-			tpm_cmd.params.getcap_in.cap =
-				cpu_to_be32(TPM_CAP_PROP);
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
-		tpm_cmd.params.getcap_in.subcap = cpu_to_be32(subcap_id);
+			tpm_buf_append_u32(&buf, TPM_CAP_PROP);
+
+		tpm_buf_append_u32(&buf, 4);
+		tpm_buf_append_u32(&buf, subcap_id);
  	}
-	rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
-			      min_cap_length, 0, desc);
+
+	rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, min_cap_length, 0,
+			      desc);
  	if (!rc)
-		*cap = tpm_cmd.params.getcap_out.cap;
+		*cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
+
+	tpm_buf_destroy(&buf);
  	return rc;
  }
  EXPORT_SYMBOL_GPL(tpm_getcap);
drivers/char/tpm/tpm.h +1 -14
index bff37be..709e7d4 100644
@@ -281,7 +281,7 @@ struct permanent_flags_t {
  typedef union {
  	struct	permanent_flags_t perm_flags;
  	struct	stclear_flags_t	stclear_flags;
-	bool	owned;
+	u8	owned;
  	__be32	num_pcrs;
  	struct	tpm_version_t	tpm_version;
  	struct	tpm_version_1_2_t tpm_version_1_2;
@@ -307,17 +307,6 @@ enum tpm_sub_capabilities {
  	TPM_CAP_PROP_TIS_DURATION = 0x120,
  };
 
-struct	tpm_getcap_params_in {
-	__be32	cap;
-	__be32	subcap_size;
-	__be32	subcap;
-} __packed;
-
-struct	tpm_getcap_params_out {
-	__be32	cap_size;
-	cap_t	cap;
-} __packed;
-
  struct	tpm_readpubek_params_out {
  	u8	algorithm[4];
  	u8	encscheme[2];
@@ -367,10 +356,8 @@ struct tpm_startup_in {
  } __packed;
 
  typedef union {
-	struct	tpm_getcap_params_out getcap_out;
  	struct	tpm_readpubek_params_out readpubek_out;
  	u8	readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
-	struct	tpm_getcap_params_in getcap_in;
  	struct	tpm_pcrread_in	pcrread_in;
  	struct	tpm_pcrread_out	pcrread_out;
  	struct	tpm_pcrextend_in pcrextend_in;