public inbox for op-tee@lists.trustedfirmware.org
 help / color / mirror / Atom feed
From: Sumit Garg via OP-TEE <op-tee@lists.trustedfirmware.org>
To: Pei Xiao <xiaopei01@kylinos.cn>
Cc: op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] tee: fix memory leak in tee_dyn_shm_alloc_helper
Date: Tue, 29 Jul 2025 14:57:44 +0530	[thread overview]
Message-ID: <aIiUEGt2uVF9yi1W@sumit-X1> (raw)
In-Reply-To: <ae66122a86295f0ca5f2ff385ac2850360a805c1.1753251689.git.xiaopei01@kylinos.cn>

On Wed, Jul 23, 2025 at 02:22:41PM +0800, Pei Xiao wrote:
> When shm_register() fails in tee_dyn_shm_alloc_helper(), the pre-allocated
> pages array is not freed, resulting in a memory leak.
> 
> Fixes: cf4441503e20 ("tee: optee: Move pool_op helper functions")
> Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
> ---
>  drivers/tee/tee_shm.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)

Reviewed-by: Sumit Garg <sumit.garg@oss.qualcomm.com>

-Sumit

> 
> diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c
> index 915239b033f5..2a7d253d9c55 100644
> --- a/drivers/tee/tee_shm.c
> +++ b/drivers/tee/tee_shm.c
> @@ -230,7 +230,7 @@ int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
>  	pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
>  	if (!pages) {
>  		rc = -ENOMEM;
> -		goto err;
> +		goto err_pages;
>  	}
>  
>  	for (i = 0; i < nr_pages; i++)
> @@ -243,11 +243,13 @@ int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
>  		rc = shm_register(shm->ctx, shm, pages, nr_pages,
>  				  (unsigned long)shm->kaddr);
>  		if (rc)
> -			goto err;
> +			goto err_kfree;
>  	}
>  
>  	return 0;
> -err:
> +err_kfree:
> +	kfree(pages);
> +err_pages:
>  	free_pages_exact(shm->kaddr, shm->size);
>  	shm->kaddr = NULL;
>  	return rc;
> -- 
> 2.25.1
> 

  reply	other threads:[~2025-07-29  9:28 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-23  6:22 [PATCH] tee: fix memory leak in tee_dyn_shm_alloc_helper Pei Xiao
2025-07-29  9:27 ` Sumit Garg via OP-TEE [this message]
2025-08-04 15:49   ` Jens Wiklander
2025-07-29 16:54 ` Markus Elfring via OP-TEE

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=aIiUEGt2uVF9yi1W@sumit-X1 \
    --to=op-tee@lists.trustedfirmware.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sumit.garg@kernel.org \
    --cc=xiaopei01@kylinos.cn \
    /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