From: Laurent Vivier <laurent@vivier.eu>
To: Peter Maydell <peter.maydell@linaro.org>,
qemu-devel@nongnu.org, qemu-trivial@nongnu.org
Cc: Randy Yates <yates@ieee.org>
Subject: Re: [PATCH] elf_ops: Don't try to g_mapped_file_unref(NULL)
Date: Mon, 4 May 2020 11:46:28 +0200 [thread overview]
Message-ID: <c44d6231-1b11-1d4e-8885-dd4e81745acf@vivier.eu> (raw)
In-Reply-To: <20200423202011.32686-1-peter.maydell@linaro.org>
Le 23/04/2020 à 22:20, Peter Maydell a écrit :
> Calling g_mapped_file_unref() on a NULL pointer is not valid, and
> glib will assert if you try it.
>
> $ qemu-system-arm -M virt -display none -device loader,file=/tmp/bad.elf
> qemu-system-arm: -device loader,file=/tmp/bad.elf: GLib: g_mapped_file_unref: assertion 'file != NULL' failed
>
> (One way to produce an ELF file that fails like this is to copy just
> the first 16 bytes of a valid ELF file; this is sufficient to fool
> the code in load_elf_ram_sym() into thinking it's an ELF file and
> calling load_elf32() or load_elf64().)
>
> The failure-exit path in load_elf can be reached from various points
> in execution, and for some of those we haven't yet called
> g_mapped_file_new_from_fd(). Add a condition to the unref call so we
> only call it if we successfully created the GMappedFile to start with.
>
> This will fix the assertion; for the specific case of the generic
> loader it will then fall back from "guess this is an ELF file" to
> "maybe it's a uImage or a hex file" and eventually to "just load as
> a raw data file".
>
> Reported-by: Randy Yates <yates@ieee.org>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> include/hw/elf_ops.h | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
> index e0bb47bb678..398a4a2c85b 100644
> --- a/include/hw/elf_ops.h
> +++ b/include/hw/elf_ops.h
> @@ -606,7 +606,9 @@ static int glue(load_elf, SZ)(const char *name, int fd,
> *highaddr = (uint64_t)(elf_sword)high;
> ret = total_size;
> fail:
> - g_mapped_file_unref(mapped_file);
> + if (mapped_file) {
> + g_mapped_file_unref(mapped_file);
> + }
> g_free(phdr);
> return ret;
> }
>
Applied to my trivial-patches branch.
Thanks,
Laurent
prev parent reply other threads:[~2020-05-04 9:49 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-23 20:20 [PATCH] elf_ops: Don't try to g_mapped_file_unref(NULL) Peter Maydell
2020-04-23 20:24 ` Philippe Mathieu-Daudé
2020-04-23 20:47 ` Peter Maydell
2020-04-24 8:02 ` Stefano Garzarella
2020-05-04 9:46 ` Laurent Vivier [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=c44d6231-1b11-1d4e-8885-dd4e81745acf@vivier.eu \
--to=laurent@vivier.eu \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-trivial@nongnu.org \
--cc=yates@ieee.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;
as well as URLs for NNTP newsgroup(s).