libdwfl: Make sure there is at least one phdr

Message ID 20211217225242.790500-1-mark@klomp.org
State Committed
Headers
Series libdwfl: Make sure there is at least one phdr |

Commit Message

Mark Wielaard Dec. 17, 2021, 10:52 p.m. UTC
  The buffer read in needs to contain room for at least one Phdr.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdwfl/ChangeLog  | 4 ++++
 libdwfl/link_map.c | 5 +++++
 2 files changed, 9 insertions(+)
  

Patch

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index d4eee639..8760b1ef 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,7 @@ 
+2021-12-16  Mark Wielaard  <mark@klomp.org>
+
+	* link_map.c (dwfl_link_map_report): Make sure phnum is non-zero.
+
 2021-12-16  Mark Wielaard  <mark@klomp.org>
 
 	* link_map.c (dwfl_link_map_report): Make sure dyn_filesz / entsize is
diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c
index 177ad9a5..c4f79f11 100644
--- a/libdwfl/link_map.c
+++ b/libdwfl/link_map.c
@@ -890,6 +890,11 @@  dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
 		{
 		  nbytes = in.d_size;
 		  phnum = nbytes / phent;
+		  if (phnum == 0)
+		    {
+		      __libdwfl_seterrno (DWFL_E_BADELF);
+		      return false;
+		    }
 		}
 	      void *buf = malloc (nbytes);
 	      Elf32_Phdr (*p32)[phnum] = buf;