linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KEYS: trusted: tee: Refactor register SHM usage
@ 2023-08-22 11:29 Sumit Garg
  2023-08-22 12:55 ` Jens Wiklander
  2023-08-22 13:52 ` Jarkko Sakkinen
  0 siblings, 2 replies; 10+ messages in thread
From: Sumit Garg @ 2023-08-22 11:29 UTC (permalink / raw)
  To: linux-integrity, keyrings
  Cc: jarkko, jejb, zohar, jens.wiklander, sudeep.holla, achin.gupta,
	linux-security-module, linux-kernel, Sumit Garg

The OP-TEE driver using the old SMC based ABI permits overlapping shared
buffers, but with the new FF-A based ABI each physical page may only
be registered once.

As the key and blob buffer are allocated adjancently, there is no need
for redundant register shared memory invocation. Also, it is incompatibile
with FF-A based ABI limitation. So refactor register shared memory
implementation to use only single invocation to register both key and blob
buffers.

Fixes: 4615e5a34b95 ("optee: add FF-A support")
Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
---
 security/keys/trusted-keys/trusted_tee.c | 64 ++++++++----------------
 1 file changed, 20 insertions(+), 44 deletions(-)

diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
index ac3e270ade69..aa3d477de6db 100644
--- a/security/keys/trusted-keys/trusted_tee.c
+++ b/security/keys/trusted-keys/trusted_tee.c
@@ -65,24 +65,16 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
 	int ret;
 	struct tee_ioctl_invoke_arg inv_arg;
 	struct tee_param param[4];
-	struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+	struct tee_shm *reg_shm = NULL;
 
 	memset(&inv_arg, 0, sizeof(inv_arg));
 	memset(&param, 0, sizeof(param));
 
-	reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
-						 p->key_len);
-	if (IS_ERR(reg_shm_in)) {
-		dev_err(pvt_data.dev, "key shm register failed\n");
-		return PTR_ERR(reg_shm_in);
-	}
-
-	reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
-						  sizeof(p->blob));
-	if (IS_ERR(reg_shm_out)) {
-		dev_err(pvt_data.dev, "blob shm register failed\n");
-		ret = PTR_ERR(reg_shm_out);
-		goto out;
+	reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
+					      sizeof(p->key) + sizeof(p->blob));
+	if (IS_ERR(reg_shm)) {
+		dev_err(pvt_data.dev, "shm register failed\n");
+		return PTR_ERR(reg_shm);
 	}
 
 	inv_arg.func = TA_CMD_SEAL;
@@ -90,13 +82,13 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
 	inv_arg.num_params = 4;
 
 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
-	param[0].u.memref.shm = reg_shm_in;
+	param[0].u.memref.shm = reg_shm;
 	param[0].u.memref.size = p->key_len;
 	param[0].u.memref.shm_offs = 0;
 	param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
-	param[1].u.memref.shm = reg_shm_out;
+	param[1].u.memref.shm = reg_shm;
 	param[1].u.memref.size = sizeof(p->blob);
-	param[1].u.memref.shm_offs = 0;
+	param[1].u.memref.shm_offs = sizeof(p->key);
 
 	ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
 	if ((ret < 0) || (inv_arg.ret != 0)) {
@@ -107,11 +99,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
 		p->blob_len = param[1].u.memref.size;
 	}
 
-out:
-	if (reg_shm_out)
-		tee_shm_free(reg_shm_out);
-	if (reg_shm_in)
-		tee_shm_free(reg_shm_in);
+	tee_shm_free(reg_shm);
 
 	return ret;
 }
@@ -124,24 +112,16 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
 	int ret;
 	struct tee_ioctl_invoke_arg inv_arg;
 	struct tee_param param[4];
-	struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
+	struct tee_shm *reg_shm = NULL;
 
 	memset(&inv_arg, 0, sizeof(inv_arg));
 	memset(&param, 0, sizeof(param));
 
-	reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
-						 p->blob_len);
-	if (IS_ERR(reg_shm_in)) {
-		dev_err(pvt_data.dev, "blob shm register failed\n");
-		return PTR_ERR(reg_shm_in);
-	}
-
-	reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
-						  sizeof(p->key));
-	if (IS_ERR(reg_shm_out)) {
-		dev_err(pvt_data.dev, "key shm register failed\n");
-		ret = PTR_ERR(reg_shm_out);
-		goto out;
+	reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
+					      sizeof(p->key) + sizeof(p->blob));
+	if (IS_ERR(reg_shm)) {
+		dev_err(pvt_data.dev, "shm register failed\n");
+		return PTR_ERR(reg_shm);
 	}
 
 	inv_arg.func = TA_CMD_UNSEAL;
@@ -149,11 +129,11 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
 	inv_arg.num_params = 4;
 
 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
-	param[0].u.memref.shm = reg_shm_in;
+	param[0].u.memref.shm = reg_shm;
 	param[0].u.memref.size = p->blob_len;
-	param[0].u.memref.shm_offs = 0;
+	param[0].u.memref.shm_offs = sizeof(p->key);
 	param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
-	param[1].u.memref.shm = reg_shm_out;
+	param[1].u.memref.shm = reg_shm;
 	param[1].u.memref.size = sizeof(p->key);
 	param[1].u.memref.shm_offs = 0;
 
@@ -166,11 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
 		p->key_len = param[1].u.memref.size;
 	}
 
-out:
-	if (reg_shm_out)
-		tee_shm_free(reg_shm_out);
-	if (reg_shm_in)
-		tee_shm_free(reg_shm_in);
+	tee_shm_free(reg_shm);
 
 	return ret;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] KEYS: trusted: tee: Refactor register SHM usage
  2023-08-22 11:29 [PATCH] KEYS: trusted: tee: Refactor register SHM usage Sumit Garg
