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>
46 #include <linux/uaccess.h>
47 #include <asm/mmu_context.h>
51 #include <trace/events/task.h>
54 #include <trace/events/sched.h>
56 static int core_uses_pid;
57 static unsigned int core_pipe_limit;
58 static char core_pattern[CORENAME_MAX_SIZE] = "core";
59 static int core_name_size = CORENAME_MAX_SIZE;
66 static int expand_corename(struct core_name *cn, int size)
68 char *corename = krealloc(cn->corename, size, GFP_KERNEL);
73 if (size > core_name_size) /* racy but harmless */
74 core_name_size = size;
76 cn->size = ksize(corename);
77 cn->corename = corename;
81 static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt,
88 free = cn->size - cn->used;
90 va_copy(arg_copy, arg);
91 need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
99 if (!expand_corename(cn, cn->size + need - free + 1))
105 static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...)
111 ret = cn_vprintf(cn, fmt, arg);
117 static __printf(2, 3)
118 int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
125 ret = cn_vprintf(cn, fmt, arg);
130 * Ensure that this coredump name component can't cause the
131 * resulting corefile path to consist of a ".." or ".".
133 if ((cn->used - cur == 1 && cn->corename[cur] == '.') ||
134 (cn->used - cur == 2 && cn->corename[cur] == '.'
135 && cn->corename[cur+1] == '.'))
136 cn->corename[cur] = '!';
139 * Empty names are fishy and could be used to create a "//" in a
140 * corefile name, causing the coredump to happen one directory
141 * level too high. Enforce that all components of the core
142 * pattern are at least one character long.
145 ret = cn_printf(cn, "!");
148 for (; cur < cn->used; ++cur) {
149 if (cn->corename[cur] == '/')
150 cn->corename[cur] = '!';
155 static int cn_print_exe_file(struct core_name *cn, bool name_only)
157 struct file *exe_file;
158 char *pathbuf, *path, *ptr;
161 exe_file = get_mm_exe_file(current->mm);
163 return cn_esc_printf(cn, "%s (path unknown)", current->comm);
165 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
171 path = file_path(exe_file, pathbuf, PATH_MAX);
178 ptr = strrchr(path, '/');
182 ret = cn_esc_printf(cn, "%s", path);
191 /* format_corename will inspect the pattern parameter, and output a
192 * name into corename, which must have space for at least
193 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
195 static int format_corename(struct core_name *cn, struct coredump_params *cprm,
196 size_t **argv, int *argc)
198 const struct cred *cred = current_cred();
199 const char *pat_ptr = core_pattern;
200 int ispipe = (*pat_ptr == '|');
201 bool was_space = false;
202 int pid_in_pattern = 0;
207 if (expand_corename(cn, core_name_size))
209 cn->corename[0] = '\0';
212 int argvs = sizeof(core_pattern) / 2;
213 (*argv) = kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL);
216 (*argv)[(*argc)++] = 0;
222 /* Repeat as long as we have more pattern to process and more output
226 * Split on spaces before doing template expansion so that
227 * %e and %E don't get split if they have spaces in them
230 if (isspace(*pat_ptr)) {
235 } else if (was_space) {
237 err = cn_printf(cn, "%c", '\0');
240 (*argv)[(*argc)++] = cn->used;
243 if (*pat_ptr != '%') {
244 err = cn_printf(cn, "%c", *pat_ptr++);
246 switch (*++pat_ptr) {
247 /* single % at the end, drop that */
250 /* Double percent, output one percent */
252 err = cn_printf(cn, "%c", '%');
257 err = cn_printf(cn, "%d",
258 task_tgid_vnr(current));
262 err = cn_printf(cn, "%d",
263 task_tgid_nr(current));
266 err = cn_printf(cn, "%d",
267 task_pid_vnr(current));
270 err = cn_printf(cn, "%d",
271 task_pid_nr(current));
275 err = cn_printf(cn, "%u",
276 from_kuid(&init_user_ns,
281 err = cn_printf(cn, "%u",
282 from_kgid(&init_user_ns,
286 err = cn_printf(cn, "%d",
287 __get_dumpable(cprm->mm_flags));
289 /* signal that caused the coredump */
291 err = cn_printf(cn, "%d",
292 cprm->siginfo->si_signo);
294 /* UNIX time of coredump */
298 time = ktime_get_real_seconds();
299 err = cn_printf(cn, "%lld", time);
305 err = cn_esc_printf(cn, "%s",
306 utsname()->nodename);
309 /* executable, could be changed by prctl PR_SET_NAME etc */
311 err = cn_esc_printf(cn, "%s", current->comm);
313 /* file name of executable */
315 err = cn_print_exe_file(cn, true);
318 err = cn_print_exe_file(cn, false);
320 /* core limit size */
322 err = cn_printf(cn, "%lu",
323 rlimit(RLIMIT_CORE));
336 /* Backward compatibility with core_uses_pid:
338 * If core_pattern does not include a %p (as is the default)
339 * and core_uses_pid is set, then .%pid will be appended to
340 * the filename. Do not do this for piped commands. */
341 if (!ispipe && !pid_in_pattern && core_uses_pid) {
342 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
349 static int zap_process(struct task_struct *start, int exit_code)
351 struct task_struct *t;
354 /* ignore all signals except SIGKILL, see prepare_signal() */
355 start->signal->flags = SIGNAL_GROUP_EXIT;
356 start->signal->group_exit_code = exit_code;
357 start->signal->group_stop_count = 0;
359 for_each_thread(start, t) {
360 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
361 if (t != current && !(t->flags & PF_POSTCOREDUMP)) {
362 sigaddset(&t->pending.signal, SIGKILL);
363 signal_wake_up(t, 1);
371 static int zap_threads(struct task_struct *tsk,
372 struct core_state *core_state, int exit_code)
374 struct signal_struct *signal = tsk->signal;
377 spin_lock_irq(&tsk->sighand->siglock);
378 if (!(signal->flags & SIGNAL_GROUP_EXIT) && !signal->group_exec_task) {
379 signal->core_state = core_state;
380 nr = zap_process(tsk, exit_code);
381 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
382 tsk->flags |= PF_DUMPCORE;
383 atomic_set(&core_state->nr_threads, nr);
385 spin_unlock_irq(&tsk->sighand->siglock);
389 static int coredump_wait(int exit_code, struct core_state *core_state)
391 struct task_struct *tsk = current;
392 int core_waiters = -EBUSY;
394 init_completion(&core_state->startup);
395 core_state->dumper.task = tsk;
396 core_state->dumper.next = NULL;
398 core_waiters = zap_threads(tsk, core_state, exit_code);
399 if (core_waiters > 0) {
400 struct core_thread *ptr;
402 freezer_do_not_count();
403 wait_for_completion(&core_state->startup);
406 * Wait for all the threads to become inactive, so that
407 * all the thread context (extended register state, like
408 * fpu etc) gets copied to the memory.
410 ptr = core_state->dumper.next;
411 while (ptr != NULL) {
412 wait_task_inactive(ptr->task, 0);
420 static void coredump_finish(bool core_dumped)
422 struct core_thread *curr, *next;
423 struct task_struct *task;
425 spin_lock_irq(¤t->sighand->siglock);
426 if (core_dumped && !__fatal_signal_pending(current))
427 current->signal->group_exit_code |= 0x80;
428 next = current->signal->core_state->dumper.next;
429 current->signal->core_state = NULL;
430 spin_unlock_irq(¤t->sighand->siglock);
432 while ((curr = next) != NULL) {
436 * see coredump_task_exit(), curr->task must not see
437 * ->task == NULL before we read ->next.
441 wake_up_process(task);
445 static bool dump_interrupted(void)
448 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
449 * can do try_to_freeze() and check __fatal_signal_pending(),
450 * but then we need to teach dump_write() to restart and clear
453 return fatal_signal_pending(current) || freezing(current);
456 static void wait_for_dump_helpers(struct file *file)
458 struct pipe_inode_info *pipe = file->private_data;
463 wake_up_interruptible_sync(&pipe->rd_wait);
464 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
468 * We actually want wait_event_freezable() but then we need
469 * to clear TIF_SIGPENDING and improve dump_interrupted().
471 wait_event_interruptible(pipe->rd_wait, pipe->readers == 1);
481 * helper function to customize the process used
482 * to collect the core in userspace. Specifically
483 * it sets up a pipe and installs it as fd 0 (stdin)
484 * for the process. Returns 0 on success, or
485 * PTR_ERR on failure.
486 * Note that it also sets the core limit to 1. This
487 * is a special value that we use to trap recursive
490 static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
492 struct file *files[2];
493 struct coredump_params *cp = (struct coredump_params *)info->data;
494 int err = create_pipe_files(files, 0);
500 err = replace_fd(0, files[0], 0);
502 /* and disallow core files too */
503 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
508 void do_coredump(const kernel_siginfo_t *siginfo)
510 struct core_state core_state;
512 struct mm_struct *mm = current->mm;
513 struct linux_binfmt * binfmt;
514 const struct cred *old_cred;
520 /* require nonrelative corefile path and be extra careful */
521 bool need_suid_safe = false;
522 bool core_dumped = false;
523 static atomic_t core_dump_count = ATOMIC_INIT(0);
524 struct coredump_params cprm = {
526 .regs = signal_pt_regs(),
527 .limit = rlimit(RLIMIT_CORE),
529 * We must use the same mm->flags while dumping core to avoid
530 * inconsistency of bit flags, since this flag is not protected
533 .mm_flags = mm->flags,
536 audit_core_dumps(siginfo->si_signo);
539 if (!binfmt || !binfmt->core_dump)
541 if (!__get_dumpable(cprm.mm_flags))
544 cred = prepare_creds();
548 * We cannot trust fsuid as being the "true" uid of the process
549 * nor do we know its entire history. We only know it was tainted
550 * so we dump it as root in mode 2, and only into a controlled
551 * environment (pipe handler or fully qualified path).
553 if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
554 /* Setuid core dump mode */
555 cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
556 need_suid_safe = true;
559 retval = coredump_wait(siginfo->si_signo, &core_state);
563 old_cred = override_creds(cred);
565 ispipe = format_corename(&cn, &cprm, &argv, &argc);
571 struct subprocess_info *sub_info;
574 printk(KERN_WARNING "format_corename failed\n");
575 printk(KERN_WARNING "Aborting core\n");
579 if (cprm.limit == 1) {
580 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
582 * Normally core limits are irrelevant to pipes, since
583 * we're not writing to the file system, but we use
584 * cprm.limit of 1 here as a special value, this is a
585 * consistent way to catch recursive crashes.
586 * We can still crash if the core_pattern binary sets
587 * RLIM_CORE = !1, but it runs as root, and can do
588 * lots of stupid things.
590 * Note that we use task_tgid_vnr here to grab the pid
591 * of the process group leader. That way we get the
592 * right pid if a thread in a multi-threaded
593 * core_pattern process dies.
596 "Process %d(%s) has RLIMIT_CORE set to 1\n",
597 task_tgid_vnr(current), current->comm);
598 printk(KERN_WARNING "Aborting core\n");
601 cprm.limit = RLIM_INFINITY;
603 dump_count = atomic_inc_return(&core_dump_count);
604 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
605 printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
606 task_tgid_vnr(current), current->comm);
607 printk(KERN_WARNING "Skipping core dump\n");
611 helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv),
614 printk(KERN_WARNING "%s failed to allocate memory\n",
618 for (argi = 0; argi < argc; argi++)
619 helper_argv[argi] = cn.corename + argv[argi];
620 helper_argv[argi] = NULL;
623 sub_info = call_usermodehelper_setup(helper_argv[0],
624 helper_argv, NULL, GFP_KERNEL,
625 umh_pipe_setup, NULL, &cprm);
627 retval = call_usermodehelper_exec(sub_info,
632 printk(KERN_INFO "Core dump to |%s pipe failed\n",
637 struct user_namespace *mnt_userns;
639 int open_flags = O_CREAT | O_RDWR | O_NOFOLLOW |
640 O_LARGEFILE | O_EXCL;
642 if (cprm.limit < binfmt->min_coredump)
645 if (need_suid_safe && cn.corename[0] != '/') {
646 printk(KERN_WARNING "Pid %d(%s) can only dump core "\
647 "to fully qualified path!\n",
648 task_tgid_vnr(current), current->comm);
649 printk(KERN_WARNING "Skipping core dump\n");
654 * Unlink the file if it exists unless this is a SUID
655 * binary - in that case, we're running around with root
656 * privs and don't want to unlink another user's coredump.
658 if (!need_suid_safe) {
660 * If it doesn't exist, that's fine. If there's some
661 * other problem, we'll catch it at the filp_open().
663 do_unlinkat(AT_FDCWD, getname_kernel(cn.corename));
667 * There is a race between unlinking and creating the
668 * file, but if that causes an EEXIST here, that's
669 * fine - another process raced with us while creating
670 * the corefile, and the other process won. To userspace,
671 * what matters is that at least one of the two processes
672 * writes its coredump successfully, not which one.
674 if (need_suid_safe) {
676 * Using user namespaces, normal user tasks can change
677 * their current->fs->root to point to arbitrary
678 * directories. Since the intention of the "only dump
679 * with a fully qualified path" rule is to control where
680 * coredumps may be placed using root privileges,
681 * current->fs->root must not be used. Instead, use the
682 * root directory of init_task.
686 task_lock(&init_task);
687 get_fs_root(init_task.fs, &root);
688 task_unlock(&init_task);
689 cprm.file = file_open_root(&root, cn.corename,
693 cprm.file = filp_open(cn.corename, open_flags, 0600);
695 if (IS_ERR(cprm.file))
698 inode = file_inode(cprm.file);
699 if (inode->i_nlink > 1)
701 if (d_unhashed(cprm.file->f_path.dentry))
704 * AK: actually i see no reason to not allow this for named
705 * pipes etc, but keep the previous behaviour for now.
707 if (!S_ISREG(inode->i_mode))
710 * Don't dump core if the filesystem changed owner or mode
711 * of the file during file creation. This is an issue when
712 * a process dumps core while its cwd is e.g. on a vfat
715 mnt_userns = file_mnt_user_ns(cprm.file);
716 if (!uid_eq(i_uid_into_mnt(mnt_userns, inode),
718 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file owner\n",
722 if ((inode->i_mode & 0677) != 0600) {
723 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file permissions\n",
727 if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
729 if (do_truncate(mnt_userns, cprm.file->f_path.dentry,
734 /* get us an unshared descriptor table; almost always a no-op */
735 /* The cell spufs coredump code reads the file descriptor tables */
736 retval = unshare_files();
739 if (!dump_interrupted()) {
741 * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
742 * have this set to NULL.
745 pr_info("Core dump to |%s disabled\n", cn.corename);
748 file_start_write(cprm.file);
749 core_dumped = binfmt->core_dump(&cprm);
751 * Ensures that file size is big enough to contain the current
752 * file postion. This prevents gdb from complaining about
753 * a truncated file if the last "write" to the file was
758 dump_emit(&cprm, "", 1);
760 file_end_write(cprm.file);
762 if (ispipe && core_pipe_limit)
763 wait_for_dump_helpers(cprm.file);
766 filp_close(cprm.file, NULL);
769 atomic_dec(&core_dump_count);
773 coredump_finish(core_dumped);
774 revert_creds(old_cred);
782 * Core dumping helper functions. These are the only things you should
783 * do on a core-file: use only these functions to write out all the
786 static int __dump_emit(struct coredump_params *cprm, const void *addr, int nr)
788 struct file *file = cprm->file;
789 loff_t pos = file->f_pos;
791 if (cprm->written + nr > cprm->limit)
795 if (dump_interrupted())
797 n = __kernel_write(file, addr, nr, &pos);
807 static int __dump_skip(struct coredump_params *cprm, size_t nr)
809 static char zeroes[PAGE_SIZE];
810 struct file *file = cprm->file;
811 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
812 if (dump_interrupted() ||
813 file->f_op->llseek(file, nr, SEEK_CUR) < 0)
818 while (nr > PAGE_SIZE) {
819 if (!__dump_emit(cprm, zeroes, PAGE_SIZE))
823 return __dump_emit(cprm, zeroes, nr);
827 int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
830 if (!__dump_skip(cprm, cprm->to_skip))
834 return __dump_emit(cprm, addr, nr);
836 EXPORT_SYMBOL(dump_emit);
838 void dump_skip_to(struct coredump_params *cprm, unsigned long pos)
840 cprm->to_skip = pos - cprm->pos;
842 EXPORT_SYMBOL(dump_skip_to);
844 void dump_skip(struct coredump_params *cprm, size_t nr)
848 EXPORT_SYMBOL(dump_skip);
850 #ifdef CONFIG_ELF_CORE
851 int dump_user_range(struct coredump_params *cprm, unsigned long start,
856 for (addr = start; addr < start + len; addr += PAGE_SIZE) {
861 * To avoid having to allocate page tables for virtual address
862 * ranges that have never been used yet, and also to make it
863 * easy to generate sparse core files, use a helper that returns
864 * NULL when encountering an empty page table entry that would
865 * otherwise have been filled with the zero page.
867 page = get_dump_page(addr);
869 void *kaddr = kmap_local_page(page);
871 stop = !dump_emit(cprm, kaddr, PAGE_SIZE);
877 dump_skip(cprm, PAGE_SIZE);
884 int dump_align(struct coredump_params *cprm, int align)
886 unsigned mod = (cprm->pos + cprm->to_skip) & (align - 1);
887 if (align & (align - 1))
890 cprm->to_skip += align - mod;
893 EXPORT_SYMBOL(dump_align);
897 void validate_coredump_safety(void)
899 if (suid_dumpable == SUID_DUMP_ROOT &&
900 core_pattern[0] != '/' && core_pattern[0] != '|') {
902 "Unsafe core_pattern used with fs.suid_dumpable=2.\n"
903 "Pipe handler or fully qualified core dump path required.\n"
904 "Set kernel.core_pattern before fs.suid_dumpable.\n"
909 static int proc_dostring_coredump(struct ctl_table *table, int write,
910 void *buffer, size_t *lenp, loff_t *ppos)
912 int error = proc_dostring(table, write, buffer, lenp, ppos);
915 validate_coredump_safety();
919 static struct ctl_table coredump_sysctls[] = {
921 .procname = "core_uses_pid",
922 .data = &core_uses_pid,
923 .maxlen = sizeof(int),
925 .proc_handler = proc_dointvec,
928 .procname = "core_pattern",
929 .data = core_pattern,
930 .maxlen = CORENAME_MAX_SIZE,
932 .proc_handler = proc_dostring_coredump,
935 .procname = "core_pipe_limit",
936 .data = &core_pipe_limit,
937 .maxlen = sizeof(unsigned int),
939 .proc_handler = proc_dointvec,
944 static int __init init_fs_coredump_sysctls(void)
946 register_sysctl_init("kernel", coredump_sysctls);
949 fs_initcall(init_fs_coredump_sysctls);
950 #endif /* CONFIG_SYSCTL */
953 * The purpose of always_dump_vma() is to make sure that special kernel mappings
954 * that are useful for post-mortem analysis are included in every core dump.
955 * In that way we ensure that the core dump is fully interpretable later
956 * without matching up the same kernel and hardware config to see what PC values
957 * meant. These special mappings include - vDSO, vsyscall, and other
958 * architecture specific mappings
960 static bool always_dump_vma(struct vm_area_struct *vma)
962 /* Any vsyscall mappings? */
963 if (vma == get_gate_vma(vma->vm_mm))
967 * Assume that all vmas with a .name op should always be dumped.
968 * If this changes, a new vm_ops field can easily be added.
970 if (vma->vm_ops && vma->vm_ops->name && vma->vm_ops->name(vma))
974 * arch_vma_name() returns non-NULL for special architecture mappings,
975 * such as vDSO sections.
977 if (arch_vma_name(vma))
984 * Decide how much of @vma's contents should be included in a core dump.
986 static unsigned long vma_dump_size(struct vm_area_struct *vma,
987 unsigned long mm_flags)
989 #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
991 /* always dump the vdso and vsyscall sections */
992 if (always_dump_vma(vma))
995 if (vma->vm_flags & VM_DONTDUMP)
998 /* support for DAX */
999 if (vma_is_dax(vma)) {
1000 if ((vma->vm_flags & VM_SHARED) && FILTER(DAX_SHARED))
1002 if (!(vma->vm_flags & VM_SHARED) && FILTER(DAX_PRIVATE))
1007 /* Hugetlb memory check */
1008 if (is_vm_hugetlb_page(vma)) {
1009 if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED))
1011 if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE))
1016 /* Do not dump I/O mapped devices or special mappings */
1017 if (vma->vm_flags & VM_IO)
1020 /* By default, dump shared memory if mapped from an anonymous file. */
1021 if (vma->vm_flags & VM_SHARED) {
1022 if (file_inode(vma->vm_file)->i_nlink == 0 ?
1023 FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
1028 /* Dump segments that have been written to. */
1029 if ((!IS_ENABLED(CONFIG_MMU) || vma->anon_vma) && FILTER(ANON_PRIVATE))
1031 if (vma->vm_file == NULL)
1034 if (FILTER(MAPPED_PRIVATE))
1038 * If this is the beginning of an executable file mapping,
1039 * dump the first page to aid in determining what was mapped here.
1041 if (FILTER(ELF_HEADERS) &&
1042 vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ) &&
1043 (READ_ONCE(file_inode(vma->vm_file)->i_mode) & 0111) != 0)
1051 return vma->vm_end - vma->vm_start;
1054 static struct vm_area_struct *first_vma(struct task_struct *tsk,
1055 struct vm_area_struct *gate_vma)
1057 struct vm_area_struct *ret = tsk->mm->mmap;
1065 * Helper function for iterating across a vma list. It ensures that the caller
1066 * will visit `gate_vma' prior to terminating the search.
1068 static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
1069 struct vm_area_struct *gate_vma)
1071 struct vm_area_struct *ret;
1073 ret = this_vma->vm_next;
1076 if (this_vma == gate_vma)
1082 * Under the mmap_lock, take a snapshot of relevant information about the task's
1085 int dump_vma_snapshot(struct coredump_params *cprm, int *vma_count,
1086 struct core_vma_metadata **vma_meta,
1087 size_t *vma_data_size_ptr)
1089 struct vm_area_struct *vma, *gate_vma;
1090 struct mm_struct *mm = current->mm;
1092 size_t vma_data_size = 0;
1095 * Once the stack expansion code is fixed to not change VMA bounds
1096 * under mmap_lock in read mode, this can be changed to take the
1097 * mmap_lock in read mode.
1099 if (mmap_write_lock_killable(mm))
1102 gate_vma = get_gate_vma(mm);
1103 *vma_count = mm->map_count + (gate_vma ? 1 : 0);
1105 *vma_meta = kvmalloc_array(*vma_count, sizeof(**vma_meta), GFP_KERNEL);
1107 mmap_write_unlock(mm);
1111 for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
1112 vma = next_vma(vma, gate_vma), i++) {
1113 struct core_vma_metadata *m = (*vma_meta) + i;
1115 m->start = vma->vm_start;
1116 m->end = vma->vm_end;
1117 m->flags = vma->vm_flags;
1118 m->dump_size = vma_dump_size(vma, cprm->mm_flags);
1120 vma_data_size += m->dump_size;
1123 mmap_write_unlock(mm);
1125 if (WARN_ON(i != *vma_count)) {
1130 *vma_data_size_ptr = vma_data_size;