From ce7874c9460a2495982499d6ae42a1f21e794b50 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Mon, 22 May 2017 01:39:52 -0500 Subject: [PATCH] fix segfault reading statically linked executables --- elf/elf.h | 2 +- elf/needed.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/elf/elf.h b/elf/elf.h index 9100cf4..60d26aa 100644 --- a/elf/elf.h +++ b/elf/elf.h @@ -575,7 +575,7 @@ Elf64_Shdr *libelf_shdr(void *elf, int n); Elf64_Shdr *libelf_sht_strtab(void *elf); char *libelf_sectionname(Elf64_Shdr *section, Elf64_Shdr *strtab); Elf64_Shdr *libelf_section_n(void *elf, int n); -Elf64_Shdr *libelf_section(void *elf, int type); +Elf64_Shdr *libelf_section(void *elf, unsigned int type); int libelf_type(void *elf); int libelf_iself(void *elf); diff --git a/elf/needed.c b/elf/needed.c index f4edd3d..28bbb9b 100644 --- a/elf/needed.c +++ b/elf/needed.c @@ -23,7 +23,7 @@ Elf64_Ehdr *libelf_header(void *elf) { } Elf64_Shdr *libelf_shdr(void *elf, int n) { - return 0; + return (Elf64_Shdr*)(char *)elf + n; } Elf64_Shdr *libelf_sht_strtab(void *elf) { @@ -58,7 +58,7 @@ Elf64_Shdr *libelf_section_n(void *elf, int n) { return (Elf64_Shdr *)((char *)elf + hdr->e_shoff + n * hdr->e_shentsize); } -Elf64_Shdr *libelf_section(void *elf, int type) { +Elf64_Shdr *libelf_section(void *elf, unsigned int type) { int i; Elf64_Ehdr *hdr; Elf64_Shdr *shdr; @@ -113,7 +113,7 @@ void *libelf_map(char *path, size_t *fsize) { return NULL; } /* not at least the size of the elf header? */ - if (sbuf.st_size < sizeof(Elf64_Ehdr)) { + if ((size_t)sbuf.st_size < sizeof(Elf64_Ehdr)) { close(elffd); return NULL; } @@ -177,7 +177,7 @@ int main(int ac, char **av) { exit(6); } - Elf64_Shdr *dsect; + Elf64_Shdr *dsect = 0; /* find program header table */ for (i = 0; i < hdr->e_phnum; i++) { phdr = (Elf64_Phdr *)((char *)elf + hdr->e_phoff + i * hdr->e_phentsize); @@ -185,6 +185,11 @@ int main(int ac, char **av) { dsect = (Elf64_Shdr *)((char *)elf + phdr->p_offset); } } + if (!dsect) { + /* no dsect, statically linked? */ + exit(7); + } + dyn = (Elf64_Dyn *)((char *)elf + dsect->sh_offset); if (!dyn) { exit(9); -- 2.40.0