@ 2023-08-22 12:55 ` Jens Wiklander
  2023-08-23  6:55   ` Sumit Garg
  2023-08-22 13:52 ` Jarkko Sakkinen
  1 sibling, 1 reply; 10+ messages in thread
From: Jens Wiklander @ 2023-08-22 12:55 UTC (permalink / raw)
  To: Sumit Garg
  Cc: linux-integrity, keyrings, jarkko, jejb, zohar, sudeep.holla,
	achin.gupta, linux-security-module, linux-kernel

On Tue, Aug 22, 2023 at 04:59:33PM +0530, Sumit Garg wrote:
> The OP-TEE driver using the old SMC based ABI permits overlapping shared
> buffers, but with the new FF-A based ABI each physical page may only
> be registered once.
> 
> As the key and blob buffer are allocated adjancently, there is no need
> for redundant register shared memory invocation. Also, it is incompatibile
> with FF-A based ABI limitation. So refactor register shared memory
> implementation to use only single invocation to register both key and blob
> buffers.
> 
> Fixes: 4615e5a34b95 ("optee: add FF-A support")
> Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
> Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
> ---
>  security/keys/trusted-keys/trusted_tee.c | 64 ++++++++----------------
>  1 file changed, 20 insertions(+), 44 deletions(-)
> 
> diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
> index ac3e270ade69..aa3d477de6db 100644
> --- a/security/keys/trusted-keys/trusted_tee.c
> +++ b/security/keys/trusted-keys/trusted_tee.c
> @@ -65,24 +65,16 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
>  	int ret;
>  	struct tee_ioctl_invoke_arg inv_arg;
>  	struct tee_param param[4];
> -	struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> +	struct tee_shm *reg_shm = NULL;
>  
>  	memset(&inv_arg, 0, sizeof(inv_arg));
>  	memset(&param, 0, sizeof(param));
>  
> -	reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> -						 p->key_len);
> -	if (IS_ERR(reg_shm_in)) {
> -		dev_err(pvt_data.dev, "key shm register failed\n");
> -		return PTR_ERR(reg_shm_in);
> -	}
> -
> -	reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> -						  sizeof(p->blob));
> -	if (IS_ERR(reg_shm_out)) {
> -		dev_err(pvt_data.dev, "blob shm register failed\n");
> -		ret = PTR_ERR(reg_shm_out);
> -		goto out;
> +	reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> +					      sizeof(p->key) + sizeof(p->blob));

This is somewhat fragile. What if struct trusted_key_payload has a small
unexpected change in layout?

Thanks,
Jens

> +	if (IS_ERR(reg_shm)) {
> +		dev_err(pvt_data.dev, "shm register failed\n");
> +		return PTR_ERR(reg_shm);
>  	}
>  
>  	inv_arg.func = TA_CMD_SEAL;
> @@ -90,13 +82,13 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
>  	inv_arg.num_params = 4;
>  
>  	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> -	param[0].u.memref.shm = reg_shm_in;
> +	param[0].u.memref.shm = reg_shm;
>  	param[0].u.memref.size = p->key_len;
>  	param[0].u.memref.shm_offs = 0;
>  	param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> -	param[1].u.memref.shm = reg_shm_out;
> +	param[1].u.memref.shm = reg_shm;
>  	param[1].u.memref.size = sizeof(p->blob);
> -	param[1].u.memref.shm_offs = 0;
> +	param[1].u.memref.shm_offs = sizeof(p->key);
>  
>  	ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
>  	if ((ret < 0) || (inv_arg.ret != 0)) {
> @@ -107,11 +99,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
>  		p->blob_len = param[1].u.memref.size;
>  	}
>  
> -out:
> -	if (reg_shm_out)
> -		tee_shm_free(reg_shm_out);
> -	if (reg_shm_in)
> -		tee_shm_free(reg_shm_in);
> +	tee_shm_free(reg_shm);
>  
>  	return ret;
>  }
> @@ -124,24 +112,16 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
>  	int ret;
>  	struct tee_ioctl_invoke_arg inv_arg;
>  	struct tee_param param[4];
> -	struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> +	struct tee_shm *reg_shm = NULL;
>  
>  	memset(&inv_arg, 0, sizeof(inv_arg));
>  	memset(&param, 0, sizeof(param));
>  
> -	reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> -						 p->blob_len);
> -	if (IS_ERR(reg_shm_in)) {
> -		dev_err(pvt_data.dev, "blob shm register failed\n");
> -		return PTR_ERR(reg_shm_in);
> -	}
> -
> -	reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> -						  sizeof(p->key));
> -	if (IS_ERR(reg_shm_out)) {
> -		dev_err(pvt_data.dev, "key shm register failed\n");
> -		ret = PTR_ERR(reg_shm_out);
> -		goto out;
> +	reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> +					      sizeof(p->key) + sizeof(p->blob));
> +	if (IS_ERR(reg_shm)) {
> +		dev_err(pvt_data.dev, "shm register failed\n");
> +		return PTR_ERR(reg_shm);
>  	}
>  
>  	inv_arg.func = TA_CMD_UNSEAL;
> @@ -149,11 +129,11 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
>  	inv_arg.num_params = 4;
>  
>  	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> -	param[0].u.memref.shm = reg_shm_in;
> +	param[0].u.memref.shm = reg_shm;
>  	param[0].u.memref.size = p->blob_len;
> -	param[0].u.memref.shm_offs = 0;
> +	param[0].u.memref.shm_offs = sizeof(p->key);
>  	param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> -	param[1].u.memref.shm = reg_shm_out;
> +	param[1].u.memref.shm = reg_shm;
>  	param[1].u.memref.size = sizeof(p->key);
>  	param[1].u.memref.shm_offs = 0;
>  
> @@ -166,11 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
>  		p->key_len = param[1].u.memref.size;
>  	}
>  
> -out:
> -	if (reg_shm_out)
> -		tee_shm_free(reg_shm_out);
> -	if (reg_shm_in)
> -		tee_shm_free(reg_shm_in);
> +	tee_shm_free(reg_shm);
>  
>  	return ret;
>  }
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] KEYS: trusted: tee: Refactor register SHM usage
  2023-08-22 11:29 [PATCH] KEYS: trusted: tee: Refactor register SHM usage Sumit Garg
  2023-08-22 12:55 ` Jens Wiklander
@ 2023-08-22 13:52 ` Jarkko Sakkinen
  2023-08-23  6:57   ` Sumit Garg
  1 sibling, 1 reply; 10+ messages in thread
From: Jarkko Sakkinen @ 2023-08-22 13:52 UTC (permalink / raw)
  To: Sumit Garg, linux-integrity, keyrings
  Cc: jejb, zohar, jens.wiklander, sudeep.holla, achin.gupta,
	linux-security-module, linux-kernel

On Tue Aug 22, 2023 at 2:29 PM EEST, Sumit Garg wrote:
> The OP-TEE driver using the old SMC based ABI permits overlapping shared
> buffers, but with the new FF-A based ABI each physical page may only
> be registered once.
>
> As the key and blob buffer are allocated adjancently, there is no need
> for redundant register shared memory invocation. Also, it is incompatibile
> with FF-A based ABI limitation. So refactor register shared memory
> implementation to use only single invocation to register both key and blob
> buffers.
>
> Fixes: 4615e5a34b95 ("optee: add FF-A support")
> Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
> Signed-off-by: Sumit Garg <sumit.garg@linaro.org>

Does this retain backwards compatibility?

> ---
>  security/keys/trusted-keys/trusted_tee.c | 64 ++++++++----------------
>  1 file changed, 20 insertions(+), 44 deletions(-)
>
> diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
> index ac3e270ade69..aa3d477de6db 100644
> --- a/security/keys/trusted-keys/trusted_tee.c
> +++ b/security/keys/trusted-keys/trusted_tee.c
> @@ -65,24 +65,16 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
>  	int ret;
>  	struct tee_ioctl_invoke_arg inv_arg;
>  	struct tee_param param[4];
> -	struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> +	struct tee_shm *reg_shm = NULL;
>  
>  	memset(&inv_arg, 0, sizeof(inv_arg));
>  	memset(&param, 0, sizeof(param));
>  
> -	reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> -						 p->key_len);
> -	if (IS_ERR(reg_shm_in)) {
> -		dev_err(pvt_data.dev, "key shm register failed\n");
> -		return PTR_ERR(reg_shm_in);
> -	}
> -
> -	reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> -						  sizeof(p->blob));
> -	if (IS_ERR(reg_shm_out)) {
> -		dev_err(pvt_data.dev, "blob shm register failed\n");
> -		ret = PTR_ERR(reg_shm_out);
> -		goto out;
> +	reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> +					      sizeof(p->key) + sizeof(p->blob));
> +	if (IS_ERR(reg_shm)) {
> +		dev_err(pvt_data.dev, "shm register failed\n");
> +		return PTR_ERR(reg_shm);
>  	}
>  
>  	inv_arg.func = TA_CMD_SEAL;
> @@ -90,13 +82,13 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
>  	inv_arg.num_params = 4;
>  
>  	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> -	param[0].u.memref.shm = reg_shm_in;
> +	param[0].u.memref.shm = reg_shm;
>  	param[0].u.memref.size = p->key_len;
>  	param[0].u.memref.shm_offs = 0;
>  	param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> -	param[1].u.memref.shm = reg_shm_out;
> +	param[1].u.memref.shm = reg_shm;
>  	param[1].u.memref.size = sizeof(p->blob);
> -	param[1].u.memref.shm_offs = 0;
> +	param[1].u.memref.shm_offs = sizeof(p->key);
>  
>  	ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
>  	if ((ret < 0) || (inv_arg.ret != 0)) {
> @@ -107,11 +99,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
>  		p->blob_len = param[1].u.memref.size;
>  	}
>  
> -out:
> -	if (reg_shm_out)
> -		tee_shm_free(reg_shm_out);
> -	if (reg_shm_in)
> -		tee_shm_free(reg_shm_in);
> +	tee_shm_free(reg_shm);
>  
>  	return ret;
>  }
> @@ -124,24 +112,16 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
>  	int ret;
>  	struct tee_ioctl_invoke_arg inv_arg;
>  	struct tee_param param[4];
> -	struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> +	struct tee_shm *reg_shm = NULL;
>  
>  	memset(&inv_arg, 0, sizeof(inv_arg));
>  	memset(&param, 0, sizeof(param));
>  
> -	reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> -						 p->blob_len);
> -	if (IS_ERR(reg_shm_in)) {
> -		dev_err(pvt_data.dev, "blob shm register failed\n");
> -		return PTR_ERR(reg_shm_in);
> -	}
> -
> -	reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> -						  sizeof(p->key));
> -	if (IS_ERR(reg_shm_out)) {
> -		dev_err(pvt_data.dev, "key shm register failed\n");
> -		ret = PTR_ERR(reg_shm_out);
> -		goto out;
> +	reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> +					      sizeof(p->key) + sizeof(p->blob));
> +	if (IS_ERR(reg_shm)) {
> +		dev_err(pvt_data.dev, "shm register failed\n");
> +		return PTR_ERR(reg_shm);
>  	}
>  
>  	inv_arg.func = TA_CMD_UNSEAL;
> @@ -149,11 +129,11 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
>  	inv_arg.num_params = 4;
>  
>  	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> -	param[0].u.memref.shm = reg_shm_in;
> +	param[0].u.memref.shm = reg_shm;
>  	param[0].u.memref.size = p->blob_len;
> -	param[0].u.memref.shm_offs = 0;
> +	param[0].u.memref.shm_offs = sizeof(p->key);
>  	param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> -	param[1].u.memref.shm = reg_shm_out;
> +	param[1].u.memref.shm = reg_shm;
>  	param[1].u.memref.size = sizeof(p->key);
>  	param[1].u.memref.shm_offs = 0;
>  
> @@ -166,11 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
>  		p->key_len = param[1].u.memref.size;
>  	}
>  
> -out:
> -	if (reg_shm_out)
> -		tee_shm_free(reg_shm_out);
> -	if (reg_shm_in)
> -		tee_shm_free(reg_shm_in);
> +	tee_shm_free(reg_shm);
>  
>  	return ret;
>  }
> -- 
> 2.34.1

