blob: cd6c574557dc19a0b405d7fac06bc166db72a830 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/exec.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * #!-checking implemented by tytso.
9 */
10/*
11 * Demand-loading implemented 01.12.91 - no need to read anything but
12 * the header into memory. The inode of the executable is put into
13 * "current->executable", and page faults do the actual loading. Clean.
14 *
15 * Once more I can proudly say that linux stood up to being changed: it
16 * was less than 2 hours work to get demand-loading completely implemented.
17 *
18 * Demand loading changed July 1993 by Eric Youngdale. Use mmap instead,
19 * current->executable is only used by the procfs. This allows a dispatch
20 * table to check for several different types of binary formats. We keep
21 * trying until we recognize the file or we run out of supported binary
22 * formats.
23 */
24
25#include <linux/config.h>
26#include <linux/slab.h>
27#include <linux/file.h>
28#include <linux/mman.h>
29#include <linux/a.out.h>
30#include <linux/stat.h>
31#include <linux/fcntl.h>
32#include <linux/smp_lock.h>
33#include <linux/init.h>
34#include <linux/pagemap.h>
35#include <linux/highmem.h>
36#include <linux/spinlock.h>
37#include <linux/key.h>
38#include <linux/personality.h>
39#include <linux/binfmts.h>
40#include <linux/swap.h>
41#include <linux/utsname.h>
42#include <linux/module.h>
43#include <linux/namei.h>
44#include <linux/proc_fs.h>
45#include <linux/ptrace.h>
46#include <linux/mount.h>
47#include <linux/security.h>
48#include <linux/syscalls.h>
49#include <linux/rmap.h>
50#include <linux/acct.h>
Matt Helsley9f460802005-11-07 00:59:16 -080051#include <linux/cn_proc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include <asm/uaccess.h>
54#include <asm/mmu_context.h>
55
56#ifdef CONFIG_KMOD
57#include <linux/kmod.h>
58#endif
59
60int core_uses_pid;
61char core_pattern[65] = "core";
Alan Coxd6e71142005-06-23 00:09:43 -070062int suid_dumpable = 0;
63
64EXPORT_SYMBOL(suid_dumpable);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* The maximal length of core_pattern is also specified in sysctl.c */
66
67static struct linux_binfmt *formats;
68static DEFINE_RWLOCK(binfmt_lock);
69
70int register_binfmt(struct linux_binfmt * fmt)
71{
72 struct linux_binfmt ** tmp = &formats;
73
74 if (!fmt)
75 return -EINVAL;
76 if (fmt->next)
77 return -EBUSY;
78 write_lock(&binfmt_lock);
79 while (*tmp) {
80 if (fmt == *tmp) {
81 write_unlock(&binfmt_lock);
82 return -EBUSY;
83 }
84 tmp = &(*tmp)->next;
85 }
86 fmt->next = formats;
87 formats = fmt;
88 write_unlock(&binfmt_lock);
89 return 0;
90}
91
92EXPORT_SYMBOL(register_binfmt);
93
94int unregister_binfmt(struct linux_binfmt * fmt)
95{
96 struct linux_binfmt ** tmp = &formats;
97
98 write_lock(&binfmt_lock);
99 while (*tmp) {
100 if (fmt == *tmp) {
101 *tmp = fmt->next;
102 write_unlock(&binfmt_lock);
103 return 0;
104 }
105 tmp = &(*tmp)->next;
106 }
107 write_unlock(&binfmt_lock);
108 return -EINVAL;
109}
110
111EXPORT_SYMBOL(unregister_binfmt);
112
113static inline void put_binfmt(struct linux_binfmt * fmt)
114{
115 module_put(fmt->module);
116}
117
118/*
119 * Note that a shared library must be both readable and executable due to
120 * security reasons.
121 *
122 * Also note that we take the address to load from from the file itself.
123 */
124asmlinkage long sys_uselib(const char __user * library)
125{
126 struct file * file;
127 struct nameidata nd;
128 int error;
129
Trond Myklebust834f2a42005-10-18 14:20:16 -0700130 error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (error)
132 goto out;
133
134 error = -EINVAL;
135 if (!S_ISREG(nd.dentry->d_inode->i_mode))
136 goto exit;
137
138 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
139 if (error)
140 goto exit;
141
Trond Myklebust834f2a42005-10-18 14:20:16 -0700142 file = nameidata_to_filp(&nd, O_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 error = PTR_ERR(file);
144 if (IS_ERR(file))
145 goto out;
146
147 error = -ENOEXEC;
148 if(file->f_op) {
149 struct linux_binfmt * fmt;
150
151 read_lock(&binfmt_lock);
152 for (fmt = formats ; fmt ; fmt = fmt->next) {
153 if (!fmt->load_shlib)
154 continue;
155 if (!try_module_get(fmt->module))
156 continue;
157 read_unlock(&binfmt_lock);
158 error = fmt->load_shlib(file);
159 read_lock(&binfmt_lock);
160 put_binfmt(fmt);
161 if (error != -ENOEXEC)
162 break;
163 }
164 read_unlock(&binfmt_lock);
165 }
166 fput(file);
167out:
168 return error;
169exit:
Trond Myklebust834f2a42005-10-18 14:20:16 -0700170 release_open_intent(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 path_release(&nd);
172 goto out;
173}
174
175/*
176 * count() counts the number of strings in array ARGV.
177 */
178static int count(char __user * __user * argv, int max)
179{
180 int i = 0;
181
182 if (argv != NULL) {
183 for (;;) {
184 char __user * p;
185
186 if (get_user(p, argv))
187 return -EFAULT;
188 if (!p)
189 break;
190 argv++;
191 if(++i > max)
192 return -E2BIG;
193 cond_resched();
194 }
195 }
196 return i;
197}
198
199/*
200 * 'copy_strings()' copies argument/environment strings from user
201 * memory to free pages in kernel mem. These are in a format ready
202 * to be put directly into the top of new user memory.
203 */
Adrian Bunk75c96f82005-05-05 16:16:09 -0700204static int copy_strings(int argc, char __user * __user * argv,
205 struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 struct page *kmapped_page = NULL;
208 char *kaddr = NULL;
209 int ret;
210
211 while (argc-- > 0) {
212 char __user *str;
213 int len;
214 unsigned long pos;
215
216 if (get_user(str, argv+argc) ||
217 !(len = strnlen_user(str, bprm->p))) {
218 ret = -EFAULT;
219 goto out;
220 }
221
222 if (bprm->p < len) {
223 ret = -E2BIG;
224 goto out;
225 }
226
227 bprm->p -= len;
228 /* XXX: add architecture specific overflow check here. */
229 pos = bprm->p;
230
231 while (len > 0) {
232 int i, new, err;
233 int offset, bytes_to_copy;
234 struct page *page;
235
236 offset = pos % PAGE_SIZE;
237 i = pos/PAGE_SIZE;
238 page = bprm->page[i];
239 new = 0;
240 if (!page) {
241 page = alloc_page(GFP_HIGHUSER);
242 bprm->page[i] = page;
243 if (!page) {
244 ret = -ENOMEM;
245 goto out;
246 }
247 new = 1;
248 }
249
250 if (page != kmapped_page) {
251 if (kmapped_page)
252 kunmap(kmapped_page);
253 kmapped_page = page;
254 kaddr = kmap(kmapped_page);
255 }
256 if (new && offset)
257 memset(kaddr, 0, offset);
258 bytes_to_copy = PAGE_SIZE - offset;
259 if (bytes_to_copy > len) {
260 bytes_to_copy = len;
261 if (new)
262 memset(kaddr+offset+len, 0,
263 PAGE_SIZE-offset-len);
264 }
265 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
266 if (err) {
267 ret = -EFAULT;
268 goto out;
269 }
270
271 pos += bytes_to_copy;
272 str += bytes_to_copy;
273 len -= bytes_to_copy;
274 }
275 }
276 ret = 0;
277out:
278 if (kmapped_page)
279 kunmap(kmapped_page);
280 return ret;
281}
282
283/*
284 * Like copy_strings, but get argv and its values from kernel memory.
285 */
286int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
287{
288 int r;
289 mm_segment_t oldfs = get_fs();
290 set_fs(KERNEL_DS);
291 r = copy_strings(argc, (char __user * __user *)argv, bprm);
292 set_fs(oldfs);
293 return r;
294}
295
296EXPORT_SYMBOL(copy_strings_kernel);
297
298#ifdef CONFIG_MMU
299/*
300 * This routine is used to map in a page into an address space: needed by
301 * execve() for the initial stack and environment pages.
302 *
303 * vma->vm_mm->mmap_sem is held for writing.
304 */
305void install_arg_page(struct vm_area_struct *vma,
306 struct page *page, unsigned long address)
307{
308 struct mm_struct *mm = vma->vm_mm;
309 pgd_t * pgd;
310 pud_t * pud;
311 pmd_t * pmd;
312 pte_t * pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -0700313 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (unlikely(anon_vma_prepare(vma)))
Hugh Dickinsc74df322005-10-29 18:16:23 -0700316 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 flush_dcache_page(page);
319 pgd = pgd_offset(mm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 pud = pud_alloc(mm, pgd, address);
321 if (!pud)
322 goto out;
323 pmd = pmd_alloc(mm, pud, address);
324 if (!pmd)
325 goto out;
Hugh Dickinsc74df322005-10-29 18:16:23 -0700326 pte = pte_alloc_map_lock(mm, pmd, address, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!pte)
328 goto out;
329 if (!pte_none(*pte)) {
Hugh Dickinsc74df322005-10-29 18:16:23 -0700330 pte_unmap_unlock(pte, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 goto out;
332 }
Hugh Dickins42946212005-10-29 18:16:05 -0700333 inc_mm_counter(mm, anon_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 lru_cache_add_active(page);
335 set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte(
336 page, vma->vm_page_prot))));
337 page_add_anon_rmap(page, vma, address);
Hugh Dickinsc74df322005-10-29 18:16:23 -0700338 pte_unmap_unlock(pte, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* no need for flush_tlb */
341 return;
342out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 __free_page(page);
344 force_sig(SIGKILL, current);
345}
346
347#define EXTRA_STACK_VM_PAGES 20 /* random */
348
349int setup_arg_pages(struct linux_binprm *bprm,
350 unsigned long stack_top,
351 int executable_stack)
352{
353 unsigned long stack_base;
354 struct vm_area_struct *mpnt;
355 struct mm_struct *mm = current->mm;
356 int i, ret;
357 long arg_size;
358
359#ifdef CONFIG_STACK_GROWSUP
360 /* Move the argument and environment strings to the bottom of the
361 * stack space.
362 */
363 int offset, j;
364 char *to, *from;
365
366 /* Start by shifting all the pages down */
367 i = 0;
368 for (j = 0; j < MAX_ARG_PAGES; j++) {
369 struct page *page = bprm->page[j];
370 if (!page)
371 continue;
372 bprm->page[i++] = page;
373 }
374
375 /* Now move them within their pages */
376 offset = bprm->p % PAGE_SIZE;
377 to = kmap(bprm->page[0]);
378 for (j = 1; j < i; j++) {
379 memmove(to, to + offset, PAGE_SIZE - offset);
380 from = kmap(bprm->page[j]);
381 memcpy(to + PAGE_SIZE - offset, from, offset);
382 kunmap(bprm->page[j - 1]);
383 to = from;
384 }
385 memmove(to, to + offset, PAGE_SIZE - offset);
386 kunmap(bprm->page[j - 1]);
387
388 /* Limit stack size to 1GB */
389 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
390 if (stack_base > (1 << 30))
391 stack_base = 1 << 30;
392 stack_base = PAGE_ALIGN(stack_top - stack_base);
393
394 /* Adjust bprm->p to point to the end of the strings. */
395 bprm->p = stack_base + PAGE_SIZE * i - offset;
396
397 mm->arg_start = stack_base;
398 arg_size = i << PAGE_SHIFT;
399
400 /* zero pages that were copied above */
401 while (i < MAX_ARG_PAGES)
402 bprm->page[i++] = NULL;
403#else
404 stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE);
405 stack_base = PAGE_ALIGN(stack_base);
406 bprm->p += stack_base;
407 mm->arg_start = bprm->p;
408 arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start);
409#endif
410
411 arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
412
413 if (bprm->loader)
414 bprm->loader += stack_base;
415 bprm->exec += stack_base;
416
417 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
418 if (!mpnt)
419 return -ENOMEM;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 memset(mpnt, 0, sizeof(*mpnt));
422
423 down_write(&mm->mmap_sem);
424 {
425 mpnt->vm_mm = mm;
426#ifdef CONFIG_STACK_GROWSUP
427 mpnt->vm_start = stack_base;
428 mpnt->vm_end = stack_base + arg_size;
429#else
430 mpnt->vm_end = stack_top;
431 mpnt->vm_start = mpnt->vm_end - arg_size;
432#endif
433 /* Adjust stack execute permissions; explicitly enable
434 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
435 * and leave alone (arch default) otherwise. */
436 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
437 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
438 else if (executable_stack == EXSTACK_DISABLE_X)
439 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
440 else
441 mpnt->vm_flags = VM_STACK_FLAGS;
442 mpnt->vm_flags |= mm->def_flags;
443 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
444 if ((ret = insert_vm_struct(mm, mpnt))) {
445 up_write(&mm->mmap_sem);
446 kmem_cache_free(vm_area_cachep, mpnt);
447 return ret;
448 }
449 mm->stack_vm = mm->total_vm = vma_pages(mpnt);
450 }
451
452 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
453 struct page *page = bprm->page[i];
454 if (page) {
455 bprm->page[i] = NULL;
456 install_arg_page(mpnt, page, stack_base);
457 }
458 stack_base += PAGE_SIZE;
459 }
460 up_write(&mm->mmap_sem);
461
462 return 0;
463}
464
465EXPORT_SYMBOL(setup_arg_pages);
466
467#define free_arg_pages(bprm) do { } while (0)
468
469#else
470
471static inline void free_arg_pages(struct linux_binprm *bprm)
472{
473 int i;
474
475 for (i = 0; i < MAX_ARG_PAGES; i++) {
476 if (bprm->page[i])
477 __free_page(bprm->page[i]);
478 bprm->page[i] = NULL;
479 }
480}
481
482#endif /* CONFIG_MMU */
483
484struct file *open_exec(const char *name)
485{
486 struct nameidata nd;
487 int err;
488 struct file *file;
489
Trond Myklebust834f2a42005-10-18 14:20:16 -0700490 err = path_lookup_open(name, LOOKUP_FOLLOW, &nd, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 file = ERR_PTR(err);
492
493 if (!err) {
494 struct inode *inode = nd.dentry->d_inode;
495 file = ERR_PTR(-EACCES);
496 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
497 S_ISREG(inode->i_mode)) {
498 int err = permission(inode, MAY_EXEC, &nd);
499 if (!err && !(inode->i_mode & 0111))
500 err = -EACCES;
501 file = ERR_PTR(err);
502 if (!err) {
Trond Myklebust834f2a42005-10-18 14:20:16 -0700503 file = nameidata_to_filp(&nd, O_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!IS_ERR(file)) {
505 err = deny_write_access(file);
506 if (err) {
507 fput(file);
508 file = ERR_PTR(err);
509 }
510 }
511out:
512 return file;
513 }
514 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700515 release_open_intent(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 path_release(&nd);
517 }
518 goto out;
519}
520
521EXPORT_SYMBOL(open_exec);
522
523int kernel_read(struct file *file, unsigned long offset,
524 char *addr, unsigned long count)
525{
526 mm_segment_t old_fs;
527 loff_t pos = offset;
528 int result;
529
530 old_fs = get_fs();
531 set_fs(get_ds());
532 /* The cast to a user pointer is valid due to the set_fs() */
533 result = vfs_read(file, (void __user *)addr, count, &pos);
534 set_fs(old_fs);
535 return result;
536}
537
538EXPORT_SYMBOL(kernel_read);
539
540static int exec_mmap(struct mm_struct *mm)
541{
542 struct task_struct *tsk;
543 struct mm_struct * old_mm, *active_mm;
544
545 /* Notify parent that we're no longer interested in the old VM */
546 tsk = current;
547 old_mm = current->mm;
548 mm_release(tsk, old_mm);
549
550 if (old_mm) {
551 /*
552 * Make sure that if there is a core dump in progress
553 * for the old mm, we get out and die instead of going
554 * through with the exec. We must hold mmap_sem around
555 * checking core_waiters and changing tsk->mm. The
556 * core-inducing thread will increment core_waiters for
557 * each thread whose ->mm == old_mm.
558 */
559 down_read(&old_mm->mmap_sem);
560 if (unlikely(old_mm->core_waiters)) {
561 up_read(&old_mm->mmap_sem);
562 return -EINTR;
563 }
564 }
565 task_lock(tsk);
566 active_mm = tsk->active_mm;
567 tsk->mm = mm;
568 tsk->active_mm = mm;
569 activate_mm(active_mm, mm);
570 task_unlock(tsk);
571 arch_pick_mmap_layout(mm);
572 if (old_mm) {
573 up_read(&old_mm->mmap_sem);
574 if (active_mm != old_mm) BUG();
575 mmput(old_mm);
576 return 0;
577 }
578 mmdrop(active_mm);
579 return 0;
580}
581
582/*
583 * This function makes sure the current process has its own signal table,
584 * so that flush_signal_handlers can later reset the handlers without
585 * disturbing other processes. (Other processes might share the signal
586 * table via the CLONE_SIGHAND option to clone().)
587 */
588static inline int de_thread(struct task_struct *tsk)
589{
590 struct signal_struct *sig = tsk->signal;
591 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
592 spinlock_t *lock = &oldsighand->siglock;
593 int count;
594
595 /*
596 * If we don't share sighandlers, then we aren't sharing anything
597 * and we can just re-use it all.
598 */
599 if (atomic_read(&oldsighand->count) <= 1) {
600 BUG_ON(atomic_read(&sig->count) != 1);
601 exit_itimers(sig);
602 return 0;
603 }
604
605 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
606 if (!newsighand)
607 return -ENOMEM;
608
609 if (thread_group_empty(current))
610 goto no_thread_group;
611
612 /*
613 * Kill all other threads in the thread group.
614 * We must hold tasklist_lock to call zap_other_threads.
615 */
616 read_lock(&tasklist_lock);
617 spin_lock_irq(lock);
618 if (sig->flags & SIGNAL_GROUP_EXIT) {
619 /*
620 * Another group action in progress, just
621 * return so that the signal is processed.
622 */
623 spin_unlock_irq(lock);
624 read_unlock(&tasklist_lock);
625 kmem_cache_free(sighand_cachep, newsighand);
626 return -EAGAIN;
627 }
628 zap_other_threads(current);
629 read_unlock(&tasklist_lock);
630
631 /*
632 * Account for the thread group leader hanging around:
633 */
Oleg Nesterov9e4e23b2005-10-30 15:01:37 -0800634 count = 1;
635 if (!thread_group_leader(current)) {
636 count = 2;
Roland McGrath53231252005-07-12 13:58:27 -0700637 /*
638 * The SIGALRM timer survives the exec, but needs to point
639 * at us as the new group leader now. We have a race with
640 * a timer firing now getting the old leader, so we need to
641 * synchronize with any firing (by calling del_timer_sync)
642 * before we can safely let the old group leader die.
643 */
644 sig->real_timer.data = (unsigned long)current;
Oleg Nesterov932aeaf2005-10-30 15:02:17 -0800645 spin_unlock_irq(lock);
Roland McGrath53231252005-07-12 13:58:27 -0700646 if (del_timer_sync(&sig->real_timer))
647 add_timer(&sig->real_timer);
Oleg Nesterov932aeaf2005-10-30 15:02:17 -0800648 spin_lock_irq(lock);
Roland McGrath53231252005-07-12 13:58:27 -0700649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 while (atomic_read(&sig->count) > count) {
651 sig->group_exit_task = current;
652 sig->notify_count = count;
653 __set_current_state(TASK_UNINTERRUPTIBLE);
654 spin_unlock_irq(lock);
655 schedule();
656 spin_lock_irq(lock);
657 }
658 sig->group_exit_task = NULL;
659 sig->notify_count = 0;
660 spin_unlock_irq(lock);
661
662 /*
663 * At this point all other threads have exited, all we have to
664 * do is to wait for the thread group leader to become inactive,
665 * and to assume its PID:
666 */
667 if (!thread_group_leader(current)) {
668 struct task_struct *leader = current->group_leader, *parent;
669 struct dentry *proc_dentry1, *proc_dentry2;
670 unsigned long exit_state, ptrace;
671
672 /*
673 * Wait for the thread group leader to be a zombie.
674 * It should already be zombie at this point, most
675 * of the time.
676 */
677 while (leader->exit_state != EXIT_ZOMBIE)
678 yield();
679
680 spin_lock(&leader->proc_lock);
681 spin_lock(&current->proc_lock);
682 proc_dentry1 = proc_pid_unhash(current);
683 proc_dentry2 = proc_pid_unhash(leader);
684 write_lock_irq(&tasklist_lock);
685
Linus Torvaldsc2a0f592005-06-18 13:06:22 -0700686 BUG_ON(leader->tgid != current->tgid);
687 BUG_ON(current->pid == current->tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
689 * An exec() starts a new thread group with the
690 * TGID of the previous thread group. Rehash the
691 * two threads with a switched PID, and release
692 * the former thread group leader:
693 */
694 ptrace = leader->ptrace;
695 parent = leader->parent;
696 if (unlikely(ptrace) && unlikely(parent == current)) {
697 /*
698 * Joker was ptracing his own group leader,
699 * and now he wants to be his own parent!
700 * We can't have that.
701 */
702 ptrace = 0;
703 }
704
705 ptrace_unlink(current);
706 ptrace_unlink(leader);
707 remove_parent(current);
708 remove_parent(leader);
709
710 switch_exec_pids(leader, current);
711
712 current->parent = current->real_parent = leader->real_parent;
713 leader->parent = leader->real_parent = child_reaper;
714 current->group_leader = current;
715 leader->group_leader = leader;
716
717 add_parent(current, current->parent);
718 add_parent(leader, leader->parent);
719 if (ptrace) {
720 current->ptrace = ptrace;
721 __ptrace_link(current, parent);
722 }
723
724 list_del(&current->tasks);
725 list_add_tail(&current->tasks, &init_task.tasks);
726 current->exit_signal = SIGCHLD;
727 exit_state = leader->exit_state;
728
729 write_unlock_irq(&tasklist_lock);
730 spin_unlock(&leader->proc_lock);
731 spin_unlock(&current->proc_lock);
732 proc_pid_flush(proc_dentry1);
733 proc_pid_flush(proc_dentry2);
734
Linus Torvaldsc2a0f592005-06-18 13:06:22 -0700735 BUG_ON(exit_state != EXIT_ZOMBIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 release_task(leader);
737 }
738
739 /*
Alexander Nybergfb085cf2005-09-14 18:54:06 +0200740 * There may be one thread left which is just exiting,
741 * but it's safe to stop telling the group to kill themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
743 sig->flags = 0;
744
745no_thread_group:
746 BUG_ON(atomic_read(&sig->count) != 1);
747 exit_itimers(sig);
748
749 if (atomic_read(&oldsighand->count) == 1) {
750 /*
751 * Now that we nuked the rest of the thread group,
752 * it turns out we are not sharing sighand any more either.
753 * So we can just keep it.
754 */
755 kmem_cache_free(sighand_cachep, newsighand);
756 } else {
757 /*
758 * Move our state over to newsighand and switch it in.
759 */
760 spin_lock_init(&newsighand->siglock);
761 atomic_set(&newsighand->count, 1);
762 memcpy(newsighand->action, oldsighand->action,
763 sizeof(newsighand->action));
764
765 write_lock_irq(&tasklist_lock);
766 spin_lock(&oldsighand->siglock);
767 spin_lock(&newsighand->siglock);
768
769 current->sighand = newsighand;
770 recalc_sigpending();
771
772 spin_unlock(&newsighand->siglock);
773 spin_unlock(&oldsighand->siglock);
774 write_unlock_irq(&tasklist_lock);
775
776 if (atomic_dec_and_test(&oldsighand->count))
777 kmem_cache_free(sighand_cachep, oldsighand);
778 }
779
Linus Torvaldsc2a0f592005-06-18 13:06:22 -0700780 BUG_ON(!thread_group_leader(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 0;
782}
783
784/*
785 * These functions flushes out all traces of the currently running executable
786 * so that a new one can be started
787 */
788
789static inline void flush_old_files(struct files_struct * files)
790{
791 long j = -1;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700792 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 spin_lock(&files->file_lock);
795 for (;;) {
796 unsigned long set, i;
797
798 j++;
799 i = j * __NFDBITS;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700800 fdt = files_fdtable(files);
801 if (i >= fdt->max_fds || i >= fdt->max_fdset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 break;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700803 set = fdt->close_on_exec->fds_bits[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!set)
805 continue;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700806 fdt->close_on_exec->fds_bits[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 spin_unlock(&files->file_lock);
808 for ( ; set ; i++,set >>= 1) {
809 if (set & 1) {
810 sys_close(i);
811 }
812 }
813 spin_lock(&files->file_lock);
814
815 }
816 spin_unlock(&files->file_lock);
817}
818
819void get_task_comm(char *buf, struct task_struct *tsk)
820{
821 /* buf must be at least sizeof(tsk->comm) in size */
822 task_lock(tsk);
823 strncpy(buf, tsk->comm, sizeof(tsk->comm));
824 task_unlock(tsk);
825}
826
827void set_task_comm(struct task_struct *tsk, char *buf)
828{
829 task_lock(tsk);
830 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
831 task_unlock(tsk);
832}
833
834int flush_old_exec(struct linux_binprm * bprm)
835{
836 char * name;
837 int i, ch, retval;
838 struct files_struct *files;
839 char tcomm[sizeof(current->comm)];
840
841 /*
842 * Make sure we have a private signal table and that
843 * we are unassociated from the previous thread group.
844 */
845 retval = de_thread(current);
846 if (retval)
847 goto out;
848
849 /*
850 * Make sure we have private file handles. Ask the
851 * fork helper to do the work for us and the exit
852 * helper to do the cleanup of the old one.
853 */
854 files = current->files; /* refcounted so safe to hold */
855 retval = unshare_files();
856 if (retval)
857 goto out;
858 /*
859 * Release all of the old mmap stuff
860 */
861 retval = exec_mmap(bprm->mm);
862 if (retval)
863 goto mmap_failed;
864
865 bprm->mm = NULL; /* We're using it now */
866
867 /* This is the point of no return */
868 steal_locks(files);
869 put_files_struct(files);
870
871 current->sas_ss_sp = current->sas_ss_size = 0;
872
873 if (current->euid == current->uid && current->egid == current->gid)
874 current->mm->dumpable = 1;
Alan Coxd6e71142005-06-23 00:09:43 -0700875 else
876 current->mm->dumpable = suid_dumpable;
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 name = bprm->filename;
Paolo 'Blaisorblade' Giarrusso36772092005-05-05 16:16:12 -0700879
880 /* Copies the binary name from after last slash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 for (i=0; (ch = *(name++)) != '\0';) {
882 if (ch == '/')
Paolo 'Blaisorblade' Giarrusso36772092005-05-05 16:16:12 -0700883 i = 0; /* overwrite what we wrote */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 else
885 if (i < (sizeof(tcomm) - 1))
886 tcomm[i++] = ch;
887 }
888 tcomm[i] = '\0';
889 set_task_comm(current, tcomm);
890
891 current->flags &= ~PF_RANDOMIZE;
892 flush_thread();
893
894 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
895 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
896 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
897 suid_keys(current);
Alan Coxd6e71142005-06-23 00:09:43 -0700898 current->mm->dumpable = suid_dumpable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900
901 /* An exec changes our domain. We are no longer part of the thread
902 group */
903
904 current->self_exec_id++;
905
906 flush_signal_handlers(current, 0);
907 flush_old_files(current->files);
908
909 return 0;
910
911mmap_failed:
912 put_files_struct(current->files);
913 current->files = files;
914out:
915 return retval;
916}
917
918EXPORT_SYMBOL(flush_old_exec);
919
920/*
921 * Fill the binprm structure from the inode.
922 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
923 */
924int prepare_binprm(struct linux_binprm *bprm)
925{
926 int mode;
927 struct inode * inode = bprm->file->f_dentry->d_inode;
928 int retval;
929
930 mode = inode->i_mode;
931 /*
932 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
933 * generic_permission lets a non-executable through
934 */
935 if (!(mode & 0111)) /* with at least _one_ execute bit set */
936 return -EACCES;
937 if (bprm->file->f_op == NULL)
938 return -EACCES;
939
940 bprm->e_uid = current->euid;
941 bprm->e_gid = current->egid;
942
943 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
944 /* Set-uid? */
945 if (mode & S_ISUID) {
946 current->personality &= ~PER_CLEAR_ON_SETID;
947 bprm->e_uid = inode->i_uid;
948 }
949
950 /* Set-gid? */
951 /*
952 * If setgid is set but no group execute bit then this
953 * is a candidate for mandatory locking, not a setgid
954 * executable.
955 */
956 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
957 current->personality &= ~PER_CLEAR_ON_SETID;
958 bprm->e_gid = inode->i_gid;
959 }
960 }
961
962 /* fill in binprm security blob */
963 retval = security_bprm_set(bprm);
964 if (retval)
965 return retval;
966
967 memset(bprm->buf,0,BINPRM_BUF_SIZE);
968 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
969}
970
971EXPORT_SYMBOL(prepare_binprm);
972
973static inline int unsafe_exec(struct task_struct *p)
974{
975 int unsafe = 0;
976 if (p->ptrace & PT_PTRACED) {
977 if (p->ptrace & PT_PTRACE_CAP)
978 unsafe |= LSM_UNSAFE_PTRACE_CAP;
979 else
980 unsafe |= LSM_UNSAFE_PTRACE;
981 }
982 if (atomic_read(&p->fs->count) > 1 ||
983 atomic_read(&p->files->count) > 1 ||
984 atomic_read(&p->sighand->count) > 1)
985 unsafe |= LSM_UNSAFE_SHARE;
986
987 return unsafe;
988}
989
990void compute_creds(struct linux_binprm *bprm)
991{
992 int unsafe;
993
994 if (bprm->e_uid != current->uid)
995 suid_keys(current);
996 exec_keys(current);
997
998 task_lock(current);
999 unsafe = unsafe_exec(current);
1000 security_bprm_apply_creds(bprm, unsafe);
1001 task_unlock(current);
1002 security_bprm_post_apply_creds(bprm);
1003}
1004
1005EXPORT_SYMBOL(compute_creds);
1006
1007void remove_arg_zero(struct linux_binprm *bprm)
1008{
1009 if (bprm->argc) {
1010 unsigned long offset;
1011 char * kaddr;
1012 struct page *page;
1013
1014 offset = bprm->p % PAGE_SIZE;
1015 goto inside;
1016
1017 while (bprm->p++, *(kaddr+offset++)) {
1018 if (offset != PAGE_SIZE)
1019 continue;
1020 offset = 0;
1021 kunmap_atomic(kaddr, KM_USER0);
1022inside:
1023 page = bprm->page[bprm->p/PAGE_SIZE];
1024 kaddr = kmap_atomic(page, KM_USER0);
1025 }
1026 kunmap_atomic(kaddr, KM_USER0);
1027 bprm->argc--;
1028 }
1029}
1030
1031EXPORT_SYMBOL(remove_arg_zero);
1032
1033/*
1034 * cycle the list of binary formats handler, until one recognizes the image
1035 */
1036int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1037{
1038 int try,retval;
1039 struct linux_binfmt *fmt;
1040#ifdef __alpha__
1041 /* handle /sbin/loader.. */
1042 {
1043 struct exec * eh = (struct exec *) bprm->buf;
1044
1045 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1046 (eh->fh.f_flags & 0x3000) == 0x3000)
1047 {
1048 struct file * file;
1049 unsigned long loader;
1050
1051 allow_write_access(bprm->file);
1052 fput(bprm->file);
1053 bprm->file = NULL;
1054
1055 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1056
1057 file = open_exec("/sbin/loader");
1058 retval = PTR_ERR(file);
1059 if (IS_ERR(file))
1060 return retval;
1061
1062 /* Remember if the application is TASO. */
1063 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1064
1065 bprm->file = file;
1066 bprm->loader = loader;
1067 retval = prepare_binprm(bprm);
1068 if (retval<0)
1069 return retval;
1070 /* should call search_binary_handler recursively here,
1071 but it does not matter */
1072 }
1073 }
1074#endif
1075 retval = security_bprm_check(bprm);
1076 if (retval)
1077 return retval;
1078
1079 /* kernel module loader fixup */
1080 /* so we don't try to load run modprobe in kernel space. */
1081 set_fs(USER_DS);
1082 retval = -ENOENT;
1083 for (try=0; try<2; try++) {
1084 read_lock(&binfmt_lock);
1085 for (fmt = formats ; fmt ; fmt = fmt->next) {
1086 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1087 if (!fn)
1088 continue;
1089 if (!try_module_get(fmt->module))
1090 continue;
1091 read_unlock(&binfmt_lock);
1092 retval = fn(bprm, regs);
1093 if (retval >= 0) {
1094 put_binfmt(fmt);
1095 allow_write_access(bprm->file);
1096 if (bprm->file)
1097 fput(bprm->file);
1098 bprm->file = NULL;
1099 current->did_exec = 1;
Matt Helsley9f460802005-11-07 00:59:16 -08001100 proc_exec_connector(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return retval;
1102 }
1103 read_lock(&binfmt_lock);
1104 put_binfmt(fmt);
1105 if (retval != -ENOEXEC || bprm->mm == NULL)
1106 break;
1107 if (!bprm->file) {
1108 read_unlock(&binfmt_lock);
1109 return retval;
1110 }
1111 }
1112 read_unlock(&binfmt_lock);
1113 if (retval != -ENOEXEC || bprm->mm == NULL) {
1114 break;
1115#ifdef CONFIG_KMOD
1116 }else{
1117#define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1118 if (printable(bprm->buf[0]) &&
1119 printable(bprm->buf[1]) &&
1120 printable(bprm->buf[2]) &&
1121 printable(bprm->buf[3]))
1122 break; /* -ENOEXEC */
1123 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1124#endif
1125 }
1126 }
1127 return retval;
1128}
1129
1130EXPORT_SYMBOL(search_binary_handler);
1131
1132/*
1133 * sys_execve() executes a new program.
1134 */
1135int do_execve(char * filename,
1136 char __user *__user *argv,
1137 char __user *__user *envp,
1138 struct pt_regs * regs)
1139{
1140 struct linux_binprm *bprm;
1141 struct file *file;
1142 int retval;
1143 int i;
1144
1145 retval = -ENOMEM;
1146 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1147 if (!bprm)
1148 goto out_ret;
1149 memset(bprm, 0, sizeof(*bprm));
1150
1151 file = open_exec(filename);
1152 retval = PTR_ERR(file);
1153 if (IS_ERR(file))
1154 goto out_kfree;
1155
1156 sched_exec();
1157
1158 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1159
1160 bprm->file = file;
1161 bprm->filename = filename;
1162 bprm->interp = filename;
1163 bprm->mm = mm_alloc();
1164 retval = -ENOMEM;
1165 if (!bprm->mm)
1166 goto out_file;
1167
1168 retval = init_new_context(current, bprm->mm);
1169 if (retval < 0)
1170 goto out_mm;
1171
1172 bprm->argc = count(argv, bprm->p / sizeof(void *));
1173 if ((retval = bprm->argc) < 0)
1174 goto out_mm;
1175
1176 bprm->envc = count(envp, bprm->p / sizeof(void *));
1177 if ((retval = bprm->envc) < 0)
1178 goto out_mm;
1179
1180 retval = security_bprm_alloc(bprm);
1181 if (retval)
1182 goto out;
1183
1184 retval = prepare_binprm(bprm);
1185 if (retval < 0)
1186 goto out;
1187
1188 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1189 if (retval < 0)
1190 goto out;
1191
1192 bprm->exec = bprm->p;
1193 retval = copy_strings(bprm->envc, envp, bprm);
1194 if (retval < 0)
1195 goto out;
1196
1197 retval = copy_strings(bprm->argc, argv, bprm);
1198 if (retval < 0)
1199 goto out;
1200
1201 retval = search_binary_handler(bprm,regs);
1202 if (retval >= 0) {
1203 free_arg_pages(bprm);
1204
1205 /* execve success */
1206 security_bprm_free(bprm);
1207 acct_update_integrals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 kfree(bprm);
1209 return retval;
1210 }
1211
1212out:
1213 /* Something went wrong, return the inode and free the argument pages*/
1214 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1215 struct page * page = bprm->page[i];
1216 if (page)
1217 __free_page(page);
1218 }
1219
1220 if (bprm->security)
1221 security_bprm_free(bprm);
1222
1223out_mm:
1224 if (bprm->mm)
1225 mmdrop(bprm->mm);
1226
1227out_file:
1228 if (bprm->file) {
1229 allow_write_access(bprm->file);
1230 fput(bprm->file);
1231 }
1232
1233out_kfree:
1234 kfree(bprm);
1235
1236out_ret:
1237 return retval;
1238}
1239
1240int set_binfmt(struct linux_binfmt *new)
1241{
1242 struct linux_binfmt *old = current->binfmt;
1243
1244 if (new) {
1245 if (!try_module_get(new->module))
1246 return -1;
1247 }
1248 current->binfmt = new;
1249 if (old)
1250 module_put(old->module);
1251 return 0;
1252}
1253
1254EXPORT_SYMBOL(set_binfmt);
1255
1256#define CORENAME_MAX_SIZE 64
1257
1258/* format_corename will inspect the pattern parameter, and output a
1259 * name into corename, which must have space for at least
1260 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1261 */
1262static void format_corename(char *corename, const char *pattern, long signr)
1263{
1264 const char *pat_ptr = pattern;
1265 char *out_ptr = corename;
1266 char *const out_end = corename + CORENAME_MAX_SIZE;
1267 int rc;
1268 int pid_in_pattern = 0;
1269
1270 /* Repeat as long as we have more pattern to process and more output
1271 space */
1272 while (*pat_ptr) {
1273 if (*pat_ptr != '%') {
1274 if (out_ptr == out_end)
1275 goto out;
1276 *out_ptr++ = *pat_ptr++;
1277 } else {
1278 switch (*++pat_ptr) {
1279 case 0:
1280 goto out;
1281 /* Double percent, output one percent */
1282 case '%':
1283 if (out_ptr == out_end)
1284 goto out;
1285 *out_ptr++ = '%';
1286 break;
1287 /* pid */
1288 case 'p':
1289 pid_in_pattern = 1;
1290 rc = snprintf(out_ptr, out_end - out_ptr,
1291 "%d", current->tgid);
1292 if (rc > out_end - out_ptr)
1293 goto out;
1294 out_ptr += rc;
1295 break;
1296 /* uid */
1297 case 'u':
1298 rc = snprintf(out_ptr, out_end - out_ptr,
1299 "%d", current->uid);
1300 if (rc > out_end - out_ptr)
1301 goto out;
1302 out_ptr += rc;
1303 break;
1304 /* gid */
1305 case 'g':
1306 rc = snprintf(out_ptr, out_end - out_ptr,
1307 "%d", current->gid);
1308 if (rc > out_end - out_ptr)
1309 goto out;
1310 out_ptr += rc;
1311 break;
1312 /* signal that caused the coredump */
1313 case 's':
1314 rc = snprintf(out_ptr, out_end - out_ptr,
1315 "%ld", signr);
1316 if (rc > out_end - out_ptr)
1317 goto out;
1318 out_ptr += rc;
1319 break;
1320 /* UNIX time of coredump */
1321 case 't': {
1322 struct timeval tv;
1323 do_gettimeofday(&tv);
1324 rc = snprintf(out_ptr, out_end - out_ptr,
1325 "%lu", tv.tv_sec);
1326 if (rc > out_end - out_ptr)
1327 goto out;
1328 out_ptr += rc;
1329 break;
1330 }
1331 /* hostname */
1332 case 'h':
1333 down_read(&uts_sem);
1334 rc = snprintf(out_ptr, out_end - out_ptr,
1335 "%s", system_utsname.nodename);
1336 up_read(&uts_sem);
1337 if (rc > out_end - out_ptr)
1338 goto out;
1339 out_ptr += rc;
1340 break;
1341 /* executable */
1342 case 'e':
1343 rc = snprintf(out_ptr, out_end - out_ptr,
1344 "%s", current->comm);
1345 if (rc > out_end - out_ptr)
1346 goto out;
1347 out_ptr += rc;
1348 break;
1349 default:
1350 break;
1351 }
1352 ++pat_ptr;
1353 }
1354 }
1355 /* Backward compatibility with core_uses_pid:
1356 *
1357 * If core_pattern does not include a %p (as is the default)
1358 * and core_uses_pid is set, then .%pid will be appended to
1359 * the filename */
1360 if (!pid_in_pattern
1361 && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1362 rc = snprintf(out_ptr, out_end - out_ptr,
1363 ".%d", current->tgid);
1364 if (rc > out_end - out_ptr)
1365 goto out;
1366 out_ptr += rc;
1367 }
1368 out:
1369 *out_ptr = 0;
1370}
1371
1372static void zap_threads (struct mm_struct *mm)
1373{
1374 struct task_struct *g, *p;
1375 struct task_struct *tsk = current;
1376 struct completion *vfork_done = tsk->vfork_done;
1377 int traced = 0;
1378
1379 /*
1380 * Make sure nobody is waiting for us to release the VM,
1381 * otherwise we can deadlock when we wait on each other
1382 */
1383 if (vfork_done) {
1384 tsk->vfork_done = NULL;
1385 complete(vfork_done);
1386 }
1387
1388 read_lock(&tasklist_lock);
1389 do_each_thread(g,p)
1390 if (mm == p->mm && p != tsk) {
1391 force_sig_specific(SIGKILL, p);
1392 mm->core_waiters++;
1393 if (unlikely(p->ptrace) &&
1394 unlikely(p->parent->mm == mm))
1395 traced = 1;
1396 }
1397 while_each_thread(g,p);
1398
1399 read_unlock(&tasklist_lock);
1400
1401 if (unlikely(traced)) {
1402 /*
1403 * We are zapping a thread and the thread it ptraces.
1404 * If the tracee went into a ptrace stop for exit tracing,
1405 * we could deadlock since the tracer is waiting for this
1406 * coredump to finish. Detach them so they can both die.
1407 */
1408 write_lock_irq(&tasklist_lock);
1409 do_each_thread(g,p) {
1410 if (mm == p->mm && p != tsk &&
1411 p->ptrace && p->parent->mm == mm) {
1412 __ptrace_unlink(p);
1413 }
1414 } while_each_thread(g,p);
1415 write_unlock_irq(&tasklist_lock);
1416 }
1417}
1418
1419static void coredump_wait(struct mm_struct *mm)
1420{
1421 DECLARE_COMPLETION(startup_done);
Oleg Nesterov2384f552005-10-30 15:02:47 -08001422 int core_waiters;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 mm->core_startup_done = &startup_done;
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 zap_threads(mm);
Oleg Nesterov2384f552005-10-30 15:02:47 -08001427 core_waiters = mm->core_waiters;
1428 up_write(&mm->mmap_sem);
1429
1430 if (core_waiters)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 wait_for_completion(&startup_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 BUG_ON(mm->core_waiters);
1433}
1434
1435int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1436{
1437 char corename[CORENAME_MAX_SIZE + 1];
1438 struct mm_struct *mm = current->mm;
1439 struct linux_binfmt * binfmt;
1440 struct inode * inode;
1441 struct file * file;
1442 int retval = 0;
Alan Coxd6e71142005-06-23 00:09:43 -07001443 int fsuid = current->fsuid;
1444 int flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 binfmt = current->binfmt;
1447 if (!binfmt || !binfmt->core_dump)
1448 goto fail;
1449 down_write(&mm->mmap_sem);
1450 if (!mm->dumpable) {
1451 up_write(&mm->mmap_sem);
1452 goto fail;
1453 }
Alan Coxd6e71142005-06-23 00:09:43 -07001454
1455 /*
1456 * We cannot trust fsuid as being the "true" uid of the
1457 * process nor do we know its entire history. We only know it
1458 * was tainted so we dump it as root in mode 2.
1459 */
1460 if (mm->dumpable == 2) { /* Setuid core dump mode */
1461 flag = O_EXCL; /* Stop rewrite attacks */
1462 current->fsuid = 0; /* Dump root private */
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 mm->dumpable = 0;
Oleg Nesterov1291cf42005-10-30 15:02:54 -08001465
1466 retval = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov1291cf42005-10-30 15:02:54 -08001468 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
1469 current->signal->flags = SIGNAL_GROUP_EXIT;
1470 current->signal->group_exit_code = exit_code;
1471 retval = 0;
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterov1291cf42005-10-30 15:02:54 -08001474 if (retval) {
1475 up_write(&mm->mmap_sem);
1476 goto fail;
1477 }
1478
1479 init_completion(&mm->core_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 coredump_wait(mm);
1481
1482 /*
1483 * Clear any false indication of pending signals that might
1484 * be seen by the filesystem code called to write the core file.
1485 */
1486 current->signal->group_stop_count = 0;
1487 clear_thread_flag(TIF_SIGPENDING);
1488
1489 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1490 goto fail_unlock;
1491
1492 /*
1493 * lock_kernel() because format_corename() is controlled by sysctl, which
1494 * uses lock_kernel()
1495 */
1496 lock_kernel();
1497 format_corename(corename, core_pattern, signr);
1498 unlock_kernel();
Alan Coxd6e71142005-06-23 00:09:43 -07001499 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (IS_ERR(file))
1501 goto fail_unlock;
1502 inode = file->f_dentry->d_inode;
1503 if (inode->i_nlink > 1)
1504 goto close_fail; /* multiple links - don't dump */
1505 if (d_unhashed(file->f_dentry))
1506 goto close_fail;
1507
1508 if (!S_ISREG(inode->i_mode))
1509 goto close_fail;
1510 if (!file->f_op)
1511 goto close_fail;
1512 if (!file->f_op->write)
1513 goto close_fail;
Miklos Szeredicc4e69d2005-11-07 00:59:49 -08001514 if (do_truncate(file->f_dentry, 0, file) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 goto close_fail;
1516
1517 retval = binfmt->core_dump(signr, regs, file);
1518
1519 if (retval)
1520 current->signal->group_exit_code |= 0x80;
1521close_fail:
1522 filp_close(file, NULL);
1523fail_unlock:
Alan Coxd6e71142005-06-23 00:09:43 -07001524 current->fsuid = fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 complete_all(&mm->core_done);
1526fail:
1527 return retval;
1528}