Skip to contentSkip to conversation

tpm: factor out tpm1 code into tpm1-cmd.c

PATCH In review Tomas Winkler 2 participants 24 messages Mar 10, 2018 – Mar 16, 2018

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

v2 cover tpm: factor out tpm1 code into tpm1-cmd.c

Move TPM1 implementation from tpm-interface.c to tpm1-cmd.c similarly to tpm2-cmd.c, as well as move TPM2 open code flows into new functions to tpm2-cmd.c
The functions in tpm-interface.c should now be in form:

int tpm_func(struct tpm_chip *chip)
{
if (chip->flags & TPM_CHIP_FLAG_TPM2)

                return tpm2_func(chip);
        else
                return tpm1_func(chip);
}

This is work in progress of possible dropping compiling out one tpm 1.x or tpm 2.x in case not needed on a target platform.

V2: 1. This resend includes more patches that cleans the tpm-interface.c completely
2. Fix notorious typo tmp -> tpm, in some patches

Tomas Winkler (8):
tpm: move tpm1_pcr_extend to tpm1-cmd.c
tpm: move tpm_getcap to tpm1-cmd.c
tpm: factor out tpm1_get_random into tpm1-cmd.c
tpm: move tpm1 selftest code from tpm-interface tpm1-cmd.c
tpm: factor out tpm1 pm suspend flow into tpm1-cmd.c
tpm: factor out tpm_startup function
tpm: Move pcr extend to tpm2-cmd.c
tpm: move tpm2 code validation to tpm2-space.c

 drivers/char/tpm/st33zp24/st33zp24.c |   2 +-
 drivers/char/tpm/tpm-interface.c     | 425 ++---------------------------------
 drivers/char/tpm/tpm-sysfs.c         |  50 ++---
 drivers/char/tpm/tpm.h               |  18 +-
 drivers/char/tpm/tpm1-cmd.c          | 358 ++++++++++++++++++++++++++++-
 drivers/char/tpm/tpm2-cmd.c          |  60 ++++-
 drivers/char/tpm/tpm2-space.c        |  32 +++
 drivers/char/tpm/tpm_tis_core.c      |   4 +-
 8 files changed, 496 insertions(+), 453 deletions(-)
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw

PATCH v2 1/8 tpm: move tpm1_pcr_extend to tpm1-cmd.c

Move tpm1_pcr_extend to tpm1-cmd.c and remove
unused pcrextend_header structure.

---
 drivers/char/tpm/tpm-interface.c | 28 ----------------------------
 drivers/char/tpm/tpm.h           |  2 ++
 drivers/char/tpm/tpm1-cmd.c      | 23 +++++++++++++++++++++++
 3 files changed, 25 insertions(+), 28 deletions(-)
drivers/char/tpm/tpm-interface.c +0 -28
index 7f6968b750c8..83eeefb2a4af 100644
@@ -514,34 +514,6 @@ int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
 }
 EXPORT_SYMBOL_GPL(tpm_pcr_read);
 