BR, Jarkko

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] KEYS: trusted: tee: Refactor register SHM usage
  2023-08-22 12:55 ` Jens Wiklander
@ 2023-08-23  6:55   ` Sumit Garg
  2023-08-23  8:01     ` Jens Wiklander
  0 siblings, 1 reply; 10+ messages in thread
From: Sumit Garg @ 2023-08-23  6:55 UTC (permalink / raw)
  To: Jens Wiklander
  Cc: linux-integrity, keyrings, jarkko, jejb, zohar, sudeep.holla,
	achin.gupta, linux-security-module, linux-kernel

On Tue, 22 Aug 2023 at 18:25, Jens Wiklander <jens.wiklander@linaro.org> wrote:
>
> On Tue, Aug 22, 2023 at 04:59:33PM +0530, Sumit Garg wrote:
> > The OP-TEE driver using the old SMC based ABI permits overlapping shared
> > buffers, but with the new FF-A based ABI each physical page may only
> > be registered once.
> >
> > As the key and blob buffer are allocated adjancently, there is no need
> > for redundant register shared memory invocation. Also, it is incompatibile
> > with FF-A based ABI limitation. So refactor register shared memory
> > implementation to use only single invocation to register both key and blob
> > buffers.
> >
> > Fixes: 4615e5a34b95 ("optee: add FF-A support")
> > Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
> > Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
> > ---
> >  security/keys/trusted-keys/trusted_tee.c | 64 ++++++++----------------
> >  1 file changed, 20 insertions(+), 44 deletions(-)
> >
> > diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
> > index ac3e270ade69..aa3d477de6db 100644
> > --- a/security/keys/trusted-keys/trusted_tee.c
> > +++ b/security/keys/trusted-keys/trusted_tee.c
> > @@ -65,24 +65,16 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> >       int ret;
> >       struct tee_ioctl_invoke_arg inv_arg;
> >       struct tee_param param[4];
> > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > +     struct tee_shm *reg_shm = NULL;
> >
> >       memset(&inv_arg, 0, sizeof(inv_arg));
> >       memset(&param, 0, sizeof(param));
> >
> > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > -                                              p->key_len);
> > -     if (IS_ERR(reg_shm_in)) {
> > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > -             return PTR_ERR(reg_shm_in);
> > -     }
> > -
> > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > -                                               sizeof(p->blob));
> > -     if (IS_ERR(reg_shm_out)) {
> > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > -             ret = PTR_ERR(reg_shm_out);
> > -             goto out;
> > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > +                                           sizeof(p->key) + sizeof(p->blob));
>
> This is somewhat fragile. What if struct trusted_key_payload has a small
> unexpected change in layout?

key and blob buffers are just two adjacent fixed sized byte arrays. So
I am not worried here as long as they stay adjacent (which has been
the case since trusted keys were introduced in the kernel).

-Sumit

>
> Thanks,
> Jens
>
> > +     if (IS_ERR(reg_shm)) {
> > +             dev_err(pvt_data.dev, "shm register failed\n");
> > +             return PTR_ERR(reg_shm);
> >       }
> >
> >       inv_arg.func = TA_CMD_SEAL;
> > @@ -90,13 +82,13 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> >       inv_arg.num_params = 4;
> >
> >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > -     param[0].u.memref.shm = reg_shm_in;
> > +     param[0].u.memref.shm = reg_shm;
> >       param[0].u.memref.size = p->key_len;
> >       param[0].u.memref.shm_offs = 0;
> >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > -     param[1].u.memref.shm = reg_shm_out;
> > +     param[1].u.memref.shm = reg_shm;
> >       param[1].u.memref.size = sizeof(p->blob);
> > -     param[1].u.memref.shm_offs = 0;
> > +     param[1].u.memref.shm_offs = sizeof(p->key);
> >
> >       ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
> >       if ((ret < 0) || (inv_arg.ret != 0)) {
> > @@ -107,11 +99,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> >               p->blob_len = param[1].u.memref.size;
> >       }
> >
> > -out:
> > -     if (reg_shm_out)
> > -             tee_shm_free(reg_shm_out);
> > -     if (reg_shm_in)
> > -             tee_shm_free(reg_shm_in);
> > +     tee_shm_free(reg_shm);
> >
> >       return ret;
> >  }
> > @@ -124,24 +112,16 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> >       int ret;
> >       struct tee_ioctl_invoke_arg inv_arg;
> >       struct tee_param param[4];
> > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > +     struct tee_shm *reg_shm = NULL;
> >
> >       memset(&inv_arg, 0, sizeof(inv_arg));
> >       memset(&param, 0, sizeof(param));
> >
> > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > -                                              p->blob_len);
> > -     if (IS_ERR(reg_shm_in)) {
> > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > -             return PTR_ERR(reg_shm_in);
> > -     }
> > -
> > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > -                                               sizeof(p->key));
> > -     if (IS_ERR(reg_shm_out)) {
> > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > -             ret = PTR_ERR(reg_shm_out);
> > -             goto out;
> > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > +                                           sizeof(p->key) + sizeof(p->blob));
> > +     if (IS_ERR(reg_shm)) {
> > +             dev_err(pvt_data.dev, "shm register failed\n");
> > +             return PTR_ERR(reg_shm);
> >       }
> >
> >       inv_arg.func = TA_CMD_UNSEAL;
> > @@ -149,11 +129,11 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> >       inv_arg.num_params = 4;
> >
> >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > -     param[0].u.memref.shm = reg_shm_in;
> > +     param[0].u.memref.shm = reg_shm;
> >       param[0].u.memref.size = p->blob_len;
> > -     param[0].u.memref.shm_offs = 0;
> > +     param[0].u.memref.shm_offs = sizeof(p->key);
> >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > -     param[1].u.memref.shm = reg_shm_out;
> > +     param[1].u.memref.shm = reg_shm;
> >       param[1].u.memref.size = sizeof(p->key);
> >       param[1].u.memref.shm_offs = 0;
> >
> > @@ -166,11 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> >               p->key_len = param[1].u.memref.size;
> >       }
> >
> > -out:
> > -     if (reg_shm_out)
> > -             tee_shm_free(reg_shm_out);
> > -     if (reg_shm_in)
> > -             tee_shm_free(reg_shm_in);
> > +     tee_shm_free(reg_shm);
> >
> >       return ret;
> >  }
> > --
> > 2.34.1
> >

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] KEYS: trusted: tee: Refactor register SHM usage
  2023-08-22 13:52 ` Jarkko Sakkinen
