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