-#define TPM_ORD_PCR_EXTEND 20
-#define EXTEND_PCR_RESULT_SIZE 34
-#define EXTEND_PCR_RESULT_BODY_SIZE 20
-static const struct tpm_input_header pcrextend_header = {
-	.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
-	.length = cpu_to_be32(34),
-	.ordinal = cpu_to_be32(TPM_ORD_PCR_EXTEND)
-};
-
-static int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
-			   char *log_msg)
-{
-	struct tpm_buf buf;
-	int rc;
-
-	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND);
-	if (rc)
-		return rc;
-
-	tpm_buf_append_u32(&buf, pcr_idx);
-	tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
-
-	rc = tpm_transmit_cmd(chip, NULL, buf.data, EXTEND_PCR_RESULT_SIZE,
-			      EXTEND_PCR_RESULT_BODY_SIZE, 0, log_msg);
-	tpm_buf_destroy(&buf);
-	return rc;
-}
-
 /**
  * tpm_pcr_extend - extend a PCR value in SHA1 bank.
  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
drivers/char/tpm/tpm.h +2 -0
index 48706f091856..4306c878f1d9 100644
@@ -535,6 +535,8 @@ int tpm_do_selftest(struct tpm_chip *chip);
 int tpm1_auto_startup(struct tpm_chip *chip);
 int tpm1_get_timeouts(struct tpm_chip *chip);
 unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
+int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
+		    const char *log_msg);
 int tpm_pm_suspend(struct device *dev);
 int tpm_pm_resume(struct device *dev);
 
drivers/char/tpm/tpm1-cmd.c +23 -0
index 14dfb2b9a067..6a0d8717fdf6 100644
@@ -414,3 +414,26 @@ int tpm1_get_timeouts(struct tpm_chip *chip)
 	chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
 	return 0;
 }
+
+#define TPM_ORD_PCR_EXTEND 20
+#define EXTEND_PCR_RESULT_SIZE 34
+#define EXTEND_PCR_RESULT_BODY_SIZE 20
+int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
+		    const char *log_msg)
+{
+	struct tpm_buf buf;
+	int rc;
+
+	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND);
+	if (rc)
+		return rc;
+
+	tpm_buf_append_u32(&buf, pcr_idx);
+	tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
+
+	rc = tpm_transmit_cmd(chip, NULL, buf.data, EXTEND_PCR_RESULT_SIZE,
+			      EXTEND_PCR_RESULT_BODY_SIZE, 0, log_msg);
+	tpm_buf_destroy(&buf);
+	return rc;
+}
+
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reviewed-by on v2 1/8
#2 lore raw

Re: [PATCH v2 1/8] tpm: move tpm1_pcr_extend to tpm1-cmd.c

Quoted 4 lines · Tomas Winkler

Move tpm1_pcr_extend to tpm1-cmd.c and remove
unused pcrextend_header structure.

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

PATCH v2 2/8 tpm: move tpm_getcap to tpm1-cmd.c

In reply to Tomas Winkler

1. Move tpm_getcap to tpm1-cmd. Rename the function to tpm1_getcap.
2. Remove unused tpm_getcap_header with unused constant
as this functionality is already implemented using tpm_buf construct.

---
 drivers/char/tpm/tpm-interface.c | 47 +-----------------------------------
 drivers/char/tpm/tpm-sysfs.c     | 48 ++++++++++++++++++-------------------
 drivers/char/tpm/tpm.h           |  4 ++--
 drivers/char/tpm/tpm1-cmd.c      | 51 +++++++++++++++++++++++++++++++++-------
 drivers/char/tpm/tpm_tis_core.c  |  2 +-
 5 files changed, 71 insertions(+), 81 deletions(-)
drivers/char/tpm/tpm-interface.c +1 -46
index 83eeefb2a4af..6b70cefed505 100644
@@ -354,52 +354,6 @@ int tpm_startup(struct tpm_chip *chip)
 	return rc;
 }
 
-#define TPM_DIGEST_SIZE 20
-#define TPM_RET_CODE_IDX 6
-#define TPM_INTERNAL_RESULT_SIZE 200
-#define TPM_ORD_GET_CAP 101
-#define TPM_ORD_GET_RANDOM 70
-
-static const struct tpm_input_header tpm_getcap_header = {
-	.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
-	.length = cpu_to_be32(22),
-	.ordinal = cpu_to_be32(TPM_ORD_GET_CAP)
-};
-
-ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
-		   const char *desc, size_t min_cap_length)
-{
-	struct tpm_buf buf;
-	int rc;
-
-	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_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_buf_append_u32(&buf, TPM_CAP_FLAG);
-		else
-			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, NULL, buf.data, PAGE_SIZE,
-			      min_cap_length, 0, desc);
-	if (!rc)
-		*cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
-
-	tpm_buf_destroy(&buf);
-	return rc;
-}
-EXPORT_SYMBOL_GPL(tpm_getcap);
-
 int tpm_get_timeouts(struct tpm_chip *chip)
 {
 	if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
@@ -753,6 +707,7 @@ int tpm_pm_resume(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(tpm_pm_resume);
 
+#define TPM_ORD_GET_RANDOM 70
 #define TPM_GETRANDOM_RESULT_SIZE	18
 static const struct tpm_input_header tpm_getrandom_header = {
 	.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
drivers/char/tpm/tpm-sysfs.c +24 -24

96 lines not on this page · raw message

drivers/char/tpm/tpm.h +2 -2
index 4306c878f1d9..1a8ef4d3cb1c 100644
@@ -527,8 +527,6 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
 			 size_t min_rsp_body_length, unsigned int flags,
 			 const char *desc);
 int tpm_startup(struct tpm_chip *chip);
-ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
-		   const char *desc, size_t min_cap_length);
 int tpm_get_timeouts(struct tpm_chip *);
 int tpm_do_selftest(struct tpm_chip *chip);
 
@@ -537,6 +535,8 @@ int tpm1_get_timeouts(struct tpm_chip *chip);
 unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
 int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
 		    const char *log_msg);
+ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
+		    const char *desc, size_t min_cap_length);
 int tpm_pm_suspend(struct device *dev);
 int tpm_pm_resume(struct device *dev);
 
drivers/char/tpm/tpm1-cmd.c +43 -8

70 lines not on this page · raw message

drivers/char/tpm/tpm_tis_core.c +1 -1
index 222e45a5a762..9278478b109b 100644
@@ -628,7 +628,7 @@ static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
 		return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
 	else
-		return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc,
+		return tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc,
 				  0);
 }
 
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reviewed-by on v2 2/8
#4 lore raw

Re: [PATCH v2 2/8] tpm: move tpm_getcap to tpm1-cmd.c

Quoted 5 lines · Tomas Winkler

1. Move tpm_getcap to tpm1-cmd. Rename the function to tpm1_getcap.
2. Remove unused tpm_getcap_header with unused constant
as this functionality is already implemented using tpm_buf construct.

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

PATCH v2 3/8 tpm: factor out tpm1_get_random into tpm1-cmd.c

In reply to Tomas Winkler

Factor out get random implementation from tpm-interface.c into tpm1_get_random function in tpm1-cmd.c.
No functional changes.

---
 drivers/char/tpm/tpm-interface.c | 56 ++++------------------------------------
 drivers/char/tpm/tpm.h           |  2 ++
 drivers/char/tpm/tpm1-cmd.c      | 54 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+), 51 deletions(-)
drivers/char/tpm/tpm-interface.c +5 -51
index 6b70cefed505..59ca2e30b4d2 100644
@@ -707,14 +707,6 @@ int tpm_pm_resume(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(tpm_pm_resume);
 
-#define TPM_ORD_GET_RANDOM 70
-#define TPM_GETRANDOM_RESULT_SIZE	18
-static const struct tpm_input_header tpm_getrandom_header = {
-	.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
-	.length = cpu_to_be32(14),
-	.ordinal = cpu_to_be32(TPM_ORD_GET_RANDOM)
-};
-
 /**
  * tpm_get_random() - get random bytes from the TPM's RNG
  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
@@ -725,57 +717,19 @@ static const struct tpm_input_header tpm_getrandom_header = {
  */
 int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
 {
-	struct tpm_cmd_t tpm_cmd;
-	u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength;
-	int err, total = 0, retries = 5;
-	u8 *dest = out;
-
-	if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
-		return -EINVAL;
+	int err;
 
 	chip = tpm_chip_find_get(chip);
 	if (!chip)
 		return -ENODEV;
 
-	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
 		err = tpm2_get_random(chip, out, max);
-		tpm_put_ops(chip);
-		return err;
-	}
-
-	do {
-		tpm_cmd.header.in = tpm_getrandom_header;
-		tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
-
-		err = tpm_transmit_cmd(chip, NULL, &tpm_cmd,
-				       TPM_GETRANDOM_RESULT_SIZE + num_bytes,
-				       offsetof(struct tpm_getrandom_out,
-						rng_data),
-				       0, "attempting get random");
-		if (err)
-			break;
-
-		recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
-		if (recd > num_bytes) {
-			total = -EFAULT;
-			break;
-		}
-
-		rlength = be32_to_cpu(tpm_cmd.header.out.length);
-		if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
-			      recd) {
-			total = -EFAULT;
-			break;
-		}
-		memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
-
-		dest += recd;
-		total += recd;
-		num_bytes -= recd;
-	} while (retries-- && total < max);
+	else
+		err = tpm1_get_random(chip, out, max);
 
 	tpm_put_ops(chip);