@ 2023-08-23  6:57   ` Sumit Garg
  0 siblings, 0 replies; 10+ messages in thread
From: Sumit Garg @ 2023-08-23  6:57 UTC (permalink / raw)
  To: Jarkko Sakkinen
  Cc: linux-integrity, keyrings, jejb, zohar, jens.wiklander,
	sudeep.holla, achin.gupta, linux-security-module, linux-kernel

On Tue, 22 Aug 2023 at 19:22, Jarkko Sakkinen <jarkko@kernel.org> wrote:
>
> On Tue Aug 22, 2023 at 2:29 PM EEST, Sumit Garg wrote:
> > The OP-TEE driver using the old SMC based ABI permits overlapping shared
> > buffers, but with the new FF-A based ABI each physical page may only
> > be registered once.
> >
> > As the key and blob buffer are allocated adjancently, there is no need
> > for redundant register shared memory invocation. Also, it is incompatibile
> > with FF-A based ABI limitation. So refactor register shared memory
> > implementation to use only single invocation to register both key and blob
> > buffers.
> >
> > Fixes: 4615e5a34b95 ("optee: add FF-A support")
> > Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
> > Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
>
> Does this retain backwards compatibility?
>

Yeah it does retain backwards compatibility since there aren't any
changes needed on OP-TEE Trusted Keys TA end.

-Sumit

> > ---
> >  security/keys/trusted-keys/trusted_tee.c | 64 ++++++++----------------
> >  1 file changed, 20 insertions(+), 44 deletions(-)
> >
> > diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
> > index ac3e270ade69..aa3d477de6db 100644
> > --- a/security/keys/trusted-keys/trusted_tee.c
> > +++ b/security/keys/trusted-keys/trusted_tee.c
> > @@ -65,24 +65,16 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> >       int ret;
> >       struct tee_ioctl_invoke_arg inv_arg;
> >       struct tee_param param[4];
> > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > +     struct tee_shm *reg_shm = NULL;
> >
> >       memset(&inv_arg, 0, sizeof(inv_arg));
> >       memset(&param, 0, sizeof(param));
> >
> > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > -                                              p->key_len);
> > -     if (IS_ERR(reg_shm_in)) {
> > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > -             return PTR_ERR(reg_shm_in);
> > -     }
> > -
> > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > -                                               sizeof(p->blob));
> > -     if (IS_ERR(reg_shm_out)) {
> > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > -             ret = PTR_ERR(reg_shm_out);
> > -             goto out;
> > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > +                                           sizeof(p->key) + sizeof(p->blob));
> > +     if (IS_ERR(reg_shm)) {
> > +             dev_err(pvt_data.dev, "shm register failed\n");
> > +             return PTR_ERR(reg_shm);
> >       }
> >
> >       inv_arg.func = TA_CMD_SEAL;
> > @@ -90,13 +82,13 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> >       inv_arg.num_params = 4;
> >
> >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > -     param[0].u.memref.shm = reg_shm_in;
> > +     param[0].u.memref.shm = reg_shm;
> >       param[0].u.memref.size = p->key_len;
> >       param[0].u.memref.shm_offs = 0;
> >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > -     param[1].u.memref.shm = reg_shm_out;
> > +     param[1].u.memref.shm = reg_shm;
> >       param[1].u.memref.size = sizeof(p->blob);
> > -     param[1].u.memref.shm_offs = 0;
> > +     param[1].u.memref.shm_offs = sizeof(p->key);
> >
> >       ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
> >       if ((ret < 0) || (inv_arg.ret != 0)) {
> > @@ -107,11 +99,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> >               p->blob_len = param[1].u.memref.size;
> >       }
> >
> > -out:
> > -     if (reg_shm_out)
> > -             tee_shm_free(reg_shm_out);
> > -     if (reg_shm_in)
> > -             tee_shm_free(reg_shm_in);
> > +     tee_shm_free(reg_shm);
> >
> >       return ret;
> >  }
> > @@ -124,24 +112,16 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> >       int ret;
> >       struct tee_ioctl_invoke_arg inv_arg;
> >       struct tee_param param[4];
> > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > +     struct tee_shm *reg_shm = NULL;
> >
> >       memset(&inv_arg, 0, sizeof(inv_arg));
> >       memset(&param, 0, sizeof(param));
> >
> > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > -                                              p->blob_len);
> > -     if (IS_ERR(reg_shm_in)) {
> > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > -             return PTR_ERR(reg_shm_in);
> > -     }
> > -
> > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > -                                               sizeof(p->key));
> > -     if (IS_ERR(reg_shm_out)) {
> > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > -             ret = PTR_ERR(reg_shm_out);
> > -             goto out;
> > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > +                                           sizeof(p->key) + sizeof(p->blob));
> > +     if (IS_ERR(reg_shm)) {
> > +             dev_err(pvt_data.dev, "shm register failed\n");
> > +             return PTR_ERR(reg_shm);
> >       }
> >
> >       inv_arg.func = TA_CMD_UNSEAL;
> > @@ -149,11 +129,11 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> >       inv_arg.num_params = 4;
> >
> >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > -     param[0].u.memref.shm = reg_shm_in;
> > +     param[0].u.memref.shm = reg_shm;
> >       param[0].u.memref.size = p->blob_len;
> > -     param[0].u.memref.shm_offs = 0;
> > +     param[0].u.memref.shm_offs = sizeof(p->key);
> >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > -     param[1].u.memref.shm = reg_shm_out;
> > +     param[1].u.memref.shm = reg_shm;
> >       param[1].u.memref.size = sizeof(p->key);
> >       param[1].u.memref.shm_offs = 0;
> >
> > @@ -166,11 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> >               p->key_len = param[1].u.memref.size;
> >       }
> >
> > -out:
> > -     if (reg_shm_out)
> > -             tee_shm_free(reg_shm_out);
> > -     if (reg_shm_in)
> > -             tee_shm_free(reg_shm_in);
> > +     tee_shm_free(reg_shm);
> >
> >       return ret;
> >  }
> > --
> > 2.34.1
>
> BR, Jarkko

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] KEYS: trusted: tee: Refactor register SHM usage
  2023-08-23  6:55   ` Sumit Garg
@ 2023-08-23  8:01     ` Jens Wiklander
  2023-08-23 13:04       ` Sumit Garg
  0 siblings, 1 reply; 10+ messages in thread
From: Jens Wiklander @ 2023-08-23  8:01 UTC (permalink / raw)
  To: Sumit Garg
  Cc: linux-integrity, keyrings, jarkko, jejb, zohar, sudeep.holla,
	achin.gupta, linux-security-module, linux-kernel

On Wed, Aug 23, 2023 at 8:55 AM Sumit Garg <sumit.garg@linaro.org> wrote:
>
> On Tue, 22 Aug 2023 at 18:25, Jens Wiklander <jens.wiklander@linaro.org> wrote:
> >
> > On Tue, Aug 22, 2023 at 04:59:33PM +0530, Sumit Garg wrote:
> > > The OP-TEE driver using the old SMC based ABI permits overlapping shared
> > > buffers, but with the new FF-A based ABI each physical page may only
> > > be registered once.
> > >
> > > As the key and blob buffer are allocated adjancently, there is no need
> > > for redundant register shared memory invocation. Also, it is incompatibile
> > > with FF-A based ABI limitation. So refactor register shared memory
> > > implementation to use only single invocation to register both key and blob
> > > buffers.
> > >
> > > Fixes: 4615e5a34b95 ("optee: add FF-A support")
> > > Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
> > > Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
> > > ---
> > >  security/keys/trusted-keys/trusted_tee.c | 64 ++++++++----------------
> > >  1 file changed, 20 insertions(+), 44 deletions(-)
> > >
> > > diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
> > > index ac3e270ade69..aa3d477de6db 100644
> > > --- a/security/keys/trusted-keys/trusted_tee.c
> > > +++ b/security/keys/trusted-keys/trusted_tee.c
> > > @@ -65,24 +65,16 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > >       int ret;
> > >       struct tee_ioctl_invoke_arg inv_arg;
> > >       struct tee_param param[4];
> > > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > > +     struct tee_shm *reg_shm = NULL;
> > >
> > >       memset(&inv_arg, 0, sizeof(inv_arg));
> > >       memset(&param, 0, sizeof(param));
> > >
> > > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > -                                              p->key_len);
> > > -     if (IS_ERR(reg_shm_in)) {
> > > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > > -             return PTR_ERR(reg_shm_in);
> > > -     }
> > > -
> > > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > > -                                               sizeof(p->blob));
> > > -     if (IS_ERR(reg_shm_out)) {
> > > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > > -             ret = PTR_ERR(reg_shm_out);
> > > -             goto out;
> > > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > +                                           sizeof(p->key) + sizeof(p->blob));
> >
> > This is somewhat fragile. What if struct trusted_key_payload has a small
> > unexpected change in layout?
>
> key and blob buffers are just two adjacent fixed sized byte arrays. So
> I am not worried here as long as they stay adjacent (which has been
> the case since trusted keys were introduced in the kernel).

Yeah, that was my point, but fine if you don't believe it's an issue.

Thanks,
Jens

