All of lore.kernel.org
 help / color / mirror / Atom feed
From: Akihiko Odaki <akihiko.odaki@daynix.com>
To: Viktor Prutyanov <viktor.prutyanov@phystech.edu>,
	 Peter Maydell <peter.maydell@linaro.org>
Cc: qemu-devel@nongnu.org, Akihiko Odaki <akihiko.odaki@daynix.com>
Subject: [PATCH v2 13/13] contrib/elf2dmp: Clamp QEMU note to file size
Date: Tue, 05 Mar 2024 16:36:30 +0900	[thread overview]
Message-ID: <20240305-elf2dmp-v2-13-86ff2163ad32@daynix.com> (raw)
In-Reply-To: <20240305-elf2dmp-v2-0-86ff2163ad32@daynix.com>

This fixes crashes with truncated dumps.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2202
Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 contrib/elf2dmp/qemu_elf.c | 87 +++++++++++++++++++++++++++++-----------------
 1 file changed, 55 insertions(+), 32 deletions(-)

diff --git a/contrib/elf2dmp/qemu_elf.c b/contrib/elf2dmp/qemu_elf.c
index 7d896cac5b15..8d750adf904a 100644
--- a/contrib/elf2dmp/qemu_elf.c
+++ b/contrib/elf2dmp/qemu_elf.c
@@ -6,6 +6,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/host-utils.h"
 #include "err.h"
 #include "qemu_elf.h"
 
@@ -15,36 +16,11 @@
 #define ROUND_UP(n, d) (((n) + (d) - 1) & -(0 ? (n) : (d)))
 #endif
 
-#ifndef DIV_ROUND_UP
-#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
-#endif
-
-#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size)   \
-    ((DIV_ROUND_UP((hdr_size), 4) +                     \
-      DIV_ROUND_UP((name_size), 4) +                    \
-      DIV_ROUND_UP((desc_size), 4)) * 4)
-
 int is_system(QEMUCPUState *s)
 {
     return s->gs.base >> 63;
 }
 
-static char *nhdr_get_name(Elf64_Nhdr *nhdr)
-{
-    return (char *)nhdr + ROUND_UP(sizeof(*nhdr), 4);
-}
-
-static void *nhdr_get_desc(Elf64_Nhdr *nhdr)
-{
-    return nhdr_get_name(nhdr) + ROUND_UP(nhdr->n_namesz, 4);
-}
-
-static Elf64_Nhdr *nhdr_get_next(Elf64_Nhdr *nhdr)
-{
-    return (void *)((uint8_t *)nhdr + ELF_NOTE_SIZE(sizeof(*nhdr),
-                nhdr->n_namesz, nhdr->n_descsz));
-}
-
 Elf64_Phdr *elf64_getphdr(void *map)
 {
     Elf64_Ehdr *ehdr = map;
@@ -60,13 +36,35 @@ Elf64_Half elf_getphdrnum(void *map)
     return ehdr->e_phnum;
 }
 
