U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Yao Zi <ziyao@disroot.org>
To: ant.v.moryakov@gmail.com, u-boot@lists.denx.de
Cc: Maks Mishin <maks.mishinFZ@gmail.com>
Subject: Re: [PATCH] tools: Fix handle leak in mmap_fdt function
Date: Fri, 18 Apr 2025 11:24:50 +0000	[thread overview]
Message-ID: <aAI2gj_PAi8zr9F-@pie.lan> (raw)
In-Reply-To: <20250418081526.11327-2-ant.v.moryakov@gmail.com>

On Fri, Apr 18, 2025 at 11:15:23AM +0300, ant.v.moryakov@gmail.com wrote:
> From: Maks Mishin <maks.mishinFZ@gmail.com>
> 
> The handle 'ptr' is created at fit_common.c:91 by calling 
> function 'mmap' and lost at fit_common.c:127: 
> Added call of free for `ptr` if ptr != MAP_FAILED.

First, why do you free() a mmapped memory chunk? This really sounds
unreasonable to me.

Second, you don't validate ptr before freeing it actually. It's possible
to branch to label "err" before ptr is initialized, thus you must guard
the free in case of garbage data in ptr (or just initialize ptr to NULL).

> Trigger was found by the Svace static analyzer.
> 
> Signed-off-by: Maks Mishin <maks.mishinFZ@gmail.com>
> ---
>  tools/fit_common.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/tools/fit_common.c b/tools/fit_common.c
> index d1cde16c1c..135e105929 100644
> --- a/tools/fit_common.c
> +++ b/tools/fit_common.c
> @@ -123,6 +123,7 @@ err:
>  		close(fd);
>  	if (delete_on_error)
>  		unlink(fname);
> +	free(ptr);
>  
>  	return -1;
>  }
> -- 
> 2.34.1

Thanks,
Yao Zi

  reply	other threads:[~2025-04-18 11:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20250418081526.11327-1-ant.v.moryakov@gmail.com>
2025-04-18  8:15 ` [PATCH] tools: Fix handle leak in mmap_fdt function ant.v.moryakov
2025-04-18 11:24   ` Yao Zi [this message]
2025-02-09 14:56 Maks Mishin
2025-02-18 19:45 ` Tom Rini

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=aAI2gj_PAi8zr9F-@pie.lan \
    --to=ziyao@disroot.org \
    --cc=ant.v.moryakov@gmail.com \
    --cc=maks.mishinFZ@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

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

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