readelf: Fix deref-of-null in handle_core_item()

Message ID 20240329224730.31706-1-maks.mishinFZ@gmail.com
State Rejected
Headers
Series readelf: Fix deref-of-null in handle_core_item() |

Commit Message

Maks Mishin March 29, 2024, 10:47 p.m. UTC
  Return value of a function 'gelf_getehdr' is dereferenced
without checking for NULL, but it is usually checked for this function.

Found by RASU JSC.

Signed-off-by: Maks Mishin <maks.mishinFZ@gmail.com>
---
 src/readelf.c | 7 +++++++
 1 file changed, 7 insertions(+)
  

Patch

diff --git a/src/readelf.c b/src/readelf.c
index 0e931184..f2ec358f 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -12440,6 +12440,13 @@  handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
 	     field went into the high half of USEC.  */
 	  GElf_Ehdr ehdr_mem;
 	  GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
+
+	  if (ehdr == NULL)
+	    {
+	      error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
+	      return;
+	    }
+
 	  if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
 	    usec >>= 32;
 	  else