Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 1 | #include <linux/slab.h> |
| 2 | #include <linux/file.h> |
| 3 | #include <linux/fdtable.h> |
Andrey Ryabinin | 70d78fe | 2016-11-10 10:46:38 -0800 | [diff] [blame] | 4 | #include <linux/freezer.h> |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 5 | #include <linux/mm.h> |
| 6 | #include <linux/stat.h> |
| 7 | #include <linux/fcntl.h> |
| 8 | #include <linux/swap.h> |
| 9 | #include <linux/string.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/perf_event.h> |
| 13 | #include <linux/highmem.h> |
| 14 | #include <linux/spinlock.h> |
| 15 | #include <linux/key.h> |
| 16 | #include <linux/personality.h> |
| 17 | #include <linux/binfmts.h> |
Alex Kelly | 179899f | 2012-10-04 17:15:24 -0700 | [diff] [blame] | 18 | #include <linux/coredump.h> |
Ingo Molnar | f7ccbae | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 19 | #include <linux/sched/coredump.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 20 | #include <linux/sched/signal.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame^] | 21 | #include <linux/sched/task_stack.h> |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 22 | #include <linux/utsname.h> |
| 23 | #include <linux/pid_namespace.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/namei.h> |
| 26 | #include <linux/mount.h> |
| 27 | #include <linux/security.h> |
| 28 | #include <linux/syscalls.h> |
| 29 | #include <linux/tsacct_kern.h> |
| 30 | #include <linux/cn_proc.h> |
| 31 | #include <linux/audit.h> |
| 32 | #include <linux/tracehook.h> |
| 33 | #include <linux/kmod.h> |
| 34 | #include <linux/fsnotify.h> |
| 35 | #include <linux/fs_struct.h> |
| 36 | #include <linux/pipe_fs_i.h> |
| 37 | #include <linux/oom.h> |
| 38 | #include <linux/compat.h> |
Jann Horn | 378c652 | 2016-03-22 14:25:36 -0700 | [diff] [blame] | 39 | #include <linux/fs.h> |
| 40 | #include <linux/path.h> |
Arnd Bergmann | 03927c8 | 2015-11-25 16:22:25 +0100 | [diff] [blame] | 41 | #include <linux/timekeeping.h> |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 42 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 43 | #include <linux/uaccess.h> |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 44 | #include <asm/mmu_context.h> |
| 45 | #include <asm/tlb.h> |
| 46 | #include <asm/exec.h> |
| 47 | |
| 48 | #include <trace/events/task.h> |
| 49 | #include "internal.h" |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 50 | |
| 51 | #include <trace/events/sched.h> |
| 52 | |
| 53 | int core_uses_pid; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 54 | unsigned int core_pipe_limit; |
Oleg Nesterov | 3ceadcf | 2013-07-03 15:08:22 -0700 | [diff] [blame] | 55 | char core_pattern[CORENAME_MAX_SIZE] = "core"; |
| 56 | static int core_name_size = CORENAME_MAX_SIZE; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 57 | |
| 58 | struct core_name { |
| 59 | char *corename; |
| 60 | int used, size; |
| 61 | }; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 62 | |
| 63 | /* The maximal length of core_pattern is also specified in sysctl.c */ |
| 64 | |
Oleg Nesterov | 3ceadcf | 2013-07-03 15:08:22 -0700 | [diff] [blame] | 65 | static int expand_corename(struct core_name *cn, int size) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 66 | { |
Oleg Nesterov | e7fd154 | 2013-07-03 15:08:16 -0700 | [diff] [blame] | 67 | char *corename = krealloc(cn->corename, size, GFP_KERNEL); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 68 | |
Oleg Nesterov | e7fd154 | 2013-07-03 15:08:16 -0700 | [diff] [blame] | 69 | if (!corename) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 70 | return -ENOMEM; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 71 | |
Oleg Nesterov | 3ceadcf | 2013-07-03 15:08:22 -0700 | [diff] [blame] | 72 | if (size > core_name_size) /* racy but harmless */ |
| 73 | core_name_size = size; |
| 74 | |
| 75 | cn->size = ksize(corename); |
Oleg Nesterov | e7fd154 | 2013-07-03 15:08:16 -0700 | [diff] [blame] | 76 | cn->corename = corename; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Nicolas Iooss | b4176b7 | 2015-06-25 15:03:53 -0700 | [diff] [blame] | 80 | static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt, |
| 81 | va_list arg) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 82 | { |
Oleg Nesterov | 5fe9d8c | 2013-07-03 15:08:19 -0700 | [diff] [blame] | 83 | int free, need; |
Eric Dumazet | 404ca80 | 2014-04-19 10:15:07 -0700 | [diff] [blame] | 84 | va_list arg_copy; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 85 | |
Oleg Nesterov | 5fe9d8c | 2013-07-03 15:08:19 -0700 | [diff] [blame] | 86 | again: |
| 87 | free = cn->size - cn->used; |
Eric Dumazet | 404ca80 | 2014-04-19 10:15:07 -0700 | [diff] [blame] | 88 | |
| 89 | va_copy(arg_copy, arg); |
| 90 | need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy); |
| 91 | va_end(arg_copy); |
| 92 | |
Oleg Nesterov | 5fe9d8c | 2013-07-03 15:08:19 -0700 | [diff] [blame] | 93 | if (need < free) { |
| 94 | cn->used += need; |
| 95 | return 0; |
| 96 | } |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 97 | |
Oleg Nesterov | 3ceadcf | 2013-07-03 15:08:22 -0700 | [diff] [blame] | 98 | if (!expand_corename(cn, cn->size + need - free + 1)) |
Oleg Nesterov | 5fe9d8c | 2013-07-03 15:08:19 -0700 | [diff] [blame] | 99 | goto again; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 100 | |
Oleg Nesterov | 5fe9d8c | 2013-07-03 15:08:19 -0700 | [diff] [blame] | 101 | return -ENOMEM; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Nicolas Iooss | b4176b7 | 2015-06-25 15:03:53 -0700 | [diff] [blame] | 104 | static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...) |
Oleg Nesterov | bc03c69 | 2013-07-03 15:08:17 -0700 | [diff] [blame] | 105 | { |
| 106 | va_list arg; |
| 107 | int ret; |
| 108 | |
| 109 | va_start(arg, fmt); |
| 110 | ret = cn_vprintf(cn, fmt, arg); |
| 111 | va_end(arg); |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
Nicolas Iooss | b4176b7 | 2015-06-25 15:03:53 -0700 | [diff] [blame] | 116 | static __printf(2, 3) |
| 117 | int cn_esc_printf(struct core_name *cn, const char *fmt, ...) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 118 | { |
Oleg Nesterov | 923bed03 | 2013-07-03 15:08:20 -0700 | [diff] [blame] | 119 | int cur = cn->used; |
| 120 | va_list arg; |
| 121 | int ret; |
| 122 | |
| 123 | va_start(arg, fmt); |
| 124 | ret = cn_vprintf(cn, fmt, arg); |
| 125 | va_end(arg); |
| 126 | |
Jann Horn | ac94b6e | 2016-01-20 15:00:08 -0800 | [diff] [blame] | 127 | if (ret == 0) { |
| 128 | /* |
| 129 | * Ensure that this coredump name component can't cause the |
| 130 | * resulting corefile path to consist of a ".." or ".". |
| 131 | */ |
| 132 | if ((cn->used - cur == 1 && cn->corename[cur] == '.') || |
| 133 | (cn->used - cur == 2 && cn->corename[cur] == '.' |
| 134 | && cn->corename[cur+1] == '.')) |
| 135 | cn->corename[cur] = '!'; |
| 136 | |
| 137 | /* |
| 138 | * Empty names are fishy and could be used to create a "//" in a |
| 139 | * corefile name, causing the coredump to happen one directory |
| 140 | * level too high. Enforce that all components of the core |
| 141 | * pattern are at least one character long. |
| 142 | */ |
| 143 | if (cn->used == cur) |
| 144 | ret = cn_printf(cn, "!"); |
| 145 | } |
| 146 | |
Oleg Nesterov | 923bed03 | 2013-07-03 15:08:20 -0700 | [diff] [blame] | 147 | for (; cur < cn->used; ++cur) { |
| 148 | if (cn->corename[cur] == '/') |
| 149 | cn->corename[cur] = '!'; |
| 150 | } |
| 151 | return ret; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static int cn_print_exe_file(struct core_name *cn) |
| 155 | { |
| 156 | struct file *exe_file; |
| 157 | char *pathbuf, *path; |
| 158 | int ret; |
| 159 | |
| 160 | exe_file = get_mm_exe_file(current->mm); |
Oleg Nesterov | 923bed03 | 2013-07-03 15:08:20 -0700 | [diff] [blame] | 161 | if (!exe_file) |
| 162 | return cn_esc_printf(cn, "%s (path unknown)", current->comm); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 163 | |
| 164 | pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY); |
| 165 | if (!pathbuf) { |
| 166 | ret = -ENOMEM; |
| 167 | goto put_exe_file; |
| 168 | } |
| 169 | |
Miklos Szeredi | 9bf39ab | 2015-06-19 10:29:13 +0200 | [diff] [blame] | 170 | path = file_path(exe_file, pathbuf, PATH_MAX); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 171 | if (IS_ERR(path)) { |
| 172 | ret = PTR_ERR(path); |
| 173 | goto free_buf; |
| 174 | } |
| 175 | |
Oleg Nesterov | 923bed03 | 2013-07-03 15:08:20 -0700 | [diff] [blame] | 176 | ret = cn_esc_printf(cn, "%s", path); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 177 | |
| 178 | free_buf: |
| 179 | kfree(pathbuf); |
| 180 | put_exe_file: |
| 181 | fput(exe_file); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | /* format_corename will inspect the pattern parameter, and output a |
| 186 | * name into corename, which must have space for at least |
| 187 | * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator. |
| 188 | */ |
Oleg Nesterov | 12a2b4b | 2012-10-04 17:15:25 -0700 | [diff] [blame] | 189 | static int format_corename(struct core_name *cn, struct coredump_params *cprm) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 190 | { |
| 191 | const struct cred *cred = current_cred(); |
| 192 | const char *pat_ptr = core_pattern; |
| 193 | int ispipe = (*pat_ptr == '|'); |
| 194 | int pid_in_pattern = 0; |
| 195 | int err = 0; |
| 196 | |
Oleg Nesterov | e7fd154 | 2013-07-03 15:08:16 -0700 | [diff] [blame] | 197 | cn->used = 0; |
Oleg Nesterov | 3ceadcf | 2013-07-03 15:08:22 -0700 | [diff] [blame] | 198 | cn->corename = NULL; |
| 199 | if (expand_corename(cn, core_name_size)) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 200 | return -ENOMEM; |
Oleg Nesterov | 888ffc5 | 2013-07-03 15:08:23 -0700 | [diff] [blame] | 201 | cn->corename[0] = '\0'; |
| 202 | |
| 203 | if (ispipe) |
| 204 | ++pat_ptr; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 205 | |
| 206 | /* Repeat as long as we have more pattern to process and more output |
| 207 | space */ |
| 208 | while (*pat_ptr) { |
| 209 | if (*pat_ptr != '%') { |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 210 | err = cn_printf(cn, "%c", *pat_ptr++); |
| 211 | } else { |
| 212 | switch (*++pat_ptr) { |
| 213 | /* single % at the end, drop that */ |
| 214 | case 0: |
| 215 | goto out; |
| 216 | /* Double percent, output one percent */ |
| 217 | case '%': |
| 218 | err = cn_printf(cn, "%c", '%'); |
| 219 | break; |
| 220 | /* pid */ |
| 221 | case 'p': |
| 222 | pid_in_pattern = 1; |
| 223 | err = cn_printf(cn, "%d", |
| 224 | task_tgid_vnr(current)); |
| 225 | break; |
Stéphane Graber | 65aafb1 | 2013-09-11 14:24:32 -0700 | [diff] [blame] | 226 | /* global pid */ |
| 227 | case 'P': |
| 228 | err = cn_printf(cn, "%d", |
| 229 | task_tgid_nr(current)); |
| 230 | break; |
Oleg Nesterov | b03023e | 2014-10-13 15:53:35 -0700 | [diff] [blame] | 231 | case 'i': |
| 232 | err = cn_printf(cn, "%d", |
| 233 | task_pid_vnr(current)); |
| 234 | break; |
| 235 | case 'I': |
| 236 | err = cn_printf(cn, "%d", |
| 237 | task_pid_nr(current)); |
| 238 | break; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 239 | /* uid */ |
| 240 | case 'u': |
Nicolas Iooss | 5202efe | 2015-06-25 15:03:51 -0700 | [diff] [blame] | 241 | err = cn_printf(cn, "%u", |
| 242 | from_kuid(&init_user_ns, |
| 243 | cred->uid)); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 244 | break; |
| 245 | /* gid */ |
| 246 | case 'g': |
Nicolas Iooss | 5202efe | 2015-06-25 15:03:51 -0700 | [diff] [blame] | 247 | err = cn_printf(cn, "%u", |
| 248 | from_kgid(&init_user_ns, |
| 249 | cred->gid)); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 250 | break; |
Oleg Nesterov | 12a2b4b | 2012-10-04 17:15:25 -0700 | [diff] [blame] | 251 | case 'd': |
| 252 | err = cn_printf(cn, "%d", |
| 253 | __get_dumpable(cprm->mm_flags)); |
| 254 | break; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 255 | /* signal that caused the coredump */ |
| 256 | case 's': |
Nicolas Iooss | b4176b7 | 2015-06-25 15:03:53 -0700 | [diff] [blame] | 257 | err = cn_printf(cn, "%d", |
| 258 | cprm->siginfo->si_signo); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 259 | break; |
| 260 | /* UNIX time of coredump */ |
| 261 | case 't': { |
Arnd Bergmann | 03927c8 | 2015-11-25 16:22:25 +0100 | [diff] [blame] | 262 | time64_t time; |
| 263 | |
| 264 | time = ktime_get_real_seconds(); |
| 265 | err = cn_printf(cn, "%lld", time); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | /* hostname */ |
Oleg Nesterov | 923bed03 | 2013-07-03 15:08:20 -0700 | [diff] [blame] | 269 | case 'h': |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 270 | down_read(&uts_sem); |
Oleg Nesterov | 923bed03 | 2013-07-03 15:08:20 -0700 | [diff] [blame] | 271 | err = cn_esc_printf(cn, "%s", |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 272 | utsname()->nodename); |
| 273 | up_read(&uts_sem); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 274 | break; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 275 | /* executable */ |
Oleg Nesterov | 923bed03 | 2013-07-03 15:08:20 -0700 | [diff] [blame] | 276 | case 'e': |
| 277 | err = cn_esc_printf(cn, "%s", current->comm); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 278 | break; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 279 | case 'E': |
| 280 | err = cn_print_exe_file(cn); |
| 281 | break; |
| 282 | /* core limit size */ |
| 283 | case 'c': |
| 284 | err = cn_printf(cn, "%lu", |
| 285 | rlimit(RLIMIT_CORE)); |
| 286 | break; |
| 287 | default: |
| 288 | break; |
| 289 | } |
| 290 | ++pat_ptr; |
| 291 | } |
| 292 | |
| 293 | if (err) |
| 294 | return err; |
| 295 | } |
| 296 | |
Oleg Nesterov | 888ffc5 | 2013-07-03 15:08:23 -0700 | [diff] [blame] | 297 | out: |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 298 | /* Backward compatibility with core_uses_pid: |
| 299 | * |
| 300 | * If core_pattern does not include a %p (as is the default) |
| 301 | * and core_uses_pid is set, then .%pid will be appended to |
| 302 | * the filename. Do not do this for piped commands. */ |
| 303 | if (!ispipe && !pid_in_pattern && core_uses_pid) { |
| 304 | err = cn_printf(cn, ".%d", task_tgid_vnr(current)); |
| 305 | if (err) |
| 306 | return err; |
| 307 | } |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 308 | return ispipe; |
| 309 | } |
| 310 | |
Oleg Nesterov | 5fa534c | 2015-11-06 16:32:31 -0800 | [diff] [blame] | 311 | static int zap_process(struct task_struct *start, int exit_code, int flags) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 312 | { |
| 313 | struct task_struct *t; |
| 314 | int nr = 0; |
| 315 | |
Oleg Nesterov | 5fa534c | 2015-11-06 16:32:31 -0800 | [diff] [blame] | 316 | /* ignore all signals except SIGKILL, see prepare_signal() */ |
| 317 | start->signal->flags = SIGNAL_GROUP_COREDUMP | flags; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 318 | start->signal->group_exit_code = exit_code; |
| 319 | start->signal->group_stop_count = 0; |
| 320 | |
Oleg Nesterov | d61ba58 | 2015-11-06 16:32:34 -0800 | [diff] [blame] | 321 | for_each_thread(start, t) { |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 322 | task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK); |
| 323 | if (t != current && t->mm) { |
| 324 | sigaddset(&t->pending.signal, SIGKILL); |
| 325 | signal_wake_up(t, 1); |
| 326 | nr++; |
| 327 | } |
Oleg Nesterov | d61ba58 | 2015-11-06 16:32:34 -0800 | [diff] [blame] | 328 | } |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 329 | |
| 330 | return nr; |
| 331 | } |
| 332 | |
Oleg Nesterov | 403bad7 | 2013-04-30 15:28:10 -0700 | [diff] [blame] | 333 | static int zap_threads(struct task_struct *tsk, struct mm_struct *mm, |
| 334 | struct core_state *core_state, int exit_code) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 335 | { |
| 336 | struct task_struct *g, *p; |
| 337 | unsigned long flags; |
| 338 | int nr = -EAGAIN; |
| 339 | |
| 340 | spin_lock_irq(&tsk->sighand->siglock); |
| 341 | if (!signal_group_exit(tsk->signal)) { |
| 342 | mm->core_state = core_state; |
Oleg Nesterov | 6cd8f0a | 2013-04-30 15:28:12 -0700 | [diff] [blame] | 343 | tsk->signal->group_exit_task = tsk; |
Oleg Nesterov | 5fa534c | 2015-11-06 16:32:31 -0800 | [diff] [blame] | 344 | nr = zap_process(tsk, exit_code, 0); |
Oleg Nesterov | 403bad7 | 2013-04-30 15:28:10 -0700 | [diff] [blame] | 345 | clear_tsk_thread_flag(tsk, TIF_SIGPENDING); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 346 | } |
| 347 | spin_unlock_irq(&tsk->sighand->siglock); |
| 348 | if (unlikely(nr < 0)) |
| 349 | return nr; |
| 350 | |
Silesh C V | aed8adb | 2014-07-23 13:59:59 -0700 | [diff] [blame] | 351 | tsk->flags |= PF_DUMPCORE; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 352 | if (atomic_read(&mm->mm_users) == nr + 1) |
| 353 | goto done; |
| 354 | /* |
| 355 | * We should find and kill all tasks which use this mm, and we should |
| 356 | * count them correctly into ->nr_threads. We don't take tasklist |
| 357 | * lock, but this is safe wrt: |
| 358 | * |
| 359 | * fork: |
| 360 | * None of sub-threads can fork after zap_process(leader). All |
| 361 | * processes which were created before this point should be |
| 362 | * visible to zap_threads() because copy_process() adds the new |
| 363 | * process to the tail of init_task.tasks list, and lock/unlock |
| 364 | * of ->siglock provides a memory barrier. |
| 365 | * |
| 366 | * do_exit: |
| 367 | * The caller holds mm->mmap_sem. This means that the task which |
| 368 | * uses this mm can't pass exit_mm(), so it can't exit or clear |
| 369 | * its ->mm. |
| 370 | * |
| 371 | * de_thread: |
| 372 | * It does list_replace_rcu(&leader->tasks, ¤t->tasks), |
| 373 | * we must see either old or new leader, this does not matter. |
| 374 | * However, it can change p->sighand, so lock_task_sighand(p) |
| 375 | * must be used. Since p->mm != NULL and we hold ->mmap_sem |
| 376 | * it can't fail. |
| 377 | * |
| 378 | * Note also that "g" can be the old leader with ->mm == NULL |
| 379 | * and already unhashed and thus removed from ->thread_group. |
| 380 | * This is OK, __unhash_process()->list_del_rcu() does not |
| 381 | * clear the ->next pointer, we will find the new leader via |
| 382 | * next_thread(). |
| 383 | */ |
| 384 | rcu_read_lock(); |
| 385 | for_each_process(g) { |
| 386 | if (g == tsk->group_leader) |
| 387 | continue; |
| 388 | if (g->flags & PF_KTHREAD) |
| 389 | continue; |
Oleg Nesterov | d61ba58 | 2015-11-06 16:32:34 -0800 | [diff] [blame] | 390 | |
| 391 | for_each_thread(g, p) { |
| 392 | if (unlikely(!p->mm)) |
| 393 | continue; |
| 394 | if (unlikely(p->mm == mm)) { |
| 395 | lock_task_sighand(p, &flags); |
| 396 | nr += zap_process(p, exit_code, |
| 397 | SIGNAL_GROUP_EXIT); |
| 398 | unlock_task_sighand(p, &flags); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 399 | } |
Oleg Nesterov | d61ba58 | 2015-11-06 16:32:34 -0800 | [diff] [blame] | 400 | break; |
| 401 | } |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 402 | } |
| 403 | rcu_read_unlock(); |
| 404 | done: |
| 405 | atomic_set(&core_state->nr_threads, nr); |
| 406 | return nr; |
| 407 | } |
| 408 | |
| 409 | static int coredump_wait(int exit_code, struct core_state *core_state) |
| 410 | { |
| 411 | struct task_struct *tsk = current; |
| 412 | struct mm_struct *mm = tsk->mm; |
| 413 | int core_waiters = -EBUSY; |
| 414 | |
| 415 | init_completion(&core_state->startup); |
| 416 | core_state->dumper.task = tsk; |
| 417 | core_state->dumper.next = NULL; |
| 418 | |
Michal Hocko | 4136c26 | 2016-05-23 16:25:57 -0700 | [diff] [blame] | 419 | if (down_write_killable(&mm->mmap_sem)) |
| 420 | return -EINTR; |
| 421 | |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 422 | if (!mm->core_state) |
| 423 | core_waiters = zap_threads(tsk, mm, core_state, exit_code); |
| 424 | up_write(&mm->mmap_sem); |
| 425 | |
| 426 | if (core_waiters > 0) { |
| 427 | struct core_thread *ptr; |
| 428 | |
Andrey Ryabinin | 70d78fe | 2016-11-10 10:46:38 -0800 | [diff] [blame] | 429 | freezer_do_not_count(); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 430 | wait_for_completion(&core_state->startup); |
Andrey Ryabinin | 70d78fe | 2016-11-10 10:46:38 -0800 | [diff] [blame] | 431 | freezer_count(); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 432 | /* |
| 433 | * Wait for all the threads to become inactive, so that |
| 434 | * all the thread context (extended register state, like |
| 435 | * fpu etc) gets copied to the memory. |
| 436 | */ |
| 437 | ptr = core_state->dumper.next; |
| 438 | while (ptr != NULL) { |
| 439 | wait_task_inactive(ptr->task, 0); |
| 440 | ptr = ptr->next; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return core_waiters; |
| 445 | } |
| 446 | |
Oleg Nesterov | acdedd9 | 2013-04-30 15:28:13 -0700 | [diff] [blame] | 447 | static void coredump_finish(struct mm_struct *mm, bool core_dumped) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 448 | { |
| 449 | struct core_thread *curr, *next; |
| 450 | struct task_struct *task; |
| 451 | |
Oleg Nesterov | 6cd8f0a | 2013-04-30 15:28:12 -0700 | [diff] [blame] | 452 | spin_lock_irq(¤t->sighand->siglock); |
Oleg Nesterov | acdedd9 | 2013-04-30 15:28:13 -0700 | [diff] [blame] | 453 | if (core_dumped && !__fatal_signal_pending(current)) |
| 454 | current->signal->group_exit_code |= 0x80; |
Oleg Nesterov | 6cd8f0a | 2013-04-30 15:28:12 -0700 | [diff] [blame] | 455 | current->signal->group_exit_task = NULL; |
| 456 | current->signal->flags = SIGNAL_GROUP_EXIT; |
| 457 | spin_unlock_irq(¤t->sighand->siglock); |
| 458 | |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 459 | next = mm->core_state->dumper.next; |
| 460 | while ((curr = next) != NULL) { |
| 461 | next = curr->next; |
| 462 | task = curr->task; |
| 463 | /* |
| 464 | * see exit_mm(), curr->task must not see |
| 465 | * ->task == NULL before we read ->next. |
| 466 | */ |
| 467 | smp_mb(); |
| 468 | curr->task = NULL; |
| 469 | wake_up_process(task); |
| 470 | } |
| 471 | |
| 472 | mm->core_state = NULL; |
| 473 | } |
| 474 | |
Oleg Nesterov | 528f827 | 2013-04-30 15:28:15 -0700 | [diff] [blame] | 475 | static bool dump_interrupted(void) |
| 476 | { |
| 477 | /* |
| 478 | * SIGKILL or freezing() interrupt the coredumping. Perhaps we |
| 479 | * can do try_to_freeze() and check __fatal_signal_pending(), |
| 480 | * but then we need to teach dump_write() to restart and clear |
| 481 | * TIF_SIGPENDING. |
| 482 | */ |
| 483 | return signal_pending(current); |
| 484 | } |
| 485 | |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 486 | static void wait_for_dump_helpers(struct file *file) |
| 487 | { |
Al Viro | de32ec4 | 2013-03-21 11:16:56 -0400 | [diff] [blame] | 488 | struct pipe_inode_info *pipe = file->private_data; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 489 | |
| 490 | pipe_lock(pipe); |
| 491 | pipe->readers++; |
| 492 | pipe->writers--; |
Oleg Nesterov | dc7ee2a | 2013-04-30 15:28:17 -0700 | [diff] [blame] | 493 | wake_up_interruptible_sync(&pipe->wait); |
| 494 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
| 495 | pipe_unlock(pipe); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 496 | |
Oleg Nesterov | dc7ee2a | 2013-04-30 15:28:17 -0700 | [diff] [blame] | 497 | /* |
| 498 | * We actually want wait_event_freezable() but then we need |
| 499 | * to clear TIF_SIGPENDING and improve dump_interrupted(). |
| 500 | */ |
| 501 | wait_event_interruptible(pipe->wait, pipe->readers == 1); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 502 | |
Oleg Nesterov | dc7ee2a | 2013-04-30 15:28:17 -0700 | [diff] [blame] | 503 | pipe_lock(pipe); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 504 | pipe->readers--; |
| 505 | pipe->writers++; |
| 506 | pipe_unlock(pipe); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* |
| 510 | * umh_pipe_setup |
| 511 | * helper function to customize the process used |
| 512 | * to collect the core in userspace. Specifically |
| 513 | * it sets up a pipe and installs it as fd 0 (stdin) |
| 514 | * for the process. Returns 0 on success, or |
| 515 | * PTR_ERR on failure. |
| 516 | * Note that it also sets the core limit to 1. This |
| 517 | * is a special value that we use to trap recursive |
| 518 | * core dumps |
| 519 | */ |
| 520 | static int umh_pipe_setup(struct subprocess_info *info, struct cred *new) |
| 521 | { |
| 522 | struct file *files[2]; |
| 523 | struct coredump_params *cp = (struct coredump_params *)info->data; |
| 524 | int err = create_pipe_files(files, 0); |
| 525 | if (err) |
| 526 | return err; |
| 527 | |
| 528 | cp->file = files[1]; |
| 529 | |
Al Viro | 45525b2 | 2012-10-16 13:30:07 -0400 | [diff] [blame] | 530 | err = replace_fd(0, files[0], 0); |
| 531 | fput(files[0]); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 532 | /* and disallow core files too */ |
| 533 | current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1}; |
| 534 | |
Al Viro | 45525b2 | 2012-10-16 13:30:07 -0400 | [diff] [blame] | 535 | return err; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 536 | } |
| 537 | |
Al Viro | ec57941 | 2013-10-13 17:57:29 -0400 | [diff] [blame] | 538 | void do_coredump(const siginfo_t *siginfo) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 539 | { |
| 540 | struct core_state core_state; |
| 541 | struct core_name cn; |
| 542 | struct mm_struct *mm = current->mm; |
| 543 | struct linux_binfmt * binfmt; |
| 544 | const struct cred *old_cred; |
| 545 | struct cred *cred; |
| 546 | int retval = 0; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 547 | int ispipe; |
| 548 | struct files_struct *displaced; |
Jann Horn | fbb1816 | 2015-09-09 15:38:28 -0700 | [diff] [blame] | 549 | /* require nonrelative corefile path and be extra careful */ |
| 550 | bool need_suid_safe = false; |
Oleg Nesterov | acdedd9 | 2013-04-30 15:28:13 -0700 | [diff] [blame] | 551 | bool core_dumped = false; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 552 | static atomic_t core_dump_count = ATOMIC_INIT(0); |
| 553 | struct coredump_params cprm = { |
Denys Vlasenko | 5ab1c30 | 2012-10-04 17:15:29 -0700 | [diff] [blame] | 554 | .siginfo = siginfo, |
Al Viro | 541880d | 2012-11-05 13:11:26 -0500 | [diff] [blame] | 555 | .regs = signal_pt_regs(), |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 556 | .limit = rlimit(RLIMIT_CORE), |
| 557 | /* |
| 558 | * We must use the same mm->flags while dumping core to avoid |
| 559 | * inconsistency of bit flags, since this flag is not protected |
| 560 | * by any locks. |
| 561 | */ |
| 562 | .mm_flags = mm->flags, |
| 563 | }; |
| 564 | |
Denys Vlasenko | 5ab1c30 | 2012-10-04 17:15:29 -0700 | [diff] [blame] | 565 | audit_core_dumps(siginfo->si_signo); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 566 | |
| 567 | binfmt = mm->binfmt; |
| 568 | if (!binfmt || !binfmt->core_dump) |
| 569 | goto fail; |
| 570 | if (!__get_dumpable(cprm.mm_flags)) |
| 571 | goto fail; |
| 572 | |
| 573 | cred = prepare_creds(); |
| 574 | if (!cred) |
| 575 | goto fail; |
| 576 | /* |
| 577 | * We cannot trust fsuid as being the "true" uid of the process |
| 578 | * nor do we know its entire history. We only know it was tainted |
| 579 | * so we dump it as root in mode 2, and only into a controlled |
| 580 | * environment (pipe handler or fully qualified path). |
| 581 | */ |
Kees Cook | e579d2c | 2013-02-27 17:03:15 -0800 | [diff] [blame] | 582 | if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) { |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 583 | /* Setuid core dump mode */ |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 584 | cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */ |
Jann Horn | fbb1816 | 2015-09-09 15:38:28 -0700 | [diff] [blame] | 585 | need_suid_safe = true; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 586 | } |
| 587 | |
Denys Vlasenko | 5ab1c30 | 2012-10-04 17:15:29 -0700 | [diff] [blame] | 588 | retval = coredump_wait(siginfo->si_signo, &core_state); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 589 | if (retval < 0) |
| 590 | goto fail_creds; |
| 591 | |
| 592 | old_cred = override_creds(cred); |
| 593 | |
Oleg Nesterov | 12a2b4b | 2012-10-04 17:15:25 -0700 | [diff] [blame] | 594 | ispipe = format_corename(&cn, &cprm); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 595 | |
Lucas De Marchi | fb96c47 | 2013-04-30 15:28:06 -0700 | [diff] [blame] | 596 | if (ispipe) { |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 597 | int dump_count; |
| 598 | char **helper_argv; |
Lucas De Marchi | 907ed13 | 2013-04-30 15:28:07 -0700 | [diff] [blame] | 599 | struct subprocess_info *sub_info; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 600 | |
| 601 | if (ispipe < 0) { |
| 602 | printk(KERN_WARNING "format_corename failed\n"); |
| 603 | printk(KERN_WARNING "Aborting core\n"); |
Oleg Nesterov | e7fd154 | 2013-07-03 15:08:16 -0700 | [diff] [blame] | 604 | goto fail_unlock; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | if (cprm.limit == 1) { |
| 608 | /* See umh_pipe_setup() which sets RLIMIT_CORE = 1. |
| 609 | * |
| 610 | * Normally core limits are irrelevant to pipes, since |
| 611 | * we're not writing to the file system, but we use |
Bastien Nocera | fcbc32b | 2015-02-05 14:35:05 +0100 | [diff] [blame] | 612 | * cprm.limit of 1 here as a special value, this is a |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 613 | * consistent way to catch recursive crashes. |
| 614 | * We can still crash if the core_pattern binary sets |
| 615 | * RLIM_CORE = !1, but it runs as root, and can do |
| 616 | * lots of stupid things. |
| 617 | * |
| 618 | * Note that we use task_tgid_vnr here to grab the pid |
| 619 | * of the process group leader. That way we get the |
| 620 | * right pid if a thread in a multi-threaded |
| 621 | * core_pattern process dies. |
| 622 | */ |
| 623 | printk(KERN_WARNING |
| 624 | "Process %d(%s) has RLIMIT_CORE set to 1\n", |
| 625 | task_tgid_vnr(current), current->comm); |
| 626 | printk(KERN_WARNING "Aborting core\n"); |
| 627 | goto fail_unlock; |
| 628 | } |
| 629 | cprm.limit = RLIM_INFINITY; |
| 630 | |
| 631 | dump_count = atomic_inc_return(&core_dump_count); |
| 632 | if (core_pipe_limit && (core_pipe_limit < dump_count)) { |
| 633 | printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n", |
| 634 | task_tgid_vnr(current), current->comm); |
| 635 | printk(KERN_WARNING "Skipping core dump\n"); |
| 636 | goto fail_dropcount; |
| 637 | } |
| 638 | |
Oleg Nesterov | 888ffc5 | 2013-07-03 15:08:23 -0700 | [diff] [blame] | 639 | helper_argv = argv_split(GFP_KERNEL, cn.corename, NULL); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 640 | if (!helper_argv) { |
| 641 | printk(KERN_WARNING "%s failed to allocate memory\n", |
| 642 | __func__); |
| 643 | goto fail_dropcount; |
| 644 | } |
| 645 | |
Lucas De Marchi | 907ed13 | 2013-04-30 15:28:07 -0700 | [diff] [blame] | 646 | retval = -ENOMEM; |
| 647 | sub_info = call_usermodehelper_setup(helper_argv[0], |
| 648 | helper_argv, NULL, GFP_KERNEL, |
| 649 | umh_pipe_setup, NULL, &cprm); |
| 650 | if (sub_info) |
| 651 | retval = call_usermodehelper_exec(sub_info, |
| 652 | UMH_WAIT_EXEC); |
| 653 | |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 654 | argv_free(helper_argv); |
| 655 | if (retval) { |
Oleg Nesterov | 888ffc5 | 2013-07-03 15:08:23 -0700 | [diff] [blame] | 656 | printk(KERN_INFO "Core dump to |%s pipe failed\n", |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 657 | cn.corename); |
| 658 | goto close_fail; |
Lucas De Marchi | fb96c47 | 2013-04-30 15:28:06 -0700 | [diff] [blame] | 659 | } |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 660 | } else { |
| 661 | struct inode *inode; |
Jann Horn | 378c652 | 2016-03-22 14:25:36 -0700 | [diff] [blame] | 662 | int open_flags = O_CREAT | O_RDWR | O_NOFOLLOW | |
| 663 | O_LARGEFILE | O_EXCL; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 664 | |
| 665 | if (cprm.limit < binfmt->min_coredump) |
| 666 | goto fail_unlock; |
| 667 | |
Jann Horn | fbb1816 | 2015-09-09 15:38:28 -0700 | [diff] [blame] | 668 | if (need_suid_safe && cn.corename[0] != '/') { |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 669 | printk(KERN_WARNING "Pid %d(%s) can only dump core "\ |
| 670 | "to fully qualified path!\n", |
| 671 | task_tgid_vnr(current), current->comm); |
| 672 | printk(KERN_WARNING "Skipping core dump\n"); |
| 673 | goto fail_unlock; |
| 674 | } |
| 675 | |
Jann Horn | fbb1816 | 2015-09-09 15:38:28 -0700 | [diff] [blame] | 676 | /* |
| 677 | * Unlink the file if it exists unless this is a SUID |
| 678 | * binary - in that case, we're running around with root |
| 679 | * privs and don't want to unlink another user's coredump. |
| 680 | */ |
| 681 | if (!need_suid_safe) { |
| 682 | mm_segment_t old_fs; |
| 683 | |
| 684 | old_fs = get_fs(); |
| 685 | set_fs(KERNEL_DS); |
| 686 | /* |
| 687 | * If it doesn't exist, that's fine. If there's some |
| 688 | * other problem, we'll catch it at the filp_open(). |
| 689 | */ |
| 690 | (void) sys_unlink((const char __user *)cn.corename); |
| 691 | set_fs(old_fs); |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * There is a race between unlinking and creating the |
| 696 | * file, but if that causes an EEXIST here, that's |
| 697 | * fine - another process raced with us while creating |
| 698 | * the corefile, and the other process won. To userspace, |
| 699 | * what matters is that at least one of the two processes |
| 700 | * writes its coredump successfully, not which one. |
| 701 | */ |
Jann Horn | 378c652 | 2016-03-22 14:25:36 -0700 | [diff] [blame] | 702 | if (need_suid_safe) { |
| 703 | /* |
| 704 | * Using user namespaces, normal user tasks can change |
| 705 | * their current->fs->root to point to arbitrary |
| 706 | * directories. Since the intention of the "only dump |
| 707 | * with a fully qualified path" rule is to control where |
| 708 | * coredumps may be placed using root privileges, |
| 709 | * current->fs->root must not be used. Instead, use the |
| 710 | * root directory of init_task. |
| 711 | */ |
| 712 | struct path root; |
| 713 | |
| 714 | task_lock(&init_task); |
| 715 | get_fs_root(init_task.fs, &root); |
| 716 | task_unlock(&init_task); |
| 717 | cprm.file = file_open_root(root.dentry, root.mnt, |
| 718 | cn.corename, open_flags, 0600); |
| 719 | path_put(&root); |
| 720 | } else { |
| 721 | cprm.file = filp_open(cn.corename, open_flags, 0600); |
| 722 | } |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 723 | if (IS_ERR(cprm.file)) |
| 724 | goto fail_unlock; |
| 725 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 726 | inode = file_inode(cprm.file); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 727 | if (inode->i_nlink > 1) |
| 728 | goto close_fail; |
| 729 | if (d_unhashed(cprm.file->f_path.dentry)) |
| 730 | goto close_fail; |
| 731 | /* |
| 732 | * AK: actually i see no reason to not allow this for named |
| 733 | * pipes etc, but keep the previous behaviour for now. |
| 734 | */ |
| 735 | if (!S_ISREG(inode->i_mode)) |
| 736 | goto close_fail; |
| 737 | /* |
Jann Horn | 40f705a | 2015-09-09 15:38:30 -0700 | [diff] [blame] | 738 | * Don't dump core if the filesystem changed owner or mode |
| 739 | * of the file during file creation. This is an issue when |
| 740 | * a process dumps core while its cwd is e.g. on a vfat |
| 741 | * filesystem. |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 742 | */ |
| 743 | if (!uid_eq(inode->i_uid, current_fsuid())) |
| 744 | goto close_fail; |
Jann Horn | 40f705a | 2015-09-09 15:38:30 -0700 | [diff] [blame] | 745 | if ((inode->i_mode & 0677) != 0600) |
| 746 | goto close_fail; |
Al Viro | 86cc058 | 2015-04-03 15:23:17 -0400 | [diff] [blame] | 747 | if (!(cprm.file->f_mode & FMODE_CAN_WRITE)) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 748 | goto close_fail; |
| 749 | if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file)) |
| 750 | goto close_fail; |
| 751 | } |
| 752 | |
| 753 | /* get us an unshared descriptor table; almost always a no-op */ |
| 754 | retval = unshare_files(&displaced); |
| 755 | if (retval) |
| 756 | goto close_fail; |
| 757 | if (displaced) |
| 758 | put_files_struct(displaced); |
Al Viro | e86d35c | 2013-05-04 14:45:54 -0400 | [diff] [blame] | 759 | if (!dump_interrupted()) { |
| 760 | file_start_write(cprm.file); |
| 761 | core_dumped = binfmt->core_dump(&cprm); |
| 762 | file_end_write(cprm.file); |
| 763 | } |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 764 | if (ispipe && core_pipe_limit) |
| 765 | wait_for_dump_helpers(cprm.file); |
| 766 | close_fail: |
| 767 | if (cprm.file) |
| 768 | filp_close(cprm.file, NULL); |
| 769 | fail_dropcount: |
| 770 | if (ispipe) |
| 771 | atomic_dec(&core_dump_count); |
| 772 | fail_unlock: |
| 773 | kfree(cn.corename); |
Oleg Nesterov | acdedd9 | 2013-04-30 15:28:13 -0700 | [diff] [blame] | 774 | coredump_finish(mm, core_dumped); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 775 | revert_creds(old_cred); |
| 776 | fail_creds: |
| 777 | put_cred(cred); |
| 778 | fail: |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * Core dumping helper functions. These are the only things you should |
| 784 | * do on a core-file: use only these functions to write out all the |
| 785 | * necessary info. |
| 786 | */ |
Al Viro | ecc8c77 | 2013-10-05 15:32:35 -0400 | [diff] [blame] | 787 | int dump_emit(struct coredump_params *cprm, const void *addr, int nr) |
| 788 | { |
| 789 | struct file *file = cprm->file; |
Al Viro | 2507a4f | 2013-10-08 09:11:48 -0400 | [diff] [blame] | 790 | loff_t pos = file->f_pos; |
| 791 | ssize_t n; |
Omar Sandoval | 2c4cb04 | 2016-05-11 15:16:37 -0700 | [diff] [blame] | 792 | if (cprm->written + nr > cprm->limit) |
Al Viro | ecc8c77 | 2013-10-05 15:32:35 -0400 | [diff] [blame] | 793 | return 0; |
Al Viro | 2507a4f | 2013-10-08 09:11:48 -0400 | [diff] [blame] | 794 | while (nr) { |
| 795 | if (dump_interrupted()) |
| 796 | return 0; |
Al Viro | 52da40a | 2013-11-15 21:58:33 -0500 | [diff] [blame] | 797 | n = __kernel_write(file, addr, nr, &pos); |
Al Viro | 2507a4f | 2013-10-08 09:11:48 -0400 | [diff] [blame] | 798 | if (n <= 0) |
| 799 | return 0; |
| 800 | file->f_pos = pos; |
Omar Sandoval | 2c4cb04 | 2016-05-11 15:16:37 -0700 | [diff] [blame] | 801 | cprm->written += n; |
Mateusz Guzik | 1607f09 | 2016-06-05 23:14:14 +0200 | [diff] [blame] | 802 | cprm->pos += n; |
Al Viro | 2507a4f | 2013-10-08 09:11:48 -0400 | [diff] [blame] | 803 | nr -= n; |
| 804 | } |
Al Viro | ecc8c77 | 2013-10-05 15:32:35 -0400 | [diff] [blame] | 805 | return 1; |
| 806 | } |
| 807 | EXPORT_SYMBOL(dump_emit); |
| 808 | |
Al Viro | 9b56d54 | 2013-10-08 09:26:08 -0400 | [diff] [blame] | 809 | int dump_skip(struct coredump_params *cprm, size_t nr) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 810 | { |
Al Viro | 9b56d54 | 2013-10-08 09:26:08 -0400 | [diff] [blame] | 811 | static char zeroes[PAGE_SIZE]; |
| 812 | struct file *file = cprm->file; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 813 | if (file->f_op->llseek && file->f_op->llseek != no_llseek) { |
Oleg Nesterov | 528f827 | 2013-04-30 15:28:15 -0700 | [diff] [blame] | 814 | if (dump_interrupted() || |
Al Viro | 9b56d54 | 2013-10-08 09:26:08 -0400 | [diff] [blame] | 815 | file->f_op->llseek(file, nr, SEEK_CUR) < 0) |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 816 | return 0; |
Mateusz Guzik | 1607f09 | 2016-06-05 23:14:14 +0200 | [diff] [blame] | 817 | cprm->pos += nr; |
Al Viro | 9b56d54 | 2013-10-08 09:26:08 -0400 | [diff] [blame] | 818 | return 1; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 819 | } else { |
Al Viro | 9b56d54 | 2013-10-08 09:26:08 -0400 | [diff] [blame] | 820 | while (nr > PAGE_SIZE) { |
| 821 | if (!dump_emit(cprm, zeroes, PAGE_SIZE)) |
| 822 | return 0; |
| 823 | nr -= PAGE_SIZE; |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 824 | } |
Al Viro | 9b56d54 | 2013-10-08 09:26:08 -0400 | [diff] [blame] | 825 | return dump_emit(cprm, zeroes, nr); |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 826 | } |
Alex Kelly | 10c28d9 | 2012-09-26 21:52:08 -0400 | [diff] [blame] | 827 | } |
Al Viro | 9b56d54 | 2013-10-08 09:26:08 -0400 | [diff] [blame] | 828 | EXPORT_SYMBOL(dump_skip); |
Al Viro | 22a8cb8 | 2013-10-08 11:05:01 -0400 | [diff] [blame] | 829 | |
| 830 | int dump_align(struct coredump_params *cprm, int align) |
| 831 | { |
Mateusz Guzik | 1607f09 | 2016-06-05 23:14:14 +0200 | [diff] [blame] | 832 | unsigned mod = cprm->pos & (align - 1); |
Al Viro | 22a8cb8 | 2013-10-08 11:05:01 -0400 | [diff] [blame] | 833 | if (align & (align - 1)) |
Al Viro | db51242 | 2013-11-15 21:55:52 -0500 | [diff] [blame] | 834 | return 0; |
| 835 | return mod ? dump_skip(cprm, align - mod) : 1; |
Al Viro | 22a8cb8 | 2013-10-08 11:05:01 -0400 | [diff] [blame] | 836 | } |
| 837 | EXPORT_SYMBOL(dump_align); |
Dave Kleikamp | 4d22c75 | 2017-01-11 13:25:00 -0600 | [diff] [blame] | 838 | |
| 839 | /* |
| 840 | * Ensures that file size is big enough to contain the current file |
| 841 | * postion. This prevents gdb from complaining about a truncated file |
| 842 | * if the last "write" to the file was dump_skip. |
| 843 | */ |
| 844 | void dump_truncate(struct coredump_params *cprm) |
| 845 | { |
| 846 | struct file *file = cprm->file; |
| 847 | loff_t offset; |
| 848 | |
| 849 | if (file->f_op->llseek && file->f_op->llseek != no_llseek) { |
| 850 | offset = file->f_op->llseek(file, 0, SEEK_CUR); |
| 851 | if (i_size_read(file->f_mapping->host) < offset) |
| 852 | do_truncate(file->f_path.dentry, offset, 0, file); |
| 853 | } |
| 854 | } |
| 855 | EXPORT_SYMBOL(dump_truncate); |