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