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 v4 18/19] contrib/elf2dmp: Clamp QEMU note to file size
Date: Thu, 07 Mar 2024 19:21:01 +0900 [thread overview]
Message-ID: <20240307-elf2dmp-v4-18-4f324ad4d99d@daynix.com> (raw)
In-Reply-To: <20240307-elf2dmp-v4-0-4f324ad4d99d@daynix.com>
This fixes crashes with truncated dumps.
Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
---
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
next prev parent reply other threads:[~2024-03-07 10:23 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-07 10:20 [PATCH v4 00/19] contrib/elf2dmp: Improve robustness Akihiko Odaki
2024-03-07 10:20 ` [PATCH v4 01/19] contrib/elf2dmp: Remove unnecessary err flags Akihiko Odaki
2024-03-07 10:20 ` [PATCH v4 02/19] contrib/elf2dmp: Assume error by default Akihiko Odaki
2024-03-07 10:20 ` [PATCH v4 03/19] contrib/elf2dmp: Continue even contexts are lacking Akihiko Odaki
2024-03-07 10:20 ` [PATCH v4 04/19] contrib/elf2dmp: Change pa_space_create() signature Akihiko Odaki
2024-03-07 10:25 ` Philippe Mathieu-Daudé
2024-03-07 10:20 ` [PATCH v4 05/19] contrib/elf2dmp: Fix error reporting style in addrspace.c Akihiko Odaki
2024-03-07 10:25 ` Philippe Mathieu-Daudé
2024-03-07 10:20 ` [PATCH v4 06/19] contrib/elf2dmp: Fix error reporting style in download.c Akihiko Odaki
2024-03-07 13:08 ` Peter Maydell
2024-03-07 10:20 ` [PATCH v4 07/19] contrib/elf2dmp: Fix error reporting style in pdb.c Akihiko Odaki
2024-03-07 13:10 ` Peter Maydell
2024-03-07 10:20 ` [PATCH v4 08/19] contrib/elf2dmp: Fix error reporting style in qemu_elf.c Akihiko Odaki
2024-03-07 13:11 ` Peter Maydell
2024-03-07 10:20 ` [PATCH v4 09/19] contrib/elf2dmp: Fix error reporting style in main.c Akihiko Odaki
2024-03-07 13:12 ` Peter Maydell
2024-03-07 10:20 ` [PATCH v4 10/19] contrib/elf2dmp: Always check for PA resolution failure Akihiko Odaki
2024-03-07 10:20 ` [PATCH v4 11/19] contrib/elf2dmp: Always destroy PA space Akihiko Odaki
2024-03-07 10:20 ` [PATCH v4 12/19] contrib/elf2dmp: Ensure segment fits in file Akihiko Odaki
2024-03-07 10:20 ` [PATCH v4 13/19] contrib/elf2dmp: Use lduw_le_p() to read PDB Akihiko Odaki
2024-03-07 10:27 ` Philippe Mathieu-Daudé
2024-03-07 10:20 ` [PATCH v4 14/19] contrib/elf2dmp: Use rol64() to decode Akihiko Odaki
2024-03-07 10:29 ` Philippe Mathieu-Daudé
2024-03-07 10:20 ` [PATCH v4 15/19] MAINTAINERS: Add Akihiko Odaki as a elf2dmp reviewer Akihiko Odaki
2024-03-07 10:29 ` Philippe Mathieu-Daudé
2024-03-10 19:43 ` Viktor Prutyanov
2024-03-07 10:20 ` [PATCH v4 16/19] contrib/elf2dmp: Build only for little endian host Akihiko Odaki
2024-03-07 10:21 ` [PATCH v4 17/19] contrib/elf2dmp: Use GPtrArray Akihiko Odaki
2024-03-07 10:21 ` Akihiko Odaki [this message]
2024-03-07 10:21 ` [PATCH v4 19/19] contrib/elf2dmp: Ensure phdrs fit in file Akihiko Odaki
2024-03-07 13:14 ` Peter Maydell
2024-03-10 20:25 ` [PATCH v4 00/19] contrib/elf2dmp: Improve robustness Viktor Prutyanov
2024-03-11 17:09 ` 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=20240307-elf2dmp-v4-18-4f324ad4d99d@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 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).