435ea7
diff --git a/src/elfclass.h b/src/elfclass.h
435ea7
index 010958a..0826ce3 100644
435ea7
--- a/src/elfclass.h
435ea7
+++ b/src/elfclass.h
435ea7
@@ -35,10 +35,12 @@
435ea7
 	switch (type) {
435ea7
 #ifdef ELFCORE
435ea7
 	case ET_CORE:
435ea7
+		phnum = elf_getu16(swap, elfhdr.e_phnum);
435ea7
+		if (phnum > MAX_PHNUM)
435ea7
+			return toomany(ms, "program", phnum);
435ea7
 		flags |= FLAGS_IS_CORE;
435ea7
 		if (dophn_core(ms, clazz, swap, fd,
435ea7
-		    (off_t)elf_getu(swap, elfhdr.e_phoff),
435ea7
-		    elf_getu16(swap, elfhdr.e_phnum), 
435ea7
+		    (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
435ea7
 		    (size_t)elf_getu16(swap, elfhdr.e_phentsize),
435ea7
 		    fsize, &flags) == -1)
435ea7
 			return -1;
435ea7
@@ -46,18 +48,24 @@
435ea7
 #endif
435ea7
 	case ET_EXEC:
435ea7
 	case ET_DYN:
435ea7
+		phnum = elf_getu16(swap, elfhdr.e_phnum);
435ea7
+		if (phnum > MAX_PHNUM)
435ea7
+			return toomany(ms, "program", phnum);
435ea7
+		shnum = elf_getu16(swap, elfhdr.e_shnum);
435ea7
+		if (shnum > MAX_SHNUM)
435ea7
+			return toomany(ms, "section", shnum);
435ea7
 		if (dophn_exec(ms, clazz, swap, fd,
435ea7
-		    (off_t)elf_getu(swap, elfhdr.e_phoff),
435ea7
-		    elf_getu16(swap, elfhdr.e_phnum), 
435ea7
+		    (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
435ea7
 		    (size_t)elf_getu16(swap, elfhdr.e_phentsize),
435ea7
-		    fsize, &flags, elf_getu16(swap, elfhdr.e_shnum))
435ea7
-		    == -1)
435ea7
+		    fsize, &flags, shnum) == -1)
435ea7
 			return -1;
435ea7
 		/*FALLTHROUGH*/
435ea7
 	case ET_REL:
435ea7
+		shnum = elf_getu16(swap, elfhdr.e_shnum);
435ea7
+		if (shnum > MAX_SHNUM)
435ea7
+			return toomany(ms, "section", shnum);
435ea7
 		if (doshn(ms, clazz, swap, fd,
435ea7
-		    (off_t)elf_getu(swap, elfhdr.e_shoff),
435ea7
-		    elf_getu16(swap, elfhdr.e_shnum),
435ea7
+		    (off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
435ea7
 		    (size_t)elf_getu16(swap, elfhdr.e_shentsize),
435ea7
 		    fsize, &flags, elf_getu16(swap, elfhdr.e_machine),
435ea7
 		    (int)elf_getu16(swap, elfhdr.e_shstrndx)) == -1)
435ea7
diff --git a/src/readelf.c b/src/readelf.c
435ea7
index de016b5..1f4d1f4 100644
435ea7
--- a/src/readelf.c
435ea7
+++ b/src/readelf.c
435ea7
@@ -60,6 +60,18 @@ private uint16_t getu16(int, uint16_t);
435ea7
 private uint32_t getu32(int, uint32_t);
435ea7
 private uint64_t getu64(int, uint64_t);
435ea7
 
435ea7
+#define MAX_PHNUM	2048
435ea7
+#define	MAX_SHNUM	32768
435ea7
+
435ea7
+private int
435ea7
+toomany(struct magic_set *ms, const char *name, uint16_t num)
435ea7
+{
435ea7
+	if (file_printf(ms, ", too many %s header sections (%u)", name, num
435ea7
+	    ) == -1)
435ea7
+		return -1;
435ea7
+	return 0;
435ea7
+}
435ea7
+
435ea7
 private uint16_t
435ea7
 getu16(int swap, uint16_t value)
435ea7
 {
435ea7
@@ -388,13 +400,13 @@ donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
435ea7
 	if (namesz & 0x80000000) {
435ea7
 	    (void)file_printf(ms, ", bad note name size 0x%lx",
435ea7
 		(unsigned long)namesz);
435ea7
-	    return offset;
435ea7
+	    return 0;
435ea7
 	}
435ea7
 
435ea7
 	if (descsz & 0x80000000) {
435ea7
 	    (void)file_printf(ms, ", bad note description size 0x%lx",
435ea7
 		(unsigned long)descsz);
435ea7
-	    return offset;
435ea7
+	    return 0;
435ea7
 	}
435ea7
 
435ea7
 
435ea7
@@ -851,6 +863,7 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
435ea7
 	Elf32_Shdr sh32;
435ea7
 	Elf64_Shdr sh64;
435ea7
 	int stripped = 1;
435ea7
+	size_t nbadcap = 0;
435ea7
 	void *nbuf;
435ea7
 	off_t noff, coff, name_off;
435ea7
 	uint64_t cap_hw1 = 0;	/* SunOS 5.x hardware capabilites */
435ea7
@@ -928,6 +941,8 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
435ea7
 			free(nbuf);
435ea7
 			break;
435ea7
 		case SHT_SUNW_cap:
435ea7
+			if (nbadcap > 5)
435ea7
+				break;
435ea7
 			if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
435ea7
 				file_badseek(ms);
435ea7
 				return -1;
435ea7
@@ -963,6 +978,8 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
435ea7
 					    (unsigned long long)xcap_tag,
435ea7
 					    (unsigned long long)xcap_val) == -1)
435ea7
 						return -1;
435ea7
+					if (nbadcap++ > 2)
435ea7
+						coff = xsh_size;
435ea7
 					break;
435ea7
 				}
435ea7
 			}
435ea7
@@ -1142,7 +1159,7 @@ file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
435ea7
 	int flags = 0;
435ea7
 	Elf32_Ehdr elf32hdr;
435ea7
 	Elf64_Ehdr elf64hdr;
435ea7
-	uint16_t type;
435ea7
+	uint16_t type, phnum, shnum;
435ea7
 
435ea7
 	if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
435ea7
 		return 0;