1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/slab.h>
3 #include <linux/file.h>
4 #include <linux/fdtable.h>
5 #include <linux/freezer.h>
7 #include <linux/stat.h>
8 #include <linux/fcntl.h>
9 #include <linux/swap.h>
10 #include <linux/ctype.h>
11 #include <linux/string.h>
12 #include <linux/init.h>
13 #include <linux/pagemap.h>
14 #include <linux/perf_event.h>
15 #include <linux/highmem.h>
16 #include <linux/spinlock.h>
17 #include <linux/key.h>
18 #include <linux/personality.h>
19 #include <linux/binfmts.h>
20 #include <linux/coredump.h>
21 #include <linux/sched/coredump.h>
22 #include <linux/sched/signal.h>
23 #include <linux/sched/task_stack.h>
24 #include <linux/utsname.h>
25 #include <linux/pid_namespace.h>
26 #include <linux/module.h>
27 #include <linux/namei.h>
28 #include <linux/mount.h>
29 #include <linux/security.h>
30 #include <linux/syscalls.h>
31 #include <linux/tsacct_kern.h>
32 #include <linux/cn_proc.h>
33 #include <linux/audit.h>
34 #include <linux/tracehook.h>
35 #include <linux/kmod.h>
36 #include <linux/fsnotify.h>
37 #include <linux/fs_struct.h>
38 #include <linux/pipe_fs_i.h>
39 #include <linux/oom.h>
40 #include <linux/compat.h>
42 #include <linux/path.h>
43 #include <linux/timekeeping.h>
44 #include <linux/elf.h>
46 #include <linux/uaccess.h>
47 #include <asm/mmu_context.h>
51 #include <trace/events/task.h>
54 #include <trace/events/sched.h>
57 unsigned int core_pipe_limit;
58 char core_pattern[CORENAME_MAX_SIZE] = "core";
59 static int core_name_size = CORENAME_MAX_SIZE;
66 /* The maximal length of core_pattern is also specified in sysctl.c */
68 static int expand_corename(struct core_name *cn, int size)
70 char *corename = krealloc(cn->corename, size, GFP_KERNEL);
75 if (size > core_name_size) /* racy but harmless */
76 core_name_size = size;
78 cn->size = ksize(corename);
79 cn->corename = corename;
83 static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt,
90 free = cn->size - cn->used;
92 va_copy(arg_copy, arg);
93 need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
101 if (!expand_corename(cn, cn->size + need - free + 1))
107 static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...)
113 ret = cn_vprintf(cn, fmt, arg);
119 static __printf(2, 3)
120 int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
127 ret = cn_vprintf(cn, fmt, arg);
132 * Ensure that this coredump name component can't cause the
133 * resulting corefile path to consist of a ".." or ".".
135 if ((cn->used - cur == 1 && cn->corename[cur] == '.') ||
136 (cn->used - cur == 2 && cn->corename[cur] == '.'
137 && cn->corename[cur+1] == '.'))
138 cn->corename[cur] = '!';
141 * Empty names are fishy and could be used to create a "//" in a
142 * corefile name, causing the coredump to happen one directory
143 * level too high. Enforce that all components of the core
144 * pattern are at least one character long.
147 ret = cn_printf(cn, "!");
150 for (; cur < cn->used; ++cur) {
151 if (cn->corename[cur] == '/')
152 cn->corename[cur] = '!';
157 static int cn_print_exe_file(struct core_name *cn, bool name_only)
159 struct file *exe_file;
160 char *pathbuf, *path, *ptr;
163 exe_file = get_mm_exe_file(current->mm);
165 return cn_esc_printf(cn, "%s (path unknown)", current->comm);
167 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
173 path = file_path(exe_file, pathbuf, PATH_MAX);
180 ptr = strrchr(path, '/');
184 ret = cn_esc_printf(cn, "%s", path);
193 /* format_corename will inspect the pattern parameter, and output a
194 * name into corename, which must have space for at least
195 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
197 static int format_corename(struct core_name *cn, struct coredump_params *cprm,
198 size_t **argv, int *argc)
200 const struct cred *cred = current_cred();
201 const char *pat_ptr = core_pattern;
202 int ispipe = (*pat_ptr == '|');
203 bool was_space = false;
204 int pid_in_pattern = 0;
209 if (expand_corename(cn, core_name_size))
211 cn->corename[0] = '\0';
214 int argvs = sizeof(core_pattern) / 2;
215 (*argv) = kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL);
218 (*argv)[(*argc)++] = 0;
224 /* Repeat as long as we have more pattern to process and more output
228 * Split on spaces before doing template expansion so that
229 * %e and %E don't get split if they have spaces in them
232 if (isspace(*pat_ptr)) {
237 } else if (was_space) {
239 err = cn_printf(cn, "%c", '\0');
242 (*argv)[(*argc)++] = cn->used;
245 if (*pat_ptr != '%') {
246 err = cn_printf(cn, "%c", *pat_ptr++);
248 switch (*++pat_ptr) {
249 /* single % at the end, drop that */
252 /* Double percent, output one percent */
254 err = cn_printf(cn, "%c", '%');
259 err = cn_printf(cn, "%d",
260 task_tgid_vnr(current));
264 err = cn_printf(cn, "%d",
265 task_tgid_nr(current));
268 err = cn_printf(cn, "%d",
269 task_pid_vnr(current));
272 err = cn_printf(cn, "%d",
273 task_pid_nr(current));
277 err = cn_printf(cn, "%u",
278 from_kuid(&init_user_ns,
283 err = cn_printf(cn, "%u",
284 from_kgid(&init_user_ns,
288 err = cn_printf(cn, "%d",
289 __get_dumpable(cprm->mm_flags));
291 /* signal that caused the coredump */
293 err = cn_printf(cn, "%d",
294 cprm->siginfo->si_signo);
296 /* UNIX time of coredump */
300 time = ktime_get_real_seconds();
301 err = cn_printf(cn, "%lld", time);
307 err = cn_esc_printf(cn, "%s",
308 utsname()->nodename);
311 /* executable, could be changed by prctl PR_SET_NAME etc */
313 err = cn_esc_printf(cn, "%s", current->comm);
315 /* file name of executable */
317 err = cn_print_exe_file(cn, true);
320 err = cn_print_exe_file(cn, false);
322 /* core limit size */
324 err = cn_printf(cn, "%lu",
325 rlimit(RLIMIT_CORE));
338 /* Backward compatibility with core_uses_pid:
340 * If core_pattern does not include a %p (as is the default)
341 * and core_uses_pid is set, then .%pid will be appended to
342 * the filename. Do not do this for piped commands. */
343 if (!ispipe && !pid_in_pattern && core_uses_pid) {
344 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
351 static int zap_process(struct task_struct *start, int exit_code, int flags)
353 struct task_struct *t;
356 /* ignore all signals except SIGKILL, see prepare_signal() */
357 start->signal->flags = SIGNAL_GROUP_COREDUMP | flags;
358 start->signal->group_exit_code = exit_code;
359 start->signal->group_stop_count = 0;
361 for_each_thread(start, t) {
362 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
363 if (t != current && t->mm) {
364 sigaddset(&t->pending.signal, SIGKILL);
365 signal_wake_up(t, 1);
373 static int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
374 struct core_state *core_state, int exit_code)
376 struct task_struct *g, *p;
380 spin_lock_irq(&tsk->sighand->siglock);
381 if (!signal_group_exit(tsk->signal)) {
382 mm->core_state = core_state;
383 tsk->signal->group_exit_task = tsk;
384 nr = zap_process(tsk, exit_code, 0);
385 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
387 spin_unlock_irq(&tsk->sighand->siglock);
388 if (unlikely(nr < 0))
391 tsk->flags |= PF_DUMPCORE;
392 if (atomic_read(&mm->mm_users) == nr + 1)
395 * We should find and kill all tasks which use this mm, and we should
396 * count them correctly into ->nr_threads. We don't take tasklist
397 * lock, but this is safe wrt:
400 * None of sub-threads can fork after zap_process(leader). All
401 * processes which were created before this point should be
402 * visible to zap_threads() because copy_process() adds the new
403 * process to the tail of init_task.tasks list, and lock/unlock
404 * of ->siglock provides a memory barrier.
407 * The caller holds mm->mmap_lock. This means that the task which
408 * uses this mm can't pass exit_mm(), so it can't exit or clear
412 * It does list_replace_rcu(&leader->tasks, ¤t->tasks),
413 * we must see either old or new leader, this does not matter.
414 * However, it can change p->sighand, so lock_task_sighand(p)
415 * must be used. Since p->mm != NULL and we hold ->mmap_lock
418 * Note also that "g" can be the old leader with ->mm == NULL
419 * and already unhashed and thus removed from ->thread_group.
420 * This is OK, __unhash_process()->list_del_rcu() does not
421 * clear the ->next pointer, we will find the new leader via
425 for_each_process(g) {
426 if (g == tsk->group_leader)
428 if (g->flags & PF_KTHREAD)
431 for_each_thread(g, p) {
432 if (unlikely(!p->mm))
434 if (unlikely(p->mm == mm)) {
435 lock_task_sighand(p, &flags);
436 nr += zap_process(p, exit_code,
438 unlock_task_sighand(p, &flags);
445 atomic_set(&core_state->nr_threads, nr);
449 static int coredump_wait(int exit_code, struct core_state *core_state)
451 struct task_struct *tsk = current;
452 struct mm_struct *mm = tsk->mm;
453 int core_waiters = -EBUSY;
455 init_completion(&core_state->startup);
456 core_state->dumper.task = tsk;
457 core_state->dumper.next = NULL;
459 if (mmap_write_lock_killable(mm))
463 core_waiters = zap_threads(tsk, mm, core_state, exit_code);
464 mmap_write_unlock(mm);
466 if (core_waiters > 0) {
467 struct core_thread *ptr;
469 freezer_do_not_count();
470 wait_for_completion(&core_state->startup);
473 * Wait for all the threads to become inactive, so that
474 * all the thread context (extended register state, like
475 * fpu etc) gets copied to the memory.
477 ptr = core_state->dumper.next;
478 while (ptr != NULL) {
479 wait_task_inactive(ptr->task, 0);
487 static void coredump_finish(struct mm_struct *mm, bool core_dumped)
489 struct core_thread *curr, *next;
490 struct task_struct *task;
492 spin_lock_irq(¤t->sighand->siglock);
493 if (core_dumped && !__fatal_signal_pending(current))
494 current->signal->group_exit_code |= 0x80;
495 current->signal->group_exit_task = NULL;
496 current->signal->flags = SIGNAL_GROUP_EXIT;
497 spin_unlock_irq(¤t->sighand->siglock);
499 next = mm->core_state->dumper.next;
500 while ((curr = next) != NULL) {
504 * see exit_mm(), curr->task must not see
505 * ->task == NULL before we read ->next.
509 wake_up_process(task);
512 mm->core_state = NULL;
515 static bool dump_interrupted(void)
518 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
519 * can do try_to_freeze() and check __fatal_signal_pending(),
520 * but then we need to teach dump_write() to restart and clear
523 return fatal_signal_pending(current) || freezing(current);
526 static void wait_for_dump_helpers(struct file *file)
528 struct pipe_inode_info *pipe = file->private_data;
533 wake_up_interruptible_sync(&pipe->rd_wait);
534 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
538 * We actually want wait_event_freezable() but then we need
539 * to clear TIF_SIGPENDING and improve dump_interrupted().
541 wait_event_interruptible(pipe->rd_wait, pipe->readers == 1);
551 * helper function to customize the process used
552 * to collect the core in userspace. Specifically
553 * it sets up a pipe and installs it as fd 0 (stdin)
554 * for the process. Returns 0 on success, or
555 * PTR_ERR on failure.
556 * Note that it also sets the core limit to 1. This
557 * is a special value that we use to trap recursive
560 static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
562 struct file *files[2];
563 struct coredump_params *cp = (struct coredump_params *)info->data;
564 int err = create_pipe_files(files, 0);
570 err = replace_fd(0, files[0], 0);
572 /* and disallow core files too */
573 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
578 void do_coredump(const kernel_siginfo_t *siginfo)
580 struct core_state core_state;
582 struct mm_struct *mm = current->mm;
583 struct linux_binfmt * binfmt;
584 const struct cred *old_cred;
590 /* require nonrelative corefile path and be extra careful */
591 bool need_suid_safe = false;
592 bool core_dumped = false;
593 static atomic_t core_dump_count = ATOMIC_INIT(0);
594 struct coredump_params cprm = {
596 .regs = signal_pt_regs(),
597 .limit = rlimit(RLIMIT_CORE),
599 * We must use the same mm->flags while dumping core to avoid
600 * inconsistency of bit flags, since this flag is not protected
603 .mm_flags = mm->flags,
606 audit_core_dumps(siginfo->si_signo);
609 if (!binfmt || !binfmt->core_dump)
611 if (!__get_dumpable(cprm.mm_flags))
614 cred = prepare_creds();
618 * We cannot trust fsuid as being the "true" uid of the process
619 * nor do we know its entire history. We only know it was tainted
620 * so we dump it as root in mode 2, and only into a controlled
621 * environment (pipe handler or fully qualified path).
623 if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
624 /* Setuid core dump mode */
625 cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
626 need_suid_safe = true;
629 retval = coredump_wait(siginfo->si_signo, &core_state);
633 old_cred = override_creds(cred);
635 ispipe = format_corename(&cn, &cprm, &argv, &argc);
641 struct subprocess_info *sub_info;
644 printk(KERN_WARNING "format_corename failed\n");
645 printk(KERN_WARNING "Aborting core\n");
649 if (cprm.limit == 1) {
650 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
652 * Normally core limits are irrelevant to pipes, since
653 * we're not writing to the file system, but we use
654 * cprm.limit of 1 here as a special value, this is a
655 * consistent way to catch recursive crashes.
656 * We can still crash if the core_pattern binary sets
657 * RLIM_CORE = !1, but it runs as root, and can do
658 * lots of stupid things.
660 * Note that we use task_tgid_vnr here to grab the pid
661 * of the process group leader. That way we get the
662 * right pid if a thread in a multi-threaded
663 * core_pattern process dies.
666 "Process %d(%s) has RLIMIT_CORE set to 1\n",
667 task_tgid_vnr(current), current->comm);
668 printk(KERN_WARNING "Aborting core\n");
671 cprm.limit = RLIM_INFINITY;
673 dump_count = atomic_inc_return(&core_dump_count);
674 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
675 printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
676 task_tgid_vnr(current), current->comm);
677 printk(KERN_WARNING "Skipping core dump\n");
681 helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv),
684 printk(KERN_WARNING "%s failed to allocate memory\n",
688 for (argi = 0; argi < argc; argi++)
689 helper_argv[argi] = cn.corename + argv[argi];
690 helper_argv[argi] = NULL;
693 sub_info = call_usermodehelper_setup(helper_argv[0],
694 helper_argv, NULL, GFP_KERNEL,
695 umh_pipe_setup, NULL, &cprm);
697 retval = call_usermodehelper_exec(sub_info,
702 printk(KERN_INFO "Core dump to |%s pipe failed\n",
707 struct user_namespace *mnt_userns;
709 int open_flags = O_CREAT | O_RDWR | O_NOFOLLOW |
710 O_LARGEFILE | O_EXCL;
712 if (cprm.limit < binfmt->min_coredump)
715 if (need_suid_safe && cn.corename[0] != '/') {
716 printk(KERN_WARNING "Pid %d(%s) can only dump core "\
717 "to fully qualified path!\n",
718 task_tgid_vnr(current), current->comm);
719 printk(KERN_WARNING "Skipping core dump\n");
724 * Unlink the file if it exists unless this is a SUID
725 * binary - in that case, we're running around with root
726 * privs and don't want to unlink another user's coredump.
728 if (!need_suid_safe) {
730 * If it doesn't exist, that's fine. If there's some
731 * other problem, we'll catch it at the filp_open().
733 do_unlinkat(AT_FDCWD, getname_kernel(cn.corename));
737 * There is a race between unlinking and creating the
738 * file, but if that causes an EEXIST here, that's
739 * fine - another process raced with us while creating
740 * the corefile, and the other process won. To userspace,
741 * what matters is that at least one of the two processes
742 * writes its coredump successfully, not which one.
744 if (need_suid_safe) {
746 * Using user namespaces, normal user tasks can change
747 * their current->fs->root to point to arbitrary
748 * directories. Since the intention of the "only dump
749 * with a fully qualified path" rule is to control where
750 * coredumps may be placed using root privileges,
751 * current->fs->root must not be used. Instead, use the
752 * root directory of init_task.
756 task_lock(&init_task);
757 get_fs_root(init_task.fs, &root);
758 task_unlock(&init_task);
759 cprm.file = file_open_root(&root, cn.corename,
763 cprm.file = filp_open(cn.corename, open_flags, 0600);
765 if (IS_ERR(cprm.file))
768 inode = file_inode(cprm.file);
769 if (inode->i_nlink > 1)
771 if (d_unhashed(cprm.file->f_path.dentry))
774 * AK: actually i see no reason to not allow this for named
775 * pipes etc, but keep the previous behaviour for now.
777 if (!S_ISREG(inode->i_mode))
780 * Don't dump core if the filesystem changed owner or mode
781 * of the file during file creation. This is an issue when
782 * a process dumps core while its cwd is e.g. on a vfat
785 mnt_userns = file_mnt_user_ns(cprm.file);
786 if (!uid_eq(i_uid_into_mnt(mnt_userns, inode),
788 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file owner\n",
792 if ((inode->i_mode & 0677) != 0600) {
793 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file permissions\n",
797 if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
799 if (do_truncate(mnt_userns, cprm.file->f_path.dentry,
804 /* get us an unshared descriptor table; almost always a no-op */
805 /* The cell spufs coredump code reads the file descriptor tables */
806 retval = unshare_files();
809 if (!dump_interrupted()) {
811 * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
812 * have this set to NULL.
815 pr_info("Core dump to |%s disabled\n", cn.corename);
818 file_start_write(cprm.file);
819 core_dumped = binfmt->core_dump(&cprm);
821 * Ensures that file size is big enough to contain the current
822 * file postion. This prevents gdb from complaining about
823 * a truncated file if the last "write" to the file was
828 dump_emit(&cprm, "", 1);
830 file_end_write(cprm.file);
832 if (ispipe && core_pipe_limit)
833 wait_for_dump_helpers(cprm.file);
836 filp_close(cprm.file, NULL);
839 atomic_dec(&core_dump_count);
843 coredump_finish(mm, core_dumped);
844 revert_creds(old_cred);
852 * Core dumping helper functions. These are the only things you should
853 * do on a core-file: use only these functions to write out all the
856 static int __dump_emit(struct coredump_params *cprm, const void *addr, int nr)
858 struct file *file = cprm->file;
859 loff_t pos = file->f_pos;
861 if (cprm->written + nr > cprm->limit)
865 if (dump_interrupted())
867 n = __kernel_write(file, addr, nr, &pos);
877 static int __dump_skip(struct coredump_params *cprm, size_t nr)
879 static char zeroes[PAGE_SIZE];
880 struct file *file = cprm->file;
881 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
882 if (dump_interrupted() ||
883 file->f_op->llseek(file, nr, SEEK_CUR) < 0)
888 while (nr > PAGE_SIZE) {
889 if (!__dump_emit(cprm, zeroes, PAGE_SIZE))
893 return __dump_emit(cprm, zeroes, nr);
897 int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
900 if (!__dump_skip(cprm, cprm->to_skip))
904 return __dump_emit(cprm, addr, nr);
906 EXPORT_SYMBOL(dump_emit);
908 void dump_skip_to(struct coredump_params *cprm, unsigned long pos)
910 cprm->to_skip = pos - cprm->pos;
912 EXPORT_SYMBOL(dump_skip_to);
914 void dump_skip(struct coredump_params *cprm, size_t nr)
918 EXPORT_SYMBOL(dump_skip);
920 #ifdef CONFIG_ELF_CORE
921 int dump_user_range(struct coredump_params *cprm, unsigned long start,
926 for (addr = start; addr < start + len; addr += PAGE_SIZE) {
931 * To avoid having to allocate page tables for virtual address
932 * ranges that have never been used yet, and also to make it
933 * easy to generate sparse core files, use a helper that returns
934 * NULL when encountering an empty page table entry that would
935 * otherwise have been filled with the zero page.
937 page = get_dump_page(addr);
939 void *kaddr = kmap_local_page(page);
941 stop = !dump_emit(cprm, kaddr, PAGE_SIZE);
947 dump_skip(cprm, PAGE_SIZE);
954 int dump_align(struct coredump_params *cprm, int align)
956 unsigned mod = (cprm->pos + cprm->to_skip) & (align - 1);
957 if (align & (align - 1))
960 cprm->to_skip += align - mod;
963 EXPORT_SYMBOL(dump_align);
966 * The purpose of always_dump_vma() is to make sure that special kernel mappings
967 * that are useful for post-mortem analysis are included in every core dump.
968 * In that way we ensure that the core dump is fully interpretable later
969 * without matching up the same kernel and hardware config to see what PC values
970 * meant. These special mappings include - vDSO, vsyscall, and other
971 * architecture specific mappings
973 static bool always_dump_vma(struct vm_area_struct *vma)
975 /* Any vsyscall mappings? */
976 if (vma == get_gate_vma(vma->vm_mm))
980 * Assume that all vmas with a .name op should always be dumped.
981 * If this changes, a new vm_ops field can easily be added.
983 if (vma->vm_ops && vma->vm_ops->name && vma->vm_ops->name(vma))
987 * arch_vma_name() returns non-NULL for special architecture mappings,
988 * such as vDSO sections.
990 if (arch_vma_name(vma))
996 #define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1
999 * Decide how much of @vma's contents should be included in a core dump.
1001 static unsigned long vma_dump_size(struct vm_area_struct *vma,
1002 unsigned long mm_flags)
1004 #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1006 /* always dump the vdso and vsyscall sections */
1007 if (always_dump_vma(vma))
1010 if (vma->vm_flags & VM_DONTDUMP)
1013 /* support for DAX */
1014 if (vma_is_dax(vma)) {
1015 if ((vma->vm_flags & VM_SHARED) && FILTER(DAX_SHARED))
1017 if (!(vma->vm_flags & VM_SHARED) && FILTER(DAX_PRIVATE))
1022 /* Hugetlb memory check */
1023 if (is_vm_hugetlb_page(vma)) {
1024 if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED))
1026 if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE))
1031 /* Do not dump I/O mapped devices or special mappings */
1032 if (vma->vm_flags & VM_IO)
1035 /* By default, dump shared memory if mapped from an anonymous file. */
1036 if (vma->vm_flags & VM_SHARED) {
1037 if (file_inode(vma->vm_file)->i_nlink == 0 ?
1038 FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
1043 /* Dump segments that have been written to. */
1044 if ((!IS_ENABLED(CONFIG_MMU) || vma->anon_vma) && FILTER(ANON_PRIVATE))
1046 if (vma->vm_file == NULL)
1049 if (FILTER(MAPPED_PRIVATE))
1053 * If this is the beginning of an executable file mapping,
1054 * dump the first page to aid in determining what was mapped here.
1056 if (FILTER(ELF_HEADERS) &&
1057 vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {
1058 if ((READ_ONCE(file_inode(vma->vm_file)->i_mode) & 0111) != 0)
1062 * ELF libraries aren't always executable.
1063 * We'll want to check whether the mapping starts with the ELF
1064 * magic, but not now - we're holding the mmap lock,
1065 * so copy_from_user() doesn't work here.
1066 * Use a placeholder instead, and fix it up later in
1067 * dump_vma_snapshot().
1069 return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER;
1077 return vma->vm_end - vma->vm_start;
1080 static struct vm_area_struct *first_vma(struct task_struct *tsk,
1081 struct vm_area_struct *gate_vma)
1083 struct vm_area_struct *ret = tsk->mm->mmap;
1091 * Helper function for iterating across a vma list. It ensures that the caller
1092 * will visit `gate_vma' prior to terminating the search.
1094 static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
1095 struct vm_area_struct *gate_vma)
1097 struct vm_area_struct *ret;
1099 ret = this_vma->vm_next;
1102 if (this_vma == gate_vma)
1108 * Under the mmap_lock, take a snapshot of relevant information about the task's
1111 int dump_vma_snapshot(struct coredump_params *cprm, int *vma_count,
1112 struct core_vma_metadata **vma_meta,
1113 size_t *vma_data_size_ptr)
1115 struct vm_area_struct *vma, *gate_vma;
1116 struct mm_struct *mm = current->mm;
1118 size_t vma_data_size = 0;
1121 * Once the stack expansion code is fixed to not change VMA bounds
1122 * under mmap_lock in read mode, this can be changed to take the
1123 * mmap_lock in read mode.
1125 if (mmap_write_lock_killable(mm))
1128 gate_vma = get_gate_vma(mm);
1129 *vma_count = mm->map_count + (gate_vma ? 1 : 0);
1131 *vma_meta = kvmalloc_array(*vma_count, sizeof(**vma_meta), GFP_KERNEL);
1133 mmap_write_unlock(mm);
1137 for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
1138 vma = next_vma(vma, gate_vma), i++) {
1139 struct core_vma_metadata *m = (*vma_meta) + i;
1141 m->start = vma->vm_start;
1142 m->end = vma->vm_end;
1143 m->flags = vma->vm_flags;
1144 m->dump_size = vma_dump_size(vma, cprm->mm_flags);
1147 mmap_write_unlock(mm);
1149 if (WARN_ON(i != *vma_count)) {
1154 for (i = 0; i < *vma_count; i++) {
1155 struct core_vma_metadata *m = (*vma_meta) + i;
1157 if (m->dump_size == DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER) {
1158 char elfmag[SELFMAG];
1160 if (copy_from_user(elfmag, (void __user *)m->start, SELFMAG) ||
1161 memcmp(elfmag, ELFMAG, SELFMAG) != 0) {
1164 m->dump_size = PAGE_SIZE;
1168 vma_data_size += m->dump_size;
1171 *vma_data_size_ptr = vma_data_size;