aboutsummaryrefslogtreecommitdiffstats
path: root/fs/binfmt_elf.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/binfmt_elf.c')
-rw-r--r--fs/binfmt_elf.c207
1 files changed, 105 insertions, 102 deletions
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index b9b3bb51b1e4..535e763ab1a6 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -31,6 +31,7 @@
31#include <linux/random.h> 31#include <linux/random.h>
32#include <linux/elf.h> 32#include <linux/elf.h>
33#include <linux/utsname.h> 33#include <linux/utsname.h>
34#include <linux/coredump.h>
34#include <asm/uaccess.h> 35#include <asm/uaccess.h>
35#include <asm/param.h> 36#include <asm/param.h>
36#include <asm/page.h> 37#include <asm/page.h>
@@ -44,8 +45,8 @@ static unsigned long elf_map(struct file *, unsigned long, struct elf_phdr *,
44 * If we don't support core dumping, then supply a NULL so we 45 * If we don't support core dumping, then supply a NULL so we
45 * don't even try. 46 * don't even try.
46 */ 47 */
47#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 48#ifdef CONFIG_ELF_CORE
48static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit); 49static int elf_core_dump(struct coredump_params *cprm);
49#else 50#else
50#define elf_core_dump NULL 51#define elf_core_dump NULL
51#endif 52#endif
@@ -662,27 +663,6 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
662 if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0') 663 if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
663 goto out_free_interp; 664 goto out_free_interp;
664 665
665 /*
666 * The early SET_PERSONALITY here is so that the lookup
667 * for the interpreter happens in the namespace of the
668 * to-be-execed image. SET_PERSONALITY can select an
669 * alternate root.
670 *
671 * However, SET_PERSONALITY is NOT allowed to switch
672 * this task into the new images's memory mapping
673 * policy - that is, TASK_SIZE must still evaluate to
674 * that which is appropriate to the execing application.
675 * This is because exit_mmap() needs to have TASK_SIZE
676 * evaluate to the size of the old image.
677 *
678 * So if (say) a 64-bit application is execing a 32-bit
679 * application it is the architecture's responsibility
680 * to defer changing the value of TASK_SIZE until the
681 * switch really is going to happen - do this in
682 * flush_thread(). - akpm
683 */
684 SET_PERSONALITY(loc->elf_ex);
685
686 interpreter = open_exec(elf_interpreter); 666 interpreter = open_exec(elf_interpreter);
687 retval = PTR_ERR(interpreter); 667 retval = PTR_ERR(interpreter);
688 if (IS_ERR(interpreter)) 668 if (IS_ERR(interpreter))
@@ -730,9 +710,6 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
730 /* Verify the interpreter has a valid arch */ 710 /* Verify the interpreter has a valid arch */
731 if (!elf_check_arch(&loc->interp_elf_ex)) 711 if (!elf_check_arch(&loc->interp_elf_ex))
732 goto out_free_dentry; 712 goto out_free_dentry;
733 } else {
734 /* Executables without an interpreter also need a personality */
735 SET_PERSONALITY(loc->elf_ex);
736 } 713 }
737 714
738 /* Flush all traces of the currently running executable */ 715 /* Flush all traces of the currently running executable */
@@ -752,7 +729,8 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
752 729
753 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) 730 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
754 current->flags |= PF_RANDOMIZE; 731 current->flags |= PF_RANDOMIZE;
755 arch_pick_mmap_layout(current->mm); 732
733 setup_new_exec(bprm);
756 734
757 /* Do this so that we can load the interpreter, if need be. We will 735 /* Do this so that we can load the interpreter, if need be. We will
758 change some of these later */ 736 change some of these later */
@@ -767,7 +745,7 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
767 745
768 current->mm->start_stack = bprm->p; 746 current->mm->start_stack = bprm->p;
769 747
770 /* Now we do a little grungy work by mmaping the ELF image into 748 /* Now we do a little grungy work by mmapping the ELF image into
771 the correct location in memory. */ 749 the correct location in memory. */
772 for(i = 0, elf_ppnt = elf_phdata; 750 for(i = 0, elf_ppnt = elf_phdata;
773 i < loc->elf_ex.e_phnum; i++, elf_ppnt++) { 751 i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
@@ -1101,48 +1079,13 @@ out:
1101 return error; 1079 return error;
1102} 1080}
1103 1081
1104/* 1082#ifdef CONFIG_ELF_CORE
1105 * Note that some platforms still use traditional core dumps and not
1106 * the ELF core dump. Each platform can select it as appropriate.
1107 */
1108#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1109
1110/* 1083/*
1111 * ELF core dumper 1084 * ELF core dumper
1112 * 1085 *
1113 * Modelled on fs/exec.c:aout_core_dump() 1086 * Modelled on fs/exec.c:aout_core_dump()
1114 * Jeremy Fitzhardinge <jeremy@sw.oz.au> 1087 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1115 */ 1088 */
1116/*
1117 * These are the only things you should do on a core-file: use only these
1118 * functions to write out all the necessary info.
1119 */
1120static int dump_write(struct file *file, const void *addr, int nr)
1121{
1122 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1123}
1124
1125static int dump_seek(struct file *file, loff_t off)
1126{
1127 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
1128 if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
1129 return 0;
1130 } else {
1131 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
1132 if (!buf)
1133 return 0;
1134 while (off > 0) {
1135 unsigned long n = off;
1136 if (n > PAGE_SIZE)
1137 n = PAGE_SIZE;
1138 if (!dump_write(file, buf, n))
1139 return 0;
1140 off -= n;
1141 }
1142 free_page((unsigned long)buf);
1143 }
1144 return 1;
1145}
1146 1089
1147/* 1090/*
1148 * Decide what to dump of a segment, part, all or none. 1091 * Decide what to dump of a segment, part, all or none.
@@ -1277,10 +1220,6 @@ static int writenote(struct memelfnote *men, struct file *file,
1277} 1220}
1278#undef DUMP_WRITE 1221#undef DUMP_WRITE
1279 1222
1280#define DUMP_WRITE(addr, nr) \
1281 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1282 goto end_coredump;
1283
1284static void fill_elf_header(struct elfhdr *elf, int segs, 1223static void fill_elf_header(struct elfhdr *elf, int segs,
1285 u16 machine, u32 flags, u8 osabi) 1224 u16 machine, u32 flags, u8 osabi)
1286{ 1225{
@@ -1899,6 +1838,34 @@ static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
1899 return gate_vma; 1838 return gate_vma;
1900} 1839}
1901 1840
1841static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
1842 elf_addr_t e_shoff, int segs)
1843{
1844 elf->e_shoff = e_shoff;
1845 elf->e_shentsize = sizeof(*shdr4extnum);
1846 elf->e_shnum = 1;
1847 elf->e_shstrndx = SHN_UNDEF;
1848
1849 memset(shdr4extnum, 0, sizeof(*shdr4extnum));
1850
1851 shdr4extnum->sh_type = SHT_NULL;
1852 shdr4extnum->sh_size = elf->e_shnum;
1853 shdr4extnum->sh_link = elf->e_shstrndx;
1854 shdr4extnum->sh_info = segs;
1855}
1856
1857static size_t elf_core_vma_data_size(struct vm_area_struct *gate_vma,
1858 unsigned long mm_flags)
1859{
1860 struct vm_area_struct *vma;
1861 size_t size = 0;
1862
1863 for (vma = first_vma(current, gate_vma); vma != NULL;
1864 vma = next_vma(vma, gate_vma))
1865 size += vma_dump_size(vma, mm_flags);
1866 return size;
1867}
1868
1902/* 1869/*
1903 * Actual dumper 1870 * Actual dumper
1904 * 1871 *
@@ -1906,7 +1873,7 @@ static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
1906 * and then they are actually written out. If we run out of core limit 1873 * and then they are actually written out. If we run out of core limit
1907 * we just truncate. 1874 * we just truncate.
1908 */ 1875 */
1909static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit) 1876static int elf_core_dump(struct coredump_params *cprm)
1910{ 1877{
1911 int has_dumped = 0; 1878 int has_dumped = 0;
1912 mm_segment_t fs; 1879 mm_segment_t fs;
@@ -1915,8 +1882,11 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
1915 struct vm_area_struct *vma, *gate_vma; 1882 struct vm_area_struct *vma, *gate_vma;
1916 struct elfhdr *elf = NULL; 1883 struct elfhdr *elf = NULL;
1917 loff_t offset = 0, dataoff, foffset; 1884 loff_t offset = 0, dataoff, foffset;
1918 unsigned long mm_flags;
1919 struct elf_note_info info; 1885 struct elf_note_info info;
1886 struct elf_phdr *phdr4note = NULL;
1887 struct elf_shdr *shdr4extnum = NULL;
1888 Elf_Half e_phnum;
1889 elf_addr_t e_shoff;
1920 1890
1921 /* 1891 /*
1922 * We no longer stop all VM operations. 1892 * We no longer stop all VM operations.
@@ -1939,20 +1909,25 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
1939 * Please check DEFAULT_MAX_MAP_COUNT definition when you modify here. 1909 * Please check DEFAULT_MAX_MAP_COUNT definition when you modify here.
1940 */ 1910 */
1941 segs = current->mm->map_count; 1911 segs = current->mm->map_count;
1942#ifdef ELF_CORE_EXTRA_PHDRS 1912 segs += elf_core_extra_phdrs();
1943 segs += ELF_CORE_EXTRA_PHDRS;
1944#endif
1945 1913
1946 gate_vma = get_gate_vma(current); 1914 gate_vma = get_gate_vma(current);
1947 if (gate_vma != NULL) 1915 if (gate_vma != NULL)
1948 segs++; 1916 segs++;
1949 1917
1918 /* for notes section */
1919 segs++;
1920
1921 /* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
1922 * this, kernel supports extended numbering. Have a look at
1923 * include/linux/elf.h for further information. */
1924 e_phnum = segs > PN_XNUM ? PN_XNUM : segs;
1925
1950 /* 1926 /*
1951 * Collect all the non-memory information about the process for the 1927 * Collect all the non-memory information about the process for the
1952 * notes. This also sets up the file header. 1928 * notes. This also sets up the file header.
1953 */ 1929 */
1954 if (!fill_note_info(elf, segs + 1, /* including notes section */ 1930 if (!fill_note_info(elf, e_phnum, &info, cprm->signr, cprm->regs))
1955 &info, signr, regs))
1956 goto cleanup; 1931 goto cleanup;
1957 1932
1958 has_dumped = 1; 1933 has_dumped = 1;
@@ -1961,31 +1936,47 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
1961 fs = get_fs(); 1936 fs = get_fs();
1962 set_fs(KERNEL_DS); 1937 set_fs(KERNEL_DS);
1963 1938
1964 DUMP_WRITE(elf, sizeof(*elf));
1965 offset += sizeof(*elf); /* Elf header */ 1939 offset += sizeof(*elf); /* Elf header */
1966 offset += (segs + 1) * sizeof(struct elf_phdr); /* Program headers */ 1940 offset += segs * sizeof(struct elf_phdr); /* Program headers */
1967 foffset = offset; 1941 foffset = offset;
1968 1942
1969 /* Write notes phdr entry */ 1943 /* Write notes phdr entry */
1970 { 1944 {
1971 struct elf_phdr phdr;
1972 size_t sz = get_note_info_size(&info); 1945 size_t sz = get_note_info_size(&info);
1973 1946
1974 sz += elf_coredump_extra_notes_size(); 1947 sz += elf_coredump_extra_notes_size();
1975 1948
1976 fill_elf_note_phdr(&phdr, sz, offset); 1949 phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
1950 if (!phdr4note)
1951 goto end_coredump;
1952
1953 fill_elf_note_phdr(phdr4note, sz, offset);
1977 offset += sz; 1954 offset += sz;
1978 DUMP_WRITE(&phdr, sizeof(phdr));
1979 } 1955 }
1980 1956
1981 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE); 1957 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1982 1958
1983 /* 1959 offset += elf_core_vma_data_size(gate_vma, cprm->mm_flags);
1984 * We must use the same mm->flags while dumping core to avoid 1960 offset += elf_core_extra_data_size();
1985 * inconsistency between the program headers and bodies, otherwise an 1961 e_shoff = offset;
1986 * unusable core file can be generated. 1962
1987 */ 1963 if (e_phnum == PN_XNUM) {
1988 mm_flags = current->mm->flags; 1964 shdr4extnum = kmalloc(sizeof(*shdr4extnum), GFP_KERNEL);
1965 if (!shdr4extnum)
1966 goto end_coredump;
1967 fill_extnum_info(elf, shdr4extnum, e_shoff, segs);
1968 }
1969
1970 offset = dataoff;
1971
1972 size += sizeof(*elf);
1973 if (size > cprm->limit || !dump_write(cprm->file, elf, sizeof(*elf)))
1974 goto end_coredump;
1975
1976 size += sizeof(*phdr4note);
1977 if (size > cprm->limit
1978 || !dump_write(cprm->file, phdr4note, sizeof(*phdr4note)))
1979 goto end_coredump;
1989 1980
1990 /* Write program headers for segments dump */ 1981 /* Write program headers for segments dump */
1991 for (vma = first_vma(current, gate_vma); vma != NULL; 1982 for (vma = first_vma(current, gate_vma); vma != NULL;
@@ -1996,7 +1987,7 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
1996 phdr.p_offset = offset; 1987 phdr.p_offset = offset;
1997 phdr.p_vaddr = vma->vm_start; 1988 phdr.p_vaddr = vma->vm_start;
1998 phdr.p_paddr = 0; 1989 phdr.p_paddr = 0;
1999 phdr.p_filesz = vma_dump_size(vma, mm_flags); 1990 phdr.p_filesz = vma_dump_size(vma, cprm->mm_flags);
2000 phdr.p_memsz = vma->vm_end - vma->vm_start; 1991 phdr.p_memsz = vma->vm_end - vma->vm_start;
2001 offset += phdr.p_filesz; 1992 offset += phdr.p_filesz;
2002 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0; 1993 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
@@ -2006,22 +1997,24 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
2006 phdr.p_flags |= PF_X; 1997 phdr.p_flags |= PF_X;
2007 phdr.p_align = ELF_EXEC_PAGESIZE; 1998 phdr.p_align = ELF_EXEC_PAGESIZE;
2008 1999
2009 DUMP_WRITE(&phdr, sizeof(phdr)); 2000 size += sizeof(phdr);
2001 if (size > cprm->limit
2002 || !dump_write(cprm->file, &phdr, sizeof(phdr)))
2003 goto end_coredump;
2010 } 2004 }
2011 2005
2012#ifdef ELF_CORE_WRITE_EXTRA_PHDRS 2006 if (!elf_core_write_extra_phdrs(cprm->file, offset, &size, cprm->limit))
2013 ELF_CORE_WRITE_EXTRA_PHDRS; 2007 goto end_coredump;
2014#endif
2015 2008
2016 /* write out the notes section */ 2009 /* write out the notes section */
2017 if (!write_note_info(&info, file, &foffset)) 2010 if (!write_note_info(&info, cprm->file, &foffset))
2018 goto end_coredump; 2011 goto end_coredump;
2019 2012
2020 if (elf_coredump_extra_notes_write(file, &foffset)) 2013 if (elf_coredump_extra_notes_write(cprm->file, &foffset))
2021 goto end_coredump; 2014 goto end_coredump;
2022 2015
2023 /* Align to page */ 2016 /* Align to page */
2024 if (!dump_seek(file, dataoff - foffset)) 2017 if (!dump_seek(cprm->file, dataoff - foffset))
2025 goto end_coredump; 2018 goto end_coredump;
2026 2019
2027 for (vma = first_vma(current, gate_vma); vma != NULL; 2020 for (vma = first_vma(current, gate_vma); vma != NULL;
@@ -2029,7 +2022,7 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
2029 unsigned long addr; 2022 unsigned long addr;
2030 unsigned long end; 2023 unsigned long end;
2031 2024
2032 end = vma->vm_start + vma_dump_size(vma, mm_flags); 2025 end = vma->vm_start + vma_dump_size(vma, cprm->mm_flags);
2033 2026
2034 for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) { 2027 for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) {
2035 struct page *page; 2028 struct page *page;
@@ -2038,32 +2031,42 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
2038 page = get_dump_page(addr); 2031 page = get_dump_page(addr);
2039 if (page) { 2032 if (page) {
2040 void *kaddr = kmap(page); 2033 void *kaddr = kmap(page);
2041 stop = ((size += PAGE_SIZE) > limit) || 2034 stop = ((size += PAGE_SIZE) > cprm->limit) ||
2042 !dump_write(file, kaddr, PAGE_SIZE); 2035 !dump_write(cprm->file, kaddr,
2036 PAGE_SIZE);
2043 kunmap(page); 2037 kunmap(page);
2044 page_cache_release(page); 2038 page_cache_release(page);
2045 } else 2039 } else
2046 stop = !dump_seek(file, PAGE_SIZE); 2040 stop = !dump_seek(cprm->file, PAGE_SIZE);
2047 if (stop) 2041 if (stop)
2048 goto end_coredump; 2042 goto end_coredump;
2049 } 2043 }
2050 } 2044 }
2051 2045
2052#ifdef ELF_CORE_WRITE_EXTRA_DATA 2046 if (!elf_core_write_extra_data(cprm->file, &size, cprm->limit))
2053 ELF_CORE_WRITE_EXTRA_DATA; 2047 goto end_coredump;
2054#endif 2048
2049 if (e_phnum == PN_XNUM) {
2050 size += sizeof(*shdr4extnum);
2051 if (size > cprm->limit
2052 || !dump_write(cprm->file, shdr4extnum,
2053 sizeof(*shdr4extnum)))
2054 goto end_coredump;
2055 }
2055 2056
2056end_coredump: 2057end_coredump:
2057 set_fs(fs); 2058 set_fs(fs);
2058 2059
2059cleanup: 2060cleanup:
2060 free_note_info(&info); 2061 free_note_info(&info);
2062 kfree(shdr4extnum);
2063 kfree(phdr4note);
2061 kfree(elf); 2064 kfree(elf);
2062out: 2065out:
2063 return has_dumped; 2066 return has_dumped;
2064} 2067}
2065 2068
2066#endif /* USE_ELF_CORE_DUMP */ 2069#endif /* CONFIG_ELF_CORE */
2067 2070
2068static int __init init_elf_binfmt(void) 2071static int __init init_elf_binfmt(void)
2069{ 2072{