>
> -Sumit
>
> >
> > Thanks,
> > Jens
> >
> > > +     if (IS_ERR(reg_shm)) {
> > > +             dev_err(pvt_data.dev, "shm register failed\n");
> > > +             return PTR_ERR(reg_shm);
> > >       }
> > >
> > >       inv_arg.func = TA_CMD_SEAL;
> > > @@ -90,13 +82,13 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > >       inv_arg.num_params = 4;
> > >
> > >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > > -     param[0].u.memref.shm = reg_shm_in;
> > > +     param[0].u.memref.shm = reg_shm;
> > >       param[0].u.memref.size = p->key_len;
> > >       param[0].u.memref.shm_offs = 0;
> > >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > > -     param[1].u.memref.shm = reg_shm_out;
> > > +     param[1].u.memref.shm = reg_shm;
> > >       param[1].u.memref.size = sizeof(p->blob);
> > > -     param[1].u.memref.shm_offs = 0;
> > > +     param[1].u.memref.shm_offs = sizeof(p->key);
> > >
> > >       ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
> > >       if ((ret < 0) || (inv_arg.ret != 0)) {
> > > @@ -107,11 +99,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > >               p->blob_len = param[1].u.memref.size;
> > >       }
> > >
> > > -out:
> > > -     if (reg_shm_out)
> > > -             tee_shm_free(reg_shm_out);
> > > -     if (reg_shm_in)
> > > -             tee_shm_free(reg_shm_in);
> > > +     tee_shm_free(reg_shm);
> > >
> > >       return ret;
> > >  }
> > > @@ -124,24 +112,16 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > >       int ret;
> > >       struct tee_ioctl_invoke_arg inv_arg;
> > >       struct tee_param param[4];
> > > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > > +     struct tee_shm *reg_shm = NULL;
> > >
> > >       memset(&inv_arg, 0, sizeof(inv_arg));
> > >       memset(&param, 0, sizeof(param));
> > >
> > > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > > -                                              p->blob_len);
> > > -     if (IS_ERR(reg_shm_in)) {
> > > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > > -             return PTR_ERR(reg_shm_in);
> > > -     }
> > > -
> > > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > -                                               sizeof(p->key));
> > > -     if (IS_ERR(reg_shm_out)) {
> > > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > > -             ret = PTR_ERR(reg_shm_out);
> > > -             goto out;
> > > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > +                                           sizeof(p->key) + sizeof(p->blob));
> > > +     if (IS_ERR(reg_shm)) {
> > > +             dev_err(pvt_data.dev, "shm register failed\n");
> > > +             return PTR_ERR(reg_shm);
> > >       }
> > >
> > >       inv_arg.func = TA_CMD_UNSEAL;
> > > @@ -149,11 +129,11 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > >       inv_arg.num_params = 4;
> > >
> > >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > > -     param[0].u.memref.shm = reg_shm_in;
> > > +     param[0].u.memref.shm = reg_shm;
> > >       param[0].u.memref.size = p->blob_len;
> > > -     param[0].u.memref.shm_offs = 0;
> > > +     param[0].u.memref.shm_offs = sizeof(p->key);
> > >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > > -     param[1].u.memref.shm = reg_shm_out;
> > > +     param[1].u.memref.shm = reg_shm;
> > >       param[1].u.memref.size = sizeof(p->key);
> > >       param[1].u.memref.shm_offs = 0;
> > >
> > > @@ -166,11 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > >               p->key_len = param[1].u.memref.size;
> > >       }
> > >
> > > -out:
> > > -     if (reg_shm_out)
> > > -             tee_shm_free(reg_shm_out);
> > > -     if (reg_shm_in)
> > > -             tee_shm_free(reg_shm_in);
> > > +     tee_shm_free(reg_shm);
> > >
> > >       return ret;
> > >  }
> > > --
> > > 2.34.1
> > >

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] KEYS: trusted: tee: Refactor register SHM usage
  2023-08-23  8:01     ` Jens Wiklander
@ 2023-08-23 13:04       ` Sumit Garg
  2023-08-23 14:28         ` Jens Wiklander
  0 siblings, 1 reply; 10+ messages in thread
From: Sumit Garg @ 2023-08-23 13:04 UTC (permalink / raw)
  To: Jens Wiklander
  Cc: linux-integrity, keyrings, jarkko, jejb, zohar, sudeep.holla,
	achin.gupta, linux-security-module, linux-kernel

On Wed, 23 Aug 2023 at 13:32, Jens Wiklander <jens.wiklander@linaro.org> wrote:
>
> On Wed, Aug 23, 2023 at 8:55 AM Sumit Garg <sumit.garg@linaro.org> wrote:
> >
> > On Tue, 22 Aug 2023 at 18:25, Jens Wiklander <jens.wiklander@linaro.org> wrote:
> > >
> > > On Tue, Aug 22, 2023 at 04:59:33PM +0530, Sumit Garg wrote:
> > > > The OP-TEE driver using the old SMC based ABI permits overlapping shared
> > > > buffers, but with the new FF-A based ABI each physical page may only
> > > > be registered once.
> > > >
> > > > As the key and blob buffer are allocated adjancently, there is no need
> > > > for redundant register shared memory invocation. Also, it is incompatibile
> > > > with FF-A based ABI limitation. So refactor register shared memory
> > > > implementation to use only single invocation to register both key and blob
> > > > buffers.
> > > >
> > > > Fixes: 4615e5a34b95 ("optee: add FF-A support")
> > > > Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
> > > > Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
> > > > ---
> > > >  security/keys/trusted-keys/trusted_tee.c | 64 ++++++++----------------
> > > >  1 file changed, 20 insertions(+), 44 deletions(-)
> > > >
> > > > diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
> > > > index ac3e270ade69..aa3d477de6db 100644
> > > > --- a/security/keys/trusted-keys/trusted_tee.c
> > > > +++ b/security/keys/trusted-keys/trusted_tee.c
> > > > @@ -65,24 +65,16 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > > >       int ret;
> > > >       struct tee_ioctl_invoke_arg inv_arg;
> > > >       struct tee_param param[4];
> > > > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > > > +     struct tee_shm *reg_shm = NULL;
> > > >
> > > >       memset(&inv_arg, 0, sizeof(inv_arg));
> > > >       memset(&param, 0, sizeof(param));
> > > >
> > > > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > -                                              p->key_len);
> > > > -     if (IS_ERR(reg_shm_in)) {
> > > > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > > > -             return PTR_ERR(reg_shm_in);
> > > > -     }
> > > > -
> > > > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > > > -                                               sizeof(p->blob));
> > > > -     if (IS_ERR(reg_shm_out)) {
> > > > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > > > -             ret = PTR_ERR(reg_shm_out);
> > > > -             goto out;
> > > > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > +                                           sizeof(p->key) + sizeof(p->blob));
> > >
> > > This is somewhat fragile. What if struct trusted_key_payload has a small
> > > unexpected change in layout?
> >
> > key and blob buffers are just two adjacent fixed sized byte arrays. So
> > I am not worried here as long as they stay adjacent (which has been
> > the case since trusted keys were introduced in the kernel).
>
> Yeah, that was my point, but fine if you don't believe it's an issue.
>

Does it resolve the issue with FFA ABI for you? It would be good to
have your Tested-by tag.

-Sumit

> Thanks,
> Jens
>
> >
> > -Sumit
> >
> > >
> > > Thanks,
> > > Jens
> > >
> > > > +     if (IS_ERR(reg_shm)) {
> > > > +             dev_err(pvt_data.dev, "shm register failed\n");
> > > > +             return PTR_ERR(reg_shm);
> > > >       }
> > > >
> > > >       inv_arg.func = TA_CMD_SEAL;
> > > > @@ -90,13 +82,13 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > > >       inv_arg.num_params = 4;
> > > >
> > > >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > > > -     param[0].u.memref.shm = reg_shm_in;
> > > > +     param[0].u.memref.shm = reg_shm;
> > > >       param[0].u.memref.size = p->key_len;
> > > >       param[0].u.memref.shm_offs = 0;
> > > >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > > > -     param[1].u.memref.shm = reg_shm_out;
> > > > +     param[1].u.memref.shm = reg_shm;
> > > >       param[1].u.memref.size = sizeof(p->blob);
> > > > -     param[1].u.memref.shm_offs = 0;
> > > > +     param[1].u.memref.shm_offs = sizeof(p->key);
> > > >
> > > >       ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
> > > >       if ((ret < 0) || (inv_arg.ret != 0)) {
> > > > @@ -107,11 +99,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > > >               p->blob_len = param[1].u.memref.size;
> > > >       }
> > > >
> > > > -out:
> > > > -     if (reg_shm_out)
> > > > -             tee_shm_free(reg_shm_out);
> > > > -     if (reg_shm_in)
> > > > -             tee_shm_free(reg_shm_in);
> > > > +     tee_shm_free(reg_shm);
> > > >
> > > >       return ret;
> > > >  }
> > > > @@ -124,24 +112,16 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > > >       int ret;
> > > >       struct tee_ioctl_invoke_arg inv_arg;
> > > >       struct tee_param param[4];
> > > > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > > > +     struct tee_shm *reg_shm = NULL;
> > > >
> > > >       memset(&inv_arg, 0, sizeof(inv_arg));
> > > >       memset(&param, 0, sizeof(param));
> > > >
> > > > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > > > -                                              p->blob_len);
> > > > -     if (IS_ERR(reg_shm_in)) {
> > > > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > > > -             return PTR_ERR(reg_shm_in);
> > > > -     }
> > > > -
> > > > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > -                                               sizeof(p->key));
> > > > -     if (IS_ERR(reg_shm_out)) {
> > > > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > > > -             ret = PTR_ERR(reg_shm_out);
> > > > -             goto out;
> > > > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > +                                           sizeof(p->key) + sizeof(p->blob));
> > > > +     if (IS_ERR(reg_shm)) {
> > > > +             dev_err(pvt_data.dev, "shm register failed\n");
> > > > +             return PTR_ERR(reg_shm);
> > > >       }
> > > >
> > > >       inv_arg.func = TA_CMD_UNSEAL;
> > > > @@ -149,11 +129,11 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > > >       inv_arg.num_params = 4;
> > > >
> > > >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > > > -     param[0].u.memref.shm = reg_shm_in;
> > > > +     param[0].u.memref.shm = reg_shm;
> > > >       param[0].u.memref.size = p->blob_len;
> > > > -     param[0].u.memref.shm_offs = 0;
> > > > +     param[0].u.memref.shm_offs = sizeof(p->key);
> > > >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > > > -     param[1].u.memref.shm = reg_shm_out;
> > > > +     param[1].u.memref.shm = reg_shm;
> > > >       param[1].u.memref.size = sizeof(p->key);
> > > >       param[1].u.memref.shm_offs = 0;
> > > >
> > > > @@ -166,11 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > > >               p->key_len = param[1].u.memref.size;
> > > >       }
> > > >
> > > > -out:
> > > > -     if (reg_shm_out)
> > > > -             tee_shm_free(reg_shm_out);
> > > > -     if (reg_shm_in)
> > > > -             tee_shm_free(reg_shm_in);
> > > > +     tee_shm_free(reg_shm);
> > > >
> > > >       return ret;
> > > >  }
> > > > --
> > > > 2.34.1
> > > >

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] KEYS: trusted: tee: Refactor register SHM usage
  2023-08-23 13:04       ` Sumit Garg
