elf: Validate shstrndx is within section header bounds
diff --git a/common/lib/elf.c b/common/lib/elf.c
index 5382efe2..eb35d513 100644
--- a/common/lib/elf.c
+++ b/common/lib/elf.c
@@ -623,6 +623,10 @@ bool elf64_load_section(uint8_t *elf, void *buffer, const char *name, size_t lim
panic(true, "elf: shdr_size < sizeof(struct elf64_shdr)");
}
+ if (hdr->shstrndx >= hdr->sh_num) {
+ return false;
+ }
+
struct elf64_shdr *shstrtab = (void *)elf + (hdr->shoff + hdr->shstrndx * hdr->shdr_size);
char *names = (void *)elf + shstrtab->sh_offset;
