diff mbox series

[v2,2/2] hw/elf_ops: clear uninitialized segment space

Message ID 20210415100409.3977971-3-philmd@redhat.com (mailing list archive)
State New, archived
Headers show
Series hw/elf_ops: clear uninitialized segment space | expand

Commit Message

Philippe Mathieu-Daudé April 15, 2021, 10:04 a.m. UTC
From: Laurent Vivier <laurent@vivier.eu>

When the mem_size of the segment is bigger than the file_size,
and if this space doesn't overlap another segment, it needs
to be cleared.

This bug is very similar to the one we had for linux-user,
22d113b52f41 ("linux-user: Fix loading of BSS segments"),
where .bss section is encoded as an extension of the the data
one by setting the segment p_memsz > p_filesz.

Signed-off-by: Laurent Vivier <laurent@vivier.eu>
Message-Id: <20210414105838.205019-1-laurent@vivier.eu>
[PMD: Use recently added address_space_set()]
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 include/hw/elf_ops.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

Comments

Stefano Garzarella April 15, 2021, 11:02 a.m. UTC | #1
On Thu, Apr 15, 2021 at 12:04:09PM +0200, Philippe Mathieu-Daudé wrote:
>From: Laurent Vivier <laurent@vivier.eu>
>
>When the mem_size of the segment is bigger than the file_size,
>and if this space doesn't overlap another segment, it needs
>to be cleared.
>
>This bug is very similar to the one we had for linux-user,
>22d113b52f41 ("linux-user: Fix loading of BSS segments"),
>where .bss section is encoded as an extension of the the data
>one by setting the segment p_memsz > p_filesz.
>
>Signed-off-by: Laurent Vivier <laurent@vivier.eu>
>Message-Id: <20210414105838.205019-1-laurent@vivier.eu>
>[PMD: Use recently added address_space_set()]
>Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
>---
> include/hw/elf_ops.h | 13 +++++++++++++
> 1 file changed, 13 insertions(+)

Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>

>
>diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
>index 6ee458e7bc3..29f4c43e231 100644
>--- a/include/hw/elf_ops.h
>+++ b/include/hw/elf_ops.h
>@@ -562,6 +562,19 @@ static int glue(load_elf, SZ)(const char *name, int fd,
>                     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.26.3
>
Laurent Vivier April 15, 2021, 1:26 p.m. UTC | #2
Le 15/04/2021 à 12:04, Philippe Mathieu-Daudé a écrit :
> From: Laurent Vivier <laurent@vivier.eu>
> 
> When the mem_size of the segment is bigger than the file_size,
> and if this space doesn't overlap another segment, it needs
> to be cleared.
> 
> This bug is very similar to the one we had for linux-user,
> 22d113b52f41 ("linux-user: Fix loading of BSS segments"),
> where .bss section is encoded as an extension of the the data
> one by setting the segment p_memsz > p_filesz.
> 
> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
> Message-Id: <20210414105838.205019-1-laurent@vivier.eu>
> [PMD: Use recently added address_space_set()]

I agree that is a better way to do.

thanks,
Laurent

> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
>  include/hw/elf_ops.h | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
> index 6ee458e7bc3..29f4c43e231 100644
> --- a/include/hw/elf_ops.h
> +++ b/include/hw/elf_ops.h
> @@ -562,6 +562,19 @@ static int glue(load_elf, SZ)(const char *name, int fd,
>                      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;
> +                        }
> +                    }
>                  }
>              }
>  
>
Richard Henderson April 15, 2021, 10:33 p.m. UTC | #3
On 4/15/21 3:04 AM, Philippe Mathieu-Daudé wrote:
> From: Laurent Vivier<laurent@vivier.eu>
> 
> When the mem_size of the segment is bigger than the file_size,
> and if this space doesn't overlap another segment, it needs
> to be cleared.
> 
> This bug is very similar to the one we had for linux-user,
> 22d113b52f41 ("linux-user: Fix loading of BSS segments"),
> where .bss section is encoded as an extension of the the data
> one by setting the segment p_memsz > p_filesz.
> 
> Signed-off-by: Laurent Vivier<laurent@vivier.eu>
> Message-Id:<20210414105838.205019-1-laurent@vivier.eu>
> [PMD: Use recently added address_space_set()]
> Signed-off-by: Philippe Mathieu-Daudé<philmd@redhat.com>
> ---
>   include/hw/elf_ops.h | 13 +++++++++++++
>   1 file changed, 13 insertions(+)

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

r~
Laurent Vivier April 17, 2021, 3:36 p.m. UTC | #4
Le 15/04/2021 à 12:04, Philippe Mathieu-Daudé a écrit :
> From: Laurent Vivier <laurent@vivier.eu>
> 
> When the mem_size of the segment is bigger than the file_size,
> and if this space doesn't overlap another segment, it needs
> to be cleared.
> 
> This bug is very similar to the one we had for linux-user,
> 22d113b52f41 ("linux-user: Fix loading of BSS segments"),
> where .bss section is encoded as an extension of the the data
> one by setting the segment p_memsz > p_filesz.
> 
> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
> Message-Id: <20210414105838.205019-1-laurent@vivier.eu>
> [PMD: Use recently added address_space_set()]
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
>  include/hw/elf_ops.h | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
> index 6ee458e7bc3..29f4c43e231 100644
> --- a/include/hw/elf_ops.h
> +++ b/include/hw/elf_ops.h
> @@ -562,6 +562,19 @@ static int glue(load_elf, SZ)(const char *name, int fd,
>                      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;
> +                        }
> +                    }
>                  }
>              }
>  
> 

It seems we need also the same kind of operation with the other branch of the if
(rom_add_elf_program()), but I'm not sure where to do it:

diff --git a/hw/core/loader.c b/hw/core/loader.c
index d3e5f3b423f6..8146fdcbb7a0 100644
--- a/hw/core/loader.c
+++ b/hw/core/loader.c
@@ -1146,9 +1146,13 @@ static void rom_reset(void *unused)
         if (rom->mr) {
             void *host = memory_region_get_ram_ptr(rom->mr);
             memcpy(host, rom->data, rom->datasize);
+            memset(host + rom->datasize, 0, rom->romsize - rom->datasize);
         } else {
             address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED,
                                     rom->data, rom->datasize);
+            address_space_set(rom->as, rom->addr + rom->datasize, 0,
+                              rom->romsize - rom->datasize,
+                              MEMTXATTRS_UNSPECIFIED);
         }
         if (rom->isrom) {
             /* rom needs to be written only once */

Thanks,
Laurent
diff mbox series

Patch

diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
index 6ee458e7bc3..29f4c43e231 100644
--- a/include/hw/elf_ops.h
+++ b/include/hw/elf_ops.h
@@ -562,6 +562,19 @@  static int glue(load_elf, SZ)(const char *name, int fd,
                     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;
+                        }
+                    }
                 }
             }