@ 2023-08-23 14:28         ` Jens Wiklander
  2023-09-05 11:04           ` Sumit Garg
  0 siblings, 1 reply; 10+ messages in thread
From: Jens Wiklander @ 2023-08-23 14:28 UTC (permalink / raw)
  To: Sumit Garg
  Cc: linux-integrity, keyrings, jarkko, jejb, zohar, sudeep.holla,
	achin.gupta, linux-security-module, linux-kernel

On Wed, Aug 23, 2023 at 3:04 PM Sumit Garg <sumit.garg@linaro.org> wrote:
>
> On Wed, 23 Aug 2023 at 13:32, Jens Wiklander <jens.wiklander@linaro.org> wrote:
> >
> > On Wed, Aug 23, 2023 at 8:55 AM Sumit Garg <sumit.garg@linaro.org> wrote:
> > >
> > > On Tue, 22 Aug 2023 at 18:25, Jens Wiklander <jens.wiklander@linaro.org> wrote:
> > > >
> > > > On Tue, Aug 22, 2023 at 04:59:33PM +0530, Sumit Garg wrote:
> > > > > The OP-TEE driver using the old SMC based ABI permits overlapping shared
> > > > > buffers, but with the new FF-A based ABI each physical page may only
> > > > > be registered once.
> > > > >
> > > > > As the key and blob buffer are allocated adjancently, there is no need
> > > > > for redundant register shared memory invocation. Also, it is incompatibile
> > > > > with FF-A based ABI limitation. So refactor register shared memory
> > > > > implementation to use only single invocation to register both key and blob
> > > > > buffers.
> > > > >
> > > > > Fixes: 4615e5a34b95 ("optee: add FF-A support")
> > > > > Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
> > > > > Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
> > > > > ---
> > > > >  security/keys/trusted-keys/trusted_tee.c | 64 ++++++++----------------
> > > > >  1 file changed, 20 insertions(+), 44 deletions(-)
> > > > >
> > > > > diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
> > > > > index ac3e270ade69..aa3d477de6db 100644
> > > > > --- a/security/keys/trusted-keys/trusted_tee.c
> > > > > +++ b/security/keys/trusted-keys/trusted_tee.c
> > > > > @@ -65,24 +65,16 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > > > >       int ret;
> > > > >       struct tee_ioctl_invoke_arg inv_arg;
> > > > >       struct tee_param param[4];
> > > > > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > > > > +     struct tee_shm *reg_shm = NULL;
> > > > >
> > > > >       memset(&inv_arg, 0, sizeof(inv_arg));
> > > > >       memset(&param, 0, sizeof(param));
> > > > >
> > > > > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > > -                                              p->key_len);
> > > > > -     if (IS_ERR(reg_shm_in)) {
> > > > > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > > > > -             return PTR_ERR(reg_shm_in);
> > > > > -     }
> > > > > -
> > > > > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > > > > -                                               sizeof(p->blob));
> > > > > -     if (IS_ERR(reg_shm_out)) {
> > > > > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > > > > -             ret = PTR_ERR(reg_shm_out);
> > > > > -             goto out;
> > > > > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > > +                                           sizeof(p->key) + sizeof(p->blob));
> > > >
> > > > This is somewhat fragile. What if struct trusted_key_payload has a small
> > > > unexpected change in layout?
> > >
> > > key and blob buffers are just two adjacent fixed sized byte arrays. So
> > > I am not worried here as long as they stay adjacent (which has been
> > > the case since trusted keys were introduced in the kernel).
> >
> > Yeah, that was my point, but fine if you don't believe it's an issue.
> >
>
> Does it resolve the issue with FFA ABI for you? It would be good to
> have your Tested-by tag.

It does:
Tested-by: Jens Wiklander <jens.wiklander@linaro.org>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>

Thanks,
Jens

