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/sort.h>
22 #include <linux/sched/coredump.h>
23 #include <linux/sched/signal.h>
24 #include <linux/sched/task_stack.h>
25 #include <linux/utsname.h>
26 #include <linux/pid_namespace.h>
27 #include <linux/module.h>
28 #include <linux/namei.h>
29 #include <linux/mount.h>
30 #include <linux/security.h>
31 #include <linux/syscalls.h>
32 #include <linux/tsacct_kern.h>
33 #include <linux/cn_proc.h>
34 #include <linux/audit.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 #define CORE_FILE_NOTE_SIZE_DEFAULT (4*1024*1024)
61 /* Define a reasonable max cap */
62 #define CORE_FILE_NOTE_SIZE_MAX (16*1024*1024)
64 static int core_uses_pid
;
65 static unsigned int core_pipe_limit
;
66 static char core_pattern
[CORENAME_MAX_SIZE
] = "core";
67 static int core_name_size
= CORENAME_MAX_SIZE
;
68 unsigned int core_file_note_size_limit
= CORE_FILE_NOTE_SIZE_DEFAULT
;
75 static int expand_corename(struct core_name
*cn
, int size
)
79 size
= kmalloc_size_roundup(size
);
80 corename
= krealloc(cn
->corename
, size
, GFP_KERNEL
);
85 if (size
> core_name_size
) /* racy but harmless */
86 core_name_size
= size
;
89 cn
->corename
= corename
;
93 static __printf(2, 0) int cn_vprintf(struct core_name
*cn
, const char *fmt
,
100 free
= cn
->size
- cn
->used
;
102 va_copy(arg_copy
, arg
);
103 need
= vsnprintf(cn
->corename
+ cn
->used
, free
, fmt
, arg_copy
);
111 if (!expand_corename(cn
, cn
->size
+ need
- free
+ 1))
117 static __printf(2, 3) int cn_printf(struct core_name
*cn
, const char *fmt
, ...)
123 ret
= cn_vprintf(cn
, fmt
, arg
);
129 static __printf(2, 3)
130 int cn_esc_printf(struct core_name
*cn
, const char *fmt
, ...)
137 ret
= cn_vprintf(cn
, fmt
, arg
);
142 * Ensure that this coredump name component can't cause the
143 * resulting corefile path to consist of a ".." or ".".
145 if ((cn
->used
- cur
== 1 && cn
->corename
[cur
] == '.') ||
146 (cn
->used
- cur
== 2 && cn
->corename
[cur
] == '.'
147 && cn
->corename
[cur
+1] == '.'))
148 cn
->corename
[cur
] = '!';
151 * Empty names are fishy and could be used to create a "//" in a
152 * corefile name, causing the coredump to happen one directory
153 * level too high. Enforce that all components of the core
154 * pattern are at least one character long.
157 ret
= cn_printf(cn
, "!");
160 for (; cur
< cn
->used
; ++cur
) {
161 if (cn
->corename
[cur
] == '/')
162 cn
->corename
[cur
] = '!';
167 static int cn_print_exe_file(struct core_name
*cn
, bool name_only
)
169 struct file
*exe_file
;
170 char *pathbuf
, *path
, *ptr
;
173 exe_file
= get_mm_exe_file(current
->mm
);
175 return cn_esc_printf(cn
, "%s (path unknown)", current
->comm
);
177 pathbuf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
183 path
= file_path(exe_file
, pathbuf
, PATH_MAX
);
190 ptr
= strrchr(path
, '/');
194 ret
= cn_esc_printf(cn
, "%s", path
);
203 /* format_corename will inspect the pattern parameter, and output a
204 * name into corename, which must have space for at least
205 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
207 static int format_corename(struct core_name
*cn
, struct coredump_params
*cprm
,
208 size_t **argv
, int *argc
)
210 const struct cred
*cred
= current_cred();
211 const char *pat_ptr
= core_pattern
;
212 int ispipe
= (*pat_ptr
== '|');
213 bool was_space
= false;
214 int pid_in_pattern
= 0;
219 if (expand_corename(cn
, core_name_size
))
221 cn
->corename
[0] = '\0';
224 int argvs
= sizeof(core_pattern
) / 2;
225 (*argv
) = kmalloc_array(argvs
, sizeof(**argv
), GFP_KERNEL
);
228 (*argv
)[(*argc
)++] = 0;
234 /* Repeat as long as we have more pattern to process and more output
238 * Split on spaces before doing template expansion so that
239 * %e and %E don't get split if they have spaces in them
242 if (isspace(*pat_ptr
)) {
247 } else if (was_space
) {
249 err
= cn_printf(cn
, "%c", '\0');
252 (*argv
)[(*argc
)++] = cn
->used
;
255 if (*pat_ptr
!= '%') {
256 err
= cn_printf(cn
, "%c", *pat_ptr
++);
258 switch (*++pat_ptr
) {
259 /* single % at the end, drop that */
262 /* Double percent, output one percent */
264 err
= cn_printf(cn
, "%c", '%');
269 err
= cn_printf(cn
, "%d",
270 task_tgid_vnr(current
));
274 err
= cn_printf(cn
, "%d",
275 task_tgid_nr(current
));
278 err
= cn_printf(cn
, "%d",
279 task_pid_vnr(current
));
282 err
= cn_printf(cn
, "%d",
283 task_pid_nr(current
));
287 err
= cn_printf(cn
, "%u",
288 from_kuid(&init_user_ns
,
293 err
= cn_printf(cn
, "%u",
294 from_kgid(&init_user_ns
,
298 err
= cn_printf(cn
, "%d",
299 __get_dumpable(cprm
->mm_flags
));
301 /* signal that caused the coredump */
303 err
= cn_printf(cn
, "%d",
304 cprm
->siginfo
->si_signo
);
306 /* UNIX time of coredump */
310 time
= ktime_get_real_seconds();
311 err
= cn_printf(cn
, "%lld", time
);
317 err
= cn_esc_printf(cn
, "%s",
318 utsname()->nodename
);
321 /* executable, could be changed by prctl PR_SET_NAME etc */
323 err
= cn_esc_printf(cn
, "%s", current
->comm
);
325 /* file name of executable */
327 err
= cn_print_exe_file(cn
, true);
330 err
= cn_print_exe_file(cn
, false);
332 /* core limit size */
334 err
= cn_printf(cn
, "%lu",
335 rlimit(RLIMIT_CORE
));
337 /* CPU the task ran on */
339 err
= cn_printf(cn
, "%d", cprm
->cpu
);
352 /* Backward compatibility with core_uses_pid:
354 * If core_pattern does not include a %p (as is the default)
355 * and core_uses_pid is set, then .%pid will be appended to
356 * the filename. Do not do this for piped commands. */
357 if (!ispipe
&& !pid_in_pattern
&& core_uses_pid
) {
358 err
= cn_printf(cn
, ".%d", task_tgid_vnr(current
));
365 static int zap_process(struct signal_struct
*signal
, int exit_code
)
367 struct task_struct
*t
;
370 signal
->flags
= SIGNAL_GROUP_EXIT
;
371 signal
->group_exit_code
= exit_code
;
372 signal
->group_stop_count
= 0;
374 __for_each_thread(signal
, t
) {
375 task_clear_jobctl_pending(t
, JOBCTL_PENDING_MASK
);
376 if (t
!= current
&& !(t
->flags
& PF_POSTCOREDUMP
)) {
377 sigaddset(&t
->pending
.signal
, SIGKILL
);
378 signal_wake_up(t
, 1);
386 static int zap_threads(struct task_struct
*tsk
,
387 struct core_state
*core_state
, int exit_code
)
389 struct signal_struct
*signal
= tsk
->signal
;
392 spin_lock_irq(&tsk
->sighand
->siglock
);
393 if (!(signal
->flags
& SIGNAL_GROUP_EXIT
) && !signal
->group_exec_task
) {
394 /* Allow SIGKILL, see prepare_signal() */
395 signal
->core_state
= core_state
;
396 nr
= zap_process(signal
, exit_code
);
397 clear_tsk_thread_flag(tsk
, TIF_SIGPENDING
);
398 tsk
->flags
|= PF_DUMPCORE
;
399 atomic_set(&core_state
->nr_threads
, nr
);
401 spin_unlock_irq(&tsk
->sighand
->siglock
);
405 static int coredump_wait(int exit_code
, struct core_state
*core_state
)
407 struct task_struct
*tsk
= current
;
408 int core_waiters
= -EBUSY
;
410 init_completion(&core_state
->startup
);
411 core_state
->dumper
.task
= tsk
;
412 core_state
->dumper
.next
= NULL
;
414 core_waiters
= zap_threads(tsk
, core_state
, exit_code
);
415 if (core_waiters
> 0) {
416 struct core_thread
*ptr
;
418 wait_for_completion_state(&core_state
->startup
,
419 TASK_UNINTERRUPTIBLE
|TASK_FREEZABLE
);
421 * Wait for all the threads to become inactive, so that
422 * all the thread context (extended register state, like
423 * fpu etc) gets copied to the memory.
425 ptr
= core_state
->dumper
.next
;
426 while (ptr
!= NULL
) {
427 wait_task_inactive(ptr
->task
, TASK_ANY
);
435 static void coredump_finish(bool core_dumped
)
437 struct core_thread
*curr
, *next
;
438 struct task_struct
*task
;
440 spin_lock_irq(¤t
->sighand
->siglock
);
441 if (core_dumped
&& !__fatal_signal_pending(current
))
442 current
->signal
->group_exit_code
|= 0x80;
443 next
= current
->signal
->core_state
->dumper
.next
;
444 current
->signal
->core_state
= NULL
;
445 spin_unlock_irq(¤t
->sighand
->siglock
);
447 while ((curr
= next
) != NULL
) {
451 * see coredump_task_exit(), curr->task must not see
452 * ->task == NULL before we read ->next.
456 wake_up_process(task
);
460 static bool dump_interrupted(void)
463 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
464 * can do try_to_freeze() and check __fatal_signal_pending(),
465 * but then we need to teach dump_write() to restart and clear
468 return fatal_signal_pending(current
) || freezing(current
);
471 static void wait_for_dump_helpers(struct file
*file
)
473 struct pipe_inode_info
*pipe
= file
->private_data
;
478 wake_up_interruptible_sync(&pipe
->rd_wait
);
479 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
483 * We actually want wait_event_freezable() but then we need
484 * to clear TIF_SIGPENDING and improve dump_interrupted().
486 wait_event_interruptible(pipe
->rd_wait
, pipe
->readers
== 1);
496 * helper function to customize the process used
497 * to collect the core in userspace. Specifically
498 * it sets up a pipe and installs it as fd 0 (stdin)
499 * for the process. Returns 0 on success, or
500 * PTR_ERR on failure.
501 * Note that it also sets the core limit to 1. This
502 * is a special value that we use to trap recursive
505 static int umh_pipe_setup(struct subprocess_info
*info
, struct cred
*new)
507 struct file
*files
[2];
508 struct coredump_params
*cp
= (struct coredump_params
*)info
->data
;
509 int err
= create_pipe_files(files
, 0);
515 err
= replace_fd(0, files
[0], 0);
517 /* and disallow core files too */
518 current
->signal
->rlim
[RLIMIT_CORE
] = (struct rlimit
){1, 1};
523 void do_coredump(const kernel_siginfo_t
*siginfo
)
525 struct core_state core_state
;
527 struct mm_struct
*mm
= current
->mm
;
528 struct linux_binfmt
* binfmt
;
529 const struct cred
*old_cred
;
535 /* require nonrelative corefile path and be extra careful */
536 bool need_suid_safe
= false;
537 bool core_dumped
= false;
538 static atomic_t core_dump_count
= ATOMIC_INIT(0);
539 struct coredump_params cprm
= {
541 .limit
= rlimit(RLIMIT_CORE
),
543 * We must use the same mm->flags while dumping core to avoid
544 * inconsistency of bit flags, since this flag is not protected
547 .mm_flags
= mm
->flags
,
549 .cpu
= raw_smp_processor_id(),
552 audit_core_dumps(siginfo
->si_signo
);
555 if (!binfmt
|| !binfmt
->core_dump
)
557 if (!__get_dumpable(cprm
.mm_flags
))
560 cred
= prepare_creds();
564 * We cannot trust fsuid as being the "true" uid of the process
565 * nor do we know its entire history. We only know it was tainted
566 * so we dump it as root in mode 2, and only into a controlled
567 * environment (pipe handler or fully qualified path).
569 if (__get_dumpable(cprm
.mm_flags
) == SUID_DUMP_ROOT
) {
570 /* Setuid core dump mode */
571 cred
->fsuid
= GLOBAL_ROOT_UID
; /* Dump root private */
572 need_suid_safe
= true;
575 retval
= coredump_wait(siginfo
->si_signo
, &core_state
);
579 old_cred
= override_creds(cred
);
581 ispipe
= format_corename(&cn
, &cprm
, &argv
, &argc
);
587 struct subprocess_info
*sub_info
;
590 coredump_report_failure("format_corename failed, aborting core");
594 if (cprm
.limit
== 1) {
595 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
597 * Normally core limits are irrelevant to pipes, since
598 * we're not writing to the file system, but we use
599 * cprm.limit of 1 here as a special value, this is a
600 * consistent way to catch recursive crashes.
601 * We can still crash if the core_pattern binary sets
602 * RLIM_CORE = !1, but it runs as root, and can do
603 * lots of stupid things.
605 * Note that we use task_tgid_vnr here to grab the pid
606 * of the process group leader. That way we get the
607 * right pid if a thread in a multi-threaded
608 * core_pattern process dies.
610 coredump_report_failure("RLIMIT_CORE is set to 1, aborting core");
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 coredump_report_failure("over core_pipe_limit, skipping core dump");
621 helper_argv
= kmalloc_array(argc
+ 1, sizeof(*helper_argv
),
624 coredump_report_failure("%s failed to allocate memory", __func__
);
627 for (argi
= 0; argi
< argc
; argi
++)
628 helper_argv
[argi
] = cn
.corename
+ argv
[argi
];
629 helper_argv
[argi
] = NULL
;
632 sub_info
= call_usermodehelper_setup(helper_argv
[0],
633 helper_argv
, NULL
, GFP_KERNEL
,
634 umh_pipe_setup
, NULL
, &cprm
);
636 retval
= call_usermodehelper_exec(sub_info
,
641 coredump_report_failure("|%s pipe failed", cn
.corename
);
645 struct mnt_idmap
*idmap
;
647 int open_flags
= O_CREAT
| O_WRONLY
| O_NOFOLLOW
|
648 O_LARGEFILE
| O_EXCL
;
650 if (cprm
.limit
< binfmt
->min_coredump
)
653 if (need_suid_safe
&& cn
.corename
[0] != '/') {
654 coredump_report_failure(
655 "this process can only dump core to a fully qualified path, skipping core dump");
660 * Unlink the file if it exists unless this is a SUID
661 * binary - in that case, we're running around with root
662 * privs and don't want to unlink another user's coredump.
664 if (!need_suid_safe
) {
666 * If it doesn't exist, that's fine. If there's some
667 * other problem, we'll catch it at the filp_open().
669 do_unlinkat(AT_FDCWD
, getname_kernel(cn
.corename
));
673 * There is a race between unlinking and creating the
674 * file, but if that causes an EEXIST here, that's
675 * fine - another process raced with us while creating
676 * the corefile, and the other process won. To userspace,
677 * what matters is that at least one of the two processes
678 * writes its coredump successfully, not which one.
680 if (need_suid_safe
) {
682 * Using user namespaces, normal user tasks can change
683 * their current->fs->root to point to arbitrary
684 * directories. Since the intention of the "only dump
685 * with a fully qualified path" rule is to control where
686 * coredumps may be placed using root privileges,
687 * current->fs->root must not be used. Instead, use the
688 * root directory of init_task.
692 task_lock(&init_task
);
693 get_fs_root(init_task
.fs
, &root
);
694 task_unlock(&init_task
);
695 cprm
.file
= file_open_root(&root
, cn
.corename
,
699 cprm
.file
= filp_open(cn
.corename
, open_flags
, 0600);
701 if (IS_ERR(cprm
.file
))
704 inode
= file_inode(cprm
.file
);
705 if (inode
->i_nlink
> 1)
707 if (d_unhashed(cprm
.file
->f_path
.dentry
))
710 * AK: actually i see no reason to not allow this for named
711 * pipes etc, but keep the previous behaviour for now.
713 if (!S_ISREG(inode
->i_mode
))
716 * Don't dump core if the filesystem changed owner or mode
717 * of the file during file creation. This is an issue when
718 * a process dumps core while its cwd is e.g. on a vfat
721 idmap
= file_mnt_idmap(cprm
.file
);
722 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap
, inode
),
724 coredump_report_failure("Core dump to %s aborted: "
725 "cannot preserve file owner", cn
.corename
);
728 if ((inode
->i_mode
& 0677) != 0600) {
729 coredump_report_failure("Core dump to %s aborted: "
730 "cannot preserve file permissions", cn
.corename
);
733 if (!(cprm
.file
->f_mode
& FMODE_CAN_WRITE
))
735 if (do_truncate(idmap
, cprm
.file
->f_path
.dentry
,
740 /* get us an unshared descriptor table; almost always a no-op */
741 /* The cell spufs coredump code reads the file descriptor tables */
742 retval
= unshare_files();
745 if (!dump_interrupted()) {
747 * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
748 * have this set to NULL.
751 coredump_report_failure("Core dump to |%s disabled", cn
.corename
);
754 if (!dump_vma_snapshot(&cprm
))
757 file_start_write(cprm
.file
);
758 core_dumped
= binfmt
->core_dump(&cprm
);
760 * Ensures that file size is big enough to contain the current
761 * file postion. This prevents gdb from complaining about
762 * a truncated file if the last "write" to the file was
767 dump_emit(&cprm
, "", 1);
769 file_end_write(cprm
.file
);
770 free_vma_snapshot(&cprm
);
772 if (ispipe
&& core_pipe_limit
)
773 wait_for_dump_helpers(cprm
.file
);
776 filp_close(cprm
.file
, NULL
);
779 atomic_dec(&core_dump_count
);
783 coredump_finish(core_dumped
);
784 revert_creds(old_cred
);
792 * Core dumping helper functions. These are the only things you should
793 * do on a core-file: use only these functions to write out all the
796 static int __dump_emit(struct coredump_params
*cprm
, const void *addr
, int nr
)
798 struct file
*file
= cprm
->file
;
799 loff_t pos
= file
->f_pos
;
801 if (cprm
->written
+ nr
> cprm
->limit
)
805 if (dump_interrupted())
807 n
= __kernel_write(file
, addr
, nr
, &pos
);
817 static int __dump_skip(struct coredump_params
*cprm
, size_t nr
)
819 static char zeroes
[PAGE_SIZE
];
820 struct file
*file
= cprm
->file
;
821 if (file
->f_mode
& FMODE_LSEEK
) {
822 if (dump_interrupted() ||
823 vfs_llseek(file
, nr
, SEEK_CUR
) < 0)
828 while (nr
> PAGE_SIZE
) {
829 if (!__dump_emit(cprm
, zeroes
, PAGE_SIZE
))
833 return __dump_emit(cprm
, zeroes
, nr
);
837 int dump_emit(struct coredump_params
*cprm
, const void *addr
, int nr
)
840 if (!__dump_skip(cprm
, cprm
->to_skip
))
844 return __dump_emit(cprm
, addr
, nr
);
846 EXPORT_SYMBOL(dump_emit
);
848 void dump_skip_to(struct coredump_params
*cprm
, unsigned long pos
)
850 cprm
->to_skip
= pos
- cprm
->pos
;
852 EXPORT_SYMBOL(dump_skip_to
);
854 void dump_skip(struct coredump_params
*cprm
, size_t nr
)
858 EXPORT_SYMBOL(dump_skip
);
860 #ifdef CONFIG_ELF_CORE
861 static int dump_emit_page(struct coredump_params
*cprm
, struct page
*page
)
864 struct iov_iter iter
;
865 struct file
*file
= cprm
->file
;
873 if (!__dump_skip(cprm
, cprm
->to_skip
))
877 if (cprm
->written
+ PAGE_SIZE
> cprm
->limit
)
879 if (dump_interrupted())
882 bvec_set_page(&bvec
, page
, PAGE_SIZE
, 0);
883 iov_iter_bvec(&iter
, ITER_SOURCE
, &bvec
, 1, PAGE_SIZE
);
884 n
= __kernel_write_iter(cprm
->file
, &iter
, &pos
);
888 cprm
->written
+= PAGE_SIZE
;
889 cprm
->pos
+= PAGE_SIZE
;
895 * If we might get machine checks from kernel accesses during the
896 * core dump, let's get those errors early rather than during the
897 * IO. This is not performance-critical enough to warrant having
898 * all the machine check logic in the iovec paths.
900 #ifdef copy_mc_to_kernel
902 #define dump_page_alloc() alloc_page(GFP_KERNEL)
903 #define dump_page_free(x) __free_page(x)
904 static struct page
*dump_page_copy(struct page
*src
, struct page
*dst
)
906 void *buf
= kmap_local_page(src
);
907 size_t left
= copy_mc_to_kernel(page_address(dst
), buf
, PAGE_SIZE
);
909 return left
? NULL
: dst
;
914 /* We just want to return non-NULL; it's never used. */
915 #define dump_page_alloc() ERR_PTR(-EINVAL)
916 #define dump_page_free(x) ((void)(x))
917 static inline struct page
*dump_page_copy(struct page
*src
, struct page
*dst
)
923 int dump_user_range(struct coredump_params
*cprm
, unsigned long start
,
927 struct page
*dump_page
;
929 dump_page
= dump_page_alloc();
933 for (addr
= start
; addr
< start
+ len
; addr
+= PAGE_SIZE
) {
937 * To avoid having to allocate page tables for virtual address
938 * ranges that have never been used yet, and also to make it
939 * easy to generate sparse core files, use a helper that returns
940 * NULL when encountering an empty page table entry that would
941 * otherwise have been filled with the zero page.
943 page
= get_dump_page(addr
);
945 int stop
= !dump_emit_page(cprm
, dump_page_copy(page
, dump_page
));
948 dump_page_free(dump_page
);
952 dump_skip(cprm
, PAGE_SIZE
);
956 dump_page_free(dump_page
);
961 int dump_align(struct coredump_params
*cprm
, int align
)
963 unsigned mod
= (cprm
->pos
+ cprm
->to_skip
) & (align
- 1);
964 if (align
& (align
- 1))
967 cprm
->to_skip
+= align
- mod
;
970 EXPORT_SYMBOL(dump_align
);
974 void validate_coredump_safety(void)
976 if (suid_dumpable
== SUID_DUMP_ROOT
&&
977 core_pattern
[0] != '/' && core_pattern
[0] != '|') {
979 coredump_report_failure("Unsafe core_pattern used with fs.suid_dumpable=2: "
980 "pipe handler or fully qualified core dump path required. "
981 "Set kernel.core_pattern before fs.suid_dumpable.");
985 static int proc_dostring_coredump(const struct ctl_table
*table
, int write
,
986 void *buffer
, size_t *lenp
, loff_t
*ppos
)
988 int error
= proc_dostring(table
, write
, buffer
, lenp
, ppos
);
991 validate_coredump_safety();
995 static const unsigned int core_file_note_size_min
= CORE_FILE_NOTE_SIZE_DEFAULT
;
996 static const unsigned int core_file_note_size_max
= CORE_FILE_NOTE_SIZE_MAX
;
998 static struct ctl_table coredump_sysctls
[] = {
1000 .procname
= "core_uses_pid",
1001 .data
= &core_uses_pid
,
1002 .maxlen
= sizeof(int),
1004 .proc_handler
= proc_dointvec
,
1007 .procname
= "core_pattern",
1008 .data
= core_pattern
,
1009 .maxlen
= CORENAME_MAX_SIZE
,
1011 .proc_handler
= proc_dostring_coredump
,
1014 .procname
= "core_pipe_limit",
1015 .data
= &core_pipe_limit
,
1016 .maxlen
= sizeof(unsigned int),
1018 .proc_handler
= proc_dointvec
,
1021 .procname
= "core_file_note_size_limit",
1022 .data
= &core_file_note_size_limit
,
1023 .maxlen
= sizeof(unsigned int),
1025 .proc_handler
= proc_douintvec_minmax
,
1026 .extra1
= (unsigned int *)&core_file_note_size_min
,
1027 .extra2
= (unsigned int *)&core_file_note_size_max
,
1031 static int __init
init_fs_coredump_sysctls(void)
1033 register_sysctl_init("kernel", coredump_sysctls
);
1036 fs_initcall(init_fs_coredump_sysctls
);
1037 #endif /* CONFIG_SYSCTL */
1040 * The purpose of always_dump_vma() is to make sure that special kernel mappings
1041 * that are useful for post-mortem analysis are included in every core dump.
1042 * In that way we ensure that the core dump is fully interpretable later
1043 * without matching up the same kernel and hardware config to see what PC values
1044 * meant. These special mappings include - vDSO, vsyscall, and other
1045 * architecture specific mappings
1047 static bool always_dump_vma(struct vm_area_struct
*vma
)
1049 /* Any vsyscall mappings? */
1050 if (vma
== get_gate_vma(vma
->vm_mm
))
1054 * Assume that all vmas with a .name op should always be dumped.
1055 * If this changes, a new vm_ops field can easily be added.
1057 if (vma
->vm_ops
&& vma
->vm_ops
->name
&& vma
->vm_ops
->name(vma
))
1061 * arch_vma_name() returns non-NULL for special architecture mappings,
1062 * such as vDSO sections.
1064 if (arch_vma_name(vma
))
1070 #define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1
1073 * Decide how much of @vma's contents should be included in a core dump.
1075 static unsigned long vma_dump_size(struct vm_area_struct
*vma
,
1076 unsigned long mm_flags
)
1078 #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1080 /* always dump the vdso and vsyscall sections */
1081 if (always_dump_vma(vma
))
1084 if (vma
->vm_flags
& VM_DONTDUMP
)
1087 /* support for DAX */
1088 if (vma_is_dax(vma
)) {
1089 if ((vma
->vm_flags
& VM_SHARED
) && FILTER(DAX_SHARED
))
1091 if (!(vma
->vm_flags
& VM_SHARED
) && FILTER(DAX_PRIVATE
))
1096 /* Hugetlb memory check */
1097 if (is_vm_hugetlb_page(vma
)) {
1098 if ((vma
->vm_flags
& VM_SHARED
) && FILTER(HUGETLB_SHARED
))
1100 if (!(vma
->vm_flags
& VM_SHARED
) && FILTER(HUGETLB_PRIVATE
))
1105 /* Do not dump I/O mapped devices or special mappings */
1106 if (vma
->vm_flags
& VM_IO
)
1109 /* By default, dump shared memory if mapped from an anonymous file. */
1110 if (vma
->vm_flags
& VM_SHARED
) {
1111 if (file_inode(vma
->vm_file
)->i_nlink
== 0 ?
1112 FILTER(ANON_SHARED
) : FILTER(MAPPED_SHARED
))
1117 /* Dump segments that have been written to. */
1118 if ((!IS_ENABLED(CONFIG_MMU
) || vma
->anon_vma
) && FILTER(ANON_PRIVATE
))
1120 if (vma
->vm_file
== NULL
)
1123 if (FILTER(MAPPED_PRIVATE
))
1127 * If this is the beginning of an executable file mapping,
1128 * dump the first page to aid in determining what was mapped here.
1130 if (FILTER(ELF_HEADERS
) &&
1131 vma
->vm_pgoff
== 0 && (vma
->vm_flags
& VM_READ
)) {
1132 if ((READ_ONCE(file_inode(vma
->vm_file
)->i_mode
) & 0111) != 0)
1136 * ELF libraries aren't always executable.
1137 * We'll want to check whether the mapping starts with the ELF
1138 * magic, but not now - we're holding the mmap lock,
1139 * so copy_from_user() doesn't work here.
1140 * Use a placeholder instead, and fix it up later in
1141 * dump_vma_snapshot().
1143 return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER
;
1151 return vma
->vm_end
- vma
->vm_start
;
1155 * Helper function for iterating across a vma list. It ensures that the caller
1156 * will visit `gate_vma' prior to terminating the search.
1158 static struct vm_area_struct
*coredump_next_vma(struct vma_iterator
*vmi
,
1159 struct vm_area_struct
*vma
,
1160 struct vm_area_struct
*gate_vma
)
1162 if (gate_vma
&& (vma
== gate_vma
))
1165 vma
= vma_next(vmi
);
1171 static void free_vma_snapshot(struct coredump_params
*cprm
)
1173 if (cprm
->vma_meta
) {
1175 for (i
= 0; i
< cprm
->vma_count
; i
++) {
1176 struct file
*file
= cprm
->vma_meta
[i
].file
;
1180 kvfree(cprm
->vma_meta
);
1181 cprm
->vma_meta
= NULL
;
1185 static int cmp_vma_size(const void *vma_meta_lhs_ptr
, const void *vma_meta_rhs_ptr
)
1187 const struct core_vma_metadata
*vma_meta_lhs
= vma_meta_lhs_ptr
;
1188 const struct core_vma_metadata
*vma_meta_rhs
= vma_meta_rhs_ptr
;
1190 if (vma_meta_lhs
->dump_size
< vma_meta_rhs
->dump_size
)
1192 if (vma_meta_lhs
->dump_size
> vma_meta_rhs
->dump_size
)
1198 * Under the mmap_lock, take a snapshot of relevant information about the task's
1201 static bool dump_vma_snapshot(struct coredump_params
*cprm
)
1203 struct vm_area_struct
*gate_vma
, *vma
= NULL
;
1204 struct mm_struct
*mm
= current
->mm
;
1205 VMA_ITERATOR(vmi
, mm
, 0);
1209 * Once the stack expansion code is fixed to not change VMA bounds
1210 * under mmap_lock in read mode, this can be changed to take the
1211 * mmap_lock in read mode.
1213 if (mmap_write_lock_killable(mm
))
1216 cprm
->vma_data_size
= 0;
1217 gate_vma
= get_gate_vma(mm
);
1218 cprm
->vma_count
= mm
->map_count
+ (gate_vma
? 1 : 0);
1220 cprm
->vma_meta
= kvmalloc_array(cprm
->vma_count
, sizeof(*cprm
->vma_meta
), GFP_KERNEL
);
1221 if (!cprm
->vma_meta
) {
1222 mmap_write_unlock(mm
);
1226 while ((vma
= coredump_next_vma(&vmi
, vma
, gate_vma
)) != NULL
) {
1227 struct core_vma_metadata
*m
= cprm
->vma_meta
+ i
;
1229 m
->start
= vma
->vm_start
;
1230 m
->end
= vma
->vm_end
;
1231 m
->flags
= vma
->vm_flags
;
1232 m
->dump_size
= vma_dump_size(vma
, cprm
->mm_flags
);
1233 m
->pgoff
= vma
->vm_pgoff
;
1234 m
->file
= vma
->vm_file
;
1240 mmap_write_unlock(mm
);
1242 for (i
= 0; i
< cprm
->vma_count
; i
++) {
1243 struct core_vma_metadata
*m
= cprm
->vma_meta
+ i
;
1245 if (m
->dump_size
== DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER
) {
1246 char elfmag
[SELFMAG
];
1248 if (copy_from_user(elfmag
, (void __user
*)m
->start
, SELFMAG
) ||
1249 memcmp(elfmag
, ELFMAG
, SELFMAG
) != 0) {
1252 m
->dump_size
= PAGE_SIZE
;
1256 cprm
->vma_data_size
+= m
->dump_size
;
1259 sort(cprm
->vma_meta
, cprm
->vma_count
, sizeof(*cprm
->vma_meta
),
1260 cmp_vma_size
, NULL
);