[applied] elf-helpers: Don't crash on unexpected ELF file

Message ID 87leiyk7rk.fsf@redhat.com
State New
Headers
Series [applied] elf-helpers: Don't crash on unexpected ELF file |

Commit Message

Dodji Seketeli April 11, 2023, 10:55 a.m. UTC
  Hello,

When get_soname_of_elf_file is given an unexpected ELF file (e.g, a
DWARF file that is at the wrong place in an RPM, for instance) it hits
an assert and aborts.  Ooops.

This patch removes the offending assert from get_soname_of_elf_file.

	* src/abg-elf-helpers.cc (get_soname_of_elf_file): If the program
	header we are looking at is not what we expect, just skip it; do
	not abort.

Signed-off-by: Dodji Seketeli <dodji@redhat.com>
---
 src/abg-elf-helpers.cc | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
  

Patch

diff --git a/src/abg-elf-helpers.cc b/src/abg-elf-helpers.cc
index 8ffeefb3..d47c1c39 100644
--- a/src/abg-elf-helpers.cc
+++ b/src/abg-elf-helpers.cc
@@ -1521,13 +1521,17 @@  get_soname_of_elf_file(const string& path, string &soname)
           Elf_Scn* scn = gelf_offscn (elf, phdr->p_offset);
           GElf_Shdr shdr_mem;
           GElf_Shdr* shdr = gelf_getshdr (scn, &shdr_mem);
+	  if (!(shdr == NULL || (shdr->sh_type == SHT_DYNAMIC
+				 || shdr->sh_type == SHT_PROGBITS)))
+	    // This program header doesn't look like one we are
+	    // looking for.  Skip to the next.
+	    continue;
+
           size_t entsize = (shdr != NULL && shdr->sh_entsize != 0
                             ? shdr->sh_entsize
                             : gelf_fsize (elf, ELF_T_DYN, 1, EV_CURRENT));
           int maxcnt = (shdr != NULL
                         ? shdr->sh_size / entsize : INT_MAX);
-          ABG_ASSERT (shdr == NULL || (shdr->sh_type == SHT_DYNAMIC
-				       || shdr->sh_type == SHT_PROGBITS));
           Elf_Data* data = elf_getdata (scn, NULL);
           if (data == NULL)
             break;