>
> -Sumit
>
> > Thanks,
> > Jens
> >
> > >
> > > -Sumit
> > >
> > > >
> > > > Thanks,
> > > > Jens
> > > >
> > > > > +     if (IS_ERR(reg_shm)) {
> > > > > +             dev_err(pvt_data.dev, "shm register failed\n");
> > > > > +             return PTR_ERR(reg_shm);
> > > > >       }
> > > > >
> > > > >       inv_arg.func = TA_CMD_SEAL;
> > > > > @@ -90,13 +82,13 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > > > >       inv_arg.num_params = 4;
> > > > >
> > > > >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > > > > -     param[0].u.memref.shm = reg_shm_in;
> > > > > +     param[0].u.memref.shm = reg_shm;
> > > > >       param[0].u.memref.size = p->key_len;
> > > > >       param[0].u.memref.shm_offs = 0;
> > > > >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > > > > -     param[1].u.memref.shm = reg_shm_out;
> > > > > +     param[1].u.memref.shm = reg_shm;
> > > > >       param[1].u.memref.size = sizeof(p->blob);
> > > > > -     param[1].u.memref.shm_offs = 0;
> > > > > +     param[1].u.memref.shm_offs = sizeof(p->key);
> > > > >
> > > > >       ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
> > > > >       if ((ret < 0) || (inv_arg.ret != 0)) {
> > > > > @@ -107,11 +99,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > > > >               p->blob_len = param[1].u.memref.size;
> > > > >       }
> > > > >
> > > > > -out:
> > > > > -     if (reg_shm_out)
> > > > > -             tee_shm_free(reg_shm_out);
> > > > > -     if (reg_shm_in)
> > > > > -             tee_shm_free(reg_shm_in);
> > > > > +     tee_shm_free(reg_shm);
> > > > >
> > > > >       return ret;
> > > > >  }
> > > > > @@ -124,24 +112,16 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > > > >       int ret;
> > > > >       struct tee_ioctl_invoke_arg inv_arg;
> > > > >       struct tee_param param[4];
> > > > > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > > > > +     struct tee_shm *reg_shm = NULL;
> > > > >
> > > > >       memset(&inv_arg, 0, sizeof(inv_arg));
> > > > >       memset(&param, 0, sizeof(param));
> > > > >
> > > > > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > > > > -                                              p->blob_len);
> > > > > -     if (IS_ERR(reg_shm_in)) {
> > > > > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > > > > -             return PTR_ERR(reg_shm_in);
> > > > > -     }
> > > > > -
> > > > > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > > -                                               sizeof(p->key));
> > > > > -     if (IS_ERR(reg_shm_out)) {
> > > > > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > > > > -             ret = PTR_ERR(reg_shm_out);
> > > > > -             goto out;
> > > > > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > > +                                           sizeof(p->key) + sizeof(p->blob));
> > > > > +     if (IS_ERR(reg_shm)) {
> > > > > +             dev_err(pvt_data.dev, "shm register failed\n");
> > > > > +             return PTR_ERR(reg_shm);
> > > > >       }
> > > > >
> > > > >       inv_arg.func = TA_CMD_UNSEAL;
> > > > > @@ -149,11 +129,11 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > > > >       inv_arg.num_params = 4;
> > > > >
> > > > >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > > > > -     param[0].u.memref.shm = reg_shm_in;
> > > > > +     param[0].u.memref.shm = reg_shm;
> > > > >       param[0].u.memref.size = p->blob_len;
> > > > > -     param[0].u.memref.shm_offs = 0;
> > > > > +     param[0].u.memref.shm_offs = sizeof(p->key);
> > > > >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > > > > -     param[1].u.memref.shm = reg_shm_out;
> > > > > +     param[1].u.memref.shm = reg_shm;
> > > > >       param[1].u.memref.size = sizeof(p->key);
> > > > >       param[1].u.memref.shm_offs = 0;
> > > > >
> > > > > @@ -166,11 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > > > >               p->key_len = param[1].u.memref.size;
> > > > >       }
> > > > >
> > > > > -out:
> > > > > -     if (reg_shm_out)
> > > > > -             tee_shm_free(reg_shm_out);
> > > > > -     if (reg_shm_in)
> > > > > -             tee_shm_free(reg_shm_in);
> > > > > +     tee_shm_free(reg_shm);
> > > > >
> > > > >       return ret;
> > > > >  }
> > > > > --
> > > > > 2.34.1
> > > > >

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] KEYS: trusted: tee: Refactor register SHM usage
  2023-08-23 14:28         ` Jens Wiklander
@ 2023-09-05 11:04           ` Sumit Garg
  2023-09-11 10:32             ` Jarkko Sakkinen
  0 siblings, 1 reply; 10+ messages in thread
From: Sumit Garg @ 2023-09-05 11:04 UTC (permalink / raw)
  To: jarkko
  Cc: linux-integrity, keyrings, Jens Wiklander, jejb, zohar,
	sudeep.holla, achin.gupta, linux-security-module, linux-kernel

Hi Jarkko,

On Wed, 23 Aug 2023 at 19:58, Jens Wiklander <jens.wiklander@linaro.org> wrote:
>
> On Wed, Aug 23, 2023 at 3:04 PM Sumit Garg <sumit.garg@linaro.org> wrote:
> >
> > On Wed, 23 Aug 2023 at 13:32, Jens Wiklander <jens.wiklander@linaro.org> wrote:
> > >
> > > On Wed, Aug 23, 2023 at 8:55 AM Sumit Garg <sumit.garg@linaro.org> wrote:
> > > >
> > > > On Tue, 22 Aug 2023 at 18:25, Jens Wiklander <jens.wiklander@linaro.org> wrote:
> > > > >
> > > > > On Tue, Aug 22, 2023 at 04:59:33PM +0530, Sumit Garg wrote:
> > > > > > The OP-TEE driver using the old SMC based ABI permits overlapping shared
> > > > > > buffers, but with the new FF-A based ABI each physical page may only
> > > > > > be registered once.
> > > > > >
> > > > > > As the key and blob buffer are allocated adjancently, there is no need
> > > > > > for redundant register shared memory invocation. Also, it is incompatibile
> > > > > > with FF-A based ABI limitation. So refactor register shared memory
> > > > > > implementation to use only single invocation to register both key and blob
> > > > > > buffers.
> > > > > >
> > > > > > Fixes: 4615e5a34b95 ("optee: add FF-A support")
> > > > > > Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
> > > > > > Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
> > > > > > ---
> > > > > >  security/keys/trusted-keys/trusted_tee.c | 64 ++++++++----------------
> > > > > >  1 file changed, 20 insertions(+), 44 deletions(-)
> > > > > >
> > > > > > diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
> > > > > > index ac3e270ade69..aa3d477de6db 100644
> > > > > > --- a/security/keys/trusted-keys/trusted_tee.c
> > > > > > +++ b/security/keys/trusted-keys/trusted_tee.c
> > > > > > @@ -65,24 +65,16 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > > > > >       int ret;
> > > > > >       struct tee_ioctl_invoke_arg inv_arg;
> > > > > >       struct tee_param param[4];
> > > > > > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > > > > > +     struct tee_shm *reg_shm = NULL;
> > > > > >
> > > > > >       memset(&inv_arg, 0, sizeof(inv_arg));
> > > > > >       memset(&param, 0, sizeof(param));
> > > > > >
> > > > > > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > > > -                                              p->key_len);
> > > > > > -     if (IS_ERR(reg_shm_in)) {
> > > > > > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > > > > > -             return PTR_ERR(reg_shm_in);
> > > > > > -     }
> > > > > > -
> > > > > > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > > > > > -                                               sizeof(p->blob));
> > > > > > -     if (IS_ERR(reg_shm_out)) {
> > > > > > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > > > > > -             ret = PTR_ERR(reg_shm_out);
> > > > > > -             goto out;
> > > > > > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > > > +                                           sizeof(p->key) + sizeof(p->blob));
> > > > >
> > > > > This is somewhat fragile. What if struct trusted_key_payload has a small
> > > > > unexpected change in layout?
> > > >
> > > > key and blob buffers are just two adjacent fixed sized byte arrays. So
> > > > I am not worried here as long as they stay adjacent (which has been
> > > > the case since trusted keys were introduced in the kernel).
> > >
> > > Yeah, that was my point, but fine if you don't believe it's an issue.
> > >
> >
> > Does it resolve the issue with FFA ABI for you? It would be good to
> > have your Tested-by tag.
>
> It does:
> Tested-by: Jens Wiklander <jens.wiklander@linaro.org>
> Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
>

Can you help pick up this fix for v6.6 kernel release?

-Sumit

> Thanks,
> Jens
>
> >
> > -Sumit
> >
> > > Thanks,
> > > Jens
> > >
> > > >
> > > > -Sumit
> > > >
> > > > >
> > > > > Thanks,
> > > > > Jens
> > > > >
> > > > > > +     if (IS_ERR(reg_shm)) {
> > > > > > +             dev_err(pvt_data.dev, "shm register failed\n");
> > > > > > +             return PTR_ERR(reg_shm);
> > > > > >       }
> > > > > >
> > > > > >       inv_arg.func = TA_CMD_SEAL;
> > > > > > @@ -90,13 +82,13 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > > > > >       inv_arg.num_params = 4;
> > > > > >
> > > > > >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > > > > > -     param[0].u.memref.shm = reg_shm_in;
> > > > > > +     param[0].u.memref.shm = reg_shm;
> > > > > >       param[0].u.memref.size = p->key_len;
> > > > > >       param[0].u.memref.shm_offs = 0;
> > > > > >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > > > > > -     param[1].u.memref.shm = reg_shm_out;
> > > > > > +     param[1].u.memref.shm = reg_shm;
> > > > > >       param[1].u.memref.size = sizeof(p->blob);
> > > > > > -     param[1].u.memref.shm_offs = 0;
> > > > > > +     param[1].u.memref.shm_offs = sizeof(p->key);
> > > > > >
> > > > > >       ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
> > > > > >       if ((ret < 0) || (inv_arg.ret != 0)) {
> > > > > > @@ -107,11 +99,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > > > > >               p->blob_len = param[1].u.memref.size;
> > > > > >       }
> > > > > >
> > > > > > -out:
> > > > > > -     if (reg_shm_out)
> > > > > > -             tee_shm_free(reg_shm_out);
> > > > > > -     if (reg_shm_in)
> > > > > > -             tee_shm_free(reg_shm_in);
> > > > > > +     tee_shm_free(reg_shm);
> > > > > >
> > > > > >       return ret;
> > > > > >  }
> > > > > > @@ -124,24 +112,16 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > > > > >       int ret;
> > > > > >       struct tee_ioctl_invoke_arg inv_arg;
> > > > > >       struct tee_param param[4];
> > > > > > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > > > > > +     struct tee_shm *reg_shm = NULL;
> > > > > >
> > > > > >       memset(&inv_arg, 0, sizeof(inv_arg));
> > > > > >       memset(&param, 0, sizeof(param));
> > > > > >
> > > > > > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > > > > > -                                              p->blob_len);
> > > > > > -     if (IS_ERR(reg_shm_in)) {
> > > > > > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > > > > > -             return PTR_ERR(reg_shm_in);
> > > > > > -     }
> > > > > > -
> > > > > > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > > > -                                               sizeof(p->key));
> > > > > > -     if (IS_ERR(reg_shm_out)) {
> > > > > > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > > > > > -             ret = PTR_ERR(reg_shm_out);
> > > > > > -             goto out;
> > > > > > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > > > +                                           sizeof(p->key) + sizeof(p->blob));
> > > > > > +     if (IS_ERR(reg_shm)) {
> > > > > > +             dev_err(pvt_data.dev, "shm register failed\n");
> > > > > > +             return PTR_ERR(reg_shm);
> > > > > >       }
> > > > > >
> > > > > >       inv_arg.func = TA_CMD_UNSEAL;
> > > > > > @@ -149,11 +129,11 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > > > > >       inv_arg.num_params = 4;
> > > > > >
> > > > > >       param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
> > > > > > -     param[0].u.memref.shm = reg_shm_in;
> > > > > > +     param[0].u.memref.shm = reg_shm;
> > > > > >       param[0].u.memref.size = p->blob_len;
> > > > > > -     param[0].u.memref.shm_offs = 0;
> > > > > > +     param[0].u.memref.shm_offs = sizeof(p->key);
> > > > > >       param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
> > > > > > -     param[1].u.memref.shm = reg_shm_out;
> > > > > > +     param[1].u.memref.shm = reg_shm;
> > > > > >       param[1].u.memref.size = sizeof(p->key);
> > > > > >       param[1].u.memref.shm_offs = 0;
> > > > > >
> > > > > > @@ -166,11 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)
> > > > > >               p->key_len = param[1].u.memref.size;
> > > > > >       }
> > > > > >
> > > > > > -out:
> > > > > > -     if (reg_shm_out)
> > > > > > -             tee_shm_free(reg_shm_out);
> > > > > > -     if (reg_shm_in)
> > > > > > -             tee_shm_free(reg_shm_in);
> > > > > > +     tee_shm_free(reg_shm);
> > > > > >
> > > > > >       return ret;
> > > > > >  }
> > > > > > --
> > > > > > 2.34.1
> > > > > >

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] KEYS: trusted: tee: Refactor register SHM usage
  2023-09-05 11:04           ` Sumit Garg
