Skip to contentSkip to conversation

cmd: optee_rpmb: make it usable by scripts

PATCH New Rasmus Villemoes 1 participant 6 messages Sep 21 TEE

lore
Share & tools
lore
#0 lore raw

v3 cover cmd: optee_rpmb: make it usable by scripts

This all started by wanting to be able to fetch a "persistent value" from RPMB and use that in boot logic. I noticed that the "optee_rpmb read_pvalue" subcommand had two flaws in that regard: No way to actually store the result in e.g. an environment variable, and having to know and provide a "large enough" size of the value.

For the former, it's just a matter of letting the caller provide the name of an environment variable to store the result to in lieu of printing it.

For the latter, the tee side should have provided a way to know if a too small buffer was passed, but that was never actually implemented, and instead a silently truncated value would be returned. That has been fixed by adding a new variant of the TA_AVB_CMD_READ_PERSIST_VALUE method which does yield an error along with the proper size to allocate:
https://github.com/OP-TEE/optee_os/pull/7959 . That is now merged, and its time to update the U-Boot side accordingly.

v3:

- Add patch updating the sandbox tee implementation (3/5) to match the semantics of the real optee-os.

v2: https://lore.kernel.org/u-boot/[email protected]/

- Drop the part touching common/avb_verify.c, now that the old TA_AVB_CMD_READ_PERSIST_VALUE method is not changed. That doesn't mean it shouldn't be updated at some point (the header file promises behaviour which is not actually implemented), but it is out of scope for what I intend to do currently.

- Do make use of the new method in optee_rpmb read_pvalue, and implement the "store result to env var".

v1: https://lore.kernel.org/u-boot/[email protected]/

Rasmus Villemoes (5):
cmd: optee_rpmb: sanitize TEE_ERROR -> E* translations optee_ta_avb.h: add TA_AVB_CMD_READ_PERSIST_VALUE2 define
tee: sandbox: support new TA_AVB_CMD_READ_PERSIST_VALUE2
cmd: optee_rpmb: allocate large enough buffer when reading persistent
value
cmd: optee_rpmb: make it usable by scripts

 cmd/optee_rpmb.c           | 70 ++++++++++++++++++++++++++------------
 drivers/tee/sandbox.c      | 20 +++++++++--
 include/tee/optee_ta_avb.h | 12 +++++++
 3 files changed, 79 insertions(+), 23 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v3 1/5 cmd: optee_rpmb: sanitize TEE_ERROR -> E* translations

Do not translate both OUT_OF_MEMORY and STORAGE_NO_SPACE to -ENOSPC; we have -ENOMEM which is more fitting for the former. OTOH, a new addition to op-tee returns TEE_ERROR_SHORT_BUFFER in case the provided buffer is too small to hold the result, so also use -ENOSPC for that.

Similarly, -ENOENT is a better match for ITEM_NOT_FOUND than "some IO went wrong".

None of the callers currently care about the actual error code, so this makes no functional change, but we will add special handling of the -ENOSPC in a later patch.

---
 cmd/optee_rpmb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
cmd/optee_rpmb.c +3 -1
index b155278ee2a..9a3274c4332 100644
@@ -52,10 +52,12 @@ static int invoke_func(u32 func, ulong num_param, struct tee_param *param)
 	case TEE_SUCCESS:
 		return 0;
 	case TEE_ERROR_OUT_OF_MEMORY:
+		return -ENOMEM;
 	case TEE_ERROR_STORAGE_NO_SPACE:
+	case TEE_ERROR_SHORT_BUFFER:
 		return -ENOSPC;
 	case TEE_ERROR_ITEM_NOT_FOUND:
