mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: SCHNEIDER Johannes <johannes.schneider@leica-geosystems.com>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: HAEMMERLE Thomas <thomas.haemmerle@leica-geosystems.com>,
	BAREBOX <barebox@lists.infradead.org>
Subject: Re: [PATCH] lib: gui: png_pico: fix use-after-free and double-free in png_open
Date: Thu, 4 Jun 2026 03:29:03 +0000	[thread overview]
Message-ID: <AM6PR06MB4150B2685D940C18CEAD8F0ABC102@AM6PR06MB4150.eurprd06.prod.outlook.com> (raw)
In-Reply-To: <667bad55-4666-4ced-be21-3cd38b0ba5df@pengutronix.de>

Hoi Ahmad,

>
>
> Hello,
>
> Thanks for the fix.
>

:-D

> On 6/2/26 4:24 AM, Johannes Schneider wrote:
> > From: Thomas Haemmerle <thomas.haemmerle@leica-geosystems.com>
> >
> > png_alloc_free_all() frees all picopng-internal allocations, including
> > the image->data buffer.  The previous code stored a pointer to this
> > buffer in img->data and called png_alloc_free_all() — leaving img->data
> > as a dangling pointer.  The subsequent png_close()'s free(img->data)
> > then performed a double-free on already-freed memory, causing a crash or
> > heap corruption when displaying the boot logo.
> >
> > Fix by copying the decoded pixel data into a fresh malloc buffer before
> > calling png_alloc_free_all().  png_close() correctly frees this copy.
>
> Never ceases to amaze how long memory corruption can go unnoticed..
>
> > -     pr_debug("png: %d x %d data@0x%p\n", img->width, img->height, img->data);
> > +     /*
> > +      * Copy decoded pixels to a stable buffer before png_alloc_free_all()
> > +      * frees the picopng internal allocations (including image->data).
> > +      * Without this copy, img->data would be a dangling pointer and
> > +      * png_close()'s free(img->data) would be a double-free.
> > +      */
> > +     imgsize = png_info->width * png_info->height * 4;
> > +     imgcopy = malloc(imgsize);
> > +     if (!imgcopy) {
> > +             ret = -ENOMEM;
> > +             goto err;
> > +     }
> > +     memcpy(imgcopy, png_info->image->data, imgsize);
> >
> >       png_alloc_free_all();
> >
> > +     img->data = imgcopy;
> > +
> > +     pr_debug("png: %d x %d data@0x%p\n", img->width, img->height, img->data);
> > +
>
> I would prefer avoiding the memory copy here. My suggestion would be adding
> (untested):
>
> void *png_alloc_detach(void *addr)
> {
>         for (png_alloc_node_t *node = png_alloc_tail; node; node = node->prev) {
>                 if (node->addr == addr) {
>                         png_alloc_remove_node(node);
>                         return addr;
>                 }
>         }
>
>         return NULL;
> }
>
> and then a single line change in png_open:
>
> -       img->data = png_info->image->data
> +       img->data = png_alloc_detach(png_info->image->data);
>
> What do you think?
>

good idea, sending out a v2
(with your 'suggested-by' :-)

>
> Cheers,
> Ahmad
>
> >       return img;
> >  err:
> >       png_alloc_free_all();
>

gruß
Johannes


      reply	other threads:[~2026-06-04  3:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-02  2:24 Johannes Schneider
2026-06-02  7:58 ` Ahmad Fatoum
2026-06-04  3:29   ` SCHNEIDER Johannes [this message]

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=AM6PR06MB4150B2685D940C18CEAD8F0ABC102@AM6PR06MB4150.eurprd06.prod.outlook.com \
    --to=johannes.schneider@leica-geosystems.com \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=thomas.haemmerle@leica-geosystems.com \
    /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