Merge tag 'libnvdimm-for-4.4' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm...
[linux-2.6-microblaze.git] / fs / binfmt_elf_fdpic.c
index 8632501..b1adb92 100644 (file)
@@ -104,19 +104,36 @@ static void __exit exit_elf_fdpic_binfmt(void)
 core_initcall(init_elf_fdpic_binfmt);
 module_exit(exit_elf_fdpic_binfmt);
 
-static int is_elf_fdpic(struct elfhdr *hdr, struct file *file)
+static int is_elf(struct elfhdr *hdr, struct file *file)
 {
        if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
                return 0;
        if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
                return 0;
-       if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr))
+       if (!elf_check_arch(hdr))
                return 0;
        if (!file->f_op->mmap)
                return 0;
        return 1;
 }
 
+#ifndef elf_check_fdpic
+#define elf_check_fdpic(x) 0
+#endif
+
+#ifndef elf_check_const_displacement
+#define elf_check_const_displacement(x) 0
+#endif
+
+static int is_constdisp(struct elfhdr *hdr)
+{
+       if (!elf_check_fdpic(hdr))
+               return 1;
+       if (elf_check_const_displacement(hdr))
+               return 1;
+       return 0;
+}
+
 /*****************************************************************************/
 /*
  * read the program headers table into memory
@@ -192,8 +209,18 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 
        /* check that this is a binary we know how to deal with */
        retval = -ENOEXEC;
-       if (!is_elf_fdpic(&exec_params.hdr, bprm->file))
+       if (!is_elf(&exec_params.hdr, bprm->file))
                goto error;
+       if (!elf_check_fdpic(&exec_params.hdr)) {
+#ifdef CONFIG_MMU
+               /* binfmt_elf handles non-fdpic elf except on nommu */
+               goto error;
+#else
+               /* nommu can only load ET_DYN (PIE) ELF */
+               if (exec_params.hdr.e_type != ET_DYN)
+                       goto error;
+#endif
+       }
 
        /* read the program header table */
        retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
@@ -270,13 +297,13 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 
        }
 
-       if (elf_check_const_displacement(&exec_params.hdr))
+       if (is_constdisp(&exec_params.hdr))
                exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
 
        /* perform insanity checks on the interpreter */
        if (interpreter_name) {
                retval = -ELIBBAD;
-               if (!is_elf_fdpic(&interp_params.hdr, interpreter))
+               if (!is_elf(&interp_params.hdr, interpreter))
                        goto error;
 
                interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
@@ -307,9 +334,9 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 
        retval = -ENOEXEC;
        if (stack_size == 0)
-               goto error;
+               stack_size = 131072UL; /* same as exec.c's default commit */
 
-       if (elf_check_const_displacement(&interp_params.hdr))
+       if (is_constdisp(&interp_params.hdr))
                interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
 
        /* flush all traces of the currently running executable */
@@ -320,7 +347,10 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
        /* there's now no turning back... the old userspace image is dead,
         * defunct, deceased, etc.
         */
-       set_personality(PER_LINUX_FDPIC);
+       if (elf_check_fdpic(&exec_params.hdr))
+               set_personality(PER_LINUX_FDPIC);
+       else
+               set_personality(PER_LINUX);
        if (elf_read_implies_exec(&exec_params.hdr, executable_stack))
                current->personality |= READ_IMPLIES_EXEC;
 
@@ -375,10 +405,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
                PAGE_ALIGN(current->mm->start_brk);
 
 #else
-       /* create a stack and brk area big enough for everyone
-        * - the brk heap starts at the bottom and works up
-        * - the stack starts at the top and works down
-        */
+       /* create a stack area and zero-size brk area */
        stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
        if (stack_size < PAGE_SIZE * 2)
                stack_size = PAGE_SIZE * 2;
@@ -401,8 +428,6 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 
        current->mm->brk = current->mm->start_brk;
        current->mm->context.end_brk = current->mm->start_brk;
-       current->mm->context.end_brk +=
-               (stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
        current->mm->start_stack = current->mm->start_brk + stack_size;
 #endif