1 #include <linux/slab.h>
2 #include <linux/file.h>
3 #include <linux/fdtable.h>
4 #include <linux/freezer.h>
6 #include <linux/stat.h>
7 #include <linux/fcntl.h>
8 #include <linux/swap.h>
9 #include <linux/string.h>
10 #include <linux/init.h>
11 #include <linux/pagemap.h>
12 #include <linux/perf_event.h>
13 #include <linux/highmem.h>
14 #include <linux/spinlock.h>
15 #include <linux/key.h>
16 #include <linux/personality.h>
17 #include <linux/binfmts.h>
18 #include <linux/coredump.h>
19 #include <linux/utsname.h>
20 #include <linux/pid_namespace.h>
21 #include <linux/module.h>
22 #include <linux/namei.h>
23 #include <linux/mount.h>
24 #include <linux/security.h>
25 #include <linux/syscalls.h>
26 #include <linux/tsacct_kern.h>
27 #include <linux/cn_proc.h>
28 #include <linux/audit.h>
29 #include <linux/tracehook.h>
30 #include <linux/kmod.h>
31 #include <linux/fsnotify.h>
32 #include <linux/fs_struct.h>
33 #include <linux/pipe_fs_i.h>
34 #include <linux/oom.h>
35 #include <linux/compat.h>
37 #include <asm/uaccess.h>
38 #include <asm/mmu_context.h>
42 #include <trace/events/task.h>
46 #include <trace/events/sched.h>
49 char core_pattern
[CORENAME_MAX_SIZE
] = "core";
50 unsigned int core_pipe_limit
;
56 static atomic_t call_count
= ATOMIC_INIT(1);
58 /* The maximal length of core_pattern is also specified in sysctl.c */
60 static int expand_corename(struct core_name
*cn
)
62 char *old_corename
= cn
->corename
;
64 cn
->size
= CORENAME_MAX_SIZE
* atomic_inc_return(&call_count
);
65 cn
->corename
= krealloc(old_corename
, cn
->size
, GFP_KERNEL
);
75 static int cn_printf(struct core_name
*cn
, const char *fmt
, ...)
83 need
= vsnprintf(NULL
, 0, fmt
, arg
);
86 if (likely(need
< cn
->size
- cn
->used
- 1))
89 ret
= expand_corename(cn
);
94 cur
= cn
->corename
+ cn
->used
;
96 vsnprintf(cur
, need
+ 1, fmt
, arg
);
105 static void cn_escape(char *str
)
112 static int cn_print_exe_file(struct core_name
*cn
)
114 struct file
*exe_file
;
115 char *pathbuf
, *path
;
118 exe_file
= get_mm_exe_file(current
->mm
);
120 char *commstart
= cn
->corename
+ cn
->used
;
121 ret
= cn_printf(cn
, "%s (path unknown)", current
->comm
);
122 cn_escape(commstart
);
126 pathbuf
= kmalloc(PATH_MAX
, GFP_TEMPORARY
);
132 path
= d_path(&exe_file
->f_path
, pathbuf
, PATH_MAX
);
140 ret
= cn_printf(cn
, "%s", path
);
149 /* format_corename will inspect the pattern parameter, and output a
150 * name into corename, which must have space for at least
151 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
153 static int format_corename(struct core_name
*cn
, struct coredump_params
*cprm
)
155 const struct cred
*cred
= current_cred();
156 const char *pat_ptr
= core_pattern
;
157 int ispipe
= (*pat_ptr
== '|');
158 int pid_in_pattern
= 0;
161 cn
->size
= CORENAME_MAX_SIZE
* atomic_read(&call_count
);
162 cn
->corename
= kmalloc(cn
->size
, GFP_KERNEL
);
168 /* Repeat as long as we have more pattern to process and more output
171 if (*pat_ptr
!= '%') {
174 err
= cn_printf(cn
, "%c", *pat_ptr
++);
176 switch (*++pat_ptr
) {
177 /* single % at the end, drop that */
180 /* Double percent, output one percent */
182 err
= cn_printf(cn
, "%c", '%');
187 err
= cn_printf(cn
, "%d",
188 task_tgid_vnr(current
));
192 err
= cn_printf(cn
, "%d", cred
->uid
);
196 err
= cn_printf(cn
, "%d", cred
->gid
);
199 err
= cn_printf(cn
, "%d",
200 __get_dumpable(cprm
->mm_flags
));
202 /* signal that caused the coredump */
204 err
= cn_printf(cn
, "%ld", cprm
->siginfo
->si_signo
);
206 /* UNIX time of coredump */
209 do_gettimeofday(&tv
);
210 err
= cn_printf(cn
, "%lu", tv
.tv_sec
);
215 char *namestart
= cn
->corename
+ cn
->used
;
217 err
= cn_printf(cn
, "%s",
218 utsname()->nodename
);
220 cn_escape(namestart
);
225 char *commstart
= cn
->corename
+ cn
->used
;
226 err
= cn_printf(cn
, "%s", current
->comm
);
227 cn_escape(commstart
);
231 err
= cn_print_exe_file(cn
);
233 /* core limit size */
235 err
= cn_printf(cn
, "%lu",
236 rlimit(RLIMIT_CORE
));
248 /* Backward compatibility with core_uses_pid:
250 * If core_pattern does not include a %p (as is the default)
251 * and core_uses_pid is set, then .%pid will be appended to
252 * the filename. Do not do this for piped commands. */
253 if (!ispipe
&& !pid_in_pattern
&& core_uses_pid
) {
254 err
= cn_printf(cn
, ".%d", task_tgid_vnr(current
));
262 static int zap_process(struct task_struct
*start
, int exit_code
)
264 struct task_struct
*t
;
267 start
->signal
->group_exit_code
= exit_code
;
268 start
->signal
->group_stop_count
= 0;
272 task_clear_jobctl_pending(t
, JOBCTL_PENDING_MASK
);
273 if (t
!= current
&& t
->mm
) {
274 sigaddset(&t
->pending
.signal
, SIGKILL
);
275 signal_wake_up(t
, 1);
278 } while_each_thread(start
, t
);
283 static int zap_threads(struct task_struct
*tsk
, struct mm_struct
*mm
,
284 struct core_state
*core_state
, int exit_code
)
286 struct task_struct
*g
, *p
;
290 spin_lock_irq(&tsk
->sighand
->siglock
);
291 if (!signal_group_exit(tsk
->signal
)) {
292 mm
->core_state
= core_state
;
293 nr
= zap_process(tsk
, exit_code
);
294 tsk
->signal
->group_exit_task
= tsk
;
295 /* ignore all signals except SIGKILL, see prepare_signal() */
296 tsk
->signal
->flags
= SIGNAL_GROUP_COREDUMP
;
297 clear_tsk_thread_flag(tsk
, TIF_SIGPENDING
);
299 spin_unlock_irq(&tsk
->sighand
->siglock
);
300 if (unlikely(nr
< 0))
303 tsk
->flags
|= PF_DUMPCORE
;
304 if (atomic_read(&mm
->mm_users
) == nr
+ 1)
307 * We should find and kill all tasks which use this mm, and we should
308 * count them correctly into ->nr_threads. We don't take tasklist
309 * lock, but this is safe wrt:
312 * None of sub-threads can fork after zap_process(leader). All
313 * processes which were created before this point should be
314 * visible to zap_threads() because copy_process() adds the new
315 * process to the tail of init_task.tasks list, and lock/unlock
316 * of ->siglock provides a memory barrier.
319 * The caller holds mm->mmap_sem. This means that the task which
320 * uses this mm can't pass exit_mm(), so it can't exit or clear
324 * It does list_replace_rcu(&leader->tasks, ¤t->tasks),
325 * we must see either old or new leader, this does not matter.
326 * However, it can change p->sighand, so lock_task_sighand(p)
327 * must be used. Since p->mm != NULL and we hold ->mmap_sem
330 * Note also that "g" can be the old leader with ->mm == NULL
331 * and already unhashed and thus removed from ->thread_group.
332 * This is OK, __unhash_process()->list_del_rcu() does not
333 * clear the ->next pointer, we will find the new leader via
337 for_each_process(g
) {
338 if (g
== tsk
->group_leader
)
340 if (g
->flags
& PF_KTHREAD
)
345 if (unlikely(p
->mm
== mm
)) {
346 lock_task_sighand(p
, &flags
);
347 nr
+= zap_process(p
, exit_code
);
348 p
->signal
->flags
= SIGNAL_GROUP_EXIT
;
349 unlock_task_sighand(p
, &flags
);
353 } while_each_thread(g
, p
);
357 atomic_set(&core_state
->nr_threads
, nr
);
361 static int coredump_wait(int exit_code
, struct core_state
*core_state
)
363 struct task_struct
*tsk
= current
;
364 struct mm_struct
*mm
= tsk
->mm
;
365 int core_waiters
= -EBUSY
;
367 init_completion(&core_state
->startup
);
368 core_state
->dumper
.task
= tsk
;
369 core_state
->dumper
.next
= NULL
;
371 down_write(&mm
->mmap_sem
);
373 core_waiters
= zap_threads(tsk
, mm
, core_state
, exit_code
);
374 up_write(&mm
->mmap_sem
);
376 if (core_waiters
> 0) {
377 struct core_thread
*ptr
;
379 freezer_do_not_count();
380 wait_for_completion(&core_state
->startup
);
383 * Wait for all the threads to become inactive, so that
384 * all the thread context (extended register state, like
385 * fpu etc) gets copied to the memory.
387 ptr
= core_state
->dumper
.next
;
388 while (ptr
!= NULL
) {
389 wait_task_inactive(ptr
->task
, 0);
397 static void coredump_finish(struct mm_struct
*mm
, bool core_dumped
)
399 struct core_thread
*curr
, *next
;
400 struct task_struct
*task
;
402 spin_lock_irq(¤t
->sighand
->siglock
);
403 if (core_dumped
&& !__fatal_signal_pending(current
))
404 current
->signal
->group_exit_code
|= 0x80;
405 current
->signal
->group_exit_task
= NULL
;
406 current
->signal
->flags
= SIGNAL_GROUP_EXIT
;
407 spin_unlock_irq(¤t
->sighand
->siglock
);
409 next
= mm
->core_state
->dumper
.next
;
410 while ((curr
= next
) != NULL
) {
414 * see exit_mm(), curr->task must not see
415 * ->task == NULL before we read ->next.
419 wake_up_process(task
);
422 mm
->core_state
= NULL
;
425 static bool dump_interrupted(void)
428 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
429 * can do try_to_freeze() and check __fatal_signal_pending(),
430 * but then we need to teach dump_write() to restart and clear
433 return signal_pending(current
);
436 static void wait_for_dump_helpers(struct file
*file
)
438 struct pipe_inode_info
*pipe
= file
->private_data
;
443 wake_up_interruptible_sync(&pipe
->wait
);
444 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
448 * We actually want wait_event_freezable() but then we need
449 * to clear TIF_SIGPENDING and improve dump_interrupted().
451 wait_event_interruptible(pipe
->wait
, pipe
->readers
== 1);
461 * helper function to customize the process used
462 * to collect the core in userspace. Specifically
463 * it sets up a pipe and installs it as fd 0 (stdin)
464 * for the process. Returns 0 on success, or
465 * PTR_ERR on failure.
466 * Note that it also sets the core limit to 1. This
467 * is a special value that we use to trap recursive
470 static int umh_pipe_setup(struct subprocess_info
*info
, struct cred
*new)
472 struct file
*files
[2];
473 struct coredump_params
*cp
= (struct coredump_params
*)info
->data
;
474 int err
= create_pipe_files(files
, 0);
480 err
= replace_fd(0, files
[0], 0);
482 /* and disallow core files too */
483 current
->signal
->rlim
[RLIMIT_CORE
] = (struct rlimit
){1, 1};
488 void do_coredump(siginfo_t
*siginfo
)
490 struct core_state core_state
;
492 struct mm_struct
*mm
= current
->mm
;
493 struct linux_binfmt
* binfmt
;
494 const struct cred
*old_cred
;
498 struct files_struct
*displaced
;
499 /* require nonrelative corefile path and be extra careful */
500 bool need_suid_safe
= false;
501 bool core_dumped
= false;
502 static atomic_t core_dump_count
= ATOMIC_INIT(0);
503 struct coredump_params cprm
= {
505 .regs
= signal_pt_regs(),
506 .limit
= rlimit(RLIMIT_CORE
),
508 * We must use the same mm->flags while dumping core to avoid
509 * inconsistency of bit flags, since this flag is not protected
512 .mm_flags
= mm
->flags
,
515 audit_core_dumps(siginfo
->si_signo
);
518 if (!binfmt
|| !binfmt
->core_dump
) {
519 printk(KERN_WARNING
"Skip process %d(%s) core dump(!binfmt?%s)\n",
520 task_tgid_vnr(current
), current
->comm
, (!binfmt
) ? "yes":"no");
523 if (!__get_dumpable(cprm
.mm_flags
)) {
524 printk(KERN_WARNING
"Skip process %d(%s) core dump(mm_flags:%x)\n",
525 task_tgid_vnr(current
), current
->comm
, (unsigned int)cprm
.mm_flags
);
529 cred
= prepare_creds();
531 printk(KERN_WARNING
"Skip process %d(%s) core dump(prepare_creds failed)\n",
532 task_tgid_vnr(current
), current
->comm
);
536 * We cannot trust fsuid as being the "true" uid of the process
537 * nor do we know its entire history. We only know it was tainted
538 * so we dump it as root in mode 2, and only into a controlled
539 * environment (pipe handler or fully qualified path).
541 if (__get_dumpable(cprm
.mm_flags
) == SUID_DUMP_ROOT
) {
542 /* Setuid core dump mode */
543 cred
->fsuid
= GLOBAL_ROOT_UID
; /* Dump root private */
544 need_suid_safe
= true;
547 retval
= coredump_wait(siginfo
->si_signo
, &core_state
);
551 old_cred
= override_creds(cred
);
553 ispipe
= format_corename(&cn
, &cprm
);
558 struct subprocess_info
*sub_info
;
561 printk(KERN_WARNING
"format_corename failed\n");
562 printk(KERN_WARNING
"Aborting core\n");
566 if (cprm
.limit
== 1) {
567 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
569 * Normally core limits are irrelevant to pipes, since
570 * we're not writing to the file system, but we use
571 * cprm.limit of 1 here as a speacial value, this is a
572 * consistent way to catch recursive crashes.
573 * We can still crash if the core_pattern binary sets
574 * RLIM_CORE = !1, but it runs as root, and can do
575 * lots of stupid things.
577 * Note that we use task_tgid_vnr here to grab the pid
578 * of the process group leader. That way we get the
579 * right pid if a thread in a multi-threaded
580 * core_pattern process dies.
583 "Process %d(%s) has RLIMIT_CORE set to 1\n",
584 task_tgid_vnr(current
), current
->comm
);
585 printk(KERN_WARNING
"Aborting core\n");
588 cprm
.limit
= RLIM_INFINITY
;
590 dump_count
= atomic_inc_return(&core_dump_count
);
591 if (core_pipe_limit
&& (core_pipe_limit
< dump_count
)) {
592 printk(KERN_WARNING
"Pid %d(%s) over core_pipe_limit\n",
593 task_tgid_vnr(current
), current
->comm
);
594 printk(KERN_WARNING
"Skipping core dump\n");
598 helper_argv
= argv_split(GFP_KERNEL
, cn
.corename
+1, NULL
);
600 printk(KERN_WARNING
"%s failed to allocate memory\n",
606 sub_info
= call_usermodehelper_setup(helper_argv
[0],
607 helper_argv
, NULL
, GFP_KERNEL
,
608 umh_pipe_setup
, NULL
, &cprm
);
610 retval
= call_usermodehelper_exec(sub_info
,
613 argv_free(helper_argv
);
615 printk(KERN_INFO
"Core dump to %s pipe failed\n",
622 if (cprm
.limit
< binfmt
->min_coredump
)
625 if (need_suid_safe
&& cn
.corename
[0] != '/') {
626 printk(KERN_WARNING
"Pid %d(%s) can only dump core "\
627 "to fully qualified path!\n",
628 task_tgid_vnr(current
), current
->comm
);
629 printk(KERN_WARNING
"Skipping core dump\n");
634 * Unlink the file if it exists unless this is a SUID
635 * binary - in that case, we're running around with root
636 * privs and don't want to unlink another user's coredump.
638 if (!need_suid_safe
) {
644 * If it doesn't exist, that's fine. If there's some
645 * other problem, we'll catch it at the filp_open().
647 (void) sys_unlink((const char __user
*)cn
.corename
);
652 * There is a race between unlinking and creating the
653 * file, but if that causes an EEXIST here, that's
654 * fine - another process raced with us while creating
655 * the corefile, and the other process won. To userspace,
656 * what matters is that at least one of the two processes
657 * writes its coredump successfully, not which one.
659 cprm
.file
= filp_open(cn
.corename
,
660 O_CREAT
| 2 | O_NOFOLLOW
|
661 O_LARGEFILE
| O_EXCL
,
663 if (IS_ERR(cprm
.file
))
666 inode
= file_inode(cprm
.file
);
667 if (inode
->i_nlink
> 1)
669 if (d_unhashed(cprm
.file
->f_path
.dentry
))
672 * AK: actually i see no reason to not allow this for named
673 * pipes etc, but keep the previous behaviour for now.
675 if (!S_ISREG(inode
->i_mode
))
678 * Dont allow local users get cute and trick others to coredump
679 * into their pre-created files.
681 if (!uid_eq(inode
->i_uid
, current_fsuid()))
683 if (!cprm
.file
->f_op
|| !cprm
.file
->f_op
->write
)
685 if (do_truncate(cprm
.file
->f_path
.dentry
, 0, 0, cprm
.file
))
689 /* get us an unshared descriptor table; almost always a no-op */
690 retval
= unshare_files(&displaced
);
694 put_files_struct(displaced
);
695 if (!dump_interrupted()) {
696 file_start_write(cprm
.file
);
697 printk(KERN_WARNING
"before %d core dump\n", current
->pid
);
698 core_dumped
= binfmt
->core_dump(&cprm
);
699 file_end_write(cprm
.file
);
702 printk(KERN_WARNING
"before %d core dump interrupted error\n", current
->pid
);
703 if (ispipe
&& core_pipe_limit
)
704 wait_for_dump_helpers(cprm
.file
);
707 filp_close(cprm
.file
, NULL
);
710 atomic_dec(&core_dump_count
);
714 coredump_finish(mm
, core_dumped
);
715 revert_creds(old_cred
);
723 * Core dumping helper functions. These are the only things you should
724 * do on a core-file: use only these functions to write out all the
727 int dump_write(struct file
*file
, const void *addr
, int nr
)
729 if (!dump_interrupted()) {
730 if (access_ok(VERIFY_READ
, addr
, nr
)) {
731 int pipe_ret
= file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
);
732 if (pipe_ret
== nr
) {
735 if (pipe_ret
== -ERESTARTSYS
) {
738 printk(KERN_WARNING
"coredump(%d): pipe dump write error nr:%d, ret:%d\n", current
->pid
, nr
, pipe_ret
);
742 printk(KERN_WARNING
"coredump(%d): access verify error\n", current
->pid
);
746 printk(KERN_WARNING
"coredump(%d): interrupted error\n", current
->pid
);
750 EXPORT_SYMBOL(dump_write
);
752 int dump_seek(struct file
*file
, loff_t off
)
756 if (file
->f_op
->llseek
&& file
->f_op
->llseek
!= no_llseek
) {
757 if (dump_interrupted() ||
758 file
->f_op
->llseek(file
, off
, SEEK_CUR
) < 0)
761 char *buf
= (char *)get_zeroed_page(GFP_KERNEL
);
766 unsigned long n
= off
;
770 if (!dump_write(file
, buf
, n
)) {
776 free_page((unsigned long)buf
);
780 EXPORT_SYMBOL(dump_seek
);