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