qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hw/elf_ops: Ignore loadable segments with zero size
@ 2024-01-16 15:50 Bin Meng
  2024-01-16 16:38 ` Richard Henderson
  2024-01-17  8:18 ` Philippe Mathieu-Daudé
  0 siblings, 2 replies; 5+ messages in thread
From: Bin Meng @ 2024-01-16 15:50 UTC (permalink / raw)
  To: qemu-devel, Richard Henderson, Thomas Huth

Some ELF files really do have segments of zero size, e.g.:

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  RISCV_ATTRIBUT 0x00000000000025b8 0x0000000000000000 0x0000000000000000
                 0x000000000000003e 0x0000000000000000  R      0x1
  LOAD           0x0000000000001000 0x0000000080200000 0x0000000080200000
                 0x00000000000001d1 0x00000000000001d1  R E    0x1000
  LOAD           0x00000000000011d1 0x00000000802001d1 0x00000000802001d1
                 0x0000000000000e37 0x0000000000000e37  RW     0x1000
  LOAD           0x0000000000000120 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000         0x1000

The current logic does not check for this condition, resulting in
the incorrect assignment of 'lowaddr' as zero.

There is already a piece of codes inside the segment traversal loop
that checks for zero-sized loadable segments for not creating empty
ROM blobs. Let's move this check to the beginning of the loop to
cover both scenarios.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
---

 include/hw/elf_ops.h | 75 +++++++++++++++++++++++---------------------
 1 file changed, 39 insertions(+), 36 deletions(-)

diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
index 0a5c258fe6..f014399b50 100644
--- a/include/hw/elf_ops.h
+++ b/include/hw/elf_ops.h
@@ -427,6 +427,16 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
             file_size = ph->p_filesz; /* Size of the allocated data */
             data_offset = ph->p_offset; /* Offset where the data is located */
 
