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 #define CORE_FILE_NOTE_SIZE_DEFAULT (4*1024*1024)
60 /* Define a reasonable max cap */
61 #define CORE_FILE_NOTE_SIZE_MAX (16*1024*1024)
63 static int core_uses_pid
;
64 static unsigned int core_pipe_limit
;
65 static char core_pattern
[CORENAME_MAX_SIZE
] = "core";
66 static int core_name_size
= CORENAME_MAX_SIZE
;
67 unsigned int core_file_note_size_limit
= CORE_FILE_NOTE_SIZE_DEFAULT
;
74 static int expand_corename(struct core_name
*cn
, int size
)
78 size
= kmalloc_size_roundup(size
);
79 corename
= krealloc(cn
->corename
, size
, GFP_KERNEL
);
84 if (size
> core_name_size
) /* racy but harmless */
85 core_name_size
= size
;
88 cn
->corename
= corename
;
92 static __printf(2, 0) int cn_vprintf(struct core_name
*cn
, const char *fmt
,
99 free
= cn
->size
- cn
->used
;
101 va_copy(arg_copy
, arg
);
102 need
= vsnprintf(cn
->corename
+ cn
->used
, free
, fmt
, arg_copy
);
110 if (!expand_corename(cn
, cn
->size
+ need
- free
+ 1))
116 static __printf(2, 3) int cn_printf(struct core_name
*cn
, const char *fmt
, ...)
122 ret
= cn_vprintf(cn
, fmt
, arg
);
128 static __printf(2, 3)
129 int cn_esc_printf(struct core_name
*cn
, const char *fmt
, ...)
136 ret
= cn_vprintf(cn
, fmt
, arg
);
141 * Ensure that this coredump name component can't cause the
142 * resulting corefile path to consist of a ".." or ".".
144 if ((cn
->used
- cur
== 1 && cn
->corename
[cur
] == '.') ||
145 (cn
->used
- cur
== 2 && cn
->corename
[cur
] == '.'
146 && cn
->corename
[cur
+1] == '.'))
147 cn
->corename
[cur
] = '!';
150 * Empty names are fishy and could be used to create a "//" in a
151 * corefile name, causing the coredump to happen one directory
152 * level too high. Enforce that all components of the core
153 * pattern are at least one character long.
156 ret
= cn_printf(cn
, "!");
159 for (; cur
< cn
->used
; ++cur
) {
160 if (cn
->corename
[cur
] == '/')
161 cn
->corename
[cur
] = '!';
166 static int cn_print_exe_file(struct core_name
*cn
, bool name_only
)
168 struct file
*exe_file
;
169 char *pathbuf
, *path
, *ptr
;
172 exe_file
= get_mm_exe_file(current
->mm
);
174 return cn_esc_printf(cn
, "%s (path unknown)", current
->comm
);
176 pathbuf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
182 path
= file_path(exe_file
, pathbuf
, PATH_MAX
);
189 ptr
= strrchr(path
, '/');
193 ret
= cn_esc_printf(cn
, "%s", path
);
202 /* format_corename will inspect the pattern parameter, and output a
203 * name into corename, which must have space for at least
204 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
206 static int format_corename(struct core_name
*cn
, struct coredump_params
*cprm
,
207 size_t **argv
, int *argc
)
209 const struct cred
*cred
= current_cred();
210 const char *pat_ptr
= core_pattern
;
211 int ispipe
= (*pat_ptr
== '|');
212 bool was_space
= false;
213 int pid_in_pattern
= 0;
218 if (expand_corename(cn
, core_name_size
))
220 cn
->corename
[0] = '\0';
223 int argvs
= sizeof(core_pattern
) / 2;
224 (*argv
) = kmalloc_array(argvs
, sizeof(**argv
), GFP_KERNEL
);
227 (*argv
)[(*argc
)++] = 0;
233 /* Repeat as long as we have more pattern to process and more output
237 * Split on spaces before doing template expansion so that
238 * %e and %E don't get split if they have spaces in them
241 if (isspace(*pat_ptr
)) {
246 } else if (was_space
) {
248 err
= cn_printf(cn
, "%c", '\0');
251 (*argv
)[(*argc
)++] = cn
->used
;
254 if (*pat_ptr
!= '%') {
255 err
= cn_printf(cn
, "%c", *pat_ptr
++);
257 switch (*++pat_ptr
) {
258 /* single % at the end, drop that */
261 /* Double percent, output one percent */
263 err
= cn_printf(cn
, "%c", '%');
268 err
= cn_printf(cn
, "%d",
269 task_tgid_vnr(current
));
273 err
= cn_printf(cn
, "%d",
274 task_tgid_nr(current
));
277 err
= cn_printf(cn
, "%d",
278 task_pid_vnr(current
));
281 err
= cn_printf(cn
, "%d",
282 task_pid_nr(current
));
286 err
= cn_printf(cn
, "%u",
287 from_kuid(&init_user_ns
,
292 err
= cn_printf(cn
, "%u",
293 from_kgid(&init_user_ns
,
297 err
= cn_printf(cn
, "%d",
298 __get_dumpable(cprm
->mm_flags
));
300 /* signal that caused the coredump */
302 err
= cn_printf(cn
, "%d",
303 cprm
->siginfo
->si_signo
);
305 /* UNIX time of coredump */
309 time
= ktime_get_real_seconds();
310 err
= cn_printf(cn
, "%lld", time
);
316 err
= cn_esc_printf(cn
, "%s",
317 utsname()->nodename
);
320 /* executable, could be changed by prctl PR_SET_NAME etc */
322 err
= cn_esc_printf(cn
, "%s", current
->comm
);
324 /* file name of executable */
326 err
= cn_print_exe_file(cn
, true);
329 err
= cn_print_exe_file(cn
, false);
331 /* core limit size */
333 err
= cn_printf(cn
, "%lu",
334 rlimit(RLIMIT_CORE
));
336 /* CPU the task ran on */
338 err
= cn_printf(cn
, "%d", cprm
->cpu
);
351 /* Backward compatibility with core_uses_pid:
353 * If core_pattern does not include a %p (as is the default)
354 * and core_uses_pid is set, then .%pid will be appended to
355 * the filename. Do not do this for piped commands. */
356 if (!ispipe
&& !pid_in_pattern
&& core_uses_pid
) {
357 err
= cn_printf(cn
, ".%d", task_tgid_vnr(current
));
364 static int zap_process(struct signal_struct
*signal
, int exit_code
)
366 struct task_struct
*t
;
369 signal
->flags
= SIGNAL_GROUP_EXIT
;
370 signal
->group_exit_code
= exit_code
;
371 signal
->group_stop_count
= 0;
373 __for_each_thread(signal
, t
) {
374 task_clear_jobctl_pending(t
, JOBCTL_PENDING_MASK
);
375 if (t
!= current
&& !(t
->flags
& PF_POSTCOREDUMP
)) {
376 sigaddset(&t
->pending
.signal
, SIGKILL
);
377 signal_wake_up(t
, 1);
385 static int zap_threads(struct task_struct
*tsk
,
386 struct core_state
*core_state
, int exit_code
)
388 struct signal_struct
*signal
= tsk
->signal
;
391 spin_lock_irq(&tsk
->sighand
->siglock
);
392 if (!(signal
->flags
& SIGNAL_GROUP_EXIT
) && !signal
->group_exec_task
) {
393 /* Allow SIGKILL, see prepare_signal() */
394 signal
->core_state
= core_state
;
395 nr
= zap_process(signal
, exit_code
);
396 clear_tsk_thread_flag(tsk
, TIF_SIGPENDING
);
397 tsk
->flags
|= PF_DUMPCORE
;
398 atomic_set(&core_state
->nr_threads
, nr
);
400 spin_unlock_irq(&tsk
->sighand
->siglock
);
404 static int coredump_wait(int exit_code
, struct core_state
*core_state
)
406 struct task_struct
*tsk
= current
;
407 int core_waiters
= -EBUSY
;
409 init_completion(&core_state
->startup
);
410 core_state
->dumper
.task
= tsk
;
411 core_state
->dumper
.next
= NULL
;
413 core_waiters
= zap_threads(tsk
, core_state
, exit_code
);
414 if (core_waiters
> 0) {
415 struct core_thread
*ptr
;
417 wait_for_completion_state(&core_state
->startup
,
418 TASK_UNINTERRUPTIBLE
|TASK_FREEZABLE
);
420 * Wait for all the threads to become inactive, so that
421 * all the thread context (extended register state, like
422 * fpu etc) gets copied to the memory.
424 ptr
= core_state
->dumper
.next
;
425 while (ptr
!= NULL
) {
426 wait_task_inactive(ptr
->task
, TASK_ANY
);
434 static void coredump_finish(bool core_dumped
)
436 struct core_thread
*curr
, *next
;
437 struct task_struct
*task
;
439 spin_lock_irq(¤t
->sighand
->siglock
);
440 if (core_dumped
&& !__fatal_signal_pending(current
))
441 current
->signal
->group_exit_code
|= 0x80;
442 next
= current
->signal
->core_state
->dumper
.next
;
443 current
->signal
->core_state
= NULL
;
444 spin_unlock_irq(¤t
->sighand
->siglock
);
446 while ((curr
= next
) != NULL
) {
450 * see coredump_task_exit(), curr->task must not see
451 * ->task == NULL before we read ->next.
455 wake_up_process(task
);
459 static bool dump_interrupted(void)
462 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
463 * can do try_to_freeze() and check __fatal_signal_pending(),
464 * but then we need to teach dump_write() to restart and clear
467 return fatal_signal_pending(current
) || freezing(current
);
470 static void wait_for_dump_helpers(struct file
*file
)
472 struct pipe_inode_info
*pipe
= file
->private_data
;
477 wake_up_interruptible_sync(&pipe
->rd_wait
);
478 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
482 * We actually want wait_event_freezable() but then we need
483 * to clear TIF_SIGPENDING and improve dump_interrupted().
485 wait_event_interruptible(pipe
->rd_wait
, pipe
->readers
== 1);
495 * helper function to customize the process used
496 * to collect the core in userspace. Specifically
497 * it sets up a pipe and installs it as fd 0 (stdin)
498 * for the process. Returns 0 on success, or
499 * PTR_ERR on failure.
500 * Note that it also sets the core limit to 1. This
501 * is a special value that we use to trap recursive
504 static int umh_pipe_setup(struct subprocess_info
*info
, struct cred
*new)
506 struct file
*files
[2];
507 struct coredump_params
*cp
= (struct coredump_params
*)info
->data
;
508 int err
= create_pipe_files(files
, 0);
514 err
= replace_fd(0, files
[0], 0);
516 /* and disallow core files too */
517 current
->signal
->rlim
[RLIMIT_CORE
] = (struct rlimit
){1, 1};
522 void do_coredump(const kernel_siginfo_t
*siginfo
)
524 struct core_state core_state
;
526 struct mm_struct
*mm
= current
->mm
;
527 struct linux_binfmt
* binfmt
;
528 const struct cred
*old_cred
;
534 /* require nonrelative corefile path and be extra careful */
535 bool need_suid_safe
= false;
536 bool core_dumped
= false;
537 static atomic_t core_dump_count
= ATOMIC_INIT(0);
538 struct coredump_params cprm
= {
540 .limit
= rlimit(RLIMIT_CORE
),
542 * We must use the same mm->flags while dumping core to avoid
543 * inconsistency of bit flags, since this flag is not protected
546 .mm_flags
= mm
->flags
,
548 .cpu
= raw_smp_processor_id(),
551 audit_core_dumps(siginfo
->si_signo
);
554 if (!binfmt
|| !binfmt
->core_dump
)
556 if (!__get_dumpable(cprm
.mm_flags
))
559 cred
= prepare_creds();
563 * We cannot trust fsuid as being the "true" uid of the process
564 * nor do we know its entire history. We only know it was tainted
565 * so we dump it as root in mode 2, and only into a controlled
566 * environment (pipe handler or fully qualified path).
568 if (__get_dumpable(cprm
.mm_flags
) == SUID_DUMP_ROOT
) {
569 /* Setuid core dump mode */
570 cred
->fsuid
= GLOBAL_ROOT_UID
; /* Dump root private */
571 need_suid_safe
= true;
574 retval
= coredump_wait(siginfo
->si_signo
, &core_state
);
578 old_cred
= override_creds(cred
);
580 ispipe
= format_corename(&cn
, &cprm
, &argv
, &argc
);
586 struct subprocess_info
*sub_info
;
589 printk(KERN_WARNING
"format_corename failed\n");
590 printk(KERN_WARNING
"Aborting core\n");
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.
611 "Process %d(%s) has RLIMIT_CORE set to 1\n",
612 task_tgid_vnr(current
), current
->comm
);
613 printk(KERN_WARNING
"Aborting core\n");
616 cprm
.limit
= RLIM_INFINITY
;
618 dump_count
= atomic_inc_return(&core_dump_count
);
619 if (core_pipe_limit
&& (core_pipe_limit
< dump_count
)) {
620 printk(KERN_WARNING
"Pid %d(%s) over core_pipe_limit\n",
621 task_tgid_vnr(current
), current
->comm
);
622 printk(KERN_WARNING
"Skipping core dump\n");
626 helper_argv
= kmalloc_array(argc
+ 1, sizeof(*helper_argv
),
629 printk(KERN_WARNING
"%s failed to allocate memory\n",
633 for (argi
= 0; argi
< argc
; argi
++)
634 helper_argv
[argi
] = cn
.corename
+ argv
[argi
];
635 helper_argv
[argi
] = NULL
;
638 sub_info
= call_usermodehelper_setup(helper_argv
[0],
639 helper_argv
, NULL
, GFP_KERNEL
,
640 umh_pipe_setup
, NULL
, &cprm
);
642 retval
= call_usermodehelper_exec(sub_info
,
647 printk(KERN_INFO
"Core dump to |%s pipe failed\n",
652 struct mnt_idmap
*idmap
;
654 int open_flags
= O_CREAT
| O_WRONLY
| O_NOFOLLOW
|
655 O_LARGEFILE
| O_EXCL
;
657 if (cprm
.limit
< binfmt
->min_coredump
)
660 if (need_suid_safe
&& cn
.corename
[0] != '/') {
661 printk(KERN_WARNING
"Pid %d(%s) can only dump core "\
662 "to fully qualified path!\n",
663 task_tgid_vnr(current
), current
->comm
);
664 printk(KERN_WARNING
"Skipping core dump\n");
669 * Unlink the file if it exists unless this is a SUID
670 * binary - in that case, we're running around with root
671 * privs and don't want to unlink another user's coredump.
673 if (!need_suid_safe
) {
675 * If it doesn't exist, that's fine. If there's some
676 * other problem, we'll catch it at the filp_open().
678 do_unlinkat(AT_FDCWD
, getname_kernel(cn
.corename
));
682 * There is a race between unlinking and creating the
683 * file, but if that causes an EEXIST here, that's
684 * fine - another process raced with us while creating
685 * the corefile, and the other process won. To userspace,
686 * what matters is that at least one of the two processes
687 * writes its coredump successfully, not which one.
689 if (need_suid_safe
) {
691 * Using user namespaces, normal user tasks can change
692 * their current->fs->root to point to arbitrary
693 * directories. Since the intention of the "only dump
694 * with a fully qualified path" rule is to control where
695 * coredumps may be placed using root privileges,
696 * current->fs->root must not be used. Instead, use the
697 * root directory of init_task.
701 task_lock(&init_task
);
702 get_fs_root(init_task
.fs
, &root
);
703 task_unlock(&init_task
);
704 cprm
.file
= file_open_root(&root
, cn
.corename
,
708 cprm
.file
= filp_open(cn
.corename
, open_flags
, 0600);
710 if (IS_ERR(cprm
.file
))
713 inode
= file_inode(cprm
.file
);
714 if (inode
->i_nlink
> 1)
716 if (d_unhashed(cprm
.file
->f_path
.dentry
))
719 * AK: actually i see no reason to not allow this for named
720 * pipes etc, but keep the previous behaviour for now.
722 if (!S_ISREG(inode
->i_mode
))
725 * Don't dump core if the filesystem changed owner or mode
726 * of the file during file creation. This is an issue when
727 * a process dumps core while its cwd is e.g. on a vfat
730 idmap
= file_mnt_idmap(cprm
.file
);
731 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap
, inode
),
733 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file owner\n",
737 if ((inode
->i_mode
& 0677) != 0600) {
738 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file permissions\n",
742 if (!(cprm
.file
->f_mode
& FMODE_CAN_WRITE
))
744 if (do_truncate(idmap
, cprm
.file
->f_path
.dentry
,
749 /* get us an unshared descriptor table; almost always a no-op */
750 /* The cell spufs coredump code reads the file descriptor tables */
751 retval
= unshare_files();
754 if (!dump_interrupted()) {
756 * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
757 * have this set to NULL.
760 pr_info("Core dump to |%s disabled\n", cn
.corename
);
763 if (!dump_vma_snapshot(&cprm
))
766 file_start_write(cprm
.file
);
767 core_dumped
= binfmt
->core_dump(&cprm
);
769 * Ensures that file size is big enough to contain the current
770 * file postion. This prevents gdb from complaining about
771 * a truncated file if the last "write" to the file was
776 dump_emit(&cprm
, "", 1);
778 file_end_write(cprm
.file
);
779 free_vma_snapshot(&cprm
);
781 if (ispipe
&& core_pipe_limit
)
782 wait_for_dump_helpers(cprm
.file
);
785 filp_close(cprm
.file
, NULL
);
788 atomic_dec(&core_dump_count
);
792 coredump_finish(core_dumped
);
793 revert_creds(old_cred
);
801 * Core dumping helper functions. These are the only things you should
802 * do on a core-file: use only these functions to write out all the
805 static int __dump_emit(struct coredump_params
*cprm
, const void *addr
, int nr
)
807 struct file
*file
= cprm
->file
;
808 loff_t pos
= file
->f_pos
;
810 if (cprm
->written
+ nr
> cprm
->limit
)
814 if (dump_interrupted())
816 n
= __kernel_write(file
, addr
, nr
, &pos
);
826 static int __dump_skip(struct coredump_params
*cprm
, size_t nr
)
828 static char zeroes
[PAGE_SIZE
];
829 struct file
*file
= cprm
->file
;
830 if (file
->f_mode
& FMODE_LSEEK
) {
831 if (dump_interrupted() ||
832 vfs_llseek(file
, nr
, SEEK_CUR
) < 0)
837 while (nr
> PAGE_SIZE
) {
838 if (!__dump_emit(cprm
, zeroes
, PAGE_SIZE
))
842 return __dump_emit(cprm
, zeroes
, nr
);
846 int dump_emit(struct coredump_params
*cprm
, const void *addr
, int nr
)
849 if (!__dump_skip(cprm
, cprm
->to_skip
))
853 return __dump_emit(cprm
, addr
, nr
);
855 EXPORT_SYMBOL(dump_emit
);
857 void dump_skip_to(struct coredump_params
*cprm
, unsigned long pos
)
859 cprm
->to_skip
= pos
- cprm
->pos
;
861 EXPORT_SYMBOL(dump_skip_to
);
863 void dump_skip(struct coredump_params
*cprm
, size_t nr
)
867 EXPORT_SYMBOL(dump_skip
);
869 #ifdef CONFIG_ELF_CORE
870 static int dump_emit_page(struct coredump_params
*cprm
, struct page
*page
)
873 struct iov_iter iter
;
874 struct file
*file
= cprm
->file
;
882 if (!__dump_skip(cprm
, cprm
->to_skip
))
886 if (cprm
->written
+ PAGE_SIZE
> cprm
->limit
)
888 if (dump_interrupted())
891 bvec_set_page(&bvec
, page
, PAGE_SIZE
, 0);
892 iov_iter_bvec(&iter
, ITER_SOURCE
, &bvec
, 1, PAGE_SIZE
);
893 n
= __kernel_write_iter(cprm
->file
, &iter
, &pos
);
897 cprm
->written
+= PAGE_SIZE
;
898 cprm
->pos
+= PAGE_SIZE
;
904 * If we might get machine checks from kernel accesses during the
905 * core dump, let's get those errors early rather than during the
906 * IO. This is not performance-critical enough to warrant having
907 * all the machine check logic in the iovec paths.
909 #ifdef copy_mc_to_kernel
911 #define dump_page_alloc() alloc_page(GFP_KERNEL)
912 #define dump_page_free(x) __free_page(x)
913 static struct page
*dump_page_copy(struct page
*src
, struct page
*dst
)
915 void *buf
= kmap_local_page(src
);
916 size_t left
= copy_mc_to_kernel(page_address(dst
), buf
, PAGE_SIZE
);
918 return left
? NULL
: dst
;
923 /* We just want to return non-NULL; it's never used. */
924 #define dump_page_alloc() ERR_PTR(-EINVAL)
925 #define dump_page_free(x) ((void)(x))
926 static inline struct page
*dump_page_copy(struct page
*src
, struct page
*dst
)
932 int dump_user_range(struct coredump_params
*cprm
, unsigned long start
,
936 struct page
*dump_page
;
938 dump_page
= dump_page_alloc();
942 for (addr
= start
; addr
< start
+ len
; addr
+= PAGE_SIZE
) {
946 * To avoid having to allocate page tables for virtual address
947 * ranges that have never been used yet, and also to make it
948 * easy to generate sparse core files, use a helper that returns
949 * NULL when encountering an empty page table entry that would
950 * otherwise have been filled with the zero page.
952 page
= get_dump_page(addr
);
954 int stop
= !dump_emit_page(cprm
, dump_page_copy(page
, dump_page
));
957 dump_page_free(dump_page
);
961 dump_skip(cprm
, PAGE_SIZE
);
964 dump_page_free(dump_page
);
969 int dump_align(struct coredump_params
*cprm
, int align
)
971 unsigned mod
= (cprm
->pos
+ cprm
->to_skip
) & (align
- 1);
972 if (align
& (align
- 1))
975 cprm
->to_skip
+= align
- mod
;
978 EXPORT_SYMBOL(dump_align
);
982 void validate_coredump_safety(void)
984 if (suid_dumpable
== SUID_DUMP_ROOT
&&
985 core_pattern
[0] != '/' && core_pattern
[0] != '|') {
987 "Unsafe core_pattern used with fs.suid_dumpable=2.\n"
988 "Pipe handler or fully qualified core dump path required.\n"
989 "Set kernel.core_pattern before fs.suid_dumpable.\n"
994 static int proc_dostring_coredump(const struct ctl_table
*table
, int write
,
995 void *buffer
, size_t *lenp
, loff_t
*ppos
)
997 int error
= proc_dostring(table
, write
, buffer
, lenp
, ppos
);
1000 validate_coredump_safety();
1004 static const unsigned int core_file_note_size_min
= CORE_FILE_NOTE_SIZE_DEFAULT
;
1005 static const unsigned int core_file_note_size_max
= CORE_FILE_NOTE_SIZE_MAX
;
1007 static struct ctl_table coredump_sysctls
[] = {
1009 .procname
= "core_uses_pid",
1010 .data
= &core_uses_pid
,
1011 .maxlen
= sizeof(int),
1013 .proc_handler
= proc_dointvec
,
1016 .procname
= "core_pattern",
1017 .data
= core_pattern
,
1018 .maxlen
= CORENAME_MAX_SIZE
,
1020 .proc_handler
= proc_dostring_coredump
,
1023 .procname
= "core_pipe_limit",
1024 .data
= &core_pipe_limit
,
1025 .maxlen
= sizeof(unsigned int),
1027 .proc_handler
= proc_dointvec
,
1030 .procname
= "core_file_note_size_limit",
1031 .data
= &core_file_note_size_limit
,
1032 .maxlen
= sizeof(unsigned int),
1034 .proc_handler
= proc_douintvec_minmax
,
1035 .extra1
= (unsigned int *)&core_file_note_size_min
,
1036 .extra2
= (unsigned int *)&core_file_note_size_max
,
1040 static int __init
init_fs_coredump_sysctls(void)
1042 register_sysctl_init("kernel", coredump_sysctls
);
1045 fs_initcall(init_fs_coredump_sysctls
);
1046 #endif /* CONFIG_SYSCTL */
1049 * The purpose of always_dump_vma() is to make sure that special kernel mappings
1050 * that are useful for post-mortem analysis are included in every core dump.
1051 * In that way we ensure that the core dump is fully interpretable later
1052 * without matching up the same kernel and hardware config to see what PC values
1053 * meant. These special mappings include - vDSO, vsyscall, and other
1054 * architecture specific mappings
1056 static bool always_dump_vma(struct vm_area_struct
*vma
)
1058 /* Any vsyscall mappings? */
1059 if (vma
== get_gate_vma(vma
->vm_mm
))
1063 * Assume that all vmas with a .name op should always be dumped.
1064 * If this changes, a new vm_ops field can easily be added.
1066 if (vma
->vm_ops
&& vma
->vm_ops
->name
&& vma
->vm_ops
->name(vma
))
1070 * arch_vma_name() returns non-NULL for special architecture mappings,
1071 * such as vDSO sections.
1073 if (arch_vma_name(vma
))
1079 #define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1
1082 * Decide how much of @vma's contents should be included in a core dump.
1084 static unsigned long vma_dump_size(struct vm_area_struct
*vma
,
1085 unsigned long mm_flags
)
1087 #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1089 /* always dump the vdso and vsyscall sections */
1090 if (always_dump_vma(vma
))
1093 if (vma
->vm_flags
& VM_DONTDUMP
)
1096 /* support for DAX */
1097 if (vma_is_dax(vma
)) {
1098 if ((vma
->vm_flags
& VM_SHARED
) && FILTER(DAX_SHARED
))
1100 if (!(vma
->vm_flags
& VM_SHARED
) && FILTER(DAX_PRIVATE
))
1105 /* Hugetlb memory check */
1106 if (is_vm_hugetlb_page(vma
)) {
1107 if ((vma
->vm_flags
& VM_SHARED
) && FILTER(HUGETLB_SHARED
))
1109 if (!(vma
->vm_flags
& VM_SHARED
) && FILTER(HUGETLB_PRIVATE
))
1114 /* Do not dump I/O mapped devices or special mappings */
1115 if (vma
->vm_flags
& VM_IO
)
1118 /* By default, dump shared memory if mapped from an anonymous file. */
1119 if (vma
->vm_flags
& VM_SHARED
) {
1120 if (file_inode(vma
->vm_file
)->i_nlink
== 0 ?
1121 FILTER(ANON_SHARED
) : FILTER(MAPPED_SHARED
))
1126 /* Dump segments that have been written to. */
1127 if ((!IS_ENABLED(CONFIG_MMU
) || vma
->anon_vma
) && FILTER(ANON_PRIVATE
))
1129 if (vma
->vm_file
== NULL
)
1132 if (FILTER(MAPPED_PRIVATE
))
1136 * If this is the beginning of an executable file mapping,
1137 * dump the first page to aid in determining what was mapped here.
1139 if (FILTER(ELF_HEADERS
) &&
1140 vma
->vm_pgoff
== 0 && (vma
->vm_flags
& VM_READ
)) {
1141 if ((READ_ONCE(file_inode(vma
->vm_file
)->i_mode
) & 0111) != 0)
1145 * ELF libraries aren't always executable.
1146 * We'll want to check whether the mapping starts with the ELF
1147 * magic, but not now - we're holding the mmap lock,
1148 * so copy_from_user() doesn't work here.
1149 * Use a placeholder instead, and fix it up later in
1150 * dump_vma_snapshot().
1152 return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER
;
1160 return vma
->vm_end
- vma
->vm_start
;
1164 * Helper function for iterating across a vma list. It ensures that the caller
1165 * will visit `gate_vma' prior to terminating the search.
1167 static struct vm_area_struct
*coredump_next_vma(struct vma_iterator
*vmi
,
1168 struct vm_area_struct
*vma
,
1169 struct vm_area_struct
*gate_vma
)
1171 if (gate_vma
&& (vma
== gate_vma
))
1174 vma
= vma_next(vmi
);
1180 static void free_vma_snapshot(struct coredump_params
*cprm
)
1182 if (cprm
->vma_meta
) {
1184 for (i
= 0; i
< cprm
->vma_count
; i
++) {
1185 struct file
*file
= cprm
->vma_meta
[i
].file
;
1189 kvfree(cprm
->vma_meta
);
1190 cprm
->vma_meta
= NULL
;
1195 * Under the mmap_lock, take a snapshot of relevant information about the task's
1198 static bool dump_vma_snapshot(struct coredump_params
*cprm
)
1200 struct vm_area_struct
*gate_vma
, *vma
= NULL
;
1201 struct mm_struct
*mm
= current
->mm
;
1202 VMA_ITERATOR(vmi
, mm
, 0);
1206 * Once the stack expansion code is fixed to not change VMA bounds
1207 * under mmap_lock in read mode, this can be changed to take the
1208 * mmap_lock in read mode.
1210 if (mmap_write_lock_killable(mm
))
1213 cprm
->vma_data_size
= 0;
1214 gate_vma
= get_gate_vma(mm
);
1215 cprm
->vma_count
= mm
->map_count
+ (gate_vma
? 1 : 0);
1217 cprm
->vma_meta
= kvmalloc_array(cprm
->vma_count
, sizeof(*cprm
->vma_meta
), GFP_KERNEL
);
1218 if (!cprm
->vma_meta
) {
1219 mmap_write_unlock(mm
);
1223 while ((vma
= coredump_next_vma(&vmi
, vma
, gate_vma
)) != NULL
) {
1224 struct core_vma_metadata
*m
= cprm
->vma_meta
+ i
;
1226 m
->start
= vma
->vm_start
;
1227 m
->end
= vma
->vm_end
;
1228 m
->flags
= vma
->vm_flags
;
1229 m
->dump_size
= vma_dump_size(vma
, cprm
->mm_flags
);
1230 m
->pgoff
= vma
->vm_pgoff
;
1231 m
->file
= vma
->vm_file
;
1237 mmap_write_unlock(mm
);
1239 for (i
= 0; i
< cprm
->vma_count
; i
++) {
1240 struct core_vma_metadata
*m
= cprm
->vma_meta
+ i
;
1242 if (m
->dump_size
== DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER
) {
1243 char elfmag
[SELFMAG
];
1245 if (copy_from_user(elfmag
, (void __user
*)m
->start
, SELFMAG
) ||
1246 memcmp(elfmag
, ELFMAG
, SELFMAG
) != 0) {
1249 m
->dump_size
= PAGE_SIZE
;
1253 cprm
->vma_data_size
+= m
->dump_size
;