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>
51 #include <linux/cn_proc.h>
53 #include <asm/uaccess.h>
54 #include <asm/mmu_context.h>
57 #include <linux/kmod.h>
61 char core_pattern
[65] = "core";
62 int suid_dumpable
= 0;
64 EXPORT_SYMBOL(suid_dumpable
);
65 /* The maximal length of core_pattern is also specified in sysctl.c */
67 static struct linux_binfmt
*formats
;
68 static DEFINE_RWLOCK(binfmt_lock
);
70 int register_binfmt(struct linux_binfmt
* fmt
)
72 struct linux_binfmt
** tmp
= &formats
;
78 write_lock(&binfmt_lock
);
81 write_unlock(&binfmt_lock
);
88 write_unlock(&binfmt_lock
);
92 EXPORT_SYMBOL(register_binfmt
);
94 int unregister_binfmt(struct linux_binfmt
* fmt
)
96 struct linux_binfmt
** tmp
= &formats
;
98 write_lock(&binfmt_lock
);
102 write_unlock(&binfmt_lock
);
107 write_unlock(&binfmt_lock
);
111 EXPORT_SYMBOL(unregister_binfmt
);
113 static inline void put_binfmt(struct linux_binfmt
* fmt
)
115 module_put(fmt
->module
);
119 * Note that a shared library must be both readable and executable due to
122 * Also note that we take the address to load from from the file itself.
124 asmlinkage
long sys_uselib(const char __user
* library
)
130 error
= __user_path_lookup_open(library
, LOOKUP_FOLLOW
, &nd
, FMODE_READ
);
135 if (!S_ISREG(nd
.dentry
->d_inode
->i_mode
))
138 error
= permission(nd
.dentry
->d_inode
, MAY_READ
| MAY_EXEC
, &nd
);
142 file
= nameidata_to_filp(&nd
, O_RDONLY
);
143 error
= PTR_ERR(file
);
149 struct linux_binfmt
* fmt
;
151 read_lock(&binfmt_lock
);
152 for (fmt
= formats
; fmt
; fmt
= fmt
->next
) {
153 if (!fmt
->load_shlib
)
155 if (!try_module_get(fmt
->module
))
157 read_unlock(&binfmt_lock
);
158 error
= fmt
->load_shlib(file
);
159 read_lock(&binfmt_lock
);
161 if (error
!= -ENOEXEC
)
164 read_unlock(&binfmt_lock
);
170 release_open_intent(&nd
);
176 * count() counts the number of strings in array ARGV.
178 static int count(char __user
* __user
* argv
, int max
)
186 if (get_user(p
, argv
))
200 * 'copy_strings()' copies argument/environment strings from user
201 * memory to free pages in kernel mem. These are in a format ready
202 * to be put directly into the top of new user memory.
204 static int copy_strings(int argc
, char __user
* __user
* argv
,
205 struct linux_binprm
*bprm
)
207 struct page
*kmapped_page
= NULL
;
216 if (get_user(str
, argv
+argc
) ||
217 !(len
= strnlen_user(str
, bprm
->p
))) {
228 /* XXX: add architecture specific overflow check here. */
233 int offset
, bytes_to_copy
;
236 offset
= pos
% PAGE_SIZE
;
238 page
= bprm
->page
[i
];
241 page
= alloc_page(GFP_HIGHUSER
);
242 bprm
->page
[i
] = page
;
250 if (page
!= kmapped_page
) {
252 kunmap(kmapped_page
);
254 kaddr
= kmap(kmapped_page
);
257 memset(kaddr
, 0, offset
);
258 bytes_to_copy
= PAGE_SIZE
- offset
;
259 if (bytes_to_copy
> len
) {
262 memset(kaddr
+offset
+len
, 0,
263 PAGE_SIZE
-offset
-len
);
265 err
= copy_from_user(kaddr
+offset
, str
, bytes_to_copy
);
271 pos
+= bytes_to_copy
;
272 str
+= bytes_to_copy
;
273 len
-= bytes_to_copy
;
279 kunmap(kmapped_page
);
284 * Like copy_strings, but get argv and its values from kernel memory.
286 int copy_strings_kernel(int argc
,char ** argv
, struct linux_binprm
*bprm
)
289 mm_segment_t oldfs
= get_fs();
291 r
= copy_strings(argc
, (char __user
* __user
*)argv
, bprm
);
296 EXPORT_SYMBOL(copy_strings_kernel
);
300 * This routine is used to map in a page into an address space: needed by
301 * execve() for the initial stack and environment pages.
303 * vma->vm_mm->mmap_sem is held for writing.
305 void install_arg_page(struct vm_area_struct
*vma
,
306 struct page
*page
, unsigned long address
)
308 struct mm_struct
*mm
= vma
->vm_mm
;
315 if (unlikely(anon_vma_prepare(vma
)))
318 flush_dcache_page(page
);
319 pgd
= pgd_offset(mm
, address
);
320 pud
= pud_alloc(mm
, pgd
, address
);
323 pmd
= pmd_alloc(mm
, pud
, address
);
326 pte
= pte_alloc_map_lock(mm
, pmd
, address
, &ptl
);
329 if (!pte_none(*pte
)) {
330 pte_unmap_unlock(pte
, ptl
);
333 inc_mm_counter(mm
, anon_rss
);
334 lru_cache_add_active(page
);
335 set_pte_at(mm
, address
, pte
, pte_mkdirty(pte_mkwrite(mk_pte(
336 page
, vma
->vm_page_prot
))));
337 page_add_anon_rmap(page
, vma
, address
);
338 pte_unmap_unlock(pte
, ptl
);
340 /* no need for flush_tlb */
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 memset(mpnt
, 0, sizeof(*mpnt
));
423 down_write(&mm
->mmap_sem
);
426 #ifdef CONFIG_STACK_GROWSUP
427 mpnt
->vm_start
= stack_base
;
428 mpnt
->vm_end
= stack_base
+ arg_size
;
430 mpnt
->vm_end
= stack_top
;
431 mpnt
->vm_start
= mpnt
->vm_end
- arg_size
;
433 /* Adjust stack execute permissions; explicitly enable
434 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
435 * and leave alone (arch default) otherwise. */
436 if (unlikely(executable_stack
== EXSTACK_ENABLE_X
))
437 mpnt
->vm_flags
= VM_STACK_FLAGS
| VM_EXEC
;
438 else if (executable_stack
== EXSTACK_DISABLE_X
)
439 mpnt
->vm_flags
= VM_STACK_FLAGS
& ~VM_EXEC
;
441 mpnt
->vm_flags
= VM_STACK_FLAGS
;
442 mpnt
->vm_flags
|= mm
->def_flags
;
443 mpnt
->vm_page_prot
= protection_map
[mpnt
->vm_flags
& 0x7];
444 if ((ret
= insert_vm_struct(mm
, mpnt
))) {
445 up_write(&mm
->mmap_sem
);
446 kmem_cache_free(vm_area_cachep
, mpnt
);
449 mm
->stack_vm
= mm
->total_vm
= vma_pages(mpnt
);
452 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
453 struct page
*page
= bprm
->page
[i
];
455 bprm
->page
[i
] = NULL
;
456 install_arg_page(mpnt
, page
, stack_base
);
458 stack_base
+= PAGE_SIZE
;
460 up_write(&mm
->mmap_sem
);
465 EXPORT_SYMBOL(setup_arg_pages
);
467 #define free_arg_pages(bprm) do { } while (0)
471 static inline void free_arg_pages(struct linux_binprm
*bprm
)
475 for (i
= 0; i
< MAX_ARG_PAGES
; i
++) {
477 __free_page(bprm
->page
[i
]);
478 bprm
->page
[i
] = NULL
;
482 #endif /* CONFIG_MMU */
484 struct file
*open_exec(const char *name
)
490 err
= path_lookup_open(name
, LOOKUP_FOLLOW
, &nd
, FMODE_READ
);
494 struct inode
*inode
= nd
.dentry
->d_inode
;
495 file
= ERR_PTR(-EACCES
);
496 if (!(nd
.mnt
->mnt_flags
& MNT_NOEXEC
) &&
497 S_ISREG(inode
->i_mode
)) {
498 int err
= permission(inode
, MAY_EXEC
, &nd
);
499 if (!err
&& !(inode
->i_mode
& 0111))
503 file
= nameidata_to_filp(&nd
, O_RDONLY
);
505 err
= deny_write_access(file
);
515 release_open_intent(&nd
);
521 EXPORT_SYMBOL(open_exec
);
523 int kernel_read(struct file
*file
, unsigned long offset
,
524 char *addr
, unsigned long count
)
532 /* The cast to a user pointer is valid due to the set_fs() */
533 result
= vfs_read(file
, (void __user
*)addr
, count
, &pos
);
538 EXPORT_SYMBOL(kernel_read
);
540 static int exec_mmap(struct mm_struct
*mm
)
542 struct task_struct
*tsk
;
543 struct mm_struct
* old_mm
, *active_mm
;
545 /* Notify parent that we're no longer interested in the old VM */
547 old_mm
= current
->mm
;
548 mm_release(tsk
, old_mm
);
552 * Make sure that if there is a core dump in progress
553 * for the old mm, we get out and die instead of going
554 * through with the exec. We must hold mmap_sem around
555 * checking core_waiters and changing tsk->mm. The
556 * core-inducing thread will increment core_waiters for
557 * each thread whose ->mm == old_mm.
559 down_read(&old_mm
->mmap_sem
);
560 if (unlikely(old_mm
->core_waiters
)) {
561 up_read(&old_mm
->mmap_sem
);
566 active_mm
= tsk
->active_mm
;
569 activate_mm(active_mm
, mm
);
571 arch_pick_mmap_layout(mm
);
573 up_read(&old_mm
->mmap_sem
);
574 if (active_mm
!= old_mm
) BUG();
583 * This function makes sure the current process has its own signal table,
584 * so that flush_signal_handlers can later reset the handlers without
585 * disturbing other processes. (Other processes might share the signal
586 * table via the CLONE_SIGHAND option to clone().)
588 static inline int de_thread(struct task_struct
*tsk
)
590 struct signal_struct
*sig
= tsk
->signal
;
591 struct sighand_struct
*newsighand
, *oldsighand
= tsk
->sighand
;
592 spinlock_t
*lock
= &oldsighand
->siglock
;
596 * If we don't share sighandlers, then we aren't sharing anything
597 * and we can just re-use it all.
599 if (atomic_read(&oldsighand
->count
) <= 1) {
600 BUG_ON(atomic_read(&sig
->count
) != 1);
605 newsighand
= kmem_cache_alloc(sighand_cachep
, GFP_KERNEL
);
609 if (thread_group_empty(current
))
610 goto no_thread_group
;
613 * Kill all other threads in the thread group.
614 * We must hold tasklist_lock to call zap_other_threads.
616 read_lock(&tasklist_lock
);
618 if (sig
->flags
& SIGNAL_GROUP_EXIT
) {
620 * Another group action in progress, just
621 * return so that the signal is processed.
623 spin_unlock_irq(lock
);
624 read_unlock(&tasklist_lock
);
625 kmem_cache_free(sighand_cachep
, newsighand
);
628 zap_other_threads(current
);
629 read_unlock(&tasklist_lock
);
632 * Account for the thread group leader hanging around:
635 if (!thread_group_leader(current
)) {
638 * The SIGALRM timer survives the exec, but needs to point
639 * at us as the new group leader now. We have a race with
640 * a timer firing now getting the old leader, so we need to
641 * synchronize with any firing (by calling del_timer_sync)
642 * before we can safely let the old group leader die.
644 sig
->real_timer
.data
= (unsigned long)current
;
645 spin_unlock_irq(lock
);
646 if (del_timer_sync(&sig
->real_timer
))
647 add_timer(&sig
->real_timer
);
650 while (atomic_read(&sig
->count
) > count
) {
651 sig
->group_exit_task
= current
;
652 sig
->notify_count
= count
;
653 __set_current_state(TASK_UNINTERRUPTIBLE
);
654 spin_unlock_irq(lock
);
658 sig
->group_exit_task
= NULL
;
659 sig
->notify_count
= 0;
660 spin_unlock_irq(lock
);
663 * At this point all other threads have exited, all we have to
664 * do is to wait for the thread group leader to become inactive,
665 * and to assume its PID:
667 if (!thread_group_leader(current
)) {
668 struct task_struct
*leader
= current
->group_leader
, *parent
;
669 struct dentry
*proc_dentry1
, *proc_dentry2
;
670 unsigned long exit_state
, ptrace
;
673 * Wait for the thread group leader to be a zombie.
674 * It should already be zombie at this point, most
677 while (leader
->exit_state
!= EXIT_ZOMBIE
)
680 spin_lock(&leader
->proc_lock
);
681 spin_lock(¤t
->proc_lock
);
682 proc_dentry1
= proc_pid_unhash(current
);
683 proc_dentry2
= proc_pid_unhash(leader
);
684 write_lock_irq(&tasklist_lock
);
686 BUG_ON(leader
->tgid
!= current
->tgid
);
687 BUG_ON(current
->pid
== current
->tgid
);
689 * An exec() starts a new thread group with the
690 * TGID of the previous thread group. Rehash the
691 * two threads with a switched PID, and release
692 * the former thread group leader:
694 ptrace
= leader
->ptrace
;
695 parent
= leader
->parent
;
696 if (unlikely(ptrace
) && unlikely(parent
== current
)) {
698 * Joker was ptracing his own group leader,
699 * and now he wants to be his own parent!
700 * We can't have that.
705 ptrace_unlink(current
);
706 ptrace_unlink(leader
);
707 remove_parent(current
);
708 remove_parent(leader
);
710 switch_exec_pids(leader
, current
);
712 current
->parent
= current
->real_parent
= leader
->real_parent
;
713 leader
->parent
= leader
->real_parent
= child_reaper
;
714 current
->group_leader
= current
;
715 leader
->group_leader
= leader
;
717 add_parent(current
, current
->parent
);
718 add_parent(leader
, leader
->parent
);
720 current
->ptrace
= ptrace
;
721 __ptrace_link(current
, parent
);
724 list_del(¤t
->tasks
);
725 list_add_tail(¤t
->tasks
, &init_task
.tasks
);
726 current
->exit_signal
= SIGCHLD
;
727 exit_state
= leader
->exit_state
;
729 write_unlock_irq(&tasklist_lock
);
730 spin_unlock(&leader
->proc_lock
);
731 spin_unlock(¤t
->proc_lock
);
732 proc_pid_flush(proc_dentry1
);
733 proc_pid_flush(proc_dentry2
);
735 BUG_ON(exit_state
!= EXIT_ZOMBIE
);
736 release_task(leader
);
740 * There may be one thread left which is just exiting,
741 * but it's safe to stop telling the group to kill themselves.
746 BUG_ON(atomic_read(&sig
->count
) != 1);
749 if (atomic_read(&oldsighand
->count
) == 1) {
751 * Now that we nuked the rest of the thread group,
752 * it turns out we are not sharing sighand any more either.
753 * So we can just keep it.
755 kmem_cache_free(sighand_cachep
, newsighand
);
758 * Move our state over to newsighand and switch it in.
760 spin_lock_init(&newsighand
->siglock
);
761 atomic_set(&newsighand
->count
, 1);
762 memcpy(newsighand
->action
, oldsighand
->action
,
763 sizeof(newsighand
->action
));
765 write_lock_irq(&tasklist_lock
);
766 spin_lock(&oldsighand
->siglock
);
767 spin_lock(&newsighand
->siglock
);
769 current
->sighand
= newsighand
;
772 spin_unlock(&newsighand
->siglock
);
773 spin_unlock(&oldsighand
->siglock
);
774 write_unlock_irq(&tasklist_lock
);
776 if (atomic_dec_and_test(&oldsighand
->count
))
777 kmem_cache_free(sighand_cachep
, oldsighand
);
780 BUG_ON(!thread_group_leader(current
));
785 * These functions flushes out all traces of the currently running executable
786 * so that a new one can be started
789 static inline void flush_old_files(struct files_struct
* files
)
794 spin_lock(&files
->file_lock
);
796 unsigned long set
, i
;
800 fdt
= files_fdtable(files
);
801 if (i
>= fdt
->max_fds
|| i
>= fdt
->max_fdset
)
803 set
= fdt
->close_on_exec
->fds_bits
[j
];
806 fdt
->close_on_exec
->fds_bits
[j
] = 0;
807 spin_unlock(&files
->file_lock
);
808 for ( ; set
; i
++,set
>>= 1) {
813 spin_lock(&files
->file_lock
);
816 spin_unlock(&files
->file_lock
);
819 void get_task_comm(char *buf
, struct task_struct
*tsk
)
821 /* buf must be at least sizeof(tsk->comm) in size */
823 strncpy(buf
, tsk
->comm
, sizeof(tsk
->comm
));
827 void set_task_comm(struct task_struct
*tsk
, char *buf
)
830 strlcpy(tsk
->comm
, buf
, sizeof(tsk
->comm
));
834 int flush_old_exec(struct linux_binprm
* bprm
)
838 struct files_struct
*files
;
839 char tcomm
[sizeof(current
->comm
)];
842 * Make sure we have a private signal table and that
843 * we are unassociated from the previous thread group.
845 retval
= de_thread(current
);
850 * Make sure we have private file handles. Ask the
851 * fork helper to do the work for us and the exit
852 * helper to do the cleanup of the old one.
854 files
= current
->files
; /* refcounted so safe to hold */
855 retval
= unshare_files();
859 * Release all of the old mmap stuff
861 retval
= exec_mmap(bprm
->mm
);
865 bprm
->mm
= NULL
; /* We're using it now */
867 /* This is the point of no return */
869 put_files_struct(files
);
871 current
->sas_ss_sp
= current
->sas_ss_size
= 0;
873 if (current
->euid
== current
->uid
&& current
->egid
== current
->gid
)
874 current
->mm
->dumpable
= 1;
876 current
->mm
->dumpable
= suid_dumpable
;
878 name
= bprm
->filename
;
880 /* Copies the binary name from after last slash */
881 for (i
=0; (ch
= *(name
++)) != '\0';) {
883 i
= 0; /* overwrite what we wrote */
885 if (i
< (sizeof(tcomm
) - 1))
889 set_task_comm(current
, tcomm
);
891 current
->flags
&= ~PF_RANDOMIZE
;
894 if (bprm
->e_uid
!= current
->euid
|| bprm
->e_gid
!= current
->egid
||
895 permission(bprm
->file
->f_dentry
->d_inode
,MAY_READ
, NULL
) ||
896 (bprm
->interp_flags
& BINPRM_FLAGS_ENFORCE_NONDUMP
)) {
898 current
->mm
->dumpable
= suid_dumpable
;
901 /* An exec changes our domain. We are no longer part of the thread
904 current
->self_exec_id
++;
906 flush_signal_handlers(current
, 0);
907 flush_old_files(current
->files
);
912 put_files_struct(current
->files
);
913 current
->files
= files
;
918 EXPORT_SYMBOL(flush_old_exec
);
921 * Fill the binprm structure from the inode.
922 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
924 int prepare_binprm(struct linux_binprm
*bprm
)
927 struct inode
* inode
= bprm
->file
->f_dentry
->d_inode
;
930 mode
= inode
->i_mode
;
932 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
933 * generic_permission lets a non-executable through
935 if (!(mode
& 0111)) /* with at least _one_ execute bit set */
937 if (bprm
->file
->f_op
== NULL
)
940 bprm
->e_uid
= current
->euid
;
941 bprm
->e_gid
= current
->egid
;
943 if(!(bprm
->file
->f_vfsmnt
->mnt_flags
& MNT_NOSUID
)) {
945 if (mode
& S_ISUID
) {
946 current
->personality
&= ~PER_CLEAR_ON_SETID
;
947 bprm
->e_uid
= inode
->i_uid
;
952 * If setgid is set but no group execute bit then this
953 * is a candidate for mandatory locking, not a setgid
956 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
957 current
->personality
&= ~PER_CLEAR_ON_SETID
;
958 bprm
->e_gid
= inode
->i_gid
;
962 /* fill in binprm security blob */
963 retval
= security_bprm_set(bprm
);
967 memset(bprm
->buf
,0,BINPRM_BUF_SIZE
);
968 return kernel_read(bprm
->file
,0,bprm
->buf
,BINPRM_BUF_SIZE
);
971 EXPORT_SYMBOL(prepare_binprm
);
973 static inline int unsafe_exec(struct task_struct
*p
)
976 if (p
->ptrace
& PT_PTRACED
) {
977 if (p
->ptrace
& PT_PTRACE_CAP
)
978 unsafe
|= LSM_UNSAFE_PTRACE_CAP
;
980 unsafe
|= LSM_UNSAFE_PTRACE
;
982 if (atomic_read(&p
->fs
->count
) > 1 ||
983 atomic_read(&p
->files
->count
) > 1 ||
984 atomic_read(&p
->sighand
->count
) > 1)
985 unsafe
|= LSM_UNSAFE_SHARE
;
990 void compute_creds(struct linux_binprm
*bprm
)
994 if (bprm
->e_uid
!= current
->uid
)
999 unsafe
= unsafe_exec(current
);
1000 security_bprm_apply_creds(bprm
, unsafe
);
1001 task_unlock(current
);
1002 security_bprm_post_apply_creds(bprm
);
1005 EXPORT_SYMBOL(compute_creds
);
1007 void remove_arg_zero(struct linux_binprm
*bprm
)
1010 unsigned long offset
;
1014 offset
= bprm
->p
% PAGE_SIZE
;
1017 while (bprm
->p
++, *(kaddr
+offset
++)) {
1018 if (offset
!= PAGE_SIZE
)
1021 kunmap_atomic(kaddr
, KM_USER0
);
1023 page
= bprm
->page
[bprm
->p
/PAGE_SIZE
];
1024 kaddr
= kmap_atomic(page
, KM_USER0
);
1026 kunmap_atomic(kaddr
, KM_USER0
);
1031 EXPORT_SYMBOL(remove_arg_zero
);
1034 * cycle the list of binary formats handler, until one recognizes the image
1036 int search_binary_handler(struct linux_binprm
*bprm
,struct pt_regs
*regs
)
1039 struct linux_binfmt
*fmt
;
1041 /* handle /sbin/loader.. */
1043 struct exec
* eh
= (struct exec
*) bprm
->buf
;
1045 if (!bprm
->loader
&& eh
->fh
.f_magic
== 0x183 &&
1046 (eh
->fh
.f_flags
& 0x3000) == 0x3000)
1049 unsigned long loader
;
1051 allow_write_access(bprm
->file
);
1055 loader
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1057 file
= open_exec("/sbin/loader");
1058 retval
= PTR_ERR(file
);
1062 /* Remember if the application is TASO. */
1063 bprm
->sh_bang
= eh
->ah
.entry
< 0x100000000UL
;
1066 bprm
->loader
= loader
;
1067 retval
= prepare_binprm(bprm
);
1070 /* should call search_binary_handler recursively here,
1071 but it does not matter */
1075 retval
= security_bprm_check(bprm
);
1079 /* kernel module loader fixup */
1080 /* so we don't try to load run modprobe in kernel space. */
1083 for (try=0; try<2; try++) {
1084 read_lock(&binfmt_lock
);
1085 for (fmt
= formats
; fmt
; fmt
= fmt
->next
) {
1086 int (*fn
)(struct linux_binprm
*, struct pt_regs
*) = fmt
->load_binary
;
1089 if (!try_module_get(fmt
->module
))
1091 read_unlock(&binfmt_lock
);
1092 retval
= fn(bprm
, regs
);
1095 allow_write_access(bprm
->file
);
1099 current
->did_exec
= 1;
1100 proc_exec_connector(current
);
1103 read_lock(&binfmt_lock
);
1105 if (retval
!= -ENOEXEC
|| bprm
->mm
== NULL
)
1108 read_unlock(&binfmt_lock
);
1112 read_unlock(&binfmt_lock
);
1113 if (retval
!= -ENOEXEC
|| bprm
->mm
== NULL
) {
1117 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1118 if (printable(bprm
->buf
[0]) &&
1119 printable(bprm
->buf
[1]) &&
1120 printable(bprm
->buf
[2]) &&
1121 printable(bprm
->buf
[3]))
1122 break; /* -ENOEXEC */
1123 request_module("binfmt-%04x", *(unsigned short *)(&bprm
->buf
[2]));
1130 EXPORT_SYMBOL(search_binary_handler
);
1133 * sys_execve() executes a new program.
1135 int do_execve(char * filename
,
1136 char __user
*__user
*argv
,
1137 char __user
*__user
*envp
,
1138 struct pt_regs
* regs
)
1140 struct linux_binprm
*bprm
;
1146 bprm
= kmalloc(sizeof(*bprm
), GFP_KERNEL
);
1149 memset(bprm
, 0, sizeof(*bprm
));
1151 file
= open_exec(filename
);
1152 retval
= PTR_ERR(file
);
1158 bprm
->p
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1161 bprm
->filename
= filename
;
1162 bprm
->interp
= filename
;
1163 bprm
->mm
= mm_alloc();
1168 retval
= init_new_context(current
, bprm
->mm
);
1172 bprm
->argc
= count(argv
, bprm
->p
/ sizeof(void *));
1173 if ((retval
= bprm
->argc
) < 0)
1176 bprm
->envc
= count(envp
, bprm
->p
/ sizeof(void *));
1177 if ((retval
= bprm
->envc
) < 0)
1180 retval
= security_bprm_alloc(bprm
);
1184 retval
= prepare_binprm(bprm
);
1188 retval
= copy_strings_kernel(1, &bprm
->filename
, bprm
);
1192 bprm
->exec
= bprm
->p
;
1193 retval
= copy_strings(bprm
->envc
, envp
, bprm
);
1197 retval
= copy_strings(bprm
->argc
, argv
, bprm
);
1201 retval
= search_binary_handler(bprm
,regs
);
1203 free_arg_pages(bprm
);
1205 /* execve success */
1206 security_bprm_free(bprm
);
1207 acct_update_integrals(current
);
1213 /* Something went wrong, return the inode and free the argument pages*/
1214 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
1215 struct page
* page
= bprm
->page
[i
];
1221 security_bprm_free(bprm
);
1229 allow_write_access(bprm
->file
);
1240 int set_binfmt(struct linux_binfmt
*new)
1242 struct linux_binfmt
*old
= current
->binfmt
;
1245 if (!try_module_get(new->module
))
1248 current
->binfmt
= new;
1250 module_put(old
->module
);
1254 EXPORT_SYMBOL(set_binfmt
);
1256 #define CORENAME_MAX_SIZE 64
1258 /* format_corename will inspect the pattern parameter, and output a
1259 * name into corename, which must have space for at least
1260 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1262 static void format_corename(char *corename
, const char *pattern
, long signr
)
1264 const char *pat_ptr
= pattern
;
1265 char *out_ptr
= corename
;
1266 char *const out_end
= corename
+ CORENAME_MAX_SIZE
;
1268 int pid_in_pattern
= 0;
1270 /* Repeat as long as we have more pattern to process and more output
1273 if (*pat_ptr
!= '%') {
1274 if (out_ptr
== out_end
)
1276 *out_ptr
++ = *pat_ptr
++;
1278 switch (*++pat_ptr
) {
1281 /* Double percent, output one percent */
1283 if (out_ptr
== out_end
)
1290 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1291 "%d", current
->tgid
);
1292 if (rc
> out_end
- out_ptr
)
1298 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1299 "%d", current
->uid
);
1300 if (rc
> out_end
- out_ptr
)
1306 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1307 "%d", current
->gid
);
1308 if (rc
> out_end
- out_ptr
)
1312 /* signal that caused the coredump */
1314 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1316 if (rc
> out_end
- out_ptr
)
1320 /* UNIX time of coredump */
1323 do_gettimeofday(&tv
);
1324 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1326 if (rc
> out_end
- out_ptr
)
1333 down_read(&uts_sem
);
1334 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1335 "%s", system_utsname
.nodename
);
1337 if (rc
> out_end
- out_ptr
)
1343 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1344 "%s", current
->comm
);
1345 if (rc
> out_end
- out_ptr
)
1355 /* Backward compatibility with core_uses_pid:
1357 * If core_pattern does not include a %p (as is the default)
1358 * and core_uses_pid is set, then .%pid will be appended to
1361 && (core_uses_pid
|| atomic_read(¤t
->mm
->mm_users
) != 1)) {
1362 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1363 ".%d", current
->tgid
);
1364 if (rc
> out_end
- out_ptr
)
1372 static void zap_threads (struct mm_struct
*mm
)
1374 struct task_struct
*g
, *p
;
1375 struct task_struct
*tsk
= current
;
1376 struct completion
*vfork_done
= tsk
->vfork_done
;
1380 * Make sure nobody is waiting for us to release the VM,
1381 * otherwise we can deadlock when we wait on each other
1384 tsk
->vfork_done
= NULL
;
1385 complete(vfork_done
);
1388 read_lock(&tasklist_lock
);
1390 if (mm
== p
->mm
&& p
!= tsk
) {
1391 force_sig_specific(SIGKILL
, p
);
1393 if (unlikely(p
->ptrace
) &&
1394 unlikely(p
->parent
->mm
== mm
))
1397 while_each_thread(g
,p
);
1399 read_unlock(&tasklist_lock
);
1401 if (unlikely(traced
)) {
1403 * We are zapping a thread and the thread it ptraces.
1404 * If the tracee went into a ptrace stop for exit tracing,
1405 * we could deadlock since the tracer is waiting for this
1406 * coredump to finish. Detach them so they can both die.
1408 write_lock_irq(&tasklist_lock
);
1409 do_each_thread(g
,p
) {
1410 if (mm
== p
->mm
&& p
!= tsk
&&
1411 p
->ptrace
&& p
->parent
->mm
== mm
) {
1414 } while_each_thread(g
,p
);
1415 write_unlock_irq(&tasklist_lock
);
1419 static void coredump_wait(struct mm_struct
*mm
)
1421 DECLARE_COMPLETION(startup_done
);
1424 mm
->core_startup_done
= &startup_done
;
1427 core_waiters
= mm
->core_waiters
;
1428 up_write(&mm
->mmap_sem
);
1431 wait_for_completion(&startup_done
);
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 */
1467 spin_lock_irq(¤t
->sighand
->siglock
);
1468 if (!(current
->signal
->flags
& SIGNAL_GROUP_EXIT
)) {
1469 current
->signal
->flags
= SIGNAL_GROUP_EXIT
;
1470 current
->signal
->group_exit_code
= exit_code
;
1473 spin_unlock_irq(¤t
->sighand
->siglock
);
1475 up_write(&mm
->mmap_sem
);
1479 init_completion(&mm
->core_done
);
1483 * Clear any false indication of pending signals that might
1484 * be seen by the filesystem code called to write the core file.
1486 current
->signal
->group_stop_count
= 0;
1487 clear_thread_flag(TIF_SIGPENDING
);
1489 if (current
->signal
->rlim
[RLIMIT_CORE
].rlim_cur
< binfmt
->min_coredump
)
1493 * lock_kernel() because format_corename() is controlled by sysctl, which
1494 * uses lock_kernel()
1497 format_corename(corename
, core_pattern
, signr
);
1499 file
= filp_open(corename
, O_CREAT
| 2 | O_NOFOLLOW
| O_LARGEFILE
| flag
, 0600);
1502 inode
= file
->f_dentry
->d_inode
;
1503 if (inode
->i_nlink
> 1)
1504 goto close_fail
; /* multiple links - don't dump */
1505 if (d_unhashed(file
->f_dentry
))
1508 if (!S_ISREG(inode
->i_mode
))
1512 if (!file
->f_op
->write
)
1514 if (do_truncate(file
->f_dentry
, 0, file
) != 0)
1517 retval
= binfmt
->core_dump(signr
, regs
, file
);
1520 current
->signal
->group_exit_code
|= 0x80;
1522 filp_close(file
, NULL
);
1524 current
->fsuid
= fsuid
;
1525 complete_all(&mm
->core_done
);