[PATCH] mm: update_hiwaters just in time
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / exec.c
CommitLineData
1da177e4
LT
1/*
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";
d6e71144
AC
61int suid_dumpable = 0;
62
63EXPORT_SYMBOL(suid_dumpable);
1da177e4
LT
64/* 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
834f2a4a 129 error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ);
1da177e4
LT
130 if (error)
131 goto out;
132
133 error = -EINVAL;
134 if (!S_ISREG(nd.dentry->d_inode->i_mode))
135 goto exit;
136
137 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
138 if (error)
139 goto exit;
140
834f2a4a 141 file = nameidata_to_filp(&nd, O_RDONLY);
1da177e4
LT
142 error = PTR_ERR(file);
143 if (IS_ERR(file))
144 goto out;
145
146 error = -ENOEXEC;
147 if(file->f_op) {
148 struct linux_binfmt * fmt;
149
150 read_lock(&binfmt_lock);
151 for (fmt = formats ; fmt ; fmt = fmt->next) {
152 if (!fmt->load_shlib)
153 continue;
154 if (!try_module_get(fmt->module))
155 continue;
156 read_unlock(&binfmt_lock);
157 error = fmt->load_shlib(file);
158 read_lock(&binfmt_lock);
159 put_binfmt(fmt);
160 if (error != -ENOEXEC)
161 break;
162 }
163 read_unlock(&binfmt_lock);
164 }
165 fput(file);
166out:
167 return error;
168exit:
834f2a4a 169 release_open_intent(&nd);
1da177e4
LT
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 */
75c96f85
AB
203static int copy_strings(int argc, char __user * __user * argv,
204 struct linux_binprm *bprm)
1da177e4
LT
205{
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 }
4294621f 333 inc_mm_counter(mm, anon_rss);
1da177e4
LT
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
1da177e4
LT
424 memset(mpnt, 0, sizeof(*mpnt));
425
426 down_write(&mm->mmap_sem);
427 {
428 mpnt->vm_mm = mm;
429#ifdef CONFIG_STACK_GROWSUP
430 mpnt->vm_start = stack_base;
431 mpnt->vm_end = stack_base + arg_size;
432#else
433 mpnt->vm_end = stack_top;
434 mpnt->vm_start = mpnt->vm_end - arg_size;
435#endif
436 /* Adjust stack execute permissions; explicitly enable
437 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
438 * and leave alone (arch default) otherwise. */
439 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
440 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
441 else if (executable_stack == EXSTACK_DISABLE_X)
442 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
443 else
444 mpnt->vm_flags = VM_STACK_FLAGS;
445 mpnt->vm_flags |= mm->def_flags;
446 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
447 if ((ret = insert_vm_struct(mm, mpnt))) {
448 up_write(&mm->mmap_sem);
449 kmem_cache_free(vm_area_cachep, mpnt);
450 return ret;
451 }
452 mm->stack_vm = mm->total_vm = vma_pages(mpnt);
453 }
454
455 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
456 struct page *page = bprm->page[i];
457 if (page) {
458 bprm->page[i] = NULL;
459 install_arg_page(mpnt, page, stack_base);
460 }
461 stack_base += PAGE_SIZE;
462 }
463 up_write(&mm->mmap_sem);
464
465 return 0;
466}
467
468EXPORT_SYMBOL(setup_arg_pages);
469
470#define free_arg_pages(bprm) do { } while (0)
471
472#else
473
474static inline void free_arg_pages(struct linux_binprm *bprm)
475{
476 int i;
477
478 for (i = 0; i < MAX_ARG_PAGES; i++) {
479 if (bprm->page[i])
480 __free_page(bprm->page[i]);
481 bprm->page[i] = NULL;
482 }
483}
484
485#endif /* CONFIG_MMU */
486
487struct file *open_exec(const char *name)
488{
489 struct nameidata nd;
490 int err;
491 struct file *file;
492
834f2a4a 493 err = path_lookup_open(name, LOOKUP_FOLLOW, &nd, FMODE_READ);
1da177e4
LT
494 file = ERR_PTR(err);
495
496 if (!err) {
497 struct inode *inode = nd.dentry->d_inode;
498 file = ERR_PTR(-EACCES);
499 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
500 S_ISREG(inode->i_mode)) {
501 int err = permission(inode, MAY_EXEC, &nd);
502 if (!err && !(inode->i_mode & 0111))
503 err = -EACCES;
504 file = ERR_PTR(err);
505 if (!err) {
834f2a4a 506 file = nameidata_to_filp(&nd, O_RDONLY);
1da177e4
LT
507 if (!IS_ERR(file)) {
508 err = deny_write_access(file);
509 if (err) {
510 fput(file);
511 file = ERR_PTR(err);
512 }
513 }
514out:
515 return file;
516 }
517 }
834f2a4a 518 release_open_intent(&nd);
1da177e4
LT
519 path_release(&nd);
520 }
521 goto out;
522}
523
524EXPORT_SYMBOL(open_exec);
525
526int kernel_read(struct file *file, unsigned long offset,
527 char *addr, unsigned long count)
528{
529 mm_segment_t old_fs;
530 loff_t pos = offset;
531 int result;
532
533 old_fs = get_fs();
534 set_fs(get_ds());
535 /* The cast to a user pointer is valid due to the set_fs() */
536 result = vfs_read(file, (void __user *)addr, count, &pos);
537 set_fs(old_fs);
538 return result;
539}
540
541EXPORT_SYMBOL(kernel_read);
542
543static int exec_mmap(struct mm_struct *mm)
544{
545 struct task_struct *tsk;
546 struct mm_struct * old_mm, *active_mm;
547
548 /* Notify parent that we're no longer interested in the old VM */
549 tsk = current;
550 old_mm = current->mm;
551 mm_release(tsk, old_mm);
552
553 if (old_mm) {
554 /*
555 * Make sure that if there is a core dump in progress
556 * for the old mm, we get out and die instead of going
557 * through with the exec. We must hold mmap_sem around
558 * checking core_waiters and changing tsk->mm. The
559 * core-inducing thread will increment core_waiters for
560 * each thread whose ->mm == old_mm.
561 */
562 down_read(&old_mm->mmap_sem);
563 if (unlikely(old_mm->core_waiters)) {
564 up_read(&old_mm->mmap_sem);
565 return -EINTR;
566 }
567 }
568 task_lock(tsk);
569 active_mm = tsk->active_mm;
570 tsk->mm = mm;
571 tsk->active_mm = mm;
572 activate_mm(active_mm, mm);
573 task_unlock(tsk);
574 arch_pick_mmap_layout(mm);
575 if (old_mm) {
576 up_read(&old_mm->mmap_sem);
577 if (active_mm != old_mm) BUG();
578 mmput(old_mm);
579 return 0;
580 }
581 mmdrop(active_mm);
582 return 0;
583}
584
585/*
586 * This function makes sure the current process has its own signal table,
587 * so that flush_signal_handlers can later reset the handlers without
588 * disturbing other processes. (Other processes might share the signal
589 * table via the CLONE_SIGHAND option to clone().)
590 */
591static inline int de_thread(struct task_struct *tsk)
592{
593 struct signal_struct *sig = tsk->signal;
594 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
595 spinlock_t *lock = &oldsighand->siglock;
596 int count;
597
598 /*
599 * If we don't share sighandlers, then we aren't sharing anything
600 * and we can just re-use it all.
601 */
602 if (atomic_read(&oldsighand->count) <= 1) {
603 BUG_ON(atomic_read(&sig->count) != 1);
604 exit_itimers(sig);
605 return 0;
606 }
607
608 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
609 if (!newsighand)
610 return -ENOMEM;
611
612 if (thread_group_empty(current))
613 goto no_thread_group;
614
615 /*
616 * Kill all other threads in the thread group.
617 * We must hold tasklist_lock to call zap_other_threads.
618 */
619 read_lock(&tasklist_lock);
620 spin_lock_irq(lock);
621 if (sig->flags & SIGNAL_GROUP_EXIT) {
622 /*
623 * Another group action in progress, just
624 * return so that the signal is processed.
625 */
626 spin_unlock_irq(lock);
627 read_unlock(&tasklist_lock);
628 kmem_cache_free(sighand_cachep, newsighand);
629 return -EAGAIN;
630 }
631 zap_other_threads(current);
632 read_unlock(&tasklist_lock);
633
634 /*
635 * Account for the thread group leader hanging around:
636 */
637 count = 2;
638 if (thread_group_leader(current))
639 count = 1;
53231250
RM
640 else {
641 /*
642 * The SIGALRM timer survives the exec, but needs to point
643 * at us as the new group leader now. We have a race with
644 * a timer firing now getting the old leader, so we need to
645 * synchronize with any firing (by calling del_timer_sync)
646 * before we can safely let the old group leader die.
647 */
648 sig->real_timer.data = (unsigned long)current;
649 if (del_timer_sync(&sig->real_timer))
650 add_timer(&sig->real_timer);
651 }
1da177e4
LT
652 while (atomic_read(&sig->count) > count) {
653 sig->group_exit_task = current;
654 sig->notify_count = count;
655 __set_current_state(TASK_UNINTERRUPTIBLE);
656 spin_unlock_irq(lock);
657 schedule();
658 spin_lock_irq(lock);
659 }
660 sig->group_exit_task = NULL;
661 sig->notify_count = 0;
c2a0f594 662 sig->real_timer.data = (unsigned long)current;
1da177e4
LT
663 spin_unlock_irq(lock);
664
665 /*
666 * At this point all other threads have exited, all we have to
667 * do is to wait for the thread group leader to become inactive,
668 * and to assume its PID:
669 */
670 if (!thread_group_leader(current)) {
671 struct task_struct *leader = current->group_leader, *parent;
672 struct dentry *proc_dentry1, *proc_dentry2;
673 unsigned long exit_state, ptrace;
674
675 /*
676 * Wait for the thread group leader to be a zombie.
677 * It should already be zombie at this point, most
678 * of the time.
679 */
680 while (leader->exit_state != EXIT_ZOMBIE)
681 yield();
682
683 spin_lock(&leader->proc_lock);
684 spin_lock(&current->proc_lock);
685 proc_dentry1 = proc_pid_unhash(current);
686 proc_dentry2 = proc_pid_unhash(leader);
687 write_lock_irq(&tasklist_lock);
688
c2a0f594
LT
689 BUG_ON(leader->tgid != current->tgid);
690 BUG_ON(current->pid == current->tgid);
1da177e4
LT
691 /*
692 * An exec() starts a new thread group with the
693 * TGID of the previous thread group. Rehash the
694 * two threads with a switched PID, and release
695 * the former thread group leader:
696 */
697 ptrace = leader->ptrace;
698 parent = leader->parent;
699 if (unlikely(ptrace) && unlikely(parent == current)) {
700 /*
701 * Joker was ptracing his own group leader,
702 * and now he wants to be his own parent!
703 * We can't have that.
704 */
705 ptrace = 0;
706 }
707
708 ptrace_unlink(current);
709 ptrace_unlink(leader);
710 remove_parent(current);
711 remove_parent(leader);
712
713 switch_exec_pids(leader, current);
714
715 current->parent = current->real_parent = leader->real_parent;
716 leader->parent = leader->real_parent = child_reaper;
717 current->group_leader = current;
718 leader->group_leader = leader;
719
720 add_parent(current, current->parent);
721 add_parent(leader, leader->parent);
722 if (ptrace) {
723 current->ptrace = ptrace;
724 __ptrace_link(current, parent);
725 }
726
727 list_del(&current->tasks);
728 list_add_tail(&current->tasks, &init_task.tasks);
729 current->exit_signal = SIGCHLD;
730 exit_state = leader->exit_state;
731
732 write_unlock_irq(&tasklist_lock);
733 spin_unlock(&leader->proc_lock);
734 spin_unlock(&current->proc_lock);
735 proc_pid_flush(proc_dentry1);
736 proc_pid_flush(proc_dentry2);
737
c2a0f594 738 BUG_ON(exit_state != EXIT_ZOMBIE);
1da177e4
LT
739 release_task(leader);
740 }
741
742 /*
fb085cf1
AN
743 * There may be one thread left which is just exiting,
744 * but it's safe to stop telling the group to kill themselves.
1da177e4
LT
745 */
746 sig->flags = 0;
747
748no_thread_group:
749 BUG_ON(atomic_read(&sig->count) != 1);
750 exit_itimers(sig);
751
752 if (atomic_read(&oldsighand->count) == 1) {
753 /*
754 * Now that we nuked the rest of the thread group,
755 * it turns out we are not sharing sighand any more either.
756 * So we can just keep it.
757 */
758 kmem_cache_free(sighand_cachep, newsighand);
759 } else {
760 /*
761 * Move our state over to newsighand and switch it in.
762 */
763 spin_lock_init(&newsighand->siglock);
764 atomic_set(&newsighand->count, 1);
765 memcpy(newsighand->action, oldsighand->action,
766 sizeof(newsighand->action));
767
768 write_lock_irq(&tasklist_lock);
769 spin_lock(&oldsighand->siglock);
770 spin_lock(&newsighand->siglock);
771
772 current->sighand = newsighand;
773 recalc_sigpending();
774
775 spin_unlock(&newsighand->siglock);
776 spin_unlock(&oldsighand->siglock);
777 write_unlock_irq(&tasklist_lock);
778
779 if (atomic_dec_and_test(&oldsighand->count))
780 kmem_cache_free(sighand_cachep, oldsighand);
781 }
782
c2a0f594 783 BUG_ON(!thread_group_leader(current));
1da177e4
LT
784 return 0;
785}
786
787/*
788 * These functions flushes out all traces of the currently running executable
789 * so that a new one can be started
790 */
791
792static inline void flush_old_files(struct files_struct * files)
793{
794 long j = -1;
badf1662 795 struct fdtable *fdt;
1da177e4
LT
796
797 spin_lock(&files->file_lock);
798 for (;;) {
799 unsigned long set, i;
800
801 j++;
802 i = j * __NFDBITS;
badf1662
DS
803 fdt = files_fdtable(files);
804 if (i >= fdt->max_fds || i >= fdt->max_fdset)
1da177e4 805 break;
badf1662 806 set = fdt->close_on_exec->fds_bits[j];
1da177e4
LT
807 if (!set)
808 continue;
badf1662 809 fdt->close_on_exec->fds_bits[j] = 0;
1da177e4
LT
810 spin_unlock(&files->file_lock);
811 for ( ; set ; i++,set >>= 1) {
812 if (set & 1) {
813 sys_close(i);
814 }
815 }
816 spin_lock(&files->file_lock);
817
818 }
819 spin_unlock(&files->file_lock);
820}
821
822void get_task_comm(char *buf, struct task_struct *tsk)
823{
824 /* buf must be at least sizeof(tsk->comm) in size */
825 task_lock(tsk);
826 strncpy(buf, tsk->comm, sizeof(tsk->comm));
827 task_unlock(tsk);
828}
829
830void set_task_comm(struct task_struct *tsk, char *buf)
831{
832 task_lock(tsk);
833 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
834 task_unlock(tsk);
835}
836
837int flush_old_exec(struct linux_binprm * bprm)
838{
839 char * name;
840 int i, ch, retval;
841 struct files_struct *files;
842 char tcomm[sizeof(current->comm)];
843
844 /*
845 * Make sure we have a private signal table and that
846 * we are unassociated from the previous thread group.
847 */
848 retval = de_thread(current);
849 if (retval)
850 goto out;
851
852 /*
853 * Make sure we have private file handles. Ask the
854 * fork helper to do the work for us and the exit
855 * helper to do the cleanup of the old one.
856 */
857 files = current->files; /* refcounted so safe to hold */
858 retval = unshare_files();
859 if (retval)
860 goto out;
861 /*
862 * Release all of the old mmap stuff
863 */
864 retval = exec_mmap(bprm->mm);
865 if (retval)
866 goto mmap_failed;
867
868 bprm->mm = NULL; /* We're using it now */
869
870 /* This is the point of no return */
871 steal_locks(files);
872 put_files_struct(files);
873
874 current->sas_ss_sp = current->sas_ss_size = 0;
875
876 if (current->euid == current->uid && current->egid == current->gid)
877 current->mm->dumpable = 1;
d6e71144
AC
878 else
879 current->mm->dumpable = suid_dumpable;
880
1da177e4 881 name = bprm->filename;
36772092
PBG
882
883 /* Copies the binary name from after last slash */
1da177e4
LT
884 for (i=0; (ch = *(name++)) != '\0';) {
885 if (ch == '/')
36772092 886 i = 0; /* overwrite what we wrote */
1da177e4
LT
887 else
888 if (i < (sizeof(tcomm) - 1))
889 tcomm[i++] = ch;
890 }
891 tcomm[i] = '\0';
892 set_task_comm(current, tcomm);
893
894 current->flags &= ~PF_RANDOMIZE;
895 flush_thread();
896
897 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
898 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
899 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
900 suid_keys(current);
d6e71144 901 current->mm->dumpable = suid_dumpable;
1da177e4
LT
902 }
903
904 /* An exec changes our domain. We are no longer part of the thread
905 group */
906
907 current->self_exec_id++;
908
909 flush_signal_handlers(current, 0);
910 flush_old_files(current->files);
911
912 return 0;
913
914mmap_failed:
915 put_files_struct(current->files);
916 current->files = files;
917out:
918 return retval;
919}
920
921EXPORT_SYMBOL(flush_old_exec);
922
923/*
924 * Fill the binprm structure from the inode.
925 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
926 */
927int prepare_binprm(struct linux_binprm *bprm)
928{
929 int mode;
930 struct inode * inode = bprm->file->f_dentry->d_inode;
931 int retval;
932
933 mode = inode->i_mode;
934 /*
935 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
936 * generic_permission lets a non-executable through
937 */
938 if (!(mode & 0111)) /* with at least _one_ execute bit set */
939 return -EACCES;
940 if (bprm->file->f_op == NULL)
941 return -EACCES;
942
943 bprm->e_uid = current->euid;
944 bprm->e_gid = current->egid;
945
946 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
947 /* Set-uid? */
948 if (mode & S_ISUID) {
949 current->personality &= ~PER_CLEAR_ON_SETID;
950 bprm->e_uid = inode->i_uid;
951 }
952
953 /* Set-gid? */
954 /*
955 * If setgid is set but no group execute bit then this
956 * is a candidate for mandatory locking, not a setgid
957 * executable.
958 */
959 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
960 current->personality &= ~PER_CLEAR_ON_SETID;
961 bprm->e_gid = inode->i_gid;
962 }
963 }
964
965 /* fill in binprm security blob */
966 retval = security_bprm_set(bprm);
967 if (retval)
968 return retval;
969
970 memset(bprm->buf,0,BINPRM_BUF_SIZE);
971 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
972}
973
974EXPORT_SYMBOL(prepare_binprm);
975
976static inline int unsafe_exec(struct task_struct *p)
977{
978 int unsafe = 0;
979 if (p->ptrace & PT_PTRACED) {
980 if (p->ptrace & PT_PTRACE_CAP)
981 unsafe |= LSM_UNSAFE_PTRACE_CAP;
982 else
983 unsafe |= LSM_UNSAFE_PTRACE;
984 }
985 if (atomic_read(&p->fs->count) > 1 ||
986 atomic_read(&p->files->count) > 1 ||
987 atomic_read(&p->sighand->count) > 1)
988 unsafe |= LSM_UNSAFE_SHARE;
989
990 return unsafe;
991}
992
993void compute_creds(struct linux_binprm *bprm)
994{
995 int unsafe;
996
997 if (bprm->e_uid != current->uid)
998 suid_keys(current);
999 exec_keys(current);
1000
1001 task_lock(current);
1002 unsafe = unsafe_exec(current);
1003 security_bprm_apply_creds(bprm, unsafe);
1004 task_unlock(current);
1005 security_bprm_post_apply_creds(bprm);
1006}
1007
1008EXPORT_SYMBOL(compute_creds);
1009
1010void remove_arg_zero(struct linux_binprm *bprm)
1011{
1012 if (bprm->argc) {
1013 unsigned long offset;
1014 char * kaddr;
1015 struct page *page;
1016
1017 offset = bprm->p % PAGE_SIZE;
1018 goto inside;
1019
1020 while (bprm->p++, *(kaddr+offset++)) {
1021 if (offset != PAGE_SIZE)
1022 continue;
1023 offset = 0;
1024 kunmap_atomic(kaddr, KM_USER0);
1025inside:
1026 page = bprm->page[bprm->p/PAGE_SIZE];
1027 kaddr = kmap_atomic(page, KM_USER0);
1028 }
1029 kunmap_atomic(kaddr, KM_USER0);
1030 bprm->argc--;
1031 }
1032}
1033
1034EXPORT_SYMBOL(remove_arg_zero);
1035
1036/*
1037 * cycle the list of binary formats handler, until one recognizes the image
1038 */
1039int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1040{
1041 int try,retval;
1042 struct linux_binfmt *fmt;
1043#ifdef __alpha__
1044 /* handle /sbin/loader.. */
1045 {
1046 struct exec * eh = (struct exec *) bprm->buf;
1047
1048 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1049 (eh->fh.f_flags & 0x3000) == 0x3000)
1050 {
1051 struct file * file;
1052 unsigned long loader;
1053
1054 allow_write_access(bprm->file);
1055 fput(bprm->file);
1056 bprm->file = NULL;
1057
1058 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1059
1060 file = open_exec("/sbin/loader");
1061 retval = PTR_ERR(file);
1062 if (IS_ERR(file))
1063 return retval;
1064
1065 /* Remember if the application is TASO. */
1066 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1067
1068 bprm->file = file;
1069 bprm->loader = loader;
1070 retval = prepare_binprm(bprm);
1071 if (retval<0)
1072 return retval;
1073 /* should call search_binary_handler recursively here,
1074 but it does not matter */
1075 }
1076 }
1077#endif
1078 retval = security_bprm_check(bprm);
1079 if (retval)
1080 return retval;
1081
1082 /* kernel module loader fixup */
1083 /* so we don't try to load run modprobe in kernel space. */
1084 set_fs(USER_DS);
1085 retval = -ENOENT;
1086 for (try=0; try<2; try++) {
1087 read_lock(&binfmt_lock);
1088 for (fmt = formats ; fmt ; fmt = fmt->next) {
1089 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1090 if (!fn)
1091 continue;
1092 if (!try_module_get(fmt->module))
1093 continue;
1094 read_unlock(&binfmt_lock);
1095 retval = fn(bprm, regs);
1096 if (retval >= 0) {
1097 put_binfmt(fmt);
1098 allow_write_access(bprm->file);
1099 if (bprm->file)
1100 fput(bprm->file);
1101 bprm->file = NULL;
1102 current->did_exec = 1;
1103 return retval;
1104 }
1105 read_lock(&binfmt_lock);
1106 put_binfmt(fmt);
1107 if (retval != -ENOEXEC || bprm->mm == NULL)
1108 break;
1109 if (!bprm->file) {
1110 read_unlock(&binfmt_lock);
1111 return retval;
1112 }
1113 }
1114 read_unlock(&binfmt_lock);
1115 if (retval != -ENOEXEC || bprm->mm == NULL) {
1116 break;
1117#ifdef CONFIG_KMOD
1118 }else{
1119#define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1120 if (printable(bprm->buf[0]) &&
1121 printable(bprm->buf[1]) &&
1122 printable(bprm->buf[2]) &&
1123 printable(bprm->buf[3]))
1124 break; /* -ENOEXEC */
1125 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1126#endif
1127 }
1128 }
1129 return retval;
1130}
1131
1132EXPORT_SYMBOL(search_binary_handler);
1133
1134/*
1135 * sys_execve() executes a new program.
1136 */
1137int do_execve(char * filename,
1138 char __user *__user *argv,
1139 char __user *__user *envp,
1140 struct pt_regs * regs)
1141{
1142 struct linux_binprm *bprm;
1143 struct file *file;
1144 int retval;
1145 int i;
1146
1147 retval = -ENOMEM;
1148 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1149 if (!bprm)
1150 goto out_ret;
1151 memset(bprm, 0, sizeof(*bprm));
1152
1153 file = open_exec(filename);
1154 retval = PTR_ERR(file);
1155 if (IS_ERR(file))
1156 goto out_kfree;
1157
1158 sched_exec();
1159
1160 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1161
1162 bprm->file = file;
1163 bprm->filename = filename;
1164 bprm->interp = filename;
1165 bprm->mm = mm_alloc();
1166 retval = -ENOMEM;
1167 if (!bprm->mm)
1168 goto out_file;
1169
1170 retval = init_new_context(current, bprm->mm);
1171 if (retval < 0)
1172 goto out_mm;
1173
1174 bprm->argc = count(argv, bprm->p / sizeof(void *));
1175 if ((retval = bprm->argc) < 0)
1176 goto out_mm;
1177
1178 bprm->envc = count(envp, bprm->p / sizeof(void *));
1179 if ((retval = bprm->envc) < 0)
1180 goto out_mm;
1181
1182 retval = security_bprm_alloc(bprm);
1183 if (retval)
1184 goto out;
1185
1186 retval = prepare_binprm(bprm);
1187 if (retval < 0)
1188 goto out;
1189
1190 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1191 if (retval < 0)
1192 goto out;
1193
1194 bprm->exec = bprm->p;
1195 retval = copy_strings(bprm->envc, envp, bprm);
1196 if (retval < 0)
1197 goto out;
1198
1199 retval = copy_strings(bprm->argc, argv, bprm);
1200 if (retval < 0)
1201 goto out;
1202
1203 retval = search_binary_handler(bprm,regs);
1204 if (retval >= 0) {
1205 free_arg_pages(bprm);
1206
1207 /* execve success */
1208 security_bprm_free(bprm);
1209 acct_update_integrals(current);
1da177e4
LT
1210 kfree(bprm);
1211 return retval;
1212 }
1213
1214out:
1215 /* Something went wrong, return the inode and free the argument pages*/
1216 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1217 struct page * page = bprm->page[i];
1218 if (page)
1219 __free_page(page);
1220 }
1221
1222 if (bprm->security)
1223 security_bprm_free(bprm);
1224
1225out_mm:
1226 if (bprm->mm)
1227 mmdrop(bprm->mm);
1228
1229out_file:
1230 if (bprm->file) {
1231 allow_write_access(bprm->file);
1232 fput(bprm->file);
1233 }
1234
1235out_kfree:
1236 kfree(bprm);
1237
1238out_ret:
1239 return retval;
1240}
1241
1242int set_binfmt(struct linux_binfmt *new)
1243{
1244 struct linux_binfmt *old = current->binfmt;
1245
1246 if (new) {
1247 if (!try_module_get(new->module))
1248 return -1;
1249 }
1250 current->binfmt = new;
1251 if (old)
1252 module_put(old->module);
1253 return 0;
1254}
1255
1256EXPORT_SYMBOL(set_binfmt);
1257
1258#define CORENAME_MAX_SIZE 64
1259
1260/* format_corename will inspect the pattern parameter, and output a
1261 * name into corename, which must have space for at least
1262 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1263 */
1264static void format_corename(char *corename, const char *pattern, long signr)
1265{
1266 const char *pat_ptr = pattern;
1267 char *out_ptr = corename;
1268 char *const out_end = corename + CORENAME_MAX_SIZE;
1269 int rc;
1270 int pid_in_pattern = 0;
1271
1272 /* Repeat as long as we have more pattern to process and more output
1273 space */
1274 while (*pat_ptr) {
1275 if (*pat_ptr != '%') {
1276 if (out_ptr == out_end)
1277 goto out;
1278 *out_ptr++ = *pat_ptr++;
1279 } else {
1280 switch (*++pat_ptr) {
1281 case 0:
1282 goto out;
1283 /* Double percent, output one percent */
1284 case '%':
1285 if (out_ptr == out_end)
1286 goto out;
1287 *out_ptr++ = '%';
1288 break;
1289 /* pid */
1290 case 'p':
1291 pid_in_pattern = 1;
1292 rc = snprintf(out_ptr, out_end - out_ptr,
1293 "%d", current->tgid);
1294 if (rc > out_end - out_ptr)
1295 goto out;
1296 out_ptr += rc;
1297 break;
1298 /* uid */
1299 case 'u':
1300 rc = snprintf(out_ptr, out_end - out_ptr,
1301 "%d", current->uid);
1302 if (rc > out_end - out_ptr)
1303 goto out;
1304 out_ptr += rc;
1305 break;
1306 /* gid */
1307 case 'g':
1308 rc = snprintf(out_ptr, out_end - out_ptr,
1309 "%d", current->gid);
1310 if (rc > out_end - out_ptr)
1311 goto out;
1312 out_ptr += rc;
1313 break;
1314 /* signal that caused the coredump */
1315 case 's':
1316 rc = snprintf(out_ptr, out_end - out_ptr,
1317 "%ld", signr);
1318 if (rc > out_end - out_ptr)
1319 goto out;
1320 out_ptr += rc;
1321 break;
1322 /* UNIX time of coredump */
1323 case 't': {
1324 struct timeval tv;
1325 do_gettimeofday(&tv);
1326 rc = snprintf(out_ptr, out_end - out_ptr,
1327 "%lu", tv.tv_sec);
1328 if (rc > out_end - out_ptr)
1329 goto out;
1330 out_ptr += rc;
1331 break;
1332 }
1333 /* hostname */
1334 case 'h':
1335 down_read(&uts_sem);
1336 rc = snprintf(out_ptr, out_end - out_ptr,
1337 "%s", system_utsname.nodename);
1338 up_read(&uts_sem);
1339 if (rc > out_end - out_ptr)
1340 goto out;
1341 out_ptr += rc;
1342 break;
1343 /* executable */
1344 case 'e':
1345 rc = snprintf(out_ptr, out_end - out_ptr,
1346 "%s", current->comm);
1347 if (rc > out_end - out_ptr)
1348 goto out;
1349 out_ptr += rc;
1350 break;
1351 default:
1352 break;
1353 }
1354 ++pat_ptr;
1355 }
1356 }
1357 /* Backward compatibility with core_uses_pid:
1358 *
1359 * If core_pattern does not include a %p (as is the default)
1360 * and core_uses_pid is set, then .%pid will be appended to
1361 * the filename */
1362 if (!pid_in_pattern
1363 && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1364 rc = snprintf(out_ptr, out_end - out_ptr,
1365 ".%d", current->tgid);
1366 if (rc > out_end - out_ptr)
1367 goto out;
1368 out_ptr += rc;
1369 }
1370 out:
1371 *out_ptr = 0;
1372}
1373
1374static void zap_threads (struct mm_struct *mm)
1375{
1376 struct task_struct *g, *p;
1377 struct task_struct *tsk = current;
1378 struct completion *vfork_done = tsk->vfork_done;
1379 int traced = 0;
1380
1381 /*
1382 * Make sure nobody is waiting for us to release the VM,
1383 * otherwise we can deadlock when we wait on each other
1384 */
1385 if (vfork_done) {
1386 tsk->vfork_done = NULL;
1387 complete(vfork_done);
1388 }
1389
1390 read_lock(&tasklist_lock);
1391 do_each_thread(g,p)
1392 if (mm == p->mm && p != tsk) {
1393 force_sig_specific(SIGKILL, p);
1394 mm->core_waiters++;
1395 if (unlikely(p->ptrace) &&
1396 unlikely(p->parent->mm == mm))
1397 traced = 1;
1398 }
1399 while_each_thread(g,p);
1400
1401 read_unlock(&tasklist_lock);
1402
1403 if (unlikely(traced)) {
1404 /*
1405 * We are zapping a thread and the thread it ptraces.
1406 * If the tracee went into a ptrace stop for exit tracing,
1407 * we could deadlock since the tracer is waiting for this
1408 * coredump to finish. Detach them so they can both die.
1409 */
1410 write_lock_irq(&tasklist_lock);
1411 do_each_thread(g,p) {
1412 if (mm == p->mm && p != tsk &&
1413 p->ptrace && p->parent->mm == mm) {
1414 __ptrace_unlink(p);
1415 }
1416 } while_each_thread(g,p);
1417 write_unlock_irq(&tasklist_lock);
1418 }
1419}
1420
1421static void coredump_wait(struct mm_struct *mm)
1422{
1423 DECLARE_COMPLETION(startup_done);
1424
1425 mm->core_waiters++; /* let other threads block */
1426 mm->core_startup_done = &startup_done;
1427
1428 /* give other threads a chance to run: */
1429 yield();
1430
1431 zap_threads(mm);
1432 if (--mm->core_waiters) {
1433 up_write(&mm->mmap_sem);
1434 wait_for_completion(&startup_done);
1435 } else
1436 up_write(&mm->mmap_sem);
1437 BUG_ON(mm->core_waiters);
1438}
1439
1440int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1441{
1442 char corename[CORENAME_MAX_SIZE + 1];
1443 struct mm_struct *mm = current->mm;
1444 struct linux_binfmt * binfmt;
1445 struct inode * inode;
1446 struct file * file;
1447 int retval = 0;
d6e71144
AC
1448 int fsuid = current->fsuid;
1449 int flag = 0;
1da177e4
LT
1450
1451 binfmt = current->binfmt;
1452 if (!binfmt || !binfmt->core_dump)
1453 goto fail;
1454 down_write(&mm->mmap_sem);
1455 if (!mm->dumpable) {
1456 up_write(&mm->mmap_sem);
1457 goto fail;
1458 }
d6e71144
AC
1459
1460 /*
1461 * We cannot trust fsuid as being the "true" uid of the
1462 * process nor do we know its entire history. We only know it
1463 * was tainted so we dump it as root in mode 2.
1464 */
1465 if (mm->dumpable == 2) { /* Setuid core dump mode */
1466 flag = O_EXCL; /* Stop rewrite attacks */
1467 current->fsuid = 0; /* Dump root private */
1468 }
1da177e4
LT
1469 mm->dumpable = 0;
1470 init_completion(&mm->core_done);
1471 spin_lock_irq(&current->sighand->siglock);
1472 current->signal->flags = SIGNAL_GROUP_EXIT;
1473 current->signal->group_exit_code = exit_code;
1474 spin_unlock_irq(&current->sighand->siglock);
1475 coredump_wait(mm);
1476
1477 /*
1478 * Clear any false indication of pending signals that might
1479 * be seen by the filesystem code called to write the core file.
1480 */
1481 current->signal->group_stop_count = 0;
1482 clear_thread_flag(TIF_SIGPENDING);
1483
1484 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1485 goto fail_unlock;
1486
1487 /*
1488 * lock_kernel() because format_corename() is controlled by sysctl, which
1489 * uses lock_kernel()
1490 */
1491 lock_kernel();
1492 format_corename(corename, core_pattern, signr);
1493 unlock_kernel();
d6e71144 1494 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1da177e4
LT
1495 if (IS_ERR(file))
1496 goto fail_unlock;
1497 inode = file->f_dentry->d_inode;
1498 if (inode->i_nlink > 1)
1499 goto close_fail; /* multiple links - don't dump */
1500 if (d_unhashed(file->f_dentry))
1501 goto close_fail;
1502
1503 if (!S_ISREG(inode->i_mode))
1504 goto close_fail;
1505 if (!file->f_op)
1506 goto close_fail;
1507 if (!file->f_op->write)
1508 goto close_fail;
1509 if (do_truncate(file->f_dentry, 0) != 0)
1510 goto close_fail;
1511
1512 retval = binfmt->core_dump(signr, regs, file);
1513
1514 if (retval)
1515 current->signal->group_exit_code |= 0x80;
1516close_fail:
1517 filp_close(file, NULL);
1518fail_unlock:
d6e71144 1519 current->fsuid = fsuid;
1da177e4
LT
1520 complete_all(&mm->core_done);
1521fail:
1522 return retval;
1523}