All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] contrib/elf2dmp: add ELF dump header checking
@ 2022-05-20  8:43 Viktor Prutyanov
  2022-05-20 15:40 ` Richard Henderson
  2022-05-21  9:13 ` Paolo Bonzini
  0 siblings, 2 replies; 3+ messages in thread
From: Viktor Prutyanov @ 2022-05-20  8:43 UTC (permalink / raw)
  To: richard.henderson, thuth, pbonzini, f4bug, peter.maydell
  Cc: qemu-devel, yan, viktor.prutyanov

Add ELF header checking to prevent processing input file which is not
QEMU x86_64 guest memory dump or even not ELF.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1013

Signed-off-by: Viktor Prutyanov <viktor.prutyanov@redhat.com>
---
 contrib/elf2dmp/qemu_elf.c | 53 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/contrib/elf2dmp/qemu_elf.c b/contrib/elf2dmp/qemu_elf.c
index b601b6d7ba..ebda60dcb8 100644
--- a/contrib/elf2dmp/qemu_elf.c
+++ b/contrib/elf2dmp/qemu_elf.c
@@ -118,6 +118,53 @@ static void exit_states(QEMU_Elf *qe)
     free(qe->state);
 }
 
+static bool check_ehdr(QEMU_Elf *qe)
+{
+    Elf64_Ehdr *ehdr = qe->map;
+
+    if (sizeof(Elf64_Ehdr) > qe->size) {
+        eprintf("Invalid input dump file size\n");
+        return false;
+    }
+
+    if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+        eprintf("Invalid ELF signature, input file is not ELF\n");
+        return false;
+    }
+
+    if (ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
+            ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
+        eprintf("Invalid ELF class or byte order, must be 64-bit LE\n");
+        return false;
+    }
+
+    if (ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
+        eprintf("Invalid ELF version\n");
+        return false;
+    }
+
+    if (ehdr->e_machine != EM_X86_64) {
+        eprintf("Invalid input dump architecture, only x86_64 is supported\n");
+        return false;
+    }
+
+    if (ehdr->e_type != ET_CORE) {
+        eprintf("Invalid ELF type, must be core file\n");
+        return false;
+    }
+
+    /*
+     * ELF dump file must contain one PT_NOTE and at least one PT_LOAD to
+     * restore physical address space.
+     */
+    if (ehdr->e_phnum < 2) {
+        eprintf("Invalid number of ELF program headers\n");
+        return false;
+    }
+
+    return true;
+}
+
 int QEMU_Elf_init(QEMU_Elf *qe, const char *filename)
 {
     GError *gerr = NULL;
@@ -133,6 +180,12 @@ int QEMU_Elf_init(QEMU_Elf *qe, const char *filename)
     qe->map = g_mapped_file_get_contents(qe->gmf);
     qe->size = g_mapped_file_get_length(qe->gmf);
 
+    if (!check_ehdr(qe)) {
+        eprintf("Input file has the wrong format\n");
+        err = 1;
+        goto out_unmap;
+    }
+
     if (init_states(qe)) {
         eprintf("Failed to extract QEMU CPU states\n");
         err = 1;
-- 
2.35.1



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

* Re: [PATCH v2] contrib/elf2dmp: add ELF dump header checking
  2022-05-20  8:43 [PATCH v2] contrib/elf2dmp: add ELF dump header checking Viktor Prutyanov
@ 2022-05-20 15:40 ` Richard Henderson
  2022-05-21  9:13 ` Paolo Bonzini
  1 sibling, 0 replies; 3+ messages in thread
From: Richard Henderson @ 2022-05-20 15:40 UTC (permalink / raw)
  To: Viktor Prutyanov, thuth, pbonzini, f4bug, peter.maydell
  Cc: qemu-devel, yan, viktor.prutyanov

On 5/20/22 01:43, Viktor Prutyanov wrote:
> Add ELF header checking to prevent processing input file which is not
> QEMU x86_64 guest memory dump or even not ELF.
> 
> Resolves:https://gitlab.com/qemu-project/qemu/-/issues/1013
> 
> Signed-off-by: Viktor Prutyanov<viktor.prutyanov@redhat.com>
> ---
>   contrib/elf2dmp/qemu_elf.c | 53 ++++++++++++++++++++++++++++++++++++++
>   1 file changed, 53 insertions(+)

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

r~


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

* Re: [PATCH v2] contrib/elf2dmp: add ELF dump header checking
  2022-05-20  8:43 [PATCH v2] contrib/elf2dmp: add ELF dump header checking Viktor Prutyanov
  2022-05-20 15:40 ` Richard Henderson
@ 2022-05-21  9:13 ` Paolo Bonzini
  1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2022-05-21  9:13 UTC (permalink / raw)
  To: Viktor Prutyanov
  Cc: richard.henderson, thuth, pbonzini, f4bug, peter.maydell,
	qemu-devel, yan, viktor.prutyanov

Queued, thanks.

Paolo




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

end of thread, other threads:[~2022-05-21  9:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-20  8:43 [PATCH v2] contrib/elf2dmp: add ELF dump header checking Viktor Prutyanov
2022-05-20 15:40 ` Richard Henderson
2022-05-21  9:13 ` Paolo Bonzini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.