blob: ef7ed64947e9c28ea89b49423feaff653e035c84 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Alex Kelly10c28d92012-09-26 21:52:08 -04002#include <linux/slab.h>
3#include <linux/file.h>
4#include <linux/fdtable.h>
Andrey Ryabinin70d78fe2016-11-10 10:46:38 -08005#include <linux/freezer.h>
Alex Kelly10c28d92012-09-26 21:52:08 -04006#include <linux/mm.h>
7#include <linux/stat.h>
8#include <linux/fcntl.h>
9#include <linux/swap.h>
10#include <linux/string.h>
11#include <linux/init.h>
12#include <linux/pagemap.h>
13#include <linux/perf_event.h>
14#include <linux/highmem.h>
15#include <linux/spinlock.h>
16#include <linux/key.h>
17#include <linux/personality.h>
18#include <linux/binfmts.h>
Alex Kelly179899f2012-10-04 17:15:24 -070019#include <linux/coredump.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010020#include <linux/sched/coredump.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010021#include <linux/sched/signal.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010022#include <linux/sched/task_stack.h>
Alex Kelly10c28d92012-09-26 21:52:08 -040023#include <linux/utsname.h>
24#include <linux/pid_namespace.h>
25#include <linux/module.h>
26#include <linux/namei.h>
27#include <linux/mount.h>
28#include <linux/security.h>
29#include <linux/syscalls.h>
30#include <linux/tsacct_kern.h>
31#include <linux/cn_proc.h>
32#include <linux/audit.h>
33#include <linux/tracehook.h>
34#include <linux/kmod.h>
35#include <linux/fsnotify.h>
36#include <linux/fs_struct.h>
37#include <linux/pipe_fs_i.h>
38#include <linux/oom.h>
39#include <linux/compat.h>
Jann Horn378c6522016-03-22 14:25:36 -070040#include <linux/fs.h>
41#include <linux/path.h>
Arnd Bergmann03927c82015-11-25 16:22:25 +010042#include <linux/timekeeping.h>
Alex Kelly10c28d92012-09-26 21:52:08 -040043
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080044#include <linux/uaccess.h>
Alex Kelly10c28d92012-09-26 21:52:08 -040045#include <asm/mmu_context.h>
46#include <asm/tlb.h>
47#include <asm/exec.h>
48
49#include <trace/events/task.h>
50#include "internal.h"
Alex Kelly10c28d92012-09-26 21:52:08 -040051
52#include <trace/events/sched.h>
53
54int core_uses_pid;
Alex Kelly10c28d92012-09-26 21:52:08 -040055unsigned int core_pipe_limit;
Oleg Nesterov3ceadcf2013-07-03 15:08:22 -070056char core_pattern[CORENAME_MAX_SIZE] = "core";
57static int core_name_size = CORENAME_MAX_SIZE;
Alex Kelly10c28d92012-09-26 21:52:08 -040058
59struct core_name {
60 char *corename;
61 int used, size;
62};
Alex Kelly10c28d92012-09-26 21:52:08 -040063
64/* The maximal length of core_pattern is also specified in sysctl.c */
65
Oleg Nesterov3ceadcf2013-07-03 15:08:22 -070066static int expand_corename(struct core_name *cn, int size)
Alex Kelly10c28d92012-09-26 21:52:08 -040067{
Oleg Nesterove7fd1542013-07-03 15:08:16 -070068 char *corename = krealloc(cn->corename, size, GFP_KERNEL);
Alex Kelly10c28d92012-09-26 21:52:08 -040069
Oleg Nesterove7fd1542013-07-03 15:08:16 -070070 if (!corename)
Alex Kelly10c28d92012-09-26 21:52:08 -040071 return -ENOMEM;
Alex Kelly10c28d92012-09-26 21:52:08 -040072
Oleg Nesterov3ceadcf2013-07-03 15:08:22 -070073 if (size > core_name_size) /* racy but harmless */
74 core_name_size = size;
75
76 cn->size = ksize(corename);
Oleg Nesterove7fd1542013-07-03 15:08:16 -070077 cn->corename = corename;
Alex Kelly10c28d92012-09-26 21:52:08 -040078 return 0;
79}
80
Nicolas Ioossb4176b72015-06-25 15:03:53 -070081static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt,
82 va_list arg)
Alex Kelly10c28d92012-09-26 21:52:08 -040083{
Oleg Nesterov5fe9d8c2013-07-03 15:08:19 -070084 int free, need;
Eric Dumazet404ca802014-04-19 10:15:07 -070085 va_list arg_copy;
Alex Kelly10c28d92012-09-26 21:52:08 -040086
Oleg Nesterov5fe9d8c2013-07-03 15:08:19 -070087again:
88 free = cn->size - cn->used;
Eric Dumazet404ca802014-04-19 10:15:07 -070089
90 va_copy(arg_copy, arg);
91 need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
92 va_end(arg_copy);
93
Oleg Nesterov5fe9d8c2013-07-03 15:08:19 -070094 if (need < free) {
95 cn->used += need;
96 return 0;
97 }
Alex Kelly10c28d92012-09-26 21:52:08 -040098
Oleg Nesterov3ceadcf2013-07-03 15:08:22 -070099 if (!expand_corename(cn, cn->size + need - free + 1))
Oleg Nesterov5fe9d8c2013-07-03 15:08:19 -0700100 goto again;
Alex Kelly10c28d92012-09-26 21:52:08 -0400101
Oleg Nesterov5fe9d8c2013-07-03 15:08:19 -0700102 return -ENOMEM;
Alex Kelly10c28d92012-09-26 21:52:08 -0400103}
104
Nicolas Ioossb4176b72015-06-25 15:03:53 -0700105static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...)
Oleg Nesterovbc03c692013-07-03 15:08:17 -0700106{
107 va_list arg;
108 int ret;
109
110 va_start(arg, fmt);
111 ret = cn_vprintf(cn, fmt, arg);
112 va_end(arg);
113
114 return ret;
115}
116
Nicolas Ioossb4176b72015-06-25 15:03:53 -0700117static __printf(2, 3)
118int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
Alex Kelly10c28d92012-09-26 21:52:08 -0400119{
Oleg Nesterov923bed032013-07-03 15:08:20 -0700120 int cur = cn->used;
121 va_list arg;
122 int ret;
123
124 va_start(arg, fmt);
125 ret = cn_vprintf(cn, fmt, arg);
126 va_end(arg);
127
Jann Hornac94b6e2016-01-20 15:00:08 -0800128 if (ret == 0) {
129 /*
130 * Ensure that this coredump name component can't cause the
131 * resulting corefile path to consist of a ".." or ".".
132 */
133 if ((cn->used - cur == 1 && cn->corename[cur] == '.') ||
134 (cn->used - cur == 2 && cn->corename[cur] == '.'
135 && cn->corename[cur+1] == '.'))
136 cn->corename[cur] = '!';
137
138 /*
139 * Empty names are fishy and could be used to create a "//" in a
140 * corefile name, causing the coredump to happen one directory
141 * level too high. Enforce that all components of the core
142 * pattern are at least one character long.
143 */
144 if (cn->used == cur)
145 ret = cn_printf(cn, "!");
146 }
147
Oleg Nesterov923bed032013-07-03 15:08:20 -0700148 for (; cur < cn->used; ++cur) {
149 if (cn->corename[cur] == '/')
150 cn->corename[cur] = '!';
151 }
152 return ret;
Alex Kelly10c28d92012-09-26 21:52:08 -0400153}
154
155static int cn_print_exe_file(struct core_name *cn)
156{
157 struct file *exe_file;
158 char *pathbuf, *path;
159 int ret;
160
161 exe_file = get_mm_exe_file(current->mm);
Oleg Nesterov923bed032013-07-03 15:08:20 -0700162 if (!exe_file)
163 return cn_esc_printf(cn, "%s (path unknown)", current->comm);
Alex Kelly10c28d92012-09-26 21:52:08 -0400164
Michal Hocko0ee931c2017-09-13 16:28:29 -0700165 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Alex Kelly10c28d92012-09-26 21:52:08 -0400166 if (!pathbuf) {
167 ret = -ENOMEM;
168 goto put_exe_file;
169 }
170
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +0200171 path = file_path(exe_file, pathbuf, PATH_MAX);
Alex Kelly10c28d92012-09-26 21:52:08 -0400172 if (IS_ERR(path)) {
173 ret = PTR_ERR(path);
174 goto free_buf;
175 }
176
Oleg Nesterov923bed032013-07-03 15:08:20 -0700177 ret = cn_esc_printf(cn, "%s", path);
Alex Kelly10c28d92012-09-26 21:52:08 -0400178
179free_buf:
180 kfree(pathbuf);
181put_exe_file:
182 fput(exe_file);
183 return ret;
184}
185
186/* format_corename will inspect the pattern parameter, and output a
187 * name into corename, which must have space for at least
188 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
189 */
Oleg Nesterov12a2b4b2012-10-04 17:15:25 -0700190static int format_corename(struct core_name *cn, struct coredump_params *cprm)
Alex Kelly10c28d92012-09-26 21:52:08 -0400191{
192 const struct cred *cred = current_cred();
193 const char *pat_ptr = core_pattern;
194 int ispipe = (*pat_ptr == '|');
195 int pid_in_pattern = 0;
196 int err = 0;
197
Oleg Nesterove7fd1542013-07-03 15:08:16 -0700198 cn->used = 0;
Oleg Nesterov3ceadcf2013-07-03 15:08:22 -0700199 cn->corename = NULL;
200 if (expand_corename(cn, core_name_size))
Alex Kelly10c28d92012-09-26 21:52:08 -0400201 return -ENOMEM;
Oleg Nesterov888ffc52013-07-03 15:08:23 -0700202 cn->corename[0] = '\0';
203
204 if (ispipe)
205 ++pat_ptr;
Alex Kelly10c28d92012-09-26 21:52:08 -0400206
207 /* Repeat as long as we have more pattern to process and more output
208 space */
209 while (*pat_ptr) {
210 if (*pat_ptr != '%') {
Alex Kelly10c28d92012-09-26 21:52:08 -0400211 err = cn_printf(cn, "%c", *pat_ptr++);
212 } else {
213 switch (*++pat_ptr) {
214 /* single % at the end, drop that */
215 case 0:
216 goto out;
217 /* Double percent, output one percent */
218 case '%':
219 err = cn_printf(cn, "%c", '%');
220 break;
221 /* pid */
222 case 'p':
223 pid_in_pattern = 1;
224 err = cn_printf(cn, "%d",
225 task_tgid_vnr(current));
226 break;
Stéphane Graber65aafb12013-09-11 14:24:32 -0700227 /* global pid */
228 case 'P':
229 err = cn_printf(cn, "%d",
230 task_tgid_nr(current));
231 break;
Oleg Nesterovb03023e2014-10-13 15:53:35 -0700232 case 'i':
233 err = cn_printf(cn, "%d",
234 task_pid_vnr(current));
235 break;
236 case 'I':
237 err = cn_printf(cn, "%d",
238 task_pid_nr(current));
239 break;
Alex Kelly10c28d92012-09-26 21:52:08 -0400240 /* uid */
241 case 'u':
Nicolas Iooss5202efe2015-06-25 15:03:51 -0700242 err = cn_printf(cn, "%u",
243 from_kuid(&init_user_ns,
244 cred->uid));
Alex Kelly10c28d92012-09-26 21:52:08 -0400245 break;
246 /* gid */
247 case 'g':
Nicolas Iooss5202efe2015-06-25 15:03:51 -0700248 err = cn_printf(cn, "%u",
249 from_kgid(&init_user_ns,
250 cred->gid));
Alex Kelly10c28d92012-09-26 21:52:08 -0400251 break;
Oleg Nesterov12a2b4b2012-10-04 17:15:25 -0700252 case 'd':
253 err = cn_printf(cn, "%d",
254 __get_dumpable(cprm->mm_flags));
255 break;
Alex Kelly10c28d92012-09-26 21:52:08 -0400256 /* signal that caused the coredump */
257 case 's':
Nicolas Ioossb4176b72015-06-25 15:03:53 -0700258 err = cn_printf(cn, "%d",
259 cprm->siginfo->si_signo);
Alex Kelly10c28d92012-09-26 21:52:08 -0400260 break;
261 /* UNIX time of coredump */
262 case 't': {
Arnd Bergmann03927c82015-11-25 16:22:25 +0100263 time64_t time;
264
265 time = ktime_get_real_seconds();
266 err = cn_printf(cn, "%lld", time);
Alex Kelly10c28d92012-09-26 21:52:08 -0400267 break;
268 }
269 /* hostname */
Oleg Nesterov923bed032013-07-03 15:08:20 -0700270 case 'h':
Alex Kelly10c28d92012-09-26 21:52:08 -0400271 down_read(&uts_sem);
Oleg Nesterov923bed032013-07-03 15:08:20 -0700272 err = cn_esc_printf(cn, "%s",
Alex Kelly10c28d92012-09-26 21:52:08 -0400273 utsname()->nodename);
274 up_read(&uts_sem);
Alex Kelly10c28d92012-09-26 21:52:08 -0400275 break;
Alex Kelly10c28d92012-09-26 21:52:08 -0400276 /* executable */
Oleg Nesterov923bed032013-07-03 15:08:20 -0700277 case 'e':
278 err = cn_esc_printf(cn, "%s", current->comm);
Alex Kelly10c28d92012-09-26 21:52:08 -0400279 break;
Alex Kelly10c28d92012-09-26 21:52:08 -0400280 case 'E':
281 err = cn_print_exe_file(cn);
282 break;
283 /* core limit size */
284 case 'c':
285 err = cn_printf(cn, "%lu",
286 rlimit(RLIMIT_CORE));
287 break;
288 default:
289 break;
290 }
291 ++pat_ptr;
292 }
293
294 if (err)
295 return err;
296 }
297
Oleg Nesterov888ffc52013-07-03 15:08:23 -0700298out:
Alex Kelly10c28d92012-09-26 21:52:08 -0400299 /* Backward compatibility with core_uses_pid:
300 *
301 * If core_pattern does not include a %p (as is the default)
302 * and core_uses_pid is set, then .%pid will be appended to
303 * the filename. Do not do this for piped commands. */
304 if (!ispipe && !pid_in_pattern && core_uses_pid) {
305 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
306 if (err)
307 return err;
308 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400309 return ispipe;
310}
311
Oleg Nesterov5fa534c2015-11-06 16:32:31 -0800312static int zap_process(struct task_struct *start, int exit_code, int flags)
Alex Kelly10c28d92012-09-26 21:52:08 -0400313{
314 struct task_struct *t;
315 int nr = 0;
316
Oleg Nesterov5fa534c2015-11-06 16:32:31 -0800317 /* ignore all signals except SIGKILL, see prepare_signal() */
318 start->signal->flags = SIGNAL_GROUP_COREDUMP | flags;
Alex Kelly10c28d92012-09-26 21:52:08 -0400319 start->signal->group_exit_code = exit_code;
320 start->signal->group_stop_count = 0;
321
Oleg Nesterovd61ba582015-11-06 16:32:34 -0800322 for_each_thread(start, t) {
Alex Kelly10c28d92012-09-26 21:52:08 -0400323 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
324 if (t != current && t->mm) {
325 sigaddset(&t->pending.signal, SIGKILL);
326 signal_wake_up(t, 1);
327 nr++;
328 }
Oleg Nesterovd61ba582015-11-06 16:32:34 -0800329 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400330
331 return nr;
332}
333
Oleg Nesterov403bad72013-04-30 15:28:10 -0700334static int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
335 struct core_state *core_state, int exit_code)
Alex Kelly10c28d92012-09-26 21:52:08 -0400336{
337 struct task_struct *g, *p;
338 unsigned long flags;
339 int nr = -EAGAIN;
340
341 spin_lock_irq(&tsk->sighand->siglock);
342 if (!signal_group_exit(tsk->signal)) {
343 mm->core_state = core_state;
Oleg Nesterov6cd8f0a2013-04-30 15:28:12 -0700344 tsk->signal->group_exit_task = tsk;
Oleg Nesterov5fa534c2015-11-06 16:32:31 -0800345 nr = zap_process(tsk, exit_code, 0);
Oleg Nesterov403bad72013-04-30 15:28:10 -0700346 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
Alex Kelly10c28d92012-09-26 21:52:08 -0400347 }
348 spin_unlock_irq(&tsk->sighand->siglock);
349 if (unlikely(nr < 0))
350 return nr;
351
Silesh C Vaed8adb2014-07-23 13:59:59 -0700352 tsk->flags |= PF_DUMPCORE;
Alex Kelly10c28d92012-09-26 21:52:08 -0400353 if (atomic_read(&mm->mm_users) == nr + 1)
354 goto done;
355 /*
356 * We should find and kill all tasks which use this mm, and we should
357 * count them correctly into ->nr_threads. We don't take tasklist
358 * lock, but this is safe wrt:
359 *
360 * fork:
361 * None of sub-threads can fork after zap_process(leader). All
362 * processes which were created before this point should be
363 * visible to zap_threads() because copy_process() adds the new
364 * process to the tail of init_task.tasks list, and lock/unlock
365 * of ->siglock provides a memory barrier.
366 *
367 * do_exit:
368 * The caller holds mm->mmap_sem. This means that the task which
369 * uses this mm can't pass exit_mm(), so it can't exit or clear
370 * its ->mm.
371 *
372 * de_thread:
373 * It does list_replace_rcu(&leader->tasks, &current->tasks),
374 * we must see either old or new leader, this does not matter.
375 * However, it can change p->sighand, so lock_task_sighand(p)
376 * must be used. Since p->mm != NULL and we hold ->mmap_sem
377 * it can't fail.
378 *
379 * Note also that "g" can be the old leader with ->mm == NULL
380 * and already unhashed and thus removed from ->thread_group.
381 * This is OK, __unhash_process()->list_del_rcu() does not
382 * clear the ->next pointer, we will find the new leader via
383 * next_thread().
384 */
385 rcu_read_lock();
386 for_each_process(g) {
387 if (g == tsk->group_leader)
388 continue;
389 if (g->flags & PF_KTHREAD)
390 continue;
Oleg Nesterovd61ba582015-11-06 16:32:34 -0800391
392 for_each_thread(g, p) {
393 if (unlikely(!p->mm))
394 continue;
395 if (unlikely(p->mm == mm)) {
396 lock_task_sighand(p, &flags);
397 nr += zap_process(p, exit_code,
398 SIGNAL_GROUP_EXIT);
399 unlock_task_sighand(p, &flags);
Alex Kelly10c28d92012-09-26 21:52:08 -0400400 }
Oleg Nesterovd61ba582015-11-06 16:32:34 -0800401 break;
402 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400403 }
404 rcu_read_unlock();
405done:
406 atomic_set(&core_state->nr_threads, nr);
407 return nr;
408}
409
410static int coredump_wait(int exit_code, struct core_state *core_state)
411{
412 struct task_struct *tsk = current;
413 struct mm_struct *mm = tsk->mm;
414 int core_waiters = -EBUSY;
415
416 init_completion(&core_state->startup);
417 core_state->dumper.task = tsk;
418 core_state->dumper.next = NULL;
419
Michal Hocko4136c262016-05-23 16:25:57 -0700420 if (down_write_killable(&mm->mmap_sem))
421 return -EINTR;
422
Alex Kelly10c28d92012-09-26 21:52:08 -0400423 if (!mm->core_state)
424 core_waiters = zap_threads(tsk, mm, core_state, exit_code);
425 up_write(&mm->mmap_sem);
426
427 if (core_waiters > 0) {
428 struct core_thread *ptr;
429
Andrey Ryabinin70d78fe2016-11-10 10:46:38 -0800430 freezer_do_not_count();
Alex Kelly10c28d92012-09-26 21:52:08 -0400431 wait_for_completion(&core_state->startup);
Andrey Ryabinin70d78fe2016-11-10 10:46:38 -0800432 freezer_count();
Alex Kelly10c28d92012-09-26 21:52:08 -0400433 /*
434 * Wait for all the threads to become inactive, so that
435 * all the thread context (extended register state, like
436 * fpu etc) gets copied to the memory.
437 */
438 ptr = core_state->dumper.next;
439 while (ptr != NULL) {
440 wait_task_inactive(ptr->task, 0);
441 ptr = ptr->next;
442 }
443 }
444
445 return core_waiters;
446}
447
Oleg Nesterovacdedd92013-04-30 15:28:13 -0700448static void coredump_finish(struct mm_struct *mm, bool core_dumped)
Alex Kelly10c28d92012-09-26 21:52:08 -0400449{
450 struct core_thread *curr, *next;
451 struct task_struct *task;
452
Oleg Nesterov6cd8f0a2013-04-30 15:28:12 -0700453 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterovacdedd92013-04-30 15:28:13 -0700454 if (core_dumped && !__fatal_signal_pending(current))
455 current->signal->group_exit_code |= 0x80;
Oleg Nesterov6cd8f0a2013-04-30 15:28:12 -0700456 current->signal->group_exit_task = NULL;
457 current->signal->flags = SIGNAL_GROUP_EXIT;
458 spin_unlock_irq(&current->sighand->siglock);
459
Alex Kelly10c28d92012-09-26 21:52:08 -0400460 next = mm->core_state->dumper.next;
461 while ((curr = next) != NULL) {
462 next = curr->next;
463 task = curr->task;
464 /*
465 * see exit_mm(), curr->task must not see
466 * ->task == NULL before we read ->next.
467 */
468 smp_mb();
469 curr->task = NULL;
470 wake_up_process(task);
471 }
472
473 mm->core_state = NULL;
474}
475
Oleg Nesterov528f8272013-04-30 15:28:15 -0700476static bool dump_interrupted(void)
477{
478 /*
479 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
480 * can do try_to_freeze() and check __fatal_signal_pending(),
481 * but then we need to teach dump_write() to restart and clear
482 * TIF_SIGPENDING.
483 */
484 return signal_pending(current);
485}
486
Alex Kelly10c28d92012-09-26 21:52:08 -0400487static void wait_for_dump_helpers(struct file *file)
488{
Al Virode32ec42013-03-21 11:16:56 -0400489 struct pipe_inode_info *pipe = file->private_data;
Alex Kelly10c28d92012-09-26 21:52:08 -0400490
491 pipe_lock(pipe);
492 pipe->readers++;
493 pipe->writers--;
Oleg Nesterovdc7ee2a2013-04-30 15:28:17 -0700494 wake_up_interruptible_sync(&pipe->wait);
495 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
496 pipe_unlock(pipe);
Alex Kelly10c28d92012-09-26 21:52:08 -0400497
Oleg Nesterovdc7ee2a2013-04-30 15:28:17 -0700498 /*
499 * We actually want wait_event_freezable() but then we need
500 * to clear TIF_SIGPENDING and improve dump_interrupted().
501 */
502 wait_event_interruptible(pipe->wait, pipe->readers == 1);
Alex Kelly10c28d92012-09-26 21:52:08 -0400503
Oleg Nesterovdc7ee2a2013-04-30 15:28:17 -0700504 pipe_lock(pipe);
Alex Kelly10c28d92012-09-26 21:52:08 -0400505 pipe->readers--;
506 pipe->writers++;
507 pipe_unlock(pipe);
Alex Kelly10c28d92012-09-26 21:52:08 -0400508}
509
510/*
511 * umh_pipe_setup
512 * helper function to customize the process used
513 * to collect the core in userspace. Specifically
514 * it sets up a pipe and installs it as fd 0 (stdin)
515 * for the process. Returns 0 on success, or
516 * PTR_ERR on failure.
517 * Note that it also sets the core limit to 1. This
518 * is a special value that we use to trap recursive
519 * core dumps
520 */
521static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
522{
523 struct file *files[2];
524 struct coredump_params *cp = (struct coredump_params *)info->data;
525 int err = create_pipe_files(files, 0);
526 if (err)
527 return err;
528
529 cp->file = files[1];
530
Al Viro45525b22012-10-16 13:30:07 -0400531 err = replace_fd(0, files[0], 0);
532 fput(files[0]);
Alex Kelly10c28d92012-09-26 21:52:08 -0400533 /* and disallow core files too */
534 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
535
Al Viro45525b22012-10-16 13:30:07 -0400536 return err;
Alex Kelly10c28d92012-09-26 21:52:08 -0400537}
538
Al Viroec579412013-10-13 17:57:29 -0400539void do_coredump(const siginfo_t *siginfo)
Alex Kelly10c28d92012-09-26 21:52:08 -0400540{
541 struct core_state core_state;
542 struct core_name cn;
543 struct mm_struct *mm = current->mm;
544 struct linux_binfmt * binfmt;
545 const struct cred *old_cred;
546 struct cred *cred;
547 int retval = 0;
Alex Kelly10c28d92012-09-26 21:52:08 -0400548 int ispipe;
549 struct files_struct *displaced;
Jann Hornfbb18162015-09-09 15:38:28 -0700550 /* require nonrelative corefile path and be extra careful */
551 bool need_suid_safe = false;
Oleg Nesterovacdedd92013-04-30 15:28:13 -0700552 bool core_dumped = false;
Alex Kelly10c28d92012-09-26 21:52:08 -0400553 static atomic_t core_dump_count = ATOMIC_INIT(0);
554 struct coredump_params cprm = {
Denys Vlasenko5ab1c302012-10-04 17:15:29 -0700555 .siginfo = siginfo,
Al Viro541880d2012-11-05 13:11:26 -0500556 .regs = signal_pt_regs(),
Alex Kelly10c28d92012-09-26 21:52:08 -0400557 .limit = rlimit(RLIMIT_CORE),
558 /*
559 * We must use the same mm->flags while dumping core to avoid
560 * inconsistency of bit flags, since this flag is not protected
561 * by any locks.
562 */
563 .mm_flags = mm->flags,
564 };
565
Denys Vlasenko5ab1c302012-10-04 17:15:29 -0700566 audit_core_dumps(siginfo->si_signo);
Alex Kelly10c28d92012-09-26 21:52:08 -0400567
568 binfmt = mm->binfmt;
569 if (!binfmt || !binfmt->core_dump)
570 goto fail;
571 if (!__get_dumpable(cprm.mm_flags))
572 goto fail;
573
574 cred = prepare_creds();
575 if (!cred)
576 goto fail;
577 /*
578 * We cannot trust fsuid as being the "true" uid of the process
579 * nor do we know its entire history. We only know it was tainted
580 * so we dump it as root in mode 2, and only into a controlled
581 * environment (pipe handler or fully qualified path).
582 */
Kees Cooke579d2c2013-02-27 17:03:15 -0800583 if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
Alex Kelly10c28d92012-09-26 21:52:08 -0400584 /* Setuid core dump mode */
Alex Kelly10c28d92012-09-26 21:52:08 -0400585 cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
Jann Hornfbb18162015-09-09 15:38:28 -0700586 need_suid_safe = true;
Alex Kelly10c28d92012-09-26 21:52:08 -0400587 }
588
Denys Vlasenko5ab1c302012-10-04 17:15:29 -0700589 retval = coredump_wait(siginfo->si_signo, &core_state);
Alex Kelly10c28d92012-09-26 21:52:08 -0400590 if (retval < 0)
591 goto fail_creds;
592
593 old_cred = override_creds(cred);
594
Oleg Nesterov12a2b4b2012-10-04 17:15:25 -0700595 ispipe = format_corename(&cn, &cprm);
Alex Kelly10c28d92012-09-26 21:52:08 -0400596
Lucas De Marchifb96c472013-04-30 15:28:06 -0700597 if (ispipe) {
Alex Kelly10c28d92012-09-26 21:52:08 -0400598 int dump_count;
599 char **helper_argv;
Lucas De Marchi907ed132013-04-30 15:28:07 -0700600 struct subprocess_info *sub_info;
Alex Kelly10c28d92012-09-26 21:52:08 -0400601
602 if (ispipe < 0) {
603 printk(KERN_WARNING "format_corename failed\n");
604 printk(KERN_WARNING "Aborting core\n");
Oleg Nesterove7fd1542013-07-03 15:08:16 -0700605 goto fail_unlock;
Alex Kelly10c28d92012-09-26 21:52:08 -0400606 }
607
608 if (cprm.limit == 1) {
609 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
610 *
611 * Normally core limits are irrelevant to pipes, since
612 * we're not writing to the file system, but we use
Bastien Nocerafcbc32b2015-02-05 14:35:05 +0100613 * cprm.limit of 1 here as a special value, this is a
Alex Kelly10c28d92012-09-26 21:52:08 -0400614 * consistent way to catch recursive crashes.
615 * We can still crash if the core_pattern binary sets
616 * RLIM_CORE = !1, but it runs as root, and can do
617 * lots of stupid things.
618 *
619 * Note that we use task_tgid_vnr here to grab the pid
620 * of the process group leader. That way we get the
621 * right pid if a thread in a multi-threaded
622 * core_pattern process dies.
623 */
624 printk(KERN_WARNING
625 "Process %d(%s) has RLIMIT_CORE set to 1\n",
626 task_tgid_vnr(current), current->comm);
627 printk(KERN_WARNING "Aborting core\n");
628 goto fail_unlock;
629 }
630 cprm.limit = RLIM_INFINITY;
631
632 dump_count = atomic_inc_return(&core_dump_count);
633 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
634 printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
635 task_tgid_vnr(current), current->comm);
636 printk(KERN_WARNING "Skipping core dump\n");
637 goto fail_dropcount;
638 }
639
Oleg Nesterov888ffc52013-07-03 15:08:23 -0700640 helper_argv = argv_split(GFP_KERNEL, cn.corename, NULL);
Alex Kelly10c28d92012-09-26 21:52:08 -0400641 if (!helper_argv) {
642 printk(KERN_WARNING "%s failed to allocate memory\n",
643 __func__);
644 goto fail_dropcount;
645 }
646
Lucas De Marchi907ed132013-04-30 15:28:07 -0700647 retval = -ENOMEM;
648 sub_info = call_usermodehelper_setup(helper_argv[0],
649 helper_argv, NULL, GFP_KERNEL,
650 umh_pipe_setup, NULL, &cprm);
651 if (sub_info)
652 retval = call_usermodehelper_exec(sub_info,
653 UMH_WAIT_EXEC);
654
Alex Kelly10c28d92012-09-26 21:52:08 -0400655 argv_free(helper_argv);
656 if (retval) {
Oleg Nesterov888ffc52013-07-03 15:08:23 -0700657 printk(KERN_INFO "Core dump to |%s pipe failed\n",
Alex Kelly10c28d92012-09-26 21:52:08 -0400658 cn.corename);
659 goto close_fail;
Lucas De Marchifb96c472013-04-30 15:28:06 -0700660 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400661 } else {
662 struct inode *inode;
Jann Horn378c6522016-03-22 14:25:36 -0700663 int open_flags = O_CREAT | O_RDWR | O_NOFOLLOW |
664 O_LARGEFILE | O_EXCL;
Alex Kelly10c28d92012-09-26 21:52:08 -0400665
666 if (cprm.limit < binfmt->min_coredump)
667 goto fail_unlock;
668
Jann Hornfbb18162015-09-09 15:38:28 -0700669 if (need_suid_safe && cn.corename[0] != '/') {
Alex Kelly10c28d92012-09-26 21:52:08 -0400670 printk(KERN_WARNING "Pid %d(%s) can only dump core "\
671 "to fully qualified path!\n",
672 task_tgid_vnr(current), current->comm);
673 printk(KERN_WARNING "Skipping core dump\n");
674 goto fail_unlock;
675 }
676
Jann Hornfbb18162015-09-09 15:38:28 -0700677 /*
678 * Unlink the file if it exists unless this is a SUID
679 * binary - in that case, we're running around with root
680 * privs and don't want to unlink another user's coredump.
681 */
682 if (!need_suid_safe) {
Jann Hornfbb18162015-09-09 15:38:28 -0700683 /*
684 * If it doesn't exist, that's fine. If there's some
685 * other problem, we'll catch it at the filp_open().
686 */
Christoph Hellwig96271652017-11-04 13:44:46 +0300687 do_unlinkat(AT_FDCWD, getname_kernel(cn.corename));
Jann Hornfbb18162015-09-09 15:38:28 -0700688 }
689
690 /*
691 * There is a race between unlinking and creating the
692 * file, but if that causes an EEXIST here, that's
693 * fine - another process raced with us while creating
694 * the corefile, and the other process won. To userspace,
695 * what matters is that at least one of the two processes
696 * writes its coredump successfully, not which one.
697 */
Jann Horn378c6522016-03-22 14:25:36 -0700698 if (need_suid_safe) {
699 /*
700 * Using user namespaces, normal user tasks can change
701 * their current->fs->root to point to arbitrary
702 * directories. Since the intention of the "only dump
703 * with a fully qualified path" rule is to control where
704 * coredumps may be placed using root privileges,
705 * current->fs->root must not be used. Instead, use the
706 * root directory of init_task.
707 */
708 struct path root;
709
710 task_lock(&init_task);
711 get_fs_root(init_task.fs, &root);
712 task_unlock(&init_task);
713 cprm.file = file_open_root(root.dentry, root.mnt,
714 cn.corename, open_flags, 0600);
715 path_put(&root);
716 } else {
717 cprm.file = filp_open(cn.corename, open_flags, 0600);
718 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400719 if (IS_ERR(cprm.file))
720 goto fail_unlock;
721
Al Viro496ad9a2013-01-23 17:07:38 -0500722 inode = file_inode(cprm.file);
Alex Kelly10c28d92012-09-26 21:52:08 -0400723 if (inode->i_nlink > 1)
724 goto close_fail;
725 if (d_unhashed(cprm.file->f_path.dentry))
726 goto close_fail;
727 /*
728 * AK: actually i see no reason to not allow this for named
729 * pipes etc, but keep the previous behaviour for now.
730 */
731 if (!S_ISREG(inode->i_mode))
732 goto close_fail;
733 /*
Jann Horn40f705a2015-09-09 15:38:30 -0700734 * Don't dump core if the filesystem changed owner or mode
735 * of the file during file creation. This is an issue when
736 * a process dumps core while its cwd is e.g. on a vfat
737 * filesystem.
Alex Kelly10c28d92012-09-26 21:52:08 -0400738 */
739 if (!uid_eq(inode->i_uid, current_fsuid()))
740 goto close_fail;
Jann Horn40f705a2015-09-09 15:38:30 -0700741 if ((inode->i_mode & 0677) != 0600)
742 goto close_fail;
Al Viro86cc0582015-04-03 15:23:17 -0400743 if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
Alex Kelly10c28d92012-09-26 21:52:08 -0400744 goto close_fail;
Greg Kroah-Hartman3b7ef782020-06-27 15:12:43 +0200745 if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
Alex Kelly10c28d92012-09-26 21:52:08 -0400746 goto close_fail;
747 }
748
749 /* get us an unshared descriptor table; almost always a no-op */
750 retval = unshare_files(&displaced);
751 if (retval)
752 goto close_fail;
753 if (displaced)
754 put_files_struct(displaced);
Al Viroe86d35c2013-05-04 14:45:54 -0400755 if (!dump_interrupted()) {
Luis Chamberlainf3a6bc02020-04-16 16:28:59 +0000756 /*
757 * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
758 * have this set to NULL.
759 */
760 if (!cprm.file) {
761 pr_info("Core dump to |%s disabled\n", cn.corename);
762 goto close_fail;
763 }
Al Viroe86d35c2013-05-04 14:45:54 -0400764 file_start_write(cprm.file);
765 core_dumped = binfmt->core_dump(&cprm);
766 file_end_write(cprm.file);
767 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400768 if (ispipe && core_pipe_limit)
769 wait_for_dump_helpers(cprm.file);
770close_fail:
771 if (cprm.file)
772 filp_close(cprm.file, NULL);
773fail_dropcount:
774 if (ispipe)
775 atomic_dec(&core_dump_count);
776fail_unlock:
777 kfree(cn.corename);
Oleg Nesterovacdedd92013-04-30 15:28:13 -0700778 coredump_finish(mm, core_dumped);
Alex Kelly10c28d92012-09-26 21:52:08 -0400779 revert_creds(old_cred);
780fail_creds:
781 put_cred(cred);
782fail:
783 return;
784}
785
786/*
787 * Core dumping helper functions. These are the only things you should
788 * do on a core-file: use only these functions to write out all the
789 * necessary info.
790 */
Al Viroecc8c772013-10-05 15:32:35 -0400791int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
792{
793 struct file *file = cprm->file;
Al Viro2507a4f2013-10-08 09:11:48 -0400794 loff_t pos = file->f_pos;
795 ssize_t n;
Omar Sandoval2c4cb042016-05-11 15:16:37 -0700796 if (cprm->written + nr > cprm->limit)
Al Viroecc8c772013-10-05 15:32:35 -0400797 return 0;
Al Viro2507a4f2013-10-08 09:11:48 -0400798 while (nr) {
799 if (dump_interrupted())
800 return 0;
Al Viro52da40a2013-11-15 21:58:33 -0500801 n = __kernel_write(file, addr, nr, &pos);
Al Viro2507a4f2013-10-08 09:11:48 -0400802 if (n <= 0)
803 return 0;
804 file->f_pos = pos;
Omar Sandoval2c4cb042016-05-11 15:16:37 -0700805 cprm->written += n;
Mateusz Guzik1607f092016-06-05 23:14:14 +0200806 cprm->pos += n;
Al Viro2507a4f2013-10-08 09:11:48 -0400807 nr -= n;
808 }
Al Viroecc8c772013-10-05 15:32:35 -0400809 return 1;
810}
811EXPORT_SYMBOL(dump_emit);
812
Al Viro9b56d542013-10-08 09:26:08 -0400813int dump_skip(struct coredump_params *cprm, size_t nr)
Alex Kelly10c28d92012-09-26 21:52:08 -0400814{
Al Viro9b56d542013-10-08 09:26:08 -0400815 static char zeroes[PAGE_SIZE];
816 struct file *file = cprm->file;
Alex Kelly10c28d92012-09-26 21:52:08 -0400817 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
Oleg Nesterov528f8272013-04-30 15:28:15 -0700818 if (dump_interrupted() ||
Al Viro9b56d542013-10-08 09:26:08 -0400819 file->f_op->llseek(file, nr, SEEK_CUR) < 0)
Alex Kelly10c28d92012-09-26 21:52:08 -0400820 return 0;
Mateusz Guzik1607f092016-06-05 23:14:14 +0200821 cprm->pos += nr;
Al Viro9b56d542013-10-08 09:26:08 -0400822 return 1;
Alex Kelly10c28d92012-09-26 21:52:08 -0400823 } else {
Al Viro9b56d542013-10-08 09:26:08 -0400824 while (nr > PAGE_SIZE) {
825 if (!dump_emit(cprm, zeroes, PAGE_SIZE))
826 return 0;
827 nr -= PAGE_SIZE;
Alex Kelly10c28d92012-09-26 21:52:08 -0400828 }
Al Viro9b56d542013-10-08 09:26:08 -0400829 return dump_emit(cprm, zeroes, nr);
Alex Kelly10c28d92012-09-26 21:52:08 -0400830 }
Alex Kelly10c28d92012-09-26 21:52:08 -0400831}
Al Viro9b56d542013-10-08 09:26:08 -0400832EXPORT_SYMBOL(dump_skip);
Al Viro22a8cb82013-10-08 11:05:01 -0400833
834int dump_align(struct coredump_params *cprm, int align)
835{
Mateusz Guzik1607f092016-06-05 23:14:14 +0200836 unsigned mod = cprm->pos & (align - 1);
Al Viro22a8cb82013-10-08 11:05:01 -0400837 if (align & (align - 1))
Al Virodb512422013-11-15 21:55:52 -0500838 return 0;
839 return mod ? dump_skip(cprm, align - mod) : 1;
Al Viro22a8cb82013-10-08 11:05:01 -0400840}
841EXPORT_SYMBOL(dump_align);
Dave Kleikamp4d22c752017-01-11 13:25:00 -0600842
843/*
844 * Ensures that file size is big enough to contain the current file
845 * postion. This prevents gdb from complaining about a truncated file
846 * if the last "write" to the file was dump_skip.
847 */
848void dump_truncate(struct coredump_params *cprm)
849{
850 struct file *file = cprm->file;
851 loff_t offset;
852
853 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
854 offset = file->f_op->llseek(file, 0, SEEK_CUR);
855 if (i_size_read(file->f_mapping->host) < offset)
856 do_truncate(file->f_path.dentry, offset, 0, file);
857 }
858}
859EXPORT_SYMBOL(dump_truncate);