-		return -EIO;
+		return -ENOENT;
 	case TEE_ERROR_TARGET_DEAD:
 		/*
 		 * The TA has paniced, close the session to reload the TA
Signature · 1 line
2.55.0
#2 lore raw

PATCH v3 2/5 optee_ta_avb.h: add TA_AVB_CMD_READ_PERSIST_VALUE2 define

In reply to Rasmus Villemoes

Add the new TA_AVB_CMD_READ_PERSIST_VALUE2 definition, and synchronize the comment for the existing TA_AVB_CMD_READ_PERSIST_VALUE with optee-os' ta/avb/include/ta_avb.h.

---
 include/tee/optee_ta_avb.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/tee/optee_ta_avb.h +12 -0
index 949875a64cb..d8db6d257bc 100644
@@ -47,6 +47,7 @@
 
 /*
  * Reads a persistent value corresponding to the given name.
+ * Only reads as much of the value as will fit in the provided buffer.
  *
  * in	params[0].u.memref:	persistent value name
  * out	params[1].u.memref:	read persistent value buffer
@@ -61,4 +62,15 @@
  */
 #define TA_AVB_CMD_WRITE_PERSIST_VALUE	5
 
+/*
+ * Reads a persistent value corresponding to the given name.
+ * If the provided buffer is smaller than the value, returns
+ * TEE_ERROR_SHORT_BUFFER and sets the output buffer size to the true
+ * size of the value.
+ *
+ * in	params[0].memref:	persistent value name
+ * out	params[1].memref:	read persistent value buffer
+ */
+#define TA_AVB_CMD_READ_PERSIST_VALUE2	6
+
 #endif /* __TA_AVB_H */
Signature · 1 line
2.55.0
#3 lore raw

PATCH v3 3/5 tee: sandbox: support new TA_AVB_CMD_READ_PERSIST_VALUE2

In reply to Rasmus Villemoes

Upstream optee-os has recently added a new TA_AVB_CMD_READ_PERSIST_VALUE2 method, which fixes a problem with the ABI of the old TA_AVB_CMD_READ_PERSIST_VALUE method: Callers could not know whether the buffer they supplied was big enough, or whether the output was silently truncated. With the new method, TEE_ERROR_SHORT_BUFFER is returned, but with the output size parameter updated to what would be the correct buffer size.

A new method was added instead of modifying the ABI of the existing method, as there might be callers that work with a truncated return value, but which would not be able to cope with an unexpected error return.

Commit 8800cbe9b83 ("tee: sandbox: check for buffer size") partially updated the sandbox version of optee-os to have the semantics that the new method in upstream optee-os now has. It added logic to also return the actual value size, but only in the success case - params[1].u.memref.size was left untouched in the "return TEE_ERROR_SHORT_BUFFER" case.

Align the sandbox implementation with optee-os: Silently return a truncated value for the old method, but do return TEE_ERROR_SHORT_BUFFER for the new method while also updating the output size parameter.

---
 drivers/tee/sandbox.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/tee/sandbox.c +18 -2