-	return total ? total : -EIO;
+	return err;
 }
 EXPORT_SYMBOL_GPL(tpm_get_random);
 
drivers/char/tpm/tpm.h +2 -0
index 1a8ef4d3cb1c..b5fe0269a833 100644
@@ -537,6 +537,8 @@ int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
 		    const char *log_msg);
 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
 		    const char *desc, size_t min_cap_length);
+int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
+
 int tpm_pm_suspend(struct device *dev);
 int tpm_pm_resume(struct device *dev);
 
drivers/char/tpm/tpm1-cmd.c +54 -0

57 lines not on this page · raw message

Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info@ http://vger.kernel.org/majordomo-info.html
#6 lore raw

Re: [PATCH v2 3/8] tpm: factor out tpm1_get_random into tpm1-cmd.c

drivers/char/tpm/tpm1-cmd.c ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap, · line 520

Quoted 7 lines · Tomas Winkler
drivers/char/tpm/tpm1-cmd.c +7 -0
+		rlength = be32_to_cpu(tpm_cmd.header.out.length);
+		if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
+			      recd) {
+			total = -EFAULT;
+			break;
+		}
+		memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);

This rlength stuff can be handled with tpm_buf_length() as I do in my pendig-for-review patch set:

https://patchwork.kernel.org/patch/10259331/

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw

RE: [PATCH v2 3/8] tpm: factor out tpm1_get_random into tpm1-cmd.c

Quoted 11 lines · Tomas Winkler

drivers/char/tpm/tpm1-cmd.c ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,

Quoted 7 lines
drivers/char/tpm/tpm1-cmd.c +7 -0
+		rlength = be32_to_cpu(tpm_cmd.header.out.length);
+		if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
+			      recd) {
+			total = -EFAULT;
+			break;
+		}
+		memcpy(dest, tpm_cmd.params.getrandom_out.rng_data,

recd);

Quoted 4 lines

This rlength stuff can be handled with tpm_buf_length() as I do in my pendig-for-review patch set:

https://patchwork.kernel.org/patch/10259331/

Right, as I wrote before not sure it's good to move and change the code more than necessary at the same time. I would leave the tpm_buf_ changes after this series.

Thanks
Tomas

