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