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