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/kmod.h>
35 #include <linux/fsnotify.h>
36 #include <linux/fs_struct.h>
37 #include <linux/pipe_fs_i.h>
38 #include <linux/oom.h>
39 #include <linux/compat.h>
41 #include <linux/path.h>
42 #include <linux/timekeeping.h>
43 #include <linux/sysctl.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>
56 static bool dump_vma_snapshot(struct coredump_params
*cprm
);
57 static void free_vma_snapshot(struct coredump_params
*cprm
);
59 static int core_uses_pid
;
60 static unsigned int core_pipe_limit
;
61 static char core_pattern
[CORENAME_MAX_SIZE
] = "core";
62 static int core_name_size
= CORENAME_MAX_SIZE
;
69 static int expand_corename(struct core_name
*cn
, int size
)
73 size
= kmalloc_size_roundup(size
);
74 corename
= krealloc(cn
->corename
, size
, GFP_KERNEL
);
79 if (size
> core_name_size
) /* racy but harmless */
80 core_name_size
= size
;
83 cn
->corename
= corename
;
87 static __printf(2, 0) int cn_vprintf(struct core_name
*cn
, const char *fmt
,
94 free
= cn
->size
- cn
->used
;
96 va_copy(arg_copy
, arg
);
97 need
= vsnprintf(cn
->corename
+ cn
->used
, free
, fmt
, arg_copy
);
105 if (!expand_corename(cn
, cn
->size
+ need
- free
+ 1))
111 static __printf(2, 3) int cn_printf(struct core_name
*cn
, const char *fmt
, ...)
117 ret
= cn_vprintf(cn
, fmt
, arg
);
123 static __printf(2, 3)
124 int cn_esc_printf(struct core_name
*cn
, const char *fmt
, ...)
131 ret
= cn_vprintf(cn
, fmt
, arg
);
136 * Ensure that this coredump name component can't cause the
137 * resulting corefile path to consist of a ".." or ".".
139 if ((cn
->used
- cur
== 1 && cn
->corename
[cur
] == '.') ||
140 (cn
->used
- cur
== 2 && cn
->corename
[cur
] == '.'
141 && cn
->corename
[cur
+1] == '.'))
142 cn
->corename
[cur
] = '!';
145 * Empty names are fishy and could be used to create a "//" in a
146 * corefile name, causing the coredump to happen one directory
147 * level too high. Enforce that all components of the core
148 * pattern are at least one character long.
151 ret
= cn_printf(cn
, "!");
154 for (; cur
< cn
->used
; ++cur
) {
155 if (cn
->corename
[cur
] == '/')
156 cn
->corename
[cur
] = '!';
161 static int cn_print_exe_file(struct core_name
*cn
, bool name_only
)
163 struct file
*exe_file
;
164 char *pathbuf
, *path
, *ptr
;
167 exe_file
= get_mm_exe_file(current
->mm
);
169 return cn_esc_printf(cn
, "%s (path unknown)", current
->comm
);
171 pathbuf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
177 path
= file_path(exe_file
, pathbuf
, PATH_MAX
);
184 ptr
= strrchr(path
, '/');
188 ret
= cn_esc_printf(cn
, "%s", path
);
197 /* format_corename will inspect the pattern parameter, and output a
198 * name into corename, which must have space for at least
199 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
201 static int format_corename(struct core_name
*cn
, struct coredump_params
*cprm
,
202 size_t **argv
, int *argc
)
204 const struct cred
*cred
= current_cred();
205 const char *pat_ptr
= core_pattern
;
206 int ispipe
= (*pat_ptr
== '|');
207 bool was_space
= false;
208 int pid_in_pattern
= 0;
213 if (expand_corename(cn
, core_name_size
))
215 cn
->corename
[0] = '\0';
218 int argvs
= sizeof(core_pattern
) / 2;
219 (*argv
) = kmalloc_array(argvs
, sizeof(**argv
), GFP_KERNEL
);
222 (*argv
)[(*argc
)++] = 0;
228 /* Repeat as long as we have more pattern to process and more output
232 * Split on spaces before doing template expansion so that
233 * %e and %E don't get split if they have spaces in them
236 if (isspace(*pat_ptr
)) {
241 } else if (was_space
) {
243 err
= cn_printf(cn
, "%c", '\0');
246 (*argv
)[(*argc
)++] = cn
->used
;
249 if (*pat_ptr
!= '%') {
250 err
= cn_printf(cn
, "%c", *pat_ptr
++);
252 switch (*++pat_ptr
) {
253 /* single % at the end, drop that */
256 /* Double percent, output one percent */
258 err
= cn_printf(cn
, "%c", '%');
263 err
= cn_printf(cn
, "%d",
264 task_tgid_vnr(current
));
268 err
= cn_printf(cn
, "%d",
269 task_tgid_nr(current
));
272 err
= cn_printf(cn
, "%d",
273 task_pid_vnr(current
));
276 err
= cn_printf(cn
, "%d",
277 task_pid_nr(current
));
281 err
= cn_printf(cn
, "%u",
282 from_kuid(&init_user_ns
,
287 err
= cn_printf(cn
, "%u",
288 from_kgid(&init_user_ns
,
292 err
= cn_printf(cn
, "%d",
293 __get_dumpable(cprm
->mm_flags
));
295 /* signal that caused the coredump */
297 err
= cn_printf(cn
, "%d",
298 cprm
->siginfo
->si_signo
);
300 /* UNIX time of coredump */
304 time
= ktime_get_real_seconds();
305 err
= cn_printf(cn
, "%lld", time
);
311 err
= cn_esc_printf(cn
, "%s",
312 utsname()->nodename
);
315 /* executable, could be changed by prctl PR_SET_NAME etc */
317 err
= cn_esc_printf(cn
, "%s", current
->comm
);
319 /* file name of executable */
321 err
= cn_print_exe_file(cn
, true);
324 err
= cn_print_exe_file(cn
, false);
326 /* core limit size */
328 err
= cn_printf(cn
, "%lu",
329 rlimit(RLIMIT_CORE
));
331 /* CPU the task ran on */
333 err
= cn_printf(cn
, "%d", cprm
->cpu
);
346 /* Backward compatibility with core_uses_pid:
348 * If core_pattern does not include a %p (as is the default)
349 * and core_uses_pid is set, then .%pid will be appended to
350 * the filename. Do not do this for piped commands. */
351 if (!ispipe
&& !pid_in_pattern
&& core_uses_pid
) {
352 err
= cn_printf(cn
, ".%d", task_tgid_vnr(current
));
359 static int zap_process(struct task_struct
*start
, int exit_code
)
361 struct task_struct
*t
;
364 /* Allow SIGKILL, see prepare_signal() */
365 start
->signal
->flags
= SIGNAL_GROUP_EXIT
;
366 start
->signal
->group_exit_code
= exit_code
;
367 start
->signal
->group_stop_count
= 0;
369 for_each_thread(start
, t
) {
370 task_clear_jobctl_pending(t
, JOBCTL_PENDING_MASK
);
371 if (t
!= current
&& !(t
->flags
& PF_POSTCOREDUMP
)) {
372 sigaddset(&t
->pending
.signal
, SIGKILL
);
373 signal_wake_up(t
, 1);
374 /* The vhost_worker does not particpate in coredumps */
375 if ((t
->flags
& (PF_USER_WORKER
| PF_IO_WORKER
)) != PF_USER_WORKER
)
383 static int zap_threads(struct task_struct
*tsk
,
384 struct core_state
*core_state
, int exit_code
)
386 struct signal_struct
*signal
= tsk
->signal
;
389 spin_lock_irq(&tsk
->sighand
->siglock
);
390 if (!(signal
->flags
& SIGNAL_GROUP_EXIT
) && !signal
->group_exec_task
) {
391 signal
->core_state
= core_state
;
392 nr
= zap_process(tsk
, exit_code
);
393 clear_tsk_thread_flag(tsk
, TIF_SIGPENDING
);
394 tsk
->flags
|= PF_DUMPCORE
;
395 atomic_set(&core_state
->nr_threads
, nr
);
397 spin_unlock_irq(&tsk
->sighand
->siglock
);
401 static int coredump_wait(int exit_code
, struct core_state
*core_state
)
403 struct task_struct
*tsk
= current
;
404 int core_waiters
= -EBUSY
;
406 init_completion(&core_state
->startup
);
407 core_state
->dumper
.task
= tsk
;
408 core_state
->dumper
.next
= NULL
;
410 core_waiters
= zap_threads(tsk
, core_state
, exit_code
);
411 if (core_waiters
> 0) {
412 struct core_thread
*ptr
;
414 wait_for_completion_state(&core_state
->startup
,
415 TASK_UNINTERRUPTIBLE
|TASK_FREEZABLE
);
417 * Wait for all the threads to become inactive, so that
418 * all the thread context (extended register state, like
419 * fpu etc) gets copied to the memory.
421 ptr
= core_state
->dumper
.next
;
422 while (ptr
!= NULL
) {
423 wait_task_inactive(ptr
->task
, TASK_ANY
);
431 static void coredump_finish(bool core_dumped
)
433 struct core_thread
*curr
, *next
;
434 struct task_struct
*task
;
436 spin_lock_irq(¤t
->sighand
->siglock
);
437 if (core_dumped
&& !__fatal_signal_pending(current
))
438 current
->signal
->group_exit_code
|= 0x80;
439 next
= current
->signal
->core_state
->dumper
.next
;
440 current
->signal
->core_state
= NULL
;
441 spin_unlock_irq(¤t
->sighand
->siglock
);
443 while ((curr
= next
) != NULL
) {
447 * see coredump_task_exit(), curr->task must not see
448 * ->task == NULL before we read ->next.
452 wake_up_process(task
);
456 static bool dump_interrupted(void)
459 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
460 * can do try_to_freeze() and check __fatal_signal_pending(),
461 * but then we need to teach dump_write() to restart and clear
464 return fatal_signal_pending(current
) || freezing(current
);
467 static void wait_for_dump_helpers(struct file
*file
)
469 struct pipe_inode_info
*pipe
= file
->private_data
;
474 wake_up_interruptible_sync(&pipe
->rd_wait
);
475 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
479 * We actually want wait_event_freezable() but then we need
480 * to clear TIF_SIGPENDING and improve dump_interrupted().
482 wait_event_interruptible(pipe
->rd_wait
, pipe
->readers
== 1);
492 * helper function to customize the process used
493 * to collect the core in userspace. Specifically
494 * it sets up a pipe and installs it as fd 0 (stdin)
495 * for the process. Returns 0 on success, or
496 * PTR_ERR on failure.
497 * Note that it also sets the core limit to 1. This
498 * is a special value that we use to trap recursive
501 static int umh_pipe_setup(struct subprocess_info
*info
, struct cred
*new)
503 struct file
*files
[2];
504 struct coredump_params
*cp
= (struct coredump_params
*)info
->data
;
505 int err
= create_pipe_files(files
, 0);
511 err
= replace_fd(0, files
[0], 0);
513 /* and disallow core files too */
514 current
->signal
->rlim
[RLIMIT_CORE
] = (struct rlimit
){1, 1};
519 void do_coredump(const kernel_siginfo_t
*siginfo
)
521 struct core_state core_state
;
523 struct mm_struct
*mm
= current
->mm
;
524 struct linux_binfmt
* binfmt
;
525 const struct cred
*old_cred
;
531 /* require nonrelative corefile path and be extra careful */
532 bool need_suid_safe
= false;
533 bool core_dumped
= false;
534 static atomic_t core_dump_count
= ATOMIC_INIT(0);
535 struct coredump_params cprm
= {
537 .limit
= rlimit(RLIMIT_CORE
),
539 * We must use the same mm->flags while dumping core to avoid
540 * inconsistency of bit flags, since this flag is not protected
543 .mm_flags
= mm
->flags
,
545 .cpu
= raw_smp_processor_id(),
548 audit_core_dumps(siginfo
->si_signo
);
551 if (!binfmt
|| !binfmt
->core_dump
)
553 if (!__get_dumpable(cprm
.mm_flags
))
556 cred
= prepare_creds();
560 * We cannot trust fsuid as being the "true" uid of the process
561 * nor do we know its entire history. We only know it was tainted
562 * so we dump it as root in mode 2, and only into a controlled
563 * environment (pipe handler or fully qualified path).
565 if (__get_dumpable(cprm
.mm_flags
) == SUID_DUMP_ROOT
) {
566 /* Setuid core dump mode */
567 cred
->fsuid
= GLOBAL_ROOT_UID
; /* Dump root private */
568 need_suid_safe
= true;
571 retval
= coredump_wait(siginfo
->si_signo
, &core_state
);
575 old_cred
= override_creds(cred
);
577 ispipe
= format_corename(&cn
, &cprm
, &argv
, &argc
);
583 struct subprocess_info
*sub_info
;
586 printk(KERN_WARNING
"format_corename failed\n");
587 printk(KERN_WARNING
"Aborting core\n");
591 if (cprm
.limit
== 1) {
592 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
594 * Normally core limits are irrelevant to pipes, since
595 * we're not writing to the file system, but we use
596 * cprm.limit of 1 here as a special value, this is a
597 * consistent way to catch recursive crashes.
598 * We can still crash if the core_pattern binary sets
599 * RLIM_CORE = !1, but it runs as root, and can do
600 * lots of stupid things.
602 * Note that we use task_tgid_vnr here to grab the pid
603 * of the process group leader. That way we get the
604 * right pid if a thread in a multi-threaded
605 * core_pattern process dies.
608 "Process %d(%s) has RLIMIT_CORE set to 1\n",
609 task_tgid_vnr(current
), current
->comm
);
610 printk(KERN_WARNING
"Aborting core\n");
613 cprm
.limit
= RLIM_INFINITY
;
615 dump_count
= atomic_inc_return(&core_dump_count
);
616 if (core_pipe_limit
&& (core_pipe_limit
< dump_count
)) {
617 printk(KERN_WARNING
"Pid %d(%s) over core_pipe_limit\n",
618 task_tgid_vnr(current
), current
->comm
);
619 printk(KERN_WARNING
"Skipping core dump\n");
623 helper_argv
= kmalloc_array(argc
+ 1, sizeof(*helper_argv
),
626 printk(KERN_WARNING
"%s failed to allocate memory\n",
630 for (argi
= 0; argi
< argc
; argi
++)
631 helper_argv
[argi
] = cn
.corename
+ argv
[argi
];
632 helper_argv
[argi
] = NULL
;
635 sub_info
= call_usermodehelper_setup(helper_argv
[0],
636 helper_argv
, NULL
, GFP_KERNEL
,
637 umh_pipe_setup
, NULL
, &cprm
);
639 retval
= call_usermodehelper_exec(sub_info
,
644 printk(KERN_INFO
"Core dump to |%s pipe failed\n",
649 struct mnt_idmap
*idmap
;
651 int open_flags
= O_CREAT
| O_WRONLY
| O_NOFOLLOW
|
652 O_LARGEFILE
| O_EXCL
;
654 if (cprm
.limit
< binfmt
->min_coredump
)
657 if (need_suid_safe
&& cn
.corename
[0] != '/') {
658 printk(KERN_WARNING
"Pid %d(%s) can only dump core "\
659 "to fully qualified path!\n",
660 task_tgid_vnr(current
), current
->comm
);
661 printk(KERN_WARNING
"Skipping core dump\n");
666 * Unlink the file if it exists unless this is a SUID
667 * binary - in that case, we're running around with root
668 * privs and don't want to unlink another user's coredump.
670 if (!need_suid_safe
) {
672 * If it doesn't exist, that's fine. If there's some
673 * other problem, we'll catch it at the filp_open().
675 do_unlinkat(AT_FDCWD
, getname_kernel(cn
.corename
));
679 * There is a race between unlinking and creating the
680 * file, but if that causes an EEXIST here, that's
681 * fine - another process raced with us while creating
682 * the corefile, and the other process won. To userspace,
683 * what matters is that at least one of the two processes
684 * writes its coredump successfully, not which one.
686 if (need_suid_safe
) {
688 * Using user namespaces, normal user tasks can change
689 * their current->fs->root to point to arbitrary
690 * directories. Since the intention of the "only dump
691 * with a fully qualified path" rule is to control where
692 * coredumps may be placed using root privileges,
693 * current->fs->root must not be used. Instead, use the
694 * root directory of init_task.
698 task_lock(&init_task
);
699 get_fs_root(init_task
.fs
, &root
);
700 task_unlock(&init_task
);
701 cprm
.file
= file_open_root(&root
, cn
.corename
,
705 cprm
.file
= filp_open(cn
.corename
, open_flags
, 0600);
707 if (IS_ERR(cprm
.file
))
710 inode
= file_inode(cprm
.file
);
711 if (inode
->i_nlink
> 1)
713 if (d_unhashed(cprm
.file
->f_path
.dentry
))
716 * AK: actually i see no reason to not allow this for named
717 * pipes etc, but keep the previous behaviour for now.
719 if (!S_ISREG(inode
->i_mode
))
722 * Don't dump core if the filesystem changed owner or mode
723 * of the file during file creation. This is an issue when
724 * a process dumps core while its cwd is e.g. on a vfat
727 idmap
= file_mnt_idmap(cprm
.file
);
728 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap
, inode
),
730 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file owner\n",
734 if ((inode
->i_mode
& 0677) != 0600) {
735 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file permissions\n",
739 if (!(cprm
.file
->f_mode
& FMODE_CAN_WRITE
))
741 if (do_truncate(idmap
, cprm
.file
->f_path
.dentry
,
746 /* get us an unshared descriptor table; almost always a no-op */
747 /* The cell spufs coredump code reads the file descriptor tables */
748 retval
= unshare_files();
751 if (!dump_interrupted()) {
753 * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
754 * have this set to NULL.
757 pr_info("Core dump to |%s disabled\n", cn
.corename
);
760 if (!dump_vma_snapshot(&cprm
))
763 file_start_write(cprm
.file
);
764 core_dumped
= binfmt
->core_dump(&cprm
);
766 * Ensures that file size is big enough to contain the current
767 * file postion. This prevents gdb from complaining about
768 * a truncated file if the last "write" to the file was
773 dump_emit(&cprm
, "", 1);
775 file_end_write(cprm
.file
);
776 free_vma_snapshot(&cprm
);
778 if (ispipe
&& core_pipe_limit
)
779 wait_for_dump_helpers(cprm
.file
);
782 filp_close(cprm
.file
, NULL
);
785 atomic_dec(&core_dump_count
);
789 coredump_finish(core_dumped
);
790 revert_creds(old_cred
);
798 * Core dumping helper functions. These are the only things you should
799 * do on a core-file: use only these functions to write out all the
802 static int __dump_emit(struct coredump_params
*cprm
, const void *addr
, int nr
)
804 struct file
*file
= cprm
->file
;
805 loff_t pos
= file
->f_pos
;
807 if (cprm
->written
+ nr
> cprm
->limit
)
811 if (dump_interrupted())
813 n
= __kernel_write(file
, addr
, nr
, &pos
);
823 static int __dump_skip(struct coredump_params
*cprm
, size_t nr
)
825 static char zeroes
[PAGE_SIZE
];
826 struct file
*file
= cprm
->file
;
827 if (file
->f_mode
& FMODE_LSEEK
) {
828 if (dump_interrupted() ||
829 vfs_llseek(file
, nr
, SEEK_CUR
) < 0)
834 while (nr
> PAGE_SIZE
) {
835 if (!__dump_emit(cprm
, zeroes
, PAGE_SIZE
))
839 return __dump_emit(cprm
, zeroes
, nr
);
843 int dump_emit(struct coredump_params
*cprm
, const void *addr
, int nr
)
846 if (!__dump_skip(cprm
, cprm
->to_skip
))
850 return __dump_emit(cprm
, addr
, nr
);
852 EXPORT_SYMBOL(dump_emit
);
854 void dump_skip_to(struct coredump_params
*cprm
, unsigned long pos
)
856 cprm
->to_skip
= pos
- cprm
->pos
;
858 EXPORT_SYMBOL(dump_skip_to
);
860 void dump_skip(struct coredump_params
*cprm
, size_t nr
)
864 EXPORT_SYMBOL(dump_skip
);
866 #ifdef CONFIG_ELF_CORE
867 static int dump_emit_page(struct coredump_params
*cprm
, struct page
*page
)
870 struct iov_iter iter
;
871 struct file
*file
= cprm
->file
;
876 if (!__dump_skip(cprm
, cprm
->to_skip
))
880 if (cprm
->written
+ PAGE_SIZE
> cprm
->limit
)
882 if (dump_interrupted())
885 bvec_set_page(&bvec
, page
, PAGE_SIZE
, 0);
886 iov_iter_bvec(&iter
, ITER_SOURCE
, &bvec
, 1, PAGE_SIZE
);
887 iov_iter_set_copy_mc(&iter
);
888 n
= __kernel_write_iter(cprm
->file
, &iter
, &pos
);
892 cprm
->written
+= PAGE_SIZE
;
893 cprm
->pos
+= PAGE_SIZE
;
898 int dump_user_range(struct coredump_params
*cprm
, unsigned long start
,
903 for (addr
= start
; addr
< start
+ len
; addr
+= PAGE_SIZE
) {
907 * To avoid having to allocate page tables for virtual address
908 * ranges that have never been used yet, and also to make it
909 * easy to generate sparse core files, use a helper that returns
910 * NULL when encountering an empty page table entry that would
911 * otherwise have been filled with the zero page.
913 page
= get_dump_page(addr
);
915 int stop
= !dump_emit_page(cprm
, page
);
920 dump_skip(cprm
, PAGE_SIZE
);
927 int dump_align(struct coredump_params
*cprm
, int align
)
929 unsigned mod
= (cprm
->pos
+ cprm
->to_skip
) & (align
- 1);
930 if (align
& (align
- 1))
933 cprm
->to_skip
+= align
- mod
;
936 EXPORT_SYMBOL(dump_align
);
940 void validate_coredump_safety(void)
942 if (suid_dumpable
== SUID_DUMP_ROOT
&&
943 core_pattern
[0] != '/' && core_pattern
[0] != '|') {
945 "Unsafe core_pattern used with fs.suid_dumpable=2.\n"
946 "Pipe handler or fully qualified core dump path required.\n"
947 "Set kernel.core_pattern before fs.suid_dumpable.\n"
952 static int proc_dostring_coredump(struct ctl_table
*table
, int write
,
953 void *buffer
, size_t *lenp
, loff_t
*ppos
)
955 int error
= proc_dostring(table
, write
, buffer
, lenp
, ppos
);
958 validate_coredump_safety();
962 static struct ctl_table coredump_sysctls
[] = {
964 .procname
= "core_uses_pid",
965 .data
= &core_uses_pid
,
966 .maxlen
= sizeof(int),
968 .proc_handler
= proc_dointvec
,
971 .procname
= "core_pattern",
972 .data
= core_pattern
,
973 .maxlen
= CORENAME_MAX_SIZE
,
975 .proc_handler
= proc_dostring_coredump
,
978 .procname
= "core_pipe_limit",
979 .data
= &core_pipe_limit
,
980 .maxlen
= sizeof(unsigned int),
982 .proc_handler
= proc_dointvec
,
986 static int __init
init_fs_coredump_sysctls(void)
988 register_sysctl_init("kernel", coredump_sysctls
);
991 fs_initcall(init_fs_coredump_sysctls
);
992 #endif /* CONFIG_SYSCTL */
995 * The purpose of always_dump_vma() is to make sure that special kernel mappings
996 * that are useful for post-mortem analysis are included in every core dump.
997 * In that way we ensure that the core dump is fully interpretable later
998 * without matching up the same kernel and hardware config to see what PC values
999 * meant. These special mappings include - vDSO, vsyscall, and other
1000 * architecture specific mappings
1002 static bool always_dump_vma(struct vm_area_struct
*vma
)
1004 /* Any vsyscall mappings? */
1005 if (vma
== get_gate_vma(vma
->vm_mm
))
1009 * Assume that all vmas with a .name op should always be dumped.
1010 * If this changes, a new vm_ops field can easily be added.
1012 if (vma
->vm_ops
&& vma
->vm_ops
->name
&& vma
->vm_ops
->name(vma
))
1016 * arch_vma_name() returns non-NULL for special architecture mappings,
1017 * such as vDSO sections.
1019 if (arch_vma_name(vma
))
1025 #define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1
1028 * Decide how much of @vma's contents should be included in a core dump.
1030 static unsigned long vma_dump_size(struct vm_area_struct
*vma
,
1031 unsigned long mm_flags
)
1033 #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1035 /* always dump the vdso and vsyscall sections */
1036 if (always_dump_vma(vma
))
1039 if (vma
->vm_flags
& VM_DONTDUMP
)
1042 /* support for DAX */
1043 if (vma_is_dax(vma
)) {
1044 if ((vma
->vm_flags
& VM_SHARED
) && FILTER(DAX_SHARED
))
1046 if (!(vma
->vm_flags
& VM_SHARED
) && FILTER(DAX_PRIVATE
))
1051 /* Hugetlb memory check */
1052 if (is_vm_hugetlb_page(vma
)) {
1053 if ((vma
->vm_flags
& VM_SHARED
) && FILTER(HUGETLB_SHARED
))
1055 if (!(vma
->vm_flags
& VM_SHARED
) && FILTER(HUGETLB_PRIVATE
))
1060 /* Do not dump I/O mapped devices or special mappings */
1061 if (vma
->vm_flags
& VM_IO
)
1064 /* By default, dump shared memory if mapped from an anonymous file. */
1065 if (vma
->vm_flags
& VM_SHARED
) {
1066 if (file_inode(vma
->vm_file
)->i_nlink
== 0 ?
1067 FILTER(ANON_SHARED
) : FILTER(MAPPED_SHARED
))
1072 /* Dump segments that have been written to. */
1073 if ((!IS_ENABLED(CONFIG_MMU
) || vma
->anon_vma
) && FILTER(ANON_PRIVATE
))
1075 if (vma
->vm_file
== NULL
)
1078 if (FILTER(MAPPED_PRIVATE
))
1082 * If this is the beginning of an executable file mapping,
1083 * dump the first page to aid in determining what was mapped here.
1085 if (FILTER(ELF_HEADERS
) &&
1086 vma
->vm_pgoff
== 0 && (vma
->vm_flags
& VM_READ
)) {
1087 if ((READ_ONCE(file_inode(vma
->vm_file
)->i_mode
) & 0111) != 0)
1091 * ELF libraries aren't always executable.
1092 * We'll want to check whether the mapping starts with the ELF
1093 * magic, but not now - we're holding the mmap lock,
1094 * so copy_from_user() doesn't work here.
1095 * Use a placeholder instead, and fix it up later in
1096 * dump_vma_snapshot().
1098 return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER
;
1106 return vma
->vm_end
- vma
->vm_start
;
1110 * Helper function for iterating across a vma list. It ensures that the caller
1111 * will visit `gate_vma' prior to terminating the search.
1113 static struct vm_area_struct
*coredump_next_vma(struct vma_iterator
*vmi
,
1114 struct vm_area_struct
*vma
,
1115 struct vm_area_struct
*gate_vma
)
1117 if (gate_vma
&& (vma
== gate_vma
))
1120 vma
= vma_next(vmi
);
1126 static void free_vma_snapshot(struct coredump_params
*cprm
)
1128 if (cprm
->vma_meta
) {
1130 for (i
= 0; i
< cprm
->vma_count
; i
++) {
1131 struct file
*file
= cprm
->vma_meta
[i
].file
;
1135 kvfree(cprm
->vma_meta
);
1136 cprm
->vma_meta
= NULL
;
1141 * Under the mmap_lock, take a snapshot of relevant information about the task's
1144 static bool dump_vma_snapshot(struct coredump_params
*cprm
)
1146 struct vm_area_struct
*gate_vma
, *vma
= NULL
;
1147 struct mm_struct
*mm
= current
->mm
;
1148 VMA_ITERATOR(vmi
, mm
, 0);
1152 * Once the stack expansion code is fixed to not change VMA bounds
1153 * under mmap_lock in read mode, this can be changed to take the
1154 * mmap_lock in read mode.
1156 if (mmap_write_lock_killable(mm
))
1159 cprm
->vma_data_size
= 0;
1160 gate_vma
= get_gate_vma(mm
);
1161 cprm
->vma_count
= mm
->map_count
+ (gate_vma
? 1 : 0);
1163 cprm
->vma_meta
= kvmalloc_array(cprm
->vma_count
, sizeof(*cprm
->vma_meta
), GFP_KERNEL
);
1164 if (!cprm
->vma_meta
) {
1165 mmap_write_unlock(mm
);
1169 while ((vma
= coredump_next_vma(&vmi
, vma
, gate_vma
)) != NULL
) {
1170 struct core_vma_metadata
*m
= cprm
->vma_meta
+ i
;
1172 m
->start
= vma
->vm_start
;
1173 m
->end
= vma
->vm_end
;
1174 m
->flags
= vma
->vm_flags
;
1175 m
->dump_size
= vma_dump_size(vma
, cprm
->mm_flags
);
1176 m
->pgoff
= vma
->vm_pgoff
;
1177 m
->file
= vma
->vm_file
;
1183 mmap_write_unlock(mm
);
1185 for (i
= 0; i
< cprm
->vma_count
; i
++) {
1186 struct core_vma_metadata
*m
= cprm
->vma_meta
+ i
;
1188 if (m
->dump_size
== DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER
) {
1189 char elfmag
[SELFMAG
];
1191 if (copy_from_user(elfmag
, (void __user
*)m
->start
, SELFMAG
) ||
1192 memcmp(elfmag
, ELFMAG
, SELFMAG
) != 0) {
1195 m
->dump_size
= PAGE_SIZE
;
1199 cprm
->vma_data_size
+= m
->dump_size
;