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/fdtable.h>
29 #include <linux/vmacache.h>
30 #include <linux/stat.h>
31 #include <linux/fcntl.h>
32 #include <linux/swap.h>
33 #include <linux/string.h>
34 #include <linux/init.h>
35 #include <linux/pagemap.h>
36 #include <linux/perf_event.h>
37 #include <linux/highmem.h>
38 #include <linux/spinlock.h>
39 #include <linux/key.h>
40 #include <linux/personality.h>
41 #include <linux/binfmts.h>
42 #include <linux/utsname.h>
43 #include <linux/pid_namespace.h>
44 #include <linux/module.h>
45 #include <linux/namei.h>
46 #include <linux/mount.h>
47 #include <linux/security.h>
48 #include <linux/syscalls.h>
49 #include <linux/tsacct_kern.h>
50 #include <linux/cn_proc.h>
51 #include <linux/audit.h>
52 #include <linux/tracehook.h>
53 #include <linux/kmod.h>
54 #include <linux/fsnotify.h>
55 #include <linux/fs_struct.h>
56 #include <linux/pipe_fs_i.h>
57 #include <linux/oom.h>
58 #include <linux/compat.h>
59 #include <linux/user_namespace.h>
61 #include <asm/uaccess.h>
62 #include <asm/mmu_context.h>
65 #include <trace/events/task.h>
68 #include <trace/events/sched.h>
70 #ifdef CONFIG_SECURITY_DEFEX
71 #include <linux/defex.h>
75 #define rkp_is_nonroot(x) ((x->cred->type)>>1 & 1)
77 #define rkp_is_lod(x) ((x->cred->type)>>3 & 1)
79 #endif /*CONFIG_RKP_KDP*/
81 int suid_dumpable
= 0;
83 static LIST_HEAD(formats
);
84 static DEFINE_RWLOCK(binfmt_lock
);
86 void __register_binfmt(struct linux_binfmt
* fmt
, int insert
)
89 if (WARN_ON(!fmt
->load_binary
))
91 write_lock(&binfmt_lock
);
92 insert
? list_add(&fmt
->lh
, &formats
) :
93 list_add_tail(&fmt
->lh
, &formats
);
94 write_unlock(&binfmt_lock
);
97 EXPORT_SYMBOL(__register_binfmt
);
99 void unregister_binfmt(struct linux_binfmt
* fmt
)
101 write_lock(&binfmt_lock
);
103 write_unlock(&binfmt_lock
);
106 EXPORT_SYMBOL(unregister_binfmt
);
108 static inline void put_binfmt(struct linux_binfmt
* fmt
)
110 module_put(fmt
->module
);
113 bool path_noexec(const struct path
*path
)
115 return (path
->mnt
->mnt_flags
& MNT_NOEXEC
) ||
116 (path
->mnt
->mnt_sb
->s_iflags
& SB_I_NOEXEC
);
121 * Note that a shared library must be both readable and executable due to
124 * Also note that we take the address to load from from the file itself.
126 SYSCALL_DEFINE1(uselib
, const char __user
*, library
)
128 struct linux_binfmt
*fmt
;
130 struct filename
*tmp
= getname(library
);
131 int error
= PTR_ERR(tmp
);
132 static const struct open_flags uselib_flags
= {
133 .open_flag
= O_LARGEFILE
| O_RDONLY
| __FMODE_EXEC
,
134 .acc_mode
= MAY_READ
| MAY_EXEC
| MAY_OPEN
,
135 .intent
= LOOKUP_OPEN
,
136 .lookup_flags
= LOOKUP_FOLLOW
,
142 file
= do_filp_open(AT_FDCWD
, tmp
, &uselib_flags
);
144 error
= PTR_ERR(file
);
149 if (!S_ISREG(file_inode(file
)->i_mode
))
153 if (path_noexec(&file
->f_path
))
160 read_lock(&binfmt_lock
);
161 list_for_each_entry(fmt
, &formats
, lh
) {
162 if (!fmt
->load_shlib
)
164 if (!try_module_get(fmt
->module
))
166 read_unlock(&binfmt_lock
);
167 error
= fmt
->load_shlib(file
);
168 read_lock(&binfmt_lock
);
170 if (error
!= -ENOEXEC
)
173 read_unlock(&binfmt_lock
);
179 #endif /* #ifdef CONFIG_USELIB */
183 * The nascent bprm->mm is not visible until exec_mmap() but it can
184 * use a lot of memory, account these pages in current->mm temporary
185 * for oom_badness()->get_mm_rss(). Once exec succeeds or fails, we
186 * change the counter back via acct_arg_size(0).
188 static void acct_arg_size(struct linux_binprm
*bprm
, unsigned long pages
)
190 struct mm_struct
*mm
= current
->mm
;
191 long diff
= (long)(pages
- bprm
->vma_pages
);
196 bprm
->vma_pages
= pages
;
197 add_mm_counter(mm
, MM_ANONPAGES
, diff
);
200 static struct page
*get_arg_page(struct linux_binprm
*bprm
, unsigned long pos
,
206 #ifdef CONFIG_STACK_GROWSUP
208 ret
= expand_downwards(bprm
->vma
, pos
);
213 ret
= get_user_pages(current
, bprm
->mm
, pos
,
214 1, write
, 1, &page
, NULL
);
219 unsigned long size
= bprm
->vma
->vm_end
- bprm
->vma
->vm_start
;
220 unsigned long ptr_size
, limit
;
223 * Since the stack will hold pointers to the strings, we
224 * must account for them as well.
226 * The size calculation is the entire vma while each arg page is
227 * built, so each time we get here it's calculating how far it
228 * is currently (rather than each call being just the newly
229 * added size from the arg page). As a result, we need to
230 * always add the entire size of the pointers, so that on the
231 * last call to get_arg_page() we'll actually have the entire
234 ptr_size
= (bprm
->argc
+ bprm
->envc
) * sizeof(void *);
235 if (ptr_size
> ULONG_MAX
- size
)
239 acct_arg_size(bprm
, size
/ PAGE_SIZE
);
242 * We've historically supported up to 32 pages (ARG_MAX)
243 * of argument strings even with small stacks
249 * Limit to 1/4 of the max stack size or 3/4 of _STK_LIM
250 * (whichever is smaller) for the argv+env strings.
252 * - the remaining binfmt code will not run out of stack space,
253 * - the program will have a reasonable amount of stack left
256 limit
= _STK_LIM
/ 4 * 3;
257 limit
= min(limit
, rlimit(RLIMIT_STACK
) / 4);
269 static void put_arg_page(struct page
*page
)
274 static void free_arg_page(struct linux_binprm
*bprm
, int i
)
278 static void free_arg_pages(struct linux_binprm
*bprm
)
282 static void flush_arg_page(struct linux_binprm
*bprm
, unsigned long pos
,
285 flush_cache_page(bprm
->vma
, pos
, page_to_pfn(page
));
288 static int __bprm_mm_init(struct linux_binprm
*bprm
)
291 struct vm_area_struct
*vma
= NULL
;
292 struct mm_struct
*mm
= bprm
->mm
;
294 bprm
->vma
= vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
298 down_write(&mm
->mmap_sem
);
302 * Place the stack at the largest stack address the architecture
303 * supports. Later, we'll move this to an appropriate place. We don't
304 * use STACK_TOP because that can depend on attributes which aren't
307 BUILD_BUG_ON(VM_STACK_FLAGS
& VM_STACK_INCOMPLETE_SETUP
);
308 vma
->vm_end
= STACK_TOP_MAX
;
309 vma
->vm_start
= vma
->vm_end
- PAGE_SIZE
;
310 vma
->vm_flags
= VM_SOFTDIRTY
| VM_STACK_FLAGS
| VM_STACK_INCOMPLETE_SETUP
;
311 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
312 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
314 err
= insert_vm_struct(mm
, vma
);
318 mm
->stack_vm
= mm
->total_vm
= 1;
319 arch_bprm_mm_init(mm
, vma
);
320 up_write(&mm
->mmap_sem
);
321 bprm
->p
= vma
->vm_end
- sizeof(void *);
324 up_write(&mm
->mmap_sem
);
326 kmem_cache_free(vm_area_cachep
, vma
);
330 static bool valid_arg_len(struct linux_binprm
*bprm
, long len
)
332 return len
<= MAX_ARG_STRLEN
;
337 static inline void acct_arg_size(struct linux_binprm
*bprm
, unsigned long pages
)
341 static struct page
*get_arg_page(struct linux_binprm
*bprm
, unsigned long pos
,
346 page
= bprm
->page
[pos
/ PAGE_SIZE
];
347 if (!page
&& write
) {
348 page
= alloc_page(GFP_HIGHUSER
|__GFP_ZERO
);
351 bprm
->page
[pos
/ PAGE_SIZE
] = page
;
357 static void put_arg_page(struct page
*page
)
361 static void free_arg_page(struct linux_binprm
*bprm
, int i
)
364 __free_page(bprm
->page
[i
]);
365 bprm
->page
[i
] = NULL
;
369 static void free_arg_pages(struct linux_binprm
*bprm
)
373 for (i
= 0; i
< MAX_ARG_PAGES
; i
++)
374 free_arg_page(bprm
, i
);
377 static void flush_arg_page(struct linux_binprm
*bprm
, unsigned long pos
,
382 static int __bprm_mm_init(struct linux_binprm
*bprm
)
384 bprm
->p
= PAGE_SIZE
* MAX_ARG_PAGES
- sizeof(void *);
388 static bool valid_arg_len(struct linux_binprm
*bprm
, long len
)
390 return len
<= bprm
->p
;
393 #endif /* CONFIG_MMU */
396 * Create a new mm_struct and populate it with a temporary stack
397 * vm_area_struct. We don't have enough context at this point to set the stack
398 * flags, permissions, and offset, so we use temporary values. We'll update
399 * them later in setup_arg_pages().
401 static int bprm_mm_init(struct linux_binprm
*bprm
)
404 struct mm_struct
*mm
= NULL
;
406 bprm
->mm
= mm
= mm_alloc();
411 err
= __bprm_mm_init(bprm
);
426 struct user_arg_ptr
{
431 const char __user
*const __user
*native
;
433 const compat_uptr_t __user
*compat
;
438 static const char __user
*get_user_arg_ptr(struct user_arg_ptr argv
, int nr
)
440 const char __user
*native
;
443 if (unlikely(argv
.is_compat
)) {
444 compat_uptr_t compat
;
446 if (get_user(compat
, argv
.ptr
.compat
+ nr
))
447 return ERR_PTR(-EFAULT
);
449 return compat_ptr(compat
);
453 if (get_user(native
, argv
.ptr
.native
+ nr
))
454 return ERR_PTR(-EFAULT
);
460 * count() counts the number of strings in array ARGV.
462 static int count(struct user_arg_ptr argv
, int max
)
466 if (argv
.ptr
.native
!= NULL
) {
468 const char __user
*p
= get_user_arg_ptr(argv
, i
);
480 if (fatal_signal_pending(current
))
481 return -ERESTARTNOHAND
;
489 * 'copy_strings()' copies argument/environment strings from the old
490 * processes's memory to the new process's stack. The call to get_user_pages()
491 * ensures the destination page is created and not swapped out.
493 static int copy_strings(int argc
, struct user_arg_ptr argv
,
494 struct linux_binprm
*bprm
)
496 struct page
*kmapped_page
= NULL
;
498 unsigned long kpos
= 0;
502 const char __user
*str
;
507 str
= get_user_arg_ptr(argv
, argc
);
511 len
= strnlen_user(str
, MAX_ARG_STRLEN
);
516 if (!valid_arg_len(bprm
, len
))
519 /* We're going to work our way backwords. */
525 int offset
, bytes_to_copy
;
527 if (fatal_signal_pending(current
)) {
528 ret
= -ERESTARTNOHAND
;
533 offset
= pos
% PAGE_SIZE
;
537 bytes_to_copy
= offset
;
538 if (bytes_to_copy
> len
)
541 offset
-= bytes_to_copy
;
542 pos
-= bytes_to_copy
;
543 str
-= bytes_to_copy
;
544 len
-= bytes_to_copy
;
546 if (!kmapped_page
|| kpos
!= (pos
& PAGE_MASK
)) {
549 page
= get_arg_page(bprm
, pos
, 1);
556 flush_kernel_dcache_page(kmapped_page
);
557 kunmap(kmapped_page
);
558 put_arg_page(kmapped_page
);
561 kaddr
= kmap(kmapped_page
);
562 kpos
= pos
& PAGE_MASK
;
563 flush_arg_page(bprm
, kpos
, kmapped_page
);
565 if (copy_from_user(kaddr
+offset
, str
, bytes_to_copy
)) {
574 flush_kernel_dcache_page(kmapped_page
);
575 kunmap(kmapped_page
);
576 put_arg_page(kmapped_page
);
582 * Like copy_strings, but get argv and its values from kernel memory.
584 int copy_strings_kernel(int argc
, const char *const *__argv
,
585 struct linux_binprm
*bprm
)
588 mm_segment_t oldfs
= get_fs();
589 struct user_arg_ptr argv
= {
590 .ptr
.native
= (const char __user
*const __user
*)__argv
,
594 r
= copy_strings(argc
, argv
, bprm
);
599 EXPORT_SYMBOL(copy_strings_kernel
);
604 * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
605 * the binfmt code determines where the new stack should reside, we shift it to
606 * its final location. The process proceeds as follows:
608 * 1) Use shift to calculate the new vma endpoints.
609 * 2) Extend vma to cover both the old and new ranges. This ensures the
610 * arguments passed to subsequent functions are consistent.
611 * 3) Move vma's page tables to the new range.
612 * 4) Free up any cleared pgd range.
613 * 5) Shrink the vma to cover only the new range.
615 static int shift_arg_pages(struct vm_area_struct
*vma
, unsigned long shift
)
617 struct mm_struct
*mm
= vma
->vm_mm
;
618 unsigned long old_start
= vma
->vm_start
;
619 unsigned long old_end
= vma
->vm_end
;
620 unsigned long length
= old_end
- old_start
;
621 unsigned long new_start
= old_start
- shift
;
622 unsigned long new_end
= old_end
- shift
;
623 struct mmu_gather tlb
;
625 BUG_ON(new_start
> new_end
);
628 * ensure there are no vmas between where we want to go
631 if (vma
!= find_vma(mm
, new_start
))
635 * cover the whole range: [new_start, old_end)
637 if (vma_adjust(vma
, new_start
, old_end
, vma
->vm_pgoff
, NULL
))
641 * move the page tables downwards, on failure we rely on
642 * process cleanup to remove whatever mess we made.
644 if (length
!= move_page_tables(vma
, old_start
,
645 vma
, new_start
, length
, false))
649 tlb_gather_mmu(&tlb
, mm
, old_start
, old_end
);
650 if (new_end
> old_start
) {
652 * when the old and new regions overlap clear from new_end.
654 free_pgd_range(&tlb
, new_end
, old_end
, new_end
,
655 vma
->vm_next
? vma
->vm_next
->vm_start
: USER_PGTABLES_CEILING
);
658 * otherwise, clean from old_start; this is done to not touch
659 * the address space in [new_end, old_start) some architectures
660 * have constraints on va-space that make this illegal (IA64) -
661 * for the others its just a little faster.
663 free_pgd_range(&tlb
, old_start
, old_end
, new_end
,
664 vma
->vm_next
? vma
->vm_next
->vm_start
: USER_PGTABLES_CEILING
);
666 tlb_finish_mmu(&tlb
, old_start
, old_end
);
669 * Shrink the vma to just the new range. Always succeeds.
671 vma_adjust(vma
, new_start
, new_end
, vma
->vm_pgoff
, NULL
);
677 * Finalizes the stack vm_area_struct. The flags and permissions are updated,
678 * the stack is optionally relocated, and some extra space is added.
680 int setup_arg_pages(struct linux_binprm
*bprm
,
681 unsigned long stack_top
,
682 int executable_stack
)
685 unsigned long stack_shift
;
686 struct mm_struct
*mm
= current
->mm
;
687 struct vm_area_struct
*vma
= bprm
->vma
;
688 struct vm_area_struct
*prev
= NULL
;
689 unsigned long vm_flags
;
690 unsigned long stack_base
;
691 unsigned long stack_size
;
692 unsigned long stack_expand
;
693 unsigned long rlim_stack
;
695 #ifdef CONFIG_STACK_GROWSUP
696 /* Limit stack size */
697 stack_base
= rlimit_max(RLIMIT_STACK
);
698 if (stack_base
> STACK_SIZE_MAX
)
699 stack_base
= STACK_SIZE_MAX
;
701 /* Add space for stack randomization. */
702 stack_base
+= (STACK_RND_MASK
<< PAGE_SHIFT
);
704 /* Make sure we didn't let the argument array grow too large. */
705 if (vma
->vm_end
- vma
->vm_start
> stack_base
)
708 stack_base
= PAGE_ALIGN(stack_top
- stack_base
);
710 stack_shift
= vma
->vm_start
- stack_base
;
711 mm
->arg_start
= bprm
->p
- stack_shift
;
712 bprm
->p
= vma
->vm_end
- stack_shift
;
714 stack_top
= arch_align_stack(stack_top
);
715 stack_top
= PAGE_ALIGN(stack_top
);
717 if (unlikely(stack_top
< mmap_min_addr
) ||
718 unlikely(vma
->vm_end
- vma
->vm_start
>= stack_top
- mmap_min_addr
))
721 stack_shift
= vma
->vm_end
- stack_top
;
723 bprm
->p
-= stack_shift
;
724 mm
->arg_start
= bprm
->p
;
728 bprm
->loader
-= stack_shift
;
729 bprm
->exec
-= stack_shift
;
731 down_write(&mm
->mmap_sem
);
732 vm_flags
= VM_STACK_FLAGS
;
735 * Adjust stack execute permissions; explicitly enable for
736 * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
737 * (arch default) otherwise.
739 if (unlikely(executable_stack
== EXSTACK_ENABLE_X
))
741 else if (executable_stack
== EXSTACK_DISABLE_X
)
742 vm_flags
&= ~VM_EXEC
;
743 vm_flags
|= mm
->def_flags
;
744 vm_flags
|= VM_STACK_INCOMPLETE_SETUP
;
746 ret
= mprotect_fixup(vma
, &prev
, vma
->vm_start
, vma
->vm_end
,
752 /* Move stack pages down in memory. */
754 ret
= shift_arg_pages(vma
, stack_shift
);
759 /* mprotect_fixup is overkill to remove the temporary stack flags */
760 vma
->vm_flags
&= ~VM_STACK_INCOMPLETE_SETUP
;
762 stack_expand
= 131072UL; /* randomly 32*4k (or 2*64k) pages */
763 stack_size
= vma
->vm_end
- vma
->vm_start
;
765 * Align this down to a page boundary as expand_stack
768 rlim_stack
= rlimit(RLIMIT_STACK
) & PAGE_MASK
;
769 #ifdef CONFIG_STACK_GROWSUP
770 if (stack_size
+ stack_expand
> rlim_stack
)
771 stack_base
= vma
->vm_start
+ rlim_stack
;
773 stack_base
= vma
->vm_end
+ stack_expand
;
775 if (stack_size
+ stack_expand
> rlim_stack
)
776 stack_base
= vma
->vm_end
- rlim_stack
;
778 stack_base
= vma
->vm_start
- stack_expand
;
780 current
->mm
->start_stack
= bprm
->p
;
781 ret
= expand_stack(vma
, stack_base
);
786 up_write(&mm
->mmap_sem
);
789 EXPORT_SYMBOL(setup_arg_pages
);
791 #endif /* CONFIG_MMU */
793 static struct file
*do_open_execat(int fd
, struct filename
*name
, int flags
)
797 struct open_flags open_exec_flags
= {
798 .open_flag
= O_LARGEFILE
| O_RDONLY
| __FMODE_EXEC
,
799 .acc_mode
= MAY_EXEC
| MAY_OPEN
,
800 .intent
= LOOKUP_OPEN
,
801 .lookup_flags
= LOOKUP_FOLLOW
,
804 if ((flags
& ~(AT_SYMLINK_NOFOLLOW
| AT_EMPTY_PATH
)) != 0)
805 return ERR_PTR(-EINVAL
);
806 if (flags
& AT_SYMLINK_NOFOLLOW
)
807 open_exec_flags
.lookup_flags
&= ~LOOKUP_FOLLOW
;
808 if (flags
& AT_EMPTY_PATH
)
809 open_exec_flags
.lookup_flags
|= LOOKUP_EMPTY
;
811 file
= do_filp_open(fd
, name
, &open_exec_flags
);
816 if (!S_ISREG(file_inode(file
)->i_mode
))
819 if (path_noexec(&file
->f_path
))
822 err
= deny_write_access(file
);
826 if (name
->name
[0] != '\0')
837 struct file
*open_exec(const char *name
)
839 struct filename
*filename
= getname_kernel(name
);
840 struct file
*f
= ERR_CAST(filename
);
842 if (!IS_ERR(filename
)) {
843 f
= do_open_execat(AT_FDCWD
, filename
, 0);
848 EXPORT_SYMBOL(open_exec
);
850 int kernel_read(struct file
*file
, loff_t offset
,
851 char *addr
, unsigned long count
)
859 /* The cast to a user pointer is valid due to the set_fs() */
860 result
= vfs_read(file
, (void __user
*)addr
, count
, &pos
);
865 EXPORT_SYMBOL(kernel_read
);
867 ssize_t
read_code(struct file
*file
, unsigned long addr
, loff_t pos
, size_t len
)
869 ssize_t res
= vfs_read(file
, (void __user
*)addr
, len
, &pos
);
871 flush_icache_range(addr
, addr
+ len
);
874 EXPORT_SYMBOL(read_code
);
876 static int exec_mmap(struct mm_struct
*mm
)
878 struct task_struct
*tsk
;
879 struct mm_struct
*old_mm
, *active_mm
;
881 /* Notify parent that we're no longer interested in the old VM */
883 old_mm
= current
->mm
;
884 mm_release(tsk
, old_mm
);
889 * Make sure that if there is a core dump in progress
890 * for the old mm, we get out and die instead of going
891 * through with the exec. We must hold mmap_sem around
892 * checking core_state and changing tsk->mm.
894 down_read(&old_mm
->mmap_sem
);
895 if (unlikely(old_mm
->core_state
)) {
896 up_read(&old_mm
->mmap_sem
);
901 active_mm
= tsk
->active_mm
;
904 activate_mm(active_mm
, mm
);
905 tsk
->mm
->vmacache_seqnum
= 0;
907 #ifdef CONFIG_RKP_KDP
909 rkp_call(RKP_CMDID(0x43),(unsigned long long)current_cred(), (unsigned long long)mm
->pgd
,0,0,0);
911 #endif /*CONFIG_RKP_KDP*/
914 up_read(&old_mm
->mmap_sem
);
915 BUG_ON(active_mm
!= old_mm
);
916 setmax_mm_hiwater_rss(&tsk
->signal
->maxrss
, old_mm
);
917 mm_update_next_owner(old_mm
);
926 * This function makes sure the current process has its own signal table,
927 * so that flush_signal_handlers can later reset the handlers without
928 * disturbing other processes. (Other processes might share the signal
929 * table via the CLONE_SIGHAND option to clone().)
931 static int de_thread(struct task_struct
*tsk
)
933 struct signal_struct
*sig
= tsk
->signal
;
934 struct sighand_struct
*oldsighand
= tsk
->sighand
;
935 spinlock_t
*lock
= &oldsighand
->siglock
;
937 if (thread_group_empty(tsk
))
938 goto no_thread_group
;
941 * Kill all other threads in the thread group.
944 if (signal_group_exit(sig
)) {
946 * Another group action in progress, just
947 * return so that the signal is processed.
949 spin_unlock_irq(lock
);
953 sig
->group_exit_task
= tsk
;
954 sig
->notify_count
= zap_other_threads(tsk
);
955 if (!thread_group_leader(tsk
))
958 while (sig
->notify_count
) {
959 __set_current_state(TASK_KILLABLE
);
960 spin_unlock_irq(lock
);
962 if (unlikely(__fatal_signal_pending(tsk
)))
966 spin_unlock_irq(lock
);
969 * At this point all other threads have exited, all we have to
970 * do is to wait for the thread group leader to become inactive,
971 * and to assume its PID:
973 if (!thread_group_leader(tsk
)) {
974 struct task_struct
*leader
= tsk
->group_leader
;
977 threadgroup_change_begin(tsk
);
978 write_lock_irq(&tasklist_lock
);
980 * Do this under tasklist_lock to ensure that
981 * exit_notify() can't miss ->group_exit_task
983 sig
->notify_count
= -1;
984 if (likely(leader
->exit_state
))
986 __set_current_state(TASK_KILLABLE
);
987 write_unlock_irq(&tasklist_lock
);
988 threadgroup_change_end(tsk
);
990 if (unlikely(__fatal_signal_pending(tsk
)))
995 * The only record we have of the real-time age of a
996 * process, regardless of execs it's done, is start_time.
997 * All the past CPU time is accumulated in signal_struct
998 * from sister threads now dead. But in this non-leader
999 * exec, nothing survives from the original leader thread,
1000 * whose birth marks the true age of this process now.
1001 * When we take on its identity by switching to its PID, we
1002 * also take its birthdate (always earlier than our own).
1004 tsk
->start_time
= leader
->start_time
;
1005 tsk
->real_start_time
= leader
->real_start_time
;
1007 BUG_ON(!same_thread_group(leader
, tsk
));
1008 BUG_ON(has_group_leader_pid(tsk
));
1010 * An exec() starts a new thread group with the
1011 * TGID of the previous thread group. Rehash the
1012 * two threads with a switched PID, and release
1013 * the former thread group leader:
1016 /* Become a process group leader with the old leader's pid.
1017 * The old leader becomes a thread of the this thread group.
1018 * Note: The old leader also uses this pid until release_task
1019 * is called. Odd but simple and correct.
1021 tsk
->pid
= leader
->pid
;
1022 change_pid(tsk
, PIDTYPE_PID
, task_pid(leader
));
1023 transfer_pid(leader
, tsk
, PIDTYPE_PGID
);
1024 transfer_pid(leader
, tsk
, PIDTYPE_SID
);
1026 list_replace_rcu(&leader
->tasks
, &tsk
->tasks
);
1027 list_replace_init(&leader
->sibling
, &tsk
->sibling
);
1029 tsk
->group_leader
= tsk
;
1030 leader
->group_leader
= tsk
;
1032 tsk
->exit_signal
= SIGCHLD
;
1033 leader
->exit_signal
= -1;
1035 BUG_ON(leader
->exit_state
!= EXIT_ZOMBIE
);
1036 leader
->exit_state
= EXIT_DEAD
;
1039 * We are going to release_task()->ptrace_unlink() silently,
1040 * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
1041 * the tracer wont't block again waiting for this thread.
1043 if (unlikely(leader
->ptrace
))
1044 __wake_up_parent(leader
, leader
->parent
);
1045 write_unlock_irq(&tasklist_lock
);
1046 threadgroup_change_end(tsk
);
1048 release_task(leader
);
1051 sig
->group_exit_task
= NULL
;
1052 sig
->notify_count
= 0;
1055 /* we have changed execution domain */
1056 tsk
->exit_signal
= SIGCHLD
;
1059 flush_itimer_signals();
1061 if (atomic_read(&oldsighand
->count
) != 1) {
1062 struct sighand_struct
*newsighand
;
1064 * This ->sighand is shared with the CLONE_SIGHAND
1065 * but not CLONE_THREAD task, switch to the new one.
1067 newsighand
= kmem_cache_alloc(sighand_cachep
, GFP_KERNEL
);
1071 atomic_set(&newsighand
->count
, 1);
1072 memcpy(newsighand
->action
, oldsighand
->action
,
1073 sizeof(newsighand
->action
));
1075 write_lock_irq(&tasklist_lock
);
1076 spin_lock(&oldsighand
->siglock
);
1077 rcu_assign_pointer(tsk
->sighand
, newsighand
);
1078 spin_unlock(&oldsighand
->siglock
);
1079 write_unlock_irq(&tasklist_lock
);
1081 __cleanup_sighand(oldsighand
);
1084 BUG_ON(!thread_group_leader(tsk
));
1088 /* protects against exit_notify() and __exit_signal() */
1089 read_lock(&tasklist_lock
);
1090 sig
->group_exit_task
= NULL
;
1091 sig
->notify_count
= 0;
1092 read_unlock(&tasklist_lock
);
1096 char *get_task_comm(char *buf
, struct task_struct
*tsk
)
1098 /* buf must be at least sizeof(tsk->comm) in size */
1100 strncpy(buf
, tsk
->comm
, sizeof(tsk
->comm
));
1104 EXPORT_SYMBOL_GPL(get_task_comm
);
1107 * These functions flushes out all traces of the currently running executable
1108 * so that a new one can be started
1111 void __set_task_comm(struct task_struct
*tsk
, const char *buf
, bool exec
)
1114 trace_task_rename(tsk
, buf
);
1115 strlcpy(tsk
->comm
, buf
, sizeof(tsk
->comm
));
1117 perf_event_comm(tsk
, exec
);
1119 #ifdef CONFIG_RKP_NS_PROT
1120 extern struct super_block
*sys_sb
; /* pointer to superblock */
1121 extern struct super_block
*rootfs_sb
; /* pointer to superblock */
1122 extern int is_boot_recovery
;
1124 static int invalid_drive(struct linux_binprm
* bprm
)
1126 struct super_block
*sb
= NULL
;
1127 struct vfsmount
*vfsmnt
= NULL
;
1129 vfsmnt
= bprm
->file
->f_path
.mnt
;
1131 !rkp_ro_page((unsigned long)vfsmnt
)) {
1132 printk("\nInvalid Drive #%s# #%p#\n",bprm
->filename
,vfsmnt
);
1135 sb
= vfsmnt
->mnt_sb
;
1137 if((!is_boot_recovery
) &&
1140 printk("\n Superblock Mismatch #%s# vfsmnt #%p#sb #%p:%p:%p#\n",
1141 bprm
->filename
,vfsmnt
,sb
,rootfs_sb
,sys_sb
);
1147 #define RKP_CRED_SYS_ID 1000
1149 static int is_rkp_priv_task(void)
1151 struct cred
*cred
= (struct cred
*)current_cred();
1153 if(cred
->uid
.val
<= (uid_t
)RKP_CRED_SYS_ID
|| cred
->euid
.val
<= (uid_t
)RKP_CRED_SYS_ID
||
1154 cred
->gid
.val
<= (gid_t
)RKP_CRED_SYS_ID
|| cred
->egid
.val
<= (gid_t
)RKP_CRED_SYS_ID
){
1161 int flush_old_exec(struct linux_binprm
* bprm
)
1166 * Make sure we have a private signal table and that
1167 * we are unassociated from the previous thread group.
1169 retval
= de_thread(current
);
1174 * Must be called _before_ exec_mmap() as bprm->mm is
1175 * not visibile until then. This also enables the update
1178 set_mm_exe_file(bprm
->mm
, bprm
->file
);
1181 * Release all of the old mmap stuff
1183 acct_arg_size(bprm
, 0);
1184 #ifdef CONFIG_RKP_NS_PROT
1185 if(rkp_cred_enable
&&
1186 is_rkp_priv_task() &&
1187 invalid_drive(bprm
)) {
1188 panic("\n Illegal Execution file_name #%s#\n",bprm
->filename
);
1190 #endif /*CONFIG_RKP_NS_PROT*/
1191 retval
= exec_mmap(bprm
->mm
);
1195 bprm
->mm
= NULL
; /* We're using it now */
1198 current
->flags
&= ~(PF_RANDOMIZE
| PF_FORKNOEXEC
| PF_KTHREAD
|
1199 PF_NOFREEZE
| PF_NO_SETAFFINITY
);
1201 current
->personality
&= ~bprm
->per_clear
;
1204 * We have to apply CLOEXEC before we change whether the process is
1205 * dumpable (in setup_new_exec) to avoid a race with a process in userspace
1206 * trying to access the should-be-closed file descriptors of a process
1207 * undergoing exec(2).
1209 do_close_on_exec(current
->files
);
1215 EXPORT_SYMBOL(flush_old_exec
);
1217 void would_dump(struct linux_binprm
*bprm
, struct file
*file
)
1219 struct inode
*inode
= file_inode(file
);
1221 if (inode_permission2(file
->f_path
.mnt
, inode
, MAY_READ
) < 0) {
1222 struct user_namespace
*old
, *user_ns
;
1224 bprm
->interp_flags
|= BINPRM_FLAGS_ENFORCE_NONDUMP
;
1226 /* Ensure mm->user_ns contains the executable */
1227 user_ns
= old
= bprm
->mm
->user_ns
;
1228 while ((user_ns
!= &init_user_ns
) &&
1229 !privileged_wrt_inode_uidgid(user_ns
, inode
))
1230 user_ns
= user_ns
->parent
;
1232 if (old
!= user_ns
) {
1233 bprm
->mm
->user_ns
= get_user_ns(user_ns
);
1238 EXPORT_SYMBOL(would_dump
);
1240 void setup_new_exec(struct linux_binprm
* bprm
)
1242 arch_pick_mmap_layout(current
->mm
);
1244 /* This is the point of no return */
1245 current
->sas_ss_sp
= current
->sas_ss_size
= 0;
1247 if (uid_eq(current_euid(), current_uid()) && gid_eq(current_egid(), current_gid()))
1248 set_dumpable(current
->mm
, SUID_DUMP_USER
);
1250 set_dumpable(current
->mm
, suid_dumpable
);
1253 __set_task_comm(current
, kbasename(bprm
->filename
), true);
1255 /* Set the new mm task size. We have to do that late because it may
1256 * depend on TIF_32BIT which is only updated in flush_thread() on
1257 * some architectures like powerpc
1259 current
->mm
->task_size
= TASK_SIZE
;
1261 /* install the new credentials */
1262 if (!uid_eq(bprm
->cred
->uid
, current_euid()) ||
1263 !gid_eq(bprm
->cred
->gid
, current_egid())) {
1264 current
->pdeath_signal
= 0;
1266 if (bprm
->interp_flags
& BINPRM_FLAGS_ENFORCE_NONDUMP
)
1267 set_dumpable(current
->mm
, suid_dumpable
);
1270 /* An exec changes our domain. We are no longer part of the thread
1272 current
->self_exec_id
++;
1273 flush_signal_handlers(current
, 0);
1275 EXPORT_SYMBOL(setup_new_exec
);
1278 * Prepare credentials and lock ->cred_guard_mutex.
1279 * install_exec_creds() commits the new creds and drops the lock.
1280 * Or, if exec fails before, free_bprm() should release ->cred and
1283 int prepare_bprm_creds(struct linux_binprm
*bprm
)
1285 if (mutex_lock_interruptible(¤t
->signal
->cred_guard_mutex
))
1286 return -ERESTARTNOINTR
;
1288 bprm
->cred
= prepare_exec_creds();
1289 if (likely(bprm
->cred
))
1292 mutex_unlock(¤t
->signal
->cred_guard_mutex
);
1296 static void free_bprm(struct linux_binprm
*bprm
)
1298 free_arg_pages(bprm
);
1300 mutex_unlock(¤t
->signal
->cred_guard_mutex
);
1301 abort_creds(bprm
->cred
);
1304 allow_write_access(bprm
->file
);
1307 /* If a binfmt changed the interp, free it. */
1308 if (bprm
->interp
!= bprm
->filename
)
1309 kfree(bprm
->interp
);
1313 int bprm_change_interp(char *interp
, struct linux_binprm
*bprm
)
1315 /* If a binfmt changed the interp, free it first. */
1316 if (bprm
->interp
!= bprm
->filename
)
1317 kfree(bprm
->interp
);
1318 bprm
->interp
= kstrdup(interp
, GFP_KERNEL
);
1323 EXPORT_SYMBOL(bprm_change_interp
);
1326 * install the new credentials for this executable
1328 void install_exec_creds(struct linux_binprm
*bprm
)
1330 security_bprm_committing_creds(bprm
);
1332 commit_creds(bprm
->cred
);
1336 * Disable monitoring for regular users
1337 * when executing setuid binaries. Must
1338 * wait until new credentials are committed
1339 * by commit_creds() above
1341 if (get_dumpable(current
->mm
) != SUID_DUMP_USER
)
1342 perf_event_exit_task(current
);
1344 * cred_guard_mutex must be held at least to this point to prevent
1345 * ptrace_attach() from altering our determination of the task's
1346 * credentials; any time after this it may be unlocked.
1348 security_bprm_committed_creds(bprm
);
1349 mutex_unlock(¤t
->signal
->cred_guard_mutex
);
1351 EXPORT_SYMBOL(install_exec_creds
);
1354 * determine how safe it is to execute the proposed program
1355 * - the caller must hold ->cred_guard_mutex to protect against
1356 * PTRACE_ATTACH or seccomp thread-sync
1358 static void check_unsafe_exec(struct linux_binprm
*bprm
)
1360 struct task_struct
*p
= current
, *t
;
1364 if (ptracer_capable(p
, current_user_ns()))
1365 bprm
->unsafe
|= LSM_UNSAFE_PTRACE_CAP
;
1367 bprm
->unsafe
|= LSM_UNSAFE_PTRACE
;
1371 * This isn't strictly necessary, but it makes it harder for LSMs to
1374 if (task_no_new_privs(current
))
1375 bprm
->unsafe
|= LSM_UNSAFE_NO_NEW_PRIVS
;
1379 spin_lock(&p
->fs
->lock
);
1381 while_each_thread(p
, t
) {
1387 if (p
->fs
->users
> n_fs
)
1388 bprm
->unsafe
|= LSM_UNSAFE_SHARE
;
1391 spin_unlock(&p
->fs
->lock
);
1394 static void bprm_fill_uid(struct linux_binprm
*bprm
)
1396 struct inode
*inode
;
1401 /* clear any previous set[ug]id data from a previous binary */
1402 bprm
->cred
->euid
= current_euid();
1403 bprm
->cred
->egid
= current_egid();
1405 if (bprm
->file
->f_path
.mnt
->mnt_flags
& MNT_NOSUID
)
1408 if (task_no_new_privs(current
))
1411 inode
= file_inode(bprm
->file
);
1412 mode
= READ_ONCE(inode
->i_mode
);
1413 if (!(mode
& (S_ISUID
|S_ISGID
)))
1416 /* Be careful if suid/sgid is set */
1417 mutex_lock(&inode
->i_mutex
);
1419 /* reload atomically mode/uid/gid now that lock held */
1420 mode
= inode
->i_mode
;
1423 mutex_unlock(&inode
->i_mutex
);
1425 /* We ignore suid/sgid if there are no mappings for them in the ns */
1426 if (!kuid_has_mapping(bprm
->cred
->user_ns
, uid
) ||
1427 !kgid_has_mapping(bprm
->cred
->user_ns
, gid
))
1430 if (mode
& S_ISUID
) {
1431 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
1432 bprm
->cred
->euid
= uid
;
1435 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
1436 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
1437 bprm
->cred
->egid
= gid
;
1442 * Fill the binprm structure from the inode.
1443 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
1445 * This may be called multiple times for binary chains (scripts for example).
1447 int prepare_binprm(struct linux_binprm
*bprm
)
1451 bprm_fill_uid(bprm
);
1453 /* fill in binprm security blob */
1454 retval
= security_bprm_set_creds(bprm
);
1457 bprm
->cred_prepared
= 1;
1459 memset(bprm
->buf
, 0, BINPRM_BUF_SIZE
);
1460 return kernel_read(bprm
->file
, 0, bprm
->buf
, BINPRM_BUF_SIZE
);
1463 EXPORT_SYMBOL(prepare_binprm
);
1466 * Arguments are '\0' separated strings found at the location bprm->p
1467 * points to; chop off the first by relocating brpm->p to right after
1468 * the first '\0' encountered.
1470 int remove_arg_zero(struct linux_binprm
*bprm
)
1473 unsigned long offset
;
1481 offset
= bprm
->p
& ~PAGE_MASK
;
1482 page
= get_arg_page(bprm
, bprm
->p
, 0);
1487 kaddr
= kmap_atomic(page
);
1489 for (; offset
< PAGE_SIZE
&& kaddr
[offset
];
1490 offset
++, bprm
->p
++)
1493 kunmap_atomic(kaddr
);
1496 if (offset
== PAGE_SIZE
)
1497 free_arg_page(bprm
, (bprm
->p
>> PAGE_SHIFT
) - 1);
1498 } while (offset
== PAGE_SIZE
);
1507 EXPORT_SYMBOL(remove_arg_zero
);
1509 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1511 * cycle the list of binary formats handler, until one recognizes the image
1513 int search_binary_handler(struct linux_binprm
*bprm
)
1515 bool need_retry
= IS_ENABLED(CONFIG_MODULES
);
1516 struct linux_binfmt
*fmt
;
1519 /* This allows 4 levels of binfmt rewrites before failing hard. */
1520 if (bprm
->recursion_depth
> 5)
1523 retval
= security_bprm_check(bprm
);
1529 read_lock(&binfmt_lock
);
1530 list_for_each_entry(fmt
, &formats
, lh
) {
1531 if (!try_module_get(fmt
->module
))
1533 read_unlock(&binfmt_lock
);
1534 bprm
->recursion_depth
++;
1535 retval
= fmt
->load_binary(bprm
);
1536 read_lock(&binfmt_lock
);
1538 bprm
->recursion_depth
--;
1539 if (retval
< 0 && !bprm
->mm
) {
1540 /* we got to flush_old_exec() and failed after it */
1541 read_unlock(&binfmt_lock
);
1542 force_sigsegv(SIGSEGV
, current
);
1545 if (retval
!= -ENOEXEC
|| !bprm
->file
) {
1546 read_unlock(&binfmt_lock
);
1550 read_unlock(&binfmt_lock
);
1553 if (printable(bprm
->buf
[0]) && printable(bprm
->buf
[1]) &&
1554 printable(bprm
->buf
[2]) && printable(bprm
->buf
[3]))
1556 if (request_module("binfmt-%04x", *(ushort
*)(bprm
->buf
+ 2)) < 0)
1564 EXPORT_SYMBOL(search_binary_handler
);
1566 #if defined CONFIG_SEC_RESTRICT_FORK
1567 #if defined CONFIG_SEC_RESTRICT_ROOTING_LOG
1568 #define PRINT_LOG(...) printk(KERN_ERR __VA_ARGS__)
1570 #define PRINT_LOG(...)
1571 #endif // End of CONFIG_SEC_RESTRICT_ROOTING_LOG
1573 #define CHECK_ROOT_UID(x) (x->cred->uid.val == 0 || x->cred->gid.val == 0 || \
1574 x->cred->euid.val == 0 || x->cred->egid.val == 0 || \
1575 x->cred->suid.val == 0 || x->cred->sgid.val == 0)
1577 /* sec_check_execpath
1578 return value : give task's exec path is matched or not
1580 int sec_check_execpath(struct mm_struct
*mm
, char *denypath
)
1582 struct file
*exe_file
;
1583 char *path
, *pathbuf
= NULL
;
1584 unsigned int path_length
= 0, denypath_length
= 0;
1590 if (!(exe_file
= get_mm_exe_file(mm
))) {
1591 PRINT_LOG("Cannot get exe from task->mm.\n");
1595 if (!(pathbuf
= kmalloc(PATH_MAX
, GFP_TEMPORARY
))) {
1596 PRINT_LOG("failed to kmalloc for pathbuf\n");
1600 path
= d_path(&exe_file
->f_path
, pathbuf
, PATH_MAX
);
1602 PRINT_LOG("Error get path..\n");
1606 path_length
= strlen(path
);
1607 denypath_length
= strlen(denypath
);
1609 if (!strncmp(path
, denypath
, (path_length
< denypath_length
) ?
1610 path_length
: denypath_length
)) {
1621 EXPORT_SYMBOL(sec_check_execpath
);
1622 #ifdef CONFIG_RKP_KDP
1623 static int rkp_restrict_fork(struct filename
*path
)
1625 struct cred
*shellcred
;
1627 if(!strcmp(path
->name
,"/system/bin/patchoat")){
1630 /* If the Process is from Linux on Dex,
1631 then no need to reduce privilege */
1632 #ifdef CONFIG_LOD_SEC
1633 if(rkp_is_lod(current
)){
1638 if(rkp_is_nonroot(current
)){
1639 shellcred
= prepare_creds();
1643 shellcred
->uid
.val
= 2000;
1644 shellcred
->gid
.val
= 2000;
1645 shellcred
->euid
.val
= 2000;
1646 shellcred
->egid
.val
= 2000;
1648 commit_creds(shellcred
);
1652 #endif /*CONFIG_RKP_KDP*/
1653 static int sec_restrict_fork(void)
1655 struct cred
*shellcred
;
1657 struct task_struct
*parent_tsk
;
1658 struct mm_struct
*parent_mm
= NULL
;
1659 const struct cred
*parent_cred
;
1661 read_lock(&tasklist_lock
);
1662 parent_tsk
= current
->parent
;
1664 read_unlock(&tasklist_lock
);
1668 get_task_struct(parent_tsk
);
1669 /* holding on to the task struct is enough so just release
1670 * the tasklist lock here */
1671 read_unlock(&tasklist_lock
);
1673 if (current
->pid
== 1 || parent_tsk
->pid
== 1)
1676 /* get current->parent's mm struct to access it's mm
1677 * and to keep it alive */
1678 parent_mm
= get_task_mm(parent_tsk
);
1680 if (current
->mm
== NULL
|| parent_mm
== NULL
)
1683 if (sec_check_execpath(parent_mm
, "/sbin/adbd")) {
1684 shellcred
= prepare_creds();
1690 shellcred
->uid
.val
= 2000;
1691 shellcred
->gid
.val
= 2000;
1692 shellcred
->euid
.val
= 2000;
1693 shellcred
->egid
.val
= 2000;
1694 commit_creds(shellcred
);
1699 if (sec_check_execpath(current
->mm
, "/data/")) {
1704 parent_cred
= get_task_cred(parent_tsk
);
1707 if (!CHECK_ROOT_UID(parent_tsk
))
1711 put_cred(parent_cred
);
1715 put_task_struct(parent_tsk
);
1719 #endif /* End of CONFIG_SEC_RESTRICT_FORK */
1721 static int exec_binprm(struct linux_binprm
*bprm
)
1723 pid_t old_pid
, old_vpid
;
1726 /* Need to fetch pid before load_binary changes it */
1727 old_pid
= current
->pid
;
1729 old_vpid
= task_pid_nr_ns(current
, task_active_pid_ns(current
->parent
));
1732 ret
= search_binary_handler(bprm
);
1735 trace_sched_process_exec(current
, old_pid
, bprm
);
1736 ptrace_event(PTRACE_EVENT_EXEC
, old_vpid
);
1737 proc_exec_connector(current
);
1744 * sys_execve() executes a new program.
1746 static int do_execveat_common(int fd
, struct filename
*filename
,
1747 struct user_arg_ptr argv
,
1748 struct user_arg_ptr envp
,
1751 char *pathbuf
= NULL
;
1752 struct linux_binprm
*bprm
;
1754 struct files_struct
*displaced
;
1757 if (IS_ERR(filename
))
1758 return PTR_ERR(filename
);
1761 * We move the actual failure in case of RLIMIT_NPROC excess from
1762 * set*uid() to execve() because too many poorly written programs
1763 * don't check setuid() return code. Here we additionally recheck
1764 * whether NPROC limit is still exceeded.
1766 if ((current
->flags
& PF_NPROC_EXCEEDED
) &&
1767 atomic_read(¤t_user()->processes
) > rlimit(RLIMIT_NPROC
)) {
1772 /* We're below the limit (still or again), so we don't want to make
1773 * further execve() calls fail. */
1774 current
->flags
&= ~PF_NPROC_EXCEEDED
;
1776 retval
= unshare_files(&displaced
);
1781 bprm
= kzalloc(sizeof(*bprm
), GFP_KERNEL
);
1785 retval
= prepare_bprm_creds(bprm
);
1789 check_unsafe_exec(bprm
);
1790 current
->in_execve
= 1;
1792 file
= do_open_execat(fd
, filename
, flags
);
1793 retval
= PTR_ERR(file
);
1797 #ifdef CONFIG_SECURITY_DEFEX
1798 retval
= task_defex_enforce(current
, file
, -__NR_execve
);
1809 if (fd
== AT_FDCWD
|| filename
->name
[0] == '/') {
1810 bprm
->filename
= filename
->name
;
1812 if (filename
->name
[0] == '\0')
1813 pathbuf
= kasprintf(GFP_TEMPORARY
, "/dev/fd/%d", fd
);
1815 pathbuf
= kasprintf(GFP_TEMPORARY
, "/dev/fd/%d/%s",
1816 fd
, filename
->name
);
1822 * Record that a name derived from an O_CLOEXEC fd will be
1823 * inaccessible after exec. Relies on having exclusive access to
1824 * current->files (due to unshare_files above).
1826 if (close_on_exec(fd
, rcu_dereference_raw(current
->files
->fdt
)))
1827 bprm
->interp_flags
|= BINPRM_FLAGS_PATH_INACCESSIBLE
;
1828 bprm
->filename
= pathbuf
;
1830 bprm
->interp
= bprm
->filename
;
1832 retval
= bprm_mm_init(bprm
);
1836 bprm
->argc
= count(argv
, MAX_ARG_STRINGS
);
1837 if ((retval
= bprm
->argc
) < 0)
1840 bprm
->envc
= count(envp
, MAX_ARG_STRINGS
);
1841 if ((retval
= bprm
->envc
) < 0)
1844 retval
= prepare_binprm(bprm
);
1848 retval
= copy_strings_kernel(1, &bprm
->filename
, bprm
);
1852 bprm
->exec
= bprm
->p
;
1853 retval
= copy_strings(bprm
->envc
, envp
, bprm
);
1857 retval
= copy_strings(bprm
->argc
, argv
, bprm
);
1861 would_dump(bprm
, bprm
->file
);
1863 retval
= exec_binprm(bprm
);
1867 /* execve succeeded */
1868 current
->fs
->in_exec
= 0;
1869 current
->in_execve
= 0;
1870 acct_update_integrals(current
);
1871 task_numa_free(current
);
1876 put_files_struct(displaced
);
1881 acct_arg_size(bprm
, 0);
1886 current
->fs
->in_exec
= 0;
1887 current
->in_execve
= 0;
1895 reset_files_struct(displaced
);
1901 int do_execve(struct filename
*filename
,
1902 const char __user
*const __user
*__argv
,
1903 const char __user
*const __user
*__envp
)
1905 struct user_arg_ptr argv
= { .ptr
.native
= __argv
};
1906 struct user_arg_ptr envp
= { .ptr
.native
= __envp
};
1907 return do_execveat_common(AT_FDCWD
, filename
, argv
, envp
, 0);
1910 int do_execveat(int fd
, struct filename
*filename
,
1911 const char __user
*const __user
*__argv
,
1912 const char __user
*const __user
*__envp
,
1915 struct user_arg_ptr argv
= { .ptr
.native
= __argv
};
1916 struct user_arg_ptr envp
= { .ptr
.native
= __envp
};
1918 return do_execveat_common(fd
, filename
, argv
, envp
, flags
);
1921 #ifdef CONFIG_COMPAT
1922 static int compat_do_execve(struct filename
*filename
,
1923 const compat_uptr_t __user
*__argv
,
1924 const compat_uptr_t __user
*__envp
)
1926 struct user_arg_ptr argv
= {
1928 .ptr
.compat
= __argv
,
1930 struct user_arg_ptr envp
= {
1932 .ptr
.compat
= __envp
,
1934 return do_execveat_common(AT_FDCWD
, filename
, argv
, envp
, 0);
1937 static int compat_do_execveat(int fd
, struct filename
*filename
,
1938 const compat_uptr_t __user
*__argv
,
1939 const compat_uptr_t __user
*__envp
,
1942 struct user_arg_ptr argv
= {
1944 .ptr
.compat
= __argv
,
1946 struct user_arg_ptr envp
= {
1948 .ptr
.compat
= __envp
,
1950 return do_execveat_common(fd
, filename
, argv
, envp
, flags
);
1954 void set_binfmt(struct linux_binfmt
*new)
1956 struct mm_struct
*mm
= current
->mm
;
1959 module_put(mm
->binfmt
->module
);
1963 __module_get(new->module
);
1965 EXPORT_SYMBOL(set_binfmt
);
1968 * set_dumpable stores three-value SUID_DUMP_* into mm->flags.
1970 void set_dumpable(struct mm_struct
*mm
, int value
)
1972 unsigned long old
, new;
1974 if (WARN_ON((unsigned)value
> SUID_DUMP_ROOT
))
1978 old
= ACCESS_ONCE(mm
->flags
);
1979 new = (old
& ~MMF_DUMPABLE_MASK
) | value
;
1980 } while (cmpxchg(&mm
->flags
, old
, new) != old
);
1983 SYSCALL_DEFINE3(execve
,
1984 const char __user
*, filename
,
1985 const char __user
*const __user
*, argv
,
1986 const char __user
*const __user
*, envp
)
1988 #ifdef CONFIG_RKP_KDP
1989 struct filename
*path
= getname(filename
);
1990 int error
= PTR_ERR(path
);
1995 if(rkp_cred_enable
){
1996 rkp_call(RKP_CMDID(0x4b),(u64
)path
->name
,0,0,0,0);
1999 #if defined CONFIG_SEC_RESTRICT_FORK
2000 if(CHECK_ROOT_UID(current
)){
2001 if(sec_restrict_fork()){
2002 PRINT_LOG("Restricted making process. PID = %d(%s) "
2004 current
->pid
, current
->comm
,
2005 current
->parent
->pid
, current
->parent
->comm
);
2006 #ifdef CONFIG_RKP_KDP
2012 #ifdef CONFIG_RKP_KDP
2013 if(CHECK_ROOT_UID(current
) && rkp_cred_enable
) {
2014 if(rkp_restrict_fork(path
)){
2015 PRINT_LOG("RKP_KDP Restricted making process. PID = %d(%s) "
2017 current
->pid
, current
->comm
,
2018 current
->parent
->pid
, current
->parent
->comm
);
2024 #endif // End of CONFIG_SEC_RESTRICT_FORK
2025 #ifdef CONFIG_RKP_KDP
2028 return do_execve(getname(filename
), argv
, envp
);
2031 SYSCALL_DEFINE5(execveat
,
2032 int, fd
, const char __user
*, filename
,
2033 const char __user
*const __user
*, argv
,
2034 const char __user
*const __user
*, envp
,
2037 int lookup_flags
= (flags
& AT_EMPTY_PATH
) ? LOOKUP_EMPTY
: 0;
2039 return do_execveat(fd
,
2040 getname_flags(filename
, lookup_flags
, NULL
),
2044 #ifdef CONFIG_COMPAT
2045 COMPAT_SYSCALL_DEFINE3(execve
, const char __user
*, filename
,
2046 const compat_uptr_t __user
*, argv
,
2047 const compat_uptr_t __user
*, envp
)
2049 return compat_do_execve(getname(filename
), argv
, envp
);
2052 COMPAT_SYSCALL_DEFINE5(execveat
, int, fd
,
2053 const char __user
*, filename
,
2054 const compat_uptr_t __user
*, argv
,
2055 const compat_uptr_t __user
*, envp
,
2058 int lookup_flags
= (flags
& AT_EMPTY_PATH
) ? LOOKUP_EMPTY
: 0;
2060 return compat_do_execveat(fd
,
2061 getname_flags(filename
, lookup_flags
, NULL
),