????{.n?+???????+%?????????w??{.n?+????{??????????v?^?)????w*jg??????????j????G?????? ???j:+v???w?j?m????????w?????f???h?????????

#8 lore raw

Re: [PATCH v2 3/8] tpm: factor out tpm1_get_random into tpm1-cmd.c

Quoted 11 lines · Tomas
Quoted 11 lines · Tomas Winkler

drivers/char/tpm/tpm1-cmd.c ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,

Quoted 7 lines
drivers/char/tpm/tpm1-cmd.c +7 -0
+		rlength = be32_to_cpu(tpm_cmd.header.out.length);
+		if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
+			      recd) {
+			total = -EFAULT;
+			break;
+		}
+		memcpy(dest, tpm_cmd.params.getrandom_out.rng_data,

recd);

Quoted 7 lines
Quoted 4 lines

This rlength stuff can be handled with tpm_buf_length() as I do in my pendig-for-review patch set:

https://patchwork.kernel.org/patch/10259331/

Right, as I wrote before not sure it's good to move and change the code more than necessary at the same time. I would leave the tpm_buf_ changes after this series.

Right, this is a valid point. I'll ack this as it is. Thanks for pointing this out.

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw

PATCH v2 4/8 tpm: move tpm1 selftest code from tpm-interface tpm1-cmd.c

In reply to Tomas Winkler

Move the tmp1 selftest code functions to tpm1-cmd.c and adjust callers to use the new function names.
1. tpm_pcr_read_dev to tpm1_pcr_read_dev
2. tpm_continue_selftest to tpm1_continue_selftest
3. tpm_do_selftest to tpm1_do_selftest

---
 drivers/char/tpm/st33zp24/st33zp24.c |   2 +-
 drivers/char/tpm/tpm-interface.c     | 144 +----------------------------------
 drivers/char/tpm/tpm-sysfs.c         |   2 +-
 drivers/char/tpm/tpm.h               |   4 +-
 drivers/char/tpm/tpm1-cmd.c          | 139 +++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_tis_core.c      |   2 +-
 6 files changed, 147 insertions(+), 146 deletions(-)
drivers/char/tpm/st33zp24/st33zp24.c +1 -1
index ad03c99899fa..575b7c2eab25 100644
@@ -651,7 +651,7 @@ int st33zp24_pm_resume(struct device *dev)
 	} else {
 		ret = tpm_pm_resume(dev);
 		if (!ret)
-			tpm_do_selftest(chip);
+			tpm1_do_selftest(chip);
 	}
 	return ret;
 } /* st33zp24_pm_resume() */
drivers/char/tpm/tpm-interface.c +3 -141

165 lines not on this page · raw message

drivers/char/tpm/tpm-sysfs.c +1 -1
index 008515314ae3..861acafd8f29 100644
@@ -114,7 +114,7 @@ static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
 
 	num_pcrs = be32_to_cpu(cap.num_pcrs);
 	for (i = 0; i < num_pcrs; i++) {
-		rc = tpm_pcr_read_dev(chip, i, digest);
+		rc = tpm1_pcr_read_dev(chip, i, digest);
 		if (rc)
 			break;
 		str += sprintf(str, "PCR-%02d: ", i);
drivers/char/tpm/tpm.h +2 -2
index b5fe0269a833..6922fdfc4274 100644
@@ -528,8 +528,8 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
 			 const char *desc);
 int tpm_startup(struct tpm_chip *chip);
 int tpm_get_timeouts(struct tpm_chip *);
-int tpm_do_selftest(struct tpm_chip *chip);
 
+int tpm1_do_selftest(struct tpm_chip *chip);
 int tpm1_auto_startup(struct tpm_chip *chip);
 int tpm1_get_timeouts(struct tpm_chip *chip);
 unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
@@ -538,6 +538,7 @@ int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
 		    const char *desc, size_t min_cap_length);
 int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
+int tpm1_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
 
 int tpm_pm_suspend(struct device *dev);
 int tpm_pm_resume(struct device *dev);
@@ -561,7 +562,6 @@ void tpm_chip_unregister(struct tpm_chip *chip);
 
 void tpm_sysfs_add_device(struct tpm_chip *chip);
 
-int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
 
 #ifdef CONFIG_ACPI
 extern void tpm_add_ppi(struct tpm_chip *chip);
drivers/char/tpm/tpm1-cmd.c +139 -0

142 lines not on this page · raw message

drivers/char/tpm/tpm_tis_core.c +1 -1
index 9278478b109b..87f63c500956 100644
@@ -1018,7 +1018,7 @@ int tpm_tis_resume(struct device *dev)
 	 * an error code but for unknown reason it isn't handled.
 	 */
 	if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
-		tpm_do_selftest(chip);
+		tpm1_do_selftest(chip);
 
 	return 0;
 }
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reviewed-by on v2 4/8
#10 lore raw

Re: [PATCH v2 4/8] tpm: move tpm1 selftest code from tpm-interface tpm1-cmd.c

Quoted 7 lines · Tomas Winkler

Move the tmp1 selftest code functions to tpm1-cmd.c and adjust callers to use the new function names.
1. tpm_pcr_read_dev to tpm1_pcr_read_dev
2. tpm_continue_selftest to tpm1_continue_selftest
3. tpm_do_selftest to tpm1_do_selftest

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#11 lore raw

PATCH v2 5/8 tpm: factor out tpm1 pm suspend flow into tpm1-cmd.c

In reply to Tomas Winkler

Factor out tpm1 suspend flow from tpm-interface.c into a new function tpm1_pm_suspend in tpm1-cmd.c

---
 drivers/char/tpm/tpm-interface.c | 55 ++++------------------------------------
 drivers/char/tpm/tpm.h           |  1 +
 drivers/char/tpm/tpm1-cmd.c      | 54 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 50 deletions(-)
drivers/char/tpm/tpm-interface.c +5 -50
index a7dfe286b343..11899ef32457 100644
@@ -485,15 +485,6 @@ int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
 }
 EXPORT_SYMBOL_GPL(tpm_send);
 
