From: Akihiko Odaki <akihiko.odaki@daynix.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: Viktor Prutyanov <viktor.prutyanov@phystech.edu>, qemu-devel@nongnu.org
Subject: Re: [PATCH 3/7] contrib/elf2dmp: Ensure segment fits in file
Date: Tue, 5 Mar 2024 12:53:38 +0900 [thread overview]
Message-ID: <b48e9a1f-7db4-4f92-bfbf-591c53f252df@daynix.com> (raw)
In-Reply-To: <CAFEAcA9Nr=rgzFP+L3UhL2AtEGBO0Mf=9iYKWJM=mRPFSwTBig@mail.gmail.com>
On 2024/03/05 2:52, Peter Maydell wrote:
> On Sun, 3 Mar 2024 at 10:53, Akihiko Odaki <akihiko.odaki@daynix.com> wrote:
>>
>> This makes elf2dmp more robust against corrupted inputs.
>>
>> Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
>> ---
>> contrib/elf2dmp/addrspace.c | 5 +++--
>> 1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/contrib/elf2dmp/addrspace.c b/contrib/elf2dmp/addrspace.c
>> index 980a7aa5f8fb..d546a400dfda 100644
>> --- a/contrib/elf2dmp/addrspace.c
>> +++ b/contrib/elf2dmp/addrspace.c
>> @@ -88,11 +88,12 @@ int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
>> ps->block = g_new(struct pa_block, ps->block_nr);
>>
>> for (i = 0; i < phdr_nr; i++) {
>> - if (phdr[i].p_type == PT_LOAD) {
>> + if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset < qemu_elf->size) {
>> ps->block[block_i] = (struct pa_block) {
>> .addr = (uint8_t *)qemu_elf->map + phdr[i].p_offset,
>> .paddr = phdr[i].p_paddr,
>> - .size = phdr[i].p_filesz,
>> + .size = MIN(phdr[i].p_filesz,
>> + qemu_elf->size - phdr[i].p_offset),
>
> Shouldn't "p_filesz is smaller than the actual amount of data in the
> file" be a failure condition? In include/hw/elf_ops.h we treat it
> that way:
>
> mem_size = ph->p_memsz; /* Size of the ROM */
> file_size = ph->p_filesz; /* Size of the allocated data */
> data_offset = ph->p_offset; /* Offset where the data is located */
>
> if (file_size > 0) {
> if (g_mapped_file_get_length(mapped_file) <
> file_size + data_offset) {
> goto fail;
> }
> [etc]
>
> Like that code, we could then only check if p_offset + p_filesz is off
> the end of the file, rather than checking p_offset separately.
>
>> };
>> pa_block_align(&ps->block[block_i]);
>> block_i = ps->block[block_i].size ? (block_i + 1) : block_i;
>
> thanks
> -- PMM
I'm making this permissive for corrupted dumps since they may still
include valuable information.
It is different from include/hw/elf_ops.h, which is presumably used to
load executables rather than dumps. Loading a corrupted executable does
nothing good.
Regards,
Akihiko Odaki
next prev parent reply other threads:[~2024-03-05 3:54 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-03 10:50 [PATCH 0/7] contrib/elf2dmp: Improve robustness Akihiko Odaki
2024-03-03 10:50 ` [PATCH 1/7] contrib/elf2dmp: Always check for PA resolution failure Akihiko Odaki
2024-03-04 17:47 ` Peter Maydell
2024-03-03 10:50 ` [PATCH 2/7] contrib/elf2dmp: Always destroy PA space Akihiko Odaki
2024-03-04 17:47 ` Peter Maydell
2024-03-03 10:50 ` [PATCH 3/7] contrib/elf2dmp: Ensure segment fits in file Akihiko Odaki
2024-03-04 17:52 ` Peter Maydell
2024-03-05 3:53 ` Akihiko Odaki [this message]
2024-03-03 10:50 ` [PATCH 4/7] contrib/elf2dmp: Use lduw_le_p() to read PDB Akihiko Odaki
2024-03-04 17:56 ` Peter Maydell
2024-03-03 10:50 ` [PATCH 5/7] contrib/elf2dmp: Use rol64() to decode Akihiko Odaki
2024-03-04 17:59 ` Peter Maydell
2024-03-03 10:50 ` [PATCH 6/7] contrib/elf2dmp: Continue even contexts are lacking Akihiko Odaki
2024-03-04 18:02 ` Peter Maydell
2024-03-03 10:50 ` [PATCH 7/7] MAINTAINERS: Add Akihiko Odaki as a elf2dmp reviewer Akihiko Odaki
2024-03-04 17:52 ` Peter Maydell
2024-03-04 18:03 ` [PATCH 0/7] contrib/elf2dmp: Improve robustness Peter Maydell
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=b48e9a1f-7db4-4f92-bfbf-591c53f252df@daynix.com \
--to=akihiko.odaki@daynix.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=viktor.prutyanov@phystech.edu \
/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).