kill coredump_params->regs
[linux-2.6-block.git] / fs / coredump.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
10c28d93
AK
2#include <linux/slab.h>
3#include <linux/file.h>
4#include <linux/fdtable.h>
70d78fe7 5#include <linux/freezer.h>
10c28d93
AK
6#include <linux/mm.h>
7#include <linux/stat.h>
8#include <linux/fcntl.h>
9#include <linux/swap.h>
315c6926 10#include <linux/ctype.h>
10c28d93
AK
11#include <linux/string.h>
12#include <linux/init.h>
13#include <linux/pagemap.h>
14#include <linux/perf_event.h>
15#include <linux/highmem.h>
16#include <linux/spinlock.h>
17#include <linux/key.h>
18#include <linux/personality.h>
19#include <linux/binfmts.h>
179899fd 20#include <linux/coredump.h>
f7ccbae4 21#include <linux/sched/coredump.h>
3f07c014 22#include <linux/sched/signal.h>
68db0cf1 23#include <linux/sched/task_stack.h>
10c28d93
AK
24#include <linux/utsname.h>
25#include <linux/pid_namespace.h>
26#include <linux/module.h>
27#include <linux/namei.h>
28#include <linux/mount.h>
29#include <linux/security.h>
30#include <linux/syscalls.h>
31#include <linux/tsacct_kern.h>
32#include <linux/cn_proc.h>
33#include <linux/audit.h>
10c28d93
AK
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>
378c6520
JH
40#include <linux/fs.h>
41#include <linux/path.h>
03927c8a 42#include <linux/timekeeping.h>
f0bc21b2 43#include <linux/sysctl.h>
84158b7f 44#include <linux/elf.h>
10c28d93 45
7c0f6ba6 46#include <linux/uaccess.h>
10c28d93
AK
47#include <asm/mmu_context.h>
48#include <asm/tlb.h>
49#include <asm/exec.h>
50
51#include <trace/events/task.h>
52#include "internal.h"
53
54#include <trace/events/sched.h>
55
95c5436a 56static bool dump_vma_snapshot(struct coredump_params *cprm);
390031c9 57static void free_vma_snapshot(struct coredump_params *cprm);
95c5436a 58
f0bc21b2
XN
59static int core_uses_pid;
60static unsigned int core_pipe_limit;
61static char core_pattern[CORENAME_MAX_SIZE] = "core";
3ceadcf6 62static int core_name_size = CORENAME_MAX_SIZE;
10c28d93
AK
63
64struct core_name {
65 char *corename;
66 int used, size;
67};
10c28d93 68
3ceadcf6 69static int expand_corename(struct core_name *cn, int size)
10c28d93 70{
e7fd1549 71 char *corename = krealloc(cn->corename, size, GFP_KERNEL);
10c28d93 72
e7fd1549 73 if (!corename)
10c28d93 74 return -ENOMEM;
10c28d93 75
3ceadcf6
ON
76 if (size > core_name_size) /* racy but harmless */
77 core_name_size = size;
78
79 cn->size = ksize(corename);
e7fd1549 80 cn->corename = corename;
10c28d93
AK
81 return 0;
82}
83
b4176b7c
NI
84static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt,
85 va_list arg)
10c28d93 86{
5fe9d8ca 87 int free, need;
404ca80e 88 va_list arg_copy;
10c28d93 89
5fe9d8ca
ON
90again:
91 free = cn->size - cn->used;
404ca80e
ED
92
93 va_copy(arg_copy, arg);
94 need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
95 va_end(arg_copy);
96
5fe9d8ca
ON
97 if (need < free) {
98 cn->used += need;
99 return 0;
100 }
10c28d93 101
3ceadcf6 102 if (!expand_corename(cn, cn->size + need - free + 1))
5fe9d8ca 103 goto again;
10c28d93 104
5fe9d8ca 105 return -ENOMEM;
10c28d93
AK
106}
107
b4176b7c 108static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...)
bc03c691
ON
109{
110 va_list arg;
111 int ret;
112
113 va_start(arg, fmt);
114 ret = cn_vprintf(cn, fmt, arg);
115 va_end(arg);
116
117 return ret;
118}
119
b4176b7c
NI
120static __printf(2, 3)
121int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
10c28d93 122{
923bed03
ON
123 int cur = cn->used;
124 va_list arg;
125 int ret;
126
127 va_start(arg, fmt);
128 ret = cn_vprintf(cn, fmt, arg);
129 va_end(arg);
130
ac94b6e3
JH
131 if (ret == 0) {
132 /*
133 * Ensure that this coredump name component can't cause the
134 * resulting corefile path to consist of a ".." or ".".
135 */
136 if ((cn->used - cur == 1 && cn->corename[cur] == '.') ||
137 (cn->used - cur == 2 && cn->corename[cur] == '.'
138 && cn->corename[cur+1] == '.'))
139 cn->corename[cur] = '!';
140
141 /*
142 * Empty names are fishy and could be used to create a "//" in a
143 * corefile name, causing the coredump to happen one directory
144 * level too high. Enforce that all components of the core
145 * pattern are at least one character long.
146 */
147 if (cn->used == cur)
148 ret = cn_printf(cn, "!");
149 }
150
923bed03
ON
151 for (; cur < cn->used; ++cur) {
152 if (cn->corename[cur] == '/')
153 cn->corename[cur] = '!';
154 }
155 return ret;
10c28d93
AK
156}
157
f38c85f1 158static int cn_print_exe_file(struct core_name *cn, bool name_only)
10c28d93
AK
159{
160 struct file *exe_file;
f38c85f1 161 char *pathbuf, *path, *ptr;
10c28d93
AK
162 int ret;
163
164 exe_file = get_mm_exe_file(current->mm);
923bed03
ON
165 if (!exe_file)
166 return cn_esc_printf(cn, "%s (path unknown)", current->comm);
10c28d93 167
0ee931c4 168 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
10c28d93
AK
169 if (!pathbuf) {
170 ret = -ENOMEM;
171 goto put_exe_file;
172 }
173
9bf39ab2 174 path = file_path(exe_file, pathbuf, PATH_MAX);
10c28d93
AK
175 if (IS_ERR(path)) {
176 ret = PTR_ERR(path);
177 goto free_buf;
178 }
179
f38c85f1
LW
180 if (name_only) {
181 ptr = strrchr(path, '/');
182 if (ptr)
183 path = ptr + 1;
184 }
923bed03 185 ret = cn_esc_printf(cn, "%s", path);
10c28d93
AK
186
187free_buf:
188 kfree(pathbuf);
189put_exe_file:
190 fput(exe_file);
191 return ret;
192}
193
194/* format_corename will inspect the pattern parameter, and output a
195 * name into corename, which must have space for at least
196 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
197 */
315c6926
PW
198static int format_corename(struct core_name *cn, struct coredump_params *cprm,
199 size_t **argv, int *argc)
10c28d93
AK
200{
201 const struct cred *cred = current_cred();
202 const char *pat_ptr = core_pattern;
203 int ispipe = (*pat_ptr == '|');
315c6926 204 bool was_space = false;
10c28d93
AK
205 int pid_in_pattern = 0;
206 int err = 0;
207
e7fd1549 208 cn->used = 0;
3ceadcf6
ON
209 cn->corename = NULL;
210 if (expand_corename(cn, core_name_size))
10c28d93 211 return -ENOMEM;
888ffc59
ON
212 cn->corename[0] = '\0';
213
315c6926
PW
214 if (ispipe) {
215 int argvs = sizeof(core_pattern) / 2;
216 (*argv) = kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL);
217 if (!(*argv))
218 return -ENOMEM;
219 (*argv)[(*argc)++] = 0;
888ffc59 220 ++pat_ptr;
db973a72
SM
221 if (!(*pat_ptr))
222 return -ENOMEM;
315c6926 223 }
10c28d93
AK
224
225 /* Repeat as long as we have more pattern to process and more output
226 space */
227 while (*pat_ptr) {
315c6926
PW
228 /*
229 * Split on spaces before doing template expansion so that
230 * %e and %E don't get split if they have spaces in them
231 */
232 if (ispipe) {
233 if (isspace(*pat_ptr)) {
2bf509d9
MD
234 if (cn->used != 0)
235 was_space = true;
315c6926
PW
236 pat_ptr++;
237 continue;
238 } else if (was_space) {
239 was_space = false;
240 err = cn_printf(cn, "%c", '\0');
241 if (err)
242 return err;
243 (*argv)[(*argc)++] = cn->used;
244 }
245 }
10c28d93 246 if (*pat_ptr != '%') {
10c28d93
AK
247 err = cn_printf(cn, "%c", *pat_ptr++);
248 } else {
249 switch (*++pat_ptr) {
250 /* single % at the end, drop that */
251 case 0:
252 goto out;
253 /* Double percent, output one percent */
254 case '%':
255 err = cn_printf(cn, "%c", '%');
256 break;
257 /* pid */
258 case 'p':
259 pid_in_pattern = 1;
260 err = cn_printf(cn, "%d",
261 task_tgid_vnr(current));
262 break;
65aafb1e
SG
263 /* global pid */
264 case 'P':
265 err = cn_printf(cn, "%d",
266 task_tgid_nr(current));
267 break;
b03023ec
ON
268 case 'i':
269 err = cn_printf(cn, "%d",
270 task_pid_vnr(current));
271 break;
272 case 'I':
273 err = cn_printf(cn, "%d",
274 task_pid_nr(current));
275 break;
10c28d93
AK
276 /* uid */
277 case 'u':
5202efe5
NI
278 err = cn_printf(cn, "%u",
279 from_kuid(&init_user_ns,
280 cred->uid));
10c28d93
AK
281 break;
282 /* gid */
283 case 'g':
5202efe5
NI
284 err = cn_printf(cn, "%u",
285 from_kgid(&init_user_ns,
286 cred->gid));
10c28d93 287 break;
12a2b4b2
ON
288 case 'd':
289 err = cn_printf(cn, "%d",
290 __get_dumpable(cprm->mm_flags));
291 break;
10c28d93
AK
292 /* signal that caused the coredump */
293 case 's':
b4176b7c
NI
294 err = cn_printf(cn, "%d",
295 cprm->siginfo->si_signo);
10c28d93
AK
296 break;
297 /* UNIX time of coredump */
298 case 't': {
03927c8a
AB
299 time64_t time;
300
301 time = ktime_get_real_seconds();
302 err = cn_printf(cn, "%lld", time);
10c28d93
AK
303 break;
304 }
305 /* hostname */
923bed03 306 case 'h':
10c28d93 307 down_read(&uts_sem);
923bed03 308 err = cn_esc_printf(cn, "%s",
10c28d93
AK
309 utsname()->nodename);
310 up_read(&uts_sem);
10c28d93 311 break;
f38c85f1 312 /* executable, could be changed by prctl PR_SET_NAME etc */
923bed03
ON
313 case 'e':
314 err = cn_esc_printf(cn, "%s", current->comm);
10c28d93 315 break;
f38c85f1
LW
316 /* file name of executable */
317 case 'f':
318 err = cn_print_exe_file(cn, true);
319 break;
10c28d93 320 case 'E':
f38c85f1 321 err = cn_print_exe_file(cn, false);
10c28d93
AK
322 break;
323 /* core limit size */
324 case 'c':
325 err = cn_printf(cn, "%lu",
326 rlimit(RLIMIT_CORE));
327 break;
328 default:
329 break;
330 }
331 ++pat_ptr;
332 }
333
334 if (err)
335 return err;
336 }
337
888ffc59 338out:
10c28d93
AK
339 /* Backward compatibility with core_uses_pid:
340 *
341 * If core_pattern does not include a %p (as is the default)
342 * and core_uses_pid is set, then .%pid will be appended to
343 * the filename. Do not do this for piped commands. */
344 if (!ispipe && !pid_in_pattern && core_uses_pid) {
345 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
346 if (err)
347 return err;
348 }
10c28d93
AK
349 return ispipe;
350}
351
752dc970 352static int zap_process(struct task_struct *start, int exit_code)
10c28d93
AK
353{
354 struct task_struct *t;
355 int nr = 0;
356
9a95f78e 357 /* Allow SIGKILL, see prepare_signal() */
2f824d4d 358 start->signal->flags = SIGNAL_GROUP_EXIT;
10c28d93
AK
359 start->signal->group_exit_code = exit_code;
360 start->signal->group_stop_count = 0;
361
d61ba589 362 for_each_thread(start, t) {
10c28d93 363 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
92307383 364 if (t != current && !(t->flags & PF_POSTCOREDUMP)) {
10c28d93
AK
365 sigaddset(&t->pending.signal, SIGKILL);
366 signal_wake_up(t, 1);
367 nr++;
368 }
d61ba589 369 }
10c28d93
AK
370
371 return nr;
372}
373
0258b5fd 374static int zap_threads(struct task_struct *tsk,
403bad72 375 struct core_state *core_state, int exit_code)
10c28d93 376{
49697335 377 struct signal_struct *signal = tsk->signal;
10c28d93
AK
378 int nr = -EAGAIN;
379
380 spin_lock_irq(&tsk->sighand->siglock);
49697335
EB
381 if (!(signal->flags & SIGNAL_GROUP_EXIT) && !signal->group_exec_task) {
382 signal->core_state = core_state;
752dc970 383 nr = zap_process(tsk, exit_code);
403bad72 384 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
0258b5fd
EB
385 tsk->flags |= PF_DUMPCORE;
386 atomic_set(&core_state->nr_threads, nr);
10c28d93
AK
387 }
388 spin_unlock_irq(&tsk->sighand->siglock);
10c28d93
AK
389 return nr;
390}
391
392static int coredump_wait(int exit_code, struct core_state *core_state)
393{
394 struct task_struct *tsk = current;
10c28d93
AK
395 int core_waiters = -EBUSY;
396
397 init_completion(&core_state->startup);
398 core_state->dumper.task = tsk;
399 core_state->dumper.next = NULL;
400
0258b5fd 401 core_waiters = zap_threads(tsk, core_state, exit_code);
10c28d93
AK
402 if (core_waiters > 0) {
403 struct core_thread *ptr;
404
f5d39b02
PZ
405 wait_for_completion_state(&core_state->startup,
406 TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
10c28d93
AK
407 /*
408 * Wait for all the threads to become inactive, so that
409 * all the thread context (extended register state, like
410 * fpu etc) gets copied to the memory.
411 */
412 ptr = core_state->dumper.next;
413 while (ptr != NULL) {
f9fc8cad 414 wait_task_inactive(ptr->task, TASK_ANY);
10c28d93
AK
415 ptr = ptr->next;
416 }
417 }
418
419 return core_waiters;
420}
421
0258b5fd 422static void coredump_finish(bool core_dumped)
10c28d93
AK
423{
424 struct core_thread *curr, *next;
425 struct task_struct *task;
426
6cd8f0ac 427 spin_lock_irq(&current->sighand->siglock);
acdedd99
ON
428 if (core_dumped && !__fatal_signal_pending(current))
429 current->signal->group_exit_code |= 0x80;
0258b5fd
EB
430 next = current->signal->core_state->dumper.next;
431 current->signal->core_state = NULL;
6cd8f0ac
ON
432 spin_unlock_irq(&current->sighand->siglock);
433
10c28d93
AK
434 while ((curr = next) != NULL) {
435 next = curr->next;
436 task = curr->task;
437 /*
92307383 438 * see coredump_task_exit(), curr->task must not see
10c28d93
AK
439 * ->task == NULL before we read ->next.
440 */
441 smp_mb();
442 curr->task = NULL;
443 wake_up_process(task);
444 }
10c28d93
AK
445}
446
528f827e
ON
447static bool dump_interrupted(void)
448{
449 /*
450 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
451 * can do try_to_freeze() and check __fatal_signal_pending(),
452 * but then we need to teach dump_write() to restart and clear
453 * TIF_SIGPENDING.
454 */
06af8679 455 return fatal_signal_pending(current) || freezing(current);
528f827e
ON
456}
457
10c28d93
AK
458static void wait_for_dump_helpers(struct file *file)
459{
de32ec4c 460 struct pipe_inode_info *pipe = file->private_data;
10c28d93
AK
461
462 pipe_lock(pipe);
463 pipe->readers++;
464 pipe->writers--;
0ddad21d 465 wake_up_interruptible_sync(&pipe->rd_wait);
dc7ee2aa
ON
466 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
467 pipe_unlock(pipe);
10c28d93 468
dc7ee2aa
ON
469 /*
470 * We actually want wait_event_freezable() but then we need
471 * to clear TIF_SIGPENDING and improve dump_interrupted().
472 */
0ddad21d 473 wait_event_interruptible(pipe->rd_wait, pipe->readers == 1);
10c28d93 474
dc7ee2aa 475 pipe_lock(pipe);
10c28d93
AK
476 pipe->readers--;
477 pipe->writers++;
478 pipe_unlock(pipe);
10c28d93
AK
479}
480
481/*
482 * umh_pipe_setup
483 * helper function to customize the process used
484 * to collect the core in userspace. Specifically
485 * it sets up a pipe and installs it as fd 0 (stdin)
486 * for the process. Returns 0 on success, or
487 * PTR_ERR on failure.
488 * Note that it also sets the core limit to 1. This
489 * is a special value that we use to trap recursive
490 * core dumps
491 */
492static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
493{
494 struct file *files[2];
495 struct coredump_params *cp = (struct coredump_params *)info->data;
496 int err = create_pipe_files(files, 0);
497 if (err)
498 return err;
499
500 cp->file = files[1];
501
45525b26
AV
502 err = replace_fd(0, files[0], 0);
503 fput(files[0]);
10c28d93
AK
504 /* and disallow core files too */
505 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
506
45525b26 507 return err;
10c28d93
AK
508}
509
ae7795bc 510void do_coredump(const kernel_siginfo_t *siginfo)
10c28d93
AK
511{
512 struct core_state core_state;
513 struct core_name cn;
514 struct mm_struct *mm = current->mm;
515 struct linux_binfmt * binfmt;
516 const struct cred *old_cred;
517 struct cred *cred;
518 int retval = 0;
10c28d93 519 int ispipe;
315c6926
PW
520 size_t *argv = NULL;
521 int argc = 0;
fbb18169
JH
522 /* require nonrelative corefile path and be extra careful */
523 bool need_suid_safe = false;
acdedd99 524 bool core_dumped = false;
10c28d93
AK
525 static atomic_t core_dump_count = ATOMIC_INIT(0);
526 struct coredump_params cprm = {
5ab1c309 527 .siginfo = siginfo,
10c28d93
AK
528 .limit = rlimit(RLIMIT_CORE),
529 /*
530 * We must use the same mm->flags while dumping core to avoid
531 * inconsistency of bit flags, since this flag is not protected
532 * by any locks.
533 */
534 .mm_flags = mm->flags,
95c5436a 535 .vma_meta = NULL,
10c28d93
AK
536 };
537
5ab1c309 538 audit_core_dumps(siginfo->si_signo);
10c28d93
AK
539
540 binfmt = mm->binfmt;
541 if (!binfmt || !binfmt->core_dump)
542 goto fail;
543 if (!__get_dumpable(cprm.mm_flags))
544 goto fail;
545
546 cred = prepare_creds();
547 if (!cred)
548 goto fail;
549 /*
550 * We cannot trust fsuid as being the "true" uid of the process
551 * nor do we know its entire history. We only know it was tainted
552 * so we dump it as root in mode 2, and only into a controlled
553 * environment (pipe handler or fully qualified path).
554 */
e579d2c2 555 if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
10c28d93 556 /* Setuid core dump mode */
10c28d93 557 cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
fbb18169 558 need_suid_safe = true;
10c28d93
AK
559 }
560
5ab1c309 561 retval = coredump_wait(siginfo->si_signo, &core_state);
10c28d93
AK
562 if (retval < 0)
563 goto fail_creds;
564
565 old_cred = override_creds(cred);
566
315c6926 567 ispipe = format_corename(&cn, &cprm, &argv, &argc);
10c28d93 568
fb96c475 569 if (ispipe) {
315c6926 570 int argi;
10c28d93
AK
571 int dump_count;
572 char **helper_argv;
907ed132 573 struct subprocess_info *sub_info;
10c28d93
AK
574
575 if (ispipe < 0) {
576 printk(KERN_WARNING "format_corename failed\n");
577 printk(KERN_WARNING "Aborting core\n");
e7fd1549 578 goto fail_unlock;
10c28d93
AK
579 }
580
581 if (cprm.limit == 1) {
582 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
583 *
584 * Normally core limits are irrelevant to pipes, since
585 * we're not writing to the file system, but we use
fcbc32bc 586 * cprm.limit of 1 here as a special value, this is a
10c28d93
AK
587 * consistent way to catch recursive crashes.
588 * We can still crash if the core_pattern binary sets
589 * RLIM_CORE = !1, but it runs as root, and can do
590 * lots of stupid things.
591 *
592 * Note that we use task_tgid_vnr here to grab the pid
593 * of the process group leader. That way we get the
594 * right pid if a thread in a multi-threaded
595 * core_pattern process dies.
596 */
597 printk(KERN_WARNING
598 "Process %d(%s) has RLIMIT_CORE set to 1\n",
599 task_tgid_vnr(current), current->comm);
600 printk(KERN_WARNING "Aborting core\n");
601 goto fail_unlock;
602 }
603 cprm.limit = RLIM_INFINITY;
604
605 dump_count = atomic_inc_return(&core_dump_count);
606 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
607 printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
608 task_tgid_vnr(current), current->comm);
609 printk(KERN_WARNING "Skipping core dump\n");
610 goto fail_dropcount;
611 }
612
315c6926
PW
613 helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv),
614 GFP_KERNEL);
10c28d93
AK
615 if (!helper_argv) {
616 printk(KERN_WARNING "%s failed to allocate memory\n",
617 __func__);
618 goto fail_dropcount;
619 }
315c6926
PW
620 for (argi = 0; argi < argc; argi++)
621 helper_argv[argi] = cn.corename + argv[argi];
622 helper_argv[argi] = NULL;
10c28d93 623
907ed132
LDM
624 retval = -ENOMEM;
625 sub_info = call_usermodehelper_setup(helper_argv[0],
626 helper_argv, NULL, GFP_KERNEL,
627 umh_pipe_setup, NULL, &cprm);
628 if (sub_info)
629 retval = call_usermodehelper_exec(sub_info,
630 UMH_WAIT_EXEC);
631
315c6926 632 kfree(helper_argv);
10c28d93 633 if (retval) {
888ffc59 634 printk(KERN_INFO "Core dump to |%s pipe failed\n",
10c28d93
AK
635 cn.corename);
636 goto close_fail;
fb96c475 637 }
10c28d93 638 } else {
643fe55a 639 struct user_namespace *mnt_userns;
10c28d93 640 struct inode *inode;
378c6520
JH
641 int open_flags = O_CREAT | O_RDWR | O_NOFOLLOW |
642 O_LARGEFILE | O_EXCL;
10c28d93
AK
643
644 if (cprm.limit < binfmt->min_coredump)
645 goto fail_unlock;
646
fbb18169 647 if (need_suid_safe && cn.corename[0] != '/') {
10c28d93
AK
648 printk(KERN_WARNING "Pid %d(%s) can only dump core "\
649 "to fully qualified path!\n",
650 task_tgid_vnr(current), current->comm);
651 printk(KERN_WARNING "Skipping core dump\n");
652 goto fail_unlock;
653 }
654
fbb18169
JH
655 /*
656 * Unlink the file if it exists unless this is a SUID
657 * binary - in that case, we're running around with root
658 * privs and don't want to unlink another user's coredump.
659 */
660 if (!need_suid_safe) {
fbb18169
JH
661 /*
662 * If it doesn't exist, that's fine. If there's some
663 * other problem, we'll catch it at the filp_open().
664 */
96271654 665 do_unlinkat(AT_FDCWD, getname_kernel(cn.corename));
fbb18169
JH
666 }
667
668 /*
669 * There is a race between unlinking and creating the
670 * file, but if that causes an EEXIST here, that's
671 * fine - another process raced with us while creating
672 * the corefile, and the other process won. To userspace,
673 * what matters is that at least one of the two processes
674 * writes its coredump successfully, not which one.
675 */
378c6520
JH
676 if (need_suid_safe) {
677 /*
678 * Using user namespaces, normal user tasks can change
679 * their current->fs->root to point to arbitrary
680 * directories. Since the intention of the "only dump
681 * with a fully qualified path" rule is to control where
682 * coredumps may be placed using root privileges,
683 * current->fs->root must not be used. Instead, use the
684 * root directory of init_task.
685 */
686 struct path root;
687
688 task_lock(&init_task);
689 get_fs_root(init_task.fs, &root);
690 task_unlock(&init_task);
ffb37ca3
AV
691 cprm.file = file_open_root(&root, cn.corename,
692 open_flags, 0600);
378c6520
JH
693 path_put(&root);
694 } else {
695 cprm.file = filp_open(cn.corename, open_flags, 0600);
696 }
10c28d93
AK
697 if (IS_ERR(cprm.file))
698 goto fail_unlock;
699
496ad9aa 700 inode = file_inode(cprm.file);
10c28d93
AK
701 if (inode->i_nlink > 1)
702 goto close_fail;
703 if (d_unhashed(cprm.file->f_path.dentry))
704 goto close_fail;
705 /*
706 * AK: actually i see no reason to not allow this for named
707 * pipes etc, but keep the previous behaviour for now.
708 */
709 if (!S_ISREG(inode->i_mode))
710 goto close_fail;
711 /*
40f705a7
JH
712 * Don't dump core if the filesystem changed owner or mode
713 * of the file during file creation. This is an issue when
714 * a process dumps core while its cwd is e.g. on a vfat
715 * filesystem.
10c28d93 716 */
643fe55a 717 mnt_userns = file_mnt_user_ns(cprm.file);
dbd9d6f8
DO
718 if (!uid_eq(i_uid_into_mnt(mnt_userns, inode),
719 current_fsuid())) {
720 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file owner\n",
721 cn.corename);
10c28d93 722 goto close_fail;
dbd9d6f8
DO
723 }
724 if ((inode->i_mode & 0677) != 0600) {
725 pr_info_ratelimited("Core dump to %s aborted: cannot preserve file permissions\n",
726 cn.corename);
40f705a7 727 goto close_fail;
dbd9d6f8 728 }
86cc0584 729 if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
10c28d93 730 goto close_fail;
643fe55a
CB
731 if (do_truncate(mnt_userns, cprm.file->f_path.dentry,
732 0, 0, cprm.file))
10c28d93
AK
733 goto close_fail;
734 }
735
736 /* get us an unshared descriptor table; almost always a no-op */
c39ab6de 737 /* The cell spufs coredump code reads the file descriptor tables */
1f702603 738 retval = unshare_files();
10c28d93
AK
739 if (retval)
740 goto close_fail;
e86d35c3 741 if (!dump_interrupted()) {
3740d93e
LC
742 /*
743 * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
744 * have this set to NULL.
745 */
746 if (!cprm.file) {
747 pr_info("Core dump to |%s disabled\n", cn.corename);
748 goto close_fail;
749 }
95c5436a
EB
750 if (!dump_vma_snapshot(&cprm))
751 goto close_fail;
752
e86d35c3
AV
753 file_start_write(cprm.file);
754 core_dumped = binfmt->core_dump(&cprm);
d0f1088b
AV
755 /*
756 * Ensures that file size is big enough to contain the current
757 * file postion. This prevents gdb from complaining about
758 * a truncated file if the last "write" to the file was
759 * dump_skip.
760 */
761 if (cprm.to_skip) {
762 cprm.to_skip--;
763 dump_emit(&cprm, "", 1);
764 }
e86d35c3 765 file_end_write(cprm.file);
390031c9 766 free_vma_snapshot(&cprm);
e86d35c3 767 }
10c28d93
AK
768 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:
315c6926 777 kfree(argv);
10c28d93 778 kfree(cn.corename);
0258b5fd 779 coredump_finish(core_dumped);
10c28d93
AK
780 revert_creds(old_cred);
781fail_creds:
782 put_cred(cred);
783fail:
784 return;
785}
786
787/*
788 * Core dumping helper functions. These are the only things you should
789 * do on a core-file: use only these functions to write out all the
790 * necessary info.
791 */
d0f1088b 792static int __dump_emit(struct coredump_params *cprm, const void *addr, int nr)
ecc8c772
AV
793{
794 struct file *file = cprm->file;
2507a4fb
AV
795 loff_t pos = file->f_pos;
796 ssize_t n;
2c4cb043 797 if (cprm->written + nr > cprm->limit)
ecc8c772 798 return 0;
df0c09c0
JH
799
800
801 if (dump_interrupted())
802 return 0;
803 n = __kernel_write(file, addr, nr, &pos);
804 if (n != nr)
805 return 0;
806 file->f_pos = pos;
807 cprm->written += n;
808 cprm->pos += n;
809
ecc8c772
AV
810 return 1;
811}
ecc8c772 812
d0f1088b 813static int __dump_skip(struct coredump_params *cprm, size_t nr)
10c28d93 814{
9b56d543
AV
815 static char zeroes[PAGE_SIZE];
816 struct file *file = cprm->file;
4e3299ea 817 if (file->f_mode & FMODE_LSEEK) {
528f827e 818 if (dump_interrupted() ||
4e3299ea 819 vfs_llseek(file, nr, SEEK_CUR) < 0)
10c28d93 820 return 0;
1607f09c 821 cprm->pos += nr;
9b56d543 822 return 1;
10c28d93 823 } else {
9b56d543 824 while (nr > PAGE_SIZE) {
d0f1088b 825 if (!__dump_emit(cprm, zeroes, PAGE_SIZE))
9b56d543
AV
826 return 0;
827 nr -= PAGE_SIZE;
10c28d93 828 }
d0f1088b 829 return __dump_emit(cprm, zeroes, nr);
10c28d93 830 }
10c28d93 831}
d0f1088b 832
06bbaa6d
AV
833static int dump_emit_page(struct coredump_params *cprm, struct page *page)
834{
835 struct bio_vec bvec = {
836 .bv_page = page,
837 .bv_offset = 0,
838 .bv_len = PAGE_SIZE,
839 };
840 struct iov_iter iter;
841 struct file *file = cprm->file;
4f526fef 842 loff_t pos;
06bbaa6d
AV
843 ssize_t n;
844
845 if (cprm->to_skip) {
846 if (!__dump_skip(cprm, cprm->to_skip))
847 return 0;
848 cprm->to_skip = 0;
849 }
850 if (cprm->written + PAGE_SIZE > cprm->limit)
851 return 0;
852 if (dump_interrupted())
853 return 0;
4f526fef 854 pos = file->f_pos;
06bbaa6d
AV
855 iov_iter_bvec(&iter, WRITE, &bvec, 1, PAGE_SIZE);
856 n = __kernel_write_iter(cprm->file, &iter, &pos);
857 if (n != PAGE_SIZE)
858 return 0;
859 file->f_pos = pos;
860 cprm->written += PAGE_SIZE;
861 cprm->pos += PAGE_SIZE;
862
863 return 1;
864}
865
d0f1088b
AV
866int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
867{
868 if (cprm->to_skip) {
869 if (!__dump_skip(cprm, cprm->to_skip))
870 return 0;
871 cprm->to_skip = 0;
872 }
873 return __dump_emit(cprm, addr, nr);
874}
875EXPORT_SYMBOL(dump_emit);
876
877void dump_skip_to(struct coredump_params *cprm, unsigned long pos)
878{
879 cprm->to_skip = pos - cprm->pos;
880}
881EXPORT_SYMBOL(dump_skip_to);
882
883void dump_skip(struct coredump_params *cprm, size_t nr)
884{
885 cprm->to_skip += nr;
886}
9b56d543 887EXPORT_SYMBOL(dump_skip);
22a8cb82 888
afc63a97
JH
889#ifdef CONFIG_ELF_CORE
890int dump_user_range(struct coredump_params *cprm, unsigned long start,
891 unsigned long len)
892{
893 unsigned long addr;
894
895 for (addr = start; addr < start + len; addr += PAGE_SIZE) {
896 struct page *page;
afc63a97
JH
897
898 /*
899 * To avoid having to allocate page tables for virtual address
900 * ranges that have never been used yet, and also to make it
901 * easy to generate sparse core files, use a helper that returns
902 * NULL when encountering an empty page table entry that would
903 * otherwise have been filled with the zero page.
904 */
905 page = get_dump_page(addr);
906 if (page) {
06bbaa6d 907 int stop = !dump_emit_page(cprm, page);
afc63a97 908 put_page(page);
d0f1088b
AV
909 if (stop)
910 return 0;
afc63a97 911 } else {
d0f1088b 912 dump_skip(cprm, PAGE_SIZE);
afc63a97 913 }
afc63a97
JH
914 }
915 return 1;
916}
917#endif
918
22a8cb82
AV
919int dump_align(struct coredump_params *cprm, int align)
920{
d0f1088b 921 unsigned mod = (cprm->pos + cprm->to_skip) & (align - 1);
22a8cb82 922 if (align & (align - 1))
db51242d 923 return 0;
d0f1088b
AV
924 if (mod)
925 cprm->to_skip += align - mod;
926 return 1;
22a8cb82
AV
927}
928EXPORT_SYMBOL(dump_align);
4d22c75d 929
f0bc21b2
XN
930#ifdef CONFIG_SYSCTL
931
932void validate_coredump_safety(void)
933{
934 if (suid_dumpable == SUID_DUMP_ROOT &&
935 core_pattern[0] != '/' && core_pattern[0] != '|') {
936 pr_warn(
937"Unsafe core_pattern used with fs.suid_dumpable=2.\n"
938"Pipe handler or fully qualified core dump path required.\n"
939"Set kernel.core_pattern before fs.suid_dumpable.\n"
940 );
941 }
942}
943
944static int proc_dostring_coredump(struct ctl_table *table, int write,
945 void *buffer, size_t *lenp, loff_t *ppos)
946{
947 int error = proc_dostring(table, write, buffer, lenp, ppos);
948
949 if (!error)
950 validate_coredump_safety();
951 return error;
952}
953
954static struct ctl_table coredump_sysctls[] = {
955 {
956 .procname = "core_uses_pid",
957 .data = &core_uses_pid,
958 .maxlen = sizeof(int),
959 .mode = 0644,
960 .proc_handler = proc_dointvec,
961 },
962 {
963 .procname = "core_pattern",
964 .data = core_pattern,
965 .maxlen = CORENAME_MAX_SIZE,
966 .mode = 0644,
967 .proc_handler = proc_dostring_coredump,
968 },
969 {
970 .procname = "core_pipe_limit",
971 .data = &core_pipe_limit,
972 .maxlen = sizeof(unsigned int),
973 .mode = 0644,
974 .proc_handler = proc_dointvec,
975 },
976 { }
977};
978
979static int __init init_fs_coredump_sysctls(void)
980{
981 register_sysctl_init("kernel", coredump_sysctls);
982 return 0;
983}
984fs_initcall(init_fs_coredump_sysctls);
985#endif /* CONFIG_SYSCTL */
986
429a22e7
JH
987/*
988 * The purpose of always_dump_vma() is to make sure that special kernel mappings
989 * that are useful for post-mortem analysis are included in every core dump.
990 * In that way we ensure that the core dump is fully interpretable later
991 * without matching up the same kernel and hardware config to see what PC values
992 * meant. These special mappings include - vDSO, vsyscall, and other
993 * architecture specific mappings
994 */
995static bool always_dump_vma(struct vm_area_struct *vma)
996{
997 /* Any vsyscall mappings? */
998 if (vma == get_gate_vma(vma->vm_mm))
999 return true;
1000
1001 /*
1002 * Assume that all vmas with a .name op should always be dumped.
1003 * If this changes, a new vm_ops field can easily be added.
1004 */
1005 if (vma->vm_ops && vma->vm_ops->name && vma->vm_ops->name(vma))
1006 return true;
1007
1008 /*
1009 * arch_vma_name() returns non-NULL for special architecture mappings,
1010 * such as vDSO sections.
1011 */
1012 if (arch_vma_name(vma))
1013 return true;
1014
1015 return false;
1016}
1017
84158b7f
JH
1018#define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1
1019
429a22e7
JH
1020/*
1021 * Decide how much of @vma's contents should be included in a core dump.
1022 */
a07279c9
JH
1023static unsigned long vma_dump_size(struct vm_area_struct *vma,
1024 unsigned long mm_flags)
429a22e7
JH
1025{
1026#define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1027
1028 /* always dump the vdso and vsyscall sections */
1029 if (always_dump_vma(vma))
1030 goto whole;
1031
1032 if (vma->vm_flags & VM_DONTDUMP)
1033 return 0;
1034
1035 /* support for DAX */
1036 if (vma_is_dax(vma)) {
1037 if ((vma->vm_flags & VM_SHARED) && FILTER(DAX_SHARED))
1038 goto whole;
1039 if (!(vma->vm_flags & VM_SHARED) && FILTER(DAX_PRIVATE))
1040 goto whole;
1041 return 0;
1042 }
1043
1044 /* Hugetlb memory check */
1045 if (is_vm_hugetlb_page(vma)) {
1046 if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED))
1047 goto whole;
1048 if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE))
1049 goto whole;
1050 return 0;
1051 }
1052
1053 /* Do not dump I/O mapped devices or special mappings */
1054 if (vma->vm_flags & VM_IO)
1055 return 0;
1056
1057 /* By default, dump shared memory if mapped from an anonymous file. */
1058 if (vma->vm_flags & VM_SHARED) {
1059 if (file_inode(vma->vm_file)->i_nlink == 0 ?
1060 FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
1061 goto whole;
1062 return 0;
1063 }
1064
1065 /* Dump segments that have been written to. */
1066 if ((!IS_ENABLED(CONFIG_MMU) || vma->anon_vma) && FILTER(ANON_PRIVATE))
1067 goto whole;
1068 if (vma->vm_file == NULL)
1069 return 0;
1070
1071 if (FILTER(MAPPED_PRIVATE))
1072 goto whole;
1073
1074 /*
1075 * If this is the beginning of an executable file mapping,
1076 * dump the first page to aid in determining what was mapped here.
1077 */
1078 if (FILTER(ELF_HEADERS) &&
84158b7f
JH
1079 vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {
1080 if ((READ_ONCE(file_inode(vma->vm_file)->i_mode) & 0111) != 0)
1081 return PAGE_SIZE;
1082
1083 /*
1084 * ELF libraries aren't always executable.
1085 * We'll want to check whether the mapping starts with the ELF
1086 * magic, but not now - we're holding the mmap lock,
1087 * so copy_from_user() doesn't work here.
1088 * Use a placeholder instead, and fix it up later in
1089 * dump_vma_snapshot().
1090 */
1091 return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER;
1092 }
429a22e7
JH
1093
1094#undef FILTER
1095
1096 return 0;
1097
1098whole:
1099 return vma->vm_end - vma->vm_start;
1100}
a07279c9 1101
a07279c9
JH
1102/*
1103 * Helper function for iterating across a vma list. It ensures that the caller
1104 * will visit `gate_vma' prior to terminating the search.
1105 */
182ea1d7
MWO
1106static struct vm_area_struct *coredump_next_vma(struct ma_state *mas,
1107 struct vm_area_struct *vma,
a07279c9
JH
1108 struct vm_area_struct *gate_vma)
1109{
182ea1d7 1110 if (gate_vma && (vma == gate_vma))
a07279c9 1111 return NULL;
182ea1d7
MWO
1112
1113 vma = mas_next(mas, ULONG_MAX);
1114 if (vma)
1115 return vma;
a07279c9
JH
1116 return gate_vma;
1117}
1118
390031c9
EB
1119static void free_vma_snapshot(struct coredump_params *cprm)
1120{
1121 if (cprm->vma_meta) {
1122 int i;
1123 for (i = 0; i < cprm->vma_count; i++) {
1124 struct file *file = cprm->vma_meta[i].file;
1125 if (file)
1126 fput(file);
1127 }
1128 kvfree(cprm->vma_meta);
1129 cprm->vma_meta = NULL;
1130 }
1131}
1132
a07279c9
JH
1133/*
1134 * Under the mmap_lock, take a snapshot of relevant information about the task's
1135 * VMAs.
1136 */
95c5436a 1137static bool dump_vma_snapshot(struct coredump_params *cprm)
a07279c9 1138{
182ea1d7 1139 struct vm_area_struct *gate_vma, *vma = NULL;
a07279c9 1140 struct mm_struct *mm = current->mm;
182ea1d7
MWO
1141 MA_STATE(mas, &mm->mm_mt, 0, 0);
1142 int i = 0;
a07279c9
JH
1143
1144 /*
1145 * Once the stack expansion code is fixed to not change VMA bounds
1146 * under mmap_lock in read mode, this can be changed to take the
1147 * mmap_lock in read mode.
1148 */
1149 if (mmap_write_lock_killable(mm))
95c5436a 1150 return false;
a07279c9 1151
95c5436a 1152 cprm->vma_data_size = 0;
a07279c9 1153 gate_vma = get_gate_vma(mm);
95c5436a 1154 cprm->vma_count = mm->map_count + (gate_vma ? 1 : 0);
a07279c9 1155
95c5436a
EB
1156 cprm->vma_meta = kvmalloc_array(cprm->vma_count, sizeof(*cprm->vma_meta), GFP_KERNEL);
1157 if (!cprm->vma_meta) {
a07279c9 1158 mmap_write_unlock(mm);
95c5436a 1159 return false;
a07279c9
JH
1160 }
1161
182ea1d7 1162 while ((vma = coredump_next_vma(&mas, vma, gate_vma)) != NULL) {
95c5436a 1163 struct core_vma_metadata *m = cprm->vma_meta + i;
a07279c9
JH
1164
1165 m->start = vma->vm_start;
1166 m->end = vma->vm_end;
1167 m->flags = vma->vm_flags;
1168 m->dump_size = vma_dump_size(vma, cprm->mm_flags);
390031c9 1169 m->pgoff = vma->vm_pgoff;
390031c9
EB
1170 m->file = vma->vm_file;
1171 if (m->file)
1172 get_file(m->file);
182ea1d7 1173 i++;
a07279c9
JH
1174 }
1175
1176 mmap_write_unlock(mm);
1177
95c5436a
EB
1178 for (i = 0; i < cprm->vma_count; i++) {
1179 struct core_vma_metadata *m = cprm->vma_meta + i;
84158b7f
JH
1180
1181 if (m->dump_size == DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER) {
1182 char elfmag[SELFMAG];
1183
1184 if (copy_from_user(elfmag, (void __user *)m->start, SELFMAG) ||
1185 memcmp(elfmag, ELFMAG, SELFMAG) != 0) {
1186 m->dump_size = 0;
1187 } else {
1188 m->dump_size = PAGE_SIZE;
1189 }
1190 }
1191
95c5436a 1192 cprm->vma_data_size += m->dump_size;
84158b7f
JH
1193 }
1194
95c5436a 1195 return true;
a07279c9 1196}