Merge tag 'pull-work.namei' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-microblaze.git] / fs / coredump.c
index 1c060c0..ebc43f9 100644 (file)
@@ -31,7 +31,6 @@
 #include <linux/tsacct_kern.h>
 #include <linux/cn_proc.h>
 #include <linux/audit.h>
-#include <linux/tracehook.h>
 #include <linux/kmod.h>
 #include <linux/fsnotify.h>
 #include <linux/fs_struct.h>
@@ -42,6 +41,7 @@
 #include <linux/path.h>
 #include <linux/timekeeping.h>
 #include <linux/sysctl.h>
+#include <linux/elf.h>
 
 #include <linux/uaccess.h>
 #include <asm/mmu_context.h>
@@ -53,6 +53,9 @@
 
 #include <trace/events/sched.h>
 
+static bool dump_vma_snapshot(struct coredump_params *cprm);
+static void free_vma_snapshot(struct coredump_params *cprm);
+
 static int core_uses_pid;
 static unsigned int core_pipe_limit;
 static char core_pattern[CORENAME_MAX_SIZE] = "core";
@@ -531,6 +534,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
                 * by any locks.
                 */
                .mm_flags = mm->flags,
+               .vma_meta = NULL,
        };
 
        audit_core_dumps(siginfo->si_signo);
@@ -745,6 +749,9 @@ void do_coredump(const kernel_siginfo_t *siginfo)
                        pr_info("Core dump to |%s disabled\n", cn.corename);
                        goto close_fail;
                }
+               if (!dump_vma_snapshot(&cprm))
+                       goto close_fail;
+
                file_start_write(cprm.file);
                core_dumped = binfmt->core_dump(&cprm);
                /*
@@ -758,6 +765,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
                        dump_emit(&cprm, "", 1);
                }
                file_end_write(cprm.file);
+               free_vma_snapshot(&cprm);
        }
        if (ispipe && core_pipe_limit)
                wait_for_dump_helpers(cprm.file);
@@ -980,6 +988,8 @@ static bool always_dump_vma(struct vm_area_struct *vma)
        return false;
 }
 
+#define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1
+
 /*
  * Decide how much of @vma's contents should be included in a core dump.
  */
@@ -1039,9 +1049,20 @@ static unsigned long vma_dump_size(struct vm_area_struct *vma,
         * dump the first page to aid in determining what was mapped here.
         */
        if (FILTER(ELF_HEADERS) &&
-           vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ) &&
-           (READ_ONCE(file_inode(vma->vm_file)->i_mode) & 0111) != 0)
-               return PAGE_SIZE;
+           vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {
+               if ((READ_ONCE(file_inode(vma->vm_file)->i_mode) & 0111) != 0)
+                       return PAGE_SIZE;
+
+               /*
+                * ELF libraries aren't always executable.
+                * We'll want to check whether the mapping starts with the ELF
+                * magic, but not now - we're holding the mmap lock,
+                * so copy_from_user() doesn't work here.
+                * Use a placeholder instead, and fix it up later in
+                * dump_vma_snapshot().
+                */
+               return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER;
+       }
 
 #undef FILTER
 
@@ -1078,18 +1099,29 @@ static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
        return gate_vma;
 }
 
+static void free_vma_snapshot(struct coredump_params *cprm)
+{
+       if (cprm->vma_meta) {
+               int i;
+               for (i = 0; i < cprm->vma_count; i++) {
+                       struct file *file = cprm->vma_meta[i].file;
+                       if (file)
+                               fput(file);
+               }
+               kvfree(cprm->vma_meta);
+               cprm->vma_meta = NULL;
+       }
+}
+
 /*
  * Under the mmap_lock, take a snapshot of relevant information about the task's
  * VMAs.
  */
-int dump_vma_snapshot(struct coredump_params *cprm, int *vma_count,
-                     struct core_vma_metadata **vma_meta,
-                     size_t *vma_data_size_ptr)
+static bool dump_vma_snapshot(struct coredump_params *cprm)
 {
        struct vm_area_struct *vma, *gate_vma;
        struct mm_struct *mm = current->mm;
        int i;
-       size_t vma_data_size = 0;
 
        /*
         * Once the stack expansion code is fixed to not change VMA bounds
@@ -1097,36 +1129,51 @@ int dump_vma_snapshot(struct coredump_params *cprm, int *vma_count,
         * mmap_lock in read mode.
         */
        if (mmap_write_lock_killable(mm))
-               return -EINTR;
+               return false;
 
+       cprm->vma_data_size = 0;
        gate_vma = get_gate_vma(mm);
-       *vma_count = mm->map_count + (gate_vma ? 1 : 0);
+       cprm->vma_count = mm->map_count + (gate_vma ? 1 : 0);
 
-       *vma_meta = kvmalloc_array(*vma_count, sizeof(**vma_meta), GFP_KERNEL);
-       if (!*vma_meta) {
+       cprm->vma_meta = kvmalloc_array(cprm->vma_count, sizeof(*cprm->vma_meta), GFP_KERNEL);
+       if (!cprm->vma_meta) {
                mmap_write_unlock(mm);
-               return -ENOMEM;
+               return false;
        }
 
        for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
                        vma = next_vma(vma, gate_vma), i++) {
-               struct core_vma_metadata *m = (*vma_meta) + i;
+               struct core_vma_metadata *m = cprm->vma_meta + i;
 
                m->start = vma->vm_start;
                m->end = vma->vm_end;
                m->flags = vma->vm_flags;
                m->dump_size = vma_dump_size(vma, cprm->mm_flags);
+               m->pgoff = vma->vm_pgoff;
 
-               vma_data_size += m->dump_size;
+               m->file = vma->vm_file;
+               if (m->file)
+                       get_file(m->file);
        }
 
        mmap_write_unlock(mm);
 
-       if (WARN_ON(i != *vma_count)) {
-               kvfree(*vma_meta);
-               return -EFAULT;
+       for (i = 0; i < cprm->vma_count; i++) {
+               struct core_vma_metadata *m = cprm->vma_meta + i;
+
+               if (m->dump_size == DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER) {
+                       char elfmag[SELFMAG];
+
+                       if (copy_from_user(elfmag, (void __user *)m->start, SELFMAG) ||
+                                       memcmp(elfmag, ELFMAG, SELFMAG) != 0) {
+                               m->dump_size = 0;
+                       } else {
+                               m->dump_size = PAGE_SIZE;
+                       }
+               }
+
+               cprm->vma_data_size += m->dump_size;
        }
 
-       *vma_data_size_ptr = vma_data_size;
-       return 0;
+       return true;
 }