-#define TPM_ORD_SAVESTATE 152
-#define SAVESTATE_RESULT_SIZE 10
-
-static const struct tpm_input_header savestate_header = {
-	.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
-	.length = cpu_to_be32(10),
-	.ordinal = cpu_to_be32(TPM_ORD_SAVESTATE)
-};
-
 /*
  * We are about to suspend. Save the TPM state
  * so that it can be restored.
@@ -501,54 +492,18 @@ static const struct tpm_input_header savestate_header = {
 int tpm_pm_suspend(struct device *dev)
 {
 	struct tpm_chip *chip = dev_get_drvdata(dev);
-	struct tpm_cmd_t cmd;
-	int rc, try;
+	int rc = 0;
 
-	u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
-
-	if (chip == NULL)
+	if (!chip)
 		return -ENODEV;
 
 	if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
 		return 0;
 
-	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
 		tpm2_shutdown(chip, TPM2_SU_STATE);
-		return 0;
-	}
-
-	/* for buggy tpm, flush pcrs with extend to selected dummy */
-	if (tpm_suspend_pcr)
-		rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash,
-				     "extending dummy pcr before suspend");
-
-	/* now do the actual savestate */
-	for (try = 0; try < TPM_RETRY; try++) {
-		cmd.header.in = savestate_header;
-		rc = tpm_transmit_cmd(chip, NULL, &cmd, SAVESTATE_RESULT_SIZE,
-				      0, 0, NULL);
-
-		/*
-		 * If the TPM indicates that it is too busy to respond to
-		 * this command then retry before giving up.  It can take
-		 * several seconds for this TPM to be ready.
-		 *
-		 * This can happen if the TPM has already been sent the
-		 * SaveState command before the driver has loaded.  TCG 1.2
-		 * specification states that any communication after SaveState
-		 * may cause the TPM to invalidate previously saved state.
-		 */
-		if (rc != TPM_WARN_RETRY)
-			break;
-		tpm_msleep(TPM_TIMEOUT_RETRY);
-	}
-
-	if (rc)
-		dev_err(&chip->dev,
-			"Error (%d) sending savestate before suspend\n", rc);
-	else if (try > 0)
-		dev_warn(&chip->dev, "TPM savestate took %dms\n",
-			 try * TPM_TIMEOUT_RETRY);
+	else
+		rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
 
 	return rc;
 }
drivers/char/tpm/tpm.h +1 -0
index 6922fdfc4274..1bed9b5441c5 100644
@@ -529,6 +529,7 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
 int tpm_startup(struct tpm_chip *chip);
 int tpm_get_timeouts(struct tpm_chip *);
 
+int tpm1_pm_suspend(struct tpm_chip *chip, int tpm_suspend_pcr);
 int tpm1_do_selftest(struct tpm_chip *chip);
 int tpm1_auto_startup(struct tpm_chip *chip);
 int tpm1_get_timeouts(struct tpm_chip *chip);
drivers/char/tpm/tpm1-cmd.c +54 -0

57 lines not on this page · raw message

Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reviewed-by on v2 5/8
#12 lore raw

Re: [PATCH v2 5/8] tpm: factor out tpm1 pm suspend flow into tpm1-cmd.c

Quoted 4 lines · Tomas Winkler

Factor out tpm1 suspend flow from tpm-interface.c into a new function tpm1_pm_suspend in tpm1-cmd.c

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#13 lore raw

PATCH v2 6/8 tpm: factor out tpm_startup function

In reply to Tomas Winkler

tpm manual startup is used only from within tpm1 or tpm2 code, hence remove tpm_startup function from tpm-interface.c and add two static functions implementations tpm1_startup and tpm2_startup into to tpm1-cmd.c and tpm2-cmd.c respectively.

---
 drivers/char/tpm/tpm-interface.c | 41 ----------------------------------------
 drivers/char/tpm/tpm.h           |  1 -
 drivers/char/tpm/tpm1-cmd.c      | 37 +++++++++++++++++++++++++++++++++++-
 drivers/char/tpm/tpm2-cmd.c      | 34 +++++++++++++++++++++++++++++++--
 4 files changed, 68 insertions(+), 45 deletions(-)
drivers/char/tpm/tpm-interface.c +0 -41
index 11899ef32457..90e14462500a 100644
@@ -313,47 +313,6 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
 }
 EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
 
-#define TPM_ORD_STARTUP 153
-#define TPM_ST_CLEAR 1
-
-/**
- * tpm_startup - turn on the TPM
- * @chip: TPM chip to use
- *
- * Normally the firmware should start the TPM. This function is provided as a
- * workaround if this does not happen. A legal case for this could be for
- * example when a TPM emulator is used.
- *
- * Return: same as tpm_transmit_cmd()
- */
-int tpm_startup(struct tpm_chip *chip)
-{
-	struct tpm_buf buf;
-	int rc;
-
-	dev_info(&chip->dev, "starting up the TPM manually\n");
-
-	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
-		rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
-		if (rc < 0)
-			return rc;
-
-		tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
-	} else {
-		rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP);
-		if (rc < 0)
-			return rc;
-
-		tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
-	}
-
-	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
-			      "attempting to start the TPM");
-
-	tpm_buf_destroy(&buf);
-	return rc;
-}
-
 int tpm_get_timeouts(struct tpm_chip *chip)
 {
 	if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
drivers/char/tpm/tpm.h +0 -1
index 1bed9b5441c5..434f11c23e3e 100644
@@ -526,7 +526,6 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
 			 void *buf, size_t bufsiz,
 			 size_t min_rsp_body_length, unsigned int flags,
 			 const char *desc);
-int tpm_startup(struct tpm_chip *chip);
 int tpm_get_timeouts(struct tpm_chip *);
 
 int tpm1_pm_suspend(struct tpm_chip *chip, int tpm_suspend_pcr);