+            /*
+             * Some ELF files really do have segments of zero size;
+             * just ignore them rather than trying to set the wrong addr,
+             * or create empty ROM blobs, because the zero-length blob can
+             * falsely trigger the overlapping-ROM-blobs check.
+             */
+            if (mem_size == 0) {
+                continue;
+            }
+
             if (file_size > 0) {
                 if (g_mapped_file_get_length(mapped_file) <
                     file_size + data_offset) {
@@ -530,45 +540,38 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
                 *pentry = ehdr.e_entry - ph->p_vaddr + ph->p_paddr;
             }
 
-            /* Some ELF files really do have segments of zero size;
-             * just ignore them rather than trying to create empty
-             * ROM blobs, because the zero-length blob can falsely
-             * trigger the overlapping-ROM-blobs check.
-             */
-            if (mem_size != 0) {
-                if (load_rom) {
-                    g_autofree char *label =
-                        g_strdup_printf("%s ELF program header segment %d",
-                                        name, i);
-
-                    /*
-                     * rom_add_elf_program() takes its own reference to
-                     * 'mapped_file'.
-                     */
-                    rom_add_elf_program(label, mapped_file, data, file_size,
-                                        mem_size, addr, as);
-                } else {
-                    MemTxResult res;
-
-                    res = address_space_write(as ? as : &address_space_memory,
-                                              addr, MEMTXATTRS_UNSPECIFIED,
-                                              data, file_size);
+            if (load_rom) {
+                g_autofree char *label =
+                    g_strdup_printf("%s ELF program header segment %d",
+                                    name, i);
+
+                /*
+                 * rom_add_elf_program() takes its own reference to
+                 * 'mapped_file'.
+                 */
+                rom_add_elf_program(label, mapped_file, data, file_size,
+                                    mem_size, addr, as);
+            } else {
+                MemTxResult res;
+
+                res = address_space_write(as ? as : &address_space_memory,
+                                          addr, MEMTXATTRS_UNSPECIFIED,
+                                          data, file_size);
+                if (res != MEMTX_OK) {
+                    goto fail;
+                }
+                /*
+                 * We need to zero'ify the space that is not copied
+                 * from file
+                 */
+                if (file_size < mem_size) {
+                    res = address_space_set(as ? as : &address_space_memory,
+                                            addr + file_size, 0,
+                                            mem_size - file_size,
+                                            MEMTXATTRS_UNSPECIFIED);
                     if (res != MEMTX_OK) {
                         goto fail;
                     }
-                    /*
-                     * We need to zero'ify the space that is not copied
-                     * from file
-                     */
-                    if (file_size < mem_size) {
-                        res = address_space_set(as ? as : &address_space_memory,
-                                                addr + file_size, 0,
-                                                mem_size - file_size,
-                                                MEMTXATTRS_UNSPECIFIED);
-                        if (res != MEMTX_OK) {
-                            goto fail;
-                        }
-                    }
                 }
             }
 
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] hw/elf_ops: Ignore loadable segments with zero size
  2024-01-16 15:50 [PATCH] hw/elf_ops: Ignore loadable segments with zero size Bin Meng
@ 2024-01-16 16:38 ` Richard Henderson
  2024-01-20 10:28   ` Michael Tokarev
  2024-01-17  8:18 ` Philippe Mathieu-Daudé
  1 sibling, 1 reply; 5+ messages in thread
From: Richard Henderson @ 2024-01-16 16:38 UTC (permalink / raw)
  To: Bin Meng, qemu-devel, Thomas Huth

On 1/17/24 02:50, Bin Meng wrote:
> Some ELF files really do have segments of zero size, e.g.:
> 
> Program Headers:
>    Type           Offset             VirtAddr           PhysAddr
>                   FileSiz            MemSiz              Flags  Align
>    RISCV_ATTRIBUT 0x00000000000025b8 0x0000000000000000 0x0000000000000000
>                   0x000000000000003e 0x0000000000000000  R      0x1
>    LOAD           0x0000000000001000 0x0000000080200000 0x0000000080200000
>                   0x00000000000001d1 0x00000000000001d1  R E    0x1000
>    LOAD           0x00000000000011d1 0x00000000802001d1 0x00000000802001d1
>                   0x0000000000000e37 0x0000000000000e37  RW     0x1000
>    LOAD           0x0000000000000120 0x0000000000000000 0x0000000000000000
>                   0x0000000000000000 0x0000000000000000         0x1000
> 
> The current logic does not check for this condition, resulting in
> the incorrect assignment of 'lowaddr' as zero.
> 
> There is already a piece of codes inside the segment traversal loop
> that checks for zero-sized loadable segments for not creating empty
> ROM blobs. Let's move this check to the beginning of the loop to
> cover both scenarios.
> 
> Signed-off-by: Bin Meng <bmeng@tinylab.org>

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

But please report this as a bug to whatever tool produced such nonsense.


r~


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] hw/elf_ops: Ignore loadable segments with zero size
  2024-01-16 15:50 [PATCH] hw/elf_ops: Ignore loadable segments with zero size Bin Meng
  2024-01-16 16:38 ` Richard Henderson
@ 2024-01-17  8:18 ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-01-17  8:18 UTC (permalink / raw)
  To: Bin Meng, qemu-devel, Richard Henderson, Thomas Huth

On 16/1/24 16:50, Bin Meng wrote:
> Some ELF files really do have segments of zero size, e.g.:
> 
> Program Headers:
>    Type           Offset             VirtAddr           PhysAddr
>                   FileSiz            MemSiz              Flags  Align
>    RISCV_ATTRIBUT 0x00000000000025b8 0x0000000000000000 0x0000000000000000
>                   0x000000000000003e 0x0000000000000000  R      0x1
>    LOAD           0x0000000000001000 0x0000000080200000 0x0000000080200000
>                   0x00000000000001d1 0x00000000000001d1  R E    0x1000
>    LOAD           0x00000000000011d1 0x00000000802001d1 0x00000000802001d1
>                   0x0000000000000e37 0x0000000000000e37  RW     0x1000
>    LOAD           0x0000000000000120 0x0000000000000000 0x0000000000000000
>                   0x0000000000000000 0x0000000000000000         0x1000
> 
> The current logic does not check for this condition, resulting in
> the incorrect assignment of 'lowaddr' as zero.
> 
> There is already a piece of codes inside the segment traversal loop
> that checks for zero-sized loadable segments for not creating empty
> ROM blobs. Let's move this check to the beginning of the loop to
> cover both scenarios.
> 
> Signed-off-by: Bin Meng <bmeng@tinylab.org>
> ---
> 
>   include/hw/elf_ops.h | 75 +++++++++++++++++++++++---------------------
>   1 file changed, 39 insertions(+), 36 deletions(-)

Thanks, patch queued.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] hw/elf_ops: Ignore loadable segments with zero size
  2024-01-16 16:38 ` Richard Henderson
@ 2024-01-20 10:28   ` Michael Tokarev
  2024-01-20 21:25     ` Richard Henderson
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Tokarev @ 2024-01-20 10:28 UTC (permalink / raw)
  To: Richard Henderson, Bin Meng, qemu-devel, Thomas Huth

16.01.2024 19:38, Richard Henderson wrote:
> On 1/17/24 02:50, Bin Meng wrote:
>> Some ELF files really do have segments of zero size, e.g.:
>>
>> Program Headers:
>>    Type           Offset             VirtAddr           PhysAddr
>>                   FileSiz            MemSiz              Flags  Align
>>    RISCV_ATTRIBUT 0x00000000000025b8 0x0000000000000000 0x0000000000000000
>>                   0x000000000000003e 0x0000000000000000  R      0x1
>>    LOAD           0x0000000000001000 0x0000000080200000 0x0000000080200000
>>                   0x00000000000001d1 0x00000000000001d1  R E    0x1000
>>    LOAD           0x00000000000011d1 0x00000000802001d1 0x00000000802001d1
>>                   0x0000000000000e37 0x0000000000000e37  RW     0x1000
>>    LOAD           0x0000000000000120 0x0000000000000000 0x0000000000000000
>>                   0x0000000000000000 0x0000000000000000         0x1000
>>
>> The current logic does not check for this condition, resulting in
>> the incorrect assignment of 'lowaddr' as zero.
>>
>> There is already a piece of codes inside the segment traversal loop
>> that checks for zero-sized loadable segments for not creating empty
>> ROM blobs. Let's move this check to the beginning of the loop to
>> cover both scenarios.
>>
>> Signed-off-by: Bin Meng <bmeng@tinylab.org>
> 
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> 
> But please report this as a bug to whatever tool produced such nonsense.

I think we've an old bug about this in debian bts, https://bugs.debian.org/919921 .

/mjt


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] hw/elf_ops: Ignore loadable segments with zero size
  2024-01-20 10:28   ` Michael Tokarev
@ 2024-01-20 21:25     ` Richard Henderson
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Henderson @ 2024-01-20 21:25 UTC (permalink / raw)
  To: Michael Tokarev, Bin Meng, qemu-devel, Thomas Huth

On 1/20/24 21:28, Michael Tokarev wrote:
> 16.01.2024 19:38, Richard Henderson wrote:
>> On 1/17/24 02:50, Bin Meng wrote:
>>> Some ELF files really do have segments of zero size, e.g.:
>>>
>>> Program Headers:
>>>    Type           Offset             VirtAddr           PhysAddr
>>>                   FileSiz            MemSiz              Flags  Align
>>>    RISCV_ATTRIBUT 0x00000000000025b8 0x0000000000000000 0x0000000000000000
>>>                   0x000000000000003e 0x0000000000000000  R      0x1
>>>    LOAD           0x0000000000001000 0x0000000080200000 0x0000000080200000
>>>                   0x00000000000001d1 0x00000000000001d1  R E    0x1000
>>>    LOAD           0x00000000000011d1 0x00000000802001d1 0x00000000802001d1
>>>                   0x0000000000000e37 0x0000000000000e37  RW     0x1000
>>>    LOAD           0x0000000000000120 0x0000000000000000 0x0000000000000000
>>>                   0x0000000000000000 0x0000000000000000         0x1000
>>>
>>> The current logic does not check for this condition, resulting in
>>> the incorrect assignment of 'lowaddr' as zero.
>>>
>>> There is already a piece of codes inside the segment traversal loop
>>> that checks for zero-sized loadable segments for not creating empty
>>> ROM blobs. Let's move this check to the beginning of the loop to
>>> cover both scenarios.
>>>
>>> Signed-off-by: Bin Meng <bmeng@tinylab.org>
>>
>> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>>
>> But please report this as a bug to whatever tool produced such nonsense.
> 
> I think we've an old bug about this in debian bts, https://bugs.debian.org/919921 .

That's different -- file size == 0, mem size != 0.


r~



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-01-20 21:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-16 15:50 [PATCH] hw/elf_ops: Ignore loadable segments with zero size Bin Meng
2024-01-16 16:38 ` Richard Henderson
2024-01-20 10:28   ` Michael Tokarev
2024-01-20 21:25     ` Richard Henderson
2024-01-17  8:18 ` Philippe Mathieu-Daudé

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).