All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Thirupathaiah Annapureddy <thiruan@linux.microsoft.com>,
	Simon Glass <sjg@chromium.org>
Subject: [PATCH v2 02/10] sandbox: tpm: Tidy up reading and writing of device state
Date: Sun, 18 Jul 2021 14:17:58 -0600	[thread overview]
Message-ID: <20210718201806.761202-3-sjg@chromium.org> (raw)
In-Reply-To: <20210718201806.761202-1-sjg@chromium.org>

At present this code assumes that the TPM data has been read but this may
not be the case. Refactor the code to use a separate pointer so we know
the current state of the data.

Add error checking for the data size.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

(no changes since v1)

 drivers/tpm/tpm_tis_sandbox.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/drivers/tpm/tpm_tis_sandbox.c b/drivers/tpm/tpm_tis_sandbox.c
index 294d98da606..f22ed846f0a 100644
--- a/drivers/tpm/tpm_tis_sandbox.c
+++ b/drivers/tpm/tpm_tis_sandbox.c
@@ -20,7 +20,7 @@
 static struct tpm_state {
 	bool valid;
 	struct nvdata_state nvdata[NV_SEQ_COUNT];
-} g_state;
+} s_state, *g_state;
 
 /**
  * sandbox_tpm_read_state() - read the sandbox EC state from the state file
@@ -33,6 +33,7 @@ static struct tpm_state {
  */
 static int sandbox_tpm_read_state(const void *blob, int node)
 {
+	struct tpm_state *state = &s_state;
 	const char *prop;
 	int len;
 	int i;
@@ -41,22 +42,27 @@ static int sandbox_tpm_read_state(const void *blob, int node)
 		return 0;
 
 	for (i = 0; i < NV_SEQ_COUNT; i++) {
+		struct nvdata_state *nvd = &state->nvdata[i];
 		char prop_name[20];
 
 		sprintf(prop_name, "nvdata%d", i);
 		prop = fdt_getprop(blob, node, prop_name, &len);
-		if (prop && len == NV_DATA_SIZE) {
-			memcpy(g_state.nvdata[i].data, prop, NV_DATA_SIZE);
-			g_state.nvdata[i].present = true;
+		if (len >= NV_DATA_SIZE)
+			return log_msg_ret("nvd", -E2BIG);
+		if (prop) {
+			memcpy(nvd->data, prop, len);
+			nvd->length = len;
+			nvd->present = true;
 		}
 	}
-	g_state.valid = true;
+
+	s_state.valid = true;
 
 	return 0;
 }
 
 /**
- * cros_ec_write_state() - Write out our state to the state file
+ * sandbox_tpm_write_state() - Write out our state to the state file
  *
  * The caller will ensure that there is a node ready for the state. The node
  * may already contain the old state, in which case it is overridden.
@@ -66,20 +72,25 @@ static int sandbox_tpm_read_state(const void *blob, int node)
  */
 static int sandbox_tpm_write_state(void *blob, int node)
 {
+	const struct tpm_state *state = g_state;
 	int i;
 
+	if (!state)
+		return 0;
+
 	/*
 	 * We are guaranteed enough space to write basic properties.
 	 * We could use fdt_add_subnode() to put each set of data in its
 	 * own node - perhaps useful if we add access informaiton to each.
 	 */
 	for (i = 0; i < NV_SEQ_COUNT; i++) {
+		const struct nvdata_state *nvd = &state->nvdata[i];
 		char prop_name[20];
 
-		if (g_state.nvdata[i].present) {
-			sprintf(prop_name, "nvdata%d", i);
-			fdt_setprop(blob, node, prop_name,
-				    g_state.nvdata[i].data, NV_DATA_SIZE);
+		if (nvd->present) {
+			snprintf(prop_name, sizeof(prop_name), "nvdata%d", i);
+			fdt_setprop(blob, node, prop_name, nvd->data,
+				    nvd->length);
 		}
 	}
 
@@ -233,7 +244,9 @@ static int sandbox_tpm_probe(struct udevice *dev)
 {
 	struct tpm_state *tpm = dev_get_priv(dev);
 
-	memcpy(tpm, &g_state, sizeof(*tpm));
+	if (s_state.valid)
+		memcpy(tpm, &s_state, sizeof(*tpm));
+	g_state = tpm;
 
 	return 0;
 }
-- 
2.32.0.402.g57bb445576-goog


  parent reply	other threads:[~2021-07-18 20:18 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-18 20:17 [PATCH v2 00/10] tpm: Enhance sandbox tpm2 emulation Simon Glass
2021-07-18 20:17 ` [PATCH v2 01/10] sandbox: tpm: Split out common nvdata code Simon Glass
2021-07-18 20:17 ` Simon Glass [this message]
2021-07-18 20:17 ` [PATCH v2 03/10] sandbox: tpm: Support the define-space command Simon Glass
2021-07-18 20:18 ` [PATCH v2 04/10] sandbox: tpm: Correct handling of get-capability Simon Glass
2021-07-18 20:18 ` [PATCH v2 05/10] sandbox: tpm: Finish comments for struct sandbox_tpm2 Simon Glass
2021-07-18 20:18 ` [PATCH v2 06/10] sandbox: tpm: Track whether the state is valid Simon Glass
2021-07-18 20:18 ` [PATCH v2 07/10] sandbox: tpm: Support nvdata in TPM2 Simon Glass
2021-07-18 20:18 ` [PATCH v2 08/10] sandbox: tpm: Support storing device state in tpm2 Simon Glass
2021-07-18 20:18 ` [PATCH v2 09/10] sandbox: tpm: Correct handling of SANDBOX_TPM_PCR_NB Simon Glass
2021-07-18 20:18 ` [PATCH v2 10/10] sandbox: tpm: Support extending a PCR multiple times Simon Glass
2021-07-24 21:11 ` Simon Glass
2021-07-24 21:12 ` [PATCH v2 09/10] sandbox: tpm: Correct handling of SANDBOX_TPM_PCR_NB Simon Glass
2021-07-24 21:12 ` [PATCH v2 08/10] sandbox: tpm: Support storing device state in tpm2 Simon Glass
2021-07-24 21:12 ` [PATCH v2 07/10] sandbox: tpm: Support nvdata in TPM2 Simon Glass
2021-07-24 21:12 ` [PATCH v2 06/10] sandbox: tpm: Track whether the state is valid Simon Glass
2021-07-24 21:12 ` [PATCH v2 05/10] sandbox: tpm: Finish comments for struct sandbox_tpm2 Simon Glass
2021-07-24 21:12 ` [PATCH v2 04/10] sandbox: tpm: Correct handling of get-capability Simon Glass
2021-07-24 21:12 ` [PATCH v2 03/10] sandbox: tpm: Support the define-space command Simon Glass
2021-07-24 21:12 ` [PATCH v2 02/10] sandbox: tpm: Tidy up reading and writing of device state Simon Glass
2021-07-24 21:12 ` [PATCH v2 01/10] sandbox: tpm: Split out common nvdata code Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210718201806.761202-3-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=ilias.apalodimas@linaro.org \
    --cc=thiruan@linux.microsoft.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.