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/sysctl.h>
45 #include <linux/elf.h>
47 #include <linux/uaccess.h>
48 #include <asm/mmu_context.h>
52 #include <trace/events/task.h>
55 #include <trace/events/sched.h>
57 static bool dump_vma_snapshot(struct coredump_params *cprm);
58 static void free_vma_snapshot(struct coredump_params *cprm);
60 static int core_uses_pid;
61 static unsigned int core_pipe_limit;
62 static char core_pattern[CORENAME_MAX_SIZE] = "core";
63 static int core_name_size = CORENAME_MAX_SIZE;
70 static int expand_corename(struct core_name *cn, int size)
72 char *corename = krealloc(cn->corename, size, GFP_KERNEL);
77 if (size > core_name_size) /* racy but harmless */
78 core_name_size = size;
80 cn->size = ksize(corename);
81 cn->corename = corename;
85 static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt,
92 free = cn->size - cn->used;
94 va_copy(arg_copy, arg);
95 need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
103 if (!expand_corename(cn, cn->size + need - free + 1))
109 static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...)
115 ret = cn_vprintf(cn, fmt, arg);
121 static __printf(2, 3)
122 int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
129 ret = cn_vprintf(cn, fmt, arg);
134 * Ensure that this coredump name component can't cause the
135 * resulting corefile path to consist of a ".." or ".".
137 if ((cn->used - cur == 1 && cn->corename[cur] == '.') ||
138 (cn->used - cur == 2 && cn->corename[cur] == '.'
139 && cn->corename[cur+1] == '.'))
140 cn->corename[cur] = '!';
143 * Empty names are fishy and could be used to create a "//" in a
144 * corefile name, causing the coredump to happen one directory
145 * level too high. Enforce that all components of the core
146 * pattern are at least one character long.
149 ret = cn_printf(cn, "!");
152 for (; cur < cn->used; ++cur) {
153 if (cn->corename[cur] == '/')
154 cn->corename[cur] = '!';
159 static int cn_print_exe_file(struct core_name *cn, bool name_only)
161 struct file *exe_file;
162 char *pathbuf, *path, *ptr;
165 exe_file = get_mm_exe_file(current->mm);
167 return cn_esc_printf(cn, "%s (path unknown)", current->comm);
169 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
175 path = file_path(exe_file, pathbuf, PATH_MAX);
182 ptr = strrchr(path, '/');
186 ret = cn_esc_printf(cn, "%s", path);
195 /* format_corename will inspect the pattern parameter, and output a
196 * name into corename, which must have space for at least
197 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
199 static int format_corename(struct core_name *cn, struct coredump_params *cprm,
200 size_t **argv, int *argc)
202 const struct cred *cred = current_cred();
203 const char *pat_ptr = core_pattern;
204 int ispipe = (*pat_ptr == '|');
205 bool was_space = false;
206 int pid_in_pattern = 0;
211 if (expand_corename(cn, core_name_size))
213 cn->corename[0] = '\0';
216 int argvs = sizeof(core_pattern) / 2;
217 (*argv) = kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL);
220 (*argv)[(*argc)++] = 0;
226 /* Repeat as long as we have more pattern to process and more output
230 * Split on spaces before doing template expansion so that
231 * %e and %E don't get split if they have spaces in them
234 if (isspace(*pat_ptr)) {
239 } else if (was_space) {
241 err = cn_printf(cn, "%c", '\0');
244 (*argv)[(*argc)++] = cn->used;
247 if (*pat_ptr != '%') {
248 err = cn_printf(cn, "%c", *pat_ptr++);
250 switch (*++pat_ptr) {
251 /* single % at the end, drop that */
254 /* Double percent, output one percent */
256 err = cn_printf(cn, "%c", '%');
261 err = cn_printf(cn, "%d",
262 task_tgid_vnr(current));
266 err = cn_printf(cn, "%d",
267 task_tgid_nr(current));
270 err = cn_printf(cn, "%d",
271 task_pid_vnr(current));
274 err = cn_printf(cn, "%d",
275 task_pid_nr(current));
279 err = cn_printf(cn, "%u",
280 from_kuid(&init_user_ns,
285 err = cn_printf(cn, "%u",
286 from_kgid(&init_user_ns,
290 err = cn_printf(cn, "%d",
291 __get_dumpable(cprm->mm_flags));
293 /* signal that caused the coredump */
295 err = cn_printf(cn, "%d",
296 cprm->siginfo->si_signo);
298 /* UNIX time of coredump */
302 time = ktime_get_real_seconds();
303 err = cn_printf(cn, "%lld", time);
309 err = cn_esc_printf(cn, "%s",
310 utsname()->nodename);
313 /* executable, could be changed by prctl PR_SET_NAME etc */
315 err = cn_esc_printf(cn, "%s", current->comm);
317 /* file name of executable */
319 err = cn_print_exe_file(cn, true);
322 err = cn_print_exe_file(cn, false);
324 /* core limit size */
326 err = cn_printf(cn, "%lu",
327 rlimit(RLIMIT_CORE));
340 /* Backward compatibility with core_uses_pid:
342 * If core_pattern does not include a %p (as is the default)
343 * and core_uses_pid is set, then .%pid will be appended to
344 * the filename. Do not do this for piped commands. */
345 if (!ispipe && !pid_in_pattern && core_uses_pid) {
346 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
353 static int zap_process(struct task_struct *start, int exit_code)
355 struct task_struct *t;
358 /* ignore all signals except SIGKILL, see prepare_signal() */
359 start->signal->flags = SIGNAL_GROUP_EXIT;
360 start->signal->group_exit_code = exit_code;
361 start->signal->group_stop_count = 0;
363 for_each_thread(start, t) {
364 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
365 if (t != current && !(t->flags & PF_POSTCOREDUMP)) {
366 sigaddset(&t->pending.signal, SIGKILL);
367 signal_wake_up(t, 1);
375 static int zap_threads(struct task_struct *tsk,
376 struct core_state *core_state, int exit_code)
378 struct signal_struct *signal = tsk->signal;
381 spin_lock_irq(&tsk->sighand->siglock);
382 if (!(signal->flags & SIGNAL_GROUP_EXIT) && !signal->group_exec_task) {
383 signal->core_state = core_state;
384 nr = zap_process(tsk, exit_code);
385 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
386 tsk->flags |= PF_DUMPCORE;
387 atomic_set(&core_state->nr_threads, nr);
389 spin_unlock_irq(&tsk->sighand->siglock);
393 static int coredump_wait(int exit_code, struct core_state *core_state)
395 struct task_struct *tsk = current;
396 int core_waiters = -EBUSY;
398 init_completion(&core_state->startup);
399 core_state->dumper.task = tsk;
400 core_state->dumper.next = NULL;
402 core_waiters = zap_threads(tsk, core_state, exit_code);
403 if (core_waiters > 0) {
404 struct core_thread *ptr;
406 freezer_do_not_count();
407 wait_for_completion(&core_state->startup);
410 * Wait for all the threads to become inactive, so that
411 * all the thread context (extended register state, like
412 * fpu etc) gets copied to the memory.
414 ptr = core_state->dumper.next;
415 while (ptr != NULL) {
416 wait_task_inactive(ptr->task, 0);
424 static void coredump_finish(bool core_dumped)
426 struct core_thread *curr, *next;
427 struct task_struct *task;
429 spin_lock_irq(¤t->sighand->siglock);
430 if (core_dumped && !__fatal_signal_pending(current))
431 current->signal->group_exit_code |= 0x80;
432 next = current->signal->core_state->dumper.next;
433 current->signal->core_state = NULL;
434 spin_unlock_irq(¤t->sighand->siglock);
436 while ((curr = next) != NULL) {
440 * see coredump_task_exit(), curr->task must not see
441 * ->task == NULL before we read ->next.
445 wake_up_process(task);
449 static bool dump_interrupted(void)
452 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
453 * can do try_to_freeze() and check __fatal_signal_pending(),
454 * but then we need to teach dump_write() to restart and clear
457 return fatal_signal_pending(current) || freezing(current);
460 static void wait_for_dump_helpers(struct file *file)
462 struct pipe_inode_info *pipe = file->private_data;
467 wake_up_interruptible_sync(&pipe->rd_wait);
468 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
472 * We actually want wait_event_freezable() but then we need
473 * to clear TIF_SIGPENDING and improve dump_interrupted().
475 wait_event_interruptible(pipe->rd_wait, pipe->readers == 1);
485 * helper function to customize the process used
486 * to collect the core in userspace. Specifically
487 * it sets up a pipe and installs it as fd 0 (stdin)
488 * for the process. Returns 0 on success, or
489 * PTR_ERR on failure.
490 * Note that it also sets the core limit to 1. This
491 * is a special value that we use to trap recursive
494 static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
496 struct file *files[2];
497 struct coredump_params *cp = (struct coredump_params *)info->data;
498 int err = create_pipe_files(files, 0);
504 err = replace_fd(0, files[0], 0);
506 /* and disallow core files too */
507 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
512 void do_coredump(const kernel_siginfo_t *siginfo)
514 struct core_state core_state;
516 struct mm_struct *mm = current->mm;
517 struct linux_binfmt * binfmt;
518 const struct cred *old_cred;
524 /* require nonrelative corefile path and be extra careful */
525 bool need_suid_safe = false;
526 bool core_dumped = false;
527 static atomic_t core_dump_count = ATOMIC_INIT(0);
528 struct coredump_params cprm = {
530 .regs = signal_pt_regs(),
531 .limit = rlimit(RLIMIT_CORE),
533 * We must use the same mm->flags while dumping core to avoid
534 * inconsistency of bit flags, since this flag is not protected
537 .mm_flags = mm->flags,
541 audit_core_dumps(siginfo->si_signo);
544 if (!binfmt || !binfmt->core_dump)
546 if (!__get_dumpable(cprm.mm_flags))
549 cred = prepare_creds();
553 * We cannot trust fsuid as being the "true" uid of the process
554 * nor do we know its entire history. We only know it was tainted
555 * so we dump it as root in mode 2, and only into a controlled
556 * environment (pipe handler or fully qualified path).
558 if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
559 /* Setuid core dump mode */
560 cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
561 need_suid_safe = true;
564 retval = coredump_wait(siginfo->si_signo, &core_state);
568 old_cred = override_creds(cred);
570 ispipe = format_corename(&cn, &cprm, &argv, &argc);
576 struct subprocess_info *sub_info;
579 printk(KERN_WARNING "format_corename failed\n");
580 printk(KERN_WARNING "Aborting core\n");
584 if (cprm.limit == 1) {
585 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
587 * Normally core limits are irrelevant to pipes, since
588 * we're not writing to the file system, but we use
589 * cprm.limit of 1 here as a special value, this is a
590 * consistent way to catch recursive crashes.
591 * We can still crash if the core_pattern binary sets
592 * RLIM_CORE = !1, but it runs as root, and can do
593 * lots of stupid things.
595 * Note that we use task_tgid_vnr here to grab the pid
596 * of the process group leader. That way we get the
597 * right pid if a thread in a multi-threaded
598 * core_pattern process dies.
601 "Process %d(%s) has RLIMIT_CORE set to 1\n",
602 task_tgid_vnr(current), current->comm);
603 printk(KERN_WARNING "Aborting core\n");
606 cprm.limit = RLIM_INFINITY;
608 dump_count = atomic_inc_return(&core_dump_count);
609 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
610 printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
611 task_tgid_vnr(current), current->comm);
612 printk(KERN_WARNING "Skipping core dump\n");
616 helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv),
619 printk(KERN_WARNING "%s failed to allocate memory\n",
623 for (argi = 0; argi < argc; argi++)
624 helper_argv[argi] = cn.corename + argv[argi];
625 helper_argv[argi] = NULL;
628 sub_info = call_usermodehelper_setup(helper_argv[0],
629 helper_argv, NULL, GFP_KERNEL,
630 umh_pipe_setup, NULL, &cprm);
632 retval = call_usermodehelper_exec(sub_info,
637 printk(KERN_INFO "Core dump to |%s pipe failed\n",
642 struct user_namespace *mnt_userns;
644 int open_flags = O_CREAT | O_RDWR | O_NOFOLLOW |
645 O_LARGEFILE | O_EXCL;
647 if (cprm.limit < binfmt->min_coredump)
650 if (need_suid_safe && cn.corename[0] != '/') {
651 printk(KERN_WARNING "Pid %d(%s) can only dump core "\
652 "to fully qualified path!\n",
653 task_tgid_vnr(current), current->comm);
654 printk(KERN_WARNING "Skipping core dump\n");
659 * Unlink the file if it exists unless this is a SUID
660 * binary - in that case, we're running around with root
661 * privs and don't want to unlink another user's coredump.
663 if (!need_suid_safe) {
665 * If it doesn't exist, that's fine. If there's some
666 * other problem, we'll catch it at the filp_open().
668 do_unlinkat(AT_FDCWD, getname_kernel(cn.corename));
672 * There is a race between unlinking and creating the
673 * file, but if that causes an EEXIST here, that's
674 * fine - another process raced with us while creating
675 * the corefile, and the other process won. To userspace,
676 * what matters is that at least one of the two processes
677 * writes its coredump successfully, not which one.
679 if (need_suid_safe) {
681 * Using user namespaces, normal user tasks can change
682 * their current->fs->root to point to arbitrary
683 * directories. Since the intention of the "only dump
684 * with a fully qualified path" rule is to control where
685 * coredumps may be placed using root privileges,
686 * current->fs->root must not be used. Instead, use the
687 * root directory of init_task.
691 task_lock(&init_task);
692 get_fs_root(init_task.fs, &root);
693 task_unlock(&init_task);
694 cprm.file = file_open_root(&root, cn.corename,
698 cprm.file = filp_open(cn.corename, open_flags, 0600);
700 if (IS_ERR(cprm.file))
703 inode = file_inode(cprm.file);
704 if (inode->i_nlink > 1)
706 if (d_unhashed(cprm.file->f_path.dentry))
709 * AK: actually i see no reason to not allow this for named
710 * pipes etc, but keep the previous behaviour for now.
712 if (!S_ISREG(inode->i_mode))
715 * Don't dump core if the filesystem changed owner or mode
716 * of the file during file creation. This is an issue when
717 * a process dumps core while its cwd is e.g. on a vfat
720 mnt_userns = file_mnt_user_ns(cprm.file);
721 if (!uid_eq(i_uid_into_mnt(mnt_userns, inode),
723 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file owner\n",
727 if ((inode->i_mode & 0677) != 0600) {
728 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file permissions\n",
732 if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
734 if (do_truncate(mnt_userns, cprm.file->f_path.dentry,
739 /* get us an unshared descriptor table; almost always a no-op */
740 /* The cell spufs coredump code reads the file descriptor tables */
741 retval = unshare_files();
744 if (!dump_interrupted()) {
746 * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
747 * have this set to NULL.
750 pr_info("Core dump to |%s disabled\n", cn.corename);
753 if (!dump_vma_snapshot(&cprm))
756 file_start_write(cprm.file);
757 core_dumped = binfmt->core_dump(&cprm);
759 * Ensures that file size is big enough to contain the current
760 * file postion. This prevents gdb from complaining about
761 * a truncated file if the last "write" to the file was
766 dump_emit(&cprm, "", 1);
768 file_end_write(cprm.file);
769 free_vma_snapshot(&cprm);
771 if (ispipe && core_pipe_limit)
772 wait_for_dump_helpers(cprm.file);
775 filp_close(cprm.file, NULL);
778 atomic_dec(&core_dump_count);
782 coredump_finish(core_dumped);
783 revert_creds(old_cred);
791 * Core dumping helper functions. These are the only things you should
792 * do on a core-file: use only these functions to write out all the
795 static int __dump_emit(struct coredump_params *cprm, const void *addr, int nr)
797 struct file *file = cprm->file;
798 loff_t pos = file->f_pos;
800 if (cprm->written + nr > cprm->limit)
804 if (dump_interrupted())
806 n = __kernel_write(file, addr, nr, &pos);
816 static int __dump_skip(struct coredump_params *cprm, size_t nr)
818 static char zeroes[PAGE_SIZE];
819 struct file *file = cprm->file;
820 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
821 if (dump_interrupted() ||
822 file->f_op->llseek(file, nr, SEEK_CUR) < 0)
827 while (nr > PAGE_SIZE) {
828 if (!__dump_emit(cprm, zeroes, PAGE_SIZE))
832 return __dump_emit(cprm, zeroes, nr);
836 int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
839 if (!__dump_skip(cprm, cprm->to_skip))
843 return __dump_emit(cprm, addr, nr);
845 EXPORT_SYMBOL(dump_emit);
847 void dump_skip_to(struct coredump_params *cprm, unsigned long pos)
849 cprm->to_skip = pos - cprm->pos;
851 EXPORT_SYMBOL(dump_skip_to);
853 void dump_skip(struct coredump_params *cprm, size_t nr)
857 EXPORT_SYMBOL(dump_skip);
859 #ifdef CONFIG_ELF_CORE
860 int dump_user_range(struct coredump_params *cprm, unsigned long start,
865 for (addr = start; addr < start + len; addr += PAGE_SIZE) {
870 * To avoid having to allocate page tables for virtual address
871 * ranges that have never been used yet, and also to make it
872 * easy to generate sparse core files, use a helper that returns
873 * NULL when encountering an empty page table entry that would
874 * otherwise have been filled with the zero page.
876 page = get_dump_page(addr);
878 void *kaddr = kmap_local_page(page);
880 stop = !dump_emit(cprm, kaddr, PAGE_SIZE);
886 dump_skip(cprm, PAGE_SIZE);
893 int dump_align(struct coredump_params *cprm, int align)
895 unsigned mod = (cprm->pos + cprm->to_skip) & (align - 1);
896 if (align & (align - 1))
899 cprm->to_skip += align - mod;
902 EXPORT_SYMBOL(dump_align);
906 void validate_coredump_safety(void)
908 if (suid_dumpable == SUID_DUMP_ROOT &&
909 core_pattern[0] != '/' && core_pattern[0] != '|') {
911 "Unsafe core_pattern used with fs.suid_dumpable=2.\n"
912 "Pipe handler or fully qualified core dump path required.\n"
913 "Set kernel.core_pattern before fs.suid_dumpable.\n"
918 static int proc_dostring_coredump(struct ctl_table *table, int write,
919 void *buffer, size_t *lenp, loff_t *ppos)
921 int error = proc_dostring(table, write, buffer, lenp, ppos);
924 validate_coredump_safety();
928 static struct ctl_table coredump_sysctls[] = {
930 .procname = "core_uses_pid",
931 .data = &core_uses_pid,
932 .maxlen = sizeof(int),
934 .proc_handler = proc_dointvec,
937 .procname = "core_pattern",
938 .data = core_pattern,
939 .maxlen = CORENAME_MAX_SIZE,
941 .proc_handler = proc_dostring_coredump,
944 .procname = "core_pipe_limit",
945 .data = &core_pipe_limit,
946 .maxlen = sizeof(unsigned int),
948 .proc_handler = proc_dointvec,
953 static int __init init_fs_coredump_sysctls(void)
955 register_sysctl_init("kernel", coredump_sysctls);
958 fs_initcall(init_fs_coredump_sysctls);
959 #endif /* CONFIG_SYSCTL */
962 * The purpose of always_dump_vma() is to make sure that special kernel mappings
963 * that are useful for post-mortem analysis are included in every core dump.
964 * In that way we ensure that the core dump is fully interpretable later
965 * without matching up the same kernel and hardware config to see what PC values
966 * meant. These special mappings include - vDSO, vsyscall, and other
967 * architecture specific mappings
969 static bool always_dump_vma(struct vm_area_struct *vma)
971 /* Any vsyscall mappings? */
972 if (vma == get_gate_vma(vma->vm_mm))
976 * Assume that all vmas with a .name op should always be dumped.
977 * If this changes, a new vm_ops field can easily be added.
979 if (vma->vm_ops && vma->vm_ops->name && vma->vm_ops->name(vma))
983 * arch_vma_name() returns non-NULL for special architecture mappings,
984 * such as vDSO sections.
986 if (arch_vma_name(vma))
992 #define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1
995 * Decide how much of @vma's contents should be included in a core dump.
997 static unsigned long vma_dump_size(struct vm_area_struct *vma,
998 unsigned long mm_flags)
1000 #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1002 /* always dump the vdso and vsyscall sections */
1003 if (always_dump_vma(vma))
1006 if (vma->vm_flags & VM_DONTDUMP)
1009 /* support for DAX */
1010 if (vma_is_dax(vma)) {
1011 if ((vma->vm_flags & VM_SHARED) && FILTER(DAX_SHARED))
1013 if (!(vma->vm_flags & VM_SHARED) && FILTER(DAX_PRIVATE))
1018 /* Hugetlb memory check */
1019 if (is_vm_hugetlb_page(vma)) {
1020 if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED))
1022 if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE))
1027 /* Do not dump I/O mapped devices or special mappings */
1028 if (vma->vm_flags & VM_IO)
1031 /* By default, dump shared memory if mapped from an anonymous file. */
1032 if (vma->vm_flags & VM_SHARED) {
1033 if (file_inode(vma->vm_file)->i_nlink == 0 ?
1034 FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
1039 /* Dump segments that have been written to. */
1040 if ((!IS_ENABLED(CONFIG_MMU) || vma->anon_vma) && FILTER(ANON_PRIVATE))
1042 if (vma->vm_file == NULL)
1045 if (FILTER(MAPPED_PRIVATE))
1049 * If this is the beginning of an executable file mapping,
1050 * dump the first page to aid in determining what was mapped here.
1052 if (FILTER(ELF_HEADERS) &&
1053 vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {
1054 if ((READ_ONCE(file_inode(vma->vm_file)->i_mode) & 0111) != 0)
1058 * ELF libraries aren't always executable.
1059 * We'll want to check whether the mapping starts with the ELF
1060 * magic, but not now - we're holding the mmap lock,
1061 * so copy_from_user() doesn't work here.
1062 * Use a placeholder instead, and fix it up later in
1063 * dump_vma_snapshot().
1065 return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER;
1073 return vma->vm_end - vma->vm_start;
1076 static struct vm_area_struct *first_vma(struct task_struct *tsk,
1077 struct vm_area_struct *gate_vma)
1079 struct vm_area_struct *ret = tsk->mm->mmap;
1087 * Helper function for iterating across a vma list. It ensures that the caller
1088 * will visit `gate_vma' prior to terminating the search.
1090 static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
1091 struct vm_area_struct *gate_vma)
1093 struct vm_area_struct *ret;
1095 ret = this_vma->vm_next;
1098 if (this_vma == gate_vma)
1103 static void free_vma_snapshot(struct coredump_params *cprm)
1105 if (cprm->vma_meta) {
1107 for (i = 0; i < cprm->vma_count; i++) {
1108 struct file *file = cprm->vma_meta[i].file;
1112 kvfree(cprm->vma_meta);
1113 cprm->vma_meta = NULL;
1118 * Under the mmap_lock, take a snapshot of relevant information about the task's
1121 static bool dump_vma_snapshot(struct coredump_params *cprm)
1123 struct vm_area_struct *vma, *gate_vma;
1124 struct mm_struct *mm = current->mm;
1128 * Once the stack expansion code is fixed to not change VMA bounds
1129 * under mmap_lock in read mode, this can be changed to take the
1130 * mmap_lock in read mode.
1132 if (mmap_write_lock_killable(mm))
1135 cprm->vma_data_size = 0;
1136 gate_vma = get_gate_vma(mm);
1137 cprm->vma_count = mm->map_count + (gate_vma ? 1 : 0);
1139 cprm->vma_meta = kvmalloc_array(cprm->vma_count, sizeof(*cprm->vma_meta), GFP_KERNEL);
1140 if (!cprm->vma_meta) {
1141 mmap_write_unlock(mm);
1145 for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
1146 vma = next_vma(vma, gate_vma), i++) {
1147 struct core_vma_metadata *m = cprm->vma_meta + i;
1149 m->start = vma->vm_start;
1150 m->end = vma->vm_end;
1151 m->flags = vma->vm_flags;
1152 m->dump_size = vma_dump_size(vma, cprm->mm_flags);
1153 m->pgoff = vma->vm_pgoff;
1155 m->file = vma->vm_file;
1160 mmap_write_unlock(mm);
1162 for (i = 0; i < cprm->vma_count; i++) {
1163 struct core_vma_metadata *m = cprm->vma_meta + i;
1165 if (m->dump_size == DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER) {
1166 char elfmag[SELFMAG];
1168 if (copy_from_user(elfmag, (void __user *)m->start, SELFMAG) ||
1169 memcmp(elfmag, ELFMAG, SELFMAG) != 0) {
1172 m->dump_size = PAGE_SIZE;
1176 cprm->vma_data_size += m->dump_size;