@ 2023-09-11 10:32             ` Jarkko Sakkinen
  0 siblings, 0 replies; 10+ messages in thread
From: Jarkko Sakkinen @ 2023-09-11 10:32 UTC (permalink / raw)
  To: Sumit Garg
  Cc: linux-integrity, keyrings, Jens Wiklander, jejb, zohar,
	sudeep.holla, achin.gupta, linux-security-module, linux-kernel

On Tue Sep 5, 2023 at 2:04 PM EEST, Sumit Garg wrote:
> Hi Jarkko,
>
> On Wed, 23 Aug 2023 at 19:58, Jens Wiklander <jens.wiklander@linaro.org> wrote:
> >
> > On Wed, Aug 23, 2023 at 3:04 PM Sumit Garg <sumit.garg@linaro.org> wrote:
> > >
> > > On Wed, 23 Aug 2023 at 13:32, Jens Wiklander <jens.wiklander@linaro.org> wrote:
> > > >
> > > > On Wed, Aug 23, 2023 at 8:55 AM Sumit Garg <sumit.garg@linaro.org> wrote:
> > > > >
> > > > > On Tue, 22 Aug 2023 at 18:25, Jens Wiklander <jens.wiklander@linaro.org> wrote:
> > > > > >
> > > > > > On Tue, Aug 22, 2023 at 04:59:33PM +0530, Sumit Garg wrote:
> > > > > > > The OP-TEE driver using the old SMC based ABI permits overlapping shared
> > > > > > > buffers, but with the new FF-A based ABI each physical page may only
> > > > > > > be registered once.
> > > > > > >
> > > > > > > As the key and blob buffer are allocated adjancently, there is no need
> > > > > > > for redundant register shared memory invocation. Also, it is incompatibile
> > > > > > > with FF-A based ABI limitation. So refactor register shared memory
> > > > > > > implementation to use only single invocation to register both key and blob
> > > > > > > buffers.
> > > > > > >
> > > > > > > Fixes: 4615e5a34b95 ("optee: add FF-A support")
> > > > > > > Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
> > > > > > > Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
> > > > > > > ---
> > > > > > >  security/keys/trusted-keys/trusted_tee.c | 64 ++++++++----------------
> > > > > > >  1 file changed, 20 insertions(+), 44 deletions(-)
> > > > > > >
> > > > > > > diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
> > > > > > > index ac3e270ade69..aa3d477de6db 100644
> > > > > > > --- a/security/keys/trusted-keys/trusted_tee.c
> > > > > > > +++ b/security/keys/trusted-keys/trusted_tee.c
> > > > > > > @@ -65,24 +65,16 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)
> > > > > > >       int ret;
> > > > > > >       struct tee_ioctl_invoke_arg inv_arg;
> > > > > > >       struct tee_param param[4];
> > > > > > > -     struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL;
> > > > > > > +     struct tee_shm *reg_shm = NULL;
> > > > > > >
> > > > > > >       memset(&inv_arg, 0, sizeof(inv_arg));
> > > > > > >       memset(&param, 0, sizeof(param));
> > > > > > >
> > > > > > > -     reg_shm_in = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > > > > -                                              p->key_len);
> > > > > > > -     if (IS_ERR(reg_shm_in)) {
> > > > > > > -             dev_err(pvt_data.dev, "key shm register failed\n");
> > > > > > > -             return PTR_ERR(reg_shm_in);
> > > > > > > -     }
> > > > > > > -
> > > > > > > -     reg_shm_out = tee_shm_register_kernel_buf(pvt_data.ctx, p->blob,
> > > > > > > -                                               sizeof(p->blob));
> > > > > > > -     if (IS_ERR(reg_shm_out)) {
> > > > > > > -             dev_err(pvt_data.dev, "blob shm register failed\n");
> > > > > > > -             ret = PTR_ERR(reg_shm_out);
> > > > > > > -             goto out;
> > > > > > > +     reg_shm = tee_shm_register_kernel_buf(pvt_data.ctx, p->key,
> > > > > > > +                                           sizeof(p->key) + sizeof(p->blob));
> > > > > >
> > > > > > This is somewhat fragile. What if struct trusted_key_payload has a small
> > > > > > unexpected change in layout?
> > > > >
> > > > > key and blob buffers are just two adjacent fixed sized byte arrays. So
> > > > > I am not worried here as long as they stay adjacent (which has been
> > > > > the case since trusted keys were introduced in the kernel).
> > > >
> > > > Yeah, that was my point, but fine if you don't believe it's an issue.
> > > >
> > >
> > > Does it resolve the issue with FFA ABI for you? It would be good to
> > > have your Tested-by tag.
> >
> > It does:
> > Tested-by: Jens Wiklander <jens.wiklander@linaro.org>
> > Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
> >
>
> Can you help pick up this fix for v6.6 kernel release?

I pushed it and also added the missing stable tag:

commit 1037d6ec29cdfaaec5277c194b0278eb0a30c3f8 (HEAD -> master, origin/master, origin/HEAD)
Author: Sumit Garg <sumit.garg@linaro.org>
Date:   Tue Aug 22 16:59:33 2023 +0530

    KEYS: trusted: tee: Refactor register SHM usage

    The OP-TEE driver using the old SMC based ABI permits overlapping shared
    buffers, but with the new FF-A based ABI each physical page may only
    be registered once.

    As the key and blob buffer are allocated adjancently, there is no need
    for redundant register shared memory invocation. Also, it is incompatibile
    with FF-A based ABI limitation. So refactor register shared memory
    implementation to use only single invocation to register both key and blob
    buffers.

    [jarkko: Added cc to stable.]
    Cc: stable@vger.kernel.org # v5.16+
    Fixes: 4615e5a34b95 ("optee: add FF-A support")
    Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
    Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
    Tested-by: Jens Wiklander <jens.wiklander@linaro.org>
    Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
    Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>


BR, Jarkko

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-09-11 22:09 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-22 11:29 [PATCH] KEYS: trusted: tee: Refactor register SHM usage Sumit Garg
2023-08-22 12:55 ` Jens Wiklander
2023-08-23  6:55   ` Sumit Garg
2023-08-23  8:01     ` Jens Wiklander
2023-08-23 13:04       ` Sumit Garg
2023-08-23 14:28         ` Jens Wiklander
2023-09-05 11:04           ` Sumit Garg
2023-09-11 10:32             ` Jarkko Sakkinen
2023-08-22 13:52 ` Jarkko Sakkinen
2023-08-23  6:57   ` Sumit Garg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).