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/utsname.h>
18 #include <linux/pid_namespace.h>
19 #include <linux/module.h>
20 #include <linux/namei.h>
21 #include <linux/mount.h>
22 #include <linux/security.h>
23 #include <linux/syscalls.h>
24 #include <linux/tsacct_kern.h>
25 #include <linux/cn_proc.h>
26 #include <linux/audit.h>
27 #include <linux/tracehook.h>
28 #include <linux/kmod.h>
29 #include <linux/fsnotify.h>
30 #include <linux/fs_struct.h>
31 #include <linux/pipe_fs_i.h>
32 #include <linux/oom.h>
33 #include <linux/compat.h>
35 #include <asm/uaccess.h>
36 #include <asm/mmu_context.h>
40 #include <trace/events/task.h>
43 #include <trace/events/sched.h>
46 char core_pattern[CORENAME_MAX_SIZE] = "core";
47 unsigned int core_pipe_limit;
53 static atomic_t call_count = ATOMIC_INIT(1);
55 /* The maximal length of core_pattern is also specified in sysctl.c */
57 static int expand_corename(struct core_name *cn)
59 char *old_corename = cn->corename;
61 cn->size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count);
62 cn->corename = krealloc(old_corename, cn->size, GFP_KERNEL);
72 static int cn_printf(struct core_name *cn, const char *fmt, ...)
80 need = vsnprintf(NULL, 0, fmt, arg);
83 if (likely(need < cn->size - cn->used - 1))
86 ret = expand_corename(cn);
91 cur = cn->corename + cn->used;
93 vsnprintf(cur, need + 1, fmt, arg);
102 static void cn_escape(char *str)
109 static int cn_print_exe_file(struct core_name *cn)
111 struct file *exe_file;
112 char *pathbuf, *path;
115 exe_file = get_mm_exe_file(current->mm);
117 char *commstart = cn->corename + cn->used;
118 ret = cn_printf(cn, "%s (path unknown)", current->comm);
119 cn_escape(commstart);
123 pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
129 path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
137 ret = cn_printf(cn, "%s", path);
146 /* format_corename will inspect the pattern parameter, and output a
147 * name into corename, which must have space for at least
148 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
150 static int format_corename(struct core_name *cn, long signr)
152 const struct cred *cred = current_cred();
153 const char *pat_ptr = core_pattern;
154 int ispipe = (*pat_ptr == '|');
155 int pid_in_pattern = 0;
158 cn->size = CORENAME_MAX_SIZE * atomic_read(&call_count);
159 cn->corename = kmalloc(cn->size, GFP_KERNEL);
165 /* Repeat as long as we have more pattern to process and more output
168 if (*pat_ptr != '%') {
171 err = cn_printf(cn, "%c", *pat_ptr++);
173 switch (*++pat_ptr) {
174 /* single % at the end, drop that */
177 /* Double percent, output one percent */
179 err = cn_printf(cn, "%c", '%');
184 err = cn_printf(cn, "%d",
185 task_tgid_vnr(current));
189 err = cn_printf(cn, "%d", cred->uid);
193 err = cn_printf(cn, "%d", cred->gid);
195 /* signal that caused the coredump */
197 err = cn_printf(cn, "%ld", signr);
199 /* UNIX time of coredump */
202 do_gettimeofday(&tv);
203 err = cn_printf(cn, "%lu", tv.tv_sec);
208 char *namestart = cn->corename + cn->used;
210 err = cn_printf(cn, "%s",
211 utsname()->nodename);
213 cn_escape(namestart);
218 char *commstart = cn->corename + cn->used;
219 err = cn_printf(cn, "%s", current->comm);
220 cn_escape(commstart);
224 err = cn_print_exe_file(cn);
226 /* core limit size */
228 err = cn_printf(cn, "%lu",
229 rlimit(RLIMIT_CORE));
241 /* Backward compatibility with core_uses_pid:
243 * If core_pattern does not include a %p (as is the default)
244 * and core_uses_pid is set, then .%pid will be appended to
245 * the filename. Do not do this for piped commands. */
246 if (!ispipe && !pid_in_pattern && core_uses_pid) {
247 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
255 static int zap_process(struct task_struct *start, int exit_code)
257 struct task_struct *t;
260 start->signal->flags = SIGNAL_GROUP_EXIT;
261 start->signal->group_exit_code = exit_code;
262 start->signal->group_stop_count = 0;
266 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
267 if (t != current && t->mm) {
268 sigaddset(&t->pending.signal, SIGKILL);
269 signal_wake_up(t, 1);
272 } while_each_thread(start, t);
277 static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
278 struct core_state *core_state, int exit_code)
280 struct task_struct *g, *p;
284 spin_lock_irq(&tsk->sighand->siglock);
285 if (!signal_group_exit(tsk->signal)) {
286 mm->core_state = core_state;
287 nr = zap_process(tsk, exit_code);
289 spin_unlock_irq(&tsk->sighand->siglock);
290 if (unlikely(nr < 0))
293 if (atomic_read(&mm->mm_users) == nr + 1)
296 * We should find and kill all tasks which use this mm, and we should
297 * count them correctly into ->nr_threads. We don't take tasklist
298 * lock, but this is safe wrt:
301 * None of sub-threads can fork after zap_process(leader). All
302 * processes which were created before this point should be
303 * visible to zap_threads() because copy_process() adds the new
304 * process to the tail of init_task.tasks list, and lock/unlock
305 * of ->siglock provides a memory barrier.
308 * The caller holds mm->mmap_sem. This means that the task which
309 * uses this mm can't pass exit_mm(), so it can't exit or clear
313 * It does list_replace_rcu(&leader->tasks, ¤t->tasks),
314 * we must see either old or new leader, this does not matter.
315 * However, it can change p->sighand, so lock_task_sighand(p)
316 * must be used. Since p->mm != NULL and we hold ->mmap_sem
319 * Note also that "g" can be the old leader with ->mm == NULL
320 * and already unhashed and thus removed from ->thread_group.
321 * This is OK, __unhash_process()->list_del_rcu() does not
322 * clear the ->next pointer, we will find the new leader via
326 for_each_process(g) {
327 if (g == tsk->group_leader)
329 if (g->flags & PF_KTHREAD)
334 if (unlikely(p->mm == mm)) {
335 lock_task_sighand(p, &flags);
336 nr += zap_process(p, exit_code);
337 unlock_task_sighand(p, &flags);
341 } while_each_thread(g, p);
345 atomic_set(&core_state->nr_threads, nr);
349 static int coredump_wait(int exit_code, struct core_state *core_state)
351 struct task_struct *tsk = current;
352 struct mm_struct *mm = tsk->mm;
353 int core_waiters = -EBUSY;
355 init_completion(&core_state->startup);
356 core_state->dumper.task = tsk;
357 core_state->dumper.next = NULL;
359 down_write(&mm->mmap_sem);
361 core_waiters = zap_threads(tsk, mm, core_state, exit_code);
362 up_write(&mm->mmap_sem);
364 if (core_waiters > 0) {
365 struct core_thread *ptr;
367 wait_for_completion(&core_state->startup);
369 * Wait for all the threads to become inactive, so that
370 * all the thread context (extended register state, like
371 * fpu etc) gets copied to the memory.
373 ptr = core_state->dumper.next;
374 while (ptr != NULL) {
375 wait_task_inactive(ptr->task, 0);
383 static void coredump_finish(struct mm_struct *mm)
385 struct core_thread *curr, *next;
386 struct task_struct *task;
388 next = mm->core_state->dumper.next;
389 while ((curr = next) != NULL) {
393 * see exit_mm(), curr->task must not see
394 * ->task == NULL before we read ->next.
398 wake_up_process(task);
401 mm->core_state = NULL;
404 static void wait_for_dump_helpers(struct file *file)
406 struct pipe_inode_info *pipe;
408 pipe = file->f_path.dentry->d_inode->i_pipe;
414 while ((pipe->readers > 1) && (!signal_pending(current))) {
415 wake_up_interruptible_sync(&pipe->wait);
416 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
428 * helper function to customize the process used
429 * to collect the core in userspace. Specifically
430 * it sets up a pipe and installs it as fd 0 (stdin)
431 * for the process. Returns 0 on success, or
432 * PTR_ERR on failure.
433 * Note that it also sets the core limit to 1. This
434 * is a special value that we use to trap recursive
437 static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
439 struct file *files[2];
440 struct coredump_params *cp = (struct coredump_params *)info->data;
441 int err = create_pipe_files(files, 0);
447 replace_fd(0, files[0], 0);
448 /* and disallow core files too */
449 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
454 void do_coredump(long signr, int exit_code, struct pt_regs *regs)
456 struct core_state core_state;
458 struct mm_struct *mm = current->mm;
459 struct linux_binfmt * binfmt;
460 const struct cred *old_cred;
465 struct files_struct *displaced;
466 bool need_nonrelative = false;
467 static atomic_t core_dump_count = ATOMIC_INIT(0);
468 struct coredump_params cprm = {
471 .limit = rlimit(RLIMIT_CORE),
473 * We must use the same mm->flags while dumping core to avoid
474 * inconsistency of bit flags, since this flag is not protected
477 .mm_flags = mm->flags,
480 audit_core_dumps(signr);
483 if (!binfmt || !binfmt->core_dump)
485 if (!__get_dumpable(cprm.mm_flags))
488 cred = prepare_creds();
492 * We cannot trust fsuid as being the "true" uid of the process
493 * nor do we know its entire history. We only know it was tainted
494 * so we dump it as root in mode 2, and only into a controlled
495 * environment (pipe handler or fully qualified path).
497 if (__get_dumpable(cprm.mm_flags) == SUID_DUMPABLE_SAFE) {
498 /* Setuid core dump mode */
499 flag = O_EXCL; /* Stop rewrite attacks */
500 cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
501 need_nonrelative = true;
504 retval = coredump_wait(exit_code, &core_state);
508 old_cred = override_creds(cred);
511 * Clear any false indication of pending signals that might
512 * be seen by the filesystem code called to write the core file.
514 clear_thread_flag(TIF_SIGPENDING);
516 ispipe = format_corename(&cn, signr);
523 printk(KERN_WARNING "format_corename failed\n");
524 printk(KERN_WARNING "Aborting core\n");
528 if (cprm.limit == 1) {
529 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
531 * Normally core limits are irrelevant to pipes, since
532 * we're not writing to the file system, but we use
533 * cprm.limit of 1 here as a speacial value, this is a
534 * consistent way to catch recursive crashes.
535 * We can still crash if the core_pattern binary sets
536 * RLIM_CORE = !1, but it runs as root, and can do
537 * lots of stupid things.
539 * Note that we use task_tgid_vnr here to grab the pid
540 * of the process group leader. That way we get the
541 * right pid if a thread in a multi-threaded
542 * core_pattern process dies.
545 "Process %d(%s) has RLIMIT_CORE set to 1\n",
546 task_tgid_vnr(current), current->comm);
547 printk(KERN_WARNING "Aborting core\n");
550 cprm.limit = RLIM_INFINITY;
552 dump_count = atomic_inc_return(&core_dump_count);
553 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
554 printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
555 task_tgid_vnr(current), current->comm);
556 printk(KERN_WARNING "Skipping core dump\n");
560 helper_argv = argv_split(GFP_KERNEL, cn.corename+1, NULL);
562 printk(KERN_WARNING "%s failed to allocate memory\n",
567 retval = call_usermodehelper_fns(helper_argv[0], helper_argv,
568 NULL, UMH_WAIT_EXEC, umh_pipe_setup,
570 argv_free(helper_argv);
572 printk(KERN_INFO "Core dump to %s pipe failed\n",
579 if (cprm.limit < binfmt->min_coredump)
582 if (need_nonrelative && cn.corename[0] != '/') {
583 printk(KERN_WARNING "Pid %d(%s) can only dump core "\
584 "to fully qualified path!\n",
585 task_tgid_vnr(current), current->comm);
586 printk(KERN_WARNING "Skipping core dump\n");
590 cprm.file = filp_open(cn.corename,
591 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
593 if (IS_ERR(cprm.file))
596 inode = cprm.file->f_path.dentry->d_inode;
597 if (inode->i_nlink > 1)
599 if (d_unhashed(cprm.file->f_path.dentry))
602 * AK: actually i see no reason to not allow this for named
603 * pipes etc, but keep the previous behaviour for now.
605 if (!S_ISREG(inode->i_mode))
608 * Dont allow local users get cute and trick others to coredump
609 * into their pre-created files.
611 if (!uid_eq(inode->i_uid, current_fsuid()))
613 if (!cprm.file->f_op || !cprm.file->f_op->write)
615 if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
619 /* get us an unshared descriptor table; almost always a no-op */
620 retval = unshare_files(&displaced);
624 put_files_struct(displaced);
625 retval = binfmt->core_dump(&cprm);
627 current->signal->group_exit_code |= 0x80;
629 if (ispipe && core_pipe_limit)
630 wait_for_dump_helpers(cprm.file);
633 filp_close(cprm.file, NULL);
636 atomic_dec(&core_dump_count);
641 revert_creds(old_cred);
649 * Core dumping helper functions. These are the only things you should
650 * do on a core-file: use only these functions to write out all the
653 int dump_write(struct file *file, const void *addr, int nr)
655 return access_ok(VERIFY_READ, addr, nr) && file->f_op->write(file, addr, nr, &file->f_pos) == nr;
657 EXPORT_SYMBOL(dump_write);
659 int dump_seek(struct file *file, loff_t off)
663 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
664 if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
667 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
672 unsigned long n = off;
676 if (!dump_write(file, buf, n)) {
682 free_page((unsigned long)buf);
686 EXPORT_SYMBOL(dump_seek);