1 #include <linux/slab.h>
2 #include <linux/file.h>
3 #include <linux/fdtable.h>
5 #include <linux/stat.h>
6 #include <linux/fcntl.h>
7 #include <linux/swap.h>
8 #include <linux/string.h>
9 #include <linux/init.h>
10 #include <linux/pagemap.h>
11 #include <linux/perf_event.h>
12 #include <linux/highmem.h>
13 #include <linux/spinlock.h>
14 #include <linux/key.h>
15 #include <linux/personality.h>
16 #include <linux/binfmts.h>
17 #include <linux/coredump.h>
18 #include <linux/utsname.h>
19 #include <linux/pid_namespace.h>
20 #include <linux/module.h>
21 #include <linux/namei.h>
22 #include <linux/mount.h>
23 #include <linux/security.h>
24 #include <linux/syscalls.h>
25 #include <linux/tsacct_kern.h>
26 #include <linux/cn_proc.h>
27 #include <linux/audit.h>
28 #include <linux/tracehook.h>
29 #include <linux/kmod.h>
30 #include <linux/fsnotify.h>
31 #include <linux/fs_struct.h>
32 #include <linux/pipe_fs_i.h>
33 #include <linux/oom.h>
34 #include <linux/compat.h>
36 #include <asm/uaccess.h>
37 #include <asm/mmu_context.h>
41 #include <trace/events/task.h>
45 #include <trace/events/sched.h>
48 unsigned int core_pipe_limit
;
49 char core_pattern
[CORENAME_MAX_SIZE
] = "core";
50 static int core_name_size
= CORENAME_MAX_SIZE
;
57 /* The maximal length of core_pattern is also specified in sysctl.c */
59 static int expand_corename(struct core_name
*cn
, int size
)
61 char *corename
= krealloc(cn
->corename
, size
, GFP_KERNEL
);
66 if (size
> core_name_size
) /* racy but harmless */
67 core_name_size
= size
;
69 cn
->size
= ksize(corename
);
70 cn
->corename
= corename
;
74 static int cn_vprintf(struct core_name
*cn
, const char *fmt
, va_list arg
)
80 free
= cn
->size
- cn
->used
;
82 va_copy(arg_copy
, arg
);
83 need
= vsnprintf(cn
->corename
+ cn
->used
, free
, fmt
, arg_copy
);
91 if (!expand_corename(cn
, cn
->size
+ need
- free
+ 1))
97 static int cn_printf(struct core_name
*cn
, const char *fmt
, ...)
103 ret
= cn_vprintf(cn
, fmt
, arg
);
109 static int cn_esc_printf(struct core_name
*cn
, const char *fmt
, ...)
116 ret
= cn_vprintf(cn
, fmt
, arg
);
119 for (; cur
< cn
->used
; ++cur
) {
120 if (cn
->corename
[cur
] == '/')
121 cn
->corename
[cur
] = '!';
126 static int cn_print_exe_file(struct core_name
*cn
)
128 struct file
*exe_file
;
129 char *pathbuf
, *path
;
132 exe_file
= get_mm_exe_file(current
->mm
);
134 return cn_esc_printf(cn
, "%s (path unknown)", current
->comm
);
136 pathbuf
= kmalloc(PATH_MAX
, GFP_TEMPORARY
);
142 path
= d_path(&exe_file
->f_path
, pathbuf
, PATH_MAX
);
148 ret
= cn_esc_printf(cn
, "%s", path
);
157 /* format_corename will inspect the pattern parameter, and output a
158 * name into corename, which must have space for at least
159 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
161 static int format_corename(struct core_name
*cn
, struct coredump_params
*cprm
)
163 const struct cred
*cred
= current_cred();
164 const char *pat_ptr
= core_pattern
;
165 int ispipe
= (*pat_ptr
== '|');
166 int pid_in_pattern
= 0;
171 if (expand_corename(cn
, core_name_size
))
173 cn
->corename
[0] = '\0';
178 /* Repeat as long as we have more pattern to process and more output
181 if (*pat_ptr
!= '%') {
182 err
= cn_printf(cn
, "%c", *pat_ptr
++);
184 switch (*++pat_ptr
) {
185 /* single % at the end, drop that */
188 /* Double percent, output one percent */
190 err
= cn_printf(cn
, "%c", '%');
195 err
= cn_printf(cn
, "%d",
196 task_tgid_vnr(current
));
200 err
= cn_printf(cn
, "%d",
201 task_tgid_nr(current
));
205 err
= cn_printf(cn
, "%d", cred
->uid
);
209 err
= cn_printf(cn
, "%d", cred
->gid
);
212 err
= cn_printf(cn
, "%d",
213 __get_dumpable(cprm
->mm_flags
));
215 /* signal that caused the coredump */
217 err
= cn_printf(cn
, "%ld", cprm
->siginfo
->si_signo
);
219 /* UNIX time of coredump */
222 do_gettimeofday(&tv
);
223 err
= cn_printf(cn
, "%lu", tv
.tv_sec
);
229 err
= cn_esc_printf(cn
, "%s",
230 utsname()->nodename
);
235 err
= cn_esc_printf(cn
, "%s", current
->comm
);
238 err
= cn_print_exe_file(cn
);
240 /* core limit size */
242 err
= cn_printf(cn
, "%lu",
243 rlimit(RLIMIT_CORE
));
256 /* Backward compatibility with core_uses_pid:
258 * If core_pattern does not include a %p (as is the default)
259 * and core_uses_pid is set, then .%pid will be appended to
260 * the filename. Do not do this for piped commands. */
261 if (!ispipe
&& !pid_in_pattern
&& core_uses_pid
) {
262 err
= cn_printf(cn
, ".%d", task_tgid_vnr(current
));
269 static int zap_process(struct task_struct
*start
, int exit_code
)
271 struct task_struct
*t
;
274 start
->signal
->group_exit_code
= exit_code
;
275 start
->signal
->group_stop_count
= 0;
279 task_clear_jobctl_pending(t
, JOBCTL_PENDING_MASK
);
280 if (t
!= current
&& t
->mm
) {
281 sigaddset(&t
->pending
.signal
, SIGKILL
);
282 signal_wake_up(t
, 1);
285 } while_each_thread(start
, t
);
290 static int zap_threads(struct task_struct
*tsk
, struct mm_struct
*mm
,
291 struct core_state
*core_state
, int exit_code
)
293 struct task_struct
*g
, *p
;
297 spin_lock_irq(&tsk
->sighand
->siglock
);
298 if (!signal_group_exit(tsk
->signal
)) {
299 mm
->core_state
= core_state
;
300 nr
= zap_process(tsk
, exit_code
);
301 tsk
->signal
->group_exit_task
= tsk
;
302 /* ignore all signals except SIGKILL, see prepare_signal() */
303 tsk
->signal
->flags
= SIGNAL_GROUP_COREDUMP
;
304 clear_tsk_thread_flag(tsk
, TIF_SIGPENDING
);
306 spin_unlock_irq(&tsk
->sighand
->siglock
);
307 if (unlikely(nr
< 0))
310 tsk
->flags
|= PF_DUMPCORE
;
311 if (atomic_read(&mm
->mm_users
) == nr
+ 1)
314 * We should find and kill all tasks which use this mm, and we should
315 * count them correctly into ->nr_threads. We don't take tasklist
316 * lock, but this is safe wrt:
319 * None of sub-threads can fork after zap_process(leader). All
320 * processes which were created before this point should be
321 * visible to zap_threads() because copy_process() adds the new
322 * process to the tail of init_task.tasks list, and lock/unlock
323 * of ->siglock provides a memory barrier.
326 * The caller holds mm->mmap_sem. This means that the task which
327 * uses this mm can't pass exit_mm(), so it can't exit or clear
331 * It does list_replace_rcu(&leader->tasks, ¤t->tasks),
332 * we must see either old or new leader, this does not matter.
333 * However, it can change p->sighand, so lock_task_sighand(p)
334 * must be used. Since p->mm != NULL and we hold ->mmap_sem
337 * Note also that "g" can be the old leader with ->mm == NULL
338 * and already unhashed and thus removed from ->thread_group.
339 * This is OK, __unhash_process()->list_del_rcu() does not
340 * clear the ->next pointer, we will find the new leader via
344 for_each_process(g
) {
345 if (g
== tsk
->group_leader
)
347 if (g
->flags
& PF_KTHREAD
)
352 if (unlikely(p
->mm
== mm
)) {
353 lock_task_sighand(p
, &flags
);
354 nr
+= zap_process(p
, exit_code
);
355 p
->signal
->flags
= SIGNAL_GROUP_EXIT
;
356 unlock_task_sighand(p
, &flags
);
360 } while_each_thread(g
, p
);
364 atomic_set(&core_state
->nr_threads
, nr
);
368 static int coredump_wait(int exit_code
, struct core_state
*core_state
)
370 struct task_struct
*tsk
= current
;
371 struct mm_struct
*mm
= tsk
->mm
;
372 int core_waiters
= -EBUSY
;
374 init_completion(&core_state
->startup
);
375 core_state
->dumper
.task
= tsk
;
376 core_state
->dumper
.next
= NULL
;
378 down_write(&mm
->mmap_sem
);
380 core_waiters
= zap_threads(tsk
, mm
, core_state
, exit_code
);
381 up_write(&mm
->mmap_sem
);
383 if (core_waiters
> 0) {
384 struct core_thread
*ptr
;
386 wait_for_completion(&core_state
->startup
);
388 * Wait for all the threads to become inactive, so that
389 * all the thread context (extended register state, like
390 * fpu etc) gets copied to the memory.
392 ptr
= core_state
->dumper
.next
;
393 while (ptr
!= NULL
) {
394 wait_task_inactive(ptr
->task
, 0);
402 static void coredump_finish(struct mm_struct
*mm
, bool core_dumped
)
404 struct core_thread
*curr
, *next
;
405 struct task_struct
*task
;
407 spin_lock_irq(¤t
->sighand
->siglock
);
408 if (core_dumped
&& !__fatal_signal_pending(current
))
409 current
->signal
->group_exit_code
|= 0x80;
410 current
->signal
->group_exit_task
= NULL
;
411 current
->signal
->flags
= SIGNAL_GROUP_EXIT
;
412 spin_unlock_irq(¤t
->sighand
->siglock
);
414 next
= mm
->core_state
->dumper
.next
;
415 while ((curr
= next
) != NULL
) {
419 * see exit_mm(), curr->task must not see
420 * ->task == NULL before we read ->next.
424 wake_up_process(task
);
427 mm
->core_state
= NULL
;
430 static bool dump_interrupted(void)
433 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
434 * can do try_to_freeze() and check __fatal_signal_pending(),
435 * but then we need to teach dump_write() to restart and clear
438 return signal_pending(current
);
441 static void wait_for_dump_helpers(struct file
*file
)
443 struct pipe_inode_info
*pipe
= file
->private_data
;
448 wake_up_interruptible_sync(&pipe
->wait
);
449 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
453 * We actually want wait_event_freezable() but then we need
454 * to clear TIF_SIGPENDING and improve dump_interrupted().
456 wait_event_interruptible(pipe
->wait
, pipe
->readers
== 1);
466 * helper function to customize the process used
467 * to collect the core in userspace. Specifically
468 * it sets up a pipe and installs it as fd 0 (stdin)
469 * for the process. Returns 0 on success, or
470 * PTR_ERR on failure.
471 * Note that it also sets the core limit to 1. This
472 * is a special value that we use to trap recursive
475 static int umh_pipe_setup(struct subprocess_info
*info
, struct cred
*new)
477 struct file
*files
[2];
478 struct coredump_params
*cp
= (struct coredump_params
*)info
->data
;
479 int err
= create_pipe_files(files
, 0);
485 err
= replace_fd(0, files
[0], 0);
487 /* and disallow core files too */
488 current
->signal
->rlim
[RLIMIT_CORE
] = (struct rlimit
){1, 1};
493 void do_coredump(siginfo_t
*siginfo
)
495 struct core_state core_state
;
497 struct mm_struct
*mm
= current
->mm
;
498 struct linux_binfmt
* binfmt
;
499 const struct cred
*old_cred
;
504 struct files_struct
*displaced
;
505 bool need_nonrelative
= false;
506 bool core_dumped
= false;
507 static atomic_t core_dump_count
= ATOMIC_INIT(0);
508 struct coredump_params cprm
= {
510 .regs
= signal_pt_regs(),
511 .limit
= rlimit(RLIMIT_CORE
),
513 * We must use the same mm->flags while dumping core to avoid
514 * inconsistency of bit flags, since this flag is not protected
517 .mm_flags
= mm
->flags
,
520 audit_core_dumps(siginfo
->si_signo
);
523 if (!binfmt
|| !binfmt
->core_dump
)
525 if (!__get_dumpable(cprm
.mm_flags
))
528 cred
= prepare_creds();
532 * We cannot trust fsuid as being the "true" uid of the process
533 * nor do we know its entire history. We only know it was tainted
534 * so we dump it as root in mode 2, and only into a controlled
535 * environment (pipe handler or fully qualified path).
537 if (__get_dumpable(cprm
.mm_flags
) == SUID_DUMP_ROOT
) {
538 /* Setuid core dump mode */
539 flag
= O_EXCL
; /* Stop rewrite attacks */
540 cred
->fsuid
= GLOBAL_ROOT_UID
; /* Dump root private */
541 need_nonrelative
= true;
544 retval
= coredump_wait(siginfo
->si_signo
, &core_state
);
548 old_cred
= override_creds(cred
);
550 ispipe
= format_corename(&cn
, &cprm
);
555 struct subprocess_info
*sub_info
;
558 printk(KERN_WARNING
"format_corename failed\n");
559 printk(KERN_WARNING
"Aborting core\n");
563 if (cprm
.limit
== 1) {
564 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
566 * Normally core limits are irrelevant to pipes, since
567 * we're not writing to the file system, but we use
568 * cprm.limit of 1 here as a speacial value, this is a
569 * consistent way to catch recursive crashes.
570 * We can still crash if the core_pattern binary sets
571 * RLIM_CORE = !1, but it runs as root, and can do
572 * lots of stupid things.
574 * Note that we use task_tgid_vnr here to grab the pid
575 * of the process group leader. That way we get the
576 * right pid if a thread in a multi-threaded
577 * core_pattern process dies.
580 "Process %d(%s) has RLIMIT_CORE set to 1\n",
581 task_tgid_vnr(current
), current
->comm
);
582 printk(KERN_WARNING
"Aborting core\n");
585 cprm
.limit
= RLIM_INFINITY
;
587 dump_count
= atomic_inc_return(&core_dump_count
);
588 if (core_pipe_limit
&& (core_pipe_limit
< dump_count
)) {
589 printk(KERN_WARNING
"Pid %d(%s) over core_pipe_limit\n",
590 task_tgid_vnr(current
), current
->comm
);
591 printk(KERN_WARNING
"Skipping core dump\n");
595 helper_argv
= argv_split(GFP_KERNEL
, cn
.corename
, NULL
);
597 printk(KERN_WARNING
"%s failed to allocate memory\n",
603 sub_info
= call_usermodehelper_setup(helper_argv
[0],
604 helper_argv
, NULL
, GFP_KERNEL
,
605 umh_pipe_setup
, NULL
, &cprm
);
607 retval
= call_usermodehelper_exec(sub_info
,
610 argv_free(helper_argv
);
612 printk(KERN_INFO
"Core dump to |%s pipe failed\n",
619 if (cprm
.limit
< binfmt
->min_coredump
)
622 if (need_nonrelative
&& cn
.corename
[0] != '/') {
623 printk(KERN_WARNING
"Pid %d(%s) can only dump core "\
624 "to fully qualified path!\n",
625 task_tgid_vnr(current
), current
->comm
);
626 printk(KERN_WARNING
"Skipping core dump\n");
630 cprm
.file
= filp_open(cn
.corename
,
631 O_CREAT
| 2 | O_NOFOLLOW
| O_LARGEFILE
| flag
,
633 if (IS_ERR(cprm
.file
))
636 inode
= file_inode(cprm
.file
);
637 if (inode
->i_nlink
> 1)
639 if (d_unhashed(cprm
.file
->f_path
.dentry
))
642 * AK: actually i see no reason to not allow this for named
643 * pipes etc, but keep the previous behaviour for now.
645 if (!S_ISREG(inode
->i_mode
))
648 * Dont allow local users get cute and trick others to coredump
649 * into their pre-created files.
651 if (!uid_eq(inode
->i_uid
, current_fsuid()))
653 if (!cprm
.file
->f_op
|| !cprm
.file
->f_op
->write
)
655 if (do_truncate(cprm
.file
->f_path
.dentry
, 0, 0, cprm
.file
))
659 /* get us an unshared descriptor table; almost always a no-op */
660 retval
= unshare_files(&displaced
);
664 put_files_struct(displaced
);
665 if (!dump_interrupted()) {
666 file_start_write(cprm
.file
);
667 core_dumped
= binfmt
->core_dump(&cprm
);
668 file_end_write(cprm
.file
);
670 if (ispipe
&& core_pipe_limit
)
671 wait_for_dump_helpers(cprm
.file
);
674 filp_close(cprm
.file
, NULL
);
677 atomic_dec(&core_dump_count
);
680 coredump_finish(mm
, core_dumped
);
681 revert_creds(old_cred
);
689 * Core dumping helper functions. These are the only things you should
690 * do on a core-file: use only these functions to write out all the
693 int dump_write(struct file
*file
, const void *addr
, int nr
)
695 return !dump_interrupted() &&
696 access_ok(VERIFY_READ
, addr
, nr
) &&
697 file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
) == nr
;
699 EXPORT_SYMBOL(dump_write
);
701 int dump_seek(struct file
*file
, loff_t off
)
705 if (file
->f_op
->llseek
&& file
->f_op
->llseek
!= no_llseek
) {
706 if (dump_interrupted() ||
707 file
->f_op
->llseek(file
, off
, SEEK_CUR
) < 0)
710 char *buf
= (char *)get_zeroed_page(GFP_KERNEL
);
715 unsigned long n
= off
;
719 if (!dump_write(file
, buf
, n
)) {
725 free_page((unsigned long)buf
);
729 EXPORT_SYMBOL(dump_seek
);