4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * #!-checking implemented by tytso.
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.
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.
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
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>
52 #include <asm/uaccess.h>
53 #include <asm/mmu_context.h>
56 #include <linux/kmod.h>
60 char core_pattern
[65] = "core";
61 /* The maximal length of core_pattern is also specified in sysctl.c */
63 static struct linux_binfmt
*formats
;
64 static DEFINE_RWLOCK(binfmt_lock
);
66 int register_binfmt(struct linux_binfmt
* fmt
)
68 struct linux_binfmt
** tmp
= &formats
;
74 write_lock(&binfmt_lock
);
77 write_unlock(&binfmt_lock
);
84 write_unlock(&binfmt_lock
);
88 EXPORT_SYMBOL(register_binfmt
);
90 int unregister_binfmt(struct linux_binfmt
* fmt
)
92 struct linux_binfmt
** tmp
= &formats
;
94 write_lock(&binfmt_lock
);
98 write_unlock(&binfmt_lock
);
103 write_unlock(&binfmt_lock
);
107 EXPORT_SYMBOL(unregister_binfmt
);
109 static inline void put_binfmt(struct linux_binfmt
* fmt
)
111 module_put(fmt
->module
);
115 * Note that a shared library must be both readable and executable due to
118 * Also note that we take the address to load from from the file itself.
120 asmlinkage
long sys_uselib(const char __user
* library
)
126 nd
.intent
.open
.flags
= FMODE_READ
;
127 error
= __user_walk(library
, LOOKUP_FOLLOW
|LOOKUP_OPEN
, &nd
);
132 if (!S_ISREG(nd
.dentry
->d_inode
->i_mode
))
135 error
= permission(nd
.dentry
->d_inode
, MAY_READ
| MAY_EXEC
, &nd
);
139 file
= dentry_open(nd
.dentry
, nd
.mnt
, O_RDONLY
);
140 error
= PTR_ERR(file
);
146 struct linux_binfmt
* fmt
;
148 read_lock(&binfmt_lock
);
149 for (fmt
= formats
; fmt
; fmt
= fmt
->next
) {
150 if (!fmt
->load_shlib
)
152 if (!try_module_get(fmt
->module
))
154 read_unlock(&binfmt_lock
);
155 error
= fmt
->load_shlib(file
);
156 read_lock(&binfmt_lock
);
158 if (error
!= -ENOEXEC
)
161 read_unlock(&binfmt_lock
);
172 * count() counts the number of strings in array ARGV.
174 static int count(char __user
* __user
* argv
, int max
)
182 if (get_user(p
, argv
))
196 * 'copy_strings()' copies argument/environment strings from user
197 * memory to free pages in kernel mem. These are in a format ready
198 * to be put directly into the top of new user memory.
200 static int copy_strings(int argc
, char __user
* __user
* argv
,
201 struct linux_binprm
*bprm
)
203 struct page
*kmapped_page
= NULL
;
212 if (get_user(str
, argv
+argc
) ||
213 !(len
= strnlen_user(str
, bprm
->p
))) {
224 /* XXX: add architecture specific overflow check here. */
229 int offset
, bytes_to_copy
;
232 offset
= pos
% PAGE_SIZE
;
234 page
= bprm
->page
[i
];
237 page
= alloc_page(GFP_HIGHUSER
);
238 bprm
->page
[i
] = page
;
246 if (page
!= kmapped_page
) {
248 kunmap(kmapped_page
);
250 kaddr
= kmap(kmapped_page
);
253 memset(kaddr
, 0, offset
);
254 bytes_to_copy
= PAGE_SIZE
- offset
;
255 if (bytes_to_copy
> len
) {
258 memset(kaddr
+offset
+len
, 0,
259 PAGE_SIZE
-offset
-len
);
261 err
= copy_from_user(kaddr
+offset
, str
, bytes_to_copy
);
267 pos
+= bytes_to_copy
;
268 str
+= bytes_to_copy
;
269 len
-= bytes_to_copy
;
275 kunmap(kmapped_page
);
280 * Like copy_strings, but get argv and its values from kernel memory.
282 int copy_strings_kernel(int argc
,char ** argv
, struct linux_binprm
*bprm
)
285 mm_segment_t oldfs
= get_fs();
287 r
= copy_strings(argc
, (char __user
* __user
*)argv
, bprm
);
292 EXPORT_SYMBOL(copy_strings_kernel
);
296 * This routine is used to map in a page into an address space: needed by
297 * execve() for the initial stack and environment pages.
299 * vma->vm_mm->mmap_sem is held for writing.
301 void install_arg_page(struct vm_area_struct
*vma
,
302 struct page
*page
, unsigned long address
)
304 struct mm_struct
*mm
= vma
->vm_mm
;
310 if (unlikely(anon_vma_prepare(vma
)))
313 flush_dcache_page(page
);
314 pgd
= pgd_offset(mm
, address
);
316 spin_lock(&mm
->page_table_lock
);
317 pud
= pud_alloc(mm
, pgd
, address
);
320 pmd
= pmd_alloc(mm
, pud
, address
);
323 pte
= pte_alloc_map(mm
, pmd
, address
);
326 if (!pte_none(*pte
)) {
330 inc_mm_counter(mm
, rss
);
331 lru_cache_add_active(page
);
332 set_pte_at(mm
, address
, pte
, pte_mkdirty(pte_mkwrite(mk_pte(
333 page
, vma
->vm_page_prot
))));
334 page_add_anon_rmap(page
, vma
, address
);
336 spin_unlock(&mm
->page_table_lock
);
338 /* no need for flush_tlb */
341 spin_unlock(&mm
->page_table_lock
);
344 force_sig(SIGKILL
, current
);
347 #define EXTRA_STACK_VM_PAGES 20 /* random */
349 int setup_arg_pages(struct linux_binprm
*bprm
,
350 unsigned long stack_top
,
351 int executable_stack
)
353 unsigned long stack_base
;
354 struct vm_area_struct
*mpnt
;
355 struct mm_struct
*mm
= current
->mm
;
359 #ifdef CONFIG_STACK_GROWSUP
360 /* Move the argument and environment strings to the bottom of the
366 /* Start by shifting all the pages down */
368 for (j
= 0; j
< MAX_ARG_PAGES
; j
++) {
369 struct page
*page
= bprm
->page
[j
];
372 bprm
->page
[i
++] = page
;
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]);
385 memmove(to
, to
+ offset
, PAGE_SIZE
- offset
);
386 kunmap(bprm
->page
[j
- 1]);
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
);
394 /* Adjust bprm->p to point to the end of the strings. */
395 bprm
->p
= stack_base
+ PAGE_SIZE
* i
- offset
;
397 mm
->arg_start
= stack_base
;
398 arg_size
= i
<< PAGE_SHIFT
;
400 /* zero pages that were copied above */
401 while (i
< MAX_ARG_PAGES
)
402 bprm
->page
[i
++] = NULL
;
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
);
411 arg_size
+= EXTRA_STACK_VM_PAGES
* PAGE_SIZE
;
414 bprm
->loader
+= stack_base
;
415 bprm
->exec
+= stack_base
;
417 mpnt
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
421 if (security_vm_enough_memory(arg_size
>> PAGE_SHIFT
)) {
422 kmem_cache_free(vm_area_cachep
, mpnt
);
426 memset(mpnt
, 0, sizeof(*mpnt
));
428 down_write(&mm
->mmap_sem
);
431 #ifdef CONFIG_STACK_GROWSUP
432 mpnt
->vm_start
= stack_base
;
433 mpnt
->vm_end
= stack_base
+ arg_size
;
435 mpnt
->vm_end
= stack_top
;
436 mpnt
->vm_start
= mpnt
->vm_end
- arg_size
;
438 /* Adjust stack execute permissions; explicitly enable
439 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
440 * and leave alone (arch default) otherwise. */
441 if (unlikely(executable_stack
== EXSTACK_ENABLE_X
))
442 mpnt
->vm_flags
= VM_STACK_FLAGS
| VM_EXEC
;
443 else if (executable_stack
== EXSTACK_DISABLE_X
)
444 mpnt
->vm_flags
= VM_STACK_FLAGS
& ~VM_EXEC
;
446 mpnt
->vm_flags
= VM_STACK_FLAGS
;
447 mpnt
->vm_flags
|= mm
->def_flags
;
448 mpnt
->vm_page_prot
= protection_map
[mpnt
->vm_flags
& 0x7];
449 if ((ret
= insert_vm_struct(mm
, mpnt
))) {
450 up_write(&mm
->mmap_sem
);
451 kmem_cache_free(vm_area_cachep
, mpnt
);
454 mm
->stack_vm
= mm
->total_vm
= vma_pages(mpnt
);
457 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
458 struct page
*page
= bprm
->page
[i
];
460 bprm
->page
[i
] = NULL
;
461 install_arg_page(mpnt
, page
, stack_base
);
463 stack_base
+= PAGE_SIZE
;
465 up_write(&mm
->mmap_sem
);
470 EXPORT_SYMBOL(setup_arg_pages
);
472 #define free_arg_pages(bprm) do { } while (0)
476 static inline void free_arg_pages(struct linux_binprm
*bprm
)
480 for (i
= 0; i
< MAX_ARG_PAGES
; i
++) {
482 __free_page(bprm
->page
[i
]);
483 bprm
->page
[i
] = NULL
;
487 #endif /* CONFIG_MMU */
489 struct file
*open_exec(const char *name
)
495 nd
.intent
.open
.flags
= FMODE_READ
;
496 err
= path_lookup(name
, LOOKUP_FOLLOW
|LOOKUP_OPEN
, &nd
);
500 struct inode
*inode
= nd
.dentry
->d_inode
;
501 file
= ERR_PTR(-EACCES
);
502 if (!(nd
.mnt
->mnt_flags
& MNT_NOEXEC
) &&
503 S_ISREG(inode
->i_mode
)) {
504 int err
= permission(inode
, MAY_EXEC
, &nd
);
505 if (!err
&& !(inode
->i_mode
& 0111))
509 file
= dentry_open(nd
.dentry
, nd
.mnt
, O_RDONLY
);
511 err
= deny_write_access(file
);
526 EXPORT_SYMBOL(open_exec
);
528 int kernel_read(struct file
*file
, unsigned long offset
,
529 char *addr
, unsigned long count
)
537 /* The cast to a user pointer is valid due to the set_fs() */
538 result
= vfs_read(file
, (void __user
*)addr
, count
, &pos
);
543 EXPORT_SYMBOL(kernel_read
);
545 static int exec_mmap(struct mm_struct
*mm
)
547 struct task_struct
*tsk
;
548 struct mm_struct
* old_mm
, *active_mm
;
550 /* Notify parent that we're no longer interested in the old VM */
552 old_mm
= current
->mm
;
553 mm_release(tsk
, old_mm
);
557 * Make sure that if there is a core dump in progress
558 * for the old mm, we get out and die instead of going
559 * through with the exec. We must hold mmap_sem around
560 * checking core_waiters and changing tsk->mm. The
561 * core-inducing thread will increment core_waiters for
562 * each thread whose ->mm == old_mm.
564 down_read(&old_mm
->mmap_sem
);
565 if (unlikely(old_mm
->core_waiters
)) {
566 up_read(&old_mm
->mmap_sem
);
571 active_mm
= tsk
->active_mm
;
574 activate_mm(active_mm
, mm
);
576 arch_pick_mmap_layout(mm
);
578 up_read(&old_mm
->mmap_sem
);
579 if (active_mm
!= old_mm
) BUG();
588 * This function makes sure the current process has its own signal table,
589 * so that flush_signal_handlers can later reset the handlers without
590 * disturbing other processes. (Other processes might share the signal
591 * table via the CLONE_SIGHAND option to clone().)
593 static inline int de_thread(struct task_struct
*tsk
)
595 struct signal_struct
*sig
= tsk
->signal
;
596 struct sighand_struct
*newsighand
, *oldsighand
= tsk
->sighand
;
597 spinlock_t
*lock
= &oldsighand
->siglock
;
601 * If we don't share sighandlers, then we aren't sharing anything
602 * and we can just re-use it all.
604 if (atomic_read(&oldsighand
->count
) <= 1) {
605 BUG_ON(atomic_read(&sig
->count
) != 1);
610 newsighand
= kmem_cache_alloc(sighand_cachep
, GFP_KERNEL
);
614 if (thread_group_empty(current
))
615 goto no_thread_group
;
618 * Kill all other threads in the thread group.
619 * We must hold tasklist_lock to call zap_other_threads.
621 read_lock(&tasklist_lock
);
623 if (sig
->flags
& SIGNAL_GROUP_EXIT
) {
625 * Another group action in progress, just
626 * return so that the signal is processed.
628 spin_unlock_irq(lock
);
629 read_unlock(&tasklist_lock
);
630 kmem_cache_free(sighand_cachep
, newsighand
);
633 zap_other_threads(current
);
634 read_unlock(&tasklist_lock
);
637 * Account for the thread group leader hanging around:
640 if (thread_group_leader(current
))
642 while (atomic_read(&sig
->count
) > count
) {
643 sig
->group_exit_task
= current
;
644 sig
->notify_count
= count
;
645 __set_current_state(TASK_UNINTERRUPTIBLE
);
646 spin_unlock_irq(lock
);
650 sig
->group_exit_task
= NULL
;
651 sig
->notify_count
= 0;
652 spin_unlock_irq(lock
);
655 * At this point all other threads have exited, all we have to
656 * do is to wait for the thread group leader to become inactive,
657 * and to assume its PID:
659 if (!thread_group_leader(current
)) {
660 struct task_struct
*leader
= current
->group_leader
, *parent
;
661 struct dentry
*proc_dentry1
, *proc_dentry2
;
662 unsigned long exit_state
, ptrace
;
665 * Wait for the thread group leader to be a zombie.
666 * It should already be zombie at this point, most
669 while (leader
->exit_state
!= EXIT_ZOMBIE
)
672 spin_lock(&leader
->proc_lock
);
673 spin_lock(¤t
->proc_lock
);
674 proc_dentry1
= proc_pid_unhash(current
);
675 proc_dentry2
= proc_pid_unhash(leader
);
676 write_lock_irq(&tasklist_lock
);
678 if (leader
->tgid
!= current
->tgid
)
680 if (current
->pid
== current
->tgid
)
683 * An exec() starts a new thread group with the
684 * TGID of the previous thread group. Rehash the
685 * two threads with a switched PID, and release
686 * the former thread group leader:
688 ptrace
= leader
->ptrace
;
689 parent
= leader
->parent
;
690 if (unlikely(ptrace
) && unlikely(parent
== current
)) {
692 * Joker was ptracing his own group leader,
693 * and now he wants to be his own parent!
694 * We can't have that.
699 ptrace_unlink(current
);
700 ptrace_unlink(leader
);
701 remove_parent(current
);
702 remove_parent(leader
);
704 switch_exec_pids(leader
, current
);
706 current
->parent
= current
->real_parent
= leader
->real_parent
;
707 leader
->parent
= leader
->real_parent
= child_reaper
;
708 current
->group_leader
= current
;
709 leader
->group_leader
= leader
;
711 add_parent(current
, current
->parent
);
712 add_parent(leader
, leader
->parent
);
714 current
->ptrace
= ptrace
;
715 __ptrace_link(current
, parent
);
718 list_del(¤t
->tasks
);
719 list_add_tail(¤t
->tasks
, &init_task
.tasks
);
720 current
->exit_signal
= SIGCHLD
;
721 exit_state
= leader
->exit_state
;
723 write_unlock_irq(&tasklist_lock
);
724 spin_unlock(&leader
->proc_lock
);
725 spin_unlock(¤t
->proc_lock
);
726 proc_pid_flush(proc_dentry1
);
727 proc_pid_flush(proc_dentry2
);
729 if (exit_state
!= EXIT_ZOMBIE
)
731 release_task(leader
);
735 * Now there are really no other threads at all,
736 * so it's safe to stop telling them to kill themselves.
741 BUG_ON(atomic_read(&sig
->count
) != 1);
744 if (atomic_read(&oldsighand
->count
) == 1) {
746 * Now that we nuked the rest of the thread group,
747 * it turns out we are not sharing sighand any more either.
748 * So we can just keep it.
750 kmem_cache_free(sighand_cachep
, newsighand
);
753 * Move our state over to newsighand and switch it in.
755 spin_lock_init(&newsighand
->siglock
);
756 atomic_set(&newsighand
->count
, 1);
757 memcpy(newsighand
->action
, oldsighand
->action
,
758 sizeof(newsighand
->action
));
760 write_lock_irq(&tasklist_lock
);
761 spin_lock(&oldsighand
->siglock
);
762 spin_lock(&newsighand
->siglock
);
764 current
->sighand
= newsighand
;
767 spin_unlock(&newsighand
->siglock
);
768 spin_unlock(&oldsighand
->siglock
);
769 write_unlock_irq(&tasklist_lock
);
771 if (atomic_dec_and_test(&oldsighand
->count
))
772 kmem_cache_free(sighand_cachep
, oldsighand
);
775 if (!thread_group_empty(current
))
777 if (!thread_group_leader(current
))
783 * These functions flushes out all traces of the currently running executable
784 * so that a new one can be started
787 static inline void flush_old_files(struct files_struct
* files
)
791 spin_lock(&files
->file_lock
);
793 unsigned long set
, i
;
797 if (i
>= files
->max_fds
|| i
>= files
->max_fdset
)
799 set
= files
->close_on_exec
->fds_bits
[j
];
802 files
->close_on_exec
->fds_bits
[j
] = 0;
803 spin_unlock(&files
->file_lock
);
804 for ( ; set
; i
++,set
>>= 1) {
809 spin_lock(&files
->file_lock
);
812 spin_unlock(&files
->file_lock
);
815 void get_task_comm(char *buf
, struct task_struct
*tsk
)
817 /* buf must be at least sizeof(tsk->comm) in size */
819 strncpy(buf
, tsk
->comm
, sizeof(tsk
->comm
));
823 void set_task_comm(struct task_struct
*tsk
, char *buf
)
826 strlcpy(tsk
->comm
, buf
, sizeof(tsk
->comm
));
830 int flush_old_exec(struct linux_binprm
* bprm
)
834 struct files_struct
*files
;
835 char tcomm
[sizeof(current
->comm
)];
838 * Make sure we have a private signal table and that
839 * we are unassociated from the previous thread group.
841 retval
= de_thread(current
);
846 * Make sure we have private file handles. Ask the
847 * fork helper to do the work for us and the exit
848 * helper to do the cleanup of the old one.
850 files
= current
->files
; /* refcounted so safe to hold */
851 retval
= unshare_files();
855 * Release all of the old mmap stuff
857 retval
= exec_mmap(bprm
->mm
);
861 bprm
->mm
= NULL
; /* We're using it now */
863 /* This is the point of no return */
865 put_files_struct(files
);
867 current
->sas_ss_sp
= current
->sas_ss_size
= 0;
869 if (current
->euid
== current
->uid
&& current
->egid
== current
->gid
)
870 current
->mm
->dumpable
= 1;
871 name
= bprm
->filename
;
873 /* Copies the binary name from after last slash */
874 for (i
=0; (ch
= *(name
++)) != '\0';) {
876 i
= 0; /* overwrite what we wrote */
878 if (i
< (sizeof(tcomm
) - 1))
882 set_task_comm(current
, tcomm
);
884 current
->flags
&= ~PF_RANDOMIZE
;
887 if (bprm
->e_uid
!= current
->euid
|| bprm
->e_gid
!= current
->egid
||
888 permission(bprm
->file
->f_dentry
->d_inode
,MAY_READ
, NULL
) ||
889 (bprm
->interp_flags
& BINPRM_FLAGS_ENFORCE_NONDUMP
)) {
891 current
->mm
->dumpable
= 0;
894 /* An exec changes our domain. We are no longer part of the thread
897 current
->self_exec_id
++;
899 flush_signal_handlers(current
, 0);
900 flush_old_files(current
->files
);
905 put_files_struct(current
->files
);
906 current
->files
= files
;
911 EXPORT_SYMBOL(flush_old_exec
);
914 * Fill the binprm structure from the inode.
915 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
917 int prepare_binprm(struct linux_binprm
*bprm
)
920 struct inode
* inode
= bprm
->file
->f_dentry
->d_inode
;
923 mode
= inode
->i_mode
;
925 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
926 * generic_permission lets a non-executable through
928 if (!(mode
& 0111)) /* with at least _one_ execute bit set */
930 if (bprm
->file
->f_op
== NULL
)
933 bprm
->e_uid
= current
->euid
;
934 bprm
->e_gid
= current
->egid
;
936 if(!(bprm
->file
->f_vfsmnt
->mnt_flags
& MNT_NOSUID
)) {
938 if (mode
& S_ISUID
) {
939 current
->personality
&= ~PER_CLEAR_ON_SETID
;
940 bprm
->e_uid
= inode
->i_uid
;
945 * If setgid is set but no group execute bit then this
946 * is a candidate for mandatory locking, not a setgid
949 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
950 current
->personality
&= ~PER_CLEAR_ON_SETID
;
951 bprm
->e_gid
= inode
->i_gid
;
955 /* fill in binprm security blob */
956 retval
= security_bprm_set(bprm
);
960 memset(bprm
->buf
,0,BINPRM_BUF_SIZE
);
961 return kernel_read(bprm
->file
,0,bprm
->buf
,BINPRM_BUF_SIZE
);
964 EXPORT_SYMBOL(prepare_binprm
);
966 static inline int unsafe_exec(struct task_struct
*p
)
969 if (p
->ptrace
& PT_PTRACED
) {
970 if (p
->ptrace
& PT_PTRACE_CAP
)
971 unsafe
|= LSM_UNSAFE_PTRACE_CAP
;
973 unsafe
|= LSM_UNSAFE_PTRACE
;
975 if (atomic_read(&p
->fs
->count
) > 1 ||
976 atomic_read(&p
->files
->count
) > 1 ||
977 atomic_read(&p
->sighand
->count
) > 1)
978 unsafe
|= LSM_UNSAFE_SHARE
;
983 void compute_creds(struct linux_binprm
*bprm
)
987 if (bprm
->e_uid
!= current
->uid
)
992 unsafe
= unsafe_exec(current
);
993 security_bprm_apply_creds(bprm
, unsafe
);
994 task_unlock(current
);
995 security_bprm_post_apply_creds(bprm
);
998 EXPORT_SYMBOL(compute_creds
);
1000 void remove_arg_zero(struct linux_binprm
*bprm
)
1003 unsigned long offset
;
1007 offset
= bprm
->p
% PAGE_SIZE
;
1010 while (bprm
->p
++, *(kaddr
+offset
++)) {
1011 if (offset
!= PAGE_SIZE
)
1014 kunmap_atomic(kaddr
, KM_USER0
);
1016 page
= bprm
->page
[bprm
->p
/PAGE_SIZE
];
1017 kaddr
= kmap_atomic(page
, KM_USER0
);
1019 kunmap_atomic(kaddr
, KM_USER0
);
1024 EXPORT_SYMBOL(remove_arg_zero
);
1027 * cycle the list of binary formats handler, until one recognizes the image
1029 int search_binary_handler(struct linux_binprm
*bprm
,struct pt_regs
*regs
)
1032 struct linux_binfmt
*fmt
;
1034 /* handle /sbin/loader.. */
1036 struct exec
* eh
= (struct exec
*) bprm
->buf
;
1038 if (!bprm
->loader
&& eh
->fh
.f_magic
== 0x183 &&
1039 (eh
->fh
.f_flags
& 0x3000) == 0x3000)
1042 unsigned long loader
;
1044 allow_write_access(bprm
->file
);
1048 loader
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1050 file
= open_exec("/sbin/loader");
1051 retval
= PTR_ERR(file
);
1055 /* Remember if the application is TASO. */
1056 bprm
->sh_bang
= eh
->ah
.entry
< 0x100000000UL
;
1059 bprm
->loader
= loader
;
1060 retval
= prepare_binprm(bprm
);
1063 /* should call search_binary_handler recursively here,
1064 but it does not matter */
1068 retval
= security_bprm_check(bprm
);
1072 /* kernel module loader fixup */
1073 /* so we don't try to load run modprobe in kernel space. */
1076 for (try=0; try<2; try++) {
1077 read_lock(&binfmt_lock
);
1078 for (fmt
= formats
; fmt
; fmt
= fmt
->next
) {
1079 int (*fn
)(struct linux_binprm
*, struct pt_regs
*) = fmt
->load_binary
;
1082 if (!try_module_get(fmt
->module
))
1084 read_unlock(&binfmt_lock
);
1085 retval
= fn(bprm
, regs
);
1088 allow_write_access(bprm
->file
);
1092 current
->did_exec
= 1;
1095 read_lock(&binfmt_lock
);
1097 if (retval
!= -ENOEXEC
|| bprm
->mm
== NULL
)
1100 read_unlock(&binfmt_lock
);
1104 read_unlock(&binfmt_lock
);
1105 if (retval
!= -ENOEXEC
|| bprm
->mm
== NULL
) {
1109 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1110 if (printable(bprm
->buf
[0]) &&
1111 printable(bprm
->buf
[1]) &&
1112 printable(bprm
->buf
[2]) &&
1113 printable(bprm
->buf
[3]))
1114 break; /* -ENOEXEC */
1115 request_module("binfmt-%04x", *(unsigned short *)(&bprm
->buf
[2]));
1122 EXPORT_SYMBOL(search_binary_handler
);
1125 * sys_execve() executes a new program.
1127 int do_execve(char * filename
,
1128 char __user
*__user
*argv
,
1129 char __user
*__user
*envp
,
1130 struct pt_regs
* regs
)
1132 struct linux_binprm
*bprm
;
1138 bprm
= kmalloc(sizeof(*bprm
), GFP_KERNEL
);
1141 memset(bprm
, 0, sizeof(*bprm
));
1143 file
= open_exec(filename
);
1144 retval
= PTR_ERR(file
);
1150 bprm
->p
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1153 bprm
->filename
= filename
;
1154 bprm
->interp
= filename
;
1155 bprm
->mm
= mm_alloc();
1160 retval
= init_new_context(current
, bprm
->mm
);
1164 bprm
->argc
= count(argv
, bprm
->p
/ sizeof(void *));
1165 if ((retval
= bprm
->argc
) < 0)
1168 bprm
->envc
= count(envp
, bprm
->p
/ sizeof(void *));
1169 if ((retval
= bprm
->envc
) < 0)
1172 retval
= security_bprm_alloc(bprm
);
1176 retval
= prepare_binprm(bprm
);
1180 retval
= copy_strings_kernel(1, &bprm
->filename
, bprm
);
1184 bprm
->exec
= bprm
->p
;
1185 retval
= copy_strings(bprm
->envc
, envp
, bprm
);
1189 retval
= copy_strings(bprm
->argc
, argv
, bprm
);
1193 retval
= search_binary_handler(bprm
,regs
);
1195 free_arg_pages(bprm
);
1197 /* execve success */
1198 security_bprm_free(bprm
);
1199 acct_update_integrals(current
);
1200 update_mem_hiwater(current
);
1206 /* Something went wrong, return the inode and free the argument pages*/
1207 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
1208 struct page
* page
= bprm
->page
[i
];
1214 security_bprm_free(bprm
);
1222 allow_write_access(bprm
->file
);
1233 int set_binfmt(struct linux_binfmt
*new)
1235 struct linux_binfmt
*old
= current
->binfmt
;
1238 if (!try_module_get(new->module
))
1241 current
->binfmt
= new;
1243 module_put(old
->module
);
1247 EXPORT_SYMBOL(set_binfmt
);
1249 #define CORENAME_MAX_SIZE 64
1251 /* format_corename will inspect the pattern parameter, and output a
1252 * name into corename, which must have space for at least
1253 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1255 static void format_corename(char *corename
, const char *pattern
, long signr
)
1257 const char *pat_ptr
= pattern
;
1258 char *out_ptr
= corename
;
1259 char *const out_end
= corename
+ CORENAME_MAX_SIZE
;
1261 int pid_in_pattern
= 0;
1263 /* Repeat as long as we have more pattern to process and more output
1266 if (*pat_ptr
!= '%') {
1267 if (out_ptr
== out_end
)
1269 *out_ptr
++ = *pat_ptr
++;
1271 switch (*++pat_ptr
) {
1274 /* Double percent, output one percent */
1276 if (out_ptr
== out_end
)
1283 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1284 "%d", current
->tgid
);
1285 if (rc
> out_end
- out_ptr
)
1291 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1292 "%d", current
->uid
);
1293 if (rc
> out_end
- out_ptr
)
1299 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1300 "%d", current
->gid
);
1301 if (rc
> out_end
- out_ptr
)
1305 /* signal that caused the coredump */
1307 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1309 if (rc
> out_end
- out_ptr
)
1313 /* UNIX time of coredump */
1316 do_gettimeofday(&tv
);
1317 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1319 if (rc
> out_end
- out_ptr
)
1326 down_read(&uts_sem
);
1327 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1328 "%s", system_utsname
.nodename
);
1330 if (rc
> out_end
- out_ptr
)
1336 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1337 "%s", current
->comm
);
1338 if (rc
> out_end
- out_ptr
)
1348 /* Backward compatibility with core_uses_pid:
1350 * If core_pattern does not include a %p (as is the default)
1351 * and core_uses_pid is set, then .%pid will be appended to
1354 && (core_uses_pid
|| atomic_read(¤t
->mm
->mm_users
) != 1)) {
1355 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1356 ".%d", current
->tgid
);
1357 if (rc
> out_end
- out_ptr
)
1365 static void zap_threads (struct mm_struct
*mm
)
1367 struct task_struct
*g
, *p
;
1368 struct task_struct
*tsk
= current
;
1369 struct completion
*vfork_done
= tsk
->vfork_done
;
1373 * Make sure nobody is waiting for us to release the VM,
1374 * otherwise we can deadlock when we wait on each other
1377 tsk
->vfork_done
= NULL
;
1378 complete(vfork_done
);
1381 read_lock(&tasklist_lock
);
1383 if (mm
== p
->mm
&& p
!= tsk
) {
1384 force_sig_specific(SIGKILL
, p
);
1386 if (unlikely(p
->ptrace
) &&
1387 unlikely(p
->parent
->mm
== mm
))
1390 while_each_thread(g
,p
);
1392 read_unlock(&tasklist_lock
);
1394 if (unlikely(traced
)) {
1396 * We are zapping a thread and the thread it ptraces.
1397 * If the tracee went into a ptrace stop for exit tracing,
1398 * we could deadlock since the tracer is waiting for this
1399 * coredump to finish. Detach them so they can both die.
1401 write_lock_irq(&tasklist_lock
);
1402 do_each_thread(g
,p
) {
1403 if (mm
== p
->mm
&& p
!= tsk
&&
1404 p
->ptrace
&& p
->parent
->mm
== mm
) {
1407 } while_each_thread(g
,p
);
1408 write_unlock_irq(&tasklist_lock
);
1412 static void coredump_wait(struct mm_struct
*mm
)
1414 DECLARE_COMPLETION(startup_done
);
1416 mm
->core_waiters
++; /* let other threads block */
1417 mm
->core_startup_done
= &startup_done
;
1419 /* give other threads a chance to run: */
1423 if (--mm
->core_waiters
) {
1424 up_write(&mm
->mmap_sem
);
1425 wait_for_completion(&startup_done
);
1427 up_write(&mm
->mmap_sem
);
1428 BUG_ON(mm
->core_waiters
);
1431 int do_coredump(long signr
, int exit_code
, struct pt_regs
* regs
)
1433 char corename
[CORENAME_MAX_SIZE
+ 1];
1434 struct mm_struct
*mm
= current
->mm
;
1435 struct linux_binfmt
* binfmt
;
1436 struct inode
* inode
;
1440 binfmt
= current
->binfmt
;
1441 if (!binfmt
|| !binfmt
->core_dump
)
1443 down_write(&mm
->mmap_sem
);
1444 if (!mm
->dumpable
) {
1445 up_write(&mm
->mmap_sem
);
1449 init_completion(&mm
->core_done
);
1450 spin_lock_irq(¤t
->sighand
->siglock
);
1451 current
->signal
->flags
= SIGNAL_GROUP_EXIT
;
1452 current
->signal
->group_exit_code
= exit_code
;
1453 spin_unlock_irq(¤t
->sighand
->siglock
);
1457 * Clear any false indication of pending signals that might
1458 * be seen by the filesystem code called to write the core file.
1460 current
->signal
->group_stop_count
= 0;
1461 clear_thread_flag(TIF_SIGPENDING
);
1463 if (current
->signal
->rlim
[RLIMIT_CORE
].rlim_cur
< binfmt
->min_coredump
)
1467 * lock_kernel() because format_corename() is controlled by sysctl, which
1468 * uses lock_kernel()
1471 format_corename(corename
, core_pattern
, signr
);
1473 file
= filp_open(corename
, O_CREAT
| 2 | O_NOFOLLOW
| O_LARGEFILE
, 0600);
1476 inode
= file
->f_dentry
->d_inode
;
1477 if (inode
->i_nlink
> 1)
1478 goto close_fail
; /* multiple links - don't dump */
1479 if (d_unhashed(file
->f_dentry
))
1482 if (!S_ISREG(inode
->i_mode
))
1486 if (!file
->f_op
->write
)
1488 if (do_truncate(file
->f_dentry
, 0) != 0)
1491 retval
= binfmt
->core_dump(signr
, regs
, file
);
1494 current
->signal
->group_exit_code
|= 0x80;
1496 filp_close(file
, NULL
);
1498 complete_all(&mm
->core_done
);