[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] hw/elf_ops: clear uninitialized segment space
From: |
Philippe Mathieu-Daudé |
Subject: |
Re: [PATCH] hw/elf_ops: clear uninitialized segment space |
Date: |
Wed, 14 Apr 2021 14:16:43 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 |
On 4/14/21 12:58 PM, Laurent Vivier wrote:
> 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>
> ---
> include/hw/elf_ops.h | 17 +++++++++++++++++
> 1 file changed, 17 insertions(+)
>
> diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
> index 6ee458e7bc3c..e3dcee3ee349 100644
> --- a/include/hw/elf_ops.h
> +++ b/include/hw/elf_ops.h
> @@ -562,6 +562,23 @@ 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) {
> + static uint8_t zero[4096];
Given it is unlikely, maybe better use:
g_autofree uint8_t *zero = g_new0(uint8_t, 4096);
> + uint64_t i;
> + for (i = file_size; i < mem_size; i += sizeof(zero))
> {
> + res = address_space_write(
> + as ? as : &address_space_memory,
> + addr + i, MEMTXATTRS_UNSPECIFIED,
> + zero, MIN(sizeof(zero), mem_size -
> i));
> + if (res != MEMTX_OK) {
> + goto fail;
> + }
> + }
> + }
> }
> }
>
>