public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: vvvvvv@google.com
To: elfutils-devel@sourceware.org
Cc: kernel-team@android.com, maennich@google.com, vvvvvv@google.com
Subject: [PATCH] libdwfl: add check for elf_memory return value
Date: Wed, 25 Jan 2023 14:16:57 +0000	[thread overview]
Message-ID: <20230125141657.881588-1-vvvvvv@google.com> (raw)

From: Aleksei Vetrov <vvvvvv@google.com>

elf_memory call in __libdw_open_elf_memory can return NULL. It was not
checked and led to the null pointer dereference.

Signed-off-by: Aleksei Vetrov <vvvvvv@google.com>
---
 libdwfl/open.c                     | 4 ++++
 tests/dwfl-report-offline-memory.c | 9 +++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/libdwfl/open.c b/libdwfl/open.c
index b97ff016..d0f357ed 100644
--- a/libdwfl/open.c
+++ b/libdwfl/open.c
@@ -211,6 +211,10 @@ __libdw_open_elf_memory (char *data, size_t size, Elf **elfp, bool archive_ok)
      won't work.  */
   int fd = -1;
   *elfp = elf_memory (data, size);
+  if (unlikely(*elfp == NULL))
+    {
+      return DWFL_E_LIBELF;
+    }
   /* Allow using this ELF as reference for subsequent elf_begin calls.  */
   (*elfp)->cmd = ELF_C_READ_MMAP_PRIVATE;
   return libdw_open_elf (&fd, elfp, false, archive_ok, true, false, true);
diff --git a/tests/dwfl-report-offline-memory.c b/tests/dwfl-report-offline-memory.c
index e6cb0a51..b3b4d9bd 100644
--- a/tests/dwfl-report-offline-memory.c
+++ b/tests/dwfl-report-offline-memory.c
@@ -77,8 +77,13 @@ main (int argc, char **argv)
   Dwfl *dwfl = dwfl_begin (&offline_callbacks);
   assert (dwfl != NULL);
 
-  Dwfl_Module *mod =
-      dwfl_report_offline_memory (dwfl, argv[1], argv[1], data, size);
+  Dwfl_Module *mod;
+
+  /* Check error handling by suppling zero data */
+  mod = dwfl_report_offline_memory(dwfl, argv[1], argv[1], NULL, 0);
+  assert(mod == NULL);
+
+  mod = dwfl_report_offline_memory(dwfl, argv[1], argv[1], data, size);
   assert (mod != NULL);
   dwfl_report_end (dwfl, NULL, NULL);
 
-- 
2.39.1.405.gd4c25cc71f-goog


             reply	other threads:[~2023-01-25 14:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-25 14:16 vvvvvv [this message]
2023-01-26 20:48 ` Mark Wielaard

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=20230125141657.881588-1-vvvvvv@google.com \
    --to=vvvvvv@google.com \
    --cc=elfutils-devel@sourceware.org \
    --cc=kernel-team@android.com \
    --cc=maennich@google.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 read-only IMAP folder(s) and NNTP newsgroup(s).