drivers/char/tpm/tpm1-cmd.c +36 -1
index e2c9f609fe64..85dd0fcbd4cc 100644
@@ -307,6 +307,40 @@ unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
 }
 EXPORT_SYMBOL_GPL(tpm1_calc_ordinal_duration);
 
+#define TPM_ORD_STARTUP 153
+#define TPM_ST_CLEAR 1
+
+/**
+ * tpm_startup - turn on the TPM
+ * @chip: TPM chip to use
+ *
+ * Normally the firmware should start the TPM. This function is provided as a
+ * workaround if this does not happen. A legal case for this could be for
+ * example when a TPM emulator is used.
+ *
+ * Return: same as tpm_transmit_cmd()
+ */
+static int tpm1_startup(struct tpm_chip *chip)
+{
+	struct tpm_buf buf;
+	int rc;
+
+	dev_info(&chip->dev, "starting up the TPM manually\n");
+
+	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP);
+	if (rc < 0)
+		return rc;
+
+	tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
+
+	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
+			      "attempting to start the TPM");
+
+	tpm_buf_destroy(&buf);
+
+	return rc;
+}
+
 int tpm1_get_timeouts(struct tpm_chip *chip)
 {
 	cap_t cap;
@@ -316,7 +350,7 @@ int tpm1_get_timeouts(struct tpm_chip *chip)
 	rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL,
 			 sizeof(cap.timeout));
 	if (rc == TPM_ERR_INVALID_POSTINIT) {
-		if (tpm_startup(chip))
+		if (tpm1_startup(chip))
 			return rc;
 
 		rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap,
@@ -719,3 +753,4 @@ int tpm1_pm_suspend(struct tpm_chip *chip, int tpm_suspend_pcr)
 
 	return rc;
 }
+
drivers/char/tpm/tpm2-cmd.c +32 -2
index 8114fd59e2c5..07636aa6f1a9 100644
@@ -1006,6 +1006,36 @@ static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
 	return rc;
 }
 
+/**
+ * tpm2_startup - turn on the TPM
+ * @chip: TPM chip to use
+ *
+ * Normally the firmware should start the TPM. This function is provided as a
+ * workaround if this does not happen. A legal case for this could be for
+ * example when a TPM emulator is used.
+ *
+ * Return: same as tpm_transmit_cmd()
+ */
+
+static int tpm2_startup(struct tpm_chip *chip)
+{
+	struct tpm_buf buf;
+	int rc;
+
+	dev_info(&chip->dev, "starting up the TPM manually\n");
+
+	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
+	if (rc < 0)
+		return rc;
+
+	tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
+	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
+			      "attempting to start the TPM");
+	tpm_buf_destroy(&buf);
+
+	return rc;
+}
+
 /**
  * tpm2_auto_startup - Perform the standard automatic TPM initialization
  *                     sequence
@@ -1017,7 +1047,7 @@ int tpm2_auto_startup(struct tpm_chip *chip)
 {
 	int rc;
 
-	rc = tpm_get_timeouts(chip);
+	rc = tpm2_get_timeouts(chip);
 	if (rc)
 		goto out;
 
@@ -1028,7 +1058,7 @@ int tpm2_auto_startup(struct tpm_chip *chip)
 	}
 
 	if (rc == TPM2_RC_INITIALIZE) {
-		rc = tpm_startup(chip);
+		rc = tpm2_startup(chip);
 		if (rc)
 			goto out;
 
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info@ http://vger.kernel.org/majordomo-info.html
Reviewed-by on v2 6/8
#14 lore raw

Re: [PATCH v2 6/8] tpm: factor out tpm_startup function

Quoted 6 lines · Tomas Winkler

tpm manual startup is used only from within tpm1 or tpm2 code, hence remove tpm_startup function from tpm-interface.c and add two static functions implementations tpm1_startup and tpm2_startup into to tpm1-cmd.c and tpm2-cmd.c respectively.

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#15 lore raw

PATCH v2 7/8 tpm: Move pcr extend to tpm2-cmd.c

In reply to Tomas Winkler

Add tpm2_pcr_extend function to tpm2-cmd.c with signature required by tpm-interface.c. It wraps the original open code implementation. The original original tpm2_pcr_extend function is renamed and made static, called only from new tpm2_pcr_extend.

---
 drivers/char/tpm/tpm-interface.c | 25 +++++--------------------
 drivers/char/tpm/tpm.h           |  3 +--
 drivers/char/tpm/tpm2-cmd.c      | 25 +++++++++++++++++++++++--
 3 files changed, 29 insertions(+), 24 deletions(-)
drivers/char/tpm/tpm-interface.c +5 -20
index 90e14462500a..d347ad8325c6 100644
@@ -391,31 +391,16 @@ EXPORT_SYMBOL_GPL(tpm_pcr_read);
 int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
 {
 	int rc;
-	struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
-	u32 count = 0;
-	int i;
 
 	chip = tpm_chip_find_get(chip);
 	if (!chip)
 		return -ENODEV;
 
-	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
-		memset(digest_list, 0, sizeof(digest_list));
-
-		for (i = 0; i < ARRAY_SIZE(chip->active_banks) &&
-			    chip->active_banks[i] != TPM2_ALG_ERROR; i++) {
-			digest_list[i].alg_id = chip->active_banks[i];
-			memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
-			count++;
-		}
-
-		rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
-		tpm_put_ops(chip);
-		return rc;
-	}
-
-	rc = tpm1_pcr_extend(chip, pcr_idx, hash,
-			     "attempting extend a PCR value");
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
+		rc = tpm2_pcr_extend(chip, pcr_idx, hash);
+	else
+		rc = tpm1_pcr_extend(chip, pcr_idx, hash,
+				     "attempting extend a PCR value");
 	tpm_put_ops(chip);
 	return rc;
 }
drivers/char/tpm/tpm.h +1 -2
index 434f11c23e3e..2b88aadc4743 100644
@@ -578,8 +578,7 @@ static inline u32 tpm2_rc_value(u32 rc)
 
 int tpm2_get_timeouts(struct tpm_chip *chip);
 int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
-int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
-		    struct tpm2_digest *digests);
+int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash);
 int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max);
 void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
 			    unsigned int flags);
drivers/char/tpm/tpm2-cmd.c +23 -2
index 07636aa6f1a9..80eb4bb5feef 100644
@@ -223,8 +223,8 @@ struct tpm2_null_auth_area {
  *
  * Return: Same as with tpm_transmit_cmd.
  */
