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/personality.h>
38 #include <linux/binfmts.h>
39 #include <linux/swap.h>
40 #include <linux/utsname.h>
41 #include <linux/module.h>
42 #include <linux/namei.h>
43 #include <linux/proc_fs.h>
44 #include <linux/ptrace.h>
45 #include <linux/mount.h>
46 #include <linux/security.h>
47 #include <linux/syscalls.h>
48 #include <linux/rmap-locking.h>
50 #include <asm/uaccess.h>
51 #include <asm/pgalloc.h>
52 #include <asm/mmu_context.h>
55 #include <linux/kmod.h>
59 char core_pattern
[65] = "core";
60 /* The maximal length of core_pattern is also specified in sysctl.c */
62 static struct linux_binfmt
*formats
;
63 static rwlock_t binfmt_lock
= RW_LOCK_UNLOCKED
;
65 int register_binfmt(struct linux_binfmt
* fmt
)
67 struct linux_binfmt
** tmp
= &formats
;
73 write_lock(&binfmt_lock
);
76 write_unlock(&binfmt_lock
);
83 write_unlock(&binfmt_lock
);
87 EXPORT_SYMBOL(register_binfmt
);
89 int unregister_binfmt(struct linux_binfmt
* fmt
)
91 struct linux_binfmt
** tmp
= &formats
;
93 write_lock(&binfmt_lock
);
97 write_unlock(&binfmt_lock
);
102 write_unlock(&binfmt_lock
);
106 EXPORT_SYMBOL(unregister_binfmt
);
108 static inline void put_binfmt(struct linux_binfmt
* fmt
)
110 module_put(fmt
->module
);
114 * Note that a shared library must be both readable and executable due to
117 * Also note that we take the address to load from from the file itself.
119 asmlinkage
long sys_uselib(const char __user
* library
)
125 nd
.intent
.open
.flags
= FMODE_READ
;
126 error
= __user_walk(library
, LOOKUP_FOLLOW
|LOOKUP_OPEN
, &nd
);
131 if (!S_ISREG(nd
.dentry
->d_inode
->i_mode
))
134 error
= permission(nd
.dentry
->d_inode
, MAY_READ
| MAY_EXEC
, &nd
);
138 file
= dentry_open(nd
.dentry
, nd
.mnt
, O_RDONLY
);
139 error
= PTR_ERR(file
);
145 struct linux_binfmt
* fmt
;
147 read_lock(&binfmt_lock
);
148 for (fmt
= formats
; fmt
; fmt
= fmt
->next
) {
149 if (!fmt
->load_shlib
)
151 if (!try_module_get(fmt
->module
))
153 read_unlock(&binfmt_lock
);
154 error
= fmt
->load_shlib(file
);
155 read_lock(&binfmt_lock
);
157 if (error
!= -ENOEXEC
)
160 read_unlock(&binfmt_lock
);
171 * count() counts the number of strings in array ARGV.
173 static int count(char __user
* __user
* argv
, int max
)
181 if (get_user(p
, argv
))
194 * 'copy_strings()' copies argument/environment strings from user
195 * memory to free pages in kernel mem. These are in a format ready
196 * to be put directly into the top of new user memory.
198 int copy_strings(int argc
,char __user
* __user
* argv
, struct linux_binprm
*bprm
)
200 struct page
*kmapped_page
= NULL
;
209 if (get_user(str
, argv
+argc
) ||
210 !(len
= strnlen_user(str
, bprm
->p
))) {
221 /* XXX: add architecture specific overflow check here. */
226 int offset
, bytes_to_copy
;
229 offset
= pos
% PAGE_SIZE
;
231 page
= bprm
->page
[i
];
234 page
= alloc_page(GFP_HIGHUSER
);
235 bprm
->page
[i
] = page
;
243 if (page
!= kmapped_page
) {
245 kunmap(kmapped_page
);
247 kaddr
= kmap(kmapped_page
);
250 memset(kaddr
, 0, offset
);
251 bytes_to_copy
= PAGE_SIZE
- offset
;
252 if (bytes_to_copy
> len
) {
255 memset(kaddr
+offset
+len
, 0,
256 PAGE_SIZE
-offset
-len
);
258 err
= copy_from_user(kaddr
+offset
, str
, bytes_to_copy
);
264 pos
+= bytes_to_copy
;
265 str
+= bytes_to_copy
;
266 len
-= bytes_to_copy
;
272 kunmap(kmapped_page
);
277 * Like copy_strings, but get argv and its values from kernel memory.
279 int copy_strings_kernel(int argc
,char ** argv
, struct linux_binprm
*bprm
)
282 mm_segment_t oldfs
= get_fs();
284 r
= copy_strings(argc
, (char __user
* __user
*)argv
, bprm
);
289 EXPORT_SYMBOL(copy_strings_kernel
);
293 * This routine is used to map in a page into an address space: needed by
294 * execve() for the initial stack and environment pages.
296 * tsk->mmap_sem is held for writing.
298 void put_dirty_page(struct task_struct
*tsk
, struct page
*page
,
299 unsigned long address
, pgprot_t prot
)
304 struct pte_chain
*pte_chain
;
306 if (page_count(page
) != 1)
307 printk(KERN_ERR
"mem_map disagrees with %p at %08lx\n",
310 pgd
= pgd_offset(tsk
->mm
, address
);
311 pte_chain
= pte_chain_alloc(GFP_KERNEL
);
314 spin_lock(&tsk
->mm
->page_table_lock
);
315 pmd
= pmd_alloc(tsk
->mm
, pgd
, address
);
318 pte
= pte_alloc_map(tsk
->mm
, pmd
, address
);
321 if (!pte_none(*pte
)) {
325 lru_cache_add_active(page
);
326 flush_dcache_page(page
);
327 set_pte(pte
, pte_mkdirty(pte_mkwrite(mk_pte(page
, prot
))));
328 pte_chain
= page_add_rmap(page
, pte
, pte_chain
);
331 spin_unlock(&tsk
->mm
->page_table_lock
);
333 /* no need for flush_tlb */
334 pte_chain_free(pte_chain
);
337 spin_unlock(&tsk
->mm
->page_table_lock
);
340 force_sig(SIGKILL
, tsk
);
341 pte_chain_free(pte_chain
);
345 int setup_arg_pages(struct linux_binprm
*bprm
)
347 unsigned long stack_base
;
348 struct vm_area_struct
*mpnt
;
349 struct mm_struct
*mm
= current
->mm
;
353 #ifdef CONFIG_STACK_GROWSUP
354 /* Move the argument and environment strings to the bottom of the
360 /* Start by shifting all the pages down */
362 for (j
= 0; j
< MAX_ARG_PAGES
; j
++) {
363 struct page
*page
= bprm
->page
[j
];
366 bprm
->page
[i
++] = page
;
369 /* Now move them within their pages */
370 offset
= bprm
->p
% PAGE_SIZE
;
371 to
= kmap(bprm
->page
[0]);
372 for (j
= 1; j
< i
; j
++) {
373 memmove(to
, to
+ offset
, PAGE_SIZE
- offset
);
374 from
= kmap(bprm
->page
[j
]);
375 memcpy(to
+ PAGE_SIZE
- offset
, from
, offset
);
376 kunmap(bprm
->page
[j
- 1]);
379 memmove(to
, to
+ offset
, PAGE_SIZE
- offset
);
380 kunmap(bprm
->page
[j
- 1]);
382 /* Adjust bprm->p to point to the end of the strings. */
383 bprm
->p
= PAGE_SIZE
* i
- offset
;
385 /* Limit stack size to 1GB */
386 stack_base
= current
->rlim
[RLIMIT_STACK
].rlim_max
;
387 if (stack_base
> (1 << 30))
388 stack_base
= 1 << 30;
389 stack_base
= PAGE_ALIGN(STACK_TOP
- stack_base
);
391 mm
->arg_start
= stack_base
;
392 arg_size
= i
<< PAGE_SHIFT
;
394 /* zero pages that were copied above */
395 while (i
< MAX_ARG_PAGES
)
396 bprm
->page
[i
++] = NULL
;
398 stack_base
= STACK_TOP
- MAX_ARG_PAGES
* PAGE_SIZE
;
399 mm
->arg_start
= bprm
->p
+ stack_base
;
400 arg_size
= STACK_TOP
- (PAGE_MASK
& (unsigned long) mm
->arg_start
);
403 bprm
->p
+= stack_base
;
405 bprm
->loader
+= stack_base
;
406 bprm
->exec
+= stack_base
;
408 mpnt
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
412 if (security_vm_enough_memory(arg_size
>> PAGE_SHIFT
)) {
413 kmem_cache_free(vm_area_cachep
, mpnt
);
417 down_write(&mm
->mmap_sem
);
420 #ifdef CONFIG_STACK_GROWSUP
421 mpnt
->vm_start
= stack_base
;
422 mpnt
->vm_end
= PAGE_MASK
&
423 (PAGE_SIZE
- 1 + (unsigned long) bprm
->p
);
425 mpnt
->vm_start
= PAGE_MASK
& (unsigned long) bprm
->p
;
426 mpnt
->vm_end
= STACK_TOP
;
428 mpnt
->vm_page_prot
= protection_map
[VM_STACK_FLAGS
& 0x7];
429 mpnt
->vm_flags
= VM_STACK_FLAGS
;
432 mpnt
->vm_file
= NULL
;
433 INIT_LIST_HEAD(&mpnt
->shared
);
434 mpnt
->vm_private_data
= (void *) 0;
435 insert_vm_struct(mm
, mpnt
);
436 mm
->total_vm
= (mpnt
->vm_end
- mpnt
->vm_start
) >> PAGE_SHIFT
;
439 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
440 struct page
*page
= bprm
->page
[i
];
442 bprm
->page
[i
] = NULL
;
443 put_dirty_page(current
, page
, stack_base
,
446 stack_base
+= PAGE_SIZE
;
448 up_write(&mm
->mmap_sem
);
453 EXPORT_SYMBOL(setup_arg_pages
);
455 #define free_arg_pages(bprm) do { } while (0)
459 static inline void free_arg_pages(struct linux_binprm
*bprm
)
463 for (i
= 0; i
< MAX_ARG_PAGES
; i
++) {
465 __free_page(bprm
->page
[i
]);
466 bprm
->page
[i
] = NULL
;
470 #endif /* CONFIG_MMU */
472 struct file
*open_exec(const char *name
)
478 nd
.intent
.open
.flags
= FMODE_READ
;
479 err
= path_lookup(name
, LOOKUP_FOLLOW
|LOOKUP_OPEN
, &nd
);
483 struct inode
*inode
= nd
.dentry
->d_inode
;
484 file
= ERR_PTR(-EACCES
);
485 if (!(nd
.mnt
->mnt_flags
& MNT_NOEXEC
) &&
486 S_ISREG(inode
->i_mode
)) {
487 int err
= permission(inode
, MAY_EXEC
, &nd
);
488 if (!err
&& !(inode
->i_mode
& 0111))
492 file
= dentry_open(nd
.dentry
, nd
.mnt
, O_RDONLY
);
494 err
= deny_write_access(file
);
509 EXPORT_SYMBOL(open_exec
);
511 int kernel_read(struct file
*file
, unsigned long offset
,
512 char *addr
, unsigned long count
)
520 /* The cast to a user pointer is valid due to the set_fs() */
521 result
= vfs_read(file
, (void __user
*)addr
, count
, &pos
);
526 EXPORT_SYMBOL(kernel_read
);
528 static int exec_mmap(struct mm_struct
*mm
)
530 struct task_struct
*tsk
;
531 struct mm_struct
* old_mm
, *active_mm
;
533 /* Add it to the list of mm's */
534 spin_lock(&mmlist_lock
);
535 list_add(&mm
->mmlist
, &init_mm
.mmlist
);
537 spin_unlock(&mmlist_lock
);
539 /* Notify parent that we're no longer interested in the old VM */
541 old_mm
= current
->mm
;
542 mm_release(tsk
, old_mm
);
545 active_mm
= tsk
->active_mm
;
548 activate_mm(active_mm
, mm
);
551 if (active_mm
!= old_mm
) BUG();
560 * This function makes sure the current process has its own signal table,
561 * so that flush_signal_handlers can later reset the handlers without
562 * disturbing other processes. (Other processes might share the signal
563 * table via the CLONE_SIGHAND option to clone().)
565 static inline int de_thread(struct task_struct
*tsk
)
567 struct signal_struct
*newsig
, *oldsig
= tsk
->signal
;
568 struct sighand_struct
*newsighand
, *oldsighand
= tsk
->sighand
;
569 spinlock_t
*lock
= &oldsighand
->siglock
;
573 * If we don't share sighandlers, then we aren't sharing anything
574 * and we can just re-use it all.
576 if (atomic_read(&oldsighand
->count
) <= 1)
579 newsighand
= kmem_cache_alloc(sighand_cachep
, GFP_KERNEL
);
583 spin_lock_init(&newsighand
->siglock
);
584 atomic_set(&newsighand
->count
, 1);
585 memcpy(newsighand
->action
, oldsighand
->action
, sizeof(newsighand
->action
));
588 * See if we need to allocate a new signal structure
591 if (atomic_read(&oldsig
->count
) > 1) {
592 newsig
= kmem_cache_alloc(signal_cachep
, GFP_KERNEL
);
594 kmem_cache_free(sighand_cachep
, newsighand
);
597 atomic_set(&newsig
->count
, 1);
598 newsig
->group_exit
= 0;
599 newsig
->group_exit_code
= 0;
600 newsig
->group_exit_task
= NULL
;
601 newsig
->group_stop_count
= 0;
602 newsig
->curr_target
= NULL
;
603 init_sigpending(&newsig
->shared_pending
);
606 if (thread_group_empty(current
))
607 goto no_thread_group
;
610 * Kill all other threads in the thread group.
611 * We must hold tasklist_lock to call zap_other_threads.
613 read_lock(&tasklist_lock
);
615 if (oldsig
->group_exit
) {
617 * Another group action in progress, just
618 * return so that the signal is processed.
620 spin_unlock_irq(lock
);
621 read_unlock(&tasklist_lock
);
622 kmem_cache_free(sighand_cachep
, newsighand
);
624 kmem_cache_free(signal_cachep
, newsig
);
627 oldsig
->group_exit
= 1;
628 zap_other_threads(current
);
629 read_unlock(&tasklist_lock
);
632 * Account for the thread group leader hanging around:
635 if (current
->pid
== current
->tgid
)
637 while (atomic_read(&oldsig
->count
) > count
) {
638 oldsig
->group_exit_task
= current
;
639 oldsig
->notify_count
= count
;
640 __set_current_state(TASK_UNINTERRUPTIBLE
);
641 spin_unlock_irq(lock
);
645 spin_unlock_irq(lock
);
648 * At this point all other threads have exited, all we have to
649 * do is to wait for the thread group leader to become inactive,
650 * and to assume its PID:
652 if (current
->pid
!= current
->tgid
) {
653 struct task_struct
*leader
= current
->group_leader
, *parent
;
654 struct dentry
*proc_dentry1
, *proc_dentry2
;
655 unsigned long state
, ptrace
;
658 * Wait for the thread group leader to be a zombie.
659 * It should already be zombie at this point, most
662 while (leader
->state
!= TASK_ZOMBIE
)
665 spin_lock(&leader
->proc_lock
);
666 spin_lock(¤t
->proc_lock
);
667 proc_dentry1
= proc_pid_unhash(current
);
668 proc_dentry2
= proc_pid_unhash(leader
);
669 write_lock_irq(&tasklist_lock
);
671 if (leader
->tgid
!= current
->tgid
)
673 if (current
->pid
== current
->tgid
)
676 * An exec() starts a new thread group with the
677 * TGID of the previous thread group. Rehash the
678 * two threads with a switched PID, and release
679 * the former thread group leader:
681 ptrace
= leader
->ptrace
;
682 parent
= leader
->parent
;
684 ptrace_unlink(current
);
685 ptrace_unlink(leader
);
686 remove_parent(current
);
687 remove_parent(leader
);
689 switch_exec_pids(leader
, current
);
691 current
->parent
= current
->real_parent
= leader
->real_parent
;
692 leader
->parent
= leader
->real_parent
= child_reaper
;
693 current
->group_leader
= current
;
694 leader
->group_leader
= leader
;
696 add_parent(current
, current
->parent
);
697 add_parent(leader
, leader
->parent
);
699 current
->ptrace
= ptrace
;
700 __ptrace_link(current
, parent
);
703 list_del(¤t
->tasks
);
704 list_add_tail(¤t
->tasks
, &init_task
.tasks
);
705 current
->exit_signal
= SIGCHLD
;
706 state
= leader
->state
;
708 write_unlock_irq(&tasklist_lock
);
709 spin_unlock(&leader
->proc_lock
);
710 spin_unlock(¤t
->proc_lock
);
711 proc_pid_flush(proc_dentry1
);
712 proc_pid_flush(proc_dentry2
);
714 if (state
!= TASK_ZOMBIE
)
716 release_task(leader
);
721 write_lock_irq(&tasklist_lock
);
722 spin_lock(&oldsighand
->siglock
);
723 spin_lock(&newsighand
->siglock
);
725 if (current
== oldsig
->curr_target
)
726 oldsig
->curr_target
= next_thread(current
);
728 current
->signal
= newsig
;
729 current
->sighand
= newsighand
;
730 init_sigpending(¤t
->pending
);
733 spin_unlock(&newsighand
->siglock
);
734 spin_unlock(&oldsighand
->siglock
);
735 write_unlock_irq(&tasklist_lock
);
737 if (newsig
&& atomic_dec_and_test(&oldsig
->count
))
738 kmem_cache_free(signal_cachep
, oldsig
);
740 if (atomic_dec_and_test(&oldsighand
->count
))
741 kmem_cache_free(sighand_cachep
, oldsighand
);
743 if (!thread_group_empty(current
))
745 if (current
->tgid
!= current
->pid
)
751 * These functions flushes out all traces of the currently running executable
752 * so that a new one can be started
755 static inline void flush_old_files(struct files_struct
* files
)
759 spin_lock(&files
->file_lock
);
761 unsigned long set
, i
;
765 if (i
>= files
->max_fds
|| i
>= files
->max_fdset
)
767 set
= files
->close_on_exec
->fds_bits
[j
];
770 files
->close_on_exec
->fds_bits
[j
] = 0;
771 spin_unlock(&files
->file_lock
);
772 for ( ; set
; i
++,set
>>= 1) {
777 spin_lock(&files
->file_lock
);
780 spin_unlock(&files
->file_lock
);
783 int flush_old_exec(struct linux_binprm
* bprm
)
787 struct files_struct
*files
;
790 * Make sure we have a private signal table and that
791 * we are unassociated from the previous thread group.
793 retval
= de_thread(current
);
798 * Make sure we have private file handles. Ask the
799 * fork helper to do the work for us and the exit
800 * helper to do the cleanup of the old one.
802 files
= current
->files
; /* refcounted so safe to hold */
803 retval
= unshare_files();
807 * Release all of the old mmap stuff
809 retval
= exec_mmap(bprm
->mm
);
813 bprm
->mm
= NULL
; /* We're using it now */
815 /* This is the point of no return */
817 put_files_struct(files
);
819 current
->sas_ss_sp
= current
->sas_ss_size
= 0;
821 if (current
->euid
== current
->uid
&& current
->egid
== current
->gid
)
822 current
->mm
->dumpable
= 1;
823 name
= bprm
->filename
;
824 for (i
=0; (ch
= *(name
++)) != '\0';) {
829 current
->comm
[i
++] = ch
;
831 current
->comm
[i
] = '\0';
835 if (bprm
->e_uid
!= current
->euid
|| bprm
->e_gid
!= current
->egid
||
836 permission(bprm
->file
->f_dentry
->d_inode
,MAY_READ
, NULL
))
837 current
->mm
->dumpable
= 0;
839 /* An exec changes our domain. We are no longer part of the thread
842 current
->self_exec_id
++;
844 flush_signal_handlers(current
, 0);
845 flush_old_files(current
->files
);
846 exit_itimers(current
);
851 put_files_struct(current
->files
);
852 current
->files
= files
;
857 EXPORT_SYMBOL(flush_old_exec
);
860 * We mustn't allow tracing of suid binaries, unless
861 * the tracer has the capability to trace anything..
863 static inline int must_not_trace_exec(struct task_struct
* p
)
865 return (p
->ptrace
& PT_PTRACED
) && !(p
->ptrace
& PT_PTRACE_CAP
);
869 * Fill the binprm structure from the inode.
870 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
872 int prepare_binprm(struct linux_binprm
*bprm
)
875 struct inode
* inode
= bprm
->file
->f_dentry
->d_inode
;
878 mode
= inode
->i_mode
;
880 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
881 * vfs_permission lets a non-executable through
883 if (!(mode
& 0111)) /* with at least _one_ execute bit set */
885 if (bprm
->file
->f_op
== NULL
)
888 bprm
->e_uid
= current
->euid
;
889 bprm
->e_gid
= current
->egid
;
891 if(!(bprm
->file
->f_vfsmnt
->mnt_flags
& MNT_NOSUID
)) {
894 bprm
->e_uid
= inode
->i_uid
;
898 * If setgid is set but no group execute bit then this
899 * is a candidate for mandatory locking, not a setgid
902 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
903 bprm
->e_gid
= inode
->i_gid
;
906 /* fill in binprm security blob */
907 retval
= security_bprm_set(bprm
);
911 memset(bprm
->buf
,0,BINPRM_BUF_SIZE
);
912 return kernel_read(bprm
->file
,0,bprm
->buf
,BINPRM_BUF_SIZE
);
915 EXPORT_SYMBOL(prepare_binprm
);
918 * This function is used to produce the new IDs and capabilities
919 * from the old ones and the file's capabilities.
921 * The formula used for evolving capabilities is:
924 * (***) pP' = (fP & X) | (fI & pI)
925 * pE' = pP' & fE [NB. fE is 0 or ~0]
927 * I=Inheritable, P=Permitted, E=Effective // p=process, f=file
928 * ' indicates post-exec(), and X is the global 'cap_bset'.
932 void compute_creds(struct linux_binprm
*bprm
)
935 if (bprm
->e_uid
!= current
->uid
|| bprm
->e_gid
!= current
->gid
) {
936 current
->mm
->dumpable
= 0;
938 if (must_not_trace_exec(current
)
939 || atomic_read(¤t
->fs
->count
) > 1
940 || atomic_read(¤t
->files
->count
) > 1
941 || atomic_read(¤t
->sighand
->count
) > 1) {
942 if(!capable(CAP_SETUID
)) {
943 bprm
->e_uid
= current
->uid
;
944 bprm
->e_gid
= current
->gid
;
949 current
->suid
= current
->euid
= current
->fsuid
= bprm
->e_uid
;
950 current
->sgid
= current
->egid
= current
->fsgid
= bprm
->e_gid
;
952 task_unlock(current
);
954 security_bprm_compute_creds(bprm
);
957 EXPORT_SYMBOL(compute_creds
);
959 void remove_arg_zero(struct linux_binprm
*bprm
)
962 unsigned long offset
;
966 offset
= bprm
->p
% PAGE_SIZE
;
969 while (bprm
->p
++, *(kaddr
+offset
++)) {
970 if (offset
!= PAGE_SIZE
)
973 kunmap_atomic(kaddr
, KM_USER0
);
975 page
= bprm
->page
[bprm
->p
/PAGE_SIZE
];
976 kaddr
= kmap_atomic(page
, KM_USER0
);
978 kunmap_atomic(kaddr
, KM_USER0
);
983 EXPORT_SYMBOL(remove_arg_zero
);
986 * cycle the list of binary formats handler, until one recognizes the image
988 int search_binary_handler(struct linux_binprm
*bprm
,struct pt_regs
*regs
)
991 struct linux_binfmt
*fmt
;
993 /* handle /sbin/loader.. */
995 struct exec
* eh
= (struct exec
*) bprm
->buf
;
997 if (!bprm
->loader
&& eh
->fh
.f_magic
== 0x183 &&
998 (eh
->fh
.f_flags
& 0x3000) == 0x3000)
1001 unsigned long loader
;
1003 allow_write_access(bprm
->file
);
1007 loader
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1009 file
= open_exec("/sbin/loader");
1010 retval
= PTR_ERR(file
);
1014 /* Remember if the application is TASO. */
1015 bprm
->sh_bang
= eh
->ah
.entry
< 0x100000000;
1018 bprm
->loader
= loader
;
1019 retval
= prepare_binprm(bprm
);
1022 /* should call search_binary_handler recursively here,
1023 but it does not matter */
1027 retval
= security_bprm_check(bprm
);
1031 /* kernel module loader fixup */
1032 /* so we don't try to load run modprobe in kernel space. */
1034 for (try=0; try<2; try++) {
1035 read_lock(&binfmt_lock
);
1036 for (fmt
= formats
; fmt
; fmt
= fmt
->next
) {
1037 int (*fn
)(struct linux_binprm
*, struct pt_regs
*) = fmt
->load_binary
;
1040 if (!try_module_get(fmt
->module
))
1042 read_unlock(&binfmt_lock
);
1043 retval
= fn(bprm
, regs
);
1046 allow_write_access(bprm
->file
);
1050 current
->did_exec
= 1;
1053 read_lock(&binfmt_lock
);
1055 if (retval
!= -ENOEXEC
|| bprm
->mm
== NULL
)
1058 read_unlock(&binfmt_lock
);
1062 read_unlock(&binfmt_lock
);
1063 if (retval
!= -ENOEXEC
|| bprm
->mm
== NULL
) {
1067 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1068 if (printable(bprm
->buf
[0]) &&
1069 printable(bprm
->buf
[1]) &&
1070 printable(bprm
->buf
[2]) &&
1071 printable(bprm
->buf
[3]))
1072 break; /* -ENOEXEC */
1073 request_module("binfmt-%04x", *(unsigned short *)(&bprm
->buf
[2]));
1080 EXPORT_SYMBOL(search_binary_handler
);
1083 * sys_execve() executes a new program.
1085 int do_execve(char * filename
,
1086 char __user
*__user
*argv
,
1087 char __user
*__user
*envp
,
1088 struct pt_regs
* regs
)
1090 struct linux_binprm bprm
;
1095 sched_balance_exec();
1097 file
= open_exec(filename
);
1099 retval
= PTR_ERR(file
);
1103 bprm
.p
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1104 memset(bprm
.page
, 0, MAX_ARG_PAGES
*sizeof(bprm
.page
[0]));
1107 bprm
.filename
= filename
;
1108 bprm
.interp
= filename
;
1112 bprm
.security
= NULL
;
1113 bprm
.mm
= mm_alloc();
1118 retval
= init_new_context(current
, bprm
.mm
);
1122 bprm
.argc
= count(argv
, bprm
.p
/ sizeof(void *));
1123 if ((retval
= bprm
.argc
) < 0)
1126 bprm
.envc
= count(envp
, bprm
.p
/ sizeof(void *));
1127 if ((retval
= bprm
.envc
) < 0)
1130 retval
= security_bprm_alloc(&bprm
);
1134 retval
= prepare_binprm(&bprm
);
1138 retval
= copy_strings_kernel(1, &bprm
.filename
, &bprm
);
1143 retval
= copy_strings(bprm
.envc
, envp
, &bprm
);
1147 retval
= copy_strings(bprm
.argc
, argv
, &bprm
);
1151 retval
= search_binary_handler(&bprm
,regs
);
1153 free_arg_pages(&bprm
);
1155 /* execve success */
1156 security_bprm_free(&bprm
);
1161 /* Something went wrong, return the inode and free the argument pages*/
1162 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
1163 struct page
* page
= bprm
.page
[i
];
1169 security_bprm_free(&bprm
);
1177 allow_write_access(bprm
.file
);
1183 EXPORT_SYMBOL(do_execve
);
1185 int set_binfmt(struct linux_binfmt
*new)
1187 struct linux_binfmt
*old
= current
->binfmt
;
1190 if (!try_module_get(new->module
))
1193 current
->binfmt
= new;
1195 module_put(old
->module
);
1199 EXPORT_SYMBOL(set_binfmt
);
1201 #define CORENAME_MAX_SIZE 64
1203 /* format_corename will inspect the pattern parameter, and output a
1204 * name into corename, which must have space for at least
1205 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1207 void format_corename(char *corename
, const char *pattern
, long signr
)
1209 const char *pat_ptr
= pattern
;
1210 char *out_ptr
= corename
;
1211 char *const out_end
= corename
+ CORENAME_MAX_SIZE
;
1213 int pid_in_pattern
= 0;
1215 /* Repeat as long as we have more pattern to process and more output
1218 if (*pat_ptr
!= '%') {
1219 if (out_ptr
== out_end
)
1221 *out_ptr
++ = *pat_ptr
++;
1223 switch (*++pat_ptr
) {
1226 /* Double percent, output one percent */
1228 if (out_ptr
== out_end
)
1235 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1236 "%d", current
->tgid
);
1237 if (rc
> out_end
- out_ptr
)
1243 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1244 "%d", current
->uid
);
1245 if (rc
> out_end
- out_ptr
)
1251 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1252 "%d", current
->gid
);
1253 if (rc
> out_end
- out_ptr
)
1257 /* signal that caused the coredump */
1259 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1261 if (rc
> out_end
- out_ptr
)
1265 /* UNIX time of coredump */
1268 do_gettimeofday(&tv
);
1269 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1271 if (rc
> out_end
- out_ptr
)
1278 down_read(&uts_sem
);
1279 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1280 "%s", system_utsname
.nodename
);
1282 if (rc
> out_end
- out_ptr
)
1288 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1289 "%s", current
->comm
);
1290 if (rc
> out_end
- out_ptr
)
1300 /* Backward compatibility with core_uses_pid:
1302 * If core_pattern does not include a %p (as is the default)
1303 * and core_uses_pid is set, then .%pid will be appended to
1306 && (core_uses_pid
|| atomic_read(¤t
->mm
->mm_users
) != 1)) {
1307 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1308 ".%d", current
->tgid
);
1309 if (rc
> out_end
- out_ptr
)
1317 static void zap_threads (struct mm_struct
*mm
)
1319 struct task_struct
*g
, *p
;
1320 struct task_struct
*tsk
= current
;
1321 struct completion
*vfork_done
= tsk
->vfork_done
;
1324 * Make sure nobody is waiting for us to release the VM,
1325 * otherwise we can deadlock when we wait on each other
1328 tsk
->vfork_done
= NULL
;
1329 complete(vfork_done
);
1332 read_lock(&tasklist_lock
);
1334 if (mm
== p
->mm
&& p
!= tsk
) {
1335 force_sig_specific(SIGKILL
, p
);
1338 while_each_thread(g
,p
);
1340 read_unlock(&tasklist_lock
);
1343 static void coredump_wait(struct mm_struct
*mm
)
1345 DECLARE_COMPLETION(startup_done
);
1347 mm
->core_waiters
++; /* let other threads block */
1348 mm
->core_startup_done
= &startup_done
;
1350 /* give other threads a chance to run: */
1354 if (--mm
->core_waiters
) {
1355 up_write(&mm
->mmap_sem
);
1356 wait_for_completion(&startup_done
);
1358 up_write(&mm
->mmap_sem
);
1359 BUG_ON(mm
->core_waiters
);
1362 int do_coredump(long signr
, int exit_code
, struct pt_regs
* regs
)
1364 char corename
[CORENAME_MAX_SIZE
+ 1];
1365 struct mm_struct
*mm
= current
->mm
;
1366 struct linux_binfmt
* binfmt
;
1367 struct inode
* inode
;
1372 binfmt
= current
->binfmt
;
1373 if (!binfmt
|| !binfmt
->core_dump
)
1375 down_write(&mm
->mmap_sem
);
1376 if (!mm
->dumpable
) {
1377 up_write(&mm
->mmap_sem
);
1381 init_completion(&mm
->core_done
);
1382 current
->signal
->group_exit
= 1;
1383 current
->signal
->group_exit_code
= exit_code
;
1386 if (current
->rlim
[RLIMIT_CORE
].rlim_cur
< binfmt
->min_coredump
)
1389 format_corename(corename
, core_pattern
, signr
);
1390 file
= filp_open(corename
, O_CREAT
| 2 | O_NOFOLLOW
| O_LARGEFILE
, 0600);
1393 inode
= file
->f_dentry
->d_inode
;
1394 if (inode
->i_nlink
> 1)
1395 goto close_fail
; /* multiple links - don't dump */
1396 if (d_unhashed(file
->f_dentry
))
1399 if (!S_ISREG(inode
->i_mode
))
1403 if (!file
->f_op
->write
)
1405 if (do_truncate(file
->f_dentry
, 0) != 0)
1408 retval
= binfmt
->core_dump(signr
, regs
, file
);
1410 current
->signal
->group_exit_code
|= 0x80;
1412 filp_close(file
, NULL
);
1414 complete_all(&mm
->core_done
);