From a0cf64e0c5f5cad4714342df53deab5251a249ac Mon Sep 17 00:00:00 2001 From: emaste Date: Fri, 23 Jan 2015 04:07:07 +0000 Subject: [PATCH] libelf: Improve ELF header validation Avoid integer overflow and reading past EOF. MFC of r276427, r276443, r277249 from contrib/elftoolchain. git-svn-id: svn://svn.freebsd.org/base/stable/10@277558 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- lib/libelf/elf_scn.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/libelf/elf_scn.c b/lib/libelf/elf_scn.c index 2eefca151..5b2b3c776 100644 --- a/lib/libelf/elf_scn.c +++ b/lib/libelf/elf_scn.c @@ -31,6 +31,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include "_libelf.h" @@ -55,8 +56,10 @@ _libelf_load_scn(Elf *e, void *ehdr) assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0); #define CHECK_EHDR(E,EH) do { \ - if (fsz != (EH)->e_shentsize || \ - shoff + fsz * shnum > e->e_rawsize) { \ + if (shoff > e->e_rawsize || \ + fsz != (EH)->e_shentsize || \ + shnum > SIZE_MAX / fsz || \ + fsz * shnum > e->e_rawsize - shoff) { \ LIBELF_SET_ERROR(HEADER, 0); \ return (0); \ } \ -- 2.45.0