-int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
-		    struct tpm2_digest *digests)
+static int __tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
+			     struct tpm2_digest *digests)
 {
 	struct tpm_buf buf;
 	struct tpm2_null_auth_area auth_area;
@@ -270,6 +270,27 @@ int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
 	return rc;
 }
 
+int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
+{
+	int rc;
+	struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
+	u32 count = 0;
+	int i;
+
+	memset(digest_list, 0, sizeof(digest_list));
+
+	for (i = 0; i < ARRAY_SIZE(chip->active_banks); i++) {
+		if (chip->active_banks[i] == TPM2_ALG_ERROR)
+			break;
+		digest_list[i].alg_id = chip->active_banks[i];
+		memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
+		count++;
+	}
+
+	rc = __tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
+	return rc;
+}
+
 
 #define TPM2_GETRANDOM_IN_SIZE \
 	(sizeof(struct tpm_input_header) + \
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#16 lore raw

Re: [PATCH v2 7/8] tpm: Move pcr extend to tpm2-cmd.c

Quoted 6 lines · Tomas Winkler

Add tpm2_pcr_extend function to tpm2-cmd.c with signature required by tpm-interface.c. It wraps the original open code implementation. The original original tpm2_pcr_extend function is renamed and made static, called only from new tpm2_pcr_extend.

This might concern some of the earlier patches too but please use exact names for functions in the short and long summary i.e.

tpm: migrate all of the PCR extension code to tpm2_pcr_extend()

Check this also for your long descriptions and previous that I gave reviewed-by (I think they were otherwise fine). And you refer to a function in text use parentheses after the name. I don't like hastily written commit messages.

/Jarkko

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#17 lore raw

RE: [PATCH v2 7/8] tpm: Move pcr extend to tpm2-cmd.c

Quoted 9 lines · Tomas Winkler
Quoted 6 lines

Add tpm2_pcr_extend function to tpm2-cmd.c with signature required by tpm-interface.c. It wraps the original open code implementation. The original original tpm2_pcr_extend function is renamed and made static, called only from new tpm2_pcr_extend.

Quoted 8 lines

This might concern some of the earlier patches too but please use exact names for functions in the short and long summary i.e.

tpm: migrate all of the PCR extension code to tpm2_pcr_extend()

Check this also for your long descriptions and previous that I gave reviewed- by (I think they were otherwise fine). And you refer to a function in text use parentheses after the name. I don't like hastily written commit messages.

Yeah, got little tired of describing the obvious. but you are right. Tomas

????{.n?+???????+%?????????w??{.n?+????{??????????v?^?)????w*jg??????????j????G?????? ???j:+v???w?j?m????????w?????f???h?????????

#18 lore raw

Re: [PATCH v2 7/8] tpm: Move pcr extend to tpm2-cmd.c

Quoted 14 lines · Tomas
Quoted 14 lines · Tomas Winkler
Quoted 6 lines

Add tpm2_pcr_extend function to tpm2-cmd.c with signature required by tpm-interface.c. It wraps the original open code implementation. The original original tpm2_pcr_extend function is renamed and made static, called only from new tpm2_pcr_extend.

This might concern some of the earlier patches too but please use exact names for functions in the short and long summary i.e.

tpm: migrate all of the PCR extension code to tpm2_pcr_extend()

Quoted 7 lines
Quoted 3 lines

Check this also for your long descriptions and previous that I gave reviewed- by (I think they were otherwise fine). And you refer to a function in text use parentheses after the name. I don't like hastily written commit messages.

Yeah, got little tired of describing the obvious. but you are right. Tomas

Might sound nit picking but a solid git log is useful tool.

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#19 lore raw

PATCH v2 8/8 tpm: move tpm2 code validation to tpm2-space.c

In reply to Tomas Winkler

Add new function tpm2_validate_command to tpm2-space.c that wraps up open coded functionality from tpm_validate_command.

---
 drivers/char/tpm/tpm-interface.c | 31 ++-----------------------------
 drivers/char/tpm/tpm.h           |  1 +
 drivers/char/tpm/tpm2-cmd.c      |  1 +
 drivers/char/tpm/tpm2-space.c    | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 36 insertions(+), 29 deletions(-)
drivers/char/tpm/tpm-interface.c +2 -29
index d347ad8325c6..93f7c12d4c4d 100644
@@ -52,40 +52,13 @@ static int tpm_validate_command(struct tpm_chip *chip,
 				 const u8 *cmd,
 				 size_t len)
 {
-	const struct tpm_input_header *header = (const void *)cmd;
-	int i;
-	u32 cc;
-	u32 attrs;
-	unsigned int nr_handles;
-
 	if (len < TPM_HEADER_SIZE)
 		return -EINVAL;
 
-	if (!space)
-		return 0;
-
-	if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) {
-		cc = be32_to_cpu(header->ordinal);
-
-		i = tpm2_find_cc(chip, cc);
-		if (i < 0) {
-			dev_dbg(&chip->dev, "0x%04X is an invalid command\n",
-				cc);
-			return -EOPNOTSUPP;
-		}
-
-		attrs = chip->cc_attrs_tbl[i];
-		nr_handles =
-			4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0));
-		if (len < TPM_HEADER_SIZE + 4 * nr_handles)
-			goto err_len;
-	}
+	if (chip->flags & TPM_CHIP_FLAG_TPM2 && space)
+		return tpm2_validate_command(chip, cmd, len);
 
 	return 0;
-err_len:
-	dev_dbg(&chip->dev,
-		"%s: insufficient command length %zu", __func__, len);
-	return -EINVAL;
 }
 
 static int tpm_request_locality(struct tpm_chip *chip)
