b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 1) // SPDX-License-Identifier: GPL-2.0
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 2) #include <linux/slab.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 3) #include <linux/file.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 4) #include <linux/fdtable.h>
70d78fe7c8b64 (Andrey Ryabinin 2016-11-10 10:46:38 -0800 5) #include <linux/freezer.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 6) #include <linux/mm.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 7) #include <linux/stat.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 8) #include <linux/fcntl.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 9) #include <linux/swap.h>
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 10) #include <linux/ctype.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 11) #include <linux/string.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 12) #include <linux/init.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 13) #include <linux/pagemap.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 14) #include <linux/perf_event.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 15) #include <linux/highmem.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 16) #include <linux/spinlock.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 17) #include <linux/key.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 18) #include <linux/personality.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 19) #include <linux/binfmts.h>
179899fd5dc78 (Alex Kelly 2012-10-04 17:15:24 -0700 20) #include <linux/coredump.h>
f7ccbae45c5e2 (Ingo Molnar 2017-02-08 18:51:30 +0100 21) #include <linux/sched/coredump.h>
3f07c0144132e (Ingo Molnar 2017-02-08 18:51:30 +0100 22) #include <linux/sched/signal.h>
68db0cf106786 (Ingo Molnar 2017-02-08 18:51:37 +0100 23) #include <linux/sched/task_stack.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 24) #include <linux/utsname.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 25) #include <linux/pid_namespace.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 26) #include <linux/module.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 27) #include <linux/namei.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 28) #include <linux/mount.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 29) #include <linux/security.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 30) #include <linux/syscalls.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 31) #include <linux/tsacct_kern.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 32) #include <linux/cn_proc.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 33) #include <linux/audit.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 34) #include <linux/tracehook.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 35) #include <linux/kmod.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 36) #include <linux/fsnotify.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 37) #include <linux/fs_struct.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 38) #include <linux/pipe_fs_i.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 39) #include <linux/oom.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 40) #include <linux/compat.h>
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 41) #include <linux/fs.h>
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 42) #include <linux/path.h>
03927c8acb631 (Arnd Bergmann 2015-11-25 16:22:25 +0100 43) #include <linux/timekeeping.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 44)
7c0f6ba682b9c (Linus Torvalds 2016-12-24 11:46:01 -0800 45) #include <linux/uaccess.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 46) #include <asm/mmu_context.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 47) #include <asm/tlb.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 48) #include <asm/exec.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 49)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 50) #include <trace/events/task.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 51) #include "internal.h"
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 52)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 53) #include <trace/events/sched.h>
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 54)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 55) int core_uses_pid;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 56) unsigned int core_pipe_limit;
3ceadcf6d4896 (Oleg Nesterov 2013-07-03 15:08:22 -0700 57) char core_pattern[CORENAME_MAX_SIZE] = "core";
3ceadcf6d4896 (Oleg Nesterov 2013-07-03 15:08:22 -0700 58) static int core_name_size = CORENAME_MAX_SIZE;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 59)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 60) struct core_name {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 61) char *corename;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 62) int used, size;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 63) };
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 64)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 65) /* The maximal length of core_pattern is also specified in sysctl.c */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 66)
3ceadcf6d4896 (Oleg Nesterov 2013-07-03 15:08:22 -0700 67) static int expand_corename(struct core_name *cn, int size)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 68) {
e7fd1549aeb83 (Oleg Nesterov 2013-07-03 15:08:16 -0700 69) char *corename = krealloc(cn->corename, size, GFP_KERNEL);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 70)
e7fd1549aeb83 (Oleg Nesterov 2013-07-03 15:08:16 -0700 71) if (!corename)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 72) return -ENOMEM;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 73)
3ceadcf6d4896 (Oleg Nesterov 2013-07-03 15:08:22 -0700 74) if (size > core_name_size) /* racy but harmless */
3ceadcf6d4896 (Oleg Nesterov 2013-07-03 15:08:22 -0700 75) core_name_size = size;
3ceadcf6d4896 (Oleg Nesterov 2013-07-03 15:08:22 -0700 76)
3ceadcf6d4896 (Oleg Nesterov 2013-07-03 15:08:22 -0700 77) cn->size = ksize(corename);
e7fd1549aeb83 (Oleg Nesterov 2013-07-03 15:08:16 -0700 78) cn->corename = corename;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 79) return 0;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 80) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 81)
b4176b7c135ed (Nicolas Iooss 2015-06-25 15:03:53 -0700 82) static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt,
b4176b7c135ed (Nicolas Iooss 2015-06-25 15:03:53 -0700 83) va_list arg)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 84) {
5fe9d8ca21cc1 (Oleg Nesterov 2013-07-03 15:08:19 -0700 85) int free, need;
404ca80eb5c27 (Eric Dumazet 2014-04-19 10:15:07 -0700 86) va_list arg_copy;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 87)
5fe9d8ca21cc1 (Oleg Nesterov 2013-07-03 15:08:19 -0700 88) again:
5fe9d8ca21cc1 (Oleg Nesterov 2013-07-03 15:08:19 -0700 89) free = cn->size - cn->used;
404ca80eb5c27 (Eric Dumazet 2014-04-19 10:15:07 -0700 90)
404ca80eb5c27 (Eric Dumazet 2014-04-19 10:15:07 -0700 91) va_copy(arg_copy, arg);
404ca80eb5c27 (Eric Dumazet 2014-04-19 10:15:07 -0700 92) need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
404ca80eb5c27 (Eric Dumazet 2014-04-19 10:15:07 -0700 93) va_end(arg_copy);
404ca80eb5c27 (Eric Dumazet 2014-04-19 10:15:07 -0700 94)
5fe9d8ca21cc1 (Oleg Nesterov 2013-07-03 15:08:19 -0700 95) if (need < free) {
5fe9d8ca21cc1 (Oleg Nesterov 2013-07-03 15:08:19 -0700 96) cn->used += need;
5fe9d8ca21cc1 (Oleg Nesterov 2013-07-03 15:08:19 -0700 97) return 0;
5fe9d8ca21cc1 (Oleg Nesterov 2013-07-03 15:08:19 -0700 98) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 99)
3ceadcf6d4896 (Oleg Nesterov 2013-07-03 15:08:22 -0700 100) if (!expand_corename(cn, cn->size + need - free + 1))
5fe9d8ca21cc1 (Oleg Nesterov 2013-07-03 15:08:19 -0700 101) goto again;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 102)
5fe9d8ca21cc1 (Oleg Nesterov 2013-07-03 15:08:19 -0700 103) return -ENOMEM;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 104) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 105)
b4176b7c135ed (Nicolas Iooss 2015-06-25 15:03:53 -0700 106) static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...)
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 107) {
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 108) va_list arg;
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 109) int ret;
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 110)
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 111) va_start(arg, fmt);
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 112) ret = cn_vprintf(cn, fmt, arg);
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 113) va_end(arg);
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 114)
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 115) return ret;
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 116) }
bc03c691aa869 (Oleg Nesterov 2013-07-03 15:08:17 -0700 117)
b4176b7c135ed (Nicolas Iooss 2015-06-25 15:03:53 -0700 118) static __printf(2, 3)
b4176b7c135ed (Nicolas Iooss 2015-06-25 15:03:53 -0700 119) int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 120) {
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 121) int cur = cn->used;
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 122) va_list arg;
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 123) int ret;
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 124)
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 125) va_start(arg, fmt);
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 126) ret = cn_vprintf(cn, fmt, arg);
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 127) va_end(arg);
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 128)
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 129) if (ret == 0) {
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 130) /*
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 131) * Ensure that this coredump name component can't cause the
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 132) * resulting corefile path to consist of a ".." or ".".
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 133) */
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 134) if ((cn->used - cur == 1 && cn->corename[cur] == '.') ||
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 135) (cn->used - cur == 2 && cn->corename[cur] == '.'
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 136) && cn->corename[cur+1] == '.'))
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 137) cn->corename[cur] = '!';
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 138)
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 139) /*
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 140) * Empty names are fishy and could be used to create a "//" in a
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 141) * corefile name, causing the coredump to happen one directory
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 142) * level too high. Enforce that all components of the core
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 143) * pattern are at least one character long.
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 144) */
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 145) if (cn->used == cur)
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 146) ret = cn_printf(cn, "!");
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 147) }
ac94b6e3ba858 (Jann Horn 2016-01-20 15:00:08 -0800 148)
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 149) for (; cur < cn->used; ++cur) {
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 150) if (cn->corename[cur] == '/')
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 151) cn->corename[cur] = '!';
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 152) }
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 153) return ret;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 154) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 155)
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 156) static int cn_print_exe_file(struct core_name *cn, bool name_only)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 157) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 158) struct file *exe_file;
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 159) char *pathbuf, *path, *ptr;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 160) int ret;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 161)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 162) exe_file = get_mm_exe_file(current->mm);
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 163) if (!exe_file)
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 164) return cn_esc_printf(cn, "%s (path unknown)", current->comm);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 165)
0ee931c4e31a5 (Michal Hocko 2017-09-13 16:28:29 -0700 166) pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 167) if (!pathbuf) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 168) ret = -ENOMEM;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 169) goto put_exe_file;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 170) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 171)
9bf39ab2adafd (Miklos Szeredi 2015-06-19 10:29:13 +0200 172) path = file_path(exe_file, pathbuf, PATH_MAX);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 173) if (IS_ERR(path)) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 174) ret = PTR_ERR(path);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 175) goto free_buf;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 176) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 177)
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 178) if (name_only) {
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 179) ptr = strrchr(path, '/');
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 180) if (ptr)
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 181) path = ptr + 1;
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 182) }
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 183) ret = cn_esc_printf(cn, "%s", path);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 184)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 185) free_buf:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 186) kfree(pathbuf);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 187) put_exe_file:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 188) fput(exe_file);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 189) return ret;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 190) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 191)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 192) /* format_corename will inspect the pattern parameter, and output a
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 193) * name into corename, which must have space for at least
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 194) * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 195) */
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 196) static int format_corename(struct core_name *cn, struct coredump_params *cprm,
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 197) size_t **argv, int *argc)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 198) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 199) const struct cred *cred = current_cred();
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 200) const char *pat_ptr = core_pattern;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 201) int ispipe = (*pat_ptr == '|');
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 202) bool was_space = false;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 203) int pid_in_pattern = 0;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 204) int err = 0;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 205)
e7fd1549aeb83 (Oleg Nesterov 2013-07-03 15:08:16 -0700 206) cn->used = 0;
3ceadcf6d4896 (Oleg Nesterov 2013-07-03 15:08:22 -0700 207) cn->corename = NULL;
3ceadcf6d4896 (Oleg Nesterov 2013-07-03 15:08:22 -0700 208) if (expand_corename(cn, core_name_size))
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 209) return -ENOMEM;
888ffc5923e43 (Oleg Nesterov 2013-07-03 15:08:23 -0700 210) cn->corename[0] = '\0';
888ffc5923e43 (Oleg Nesterov 2013-07-03 15:08:23 -0700 211)
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 212) if (ispipe) {
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 213) int argvs = sizeof(core_pattern) / 2;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 214) (*argv) = kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL);
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 215) if (!(*argv))
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 216) return -ENOMEM;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 217) (*argv)[(*argc)++] = 0;
888ffc5923e43 (Oleg Nesterov 2013-07-03 15:08:23 -0700 218) ++pat_ptr;
db973a7289dad (Sudip Mukherjee 2020-04-20 18:14:20 -0700 219) if (!(*pat_ptr))
db973a7289dad (Sudip Mukherjee 2020-04-20 18:14:20 -0700 220) return -ENOMEM;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 221) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 222)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 223) /* Repeat as long as we have more pattern to process and more output
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 224) space */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 225) while (*pat_ptr) {
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 226) /*
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 227) * Split on spaces before doing template expansion so that
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 228) * %e and %E don't get split if they have spaces in them
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 229) */
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 230) if (ispipe) {
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 231) if (isspace(*pat_ptr)) {
2bf509d96d84c (Menglong Dong 2020-12-05 22:14:42 -0800 232) if (cn->used != 0)
2bf509d96d84c (Menglong Dong 2020-12-05 22:14:42 -0800 233) was_space = true;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 234) pat_ptr++;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 235) continue;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 236) } else if (was_space) {
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 237) was_space = false;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 238) err = cn_printf(cn, "%c", '\0');
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 239) if (err)
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 240) return err;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 241) (*argv)[(*argc)++] = cn->used;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 242) }
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 243) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 244) if (*pat_ptr != '%') {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 245) err = cn_printf(cn, "%c", *pat_ptr++);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 246) } else {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 247) switch (*++pat_ptr) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 248) /* single % at the end, drop that */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 249) case 0:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 250) goto out;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 251) /* Double percent, output one percent */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 252) case '%':
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 253) err = cn_printf(cn, "%c", '%');
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 254) break;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 255) /* pid */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 256) case 'p':
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 257) pid_in_pattern = 1;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 258) err = cn_printf(cn, "%d",
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 259) task_tgid_vnr(current));
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 260) break;
65aafb1e7484b (Stéphane Graber 2013-09-11 14:24:32 -0700 261) /* global pid */
65aafb1e7484b (Stéphane Graber 2013-09-11 14:24:32 -0700 262) case 'P':
65aafb1e7484b (Stéphane Graber 2013-09-11 14:24:32 -0700 263) err = cn_printf(cn, "%d",
65aafb1e7484b (Stéphane Graber 2013-09-11 14:24:32 -0700 264) task_tgid_nr(current));
65aafb1e7484b (Stéphane Graber 2013-09-11 14:24:32 -0700 265) break;
b03023ecbdb76 (Oleg Nesterov 2014-10-13 15:53:35 -0700 266) case 'i':
b03023ecbdb76 (Oleg Nesterov 2014-10-13 15:53:35 -0700 267) err = cn_printf(cn, "%d",
b03023ecbdb76 (Oleg Nesterov 2014-10-13 15:53:35 -0700 268) task_pid_vnr(current));
b03023ecbdb76 (Oleg Nesterov 2014-10-13 15:53:35 -0700 269) break;
b03023ecbdb76 (Oleg Nesterov 2014-10-13 15:53:35 -0700 270) case 'I':
b03023ecbdb76 (Oleg Nesterov 2014-10-13 15:53:35 -0700 271) err = cn_printf(cn, "%d",
b03023ecbdb76 (Oleg Nesterov 2014-10-13 15:53:35 -0700 272) task_pid_nr(current));
b03023ecbdb76 (Oleg Nesterov 2014-10-13 15:53:35 -0700 273) break;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 274) /* uid */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 275) case 'u':
5202efe544c27 (Nicolas Iooss 2015-06-25 15:03:51 -0700 276) err = cn_printf(cn, "%u",
5202efe544c27 (Nicolas Iooss 2015-06-25 15:03:51 -0700 277) from_kuid(&init_user_ns,
5202efe544c27 (Nicolas Iooss 2015-06-25 15:03:51 -0700 278) cred->uid));
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 279) break;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 280) /* gid */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 281) case 'g':
5202efe544c27 (Nicolas Iooss 2015-06-25 15:03:51 -0700 282) err = cn_printf(cn, "%u",
5202efe544c27 (Nicolas Iooss 2015-06-25 15:03:51 -0700 283) from_kgid(&init_user_ns,
5202efe544c27 (Nicolas Iooss 2015-06-25 15:03:51 -0700 284) cred->gid));
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 285) break;
12a2b4b2241e3 (Oleg Nesterov 2012-10-04 17:15:25 -0700 286) case 'd':
12a2b4b2241e3 (Oleg Nesterov 2012-10-04 17:15:25 -0700 287) err = cn_printf(cn, "%d",
12a2b4b2241e3 (Oleg Nesterov 2012-10-04 17:15:25 -0700 288) __get_dumpable(cprm->mm_flags));
12a2b4b2241e3 (Oleg Nesterov 2012-10-04 17:15:25 -0700 289) break;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 290) /* signal that caused the coredump */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 291) case 's':
b4176b7c135ed (Nicolas Iooss 2015-06-25 15:03:53 -0700 292) err = cn_printf(cn, "%d",
b4176b7c135ed (Nicolas Iooss 2015-06-25 15:03:53 -0700 293) cprm->siginfo->si_signo);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 294) break;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 295) /* UNIX time of coredump */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 296) case 't': {
03927c8acb631 (Arnd Bergmann 2015-11-25 16:22:25 +0100 297) time64_t time;
03927c8acb631 (Arnd Bergmann 2015-11-25 16:22:25 +0100 298)
03927c8acb631 (Arnd Bergmann 2015-11-25 16:22:25 +0100 299) time = ktime_get_real_seconds();
03927c8acb631 (Arnd Bergmann 2015-11-25 16:22:25 +0100 300) err = cn_printf(cn, "%lld", time);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 301) break;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 302) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 303) /* hostname */
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 304) case 'h':
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 305) down_read(&uts_sem);
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 306) err = cn_esc_printf(cn, "%s",
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 307) utsname()->nodename);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 308) up_read(&uts_sem);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 309) break;
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 310) /* executable, could be changed by prctl PR_SET_NAME etc */
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 311) case 'e':
923bed030ff6e (Oleg Nesterov 2013-07-03 15:08:20 -0700 312) err = cn_esc_printf(cn, "%s", current->comm);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 313) break;
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 314) /* file name of executable */
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 315) case 'f':
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 316) err = cn_print_exe_file(cn, true);
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 317) break;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 318) case 'E':
f38c85f1ba690 (Lepton Wu 2020-08-11 18:36:20 -0700 319) err = cn_print_exe_file(cn, false);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 320) break;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 321) /* core limit size */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 322) case 'c':
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 323) err = cn_printf(cn, "%lu",
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 324) rlimit(RLIMIT_CORE));
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 325) break;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 326) default:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 327) break;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 328) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 329) ++pat_ptr;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 330) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 331)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 332) if (err)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 333) return err;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 334) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 335)
888ffc5923e43 (Oleg Nesterov 2013-07-03 15:08:23 -0700 336) out:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 337) /* Backward compatibility with core_uses_pid:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 338) *
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 339) * If core_pattern does not include a %p (as is the default)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 340) * and core_uses_pid is set, then .%pid will be appended to
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 341) * the filename. Do not do this for piped commands. */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 342) if (!ispipe && !pid_in_pattern && core_uses_pid) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 343) err = cn_printf(cn, ".%d", task_tgid_vnr(current));
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 344) if (err)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 345) return err;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 346) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 347) return ispipe;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 348) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 349)
5fa534c987784 (Oleg Nesterov 2015-11-06 16:32:31 -0800 350) static int zap_process(struct task_struct *start, int exit_code, int flags)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 351) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 352) struct task_struct *t;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 353) int nr = 0;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 354)
5fa534c987784 (Oleg Nesterov 2015-11-06 16:32:31 -0800 355) /* ignore all signals except SIGKILL, see prepare_signal() */
5fa534c987784 (Oleg Nesterov 2015-11-06 16:32:31 -0800 356) start->signal->flags = SIGNAL_GROUP_COREDUMP | flags;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 357) start->signal->group_exit_code = exit_code;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 358) start->signal->group_stop_count = 0;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 359)
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 360) for_each_thread(start, t) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 361) task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 362) if (t != current && t->mm) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 363) sigaddset(&t->pending.signal, SIGKILL);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 364) signal_wake_up(t, 1);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 365) nr++;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 366) }
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 367) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 368)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 369) return nr;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 370) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 371)
403bad72b67d8 (Oleg Nesterov 2013-04-30 15:28:10 -0700 372) static int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
403bad72b67d8 (Oleg Nesterov 2013-04-30 15:28:10 -0700 373) struct core_state *core_state, int exit_code)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 374) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 375) struct task_struct *g, *p;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 376) unsigned long flags;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 377) int nr = -EAGAIN;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 378)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 379) spin_lock_irq(&tsk->sighand->siglock);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 380) if (!signal_group_exit(tsk->signal)) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 381) mm->core_state = core_state;
6cd8f0acae342 (Oleg Nesterov 2013-04-30 15:28:12 -0700 382) tsk->signal->group_exit_task = tsk;
5fa534c987784 (Oleg Nesterov 2015-11-06 16:32:31 -0800 383) nr = zap_process(tsk, exit_code, 0);
403bad72b67d8 (Oleg Nesterov 2013-04-30 15:28:10 -0700 384) clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 385) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 386) spin_unlock_irq(&tsk->sighand->siglock);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 387) if (unlikely(nr < 0))
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 388) return nr;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 389)
aed8adb7688d5 (Silesh C V 2014-07-23 13:59:59 -0700 390) tsk->flags |= PF_DUMPCORE;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 391) if (atomic_read(&mm->mm_users) == nr + 1)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 392) goto done;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 393) /*
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 394) * We should find and kill all tasks which use this mm, and we should
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 395) * count them correctly into ->nr_threads. We don't take tasklist
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 396) * lock, but this is safe wrt:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 397) *
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 398) * fork:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 399) * None of sub-threads can fork after zap_process(leader). All
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 400) * processes which were created before this point should be
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 401) * visible to zap_threads() because copy_process() adds the new
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 402) * process to the tail of init_task.tasks list, and lock/unlock
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 403) * of ->siglock provides a memory barrier.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 404) *
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 405) * do_exit:
c1e8d7c6a7a68 (Michel Lespinasse 2020-06-08 21:33:54 -0700 406) * The caller holds mm->mmap_lock. This means that the task which
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 407) * uses this mm can't pass exit_mm(), so it can't exit or clear
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 408) * its ->mm.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 409) *
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 410) * de_thread:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 411) * It does list_replace_rcu(&leader->tasks, ¤t->tasks),
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 412) * we must see either old or new leader, this does not matter.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 413) * However, it can change p->sighand, so lock_task_sighand(p)
c1e8d7c6a7a68 (Michel Lespinasse 2020-06-08 21:33:54 -0700 414) * must be used. Since p->mm != NULL and we hold ->mmap_lock
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 415) * it can't fail.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 416) *
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 417) * Note also that "g" can be the old leader with ->mm == NULL
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 418) * and already unhashed and thus removed from ->thread_group.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 419) * This is OK, __unhash_process()->list_del_rcu() does not
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 420) * clear the ->next pointer, we will find the new leader via
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 421) * next_thread().
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 422) */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 423) rcu_read_lock();
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 424) for_each_process(g) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 425) if (g == tsk->group_leader)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 426) continue;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 427) if (g->flags & PF_KTHREAD)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 428) continue;
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 429)
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 430) for_each_thread(g, p) {
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 431) if (unlikely(!p->mm))
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 432) continue;
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 433) if (unlikely(p->mm == mm)) {
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 434) lock_task_sighand(p, &flags);
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 435) nr += zap_process(p, exit_code,
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 436) SIGNAL_GROUP_EXIT);
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 437) unlock_task_sighand(p, &flags);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 438) }
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 439) break;
d61ba58953fcf (Oleg Nesterov 2015-11-06 16:32:34 -0800 440) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 441) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 442) rcu_read_unlock();
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 443) done:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 444) atomic_set(&core_state->nr_threads, nr);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 445) return nr;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 446) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 447)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 448) static int coredump_wait(int exit_code, struct core_state *core_state)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 449) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 450) struct task_struct *tsk = current;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 451) struct mm_struct *mm = tsk->mm;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 452) int core_waiters = -EBUSY;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 453)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 454) init_completion(&core_state->startup);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 455) core_state->dumper.task = tsk;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 456) core_state->dumper.next = NULL;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 457)
d8ed45c5dcd45 (Michel Lespinasse 2020-06-08 21:33:25 -0700 458) if (mmap_write_lock_killable(mm))
4136c26b65d12 (Michal Hocko 2016-05-23 16:25:57 -0700 459) return -EINTR;
4136c26b65d12 (Michal Hocko 2016-05-23 16:25:57 -0700 460)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 461) if (!mm->core_state)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 462) core_waiters = zap_threads(tsk, mm, core_state, exit_code);
d8ed45c5dcd45 (Michel Lespinasse 2020-06-08 21:33:25 -0700 463) mmap_write_unlock(mm);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 464)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 465) if (core_waiters > 0) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 466) struct core_thread *ptr;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 467)
70d78fe7c8b64 (Andrey Ryabinin 2016-11-10 10:46:38 -0800 468) freezer_do_not_count();
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 469) wait_for_completion(&core_state->startup);
70d78fe7c8b64 (Andrey Ryabinin 2016-11-10 10:46:38 -0800 470) freezer_count();
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 471) /*
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 472) * Wait for all the threads to become inactive, so that
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 473) * all the thread context (extended register state, like
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 474) * fpu etc) gets copied to the memory.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 475) */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 476) ptr = core_state->dumper.next;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 477) while (ptr != NULL) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 478) wait_task_inactive(ptr->task, 0);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 479) ptr = ptr->next;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 480) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 481) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 482)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 483) return core_waiters;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 484) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 485)
acdedd99b0f3b (Oleg Nesterov 2013-04-30 15:28:13 -0700 486) static void coredump_finish(struct mm_struct *mm, bool core_dumped)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 487) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 488) struct core_thread *curr, *next;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 489) struct task_struct *task;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 490)
6cd8f0acae342 (Oleg Nesterov 2013-04-30 15:28:12 -0700 491) spin_lock_irq(¤t->sighand->siglock);
acdedd99b0f3b (Oleg Nesterov 2013-04-30 15:28:13 -0700 492) if (core_dumped && !__fatal_signal_pending(current))
acdedd99b0f3b (Oleg Nesterov 2013-04-30 15:28:13 -0700 493) current->signal->group_exit_code |= 0x80;
6cd8f0acae342 (Oleg Nesterov 2013-04-30 15:28:12 -0700 494) current->signal->group_exit_task = NULL;
6cd8f0acae342 (Oleg Nesterov 2013-04-30 15:28:12 -0700 495) current->signal->flags = SIGNAL_GROUP_EXIT;
6cd8f0acae342 (Oleg Nesterov 2013-04-30 15:28:12 -0700 496) spin_unlock_irq(¤t->sighand->siglock);
6cd8f0acae342 (Oleg Nesterov 2013-04-30 15:28:12 -0700 497)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 498) next = mm->core_state->dumper.next;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 499) while ((curr = next) != NULL) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 500) next = curr->next;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 501) task = curr->task;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 502) /*
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 503) * see exit_mm(), curr->task must not see
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 504) * ->task == NULL before we read ->next.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 505) */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 506) smp_mb();
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 507) curr->task = NULL;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 508) wake_up_process(task);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 509) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 510)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 511) mm->core_state = NULL;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 512) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 513)
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 514) static bool dump_interrupted(void)
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 515) {
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 516) /*
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 517) * SIGKILL or freezing() interrupt the coredumping. Perhaps we
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 518) * can do try_to_freeze() and check __fatal_signal_pending(),
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 519) * but then we need to teach dump_write() to restart and clear
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 520) * TIF_SIGPENDING.
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 521) */
06af8679449d4 (Eric W. Biederman 2021-06-10 15:11:11 -0500 522) return fatal_signal_pending(current) || freezing(current);
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 523) }
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 524)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 525) static void wait_for_dump_helpers(struct file *file)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 526) {
de32ec4cfeb3b (Al Viro 2013-03-21 11:16:56 -0400 527) struct pipe_inode_info *pipe = file->private_data;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 528)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 529) pipe_lock(pipe);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 530) pipe->readers++;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 531) pipe->writers--;
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 532) wake_up_interruptible_sync(&pipe->rd_wait);
dc7ee2aac830e (Oleg Nesterov 2013-04-30 15:28:17 -0700 533) kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
dc7ee2aac830e (Oleg Nesterov 2013-04-30 15:28:17 -0700 534) pipe_unlock(pipe);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 535)
dc7ee2aac830e (Oleg Nesterov 2013-04-30 15:28:17 -0700 536) /*
dc7ee2aac830e (Oleg Nesterov 2013-04-30 15:28:17 -0700 537) * We actually want wait_event_freezable() but then we need
dc7ee2aac830e (Oleg Nesterov 2013-04-30 15:28:17 -0700 538) * to clear TIF_SIGPENDING and improve dump_interrupted().
dc7ee2aac830e (Oleg Nesterov 2013-04-30 15:28:17 -0700 539) */
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 540) wait_event_interruptible(pipe->rd_wait, pipe->readers == 1);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 541)
dc7ee2aac830e (Oleg Nesterov 2013-04-30 15:28:17 -0700 542) pipe_lock(pipe);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 543) pipe->readers--;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 544) pipe->writers++;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 545) pipe_unlock(pipe);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 546) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 547)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 548) /*
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 549) * umh_pipe_setup
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 550) * helper function to customize the process used
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 551) * to collect the core in userspace. Specifically
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 552) * it sets up a pipe and installs it as fd 0 (stdin)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 553) * for the process. Returns 0 on success, or
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 554) * PTR_ERR on failure.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 555) * Note that it also sets the core limit to 1. This
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 556) * is a special value that we use to trap recursive
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 557) * core dumps
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 558) */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 559) static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 560) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 561) struct file *files[2];
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 562) struct coredump_params *cp = (struct coredump_params *)info->data;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 563) int err = create_pipe_files(files, 0);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 564) if (err)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 565) return err;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 566)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 567) cp->file = files[1];
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 568)
45525b26a46cd (Al Viro 2012-10-16 13:30:07 -0400 569) err = replace_fd(0, files[0], 0);
45525b26a46cd (Al Viro 2012-10-16 13:30:07 -0400 570) fput(files[0]);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 571) /* and disallow core files too */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 572) current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 573)
45525b26a46cd (Al Viro 2012-10-16 13:30:07 -0400 574) return err;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 575) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 576)
ae7795bc6187a (Eric W. Biederman 2018-09-25 11:27:20 +0200 577) void do_coredump(const kernel_siginfo_t *siginfo)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 578) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 579) struct core_state core_state;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 580) struct core_name cn;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 581) struct mm_struct *mm = current->mm;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 582) struct linux_binfmt * binfmt;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 583) const struct cred *old_cred;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 584) struct cred *cred;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 585) int retval = 0;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 586) int ispipe;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 587) size_t *argv = NULL;
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 588) int argc = 0;
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 589) /* require nonrelative corefile path and be extra careful */
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 590) bool need_suid_safe = false;
acdedd99b0f3b (Oleg Nesterov 2013-04-30 15:28:13 -0700 591) bool core_dumped = false;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 592) static atomic_t core_dump_count = ATOMIC_INIT(0);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 593) struct coredump_params cprm = {
5ab1c309b3448 (Denys Vlasenko 2012-10-04 17:15:29 -0700 594) .siginfo = siginfo,
541880d9a2c78 (Al Viro 2012-11-05 13:11:26 -0500 595) .regs = signal_pt_regs(),
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 596) .limit = rlimit(RLIMIT_CORE),
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 597) /*
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 598) * We must use the same mm->flags while dumping core to avoid
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 599) * inconsistency of bit flags, since this flag is not protected
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 600) * by any locks.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 601) */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 602) .mm_flags = mm->flags,
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 603) };
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 604)
5ab1c309b3448 (Denys Vlasenko 2012-10-04 17:15:29 -0700 605) audit_core_dumps(siginfo->si_signo);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 606)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 607) binfmt = mm->binfmt;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 608) if (!binfmt || !binfmt->core_dump)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 609) goto fail;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 610) if (!__get_dumpable(cprm.mm_flags))
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 611) goto fail;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 612)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 613) cred = prepare_creds();
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 614) if (!cred)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 615) goto fail;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 616) /*
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 617) * We cannot trust fsuid as being the "true" uid of the process
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 618) * nor do we know its entire history. We only know it was tainted
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 619) * so we dump it as root in mode 2, and only into a controlled
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 620) * environment (pipe handler or fully qualified path).
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 621) */
e579d2c259be4 (Kees Cook 2013-02-27 17:03:15 -0800 622) if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 623) /* Setuid core dump mode */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 624) cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 625) need_suid_safe = true;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 626) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 627)
5ab1c309b3448 (Denys Vlasenko 2012-10-04 17:15:29 -0700 628) retval = coredump_wait(siginfo->si_signo, &core_state);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 629) if (retval < 0)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 630) goto fail_creds;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 631)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 632) old_cred = override_creds(cred);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 633)
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 634) ispipe = format_corename(&cn, &cprm, &argv, &argc);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 635)
fb96c475f6b27 (Lucas De Marchi 2013-04-30 15:28:06 -0700 636) if (ispipe) {
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 637) int argi;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 638) int dump_count;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 639) char **helper_argv;
907ed1328d2a7 (Lucas De Marchi 2013-04-30 15:28:07 -0700 640) struct subprocess_info *sub_info;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 641)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 642) if (ispipe < 0) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 643) printk(KERN_WARNING "format_corename failed\n");
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 644) printk(KERN_WARNING "Aborting core\n");
e7fd1549aeb83 (Oleg Nesterov 2013-07-03 15:08:16 -0700 645) goto fail_unlock;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 646) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 647)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 648) if (cprm.limit == 1) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 649) /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 650) *
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 651) * Normally core limits are irrelevant to pipes, since
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 652) * we're not writing to the file system, but we use
fcbc32bc6cb59 (Bastien Nocera 2015-02-05 14:35:05 +0100 653) * cprm.limit of 1 here as a special value, this is a
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 654) * consistent way to catch recursive crashes.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 655) * We can still crash if the core_pattern binary sets
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 656) * RLIM_CORE = !1, but it runs as root, and can do
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 657) * lots of stupid things.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 658) *
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 659) * Note that we use task_tgid_vnr here to grab the pid
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 660) * of the process group leader. That way we get the
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 661) * right pid if a thread in a multi-threaded
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 662) * core_pattern process dies.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 663) */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 664) printk(KERN_WARNING
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 665) "Process %d(%s) has RLIMIT_CORE set to 1\n",
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 666) task_tgid_vnr(current), current->comm);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 667) printk(KERN_WARNING "Aborting core\n");
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 668) goto fail_unlock;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 669) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 670) cprm.limit = RLIM_INFINITY;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 671)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 672) dump_count = atomic_inc_return(&core_dump_count);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 673) if (core_pipe_limit && (core_pipe_limit < dump_count)) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 674) printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 675) task_tgid_vnr(current), current->comm);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 676) printk(KERN_WARNING "Skipping core dump\n");
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 677) goto fail_dropcount;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 678) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 679)
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 680) helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv),
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 681) GFP_KERNEL);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 682) if (!helper_argv) {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 683) printk(KERN_WARNING "%s failed to allocate memory\n",
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 684) __func__);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 685) goto fail_dropcount;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 686) }
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 687) for (argi = 0; argi < argc; argi++)
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 688) helper_argv[argi] = cn.corename + argv[argi];
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 689) helper_argv[argi] = NULL;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 690)
907ed1328d2a7 (Lucas De Marchi 2013-04-30 15:28:07 -0700 691) retval = -ENOMEM;
907ed1328d2a7 (Lucas De Marchi 2013-04-30 15:28:07 -0700 692) sub_info = call_usermodehelper_setup(helper_argv[0],
907ed1328d2a7 (Lucas De Marchi 2013-04-30 15:28:07 -0700 693) helper_argv, NULL, GFP_KERNEL,
907ed1328d2a7 (Lucas De Marchi 2013-04-30 15:28:07 -0700 694) umh_pipe_setup, NULL, &cprm);
907ed1328d2a7 (Lucas De Marchi 2013-04-30 15:28:07 -0700 695) if (sub_info)
907ed1328d2a7 (Lucas De Marchi 2013-04-30 15:28:07 -0700 696) retval = call_usermodehelper_exec(sub_info,
907ed1328d2a7 (Lucas De Marchi 2013-04-30 15:28:07 -0700 697) UMH_WAIT_EXEC);
907ed1328d2a7 (Lucas De Marchi 2013-04-30 15:28:07 -0700 698)
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 699) kfree(helper_argv);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 700) if (retval) {
888ffc5923e43 (Oleg Nesterov 2013-07-03 15:08:23 -0700 701) printk(KERN_INFO "Core dump to |%s pipe failed\n",
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 702) cn.corename);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 703) goto close_fail;
fb96c475f6b27 (Lucas De Marchi 2013-04-30 15:28:06 -0700 704) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 705) } else {
643fe55a0679a (Christian Brauner 2021-01-21 14:19:34 +0100 706) struct user_namespace *mnt_userns;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 707) struct inode *inode;
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 708) int open_flags = O_CREAT | O_RDWR | O_NOFOLLOW |
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 709) O_LARGEFILE | O_EXCL;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 710)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 711) if (cprm.limit < binfmt->min_coredump)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 712) goto fail_unlock;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 713)
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 714) if (need_suid_safe && cn.corename[0] != '/') {
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 715) printk(KERN_WARNING "Pid %d(%s) can only dump core "\
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 716) "to fully qualified path!\n",
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 717) task_tgid_vnr(current), current->comm);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 718) printk(KERN_WARNING "Skipping core dump\n");
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 719) goto fail_unlock;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 720) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 721)
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 722) /*
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 723) * Unlink the file if it exists unless this is a SUID
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 724) * binary - in that case, we're running around with root
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 725) * privs and don't want to unlink another user's coredump.
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 726) */
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 727) if (!need_suid_safe) {
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 728) /*
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 729) * If it doesn't exist, that's fine. If there's some
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 730) * other problem, we'll catch it at the filp_open().
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 731) */
96271654f55c7 (Christoph Hellwig 2017-11-04 13:44:46 +0300 732) do_unlinkat(AT_FDCWD, getname_kernel(cn.corename));
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 733) }
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 734)
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 735) /*
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 736) * There is a race between unlinking and creating the
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 737) * file, but if that causes an EEXIST here, that's
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 738) * fine - another process raced with us while creating
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 739) * the corefile, and the other process won. To userspace,
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 740) * what matters is that at least one of the two processes
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 741) * writes its coredump successfully, not which one.
fbb1816942c04 (Jann Horn 2015-09-09 15:38:28 -0700 742) */
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 743) if (need_suid_safe) {
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 744) /*
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 745) * Using user namespaces, normal user tasks can change
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 746) * their current->fs->root to point to arbitrary
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 747) * directories. Since the intention of the "only dump
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 748) * with a fully qualified path" rule is to control where
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 749) * coredumps may be placed using root privileges,
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 750) * current->fs->root must not be used. Instead, use the
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 751) * root directory of init_task.
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 752) */
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 753) struct path root;
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 754)
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 755) task_lock(&init_task);
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 756) get_fs_root(init_task.fs, &root);
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 757) task_unlock(&init_task);
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 758) cprm.file = file_open_root(root.dentry, root.mnt,
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 759) cn.corename, open_flags, 0600);
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 760) path_put(&root);
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 761) } else {
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 762) cprm.file = filp_open(cn.corename, open_flags, 0600);
378c6520e7d29 (Jann Horn 2016-03-22 14:25:36 -0700 763) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 764) if (IS_ERR(cprm.file))
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 765) goto fail_unlock;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 766)
496ad9aa8ef44 (Al Viro 2013-01-23 17:07:38 -0500 767) inode = file_inode(cprm.file);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 768) if (inode->i_nlink > 1)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 769) goto close_fail;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 770) if (d_unhashed(cprm.file->f_path.dentry))
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 771) goto close_fail;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 772) /*
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 773) * AK: actually i see no reason to not allow this for named
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 774) * pipes etc, but keep the previous behaviour for now.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 775) */
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 776) if (!S_ISREG(inode->i_mode))
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 777) goto close_fail;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 778) /*
40f705a736eac (Jann Horn 2015-09-09 15:38:30 -0700 779) * Don't dump core if the filesystem changed owner or mode
40f705a736eac (Jann Horn 2015-09-09 15:38:30 -0700 780) * of the file during file creation. This is an issue when
40f705a736eac (Jann Horn 2015-09-09 15:38:30 -0700 781) * a process dumps core while its cwd is e.g. on a vfat
40f705a736eac (Jann Horn 2015-09-09 15:38:30 -0700 782) * filesystem.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 783) */
643fe55a0679a (Christian Brauner 2021-01-21 14:19:34 +0100 784) mnt_userns = file_mnt_user_ns(cprm.file);
643fe55a0679a (Christian Brauner 2021-01-21 14:19:34 +0100 785) if (!uid_eq(i_uid_into_mnt(mnt_userns, inode), current_fsuid()))
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 786) goto close_fail;
40f705a736eac (Jann Horn 2015-09-09 15:38:30 -0700 787) if ((inode->i_mode & 0677) != 0600)
40f705a736eac (Jann Horn 2015-09-09 15:38:30 -0700 788) goto close_fail;
86cc05840a0da (Al Viro 2015-04-03 15:23:17 -0400 789) if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 790) goto close_fail;
643fe55a0679a (Christian Brauner 2021-01-21 14:19:34 +0100 791) if (do_truncate(mnt_userns, cprm.file->f_path.dentry,
643fe55a0679a (Christian Brauner 2021-01-21 14:19:34 +0100 792) 0, 0, cprm.file))
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 793) goto close_fail;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 794) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 795)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 796) /* get us an unshared descriptor table; almost always a no-op */
c39ab6de22730 (Eric W. Biederman 2020-11-25 15:51:32 -0600 797) /* The cell spufs coredump code reads the file descriptor tables */
1f702603e7125 (Eric W. Biederman 2020-11-20 17:14:19 -0600 798) retval = unshare_files();
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 799) if (retval)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 800) goto close_fail;
e86d35c38e37e (Al Viro 2013-05-04 14:45:54 -0400 801) if (!dump_interrupted()) {
3740d93e37902 (Luis Chamberlain 2020-04-16 16:28:59 +0000 802) /*
3740d93e37902 (Luis Chamberlain 2020-04-16 16:28:59 +0000 803) * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
3740d93e37902 (Luis Chamberlain 2020-04-16 16:28:59 +0000 804) * have this set to NULL.
3740d93e37902 (Luis Chamberlain 2020-04-16 16:28:59 +0000 805) */
3740d93e37902 (Luis Chamberlain 2020-04-16 16:28:59 +0000 806) if (!cprm.file) {
3740d93e37902 (Luis Chamberlain 2020-04-16 16:28:59 +0000 807) pr_info("Core dump to |%s disabled\n", cn.corename);
3740d93e37902 (Luis Chamberlain 2020-04-16 16:28:59 +0000 808) goto close_fail;
3740d93e37902 (Luis Chamberlain 2020-04-16 16:28:59 +0000 809) }
e86d35c38e37e (Al Viro 2013-05-04 14:45:54 -0400 810) file_start_write(cprm.file);
e86d35c38e37e (Al Viro 2013-05-04 14:45:54 -0400 811) core_dumped = binfmt->core_dump(&cprm);
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 812) /*
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 813) * Ensures that file size is big enough to contain the current
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 814) * file postion. This prevents gdb from complaining about
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 815) * a truncated file if the last "write" to the file was
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 816) * dump_skip.
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 817) */
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 818) if (cprm.to_skip) {
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 819) cprm.to_skip--;
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 820) dump_emit(&cprm, "", 1);
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 821) }
e86d35c38e37e (Al Viro 2013-05-04 14:45:54 -0400 822) file_end_write(cprm.file);
e86d35c38e37e (Al Viro 2013-05-04 14:45:54 -0400 823) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 824) if (ispipe && core_pipe_limit)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 825) wait_for_dump_helpers(cprm.file);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 826) close_fail:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 827) if (cprm.file)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 828) filp_close(cprm.file, NULL);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 829) fail_dropcount:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 830) if (ispipe)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 831) atomic_dec(&core_dump_count);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 832) fail_unlock:
315c69261dd3f (Paul Wise 2019-08-02 21:49:05 -0700 833) kfree(argv);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 834) kfree(cn.corename);
acdedd99b0f3b (Oleg Nesterov 2013-04-30 15:28:13 -0700 835) coredump_finish(mm, core_dumped);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 836) revert_creds(old_cred);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 837) fail_creds:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 838) put_cred(cred);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 839) fail:
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 840) return;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 841) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 842)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 843) /*
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 844) * Core dumping helper functions. These are the only things you should
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 845) * do on a core-file: use only these functions to write out all the
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 846) * necessary info.
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 847) */
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 848) static int __dump_emit(struct coredump_params *cprm, const void *addr, int nr)
ecc8c7725e6c2 (Al Viro 2013-10-05 15:32:35 -0400 849) {
ecc8c7725e6c2 (Al Viro 2013-10-05 15:32:35 -0400 850) struct file *file = cprm->file;
2507a4fbd48a9 (Al Viro 2013-10-08 09:11:48 -0400 851) loff_t pos = file->f_pos;
2507a4fbd48a9 (Al Viro 2013-10-08 09:11:48 -0400 852) ssize_t n;
2c4cb04300fa1 (Omar Sandoval 2016-05-11 15:16:37 -0700 853) if (cprm->written + nr > cprm->limit)
ecc8c7725e6c2 (Al Viro 2013-10-05 15:32:35 -0400 854) return 0;
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 855)
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 856)
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 857) if (dump_interrupted())
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 858) return 0;
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 859) n = __kernel_write(file, addr, nr, &pos);
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 860) if (n != nr)
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 861) return 0;
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 862) file->f_pos = pos;
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 863) cprm->written += n;
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 864) cprm->pos += n;
df0c09c011730 (Jann Horn 2020-10-15 20:12:43 -0700 865)
ecc8c7725e6c2 (Al Viro 2013-10-05 15:32:35 -0400 866) return 1;
ecc8c7725e6c2 (Al Viro 2013-10-05 15:32:35 -0400 867) }
ecc8c7725e6c2 (Al Viro 2013-10-05 15:32:35 -0400 868)
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 869) static int __dump_skip(struct coredump_params *cprm, size_t nr)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 870) {
9b56d54380adb (Al Viro 2013-10-08 09:26:08 -0400 871) static char zeroes[PAGE_SIZE];
9b56d54380adb (Al Viro 2013-10-08 09:26:08 -0400 872) struct file *file = cprm->file;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 873) if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
528f827ee0bb5 (Oleg Nesterov 2013-04-30 15:28:15 -0700 874) if (dump_interrupted() ||
9b56d54380adb (Al Viro 2013-10-08 09:26:08 -0400 875) file->f_op->llseek(file, nr, SEEK_CUR) < 0)
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 876) return 0;
1607f09c226d1 (Mateusz Guzik 2016-06-05 23:14:14 +0200 877) cprm->pos += nr;
9b56d54380adb (Al Viro 2013-10-08 09:26:08 -0400 878) return 1;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 879) } else {
9b56d54380adb (Al Viro 2013-10-08 09:26:08 -0400 880) while (nr > PAGE_SIZE) {
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 881) if (!__dump_emit(cprm, zeroes, PAGE_SIZE))
9b56d54380adb (Al Viro 2013-10-08 09:26:08 -0400 882) return 0;
9b56d54380adb (Al Viro 2013-10-08 09:26:08 -0400 883) nr -= PAGE_SIZE;
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 884) }
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 885) return __dump_emit(cprm, zeroes, nr);
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 886) }
10c28d937e2cc (Alex Kelly 2012-09-26 21:52:08 -0400 887) }
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 888)
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 889) int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 890) {
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 891) if (cprm->to_skip) {
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 892) if (!__dump_skip(cprm, cprm->to_skip))
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 893) return 0;
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 894) cprm->to_skip = 0;
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 895) }
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 896) return __dump_emit(cprm, addr, nr);
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 897) }
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 898) EXPORT_SYMBOL(dump_emit);
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 899)
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 900) void dump_skip_to(struct coredump_params *cprm, unsigned long pos)
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 901) {
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 902) cprm->to_skip = pos - cprm->pos;
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 903) }
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 904) EXPORT_SYMBOL(dump_skip_to);
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 905)
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 906) void dump_skip(struct coredump_params *cprm, size_t nr)
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 907) {
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 908) cprm->to_skip += nr;
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 909) }
9b56d54380adb (Al Viro 2013-10-08 09:26:08 -0400 910) EXPORT_SYMBOL(dump_skip);
22a8cb8248ba5 (Al Viro 2013-10-08 11:05:01 -0400 911)
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 912) #ifdef CONFIG_ELF_CORE
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 913) int dump_user_range(struct coredump_params *cprm, unsigned long start,
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 914) unsigned long len)
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 915) {
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 916) unsigned long addr;
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 917)
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 918) for (addr = start; addr < start + len; addr += PAGE_SIZE) {
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 919) struct page *page;
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 920) int stop;
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 921)
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 922) /*
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 923) * To avoid having to allocate page tables for virtual address
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 924) * ranges that have never been used yet, and also to make it
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 925) * easy to generate sparse core files, use a helper that returns
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 926) * NULL when encountering an empty page table entry that would
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 927) * otherwise have been filled with the zero page.
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 928) */
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 929) page = get_dump_page(addr);
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 930) if (page) {
3159ed57792be (Ira Weiny 2021-02-25 17:22:22 -0800 931) void *kaddr = kmap_local_page(page);
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 932)
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 933) stop = !dump_emit(cprm, kaddr, PAGE_SIZE);
3159ed57792be (Ira Weiny 2021-02-25 17:22:22 -0800 934) kunmap_local(kaddr);
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 935) put_page(page);
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 936) if (stop)
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 937) return 0;
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 938) } else {
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 939) dump_skip(cprm, PAGE_SIZE);
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 940) }
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 941) }
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 942) return 1;
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 943) }
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 944) #endif
afc63a97b764b (Jann Horn 2020-10-15 20:12:46 -0700 945)
22a8cb8248ba5 (Al Viro 2013-10-08 11:05:01 -0400 946) int dump_align(struct coredump_params *cprm, int align)
22a8cb8248ba5 (Al Viro 2013-10-08 11:05:01 -0400 947) {
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 948) unsigned mod = (cprm->pos + cprm->to_skip) & (align - 1);
22a8cb8248ba5 (Al Viro 2013-10-08 11:05:01 -0400 949) if (align & (align - 1))
db51242d89b30 (Al Viro 2013-11-15 21:55:52 -0500 950) return 0;
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 951) if (mod)
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 952) cprm->to_skip += align - mod;
d0f1088b31db2 (Al Viro 2020-03-08 09:16:37 -0400 953) return 1;
22a8cb8248ba5 (Al Viro 2013-10-08 11:05:01 -0400 954) }
22a8cb8248ba5 (Al Viro 2013-10-08 11:05:01 -0400 955) EXPORT_SYMBOL(dump_align);
4d22c75d4c7b5 (Dave Kleikamp 2017-01-11 13:25:00 -0600 956)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 957) /*
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 958) * The purpose of always_dump_vma() is to make sure that special kernel mappings
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 959) * that are useful for post-mortem analysis are included in every core dump.
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 960) * In that way we ensure that the core dump is fully interpretable later
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 961) * without matching up the same kernel and hardware config to see what PC values
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 962) * meant. These special mappings include - vDSO, vsyscall, and other
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 963) * architecture specific mappings
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 964) */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 965) static bool always_dump_vma(struct vm_area_struct *vma)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 966) {
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 967) /* Any vsyscall mappings? */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 968) if (vma == get_gate_vma(vma->vm_mm))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 969) return true;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 970)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 971) /*
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 972) * Assume that all vmas with a .name op should always be dumped.
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 973) * If this changes, a new vm_ops field can easily be added.
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 974) */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 975) if (vma->vm_ops && vma->vm_ops->name && vma->vm_ops->name(vma))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 976) return true;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 977)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 978) /*
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 979) * arch_vma_name() returns non-NULL for special architecture mappings,
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 980) * such as vDSO sections.
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 981) */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 982) if (arch_vma_name(vma))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 983) return true;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 984)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 985) return false;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 986) }
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 987)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 988) /*
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 989) * Decide how much of @vma's contents should be included in a core dump.
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 990) */
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 991) static unsigned long vma_dump_size(struct vm_area_struct *vma,
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 992) unsigned long mm_flags)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 993) {
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 994) #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 995)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 996) /* always dump the vdso and vsyscall sections */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 997) if (always_dump_vma(vma))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 998) goto whole;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 999)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1000) if (vma->vm_flags & VM_DONTDUMP)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1001) return 0;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1002)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1003) /* support for DAX */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1004) if (vma_is_dax(vma)) {
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1005) if ((vma->vm_flags & VM_SHARED) && FILTER(DAX_SHARED))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1006) goto whole;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1007) if (!(vma->vm_flags & VM_SHARED) && FILTER(DAX_PRIVATE))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1008) goto whole;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1009) return 0;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1010) }
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1011)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1012) /* Hugetlb memory check */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1013) if (is_vm_hugetlb_page(vma)) {
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1014) if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1015) goto whole;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1016) if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1017) goto whole;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1018) return 0;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1019) }
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1020)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1021) /* Do not dump I/O mapped devices or special mappings */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1022) if (vma->vm_flags & VM_IO)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1023) return 0;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1024)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1025) /* By default, dump shared memory if mapped from an anonymous file. */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1026) if (vma->vm_flags & VM_SHARED) {
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1027) if (file_inode(vma->vm_file)->i_nlink == 0 ?
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1028) FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1029) goto whole;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1030) return 0;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1031) }
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1032)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1033) /* Dump segments that have been written to. */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1034) if ((!IS_ENABLED(CONFIG_MMU) || vma->anon_vma) && FILTER(ANON_PRIVATE))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1035) goto whole;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1036) if (vma->vm_file == NULL)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1037) return 0;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1038)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1039) if (FILTER(MAPPED_PRIVATE))
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1040) goto whole;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1041)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1042) /*
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1043) * If this is the beginning of an executable file mapping,
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1044) * dump the first page to aid in determining what was mapped here.
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1045) */
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1046) if (FILTER(ELF_HEADERS) &&
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1047) vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ) &&
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1048) (READ_ONCE(file_inode(vma->vm_file)->i_mode) & 0111) != 0)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1049) return PAGE_SIZE;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1050)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1051) #undef FILTER
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1052)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1053) return 0;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1054)
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1055) whole:
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1056) return vma->vm_end - vma->vm_start;
429a22e776a2b (Jann Horn 2020-10-15 20:12:50 -0700 1057) }
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1058)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1059) static struct vm_area_struct *first_vma(struct task_struct *tsk,
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1060) struct vm_area_struct *gate_vma)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1061) {
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1062) struct vm_area_struct *ret = tsk->mm->mmap;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1063)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1064) if (ret)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1065) return ret;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1066) return gate_vma;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1067) }
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1068)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1069) /*
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1070) * Helper function for iterating across a vma list. It ensures that the caller
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1071) * will visit `gate_vma' prior to terminating the search.
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1072) */
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1073) static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1074) struct vm_area_struct *gate_vma)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1075) {
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1076) struct vm_area_struct *ret;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1077)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1078) ret = this_vma->vm_next;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1079) if (ret)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1080) return ret;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1081) if (this_vma == gate_vma)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1082) return NULL;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1083) return gate_vma;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1084) }
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1085)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1086) /*
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1087) * Under the mmap_lock, take a snapshot of relevant information about the task's
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1088) * VMAs.
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1089) */
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1090) int dump_vma_snapshot(struct coredump_params *cprm, int *vma_count,
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1091) struct core_vma_metadata **vma_meta,
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1092) size_t *vma_data_size_ptr)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1093) {
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1094) struct vm_area_struct *vma, *gate_vma;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1095) struct mm_struct *mm = current->mm;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1096) int i;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1097) size_t vma_data_size = 0;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1098)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1099) /*
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1100) * Once the stack expansion code is fixed to not change VMA bounds
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1101) * under mmap_lock in read mode, this can be changed to take the
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1102) * mmap_lock in read mode.
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1103) */
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1104) if (mmap_write_lock_killable(mm))
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1105) return -EINTR;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1106)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1107) gate_vma = get_gate_vma(mm);
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1108) *vma_count = mm->map_count + (gate_vma ? 1 : 0);
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1109)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1110) *vma_meta = kvmalloc_array(*vma_count, sizeof(**vma_meta), GFP_KERNEL);
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1111) if (!*vma_meta) {
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1112) mmap_write_unlock(mm);
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1113) return -ENOMEM;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1114) }
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1115)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1116) for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1117) vma = next_vma(vma, gate_vma), i++) {
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1118) struct core_vma_metadata *m = (*vma_meta) + i;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1119)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1120) m->start = vma->vm_start;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1121) m->end = vma->vm_end;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1122) m->flags = vma->vm_flags;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1123) m->dump_size = vma_dump_size(vma, cprm->mm_flags);
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1124)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1125) vma_data_size += m->dump_size;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1126) }
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1127)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1128) mmap_write_unlock(mm);
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1129)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1130) if (WARN_ON(i != *vma_count))
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1131) return -EFAULT;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1132)
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1133) *vma_data_size_ptr = vma_data_size;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1134) return 0;
a07279c9a8cd7 (Jann Horn 2020-10-15 20:12:54 -0700 1135) }