From: Laurent Vivier <laurent@vivier.eu>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>,
"Laurent Vivier" <laurent@vivier.eu>,
"Stefano Garzarella" <sgarzare@redhat.com>
Subject: [PATCH] hw/elf_ops: clear uninitialized segment space
Date: Wed, 14 Apr 2021 12:58:38 +0200 [thread overview]
Message-ID: <20210414105838.205019-1-laurent@vivier.eu> (raw)
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];
+ 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;
+ }
+ }
+ }
}
}
--
2.30.2
next reply other threads:[~2021-04-14 10:59 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-14 10:58 Laurent Vivier [this message]
2021-04-14 12:16 ` [PATCH] hw/elf_ops: clear uninitialized segment space Philippe Mathieu-Daudé
2021-04-14 12:41 ` Laurent Vivier
2021-04-15 8:44 ` Philippe Mathieu-Daudé
2021-04-15 9:57 ` Philippe Mathieu-Daudé
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=20210414105838.205019-1-laurent@vivier.eu \
--to=laurent@vivier.eu \
--cc=peter.maydell@linaro.org \
--cc=philmd@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=sgarzare@redhat.com \
/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).