drivers/char/tpm/tpm.h +1 -0
index 2b88aadc4743..15453a78a0d0 100644
@@ -602,6 +602,7 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u32 cc,
 		       u8 *cmd);
 int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
 		      u32 cc, u8 *buf, size_t *bufsiz);
+int tpm2_validate_command(struct tpm_chip *chip, const u8 *cmd, size_t len);
 
 extern const struct seq_operations tpm2_binary_b_measurements_seqops;
 
drivers/char/tpm/tpm2-cmd.c +1 -0
index 80eb4bb5feef..6db13cf801b4 100644
@@ -1112,3 +1112,4 @@ int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
 
 	return -1;
 }
+
drivers/char/tpm/tpm2-space.c +32 -0
index 4e4014eabdb9..ed9eff948c05 100644
@@ -263,6 +263,38 @@ static int tpm2_map_command(struct tpm_chip *chip, u32 cc, u8 *cmd)
 	return 0;
 }
 
+int tpm2_validate_command(struct tpm_chip *chip, const u8 *cmd, size_t len)
+{
+	int i;
+	u32 cc;
+	u32 attrs;
+	unsigned int nr_handles;
+
+	if (len < TPM_HEADER_SIZE)
+		return -EINVAL;
+
+	if (!chip->nr_commands)
+		return 0;
+
+	cc =  be32_to_cpup((__be32 *)(cmd + 6));
+
+	i = tpm2_find_cc(chip, cc);
+	if (i < 0) {
+		dev_dbg(&chip->dev, "0x%04X is an invalid command\n", cc);
+		return -EOPNOTSUPP;
+	}
+
+	attrs = chip->cc_attrs_tbl[i];
+	nr_handles = 4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0));
+	if (len < TPM_HEADER_SIZE + 4 * nr_handles) {
+		dev_dbg(&chip->dev,
+			"%s: insufficient command length %zu", __func__, len);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u32 cc,
 		       u8 *cmd)
 {
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#20 lore raw

Re: [PATCH v2 8/8] tpm: move tpm2 code validation to tpm2-space.c

Quoted 4 lines · Tomas Winkler

Add new function tpm2_validate_command to tpm2-space.c that wraps up open coded functionality from tpm_validate_command.

Same comment as for 7/8. Probably might apply also for 1-6.

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

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

Move TPM1 implementation from tpm-interface.c to tpm1-cmd.c similarly to tpm2-cmd.c, as well as move TPM2 open code flows into new functions to tpm2-cmd.c
The functions in tpm-interface.c should now be in form:

In which patch tpm1-cmd.c is created?

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#22 lore raw
Quoted 6 lines · Tomas Winkler
Quoted 3 lines

Move TPM1 implementation from tpm-interface.c to tpm1-cmd.c similarly to tpm2-cmd.c, as well as move TPM2 open code flows into new functions to tpm2-cmd.c The functions in tpm-interface.c should now be in form:

In which patch tpm1-cmd.c is created?

tpm: factor out tpm 1.2 duration calculation to tpm1-cmd.c

https://patchwork.kernel.org/patch/10259691/

Quoted 2 lines

/Jarkko

????{.n?+???????+%?????????w??{.n?+????{??????????v?^?)????w*jg??????????j????G?????? ???j:+v???w?j?m????????w?????f???h?????????

#23 lore raw
Quoted 10 lines · Tomas
Quoted 6 lines · Tomas Winkler
Quoted 3 lines

Move TPM1 implementation from tpm-interface.c to tpm1-cmd.c similarly to tpm2-cmd.c, as well as move TPM2 open code flows into new functions to tpm2-cmd.c The functions in tpm-interface.c should now be in form:

In which patch tpm1-cmd.c is created?

tpm: factor out tpm 1.2 duration calculation to tpm1-cmd.c

https://patchwork.kernel.org/patch/10259691/

Please include it to the series.

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html