+static bool advance_note_offset(uint64_t *offsetp, uint64_t size, uint64_t end)
+{
+    uint64_t offset = *offsetp;
+
+    if (uadd64_overflow(offset, size, &offset) || offset > UINT64_MAX - 3) {
+        return false;
+    }
+
+    offset = ROUND_UP(offset, 4);
+
+    if (offset > end) {
+        return false;
+    }
+
+    *offsetp = offset;
+
+    return true;
+}
+
 static bool init_states(QEMU_Elf *qe)
 {
     Elf64_Phdr *phdr = elf64_getphdr(qe->map);
-    Elf64_Nhdr *start = (void *)((uint8_t *)qe->map + phdr[0].p_offset);
-    Elf64_Nhdr *end = (void *)((uint8_t *)start + phdr[0].p_memsz);
     Elf64_Nhdr *nhdr;
     GPtrArray *states;
+    QEMUCPUState *state;
+    uint32_t state_size;
+    uint64_t offset;
+    uint64_t end_offset;
+    char *name;
 
     if (phdr[0].p_type != PT_NOTE) {
         eprintf("Failed to find PT_NOTE\n");
@@ -74,15 +72,40 @@ static bool init_states(QEMU_Elf *qe)
     }
 
     qe->has_kernel_gs_base = 1;
+    offset = phdr[0].p_offset;
     states = g_ptr_array_new();
 
-    for (nhdr = start; nhdr < end; nhdr = nhdr_get_next(nhdr)) {
-        if (!strcmp(nhdr_get_name(nhdr), QEMU_NOTE_NAME)) {
-            QEMUCPUState *state = nhdr_get_desc(nhdr);
+    if (uadd64_overflow(offset, phdr[0].p_memsz, &end_offset) ||
+        end_offset > qe->size) {
+        end_offset = qe->size;
+    }
+
+    while (offset < end_offset) {
+        nhdr = (void *)((uint8_t *)qe->map + offset);
+
+        if (!advance_note_offset(&offset, sizeof(*nhdr), end_offset)) {
+            break;
+        }
+
+        name = (char *)qe->map + offset;
+
+        if (!advance_note_offset(&offset, nhdr->n_namesz, end_offset)) {
+            break;
+        }
+
+        state = (void *)((uint8_t *)qe->map + offset);
+
+        if (!advance_note_offset(&offset, nhdr->n_descsz, end_offset)) {
+            break;
+        }
+
+        if (!strcmp(name, QEMU_NOTE_NAME) &&
+            nhdr->n_descsz >= offsetof(QEMUCPUState, kernel_gs_base)) {
+            state_size = MIN(state->size, nhdr->n_descsz);
 
-            if (state->size < sizeof(*state)) {
+            if (state_size < sizeof(*state)) {
                 eprintf("CPU #%u: QEMU CPU state size %u doesn't match\n",
-                        states->len, state->size);
+                        states->len, state_size);
                 /*
                  * We assume either every QEMU CPU state has KERNEL_GS_BASE or
                  * no one has.

-- 
2.44.0



  parent reply	other threads:[~2024-03-05  7:38 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-05  7:36 [PATCH v2 00/13] contrib/elf2dmp: Improve robustness Akihiko Odaki
2024-03-05  7:36 ` [PATCH v2 01/13] contrib/elf2dmp: Remove unnecessary err flags Akihiko Odaki
2024-03-05 13:24   ` Peter Maydell
2024-03-05  7:36 ` [PATCH v2 02/13] contrib/elf2dmp: Assume error by default Akihiko Odaki
2024-03-05 13:24   ` Peter Maydell
2024-03-05  7:36 ` [PATCH v2 03/13] contrib/elf2dmp: Continue even contexts are lacking Akihiko Odaki
2024-03-05  7:36 ` [PATCH v2 04/13] contrib/elf2dmp: Conform to the error reporting pattern Akihiko Odaki
2024-03-05 13:28   ` Peter Maydell
2024-03-06  5:00     ` Akihiko Odaki
2024-03-06 12:53       ` Peter Maydell
2024-03-05  7:36 ` [PATCH v2 05/13] contrib/elf2dmp: Always check for PA resolution failure Akihiko Odaki
2024-03-05 13:29   ` Peter Maydell
2024-03-05  7:36 ` [PATCH v2 06/13] contrib/elf2dmp: Always destroy PA space Akihiko Odaki
2024-03-05  7:36 ` [PATCH v2 07/13] contrib/elf2dmp: Ensure segment fits in file Akihiko Odaki
2024-03-05 13:31   ` Peter Maydell
2024-03-05  7:36 ` [PATCH v2 08/13] contrib/elf2dmp: Use lduw_le_p() to read PDB Akihiko Odaki
2024-03-05 13:32   ` Peter Maydell
2024-03-05  7:36 ` [PATCH v2 09/13] contrib/elf2dmp: Use rol64() to decode Akihiko Odaki
2024-03-05  7:36 ` [PATCH v2 10/13] MAINTAINERS: Add Akihiko Odaki as a elf2dmp reviewer Akihiko Odaki
2024-03-05  7:36 ` [PATCH v2 11/13] contrib/elf2dmp: Build only for little endian host Akihiko Odaki
2024-03-05 13:33   ` Peter Maydell
2024-03-06  5:03     ` Akihiko Odaki
2024-03-05  7:36 ` [PATCH v2 12/13] contrib/elf2dmp: Use GPtrArray Akihiko Odaki
2024-03-05 13:35   ` Peter Maydell
2024-03-05  7:36 ` Akihiko Odaki [this message]
2024-03-05 13:38   ` [PATCH v2 13/13] contrib/elf2dmp: Clamp QEMU note to file size Peter Maydell

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=20240305-elf2dmp-v2-13-86ff2163ad32@daynix.com \
    --to=akihiko.odaki@daynix.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=viktor.prutyanov@phystech.edu \
    /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 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.