OP-TEE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Sumit Garg via OP-TEE <op-tee@lists.trustedfirmware.org>
To: Qihang <q.h.hack.winter@gmail.com>
Cc: op-tee@lists.trustedfirmware.org
Subject: Re: [PATCH] tee: fix missing shm reference cleanup in tee_ioctl_supp_recv
Date: Fri, 1 May 2026 20:01:59 +0530	[thread overview]
Message-ID: <afS5XzaeLUx1ngAl@sumit-xelite> (raw)
In-Reply-To: <20260429113219.88452-1-q.h.hack.winter@gmail.com>

On Wed, Apr 29, 2026 at 07:32:19PM +0800, Qihang wrote:
> params_from_user() acquires tee_shm references for MEMREF parameters and
> expects the caller to release those references with tee_shm_put() during
> cleanup.
> 
> tee_ioctl_open_session(), tee_ioctl_invoke(), and
> tee_ioctl_object_invoke() all do this, but tee_ioctl_supp_recv() only
> frees the parameter array and does not drop any acquired shared-memory
> references.
> 
> Fix this by using a common helper to release MEMREF references before
> freeing the parameter array, and apply it to tee_ioctl_supp_recv() as
> well.
> 
> Since supp_recv backends may update num_params, preserve the original
> allocated parameter count for cleanup.
> 
> Signed-off-by: Qihang <q.h.hack.winter@gmail.com>
> ---
>  drivers/tee/tee_core.c | 49 +++++++++++++++++++-----------------------
>  1 file changed, 22 insertions(+), 27 deletions(-)
> 
> diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
> index ef9642d72672..adad1ea8e31b 100644
> --- a/drivers/tee/tee_core.c
> +++ b/drivers/tee/tee_core.c
> @@ -530,6 +530,21 @@ static int params_to_user(struct tee_ioctl_param __user *uparams,
>  	return 0;
>  }
>  
> +static void params_free_decref(struct tee_param *params, size_t num_params)

I would rather rename this API as free_params().

> +{
> +	size_t n;
> +
> +	if (!params)
> +		return;
> +
> +	for (n = 0; n < num_params; n++)
> +		if (tee_param_is_memref(params + n) &&
> +		    params[n].u.memref.shm)
> +			tee_shm_put(params[n].u.memref.shm);
> +
> +	kfree(params);
> +}
> +
>  static int tee_ioctl_open_session(struct tee_context *ctx,
>  				  struct tee_ioctl_buf_data __user *ubuf)
>  {
> @@ -595,16 +610,7 @@ static int tee_ioctl_open_session(struct tee_context *ctx,
>  	 */
>  	if (rc && have_session && ctx->teedev->desc->ops->close_session)
>  		ctx->teedev->desc->ops->close_session(ctx, arg.session);
> -
> -	if (params) {
> -		/* Decrease ref count for all valid shared memory pointers */
> -		for (n = 0; n < arg.num_params; n++)
> -			if (tee_param_is_memref(params + n) &&
> -			    params[n].u.memref.shm)
> -				tee_shm_put(params[n].u.memref.shm);
> -		kfree(params);
> -	}
> -
> +	params_free_decref(params, arg.num_params);
>  	return rc;
>  }
>  
> @@ -657,14 +663,7 @@ static int tee_ioctl_invoke(struct tee_context *ctx,
>  	}
>  	rc = params_to_user(uparams, arg.num_params, params);
>  out:
> -	if (params) {
> -		/* Decrease ref count for all valid shared memory pointers */
> -		for (n = 0; n < arg.num_params; n++)
> -			if (tee_param_is_memref(params + n) &&
> -			    params[n].u.memref.shm)
> -				tee_shm_put(params[n].u.memref.shm);
> -		kfree(params);
> -	}
> +	params_free_decref(params, arg.num_params);
>  	return rc;
>  }
>  
> @@ -716,14 +715,7 @@ static int tee_ioctl_object_invoke(struct tee_context *ctx,
>  	}
>  	rc = params_to_user(uparams, arg.num_params, params);
>  out:
> -	if (params) {
> -		/* Decrease ref count for all valid shared memory pointers */
> -		for (n = 0; n < arg.num_params; n++)
> -			if (tee_param_is_memref(params + n) &&
> -			    params[n].u.memref.shm)
> -				tee_shm_put(params[n].u.memref.shm);
> -		kfree(params);
> -	}
> +	params_free_decref(params, arg.num_params);
>  	return rc;
>  }
>  
> @@ -822,6 +814,7 @@ static int tee_ioctl_supp_recv(struct tee_context *ctx,
>  	struct tee_iocl_supp_recv_arg __user *uarg;
>  	struct tee_param *params;
>  	u32 num_params;
> +	u32 alloc_num_params;
>  	u32 func;
>  
>  	if (!ctx->teedev->desc->ops->supp_recv)
> @@ -838,6 +831,8 @@ static int tee_ioctl_supp_recv(struct tee_context *ctx,
>  	if (get_user(num_params, &uarg->num_params))
>  		return -EFAULT;
>  
> +	alloc_num_params = num_params;

Why is this needed? Shouldn't the updated num_params will point to size
of params array?

-Sumit

> +
>  	if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) != buf.buf_len)
>  		return -EINVAL;
>  
> @@ -861,7 +856,7 @@ static int tee_ioctl_supp_recv(struct tee_context *ctx,
>  
>  	rc = params_to_supp(ctx, uarg->params, num_params, params);
>  out:
> -	kfree(params);
> +	params_free_decref(params, alloc_num_params);
>  	return rc;
>  }
>  
> -- 
> 2.39.5 (Apple Git-154)
> 

  reply	other threads:[~2026-05-01 14:32 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-29  3:07 [REPORT] tee: tee_ioctl_supp_recv() missing tee_shm_put() cleanup for memref params Qihang
2026-04-29  6:34 ` Jens Wiklander
2026-04-29 11:32   ` [PATCH] tee: fix missing shm reference cleanup in tee_ioctl_supp_recv Qihang
2026-05-01 14:31     ` Sumit Garg via OP-TEE [this message]
2026-05-05 15:08       ` Qihang
2026-05-05 15:30       ` [PATCH v2] " Qihang
2026-05-06  2:18         ` Qihang
2026-05-07  7:31           ` Jens Wiklander
2026-05-07  7:47         ` Jens Wiklander
2026-05-07  9:45         ` [PATCH v3] tee: fix params_from_user() error path " Qihang
2026-05-07 10:40           ` Jens Wiklander
2026-05-07 15:39           ` [PATCH v4] " Qihang
2026-05-11 13:18             ` Jens Wiklander

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=afS5XzaeLUx1ngAl@sumit-xelite \
    --to=op-tee@lists.trustedfirmware.org \
    --cc=q.h.hack.winter@gmail.com \
    --cc=sumit.garg@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox