Date: Fri, 11 Sep 2020 10:11:04 +0000 (UTC) From: Konstantin Belousov <kib@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r365629 - stable/11/libexec/rtld-elf Message-ID: <202009111011.08BAB4HJ039843@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: kib Date: Fri Sep 11 10:11:04 2020 New Revision: 365629 URL: https://svnweb.freebsd.org/changeset/base/365629 Log: MFC r365360, r365370: rtld: Handle ELF dso with program headers outside the first page. PR: 229708 Modified: stable/11/libexec/rtld-elf/map_object.c Directory Properties: stable/11/ (props changed) Modified: stable/11/libexec/rtld-elf/map_object.c ============================================================================== --- stable/11/libexec/rtld-elf/map_object.c Fri Sep 11 10:07:09 2020 (r365628) +++ stable/11/libexec/rtld-elf/map_object.c Fri Sep 11 10:11:04 2020 (r365629) @@ -38,11 +38,19 @@ #include "debug.h" #include "rtld.h" -static Elf_Ehdr *get_elf_header(int, const char *, const struct stat *); +static Elf_Ehdr *get_elf_header(int, const char *, const struct stat *, + Elf_Phdr **phdr); static int convert_flags(int); /* Elf flags -> mmap flags */ int __getosreldate(void); +static bool +phdr_in_zero_page(const Elf_Ehdr *hdr) +{ + return (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) <= + (size_t)PAGE_SIZE); +} + /* * Map a shared object into memory. The "fd" argument is a file descriptor, * which must be open on the object and positioned at its beginning. @@ -92,17 +100,15 @@ map_object(int fd, const char *path, const struct stat char *note_map; size_t note_map_len; - hdr = get_elf_header(fd, path, sb); + hdr = get_elf_header(fd, path, sb, &phdr); if (hdr == NULL) return (NULL); /* * Scan the program header entries, and save key information. - * * We expect that the loadable segments are ordered by load address. */ - phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff); - phsize = hdr->e_phnum * sizeof (phdr[0]); + phsize = hdr->e_phnum * sizeof(phdr[0]); phlimit = phdr + hdr->e_phnum; nsegs = -1; phdyn = phinterp = phtls = NULL; @@ -324,14 +330,18 @@ error1: error: if (note_map != NULL && note_map != MAP_FAILED) munmap(note_map, note_map_len); + if (!phdr_in_zero_page(hdr)) + munmap(phdr, hdr->e_phnum * sizeof(phdr[0])); munmap(hdr, PAGE_SIZE); return (NULL); } static Elf_Ehdr * -get_elf_header(int fd, const char *path, const struct stat *sbp) +get_elf_header(int fd, const char *path, const struct stat *sbp, + Elf_Phdr **phdr_p) { Elf_Ehdr *hdr; + Elf_Phdr *phdr; /* Make sure file has enough data for the ELF header */ if (sbp != NULL && sbp->st_size < sizeof(Elf_Ehdr)) { @@ -380,11 +390,19 @@ get_elf_header(int fd, const char *path, const struct "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path); goto error; } - if (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) > - (size_t)PAGE_SIZE) { - _rtld_error("%s: program header too large", path); - goto error; + if (phdr_in_zero_page(hdr)) { + phdr = (Elf_Phdr *)((char *)hdr + hdr->e_phoff); + } else { + phdr = mmap(NULL, hdr->e_phnum * sizeof(phdr[0]), + PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ, fd, + hdr->e_phoff); + if (phdr == MAP_FAILED) { + _rtld_error("%s: error mapping phdr: %s", path, + rtld_strerror(errno)); + goto error; + } } + *phdr_p = phdr; return (hdr); error:
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202009111011.08BAB4HJ039843>