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/slab.h>
26 #include <linux/file.h>
27 #include <linux/mman.h>
28 #include <linux/a.out.h>
29 #include <linux/stat.h>
30 #include <linux/fcntl.h>
31 #include <linux/smp_lock.h>
32 #include <linux/init.h>
33 #include <linux/pagemap.h>
34 #include <linux/highmem.h>
35 #include <linux/spinlock.h>
36 #include <linux/key.h>
37 #include <linux/personality.h>
38 #include <linux/binfmts.h>
39 #include <linux/swap.h>
40 #include <linux/utsname.h>
41 #include <linux/pid_namespace.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/tsacct_kern.h>
51 #include <linux/cn_proc.h>
52 #include <linux/audit.h>
53 #include <linux/signalfd.h>
55 #include <asm/uaccess.h>
56 #include <asm/mmu_context.h>
59 #include <linux/kmod.h>
63 char core_pattern
[CORENAME_MAX_SIZE
] = "core";
64 int suid_dumpable
= 0;
66 EXPORT_SYMBOL(suid_dumpable
);
67 /* The maximal length of core_pattern is also specified in sysctl.c */
69 static struct linux_binfmt
*formats
;
70 static DEFINE_RWLOCK(binfmt_lock
);
72 int register_binfmt(struct linux_binfmt
* fmt
)
74 struct linux_binfmt
** tmp
= &formats
;
80 write_lock(&binfmt_lock
);
83 write_unlock(&binfmt_lock
);
90 write_unlock(&binfmt_lock
);
94 EXPORT_SYMBOL(register_binfmt
);
96 int unregister_binfmt(struct linux_binfmt
* fmt
)
98 struct linux_binfmt
** tmp
= &formats
;
100 write_lock(&binfmt_lock
);
105 write_unlock(&binfmt_lock
);
110 write_unlock(&binfmt_lock
);
114 EXPORT_SYMBOL(unregister_binfmt
);
116 static inline void put_binfmt(struct linux_binfmt
* fmt
)
118 module_put(fmt
->module
);
122 * Note that a shared library must be both readable and executable due to
125 * Also note that we take the address to load from from the file itself.
127 asmlinkage
long sys_uselib(const char __user
* library
)
133 error
= __user_path_lookup_open(library
, LOOKUP_FOLLOW
, &nd
, FMODE_READ
|FMODE_EXEC
);
138 if (nd
.mnt
->mnt_flags
& MNT_NOEXEC
)
141 if (!S_ISREG(nd
.dentry
->d_inode
->i_mode
))
144 error
= vfs_permission(&nd
, MAY_READ
| MAY_EXEC
);
148 file
= nameidata_to_filp(&nd
, O_RDONLY
);
149 error
= PTR_ERR(file
);
155 struct linux_binfmt
* fmt
;
157 read_lock(&binfmt_lock
);
158 for (fmt
= formats
; fmt
; fmt
= fmt
->next
) {
159 if (!fmt
->load_shlib
)
161 if (!try_module_get(fmt
->module
))
163 read_unlock(&binfmt_lock
);
164 error
= fmt
->load_shlib(file
);
165 read_lock(&binfmt_lock
);
167 if (error
!= -ENOEXEC
)
170 read_unlock(&binfmt_lock
);
176 release_open_intent(&nd
);
182 * count() counts the number of strings in array ARGV.
184 static int count(char __user
* __user
* argv
, int max
)
192 if (get_user(p
, argv
))
206 * 'copy_strings()' copies argument/environment strings from user
207 * memory to free pages in kernel mem. These are in a format ready
208 * to be put directly into the top of new user memory.
210 static int copy_strings(int argc
, char __user
* __user
* argv
,
211 struct linux_binprm
*bprm
)
213 struct page
*kmapped_page
= NULL
;
222 if (get_user(str
, argv
+argc
) ||
223 !(len
= strnlen_user(str
, bprm
->p
))) {
234 /* XXX: add architecture specific overflow check here. */
239 int offset
, bytes_to_copy
;
242 offset
= pos
% PAGE_SIZE
;
244 page
= bprm
->page
[i
];
247 page
= alloc_page(GFP_HIGHUSER
);
248 bprm
->page
[i
] = page
;
256 if (page
!= kmapped_page
) {
258 kunmap(kmapped_page
);
260 kaddr
= kmap(kmapped_page
);
263 memset(kaddr
, 0, offset
);
264 bytes_to_copy
= PAGE_SIZE
- offset
;
265 if (bytes_to_copy
> len
) {
268 memset(kaddr
+offset
+len
, 0,
269 PAGE_SIZE
-offset
-len
);
271 err
= copy_from_user(kaddr
+offset
, str
, bytes_to_copy
);
277 pos
+= bytes_to_copy
;
278 str
+= bytes_to_copy
;
279 len
-= bytes_to_copy
;
285 kunmap(kmapped_page
);
290 * Like copy_strings, but get argv and its values from kernel memory.
292 int copy_strings_kernel(int argc
,char ** argv
, struct linux_binprm
*bprm
)
295 mm_segment_t oldfs
= get_fs();
297 r
= copy_strings(argc
, (char __user
* __user
*)argv
, bprm
);
302 EXPORT_SYMBOL(copy_strings_kernel
);
306 * This routine is used to map in a page into an address space: needed by
307 * execve() for the initial stack and environment pages.
309 * vma->vm_mm->mmap_sem is held for writing.
311 void install_arg_page(struct vm_area_struct
*vma
,
312 struct page
*page
, unsigned long address
)
314 struct mm_struct
*mm
= vma
->vm_mm
;
318 if (unlikely(anon_vma_prepare(vma
)))
321 flush_dcache_page(page
);
322 pte
= get_locked_pte(mm
, address
, &ptl
);
325 if (!pte_none(*pte
)) {
326 pte_unmap_unlock(pte
, ptl
);
329 inc_mm_counter(mm
, anon_rss
);
330 lru_cache_add_active(page
);
331 set_pte_at(mm
, address
, pte
, pte_mkdirty(pte_mkwrite(mk_pte(
332 page
, vma
->vm_page_prot
))));
333 page_add_new_anon_rmap(page
, vma
, address
);
334 pte_unmap_unlock(pte
, ptl
);
336 /* no need for flush_tlb */
340 force_sig(SIGKILL
, current
);
343 #define EXTRA_STACK_VM_PAGES 20 /* random */
345 int setup_arg_pages(struct linux_binprm
*bprm
,
346 unsigned long stack_top
,
347 int executable_stack
)
349 unsigned long stack_base
;
350 struct vm_area_struct
*mpnt
;
351 struct mm_struct
*mm
= current
->mm
;
355 #ifdef CONFIG_STACK_GROWSUP
356 /* Move the argument and environment strings to the bottom of the
362 /* Start by shifting all the pages down */
364 for (j
= 0; j
< MAX_ARG_PAGES
; j
++) {
365 struct page
*page
= bprm
->page
[j
];
368 bprm
->page
[i
++] = page
;
371 /* Now move them within their pages */
372 offset
= bprm
->p
% PAGE_SIZE
;
373 to
= kmap(bprm
->page
[0]);
374 for (j
= 1; j
< i
; j
++) {
375 memmove(to
, to
+ offset
, PAGE_SIZE
- offset
);
376 from
= kmap(bprm
->page
[j
]);
377 memcpy(to
+ PAGE_SIZE
- offset
, from
, offset
);
378 kunmap(bprm
->page
[j
- 1]);
381 memmove(to
, to
+ offset
, PAGE_SIZE
- offset
);
382 kunmap(bprm
->page
[j
- 1]);
384 /* Limit stack size to 1GB */
385 stack_base
= current
->signal
->rlim
[RLIMIT_STACK
].rlim_max
;
386 if (stack_base
> (1 << 30))
387 stack_base
= 1 << 30;
388 stack_base
= PAGE_ALIGN(stack_top
- stack_base
);
390 /* Adjust bprm->p to point to the end of the strings. */
391 bprm
->p
= stack_base
+ PAGE_SIZE
* i
- offset
;
393 mm
->arg_start
= stack_base
;
394 arg_size
= i
<< PAGE_SHIFT
;
396 /* zero pages that were copied above */
397 while (i
< MAX_ARG_PAGES
)
398 bprm
->page
[i
++] = NULL
;
400 stack_base
= arch_align_stack(stack_top
- MAX_ARG_PAGES
*PAGE_SIZE
);
401 stack_base
= PAGE_ALIGN(stack_base
);
402 bprm
->p
+= stack_base
;
403 mm
->arg_start
= bprm
->p
;
404 arg_size
= stack_top
- (PAGE_MASK
& (unsigned long) mm
->arg_start
);
407 arg_size
+= EXTRA_STACK_VM_PAGES
* PAGE_SIZE
;
410 bprm
->loader
+= stack_base
;
411 bprm
->exec
+= stack_base
;
413 mpnt
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
417 down_write(&mm
->mmap_sem
);
420 #ifdef CONFIG_STACK_GROWSUP
421 mpnt
->vm_start
= stack_base
;
422 mpnt
->vm_end
= stack_base
+ arg_size
;
424 mpnt
->vm_end
= stack_top
;
425 mpnt
->vm_start
= mpnt
->vm_end
- arg_size
;
427 /* Adjust stack execute permissions; explicitly enable
428 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
429 * and leave alone (arch default) otherwise. */
430 if (unlikely(executable_stack
== EXSTACK_ENABLE_X
))
431 mpnt
->vm_flags
= VM_STACK_FLAGS
| VM_EXEC
;
432 else if (executable_stack
== EXSTACK_DISABLE_X
)
433 mpnt
->vm_flags
= VM_STACK_FLAGS
& ~VM_EXEC
;
435 mpnt
->vm_flags
= VM_STACK_FLAGS
;
436 mpnt
->vm_flags
|= mm
->def_flags
;
437 mpnt
->vm_page_prot
= protection_map
[mpnt
->vm_flags
& 0x7];
438 if ((ret
= insert_vm_struct(mm
, mpnt
))) {
439 up_write(&mm
->mmap_sem
);
440 kmem_cache_free(vm_area_cachep
, mpnt
);
443 mm
->stack_vm
= mm
->total_vm
= vma_pages(mpnt
);
446 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
447 struct page
*page
= bprm
->page
[i
];
449 bprm
->page
[i
] = NULL
;
450 install_arg_page(mpnt
, page
, stack_base
);
452 stack_base
+= PAGE_SIZE
;
454 up_write(&mm
->mmap_sem
);
459 EXPORT_SYMBOL(setup_arg_pages
);
461 #define free_arg_pages(bprm) do { } while (0)
465 static inline void free_arg_pages(struct linux_binprm
*bprm
)
469 for (i
= 0; i
< MAX_ARG_PAGES
; i
++) {
471 __free_page(bprm
->page
[i
]);
472 bprm
->page
[i
] = NULL
;
476 #endif /* CONFIG_MMU */
478 struct file
*open_exec(const char *name
)
484 err
= path_lookup_open(AT_FDCWD
, name
, LOOKUP_FOLLOW
, &nd
, FMODE_READ
|FMODE_EXEC
);
488 struct inode
*inode
= nd
.dentry
->d_inode
;
489 file
= ERR_PTR(-EACCES
);
490 if (!(nd
.mnt
->mnt_flags
& MNT_NOEXEC
) &&
491 S_ISREG(inode
->i_mode
)) {
492 int err
= vfs_permission(&nd
, MAY_EXEC
);
495 file
= nameidata_to_filp(&nd
, O_RDONLY
);
497 err
= deny_write_access(file
);
507 release_open_intent(&nd
);
513 EXPORT_SYMBOL(open_exec
);
515 int kernel_read(struct file
*file
, unsigned long offset
,
516 char *addr
, unsigned long count
)
524 /* The cast to a user pointer is valid due to the set_fs() */
525 result
= vfs_read(file
, (void __user
*)addr
, count
, &pos
);
530 EXPORT_SYMBOL(kernel_read
);
532 static int exec_mmap(struct mm_struct
*mm
)
534 struct task_struct
*tsk
;
535 struct mm_struct
* old_mm
, *active_mm
;
537 /* Notify parent that we're no longer interested in the old VM */
539 old_mm
= current
->mm
;
540 mm_release(tsk
, old_mm
);
544 * Make sure that if there is a core dump in progress
545 * for the old mm, we get out and die instead of going
546 * through with the exec. We must hold mmap_sem around
547 * checking core_waiters and changing tsk->mm. The
548 * core-inducing thread will increment core_waiters for
549 * each thread whose ->mm == old_mm.
551 down_read(&old_mm
->mmap_sem
);
552 if (unlikely(old_mm
->core_waiters
)) {
553 up_read(&old_mm
->mmap_sem
);
558 active_mm
= tsk
->active_mm
;
561 activate_mm(active_mm
, mm
);
563 arch_pick_mmap_layout(mm
);
565 up_read(&old_mm
->mmap_sem
);
566 BUG_ON(active_mm
!= old_mm
);
575 * This function makes sure the current process has its own signal table,
576 * so that flush_signal_handlers can later reset the handlers without
577 * disturbing other processes. (Other processes might share the signal
578 * table via the CLONE_SIGHAND option to clone().)
580 static int de_thread(struct task_struct
*tsk
)
582 struct signal_struct
*sig
= tsk
->signal
;
583 struct sighand_struct
*newsighand
, *oldsighand
= tsk
->sighand
;
584 spinlock_t
*lock
= &oldsighand
->siglock
;
585 struct task_struct
*leader
= NULL
;
589 * Tell all the sighand listeners that this sighand has
590 * been detached. The signalfd_detach() function grabs the
591 * sighand lock, if signal listeners are present on the sighand.
593 signalfd_detach(tsk
);
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(tsk
))
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
);
630 * child_reaper ignores SIGKILL, change it now.
631 * Reparenting needs write_lock on tasklist_lock,
632 * so it is safe to do it under read_lock.
634 if (unlikely(tsk
->group_leader
== child_reaper(tsk
)))
635 tsk
->nsproxy
->pid_ns
->child_reaper
= tsk
;
637 zap_other_threads(tsk
);
638 read_unlock(&tasklist_lock
);
641 * Account for the thread group leader hanging around:
644 if (!thread_group_leader(tsk
)) {
647 * The SIGALRM timer survives the exec, but needs to point
648 * at us as the new group leader now. We have a race with
649 * a timer firing now getting the old leader, so we need to
650 * synchronize with any firing (by calling del_timer_sync)
651 * before we can safely let the old group leader die.
654 spin_unlock_irq(lock
);
655 if (hrtimer_cancel(&sig
->real_timer
))
656 hrtimer_restart(&sig
->real_timer
);
659 while (atomic_read(&sig
->count
) > count
) {
660 sig
->group_exit_task
= tsk
;
661 sig
->notify_count
= count
;
662 __set_current_state(TASK_UNINTERRUPTIBLE
);
663 spin_unlock_irq(lock
);
667 sig
->group_exit_task
= NULL
;
668 sig
->notify_count
= 0;
669 spin_unlock_irq(lock
);
672 * At this point all other threads have exited, all we have to
673 * do is to wait for the thread group leader to become inactive,
674 * and to assume its PID:
676 if (!thread_group_leader(tsk
)) {
678 * Wait for the thread group leader to be a zombie.
679 * It should already be zombie at this point, most
682 leader
= tsk
->group_leader
;
683 while (leader
->exit_state
!= EXIT_ZOMBIE
)
687 * The only record we have of the real-time age of a
688 * process, regardless of execs it's done, is start_time.
689 * All the past CPU time is accumulated in signal_struct
690 * from sister threads now dead. But in this non-leader
691 * exec, nothing survives from the original leader thread,
692 * whose birth marks the true age of this process now.
693 * When we take on its identity by switching to its PID, we
694 * also take its birthdate (always earlier than our own).
696 tsk
->start_time
= leader
->start_time
;
698 write_lock_irq(&tasklist_lock
);
700 BUG_ON(leader
->tgid
!= tsk
->tgid
);
701 BUG_ON(tsk
->pid
== tsk
->tgid
);
703 * An exec() starts a new thread group with the
704 * TGID of the previous thread group. Rehash the
705 * two threads with a switched PID, and release
706 * the former thread group leader:
709 /* Become a process group leader with the old leader's pid.
710 * The old leader becomes a thread of the this thread group.
711 * Note: The old leader also uses this pid until release_task
712 * is called. Odd but simple and correct.
714 detach_pid(tsk
, PIDTYPE_PID
);
715 tsk
->pid
= leader
->pid
;
716 attach_pid(tsk
, PIDTYPE_PID
, find_pid(tsk
->pid
));
717 transfer_pid(leader
, tsk
, PIDTYPE_PGID
);
718 transfer_pid(leader
, tsk
, PIDTYPE_SID
);
719 list_replace_rcu(&leader
->tasks
, &tsk
->tasks
);
721 tsk
->group_leader
= tsk
;
722 leader
->group_leader
= tsk
;
724 tsk
->exit_signal
= SIGCHLD
;
726 BUG_ON(leader
->exit_state
!= EXIT_ZOMBIE
);
727 leader
->exit_state
= EXIT_DEAD
;
729 write_unlock_irq(&tasklist_lock
);
733 * There may be one thread left which is just exiting,
734 * but it's safe to stop telling the group to kill themselves.
741 release_task(leader
);
743 BUG_ON(atomic_read(&sig
->count
) != 1);
745 if (atomic_read(&oldsighand
->count
) == 1) {
747 * Now that we nuked the rest of the thread group,
748 * it turns out we are not sharing sighand any more either.
749 * So we can just keep it.
751 kmem_cache_free(sighand_cachep
, newsighand
);
754 * Move our state over to newsighand and switch it in.
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_nested(&newsighand
->siglock
, SINGLE_DEPTH_NESTING
);
764 rcu_assign_pointer(tsk
->sighand
, newsighand
);
767 spin_unlock(&newsighand
->siglock
);
768 spin_unlock(&oldsighand
->siglock
);
769 write_unlock_irq(&tasklist_lock
);
771 __cleanup_sighand(oldsighand
);
774 BUG_ON(!thread_group_leader(tsk
));
779 * These functions flushes out all traces of the currently running executable
780 * so that a new one can be started
783 static void flush_old_files(struct files_struct
* files
)
788 spin_lock(&files
->file_lock
);
790 unsigned long set
, i
;
794 fdt
= files_fdtable(files
);
795 if (i
>= fdt
->max_fds
)
797 set
= fdt
->close_on_exec
->fds_bits
[j
];
800 fdt
->close_on_exec
->fds_bits
[j
] = 0;
801 spin_unlock(&files
->file_lock
);
802 for ( ; set
; i
++,set
>>= 1) {
807 spin_lock(&files
->file_lock
);
810 spin_unlock(&files
->file_lock
);
813 void get_task_comm(char *buf
, struct task_struct
*tsk
)
815 /* buf must be at least sizeof(tsk->comm) in size */
817 strncpy(buf
, tsk
->comm
, sizeof(tsk
->comm
));
821 void set_task_comm(struct task_struct
*tsk
, char *buf
)
824 strlcpy(tsk
->comm
, buf
, sizeof(tsk
->comm
));
828 int flush_old_exec(struct linux_binprm
* bprm
)
832 struct files_struct
*files
;
833 char tcomm
[sizeof(current
->comm
)];
836 * Make sure we have a private signal table and that
837 * we are unassociated from the previous thread group.
839 retval
= de_thread(current
);
844 * Make sure we have private file handles. Ask the
845 * fork helper to do the work for us and the exit
846 * helper to do the cleanup of the old one.
848 files
= current
->files
; /* refcounted so safe to hold */
849 retval
= unshare_files();
853 * Release all of the old mmap stuff
855 retval
= exec_mmap(bprm
->mm
);
859 bprm
->mm
= NULL
; /* We're using it now */
861 /* This is the point of no return */
862 put_files_struct(files
);
864 current
->sas_ss_sp
= current
->sas_ss_size
= 0;
866 if (current
->euid
== current
->uid
&& current
->egid
== current
->gid
)
867 current
->mm
->dumpable
= 1;
869 current
->mm
->dumpable
= suid_dumpable
;
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 /* Set the new mm task size. We have to do that late because it may
888 * depend on TIF_32BIT which is only updated in flush_thread() on
889 * some architectures like powerpc
891 current
->mm
->task_size
= TASK_SIZE
;
893 if (bprm
->e_uid
!= current
->euid
|| bprm
->e_gid
!= current
->egid
||
894 file_permission(bprm
->file
, MAY_READ
) ||
895 (bprm
->interp_flags
& BINPRM_FLAGS_ENFORCE_NONDUMP
)) {
897 current
->mm
->dumpable
= suid_dumpable
;
900 /* An exec changes our domain. We are no longer part of the thread
903 current
->self_exec_id
++;
905 flush_signal_handlers(current
, 0);
906 flush_old_files(current
->files
);
911 reset_files_struct(current
, 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_path
.dentry
->d_inode
;
928 mode
= inode
->i_mode
;
929 if (bprm
->file
->f_op
== NULL
)
932 bprm
->e_uid
= current
->euid
;
933 bprm
->e_gid
= current
->egid
;
935 if(!(bprm
->file
->f_path
.mnt
->mnt_flags
& MNT_NOSUID
)) {
937 if (mode
& S_ISUID
) {
938 current
->personality
&= ~PER_CLEAR_ON_SETID
;
939 bprm
->e_uid
= inode
->i_uid
;
944 * If setgid is set but no group execute bit then this
945 * is a candidate for mandatory locking, not a setgid
948 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
949 current
->personality
&= ~PER_CLEAR_ON_SETID
;
950 bprm
->e_gid
= inode
->i_gid
;
954 /* fill in binprm security blob */
955 retval
= security_bprm_set(bprm
);
959 memset(bprm
->buf
,0,BINPRM_BUF_SIZE
);
960 return kernel_read(bprm
->file
,0,bprm
->buf
,BINPRM_BUF_SIZE
);
963 EXPORT_SYMBOL(prepare_binprm
);
965 static int unsafe_exec(struct task_struct
*p
)
968 if (p
->ptrace
& PT_PTRACED
) {
969 if (p
->ptrace
& PT_PTRACE_CAP
)
970 unsafe
|= LSM_UNSAFE_PTRACE_CAP
;
972 unsafe
|= LSM_UNSAFE_PTRACE
;
974 if (atomic_read(&p
->fs
->count
) > 1 ||
975 atomic_read(&p
->files
->count
) > 1 ||
976 atomic_read(&p
->sighand
->count
) > 1)
977 unsafe
|= LSM_UNSAFE_SHARE
;
982 void compute_creds(struct linux_binprm
*bprm
)
986 if (bprm
->e_uid
!= current
->uid
)
991 unsafe
= unsafe_exec(current
);
992 security_bprm_apply_creds(bprm
, unsafe
);
993 task_unlock(current
);
994 security_bprm_post_apply_creds(bprm
);
996 EXPORT_SYMBOL(compute_creds
);
999 * Arguments are '\0' separated strings found at the location bprm->p
1000 * points to; chop off the first by relocating brpm->p to right after
1001 * the first '\0' encountered.
1003 void remove_arg_zero(struct linux_binprm
*bprm
)
1009 unsigned long offset
;
1010 unsigned long index
;
1014 offset
= bprm
->p
& ~PAGE_MASK
;
1015 index
= bprm
->p
>> PAGE_SHIFT
;
1017 page
= bprm
->page
[index
];
1018 kaddr
= kmap_atomic(page
, KM_USER0
);
1020 /* run through page until we reach end or find NUL */
1022 ch
= *(kaddr
+ offset
);
1024 /* discard that character... */
1027 } while (offset
< PAGE_SIZE
&& ch
!= '\0');
1029 kunmap_atomic(kaddr
, KM_USER0
);
1031 /* free the old page */
1032 if (offset
== PAGE_SIZE
) {
1034 bprm
->page
[index
] = NULL
;
1036 } while (ch
!= '\0');
1041 EXPORT_SYMBOL(remove_arg_zero
);
1044 * cycle the list of binary formats handler, until one recognizes the image
1046 int search_binary_handler(struct linux_binprm
*bprm
,struct pt_regs
*regs
)
1049 struct linux_binfmt
*fmt
;
1051 /* handle /sbin/loader.. */
1053 struct exec
* eh
= (struct exec
*) bprm
->buf
;
1055 if (!bprm
->loader
&& eh
->fh
.f_magic
== 0x183 &&
1056 (eh
->fh
.f_flags
& 0x3000) == 0x3000)
1059 unsigned long loader
;
1061 allow_write_access(bprm
->file
);
1065 loader
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1067 file
= open_exec("/sbin/loader");
1068 retval
= PTR_ERR(file
);
1072 /* Remember if the application is TASO. */
1073 bprm
->sh_bang
= eh
->ah
.entry
< 0x100000000UL
;
1076 bprm
->loader
= loader
;
1077 retval
= prepare_binprm(bprm
);
1080 /* should call search_binary_handler recursively here,
1081 but it does not matter */
1085 retval
= security_bprm_check(bprm
);
1089 /* kernel module loader fixup */
1090 /* so we don't try to load run modprobe in kernel space. */
1093 retval
= audit_bprm(bprm
);
1098 for (try=0; try<2; try++) {
1099 read_lock(&binfmt_lock
);
1100 for (fmt
= formats
; fmt
; fmt
= fmt
->next
) {
1101 int (*fn
)(struct linux_binprm
*, struct pt_regs
*) = fmt
->load_binary
;
1104 if (!try_module_get(fmt
->module
))
1106 read_unlock(&binfmt_lock
);
1107 retval
= fn(bprm
, regs
);
1110 allow_write_access(bprm
->file
);
1114 current
->did_exec
= 1;
1115 proc_exec_connector(current
);
1118 read_lock(&binfmt_lock
);
1120 if (retval
!= -ENOEXEC
|| bprm
->mm
== NULL
)
1123 read_unlock(&binfmt_lock
);
1127 read_unlock(&binfmt_lock
);
1128 if (retval
!= -ENOEXEC
|| bprm
->mm
== NULL
) {
1132 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1133 if (printable(bprm
->buf
[0]) &&
1134 printable(bprm
->buf
[1]) &&
1135 printable(bprm
->buf
[2]) &&
1136 printable(bprm
->buf
[3]))
1137 break; /* -ENOEXEC */
1138 request_module("binfmt-%04x", *(unsigned short *)(&bprm
->buf
[2]));
1145 EXPORT_SYMBOL(search_binary_handler
);
1148 * sys_execve() executes a new program.
1150 int do_execve(char * filename
,
1151 char __user
*__user
*argv
,
1152 char __user
*__user
*envp
,
1153 struct pt_regs
* regs
)
1155 struct linux_binprm
*bprm
;
1161 bprm
= kzalloc(sizeof(*bprm
), GFP_KERNEL
);
1165 file
= open_exec(filename
);
1166 retval
= PTR_ERR(file
);
1172 bprm
->p
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1175 bprm
->filename
= filename
;
1176 bprm
->interp
= filename
;
1177 bprm
->mm
= mm_alloc();
1182 retval
= init_new_context(current
, bprm
->mm
);
1186 bprm
->argc
= count(argv
, bprm
->p
/ sizeof(void *));
1187 if ((retval
= bprm
->argc
) < 0)
1190 bprm
->envc
= count(envp
, bprm
->p
/ sizeof(void *));
1191 if ((retval
= bprm
->envc
) < 0)
1194 retval
= security_bprm_alloc(bprm
);
1198 retval
= prepare_binprm(bprm
);
1202 retval
= copy_strings_kernel(1, &bprm
->filename
, bprm
);
1206 bprm
->exec
= bprm
->p
;
1207 retval
= copy_strings(bprm
->envc
, envp
, bprm
);
1211 retval
= copy_strings(bprm
->argc
, argv
, bprm
);
1215 retval
= search_binary_handler(bprm
,regs
);
1217 free_arg_pages(bprm
);
1219 /* execve success */
1220 security_bprm_free(bprm
);
1221 acct_update_integrals(current
);
1227 /* Something went wrong, return the inode and free the argument pages*/
1228 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
1229 struct page
* page
= bprm
->page
[i
];
1235 security_bprm_free(bprm
);
1243 allow_write_access(bprm
->file
);
1254 int set_binfmt(struct linux_binfmt
*new)
1256 struct linux_binfmt
*old
= current
->binfmt
;
1259 if (!try_module_get(new->module
))
1262 current
->binfmt
= new;
1264 module_put(old
->module
);
1268 EXPORT_SYMBOL(set_binfmt
);
1270 /* format_corename will inspect the pattern parameter, and output a
1271 * name into corename, which must have space for at least
1272 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1274 static int format_corename(char *corename
, const char *pattern
, long signr
)
1276 const char *pat_ptr
= pattern
;
1277 char *out_ptr
= corename
;
1278 char *const out_end
= corename
+ CORENAME_MAX_SIZE
;
1280 int pid_in_pattern
= 0;
1283 if (*pattern
== '|')
1286 /* Repeat as long as we have more pattern to process and more output
1289 if (*pat_ptr
!= '%') {
1290 if (out_ptr
== out_end
)
1292 *out_ptr
++ = *pat_ptr
++;
1294 switch (*++pat_ptr
) {
1297 /* Double percent, output one percent */
1299 if (out_ptr
== out_end
)
1306 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1307 "%d", current
->tgid
);
1308 if (rc
> out_end
- out_ptr
)
1314 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1315 "%d", current
->uid
);
1316 if (rc
> out_end
- out_ptr
)
1322 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1323 "%d", current
->gid
);
1324 if (rc
> out_end
- out_ptr
)
1328 /* signal that caused the coredump */
1330 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1332 if (rc
> out_end
- out_ptr
)
1336 /* UNIX time of coredump */
1339 do_gettimeofday(&tv
);
1340 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1342 if (rc
> out_end
- out_ptr
)
1349 down_read(&uts_sem
);
1350 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1351 "%s", utsname()->nodename
);
1353 if (rc
> out_end
- out_ptr
)
1359 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1360 "%s", current
->comm
);
1361 if (rc
> out_end
- out_ptr
)
1371 /* Backward compatibility with core_uses_pid:
1373 * If core_pattern does not include a %p (as is the default)
1374 * and core_uses_pid is set, then .%pid will be appended to
1375 * the filename. Do not do this for piped commands. */
1376 if (!ispipe
&& !pid_in_pattern
1377 && (core_uses_pid
|| atomic_read(¤t
->mm
->mm_users
) != 1)) {
1378 rc
= snprintf(out_ptr
, out_end
- out_ptr
,
1379 ".%d", current
->tgid
);
1380 if (rc
> out_end
- out_ptr
)
1389 static void zap_process(struct task_struct
*start
)
1391 struct task_struct
*t
;
1393 start
->signal
->flags
= SIGNAL_GROUP_EXIT
;
1394 start
->signal
->group_stop_count
= 0;
1398 if (t
!= current
&& t
->mm
) {
1399 t
->mm
->core_waiters
++;
1400 sigaddset(&t
->pending
.signal
, SIGKILL
);
1401 signal_wake_up(t
, 1);
1403 } while ((t
= next_thread(t
)) != start
);
1406 static inline int zap_threads(struct task_struct
*tsk
, struct mm_struct
*mm
,
1409 struct task_struct
*g
, *p
;
1410 unsigned long flags
;
1413 spin_lock_irq(&tsk
->sighand
->siglock
);
1414 if (!(tsk
->signal
->flags
& SIGNAL_GROUP_EXIT
)) {
1415 tsk
->signal
->group_exit_code
= exit_code
;
1419 spin_unlock_irq(&tsk
->sighand
->siglock
);
1423 if (atomic_read(&mm
->mm_users
) == mm
->core_waiters
+ 1)
1427 for_each_process(g
) {
1428 if (g
== tsk
->group_leader
)
1436 * p->sighand can't disappear, but
1437 * may be changed by de_thread()
1439 lock_task_sighand(p
, &flags
);
1441 unlock_task_sighand(p
, &flags
);
1445 } while ((p
= next_thread(p
)) != g
);
1449 return mm
->core_waiters
;
1452 static int coredump_wait(int exit_code
)
1454 struct task_struct
*tsk
= current
;
1455 struct mm_struct
*mm
= tsk
->mm
;
1456 struct completion startup_done
;
1457 struct completion
*vfork_done
;
1460 init_completion(&mm
->core_done
);
1461 init_completion(&startup_done
);
1462 mm
->core_startup_done
= &startup_done
;
1464 core_waiters
= zap_threads(tsk
, mm
, exit_code
);
1465 up_write(&mm
->mmap_sem
);
1467 if (unlikely(core_waiters
< 0))
1471 * Make sure nobody is waiting for us to release the VM,
1472 * otherwise we can deadlock when we wait on each other
1474 vfork_done
= tsk
->vfork_done
;
1476 tsk
->vfork_done
= NULL
;
1477 complete(vfork_done
);
1481 wait_for_completion(&startup_done
);
1483 BUG_ON(mm
->core_waiters
);
1484 return core_waiters
;
1487 int do_coredump(long signr
, int exit_code
, struct pt_regs
* regs
)
1489 char corename
[CORENAME_MAX_SIZE
+ 1];
1490 struct mm_struct
*mm
= current
->mm
;
1491 struct linux_binfmt
* binfmt
;
1492 struct inode
* inode
;
1495 int fsuid
= current
->fsuid
;
1499 audit_core_dumps(signr
);
1501 binfmt
= current
->binfmt
;
1502 if (!binfmt
|| !binfmt
->core_dump
)
1504 down_write(&mm
->mmap_sem
);
1505 if (!mm
->dumpable
) {
1506 up_write(&mm
->mmap_sem
);
1511 * We cannot trust fsuid as being the "true" uid of the
1512 * process nor do we know its entire history. We only know it
1513 * was tainted so we dump it as root in mode 2.
1515 if (mm
->dumpable
== 2) { /* Setuid core dump mode */
1516 flag
= O_EXCL
; /* Stop rewrite attacks */
1517 current
->fsuid
= 0; /* Dump root private */
1521 retval
= coredump_wait(exit_code
);
1526 * Clear any false indication of pending signals that might
1527 * be seen by the filesystem code called to write the core file.
1529 clear_thread_flag(TIF_SIGPENDING
);
1531 if (current
->signal
->rlim
[RLIMIT_CORE
].rlim_cur
< binfmt
->min_coredump
)
1535 * lock_kernel() because format_corename() is controlled by sysctl, which
1536 * uses lock_kernel()
1539 ispipe
= format_corename(corename
, core_pattern
, signr
);
1542 /* SIGPIPE can happen, but it's just never processed */
1543 if(call_usermodehelper_pipe(corename
+1, NULL
, NULL
, &file
)) {
1544 printk(KERN_INFO
"Core dump to %s pipe failed\n",
1549 file
= filp_open(corename
,
1550 O_CREAT
| 2 | O_NOFOLLOW
| O_LARGEFILE
| flag
,
1554 inode
= file
->f_path
.dentry
->d_inode
;
1555 if (inode
->i_nlink
> 1)
1556 goto close_fail
; /* multiple links - don't dump */
1557 if (!ispipe
&& d_unhashed(file
->f_path
.dentry
))
1560 /* AK: actually i see no reason to not allow this for named pipes etc.,
1561 but keep the previous behaviour for now. */
1562 if (!ispipe
&& !S_ISREG(inode
->i_mode
))
1566 if (!file
->f_op
->write
)
1568 if (!ispipe
&& do_truncate(file
->f_path
.dentry
, 0, 0, file
) != 0)
1571 retval
= binfmt
->core_dump(signr
, regs
, file
);
1574 current
->signal
->group_exit_code
|= 0x80;
1576 filp_close(file
, NULL
);
1578 current
->fsuid
= fsuid
;
1579 complete_all(&mm
->core_done
);