index 86b16a3bb8d..16e5d95616c 100644
@@ -248,6 +248,7 @@ static u32 ta_avb_invoke_func(struct udevice *dev, u32 func, uint num_params,
 
 		return TEE_SUCCESS;
 	case TA_AVB_CMD_READ_PERSIST_VALUE:
+	case TA_AVB_CMD_READ_PERSIST_VALUE2:
 		res = check_params(TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
 				   TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
 				   TEE_PARAM_ATTR_TYPE_NONE,
@@ -268,8 +269,23 @@ static u32 ta_avb_invoke_func(struct udevice *dev, u32 func, uint num_params,
 			return TEE_ERROR_ITEM_NOT_FOUND;
 
 		tmp_sz = strlen(ep->data) + 1;
-		if (value_sz < tmp_sz)
-			return TEE_ERROR_SHORT_BUFFER;
+		if (value_sz < tmp_sz) {
+			/*
+			 * For TA_AVB_CMD_READ_PERSIST_VALUE2, reject
+			 * a too short output buffer, but do inform
+			 * the caller of what would be the correct
+			 * size.
+			 */
+			if (func == TA_AVB_CMD_READ_PERSIST_VALUE2) {
+				params[1].u.memref.size = tmp_sz;
+				return TEE_ERROR_SHORT_BUFFER;
+			}
+			/*
+			 * For TA_AVB_CMD_READ_PERSIST_VALUE, truncate
+			 * the value to the given size.
+			 */
+			tmp_sz = value_sz;
+		}
 
 		memcpy(value, ep->data, tmp_sz);
 		params[1].u.memref.size = tmp_sz;
Signature · 1 line
2.55.0
#4 lore raw

PATCH v3 4/5 cmd: optee_rpmb: allocate large enough buffer when reading persistent value

In reply to Rasmus Villemoes

It is implied by the comments in avb_ops.h and the translation of TEE_ERROR_STORAGE_NO_SPACE to AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE done in common/avb_verify.c:invoke_func() that the TA_AVB_CMD_READ_PERSIST_VALUE could return TEE_ERROR_STORAGE_NO_SPACE when the value is longer than the passed buffer size, and that param[1].u.memref.size would be set to the actual size, so that one can allocate an appropriate buffer and re-read.

However, that has AFAICT never been the case; there is no mention of TEE_ERROR_STORAGE_NO_SPACE in the history of ta/avb/ in
https://github.com/OP-TEE/optee_os.git, and what the code does instead is to return a value truncated to the given buffer size. In other words, not only can one not determine the correct buffer size to allocate, one is not even told that truncation happened.

Changing the ABI of the existing TA_AVB_CMD_READ_PERSIST_VALUE method to return an error in the case of a too small buffer was rejected. Instead, a new TA_AVB_CMD_READ_PERSIST_VALUE2 method is implemented which does return an error in case of a too small buffer
<https://github.com/OP-TEE/optee_os/pull/7959>.

Make use of that method, thus making the <bytes> argument to read_pvalue redundant - continue to accept it, but only use it as a hint for the initial size, defaulting to 64.

This obviously requires running against an updated op-tee, but as the optee_rpmb command so far has not been usable programmatically (the values read are only printed to the console), no existing boot logic can have been relying on this command.

---
 cmd/optee_rpmb.c | 43 +++++++++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 16 deletions(-)
cmd/optee_rpmb.c +27 -16
index 9a3274c4332..6f6c70ac084 100644
@@ -72,8 +72,8 @@ static int invoke_func(u32 func, ulong num_param, struct tee_param *param)
 }
 
 static int read_persistent_value(const char *name,
-				 size_t buffer_size,
-				 u8 *out_buffer,
+				 size_t size_hint,
+				 char **out_buffer,
 				 size_t *out_num_bytes_read)
 {
 	int rc = 0;
@@ -81,6 +81,8 @@ static int read_persistent_value(const char *name,
 	struct tee_shm *shm_buf;
 	struct tee_param param[2];
 	size_t name_size = strlen(name) + 1;
+	size_t buffer_size = size_hint;
+	int retry = 1;
 
 	if (!tee)
 		if (avb_ta_open_session())
@@ -93,6 +95,7 @@ static int read_persistent_value(const char *name,
 		goto close_session;
 	}
 
+again:
 	rc = tee_shm_alloc(tee, buffer_size,
 			   TEE_SHM_ALLOC, &shm_buf);
 	if (rc) {
@@ -110,8 +113,16 @@ static int read_persistent_value(const char *name,
 	param[1].u.memref.shm = shm_buf;
 	param[1].u.memref.size = buffer_size;
 
-	rc = invoke_func(TA_AVB_CMD_READ_PERSIST_VALUE,
+	rc = invoke_func(TA_AVB_CMD_READ_PERSIST_VALUE2,
 			 2, param);
+
+	if (rc == -ENOSPC && param[1].u.memref.size > buffer_size && retry) {
+		retry = 0;
+		tee_shm_free(shm_buf);
+		buffer_size = param[1].u.memref.size;
+		goto again;
+	}
+
 	if (rc)
 		goto out;
 
@@ -121,8 +132,9 @@ static int read_persistent_value(const char *name,
 	}
 
 	*out_num_bytes_read = param[1].u.memref.size;
-
-	memcpy(out_buffer, shm_buf->addr, *out_num_bytes_read);
+	*out_buffer = memdup(shm_buf->addr, *out_num_bytes_read);
+	if (!*out_buffer)
+		rc = -ENOMEM;
 
 out:
 	tee_shm_free(shm_buf);
@@ -198,24 +210,23 @@ int do_optee_rpmb_read(struct cmd_tbl *cmdtp, int flag, int argc,
 		       char * const argv[])
 {
 	const char *name;
-	size_t bytes;
 	size_t bytes_read;
-	void *buffer;
+	char *buffer = NULL;
+	size_t bytes = 64; /* Probably enough for most cases to not require two roundtrips. */
 	char *endp;
 
-	if (argc != 3)
+	/* Use a third argument merely as a size hint. */
+	if (argc < 2 || argc > 3)
 		return CMD_RET_USAGE;
 
 	name = argv[1];
-	bytes = dectoul(argv[2], &endp);
-	if (*endp && *endp != '\n')
-		return CMD_RET_USAGE;
-
-	buffer = malloc(bytes);
-	if (!buffer)
-		return CMD_RET_FAILURE;
+	if (argc >= 3) {
+		bytes = dectoul(argv[2], &endp);
+		if (*endp && *endp != '\n')
+			return CMD_RET_USAGE;
+	}
 
-	if (read_persistent_value(name, bytes, buffer, &bytes_read) == 0) {
+	if (read_persistent_value(name, bytes, &buffer, &bytes_read) == 0) {
 		printf("Read %zu bytes, value = %s\n", bytes_read,
 		       (char *)buffer);
 		free(buffer);
Signature · 1 line
2.55.0
#5 lore raw

PATCH v3 5/5 cmd: optee_rpmb: make it usable by scripts

In reply to Rasmus Villemoes

Currently, there is no way to programmatically fetch a persistent value and use the result in the boot logic - the read_pvalue subcommand always just prints the result to the console.

Since the "bytes" argument is now redundant and optional, we can repurpose it: If given, instead of printing the result, the value is stored in the U-Boot environment variable by that name. We do continue to accept a numeric argument (i.e. anything beginning with a digit) and use that as a size hint, but don't document that.

---
 cmd/optee_rpmb.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
cmd/optee_rpmb.c +23 -8
index 6f6c70ac084..06633758673 100644
@@ -7,6 +7,7 @@
 #include <env.h>
 #include <errno.h>
 #include <image.h>
+#include <linux/ctype.h>
 #include <malloc.h>
 #include <mmc.h>
 #include <tee.h>
@@ -213,22 +214,36 @@ int do_optee_rpmb_read(struct cmd_tbl *cmdtp, int flag, int argc,
 	size_t bytes_read;
 	char *buffer = NULL;
 	size_t bytes = 64; /* Probably enough for most cases to not require two roundtrips. */
+	const char *varname = NULL;
 	char *endp;
 
-	/* Use a third argument merely as a size hint. */
 	if (argc < 2 || argc > 3)
 		return CMD_RET_USAGE;
 
 	name = argv[1];
 	if (argc >= 3) {
-		bytes = dectoul(argv[2], &endp);
-		if (*endp && *endp != '\n')
-			return CMD_RET_USAGE;
+		/*
+		 * For backward compatibility, a numerical third
+		 * argument is accepted, but merely treated as a size
+		 * hint. A non-numerical argument is the name of an
+		 * environment variable to store the value into.
+		 */
+		if (isdigit(argv[2][0])) {
+			bytes = dectoul(argv[2], &endp);
+			if (*endp && *endp != '\n')
+				return CMD_RET_USAGE;
+		} else {
+			varname = argv[2];
+		}
 	}
 
 	if (read_persistent_value(name, bytes, &buffer, &bytes_read) == 0) {
-		printf("Read %zu bytes, value = %s\n", bytes_read,
-		       (char *)buffer);
+		if (varname) {
+			env_set(varname, buffer);
+		} else {
+			printf("Read %zu bytes, value = %s\n", bytes_read,
+			       (char *)buffer);
+		}
 		free(buffer);
 		return CMD_RET_SUCCESS;
 	}
@@ -289,7 +304,7 @@ static int do_optee_rpmb(struct cmd_tbl *cmdtp, int flag, int argc,
 
 U_BOOT_CMD (
 	optee_rpmb, 29, 0, do_optee_rpmb,
-	"Provides commands for testing secure storage on RPMB on OPTEE",
-	"read_pvalue <name> <bytes> - read a persistent value <name>\n"
+	"Provides commands for accessing secure storage on RPMB on OPTEE",
+	"read_pvalue <name> [<varname>] - read a persistent value <name> [store it to env var <varname>]\n"
 	"optee_rpmb write_pvalue <name> <value> - write a persistent value <name>\n"
 	);
Signature · 1 line
2.55.0