Linux 3.12.28
[linux/fpc-iii.git] / fs / coredump.c
blob88adbdd151936f24574570bcae80e4f1ff123735
1 #include <linux/slab.h>
2 #include <linux/file.h>
3 #include <linux/fdtable.h>
4 #include <linux/mm.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>
38 #include <asm/tlb.h>
39 #include <asm/exec.h>
41 #include <trace/events/task.h>
42 #include "internal.h"
43 #include "coredump.h"
45 #include <trace/events/sched.h>
47 int core_uses_pid;
48 unsigned int core_pipe_limit;
49 char core_pattern[CORENAME_MAX_SIZE] = "core";
50 static int core_name_size = CORENAME_MAX_SIZE;
52 struct core_name {
53 char *corename;
54 int used, 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);
63 if (!corename)
64 return -ENOMEM;
66 if (size > core_name_size) /* racy but harmless */
67 core_name_size = size;
69 cn->size = ksize(corename);
70 cn->corename = corename;
71 return 0;
74 static int cn_vprintf(struct core_name *cn, const char *fmt, va_list arg)
76 int free, need;
77 va_list arg_copy;
79 again:
80 free = cn->size - cn->used;
82 va_copy(arg_copy, arg);
83 need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
84 va_end(arg_copy);
86 if (need < free) {
87 cn->used += need;
88 return 0;
91 if (!expand_corename(cn, cn->size + need - free + 1))
92 goto again;
94 return -ENOMEM;
97 static int cn_printf(struct core_name *cn, const char *fmt, ...)
99 va_list arg;
100 int ret;
102 va_start(arg, fmt);
103 ret = cn_vprintf(cn, fmt, arg);
104 va_end(arg);
106 return ret;
109 static int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
111 int cur = cn->used;
112 va_list arg;
113 int ret;
115 va_start(arg, fmt);
116 ret = cn_vprintf(cn, fmt, arg);
117 va_end(arg);
119 for (; cur < cn->used; ++cur) {
120 if (cn->corename[cur] == '/')
121 cn->corename[cur] = '!';
123 return ret;
126 static int cn_print_exe_file(struct core_name *cn)
128 struct file *exe_file;
129 char *pathbuf, *path;
130 int ret;
132 exe_file = get_mm_exe_file(current->mm);
133 if (!exe_file)
134 return cn_esc_printf(cn, "%s (path unknown)", current->comm);
136 pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
137 if (!pathbuf) {
138 ret = -ENOMEM;
139 goto put_exe_file;
142 path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
143 if (IS_ERR(path)) {
144 ret = PTR_ERR(path);
145 goto free_buf;
148 ret = cn_esc_printf(cn, "%s", path);
150 free_buf:
151 kfree(pathbuf);
152 put_exe_file:
153 fput(exe_file);
154 return ret;
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;
167 int err = 0;
169 cn->used = 0;
170 cn->corename = NULL;
171 if (expand_corename(cn, core_name_size))
172 return -ENOMEM;
173 cn->corename[0] = '\0';
175 if (ispipe)
176 ++pat_ptr;
178 /* Repeat as long as we have more pattern to process and more output
179 space */
180 while (*pat_ptr) {
181 if (*pat_ptr != '%') {
182 err = cn_printf(cn, "%c", *pat_ptr++);
183 } else {
184 switch (*++pat_ptr) {
185 /* single % at the end, drop that */
186 case 0:
187 goto out;
188 /* Double percent, output one percent */
189 case '%':
190 err = cn_printf(cn, "%c", '%');
191 break;
192 /* pid */
193 case 'p':
194 pid_in_pattern = 1;
195 err = cn_printf(cn, "%d",
196 task_tgid_vnr(current));
197 break;
198 /* global pid */
199 case 'P':
200 err = cn_printf(cn, "%d",
201 task_tgid_nr(current));
202 break;
203 /* uid */
204 case 'u':
205 err = cn_printf(cn, "%d", cred->uid);
206 break;
207 /* gid */
208 case 'g':
209 err = cn_printf(cn, "%d", cred->gid);
210 break;
211 case 'd':
212 err = cn_printf(cn, "%d",
213 __get_dumpable(cprm->mm_flags));
214 break;
215 /* signal that caused the coredump */
216 case 's':
217 err = cn_printf(cn, "%ld", cprm->siginfo->si_signo);
218 break;
219 /* UNIX time of coredump */
220 case 't': {
221 struct timeval tv;
222 do_gettimeofday(&tv);
223 err = cn_printf(cn, "%lu", tv.tv_sec);
224 break;
226 /* hostname */
227 case 'h':
228 down_read(&uts_sem);
229 err = cn_esc_printf(cn, "%s",
230 utsname()->nodename);
231 up_read(&uts_sem);
232 break;
233 /* executable */
234 case 'e':
235 err = cn_esc_printf(cn, "%s", current->comm);
236 break;
237 case 'E':
238 err = cn_print_exe_file(cn);
239 break;
240 /* core limit size */
241 case 'c':
242 err = cn_printf(cn, "%lu",
243 rlimit(RLIMIT_CORE));
244 break;
245 default:
246 break;
248 ++pat_ptr;
251 if (err)
252 return err;
255 out:
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));
263 if (err)
264 return err;
266 return ispipe;
269 static int zap_process(struct task_struct *start, int exit_code)
271 struct task_struct *t;
272 int nr = 0;
274 start->signal->group_exit_code = exit_code;
275 start->signal->group_stop_count = 0;
277 t = start;
278 do {
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);
283 nr++;
285 } while_each_thread(start, t);
287 return nr;
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;
294 unsigned long flags;
295 int nr = -EAGAIN;
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))
308 return nr;
310 tsk->flags |= PF_DUMPCORE;
311 if (atomic_read(&mm->mm_users) == nr + 1)
312 goto done;
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:
318 * fork:
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.
325 * do_exit:
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
328 * its ->mm.
330 * de_thread:
331 * It does list_replace_rcu(&leader->tasks, &current->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
335 * it can't fail.
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
341 * next_thread().
343 rcu_read_lock();
344 for_each_process(g) {
345 if (g == tsk->group_leader)
346 continue;
347 if (g->flags & PF_KTHREAD)
348 continue;
349 p = g;
350 do {
351 if (p->mm) {
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);
358 break;
360 } while_each_thread(g, p);
362 rcu_read_unlock();
363 done:
364 atomic_set(&core_state->nr_threads, nr);
365 return 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);
379 if (!mm->core_state)
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);
395 ptr = ptr->next;
399 return core_waiters;
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(&current->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(&current->sighand->siglock);
414 next = mm->core_state->dumper.next;
415 while ((curr = next) != NULL) {
416 next = curr->next;
417 task = curr->task;
419 * see exit_mm(), curr->task must not see
420 * ->task == NULL before we read ->next.
422 smp_mb();
423 curr->task = NULL;
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
436 * TIF_SIGPENDING.
438 return signal_pending(current);
441 static void wait_for_dump_helpers(struct file *file)
443 struct pipe_inode_info *pipe = file->private_data;
445 pipe_lock(pipe);
446 pipe->readers++;
447 pipe->writers--;
448 wake_up_interruptible_sync(&pipe->wait);
449 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
450 pipe_unlock(pipe);
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);
458 pipe_lock(pipe);
459 pipe->readers--;
460 pipe->writers++;
461 pipe_unlock(pipe);
465 * umh_pipe_setup
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
473 * core dumps
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);
480 if (err)
481 return err;
483 cp->file = files[1];
485 err = replace_fd(0, files[0], 0);
486 fput(files[0]);
487 /* and disallow core files too */
488 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
490 return err;
493 void do_coredump(siginfo_t *siginfo)
495 struct core_state core_state;
496 struct core_name cn;
497 struct mm_struct *mm = current->mm;
498 struct linux_binfmt * binfmt;
499 const struct cred *old_cred;
500 struct cred *cred;
501 int retval = 0;
502 int flag = 0;
503 int ispipe;
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 = {
509 .siginfo = siginfo,
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
515 * by any locks.
517 .mm_flags = mm->flags,
520 audit_core_dumps(siginfo->si_signo);
522 binfmt = mm->binfmt;
523 if (!binfmt || !binfmt->core_dump)
524 goto fail;
525 if (!__get_dumpable(cprm.mm_flags))
526 goto fail;
528 cred = prepare_creds();
529 if (!cred)
530 goto fail;
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);
545 if (retval < 0)
546 goto fail_creds;
548 old_cred = override_creds(cred);
550 ispipe = format_corename(&cn, &cprm);
552 if (ispipe) {
553 int dump_count;
554 char **helper_argv;
555 struct subprocess_info *sub_info;
557 if (ispipe < 0) {
558 printk(KERN_WARNING "format_corename failed\n");
559 printk(KERN_WARNING "Aborting core\n");
560 goto fail_unlock;
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.
579 printk(KERN_WARNING
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");
583 goto fail_unlock;
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");
592 goto fail_dropcount;
595 helper_argv = argv_split(GFP_KERNEL, cn.corename, NULL);
596 if (!helper_argv) {
597 printk(KERN_WARNING "%s failed to allocate memory\n",
598 __func__);
599 goto fail_dropcount;
602 retval = -ENOMEM;
603 sub_info = call_usermodehelper_setup(helper_argv[0],
604 helper_argv, NULL, GFP_KERNEL,
605 umh_pipe_setup, NULL, &cprm);
606 if (sub_info)
607 retval = call_usermodehelper_exec(sub_info,
608 UMH_WAIT_EXEC);
610 argv_free(helper_argv);
611 if (retval) {
612 printk(KERN_INFO "Core dump to |%s pipe failed\n",
613 cn.corename);
614 goto close_fail;
616 } else {
617 struct inode *inode;
619 if (cprm.limit < binfmt->min_coredump)
620 goto fail_unlock;
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");
627 goto fail_unlock;
630 cprm.file = filp_open(cn.corename,
631 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
632 0600);
633 if (IS_ERR(cprm.file))
634 goto fail_unlock;
636 inode = file_inode(cprm.file);
637 if (inode->i_nlink > 1)
638 goto close_fail;
639 if (d_unhashed(cprm.file->f_path.dentry))
640 goto close_fail;
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))
646 goto close_fail;
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()))
652 goto close_fail;
653 if (!cprm.file->f_op || !cprm.file->f_op->write)
654 goto close_fail;
655 if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
656 goto close_fail;
659 /* get us an unshared descriptor table; almost always a no-op */
660 retval = unshare_files(&displaced);
661 if (retval)
662 goto close_fail;
663 if (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);
672 close_fail:
673 if (cprm.file)
674 filp_close(cprm.file, NULL);
675 fail_dropcount:
676 if (ispipe)
677 atomic_dec(&core_dump_count);
678 fail_unlock:
679 kfree(cn.corename);
680 coredump_finish(mm, core_dumped);
681 revert_creds(old_cred);
682 fail_creds:
683 put_cred(cred);
684 fail:
685 return;
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
691 * necessary info.
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)
703 int ret = 1;
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)
708 return 0;
709 } else {
710 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
712 if (!buf)
713 return 0;
714 while (off > 0) {
715 unsigned long n = off;
717 if (n > PAGE_SIZE)
718 n = PAGE_SIZE;
719 if (!dump_write(file, buf, n)) {
720 ret = 0;
721 break;
723 off -= n;
725 free_page((unsigned long)buf);
727 return ret;
729 EXPORT_SYMBOL(dump_seek);