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