blob: 7227ad89ecee9d5ac7dcd47b65fdae68d5a67cbb [file] [log] [blame]
Alex Kelly10c28d92012-09-26 21:52:08 -04001#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>
Alex Kelly179899f2012-10-04 17:15:24 -070017#include <linux/coredump.h>
Alex Kelly10c28d92012-09-26 21:52:08 -040018#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>
Arnd Bergmann0fea20f2015-11-25 16:22:25 +010035#include <linux/timekeeping.h>
Alex Kelly10c28d92012-09-26 21:52:08 -040036
37#include <asm/uaccess.h>
38#include <asm/mmu_context.h>
39#include <asm/tlb.h>
40#include <asm/exec.h>
41
42#include <trace/events/task.h>
43#include "internal.h"
Alex Kelly10c28d92012-09-26 21:52:08 -040044
45#include <trace/events/sched.h>
46
47int core_uses_pid;
Alex Kelly10c28d92012-09-26 21:52:08 -040048unsigned int core_pipe_limit;
Oleg Nesterov3ceadcf2013-07-03 15:08:22 -070049char core_pattern[CORENAME_MAX_SIZE] = "core";
50static int core_name_size = CORENAME_MAX_SIZE;
Alex Kelly10c28d92012-09-26 21:52:08 -040051
52struct core_name {
53 char *corename;
54 int used, size;
55};
Alex Kelly10c28d92012-09-26 21:52:08 -040056
57/* The maximal length of core_pattern is also specified in sysctl.c */
58
Oleg Nesterov3ceadcf2013-07-03 15:08:22 -070059static int expand_corename(struct core_name *cn, int size)
Alex Kelly10c28d92012-09-26 21:52:08 -040060{
Oleg Nesterove7fd1542013-07-03 15:08:16 -070061 char *corename = krealloc(cn->corename, size, GFP_KERNEL);
Alex Kelly10c28d92012-09-26 21:52:08 -040062
Oleg Nesterove7fd1542013-07-03 15:08:16 -070063 if (!corename)
Alex Kelly10c28d92012-09-26 21:52:08 -040064 return -ENOMEM;
Alex Kelly10c28d92012-09-26 21:52:08 -040065
Oleg Nesterov3ceadcf2013-07-03 15:08:22 -070066 if (size > core_name_size) /* racy but harmless */
67 core_name_size = size;
68
69 cn->size = ksize(corename);
Oleg Nesterove7fd1542013-07-03 15:08:16 -070070 cn->corename = corename;
Alex Kelly10c28d92012-09-26 21:52:08 -040071 return 0;
72}
73
Oleg Nesterovbc03c692013-07-03 15:08:17 -070074static int cn_vprintf(struct core_name *cn, const char *fmt, va_list arg)
Alex Kelly10c28d92012-09-26 21:52:08 -040075{
Oleg Nesterov5fe9d8c2013-07-03 15:08:19 -070076 int free, need;
Eric Dumazet404ca802014-04-19 10:15:07 -070077 va_list arg_copy;
Alex Kelly10c28d92012-09-26 21:52:08 -040078
Oleg Nesterov5fe9d8c2013-07-03 15:08:19 -070079again:
80 free = cn->size - cn->used;
Eric Dumazet404ca802014-04-19 10:15:07 -070081
82 va_copy(arg_copy, arg);
83 need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
84 va_end(arg_copy);
85
Oleg Nesterov5fe9d8c2013-07-03 15:08:19 -070086 if (need < free) {
87 cn->used += need;
88 return 0;
89 }
Alex Kelly10c28d92012-09-26 21:52:08 -040090
Oleg Nesterov3ceadcf2013-07-03 15:08:22 -070091 if (!expand_corename(cn, cn->size + need - free + 1))
Oleg Nesterov5fe9d8c2013-07-03 15:08:19 -070092 goto again;
Alex Kelly10c28d92012-09-26 21:52:08 -040093
Oleg Nesterov5fe9d8c2013-07-03 15:08:19 -070094 return -ENOMEM;
Alex Kelly10c28d92012-09-26 21:52:08 -040095}
96
Oleg Nesterovbc03c692013-07-03 15:08:17 -070097static int cn_printf(struct core_name *cn, const char *fmt, ...)
98{
99 va_list arg;
100 int ret;
101
102 va_start(arg, fmt);
103 ret = cn_vprintf(cn, fmt, arg);
104 va_end(arg);
105
106 return ret;
107}
108
Oleg Nesterov923bed032013-07-03 15:08:20 -0700109static int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
Alex Kelly10c28d92012-09-26 21:52:08 -0400110{
Oleg Nesterov923bed032013-07-03 15:08:20 -0700111 int cur = cn->used;
112 va_list arg;
113 int ret;
114
115 va_start(arg, fmt);
116 ret = cn_vprintf(cn, fmt, arg);
117 va_end(arg);
118
119 for (; cur < cn->used; ++cur) {
120 if (cn->corename[cur] == '/')
121 cn->corename[cur] = '!';
122 }
123 return ret;
Alex Kelly10c28d92012-09-26 21:52:08 -0400124}
125
126static int cn_print_exe_file(struct core_name *cn)
127{
128 struct file *exe_file;
129 char *pathbuf, *path;
130 int ret;
131
132 exe_file = get_mm_exe_file(current->mm);
Oleg Nesterov923bed032013-07-03 15:08:20 -0700133 if (!exe_file)
134 return cn_esc_printf(cn, "%s (path unknown)", current->comm);
Alex Kelly10c28d92012-09-26 21:52:08 -0400135
136 pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
137 if (!pathbuf) {
138 ret = -ENOMEM;
139 goto put_exe_file;
140 }
141
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;
146 }
147
Oleg Nesterov923bed032013-07-03 15:08:20 -0700148 ret = cn_esc_printf(cn, "%s", path);
Alex Kelly10c28d92012-09-26 21:52:08 -0400149
150free_buf:
151 kfree(pathbuf);
152put_exe_file:
153 fput(exe_file);
154 return ret;
155}
156
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.
160 */
Oleg Nesterov12a2b4b2012-10-04 17:15:25 -0700161static int format_corename(struct core_name *cn, struct coredump_params *cprm)
Alex Kelly10c28d92012-09-26 21:52:08 -0400162{
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;
168
Oleg Nesterove7fd1542013-07-03 15:08:16 -0700169 cn->used = 0;
Oleg Nesterov3ceadcf2013-07-03 15:08:22 -0700170 cn->corename = NULL;
171 if (expand_corename(cn, core_name_size))
Alex Kelly10c28d92012-09-26 21:52:08 -0400172 return -ENOMEM;
Oleg Nesterov888ffc52013-07-03 15:08:23 -0700173 cn->corename[0] = '\0';
174
175 if (ispipe)
176 ++pat_ptr;
Alex Kelly10c28d92012-09-26 21:52:08 -0400177
178 /* Repeat as long as we have more pattern to process and more output
179 space */
180 while (*pat_ptr) {
181 if (*pat_ptr != '%') {
Alex Kelly10c28d92012-09-26 21:52:08 -0400182 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;
Stéphane Graber65aafb12013-09-11 14:24:32 -0700198 /* global pid */
199 case 'P':
200 err = cn_printf(cn, "%d",
201 task_tgid_nr(current));
202 break;
Oleg Nesterovb03023e2014-10-13 15:53:35 -0700203 case 'i':
204 err = cn_printf(cn, "%d",
205 task_pid_vnr(current));
206 break;
207 case 'I':
208 err = cn_printf(cn, "%d",
209 task_pid_nr(current));
210 break;
Alex Kelly10c28d92012-09-26 21:52:08 -0400211 /* uid */
212 case 'u':
213 err = cn_printf(cn, "%d", cred->uid);
214 break;
215 /* gid */
216 case 'g':
217 err = cn_printf(cn, "%d", cred->gid);
218 break;
Oleg Nesterov12a2b4b2012-10-04 17:15:25 -0700219 case 'd':
220 err = cn_printf(cn, "%d",
221 __get_dumpable(cprm->mm_flags));
222 break;
Alex Kelly10c28d92012-09-26 21:52:08 -0400223 /* signal that caused the coredump */
224 case 's':
Denys Vlasenko5ab1c302012-10-04 17:15:29 -0700225 err = cn_printf(cn, "%ld", cprm->siginfo->si_signo);
Alex Kelly10c28d92012-09-26 21:52:08 -0400226 break;
227 /* UNIX time of coredump */
228 case 't': {
Arnd Bergmann0fea20f2015-11-25 16:22:25 +0100229 time64_t time;
230
231 time = ktime_get_real_seconds();
232 err = cn_printf(cn, "%lld", time);
Alex Kelly10c28d92012-09-26 21:52:08 -0400233 break;
234 }
235 /* hostname */
Oleg Nesterov923bed032013-07-03 15:08:20 -0700236 case 'h':
Alex Kelly10c28d92012-09-26 21:52:08 -0400237 down_read(&uts_sem);
Oleg Nesterov923bed032013-07-03 15:08:20 -0700238 err = cn_esc_printf(cn, "%s",
Alex Kelly10c28d92012-09-26 21:52:08 -0400239 utsname()->nodename);
240 up_read(&uts_sem);
Alex Kelly10c28d92012-09-26 21:52:08 -0400241 break;
Alex Kelly10c28d92012-09-26 21:52:08 -0400242 /* executable */
Oleg Nesterov923bed032013-07-03 15:08:20 -0700243 case 'e':
244 err = cn_esc_printf(cn, "%s", current->comm);
Alex Kelly10c28d92012-09-26 21:52:08 -0400245 break;
Alex Kelly10c28d92012-09-26 21:52:08 -0400246 case 'E':
247 err = cn_print_exe_file(cn);
248 break;
249 /* core limit size */
250 case 'c':
251 err = cn_printf(cn, "%lu",
252 rlimit(RLIMIT_CORE));
253 break;
254 default:
255 break;
256 }
257 ++pat_ptr;
258 }
259
260 if (err)
261 return err;
262 }
263
Oleg Nesterov888ffc52013-07-03 15:08:23 -0700264out:
Alex Kelly10c28d92012-09-26 21:52:08 -0400265 /* Backward compatibility with core_uses_pid:
266 *
267 * If core_pattern does not include a %p (as is the default)
268 * and core_uses_pid is set, then .%pid will be appended to
269 * the filename. Do not do this for piped commands. */
270 if (!ispipe && !pid_in_pattern && core_uses_pid) {
271 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
272 if (err)
273 return err;
274 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400275 return ispipe;
276}
277
278static int zap_process(struct task_struct *start, int exit_code)
279{
280 struct task_struct *t;
281 int nr = 0;
282
Alex Kelly10c28d92012-09-26 21:52:08 -0400283 start->signal->group_exit_code = exit_code;
284 start->signal->group_stop_count = 0;
285
286 t = start;
287 do {
288 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
289 if (t != current && t->mm) {
290 sigaddset(&t->pending.signal, SIGKILL);
291 signal_wake_up(t, 1);
292 nr++;
293 }
294 } while_each_thread(start, t);
295
296 return nr;
297}
298
Oleg Nesterov403bad72013-04-30 15:28:10 -0700299static int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
300 struct core_state *core_state, int exit_code)
Alex Kelly10c28d92012-09-26 21:52:08 -0400301{
302 struct task_struct *g, *p;
303 unsigned long flags;
304 int nr = -EAGAIN;
305
306 spin_lock_irq(&tsk->sighand->siglock);
307 if (!signal_group_exit(tsk->signal)) {
308 mm->core_state = core_state;
309 nr = zap_process(tsk, exit_code);
Oleg Nesterov6cd8f0a2013-04-30 15:28:12 -0700310 tsk->signal->group_exit_task = tsk;
Oleg Nesterov403bad72013-04-30 15:28:10 -0700311 /* ignore all signals except SIGKILL, see prepare_signal() */
Oleg Nesterov6cd8f0a2013-04-30 15:28:12 -0700312 tsk->signal->flags = SIGNAL_GROUP_COREDUMP;
Oleg Nesterov403bad72013-04-30 15:28:10 -0700313 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
Alex Kelly10c28d92012-09-26 21:52:08 -0400314 }
315 spin_unlock_irq(&tsk->sighand->siglock);
316 if (unlikely(nr < 0))
317 return nr;
318
Silesh C Vaed8adb2014-07-23 13:59:59 -0700319 tsk->flags |= PF_DUMPCORE;
Alex Kelly10c28d92012-09-26 21:52:08 -0400320 if (atomic_read(&mm->mm_users) == nr + 1)
321 goto done;
322 /*
323 * We should find and kill all tasks which use this mm, and we should
324 * count them correctly into ->nr_threads. We don't take tasklist
325 * lock, but this is safe wrt:
326 *
327 * fork:
328 * None of sub-threads can fork after zap_process(leader). All
329 * processes which were created before this point should be
330 * visible to zap_threads() because copy_process() adds the new
331 * process to the tail of init_task.tasks list, and lock/unlock
332 * of ->siglock provides a memory barrier.
333 *
334 * do_exit:
335 * The caller holds mm->mmap_sem. This means that the task which
336 * uses this mm can't pass exit_mm(), so it can't exit or clear
337 * its ->mm.
338 *
339 * de_thread:
340 * It does list_replace_rcu(&leader->tasks, &current->tasks),
341 * we must see either old or new leader, this does not matter.
342 * However, it can change p->sighand, so lock_task_sighand(p)
343 * must be used. Since p->mm != NULL and we hold ->mmap_sem
344 * it can't fail.
345 *
346 * Note also that "g" can be the old leader with ->mm == NULL
347 * and already unhashed and thus removed from ->thread_group.
348 * This is OK, __unhash_process()->list_del_rcu() does not
349 * clear the ->next pointer, we will find the new leader via
350 * next_thread().
351 */
352 rcu_read_lock();
353 for_each_process(g) {
354 if (g == tsk->group_leader)
355 continue;
356 if (g->flags & PF_KTHREAD)
357 continue;
358 p = g;
359 do {
360 if (p->mm) {
361 if (unlikely(p->mm == mm)) {
362 lock_task_sighand(p, &flags);
363 nr += zap_process(p, exit_code);
Oleg Nesterov6cd8f0a2013-04-30 15:28:12 -0700364 p->signal->flags = SIGNAL_GROUP_EXIT;
Alex Kelly10c28d92012-09-26 21:52:08 -0400365 unlock_task_sighand(p, &flags);
366 }
367 break;
368 }
369 } while_each_thread(g, p);
370 }
371 rcu_read_unlock();
372done:
373 atomic_set(&core_state->nr_threads, nr);
374 return nr;
375}
376
377static int coredump_wait(int exit_code, struct core_state *core_state)
378{
379 struct task_struct *tsk = current;
380 struct mm_struct *mm = tsk->mm;
381 int core_waiters = -EBUSY;
382
383 init_completion(&core_state->startup);
384 core_state->dumper.task = tsk;
385 core_state->dumper.next = NULL;
386
387 down_write(&mm->mmap_sem);
388 if (!mm->core_state)
389 core_waiters = zap_threads(tsk, mm, core_state, exit_code);
390 up_write(&mm->mmap_sem);
391
392 if (core_waiters > 0) {
393 struct core_thread *ptr;
394
395 wait_for_completion(&core_state->startup);
396 /*
397 * Wait for all the threads to become inactive, so that
398 * all the thread context (extended register state, like
399 * fpu etc) gets copied to the memory.
400 */
401 ptr = core_state->dumper.next;
402 while (ptr != NULL) {
403 wait_task_inactive(ptr->task, 0);
404 ptr = ptr->next;
405 }
406 }
407
408 return core_waiters;
409}
410
Oleg Nesterovacdedd92013-04-30 15:28:13 -0700411static void coredump_finish(struct mm_struct *mm, bool core_dumped)
Alex Kelly10c28d92012-09-26 21:52:08 -0400412{
413 struct core_thread *curr, *next;
414 struct task_struct *task;
415
Oleg Nesterov6cd8f0a2013-04-30 15:28:12 -0700416 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterovacdedd92013-04-30 15:28:13 -0700417 if (core_dumped && !__fatal_signal_pending(current))
418 current->signal->group_exit_code |= 0x80;
Oleg Nesterov6cd8f0a2013-04-30 15:28:12 -0700419 current->signal->group_exit_task = NULL;
420 current->signal->flags = SIGNAL_GROUP_EXIT;
421 spin_unlock_irq(&current->sighand->siglock);
422
Alex Kelly10c28d92012-09-26 21:52:08 -0400423 next = mm->core_state->dumper.next;
424 while ((curr = next) != NULL) {
425 next = curr->next;
426 task = curr->task;
427 /*
428 * see exit_mm(), curr->task must not see
429 * ->task == NULL before we read ->next.
430 */
431 smp_mb();
432 curr->task = NULL;
433 wake_up_process(task);
434 }
435
436 mm->core_state = NULL;
437}
438
Oleg Nesterov528f8272013-04-30 15:28:15 -0700439static bool dump_interrupted(void)
440{
441 /*
442 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
443 * can do try_to_freeze() and check __fatal_signal_pending(),
444 * but then we need to teach dump_write() to restart and clear
445 * TIF_SIGPENDING.
446 */
447 return signal_pending(current);
448}
449
Alex Kelly10c28d92012-09-26 21:52:08 -0400450static void wait_for_dump_helpers(struct file *file)
451{
Al Virode32ec42013-03-21 11:16:56 -0400452 struct pipe_inode_info *pipe = file->private_data;
Alex Kelly10c28d92012-09-26 21:52:08 -0400453
454 pipe_lock(pipe);
455 pipe->readers++;
456 pipe->writers--;
Oleg Nesterovdc7ee2a2013-04-30 15:28:17 -0700457 wake_up_interruptible_sync(&pipe->wait);
458 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
459 pipe_unlock(pipe);
Alex Kelly10c28d92012-09-26 21:52:08 -0400460
Oleg Nesterovdc7ee2a2013-04-30 15:28:17 -0700461 /*
462 * We actually want wait_event_freezable() but then we need
463 * to clear TIF_SIGPENDING and improve dump_interrupted().
464 */
465 wait_event_interruptible(pipe->wait, pipe->readers == 1);
Alex Kelly10c28d92012-09-26 21:52:08 -0400466
Oleg Nesterovdc7ee2a2013-04-30 15:28:17 -0700467 pipe_lock(pipe);
Alex Kelly10c28d92012-09-26 21:52:08 -0400468 pipe->readers--;
469 pipe->writers++;
470 pipe_unlock(pipe);
Alex Kelly10c28d92012-09-26 21:52:08 -0400471}
472
473/*
474 * umh_pipe_setup
475 * helper function to customize the process used
476 * to collect the core in userspace. Specifically
477 * it sets up a pipe and installs it as fd 0 (stdin)
478 * for the process. Returns 0 on success, or
479 * PTR_ERR on failure.
480 * Note that it also sets the core limit to 1. This
481 * is a special value that we use to trap recursive
482 * core dumps
483 */
484static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
485{
486 struct file *files[2];
487 struct coredump_params *cp = (struct coredump_params *)info->data;
488 int err = create_pipe_files(files, 0);
489 if (err)
490 return err;
491
492 cp->file = files[1];
493
Al Viro45525b22012-10-16 13:30:07 -0400494 err = replace_fd(0, files[0], 0);
495 fput(files[0]);
Alex Kelly10c28d92012-09-26 21:52:08 -0400496 /* and disallow core files too */
497 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
498
Al Viro45525b22012-10-16 13:30:07 -0400499 return err;
Alex Kelly10c28d92012-09-26 21:52:08 -0400500}
501
Al Viroec579412013-10-13 17:57:29 -0400502void do_coredump(const siginfo_t *siginfo)
Alex Kelly10c28d92012-09-26 21:52:08 -0400503{
504 struct core_state core_state;
505 struct core_name cn;
506 struct mm_struct *mm = current->mm;
507 struct linux_binfmt * binfmt;
508 const struct cred *old_cred;
509 struct cred *cred;
510 int retval = 0;
Alex Kelly10c28d92012-09-26 21:52:08 -0400511 int ispipe;
512 struct files_struct *displaced;
Jann Horn244d3c12015-09-09 15:38:28 -0700513 /* require nonrelative corefile path and be extra careful */
514 bool need_suid_safe = false;
Oleg Nesterovacdedd92013-04-30 15:28:13 -0700515 bool core_dumped = false;
Alex Kelly10c28d92012-09-26 21:52:08 -0400516 static atomic_t core_dump_count = ATOMIC_INIT(0);
517 struct coredump_params cprm = {
Denys Vlasenko5ab1c302012-10-04 17:15:29 -0700518 .siginfo = siginfo,
Al Viro541880d2012-11-05 13:11:26 -0500519 .regs = signal_pt_regs(),
Alex Kelly10c28d92012-09-26 21:52:08 -0400520 .limit = rlimit(RLIMIT_CORE),
521 /*
522 * We must use the same mm->flags while dumping core to avoid
523 * inconsistency of bit flags, since this flag is not protected
524 * by any locks.
525 */
526 .mm_flags = mm->flags,
527 };
528
Denys Vlasenko5ab1c302012-10-04 17:15:29 -0700529 audit_core_dumps(siginfo->si_signo);
Alex Kelly10c28d92012-09-26 21:52:08 -0400530
531 binfmt = mm->binfmt;
532 if (!binfmt || !binfmt->core_dump)
533 goto fail;
534 if (!__get_dumpable(cprm.mm_flags))
535 goto fail;
536
537 cred = prepare_creds();
538 if (!cred)
539 goto fail;
540 /*
541 * We cannot trust fsuid as being the "true" uid of the process
542 * nor do we know its entire history. We only know it was tainted
543 * so we dump it as root in mode 2, and only into a controlled
544 * environment (pipe handler or fully qualified path).
545 */
Kees Cooke579d2c2013-02-27 17:03:15 -0800546 if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
Alex Kelly10c28d92012-09-26 21:52:08 -0400547 /* Setuid core dump mode */
Alex Kelly10c28d92012-09-26 21:52:08 -0400548 cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
Jann Horn244d3c12015-09-09 15:38:28 -0700549 need_suid_safe = true;
Alex Kelly10c28d92012-09-26 21:52:08 -0400550 }
551
Denys Vlasenko5ab1c302012-10-04 17:15:29 -0700552 retval = coredump_wait(siginfo->si_signo, &core_state);
Alex Kelly10c28d92012-09-26 21:52:08 -0400553 if (retval < 0)
554 goto fail_creds;
555
556 old_cred = override_creds(cred);
557
Oleg Nesterov12a2b4b2012-10-04 17:15:25 -0700558 ispipe = format_corename(&cn, &cprm);
Alex Kelly10c28d92012-09-26 21:52:08 -0400559
Lucas De Marchifb96c472013-04-30 15:28:06 -0700560 if (ispipe) {
Alex Kelly10c28d92012-09-26 21:52:08 -0400561 int dump_count;
562 char **helper_argv;
Lucas De Marchi907ed132013-04-30 15:28:07 -0700563 struct subprocess_info *sub_info;
Alex Kelly10c28d92012-09-26 21:52:08 -0400564
565 if (ispipe < 0) {
566 printk(KERN_WARNING "format_corename failed\n");
567 printk(KERN_WARNING "Aborting core\n");
Oleg Nesterove7fd1542013-07-03 15:08:16 -0700568 goto fail_unlock;
Alex Kelly10c28d92012-09-26 21:52:08 -0400569 }
570
571 if (cprm.limit == 1) {
572 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
573 *
574 * Normally core limits are irrelevant to pipes, since
575 * we're not writing to the file system, but we use
Bastien Nocerafcbc32b2015-02-05 14:35:05 +0100576 * cprm.limit of 1 here as a special value, this is a
Alex Kelly10c28d92012-09-26 21:52:08 -0400577 * consistent way to catch recursive crashes.
578 * We can still crash if the core_pattern binary sets
579 * RLIM_CORE = !1, but it runs as root, and can do
580 * lots of stupid things.
581 *
582 * Note that we use task_tgid_vnr here to grab the pid
583 * of the process group leader. That way we get the
584 * right pid if a thread in a multi-threaded
585 * core_pattern process dies.
586 */
587 printk(KERN_WARNING
588 "Process %d(%s) has RLIMIT_CORE set to 1\n",
589 task_tgid_vnr(current), current->comm);
590 printk(KERN_WARNING "Aborting core\n");
591 goto fail_unlock;
592 }
593 cprm.limit = RLIM_INFINITY;
594
595 dump_count = atomic_inc_return(&core_dump_count);
596 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
597 printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
598 task_tgid_vnr(current), current->comm);
599 printk(KERN_WARNING "Skipping core dump\n");
600 goto fail_dropcount;
601 }
602
Oleg Nesterov888ffc52013-07-03 15:08:23 -0700603 helper_argv = argv_split(GFP_KERNEL, cn.corename, NULL);
Alex Kelly10c28d92012-09-26 21:52:08 -0400604 if (!helper_argv) {
605 printk(KERN_WARNING "%s failed to allocate memory\n",
606 __func__);
607 goto fail_dropcount;
608 }
609
Lucas De Marchi907ed132013-04-30 15:28:07 -0700610 retval = -ENOMEM;
611 sub_info = call_usermodehelper_setup(helper_argv[0],
612 helper_argv, NULL, GFP_KERNEL,
613 umh_pipe_setup, NULL, &cprm);
614 if (sub_info)
615 retval = call_usermodehelper_exec(sub_info,
616 UMH_WAIT_EXEC);
617
Alex Kelly10c28d92012-09-26 21:52:08 -0400618 argv_free(helper_argv);
619 if (retval) {
Oleg Nesterov888ffc52013-07-03 15:08:23 -0700620 printk(KERN_INFO "Core dump to |%s pipe failed\n",
Alex Kelly10c28d92012-09-26 21:52:08 -0400621 cn.corename);
622 goto close_fail;
Lucas De Marchifb96c472013-04-30 15:28:06 -0700623 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400624 } else {
625 struct inode *inode;
626
627 if (cprm.limit < binfmt->min_coredump)
628 goto fail_unlock;
629
Jann Horn244d3c12015-09-09 15:38:28 -0700630 if (need_suid_safe && cn.corename[0] != '/') {
Alex Kelly10c28d92012-09-26 21:52:08 -0400631 printk(KERN_WARNING "Pid %d(%s) can only dump core "\
632 "to fully qualified path!\n",
633 task_tgid_vnr(current), current->comm);
634 printk(KERN_WARNING "Skipping core dump\n");
635 goto fail_unlock;
636 }
637
Jann Horn244d3c12015-09-09 15:38:28 -0700638 /*
639 * Unlink the file if it exists unless this is a SUID
640 * binary - in that case, we're running around with root
641 * privs and don't want to unlink another user's coredump.
642 */
643 if (!need_suid_safe) {
644 mm_segment_t old_fs;
645
646 old_fs = get_fs();
647 set_fs(KERNEL_DS);
648 /*
649 * If it doesn't exist, that's fine. If there's some
650 * other problem, we'll catch it at the filp_open().
651 */
652 (void) sys_unlink((const char __user *)cn.corename);
653 set_fs(old_fs);
654 }
655
656 /*
657 * There is a race between unlinking and creating the
658 * file, but if that causes an EEXIST here, that's
659 * fine - another process raced with us while creating
660 * the corefile, and the other process won. To userspace,
661 * what matters is that at least one of the two processes
662 * writes its coredump successfully, not which one.
663 */
Alex Kelly10c28d92012-09-26 21:52:08 -0400664 cprm.file = filp_open(cn.corename,
Jann Horn244d3c12015-09-09 15:38:28 -0700665 O_CREAT | 2 | O_NOFOLLOW |
666 O_LARGEFILE | O_EXCL,
Alex Kelly10c28d92012-09-26 21:52:08 -0400667 0600);
668 if (IS_ERR(cprm.file))
669 goto fail_unlock;
670
Al Viro496ad9a2013-01-23 17:07:38 -0500671 inode = file_inode(cprm.file);
Alex Kelly10c28d92012-09-26 21:52:08 -0400672 if (inode->i_nlink > 1)
673 goto close_fail;
674 if (d_unhashed(cprm.file->f_path.dentry))
675 goto close_fail;
676 /*
677 * AK: actually i see no reason to not allow this for named
678 * pipes etc, but keep the previous behaviour for now.
679 */
680 if (!S_ISREG(inode->i_mode))
681 goto close_fail;
682 /*
Jann Horn2be9c822015-09-09 15:38:30 -0700683 * Don't dump core if the filesystem changed owner or mode
684 * of the file during file creation. This is an issue when
685 * a process dumps core while its cwd is e.g. on a vfat
686 * filesystem.
Alex Kelly10c28d92012-09-26 21:52:08 -0400687 */
688 if (!uid_eq(inode->i_uid, current_fsuid()))
689 goto close_fail;
Jann Horn2be9c822015-09-09 15:38:30 -0700690 if ((inode->i_mode & 0677) != 0600)
691 goto close_fail;
Al Viro86cc0582015-04-03 15:23:17 -0400692 if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
Alex Kelly10c28d92012-09-26 21:52:08 -0400693 goto close_fail;
694 if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
695 goto close_fail;
696 }
697
698 /* get us an unshared descriptor table; almost always a no-op */
699 retval = unshare_files(&displaced);
700 if (retval)
701 goto close_fail;
702 if (displaced)
703 put_files_struct(displaced);
Al Viroe86d35c2013-05-04 14:45:54 -0400704 if (!dump_interrupted()) {
705 file_start_write(cprm.file);
706 core_dumped = binfmt->core_dump(&cprm);
707 file_end_write(cprm.file);
708 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400709 if (ispipe && core_pipe_limit)
710 wait_for_dump_helpers(cprm.file);
711close_fail:
712 if (cprm.file)
713 filp_close(cprm.file, NULL);
714fail_dropcount:
715 if (ispipe)
716 atomic_dec(&core_dump_count);
717fail_unlock:
718 kfree(cn.corename);
Oleg Nesterovacdedd92013-04-30 15:28:13 -0700719 coredump_finish(mm, core_dumped);
Alex Kelly10c28d92012-09-26 21:52:08 -0400720 revert_creds(old_cred);
721fail_creds:
722 put_cred(cred);
723fail:
724 return;
725}
726
727/*
728 * Core dumping helper functions. These are the only things you should
729 * do on a core-file: use only these functions to write out all the
730 * necessary info.
731 */
Al Viroecc8c772013-10-05 15:32:35 -0400732int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
733{
734 struct file *file = cprm->file;
Al Viro2507a4f2013-10-08 09:11:48 -0400735 loff_t pos = file->f_pos;
736 ssize_t n;
Al Viroecc8c772013-10-05 15:32:35 -0400737 if (cprm->written + nr > cprm->limit)
738 return 0;
Al Viro2507a4f2013-10-08 09:11:48 -0400739 while (nr) {
740 if (dump_interrupted())
741 return 0;
Al Viro52da40a2013-11-15 21:58:33 -0500742 n = __kernel_write(file, addr, nr, &pos);
Al Viro2507a4f2013-10-08 09:11:48 -0400743 if (n <= 0)
744 return 0;
745 file->f_pos = pos;
746 cprm->written += n;
747 nr -= n;
748 }
Al Viroecc8c772013-10-05 15:32:35 -0400749 return 1;
750}
751EXPORT_SYMBOL(dump_emit);
752
Al Viro9b56d542013-10-08 09:26:08 -0400753int dump_skip(struct coredump_params *cprm, size_t nr)
Alex Kelly10c28d92012-09-26 21:52:08 -0400754{
Al Viro9b56d542013-10-08 09:26:08 -0400755 static char zeroes[PAGE_SIZE];
756 struct file *file = cprm->file;
Alex Kelly10c28d92012-09-26 21:52:08 -0400757 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
Al Viro9b56d542013-10-08 09:26:08 -0400758 if (cprm->written + nr > cprm->limit)
759 return 0;
Oleg Nesterov528f8272013-04-30 15:28:15 -0700760 if (dump_interrupted() ||
Al Viro9b56d542013-10-08 09:26:08 -0400761 file->f_op->llseek(file, nr, SEEK_CUR) < 0)
Alex Kelly10c28d92012-09-26 21:52:08 -0400762 return 0;
Al Viro9b56d542013-10-08 09:26:08 -0400763 cprm->written += nr;
764 return 1;
Alex Kelly10c28d92012-09-26 21:52:08 -0400765 } else {
Al Viro9b56d542013-10-08 09:26:08 -0400766 while (nr > PAGE_SIZE) {
767 if (!dump_emit(cprm, zeroes, PAGE_SIZE))
768 return 0;
769 nr -= PAGE_SIZE;
Alex Kelly10c28d92012-09-26 21:52:08 -0400770 }
Al Viro9b56d542013-10-08 09:26:08 -0400771 return dump_emit(cprm, zeroes, nr);
Alex Kelly10c28d92012-09-26 21:52:08 -0400772 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400773}
Al Viro9b56d542013-10-08 09:26:08 -0400774EXPORT_SYMBOL(dump_skip);
Al Viro22a8cb82013-10-08 11:05:01 -0400775
776int dump_align(struct coredump_params *cprm, int align)
777{
778 unsigned mod = cprm->written & (align - 1);
779 if (align & (align - 1))
Al Virodb512422013-11-15 21:55:52 -0500780 return 0;
781 return mod ? dump_skip(cprm, align - mod) : 1;
Al Viro22a8cb82013-10-08 11:05:01 -0400782}
783EXPORT_SYMBOL(dump_align);