public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] libdwfl: add check for elf_memory return value
@ 2023-01-25 14:16 vvvvvv
  2023-01-26 20:48 ` Mark Wielaard
  0 siblings, 1 reply; 2+ messages in thread
From: vvvvvv @ 2023-01-25 14:16 UTC (permalink / raw)
  To: elfutils-devel; +Cc: kernel-team, maennich, vvvvvv

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


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

* Re: [PATCH] libdwfl: add check for elf_memory return value
  2023-01-25 14:16 [PATCH] libdwfl: add check for elf_memory return value vvvvvv
@ 2023-01-26 20:48 ` Mark Wielaard
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Wielaard @ 2023-01-26 20:48 UTC (permalink / raw)
  To: vvvvvv; +Cc: elfutils-devel, kernel-team, maennich

Hi Aleksei,

On Wed, Jan 25, 2023 at 02:16:57PM +0000, Aleksei Vetrov via Elfutils-devel wrote:
> 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.

Looks good. Pushed.
Thanks for finding this,

Cheers,

Mark


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

end of thread, other threads:[~2023-01-26 20:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-25 14:16 [PATCH] libdwfl: add check for elf_memory return value vvvvvv
2023-01-26 20:48 ` Mark Wielaard

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).