From owner-svn-src-head@FreeBSD.ORG Thu Jul 19 11:15:54 2012 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 509AF1065695; Thu, 19 Jul 2012 11:15:54 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 3BFB58FC1B; Thu, 19 Jul 2012 11:15:54 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q6JBFsOJ075302; Thu, 19 Jul 2012 11:15:54 GMT (envelope-from kib@svn.freebsd.org) Received: (from kib@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q6JBFsmf075300; Thu, 19 Jul 2012 11:15:54 GMT (envelope-from kib@svn.freebsd.org) Message-Id: <201207191115.q6JBFsmf075300@svn.freebsd.org> From: Konstantin Belousov Date: Thu, 19 Jul 2012 11:15:54 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r238617 - head/sys/kern X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Jul 2012 11:15:54 -0000 Author: kib Date: Thu Jul 19 11:15:53 2012 New Revision: 238617 URL: http://svn.freebsd.org/changeset/base/238617 Log: Fix several reads beyond the mapped first page of the binary in the ELF parser. Specifically, do not allow note reader and interpreter path comparision in the brandelf code to read past end of the page. This may happen if specially crafter ELF image is activated. Submitted by: Lukasz Wojcik MFC after: 3 days Modified: head/sys/kern/imgact_elf.c Modified: head/sys/kern/imgact_elf.c ============================================================================== --- head/sys/kern/imgact_elf.c Thu Jul 19 10:28:29 2012 (r238616) +++ head/sys/kern/imgact_elf.c Thu Jul 19 11:15:53 2012 (r238617) @@ -83,7 +83,7 @@ __FBSDID("$FreeBSD$"); static int __elfN(check_header)(const Elf_Ehdr *hdr); static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp, - const char *interp, int32_t *osrel); + const char *interp, int interp_name_len, int32_t *osrel); static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr, u_long *entry, size_t pagesize); static int __elfN(load_section)(struct image_params *imgp, vm_offset_t offset, @@ -254,7 +254,7 @@ __elfN(brand_inuse)(Elf_Brandinfo *entry static Elf_Brandinfo * __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, - int32_t *osrel) + int interp_name_len, int32_t *osrel) { const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; Elf_Brandinfo *bi; @@ -300,7 +300,10 @@ __elfN(get_brandinfo)(struct image_param if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) continue; if (hdr->e_machine == bi->machine && - strcmp(interp, bi->interp_path) == 0) + /* ELF image p_filesz includes terminating zero */ + strlen(bi->interp_path) + 1 == interp_name_len && + strncmp(interp, bi->interp_path, interp_name_len) + == 0) return (bi); } } @@ -722,7 +725,7 @@ __CONCAT(exec_, __elfN(imgact))(struct i u_long seg_size, seg_addr; u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0; int32_t osrel = 0; - int error = 0, i, n; + int error = 0, i, n, interp_name_len = 0; const char *interp = NULL, *newinterp = NULL; Elf_Brandinfo *brand_info; char *path; @@ -763,9 +766,11 @@ __CONCAT(exec_, __elfN(imgact))(struct i case PT_INTERP: /* Path to interpreter */ if (phdr[i].p_filesz > MAXPATHLEN || - phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) + phdr[i].p_offset >= PAGE_SIZE || + phdr[i].p_offset + phdr[i].p_filesz >= PAGE_SIZE) return (ENOEXEC); interp = imgp->image_header + phdr[i].p_offset; + interp_name_len = phdr[i].p_filesz; break; case PT_GNU_STACK: if (__elfN(nxstack)) @@ -775,7 +780,8 @@ __CONCAT(exec_, __elfN(imgact))(struct i } } - brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel); + brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len, + &osrel); if (brand_info == NULL) { uprintf("ELF binary type \"%u\" not known.\n", hdr->e_ident[EI_OSABI]); @@ -1562,6 +1568,7 @@ __elfN(parse_notes)(struct image_params int i; if (pnote == NULL || pnote->p_offset >= PAGE_SIZE || + pnote->p_filesz > PAGE_SIZE || pnote->p_offset + pnote->p_filesz >= PAGE_SIZE) return (FALSE); @@ -1569,15 +1576,17 @@ __elfN(parse_notes)(struct image_params note_end = (const Elf_Note *)(imgp->image_header + pnote->p_offset + pnote->p_filesz); for (i = 0; i < 100 && note >= note0 && note < note_end; i++) { - if (!aligned(note, Elf32_Addr)) + if (!aligned(note, Elf32_Addr) || (const char *)note_end - + (const char *)note < sizeof(Elf_Note)) return (FALSE); if (note->n_namesz != checknote->hdr.n_namesz || note->n_descsz != checknote->hdr.n_descsz || note->n_type != checknote->hdr.n_type) goto nextnote; note_name = (const char *)(note + 1); - if (strncmp(checknote->vendor, note_name, - checknote->hdr.n_namesz) != 0) + if (note_name + checknote->hdr.n_namesz >= + (const char *)note_end || strncmp(checknote->vendor, + note_name, checknote->hdr.n_namesz) != 0) goto nextnote; /*