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 int suid_dumpable
= 0;
63 EXPORT_SYMBOL(suid_dumpable
);
64 /* The maximal length of core_pattern is also specified in sysctl.c */
66 static struct linux_binfmt
*formats
;
67 static DEFINE_RWLOCK(binfmt_lock
);
69 int register_binfmt(struct linux_binfmt
* fmt
)
71 struct linux_binfmt
** tmp
= &formats
;
77 write_lock(&binfmt_lock
);
80 write_unlock(&binfmt_lock
);
87 write_unlock(&binfmt_lock
);
91 EXPORT_SYMBOL(register_binfmt
);
93 int unregister_binfmt(struct linux_binfmt
* fmt
)
95 struct linux_binfmt
** tmp
= &formats
;
97 write_lock(&binfmt_lock
);
101 write_unlock(&binfmt_lock
);
106 write_unlock(&binfmt_lock
);
110 EXPORT_SYMBOL(unregister_binfmt
);
112 static inline void put_binfmt(struct linux_binfmt
* fmt
)
114 module_put(fmt
->module
);
118 * Note that a shared library must be both readable and executable due to
121 * Also note that we take the address to load from from the file itself.
123 asmlinkage
long sys_uselib(const char __user
* library
)
129 error
= __user_path_lookup_open(library
, LOOKUP_FOLLOW
, &nd
, FMODE_READ
);
134 if (!S_ISREG(nd
.dentry
->d_inode
->i_mode
))
137 error
= permission(nd
.dentry
->d_inode
, MAY_READ
| MAY_EXEC
, &nd
);
141 file
= nameidata_to_filp(&nd
, O_RDONLY
);
142 error
= PTR_ERR(file
);
148 struct linux_binfmt
* fmt
;
150 read_lock(&binfmt_lock
);
151 for (fmt
= formats
; fmt
; fmt
= fmt
->next
) {
152 if (!fmt
->load_shlib
)
154 if (!try_module_get(fmt
->module
))
156 read_unlock(&binfmt_lock
);
157 error
= fmt
->load_shlib(file
);
158 read_lock(&binfmt_lock
);
160 if (error
!= -ENOEXEC
)
163 read_unlock(&binfmt_lock
);
169 release_open_intent(&nd
);
175 * count() counts the number of strings in array ARGV.
177 static int count(char __user
* __user
* argv
, int max
)
185 if (get_user(p
, argv
))
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.
203 static int copy_strings(int argc
, char __user
* __user
* argv
,
204 struct linux_binprm
*bprm
)
206 struct page
*kmapped_page
= NULL
;
215 if (get_user(str
, argv
+argc
) ||
216 !(len
= strnlen_user(str
, bprm
->p
))) {
227 /* XXX: add architecture specific overflow check here. */
232 int offset
, bytes_to_copy
;
235 offset
= pos
% PAGE_SIZE
;
237 page
= bprm
->page
[i
];
240 page
= alloc_page(GFP_HIGHUSER
);
241 bprm
->page
[i
] = page
;
249 if (page
!= kmapped_page
) {
251 kunmap(kmapped_page
);
253 kaddr
= kmap(kmapped_page
);
256 memset(kaddr
, 0, offset
);
257 bytes_to_copy
= PAGE_SIZE
- offset
;
258 if (bytes_to_copy
> len
) {
261 memset(kaddr
+offset
+len
, 0,
262 PAGE_SIZE
-offset
-len
);
264 err
= copy_from_user(kaddr
+offset
, str
, bytes_to_copy
);
270 pos
+= bytes_to_copy
;
271 str
+= bytes_to_copy
;
272 len
-= bytes_to_copy
;
278 kunmap(kmapped_page
);
283 * Like copy_strings, but get argv and its values from kernel memory.
285 int copy_strings_kernel(int argc
,char ** argv
, struct linux_binprm
*bprm
)
288 mm_segment_t oldfs
= get_fs();
290 r
= copy_strings(argc
, (char __user
* __user
*)argv
, bprm
);
295 EXPORT_SYMBOL(copy_strings_kernel
);
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.
302 * vma->vm_mm->mmap_sem is held for writing.
304 void install_arg_page(struct vm_area_struct
*vma
,
305 struct page
*page
, unsigned long address
)
307 struct mm_struct
*mm
= vma
->vm_mm
;
314 if (unlikely(anon_vma_prepare(vma
)))
317 flush_dcache_page(page
);
318 pgd
= pgd_offset(mm
, address
);
319 pud
= pud_alloc(mm
, pgd
, address
);
322 pmd
= pmd_alloc(mm
, pud
, address
);
325 pte
= pte_alloc_map_lock(mm
, pmd
, address
, &ptl
);
328 if (!pte_none(*pte
)) {
329 pte_unmap_unlock(pte
, ptl
);
332 inc_mm_counter(mm
, anon_rss
);
333 lru_cache_add_active(page
);
334 set_pte_at(mm
, address
, pte
, pte_mkdirty(pte_mkwrite(mk_pte(
335 page
, vma
->vm_page_prot
))));
336 page_add_anon_rmap(page
, vma
, address
);
337 pte_unmap_unlock(pte
, ptl
);
339 /* no need for flush_tlb */
343 force_sig(SIGKILL
, current
);
346 #define EXTRA_STACK_VM_PAGES 20 /* random */
348 int setup_arg_pages(struct linux_binprm
*bprm
,
349 unsigned long stack_top
,
350 int executable_stack
)
352 unsigned long stack_base
;
353 struct vm_area_struct
*mpnt
;
354 struct mm_struct
*mm
= current
->mm
;
358 #ifdef CONFIG_STACK_GROWSUP
359 /* Move the argument and environment strings to the bottom of the
365 /* Start by shifting all the pages down */
367 for (j
= 0; j
< MAX_ARG_PAGES
; j
++) {
368 struct page
*page
= bprm
->page
[j
];
371 bprm
->page
[i
++] = page
;
374 /* Now move them within their pages */
375 offset
= bprm
->p
% PAGE_SIZE
;
376 to
= kmap(bprm
->page
[0]);
377 for (j
= 1; j
< i
; j
++) {
378 memmove(to
, to
+ offset
, PAGE_SIZE
- offset
);
379 from
= kmap(bprm
->page
[j
]);
380 memcpy(to
+ PAGE_SIZE
- offset
, from
, offset
);
381 kunmap(bprm
->page
[j
- 1]);
384 memmove(to
, to
+ offset
, PAGE_SIZE
- offset
);
385 kunmap(bprm
->page
[j
- 1]);
387 /* Limit stack size to 1GB */
388 stack_base
= current
->signal
->rlim
[RLIMIT_STACK
].rlim_max
;
389 if (stack_base
> (1 << 30))
390 stack_base
= 1 << 30;
391 stack_base
= PAGE_ALIGN(stack_top
- stack_base
);
393 /* Adjust bprm->p to point to the end of the strings. */
394 bprm
->p
= stack_base
+ PAGE_SIZE
* i
- offset
;
396 mm
->arg_start
= stack_base
;
397 arg_size
= i
<< PAGE_SHIFT
;
399 /* zero pages that were copied above */
400 while (i
< MAX_ARG_PAGES
)
401 bprm
->page
[i
++] = NULL
;
403 stack_base
= arch_align_stack(stack_top
- MAX_ARG_PAGES
*PAGE_SIZE
);
404 stack_base
= PAGE_ALIGN(stack_base
);
405 bprm
->p
+= stack_base
;
406 mm
->arg_start
= bprm
->p
;
407 arg_size
= stack_top
- (PAGE_MASK
& (unsigned long) mm
->arg_start
);
410 arg_size
+= EXTRA_STACK_VM_PAGES
* PAGE_SIZE
;
413 bprm
->loader
+= stack_base
;
414 bprm
->exec
+= stack_base
;
416 mpnt
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
420 memset(mpnt
, 0, sizeof(*mpnt
));
422 down_write(&mm
->mmap_sem
);
425 #ifdef CONFIG_STACK_GROWSUP
426 mpnt
->vm_start
= stack_base
;
427 mpnt
->vm_end
= stack_base
+ arg_size
;
429 mpnt
->vm_end
= stack_top
;
430 mpnt
->vm_start
= mpnt
->vm_end
- arg_size
;
432 /* Adjust stack execute permissions; explicitly enable
433 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
434 * and leave alone (arch default) otherwise. */
435 if (unlikely(executable_stack
== EXSTACK_ENABLE_X
))
436 mpnt
->vm_flags
= VM_STACK_FLAGS
| VM_EXEC
;
437 else if (executable_stack
== EXSTACK_DISABLE_X
)
438 mpnt
->vm_flags
= VM_STACK_FLAGS
& ~VM_EXEC
;
440 mpnt
->vm_flags
= VM_STACK_FLAGS
;
441 mpnt
->vm_flags
|= mm
->def_flags
;
442 mpnt
->vm_page_prot
= protection_map
[mpnt
->vm_flags
& 0x7];
443 if ((ret
= insert_vm_struct(mm
, mpnt
))) {
444 up_write(&mm
->mmap_sem
);
445 kmem_cache_free(vm_area_cachep
, mpnt
);
448 mm
->stack_vm
= mm
->total_vm
= vma_pages(mpnt
);
451 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
452 struct page
*page
= bprm
->page
[i
];
454 bprm
->page
[i
] = NULL
;
455 install_arg_page(mpnt
, page
, stack_base
);
457 stack_base
+= PAGE_SIZE
;
459 up_write(&mm
->mmap_sem
);
464 EXPORT_SYMBOL(setup_arg_pages
);
466 #define free_arg_pages(bprm) do { } while (0)
470 static inline void free_arg_pages(struct linux_binprm
*bprm
)
474 for (i
= 0; i
< MAX_ARG_PAGES
; i
++) {
476 __free_page(bprm
->page
[i
]);
477 bprm
->page
[i
] = NULL
;
481 #endif /* CONFIG_MMU */
483 struct file
*open_exec(const char *name
)
489 err
= path_lookup_open(name
, LOOKUP_FOLLOW
, &nd
, FMODE_READ
);
493 struct inode
*inode
= nd
.dentry
->d_inode
;
494 file
= ERR_PTR(-EACCES
);
495 if (!(nd
.mnt
->mnt_flags
& MNT_NOEXEC
) &&
496 S_ISREG(inode
->i_mode
)) {
497 int err
= permission(inode
, MAY_EXEC
, &nd
);
498 if (!err
&& !(inode
->i_mode
& 0111))
502 file
= nameidata_to_filp(&nd
, O_RDONLY
);
504 err
= deny_write_access(file
);
514 release_open_intent(&nd
);
520 EXPORT_SYMBOL(open_exec
);
522 int kernel_read(struct file
*file
, unsigned long offset
,
523 char *addr
, unsigned long count
)
531 /* The cast to a user pointer is valid due to the set_fs() */
532 result
= vfs_read(file
, (void __user
*)addr
, count
, &pos
);
537 EXPORT_SYMBOL(kernel_read
);
539 static int exec_mmap(struct mm_struct
*mm
)
541 struct task_struct
*tsk
;
542 struct mm_struct
* old_mm
, *active_mm
;
544 /* Notify parent that we're no longer interested in the old VM */
546 old_mm
= current
->mm
;
547 mm_release(tsk
, old_mm
);
551 * Make sure that if there is a core dump in progress
552 * for the old mm, we get out and die instead of going
553 * through with the exec. We must hold mmap_sem around
554 * checking core_waiters and changing tsk->mm. The
555 * core-inducing thread will increment core_waiters for
556 * each thread whose ->mm == old_mm.
558 down_read(&old_mm
->mmap_sem
);
559 if (unlikely(old_mm
->core_waiters
)) {
560 up_read(&old_mm
->mmap_sem
);
565 active_mm
= tsk
->active_mm
;
568 activate_mm(active_mm
, mm
);
570 arch_pick_mmap_layout(mm
);
572 up_read(&old_mm
->mmap_sem
);
573 if (active_mm
!= old_mm
) BUG();
582 * This function makes sure the current process has its own signal table,
583 * so that flush_signal_handlers can later reset the handlers without
584 * disturbing other processes. (Other processes might share the signal
585 * table via the CLONE_SIGHAND option to clone().)
587 static inline int de_thread(struct task_struct
*tsk
)
589 struct signal_struct
*sig
= tsk
->signal
;
590 struct sighand_struct
*newsighand
, *oldsighand
= tsk
->sighand
;
591 spinlock_t
*lock
= &oldsighand
->siglock
;
595 * If we don't share sighandlers, then we aren't sharing anything
596 * and we can just re-use it all.
598 if (atomic_read(&oldsighand
->count
) <= 1) {
599 BUG_ON(atomic_read(&sig
->count
) != 1);
604 newsighand
= kmem_cache_alloc(sighand_cachep
, GFP_KERNEL
);
608 if (thread_group_empty(current
))
609 goto no_thread_group
;
612 * Kill all other threads in the thread group.
613 * We must hold tasklist_lock to call zap_other_threads.
615 read_lock(&tasklist_lock
);
617 if (sig
->flags
& SIGNAL_GROUP_EXIT
) {
619 * Another group action in progress, just
620 * return so that the signal is processed.
622 spin_unlock_irq(lock
);
623 read_unlock(&tasklist_lock
);
624 kmem_cache_free(sighand_cachep
, newsighand
);
627 zap_other_threads(current
);
628 read_unlock(&tasklist_lock
);
631 * Account for the thread group leader hanging around:
634 if (!thread_group_leader(current
)) {
637 * The SIGALRM timer survives the exec, but needs to point
638 * at us as the new group leader now. We have a race with
639 * a timer firing now getting the old leader, so we need to
640 * synchronize with any firing (by calling del_timer_sync)
641 * before we can safely let the old group leader die.
643 sig
->real_timer
.data
= (unsigned long)current
;
644 if (del_timer_sync(&sig
->real_timer
))
645 add_timer(&sig
->real_timer
);
647 while (atomic_read(&sig
->count
) > count
) {
648 sig
->group_exit_task
= current
;
649 sig
->notify_count
= count
;
650 __set_current_state(TASK_UNINTERRUPTIBLE
);
651 spin_unlock_irq(lock
);
655 sig
->group_exit_task
= NULL
;
656 sig
->notify_count
= 0;
657 sig
->real_timer
.data
= (unsigned long)current
;
658 spin_unlock_irq(lock
);
661 * At this point all other threads have exited, all we have to
662 * do is to wait for the thread group leader to become inactive,
663 * and to assume its PID:
665 if (!thread_group_leader(current
)) {
666 struct task_struct
*leader
= current
->group_leader
, *parent
;
667 struct dentry
*proc_dentry1
, *proc_dentry2
;
668 unsigned long exit_state
, ptrace
;
671 * Wait for the thread group leader to be a zombie.
672 * It should already be zombie at this point, most
675 while (leader
->exit_state
!= EXIT_ZOMBIE
)
678 spin_lock(&leader
->proc_lock
);
679 spin_lock(¤t
->proc_lock
);
680 proc_dentry1
= proc_pid_unhash(current
);
681 proc_dentry2
= proc_pid_unhash(leader
);
682 write_lock_irq(&tasklist_lock
);
684 BUG_ON(leader
->tgid
!= current
->tgid
);
685 BUG_ON(current
->pid
== current
->tgid
);
687 * An exec() starts a new thread group with the
688 * TGID of the previous thread group. Rehash the
689 * two threads with a switched PID, and release
690 * the former thread group leader:
692 ptrace
= leader
->ptrace
;
693 parent
= leader
->parent
;
694 if (unlikely(ptrace
) && unlikely(parent
== current
)) {
696 * Joker was ptracing his own group leader,
697 * and now he wants to be his own parent!
698 * We can't have that.
703 ptrace_unlink(current
);
704 ptrace_unlink(leader
);
705 remove_parent(current
);
706 remove_parent(leader
);
708 switch_exec_pids(leader
, current
);
710 current
->parent
= current
->real_parent
= leader
->real_parent
;
711 leader
->parent
= leader
->real_parent
= child_reaper
;
712 current
->group_leader
= current
;
713 leader
->group_leader
= leader
;
715 add_parent(current
, current
->parent
);
716 add_parent(leader
, leader
->parent
);
718 current
->ptrace
= ptrace
;
719 __ptrace_link(current
, parent
);
722 list_del(¤t
->tasks
);
723 list_add_tail(¤t
->tasks
, &init_task
.tasks
);
724 current
->exit_signal
= SIGCHLD
;
725 exit_state
= leader
->exit_state
;
727 write_unlock_irq(&tasklist_lock
);
728 spin_unlock(&leader
->proc_lock
);
729 spin_unlock(¤t
->proc_lock
);
730 proc_pid_flush(proc_dentry1
);
731 proc_pid_flush(proc_dentry2
);
733 BUG_ON(exit_state
!= EXIT_ZOMBIE
);
734 release_task(leader
);
738 * There may be one thread left which is just exiting,
739 * but it's safe to stop telling the group to kill themselves.
744 BUG_ON(atomic_read(&sig
->count
) != 1);
747 if (atomic_read(&oldsighand
->count
) == 1) {
749 * Now that we nuked the rest of the thread group,
750 * it turns out we are not sharing sighand any more either.
751 * So we can just keep it.
753 kmem_cache_free(sighand_cachep
, newsighand
);
756 * Move our state over to newsighand and switch it in.
758 spin_lock_init(&newsighand
->siglock
);
759 atomic_set(&newsighand
->count
, 1);
760 memcpy(newsighand
->action
, oldsighand
->action
,
761 sizeof(newsighand
->action
));
763 write_lock_irq(&tasklist_lock
);
764 spin_lock(&oldsighand
->siglock
);
765 spin_lock(&newsighand
->siglock
);
767 current
->sighand
= newsighand
;
770 spin_unlock(&newsighand
->siglock
);
771 spin_unlock(&oldsighand
->siglock
);
772 write_unlock_irq(&tasklist_lock
);
774 if (atomic_dec_and_test(&oldsighand
->count
))
775 kmem_cache_free(sighand_cachep
, oldsighand
);
778 BUG_ON(!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
)
792 spin_lock(&files
->file_lock
);
794 unsigned long set
, i
;
798 fdt
= files_fdtable(files
);
799 if (i
>= fdt
->max_fds
|| i
>= fdt
->max_fdset
)
801 set
= fdt
->close_on_exec
->fds_bits
[j
];
804 fdt
->close_on_exec
->fds_bits
[j
] = 0;
805 spin_unlock(&files
->file_lock
);
806 for ( ; set
; i
++,set
>>= 1) {
811 spin_lock(&files
->file_lock
);
814 spin_unlock(&files
->file_lock
);
817 void get_task_comm(char *buf
, struct task_struct
*tsk
)
819 /* buf must be at least sizeof(tsk->comm) in size */
821 strncpy(buf
, tsk
->comm
, sizeof(tsk
->comm
));
825 void set_task_comm(struct task_struct
*tsk
, char *buf
)
828 strlcpy(tsk
->comm
, buf
, sizeof(tsk
->comm
));
832 int flush_old_exec(struct linux_binprm
* bprm
)
836 struct files_struct
*files
;
837 char tcomm
[sizeof(current
->comm
)];
840 * Make sure we have a private signal table and that
841 * we are unassociated from the previous thread group.
843 retval
= de_thread(current
);
848 * Make sure we have private file handles. Ask the
849 * fork helper to do the work for us and the exit
850 * helper to do the cleanup of the old one.
852 files
= current
->files
; /* refcounted so safe to hold */
853 retval
= unshare_files();
857 * Release all of the old mmap stuff
859 retval
= exec_mmap(bprm
->mm
);
863 bprm
->mm
= NULL
; /* We're using it now */
865 /* This is the point of no return */
867 put_files_struct(files
);
869 current
->sas_ss_sp
= current
->sas_ss_size
= 0;
871 if (current
->euid
== current
->uid
&& current
->egid
== current
->gid
)
872 current
->mm
->dumpable
= 1;
874 current
->mm
->dumpable
= suid_dumpable
;
876 name
= bprm
->filename
;
878 /* Copies the binary name from after last slash */
879 for (i
=0; (ch
= *(name
++)) != '\0';) {
881 i
= 0; /* overwrite what we wrote */
883 if (i
< (sizeof(tcomm
) - 1))
887 set_task_comm(current
, tcomm
);
889 current
->flags
&= ~PF_RANDOMIZE
;
892 if (bprm
->e_uid
!= current
->euid
|| bprm
->e_gid
!= current
->egid
||
893 permission(bprm
->file
->f_dentry
->d_inode
,MAY_READ
, NULL
) ||
894 (bprm
->interp_flags
& BINPRM_FLAGS_ENFORCE_NONDUMP
)) {
896 current
->mm
->dumpable
= suid_dumpable
;
899 /* An exec changes our domain. We are no longer part of the thread
902 current
->self_exec_id
++;
904 flush_signal_handlers(current
, 0);
905 flush_old_files(current
->files
);
910 put_files_struct(current
->files
);
911 current
->files
= files
;
916 EXPORT_SYMBOL(flush_old_exec
);
919 * Fill the binprm structure from the inode.
920 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
922 int prepare_binprm(struct linux_binprm
*bprm
)
925 struct inode
* inode
= bprm
->file
->f_dentry
->d_inode
;
928 mode
= inode
->i_mode
;
930 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
931 * generic_permission lets a non-executable through
933 if (!(mode
& 0111)) /* with at least _one_ execute bit set */
935 if (bprm
->file
->f_op
== NULL
)
938 bprm
->e_uid
= current
->euid
;
939 bprm
->e_gid
= current
->egid
;
941 if(!(bprm
->file
->f_vfsmnt
->mnt_flags
& MNT_NOSUID
)) {
943 if (mode
& S_ISUID
) {
944 current
->personality
&= ~PER_CLEAR_ON_SETID
;
945 bprm
->e_uid
= inode
->i_uid
;
950 * If setgid is set but no group execute bit then this
951 * is a candidate for mandatory locking, not a setgid
954 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
955 current
->personality
&= ~PER_CLEAR_ON_SETID
;
956 bprm
->e_gid
= inode
->i_gid
;
960 /* fill in binprm security blob */
961 retval
= security_bprm_set(bprm
);
965 memset(bprm
->buf
,0,BINPRM_BUF_SIZE
);
966 return kernel_read(bprm
->file
,0,bprm
->buf
,BINPRM_BUF_SIZE
);
969 EXPORT_SYMBOL(prepare_binprm
);
971 static inline int unsafe_exec(struct task_struct
*p
)
974 if (p
->ptrace
& PT_PTRACED
) {
975 if (p
->ptrace
& PT_PTRACE_CAP
)
976 unsafe
|= LSM_UNSAFE_PTRACE_CAP
;
978 unsafe
|= LSM_UNSAFE_PTRACE
;
980 if (atomic_read(&p
->fs
->count
) > 1 ||
981 atomic_read(&p
->files
->count
) > 1 ||
982 atomic_read(&p
->sighand
->count
) > 1)
983 unsafe
|= LSM_UNSAFE_SHARE
;
988 void compute_creds(struct linux_binprm
*bprm
)
992 if (bprm
->e_uid
!= current
->uid
)
997 unsafe
= unsafe_exec(current
);
998 security_bprm_apply_creds(bprm
, unsafe
);
999 task_unlock(current
);
1000 security_bprm_post_apply_creds(bprm
);
1003 EXPORT_SYMBOL(compute_creds
);
1005 void remove_arg_zero(struct linux_binprm
*bprm
)
1008 unsigned long offset
;
1012 offset
= bprm
->p
% PAGE_SIZE
;
1015 while (bprm
->p
++, *(kaddr
+offset
++)) {
1016 if (offset
!= PAGE_SIZE
)
1019 kunmap_atomic(kaddr
, KM_USER0
);
1021 page
= bprm
->page
[bprm
->p
/PAGE_SIZE
];
1022 kaddr
= kmap_atomic(page
, KM_USER0
);
1024 kunmap_atomic(kaddr
, KM_USER0
);
1029 EXPORT_SYMBOL(remove_arg_zero
);
1032 * cycle the list of binary formats handler, until one recognizes the image
1034 int search_binary_handler(struct linux_binprm
*bprm
,struct pt_regs
*regs
)
1037 struct linux_binfmt
*fmt
;
1039 /* handle /sbin/loader.. */
1041 struct exec
* eh
= (struct exec
*) bprm
->buf
;
1043 if (!bprm
->loader
&& eh
->fh
.f_magic
== 0x183 &&
1044 (eh
->fh
.f_flags
& 0x3000) == 0x3000)
1047 unsigned long loader
;
1049 allow_write_access(bprm
->file
);
1053 loader
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1055 file
= open_exec("/sbin/loader");
1056 retval
= PTR_ERR(file
);
1060 /* Remember if the application is TASO. */
1061 bprm
->sh_bang
= eh
->ah
.entry
< 0x100000000UL
;
1064 bprm
->loader
= loader
;
1065 retval
= prepare_binprm(bprm
);
1068 /* should call search_binary_handler recursively here,
1069 but it does not matter */
1073 retval
= security_bprm_check(bprm
);
1077 /* kernel module loader fixup */
1078 /* so we don't try to load run modprobe in kernel space. */
1081 for (try=0; try<2; try++) {
1082 read_lock(&binfmt_lock
);
1083 for (fmt
= formats
; fmt
; fmt
= fmt
->next
) {
1084 int (*fn
)(struct linux_binprm
*, struct pt_regs
*) = fmt
->load_binary
;
1087 if (!try_module_get(fmt
->module
))
1089 read_unlock(&binfmt_lock
);
1090 retval
= fn(bprm
, regs
);
1093 allow_write_access(bprm
->file
);
1097 current
->did_exec
= 1;
1100 read_lock(&binfmt_lock
);
1102 if (retval
!= -ENOEXEC
|| bprm
->mm
== NULL
)
1105 read_unlock(&binfmt_lock
);
1109 read_unlock(&binfmt_lock
);
1110 if (retval
!= -ENOEXEC
|| bprm
->mm
== NULL
) {
1114 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1115 if (printable(bprm
->buf
[0]) &&
1116 printable(bprm
->buf
[1]) &&
1117 printable(bprm
->buf
[2]) &&
1118 printable(bprm
->buf
[3]))
1119 break; /* -ENOEXEC */
1120 request_module("binfmt-%04x", *(unsigned short *)(&bprm
->buf
[2]));
1127 EXPORT_SYMBOL(search_binary_handler
);
1130 * sys_execve() executes a new program.
1132 int do_execve(char * filename
,
1133 char __user
*__user
*argv
,
1134 char __user
*__user
*envp
,
1135 struct pt_regs
* regs
)
1137 struct linux_binprm
*bprm
;
1143 bprm
= kmalloc(sizeof(*bprm
), GFP_KERNEL
);
1146 memset(bprm
, 0, sizeof(*bprm
));
1148 file
= open_exec(filename
);
1149 retval
= PTR_ERR(file
);
1155 bprm
->p
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1158 bprm
->filename
= filename
;
1159 bprm
->interp
= filename
;
1160 bprm
->mm
= mm_alloc();
1165 retval
= init_new_context(current
, bprm
->mm
);
1169 bprm
->argc
= count(argv
, bprm
->p
/ sizeof(void *));
1170 if ((retval
= bprm
->argc
) < 0)
1173 bprm
->envc
= count(envp
, bprm
->p
/ sizeof(void *));
1174 if ((retval
= bprm
->envc
) < 0)
1177 retval
= security_bprm_alloc(bprm
);
1181 retval
= prepare_binprm(bprm
);
1185 retval
= copy_strings_kernel(1, &bprm
->filename
, bprm
);
1189 bprm
->exec
= bprm
->p
;
1190 retval
= copy_strings(bprm
->envc
, envp
, bprm
);
1194 retval
= copy_strings(bprm
->argc
, argv
, bprm
);
1198 retval
= search_binary_handler(bprm
,regs
);
1200 free_arg_pages(bprm
);
1202 /* execve success */
1203 security_bprm_free(bprm
);
1204 acct_update_integrals(current
);
1210 /* Something went wrong, return the inode and free the argument pages*/
1211 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
1212 struct page
* page
= bprm
->page
[i
];
1218 security_bprm_free(bprm
);
1226 allow_write_access(bprm
->file
);
1237 int set_binfmt(struct linux_binfmt
*new)
1239 struct linux_binfmt
*old
= current
->binfmt
;
1242 if (!try_module_get(new->module
))
1245 current
->binfmt
= new;
1247 module_put(old
->module
);
1251 EXPORT_SYMBOL(set_binfmt
);
1253 #define CORENAME_MAX_SIZE 64
1255 /* format_corename will inspect the pattern parameter, and output a
1256 * name into corename, which must have space for at least
1257 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1259 static void format_corename(char *corename
, const char *pattern
, long signr
)
1261 const char *pat_ptr
= pattern
;
1262 char *out_ptr
= corename
;
1263 char *const out_end
= corename
+ CORENAME_MAX_SIZE
;
1265 int pid_in_pattern
= 0;
1267 /* Repeat as long as we have more pattern to process and more output
1270 if (*pat_ptr
!= '%') {
1271 if (out_ptr
== out_end
)
1273 *out_ptr
++ = *pat_ptr
++;
1275 switch (*++pat_ptr
) {
1278 /* Double percent, output one percent */
1280 if (out_ptr
== out_end
)
1287 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1288 "%d", current
->tgid
);
1289 if (rc
> out_end
- out_ptr
)
1295 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1296 "%d", current
->uid
);
1297 if (rc
> out_end
- out_ptr
)
1303 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1304 "%d", current
->gid
);
1305 if (rc
> out_end
- out_ptr
)
1309 /* signal that caused the coredump */
1311 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1313 if (rc
> out_end
- out_ptr
)
1317 /* UNIX time of coredump */
1320 do_gettimeofday(&tv
);
1321 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1323 if (rc
> out_end
- out_ptr
)
1330 down_read(&uts_sem
);
1331 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1332 "%s", system_utsname
.nodename
);
1334 if (rc
> out_end
- out_ptr
)
1340 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1341 "%s", current
->comm
);
1342 if (rc
> out_end
- out_ptr
)
1352 /* Backward compatibility with core_uses_pid:
1354 * If core_pattern does not include a %p (as is the default)
1355 * and core_uses_pid is set, then .%pid will be appended to
1358 && (core_uses_pid
|| atomic_read(¤t
->mm
->mm_users
) != 1)) {
1359 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1360 ".%d", current
->tgid
);
1361 if (rc
> out_end
- out_ptr
)
1369 static void zap_threads (struct mm_struct
*mm
)
1371 struct task_struct
*g
, *p
;
1372 struct task_struct
*tsk
= current
;
1373 struct completion
*vfork_done
= tsk
->vfork_done
;
1377 * Make sure nobody is waiting for us to release the VM,
1378 * otherwise we can deadlock when we wait on each other
1381 tsk
->vfork_done
= NULL
;
1382 complete(vfork_done
);
1385 read_lock(&tasklist_lock
);
1387 if (mm
== p
->mm
&& p
!= tsk
) {
1388 force_sig_specific(SIGKILL
, p
);
1390 if (unlikely(p
->ptrace
) &&
1391 unlikely(p
->parent
->mm
== mm
))
1394 while_each_thread(g
,p
);
1396 read_unlock(&tasklist_lock
);
1398 if (unlikely(traced
)) {
1400 * We are zapping a thread and the thread it ptraces.
1401 * If the tracee went into a ptrace stop for exit tracing,
1402 * we could deadlock since the tracer is waiting for this
1403 * coredump to finish. Detach them so they can both die.
1405 write_lock_irq(&tasklist_lock
);
1406 do_each_thread(g
,p
) {
1407 if (mm
== p
->mm
&& p
!= tsk
&&
1408 p
->ptrace
&& p
->parent
->mm
== mm
) {
1411 } while_each_thread(g
,p
);
1412 write_unlock_irq(&tasklist_lock
);
1416 static void coredump_wait(struct mm_struct
*mm
)
1418 DECLARE_COMPLETION(startup_done
);
1420 mm
->core_waiters
++; /* let other threads block */
1421 mm
->core_startup_done
= &startup_done
;
1423 /* give other threads a chance to run: */
1427 if (--mm
->core_waiters
) {
1428 up_write(&mm
->mmap_sem
);
1429 wait_for_completion(&startup_done
);
1431 up_write(&mm
->mmap_sem
);
1432 BUG_ON(mm
->core_waiters
);
1435 int do_coredump(long signr
, int exit_code
, struct pt_regs
* regs
)
1437 char corename
[CORENAME_MAX_SIZE
+ 1];
1438 struct mm_struct
*mm
= current
->mm
;
1439 struct linux_binfmt
* binfmt
;
1440 struct inode
* inode
;
1443 int fsuid
= current
->fsuid
;
1446 binfmt
= current
->binfmt
;
1447 if (!binfmt
|| !binfmt
->core_dump
)
1449 down_write(&mm
->mmap_sem
);
1450 if (!mm
->dumpable
) {
1451 up_write(&mm
->mmap_sem
);
1456 * We cannot trust fsuid as being the "true" uid of the
1457 * process nor do we know its entire history. We only know it
1458 * was tainted so we dump it as root in mode 2.
1460 if (mm
->dumpable
== 2) { /* Setuid core dump mode */
1461 flag
= O_EXCL
; /* Stop rewrite attacks */
1462 current
->fsuid
= 0; /* Dump root private */
1465 init_completion(&mm
->core_done
);
1466 spin_lock_irq(¤t
->sighand
->siglock
);
1467 current
->signal
->flags
= SIGNAL_GROUP_EXIT
;
1468 current
->signal
->group_exit_code
= exit_code
;
1469 spin_unlock_irq(¤t
->sighand
->siglock
);
1473 * Clear any false indication of pending signals that might
1474 * be seen by the filesystem code called to write the core file.
1476 current
->signal
->group_stop_count
= 0;
1477 clear_thread_flag(TIF_SIGPENDING
);
1479 if (current
->signal
->rlim
[RLIMIT_CORE
].rlim_cur
< binfmt
->min_coredump
)
1483 * lock_kernel() because format_corename() is controlled by sysctl, which
1484 * uses lock_kernel()
1487 format_corename(corename
, core_pattern
, signr
);
1489 file
= filp_open(corename
, O_CREAT
| 2 | O_NOFOLLOW
| O_LARGEFILE
| flag
, 0600);
1492 inode
= file
->f_dentry
->d_inode
;
1493 if (inode
->i_nlink
> 1)
1494 goto close_fail
; /* multiple links - don't dump */
1495 if (d_unhashed(file
->f_dentry
))
1498 if (!S_ISREG(inode
->i_mode
))
1502 if (!file
->f_op
->write
)
1504 if (do_truncate(file
->f_dentry
, 0) != 0)
1507 retval
= binfmt
->core_dump(signr
, regs
, file
);
1510 current
->signal
->group_exit_code
|= 0x80;
1512 filp_close(file
, NULL
);
1514 current
->fsuid
= fsuid
;
1515 complete_all(&mm
->core_done
);