platform: fimc: fix bitwise negation of a boolean expression
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / fs / exec.c
1 /*
2 * linux/fs/exec.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7 /*
8 * #!-checking implemented by tytso.
9 */
10 /*
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.
14 *
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.
17 *
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
22 * formats.
23 */
24
25 #include <linux/slab.h>
26 #include <linux/file.h>
27 #include <linux/fdtable.h>
28 #include <linux/mm.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>
60
61 #include <asm/uaccess.h>
62 #include <asm/mmu_context.h>
63 #include <asm/tlb.h>
64
65 #include <trace/events/task.h>
66 #include "internal.h"
67
68 #include <trace/events/sched.h>
69
70 #ifdef CONFIG_SECURITY_DEFEX
71 #include <linux/defex.h>
72 #endif
73
74 #ifdef CONFIG_RKP_KDP
75 #define rkp_is_nonroot(x) ((x->cred->type)>>1 & 1)
76 #ifdef CONFIG_LOD_SEC
77 #define rkp_is_lod(x) ((x->cred->type)>>3 & 1)
78 #endif
79 #endif /*CONFIG_RKP_KDP*/
80
81 int suid_dumpable = 0;
82
83 static LIST_HEAD(formats);
84 static DEFINE_RWLOCK(binfmt_lock);
85
86 void __register_binfmt(struct linux_binfmt * fmt, int insert)
87 {
88 BUG_ON(!fmt);
89 if (WARN_ON(!fmt->load_binary))
90 return;
91 write_lock(&binfmt_lock);
92 insert ? list_add(&fmt->lh, &formats) :
93 list_add_tail(&fmt->lh, &formats);
94 write_unlock(&binfmt_lock);
95 }
96
97 EXPORT_SYMBOL(__register_binfmt);
98
99 void unregister_binfmt(struct linux_binfmt * fmt)
100 {
101 write_lock(&binfmt_lock);
102 list_del(&fmt->lh);
103 write_unlock(&binfmt_lock);
104 }
105
106 EXPORT_SYMBOL(unregister_binfmt);
107
108 static inline void put_binfmt(struct linux_binfmt * fmt)
109 {
110 module_put(fmt->module);
111 }
112
113 bool path_noexec(const struct path *path)
114 {
115 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
116 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
117 }
118
119 #ifdef CONFIG_USELIB
120 /*
121 * Note that a shared library must be both readable and executable due to
122 * security reasons.
123 *
124 * Also note that we take the address to load from from the file itself.
125 */
126 SYSCALL_DEFINE1(uselib, const char __user *, library)
127 {
128 struct linux_binfmt *fmt;
129 struct file *file;
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,
137 };
138
139 if (IS_ERR(tmp))
140 goto out;
141
142 file = do_filp_open(AT_FDCWD, tmp, &uselib_flags);
143 putname(tmp);
144 error = PTR_ERR(file);
145 if (IS_ERR(file))
146 goto out;
147
148 error = -EINVAL;
149 if (!S_ISREG(file_inode(file)->i_mode))
150 goto exit;
151
152 error = -EACCES;
153 if (path_noexec(&file->f_path))
154 goto exit;
155
156 fsnotify_open(file);
157
158 error = -ENOEXEC;
159
160 read_lock(&binfmt_lock);
161 list_for_each_entry(fmt, &formats, lh) {
162 if (!fmt->load_shlib)
163 continue;
164 if (!try_module_get(fmt->module))
165 continue;
166 read_unlock(&binfmt_lock);
167 error = fmt->load_shlib(file);
168 read_lock(&binfmt_lock);
169 put_binfmt(fmt);
170 if (error != -ENOEXEC)
171 break;
172 }
173 read_unlock(&binfmt_lock);
174 exit:
175 fput(file);
176 out:
177 return error;
178 }
179 #endif /* #ifdef CONFIG_USELIB */
180
181 #ifdef CONFIG_MMU
182 /*
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).
187 */
188 static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
189 {
190 struct mm_struct *mm = current->mm;
191 long diff = (long)(pages - bprm->vma_pages);
192
193 if (!mm || !diff)
194 return;
195
196 bprm->vma_pages = pages;
197 add_mm_counter(mm, MM_ANONPAGES, diff);
198 }
199
200 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
201 int write)
202 {
203 struct page *page;
204 int ret;
205
206 #ifdef CONFIG_STACK_GROWSUP
207 if (write) {
208 ret = expand_downwards(bprm->vma, pos);
209 if (ret < 0)
210 return NULL;
211 }
212 #endif
213 ret = get_user_pages(current, bprm->mm, pos,
214 1, write, 1, &page, NULL);
215 if (ret <= 0)
216 return NULL;
217
218 if (write) {
219 unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
220 unsigned long ptr_size, limit;
221
222 /*
223 * Since the stack will hold pointers to the strings, we
224 * must account for them as well.
225 *
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
232 * correct size.
233 */
234 ptr_size = (bprm->argc + bprm->envc) * sizeof(void *);
235 if (ptr_size > ULONG_MAX - size)
236 goto fail;
237 size += ptr_size;
238
239 acct_arg_size(bprm, size / PAGE_SIZE);
240
241 /*
242 * We've historically supported up to 32 pages (ARG_MAX)
243 * of argument strings even with small stacks
244 */
245 if (size <= ARG_MAX)
246 return page;
247
248 /*
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.
251 * This ensures that:
252 * - the remaining binfmt code will not run out of stack space,
253 * - the program will have a reasonable amount of stack left
254 * to work from.
255 */
256 limit = _STK_LIM / 4 * 3;
257 limit = min(limit, rlimit(RLIMIT_STACK) / 4);
258 if (size > limit)
259 goto fail;
260 }
261
262 return page;
263
264 fail:
265 put_page(page);
266 return NULL;
267 }
268
269 static void put_arg_page(struct page *page)
270 {
271 put_page(page);
272 }
273
274 static void free_arg_page(struct linux_binprm *bprm, int i)
275 {
276 }
277
278 static void free_arg_pages(struct linux_binprm *bprm)
279 {
280 }
281
282 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
283 struct page *page)
284 {
285 flush_cache_page(bprm->vma, pos, page_to_pfn(page));
286 }
287
288 static int __bprm_mm_init(struct linux_binprm *bprm)
289 {
290 int err;
291 struct vm_area_struct *vma = NULL;
292 struct mm_struct *mm = bprm->mm;
293
294 bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
295 if (!vma)
296 return -ENOMEM;
297
298 down_write(&mm->mmap_sem);
299 vma->vm_mm = mm;
300
301 /*
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
305 * configured yet.
306 */
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);
313
314 err = insert_vm_struct(mm, vma);
315 if (err)
316 goto err;
317
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 *);
322 return 0;
323 err:
324 up_write(&mm->mmap_sem);
325 bprm->vma = NULL;
326 kmem_cache_free(vm_area_cachep, vma);
327 return err;
328 }
329
330 static bool valid_arg_len(struct linux_binprm *bprm, long len)
331 {
332 return len <= MAX_ARG_STRLEN;
333 }
334
335 #else
336
337 static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
338 {
339 }
340
341 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
342 int write)
343 {
344 struct page *page;
345
346 page = bprm->page[pos / PAGE_SIZE];
347 if (!page && write) {
348 page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
349 if (!page)
350 return NULL;
351 bprm->page[pos / PAGE_SIZE] = page;
352 }
353
354 return page;
355 }
356
357 static void put_arg_page(struct page *page)
358 {
359 }
360
361 static void free_arg_page(struct linux_binprm *bprm, int i)
362 {
363 if (bprm->page[i]) {
364 __free_page(bprm->page[i]);
365 bprm->page[i] = NULL;
366 }
367 }
368
369 static void free_arg_pages(struct linux_binprm *bprm)
370 {
371 int i;
372
373 for (i = 0; i < MAX_ARG_PAGES; i++)
374 free_arg_page(bprm, i);
375 }
376
377 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
378 struct page *page)
379 {
380 }
381
382 static int __bprm_mm_init(struct linux_binprm *bprm)
383 {
384 bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
385 return 0;
386 }
387
388 static bool valid_arg_len(struct linux_binprm *bprm, long len)
389 {
390 return len <= bprm->p;
391 }
392
393 #endif /* CONFIG_MMU */
394
395 /*
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().
400 */
401 static int bprm_mm_init(struct linux_binprm *bprm)
402 {
403 int err;
404 struct mm_struct *mm = NULL;
405
406 bprm->mm = mm = mm_alloc();
407 err = -ENOMEM;
408 if (!mm)
409 goto err;
410
411 err = __bprm_mm_init(bprm);
412 if (err)
413 goto err;
414
415 return 0;
416
417 err:
418 if (mm) {
419 bprm->mm = NULL;
420 mmdrop(mm);
421 }
422
423 return err;
424 }
425
426 struct user_arg_ptr {
427 #ifdef CONFIG_COMPAT
428 bool is_compat;
429 #endif
430 union {
431 const char __user *const __user *native;
432 #ifdef CONFIG_COMPAT
433 const compat_uptr_t __user *compat;
434 #endif
435 } ptr;
436 };
437
438 static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr)
439 {
440 const char __user *native;
441
442 #ifdef CONFIG_COMPAT
443 if (unlikely(argv.is_compat)) {
444 compat_uptr_t compat;
445
446 if (get_user(compat, argv.ptr.compat + nr))
447 return ERR_PTR(-EFAULT);
448
449 return compat_ptr(compat);
450 }
451 #endif
452
453 if (get_user(native, argv.ptr.native + nr))
454 return ERR_PTR(-EFAULT);
455
456 return native;
457 }
458
459 /*
460 * count() counts the number of strings in array ARGV.
461 */
462 static int count(struct user_arg_ptr argv, int max)
463 {
464 int i = 0;
465
466 if (argv.ptr.native != NULL) {
467 for (;;) {
468 const char __user *p = get_user_arg_ptr(argv, i);
469
470 if (!p)
471 break;
472
473 if (IS_ERR(p))
474 return -EFAULT;
475
476 if (i >= max)
477 return -E2BIG;
478 ++i;
479
480 if (fatal_signal_pending(current))
481 return -ERESTARTNOHAND;
482 cond_resched();
483 }
484 }
485 return i;
486 }
487
488 /*
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.
492 */
493 static int copy_strings(int argc, struct user_arg_ptr argv,
494 struct linux_binprm *bprm)
495 {
496 struct page *kmapped_page = NULL;
497 char *kaddr = NULL;
498 unsigned long kpos = 0;
499 int ret;
500
501 while (argc-- > 0) {
502 const char __user *str;
503 int len;
504 unsigned long pos;
505
506 ret = -EFAULT;
507 str = get_user_arg_ptr(argv, argc);
508 if (IS_ERR(str))
509 goto out;
510
511 len = strnlen_user(str, MAX_ARG_STRLEN);
512 if (!len)
513 goto out;
514
515 ret = -E2BIG;
516 if (!valid_arg_len(bprm, len))
517 goto out;
518
519 /* We're going to work our way backwords. */
520 pos = bprm->p;
521 str += len;
522 bprm->p -= len;
523
524 while (len > 0) {
525 int offset, bytes_to_copy;
526
527 if (fatal_signal_pending(current)) {
528 ret = -ERESTARTNOHAND;
529 goto out;
530 }
531 cond_resched();
532
533 offset = pos % PAGE_SIZE;
534 if (offset == 0)
535 offset = PAGE_SIZE;
536
537 bytes_to_copy = offset;
538 if (bytes_to_copy > len)
539 bytes_to_copy = len;
540
541 offset -= bytes_to_copy;
542 pos -= bytes_to_copy;
543 str -= bytes_to_copy;
544 len -= bytes_to_copy;
545
546 if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
547 struct page *page;
548
549 page = get_arg_page(bprm, pos, 1);
550 if (!page) {
551 ret = -E2BIG;
552 goto out;
553 }
554
555 if (kmapped_page) {
556 flush_kernel_dcache_page(kmapped_page);
557 kunmap(kmapped_page);
558 put_arg_page(kmapped_page);
559 }
560 kmapped_page = page;
561 kaddr = kmap(kmapped_page);
562 kpos = pos & PAGE_MASK;
563 flush_arg_page(bprm, kpos, kmapped_page);
564 }
565 if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
566 ret = -EFAULT;
567 goto out;
568 }
569 }
570 }
571 ret = 0;
572 out:
573 if (kmapped_page) {
574 flush_kernel_dcache_page(kmapped_page);
575 kunmap(kmapped_page);
576 put_arg_page(kmapped_page);
577 }
578 return ret;
579 }
580
581 /*
582 * Like copy_strings, but get argv and its values from kernel memory.
583 */
584 int copy_strings_kernel(int argc, const char *const *__argv,
585 struct linux_binprm *bprm)
586 {
587 int r;
588 mm_segment_t oldfs = get_fs();
589 struct user_arg_ptr argv = {
590 .ptr.native = (const char __user *const __user *)__argv,
591 };
592
593 set_fs(KERNEL_DS);
594 r = copy_strings(argc, argv, bprm);
595 set_fs(oldfs);
596
597 return r;
598 }
599 EXPORT_SYMBOL(copy_strings_kernel);
600
601 #ifdef CONFIG_MMU
602
603 /*
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:
607 *
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.
614 */
615 static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
616 {
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;
624
625 BUG_ON(new_start > new_end);
626
627 /*
628 * ensure there are no vmas between where we want to go
629 * and where we are
630 */
631 if (vma != find_vma(mm, new_start))
632 return -EFAULT;
633
634 /*
635 * cover the whole range: [new_start, old_end)
636 */
637 if (vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL))
638 return -ENOMEM;
639
640 /*
641 * move the page tables downwards, on failure we rely on
642 * process cleanup to remove whatever mess we made.
643 */
644 if (length != move_page_tables(vma, old_start,
645 vma, new_start, length, false))
646 return -ENOMEM;
647
648 lru_add_drain();
649 tlb_gather_mmu(&tlb, mm, old_start, old_end);
650 if (new_end > old_start) {
651 /*
652 * when the old and new regions overlap clear from new_end.
653 */
654 free_pgd_range(&tlb, new_end, old_end, new_end,
655 vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING);
656 } else {
657 /*
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.
662 */
663 free_pgd_range(&tlb, old_start, old_end, new_end,
664 vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING);
665 }
666 tlb_finish_mmu(&tlb, old_start, old_end);
667
668 /*
669 * Shrink the vma to just the new range. Always succeeds.
670 */
671 vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
672
673 return 0;
674 }
675
676 /*
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.
679 */
680 int setup_arg_pages(struct linux_binprm *bprm,
681 unsigned long stack_top,
682 int executable_stack)
683 {
684 unsigned long ret;
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;
694
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;
700
701 /* Add space for stack randomization. */
702 stack_base += (STACK_RND_MASK << PAGE_SHIFT);
703
704 /* Make sure we didn't let the argument array grow too large. */
705 if (vma->vm_end - vma->vm_start > stack_base)
706 return -ENOMEM;
707
708 stack_base = PAGE_ALIGN(stack_top - stack_base);
709
710 stack_shift = vma->vm_start - stack_base;
711 mm->arg_start = bprm->p - stack_shift;
712 bprm->p = vma->vm_end - stack_shift;
713 #else
714 stack_top = arch_align_stack(stack_top);
715 stack_top = PAGE_ALIGN(stack_top);
716
717 if (unlikely(stack_top < mmap_min_addr) ||
718 unlikely(vma->vm_end - vma->vm_start >= stack_top - mmap_min_addr))
719 return -ENOMEM;
720
721 stack_shift = vma->vm_end - stack_top;
722
723 bprm->p -= stack_shift;
724 mm->arg_start = bprm->p;
725 #endif
726
727 if (bprm->loader)
728 bprm->loader -= stack_shift;
729 bprm->exec -= stack_shift;
730
731 down_write(&mm->mmap_sem);
732 vm_flags = VM_STACK_FLAGS;
733
734 /*
735 * Adjust stack execute permissions; explicitly enable for
736 * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
737 * (arch default) otherwise.
738 */
739 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
740 vm_flags |= VM_EXEC;
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;
745
746 ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
747 vm_flags);
748 if (ret)
749 goto out_unlock;
750 BUG_ON(prev != vma);
751
752 /* Move stack pages down in memory. */
753 if (stack_shift) {
754 ret = shift_arg_pages(vma, stack_shift);
755 if (ret)
756 goto out_unlock;
757 }
758
759 /* mprotect_fixup is overkill to remove the temporary stack flags */
760 vma->vm_flags &= ~VM_STACK_INCOMPLETE_SETUP;
761
762 stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
763 stack_size = vma->vm_end - vma->vm_start;
764 /*
765 * Align this down to a page boundary as expand_stack
766 * will align it up.
767 */
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;
772 else
773 stack_base = vma->vm_end + stack_expand;
774 #else
775 if (stack_size + stack_expand > rlim_stack)
776 stack_base = vma->vm_end - rlim_stack;
777 else
778 stack_base = vma->vm_start - stack_expand;
779 #endif
780 current->mm->start_stack = bprm->p;
781 ret = expand_stack(vma, stack_base);
782 if (ret)
783 ret = -EFAULT;
784
785 out_unlock:
786 up_write(&mm->mmap_sem);
787 return ret;
788 }
789 EXPORT_SYMBOL(setup_arg_pages);
790
791 #endif /* CONFIG_MMU */
792
793 static struct file *do_open_execat(int fd, struct filename *name, int flags)
794 {
795 struct file *file;
796 int err;
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,
802 };
803
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;
810
811 file = do_filp_open(fd, name, &open_exec_flags);
812 if (IS_ERR(file))
813 goto out;
814
815 err = -EACCES;
816 if (!S_ISREG(file_inode(file)->i_mode))
817 goto exit;
818
819 if (path_noexec(&file->f_path))
820 goto exit;
821
822 err = deny_write_access(file);
823 if (err)
824 goto exit;
825
826 if (name->name[0] != '\0')
827 fsnotify_open(file);
828
829 out:
830 return file;
831
832 exit:
833 fput(file);
834 return ERR_PTR(err);
835 }
836
837 struct file *open_exec(const char *name)
838 {
839 struct filename *filename = getname_kernel(name);
840 struct file *f = ERR_CAST(filename);
841
842 if (!IS_ERR(filename)) {
843 f = do_open_execat(AT_FDCWD, filename, 0);
844 putname(filename);
845 }
846 return f;
847 }
848 EXPORT_SYMBOL(open_exec);
849
850 int kernel_read(struct file *file, loff_t offset,
851 char *addr, unsigned long count)
852 {
853 mm_segment_t old_fs;
854 loff_t pos = offset;
855 int result;
856
857 old_fs = get_fs();
858 set_fs(get_ds());
859 /* The cast to a user pointer is valid due to the set_fs() */
860 result = vfs_read(file, (void __user *)addr, count, &pos);
861 set_fs(old_fs);
862 return result;
863 }
864
865 EXPORT_SYMBOL(kernel_read);
866
867 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
868 {
869 ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
870 if (res > 0)
871 flush_icache_range(addr, addr + len);
872 return res;
873 }
874 EXPORT_SYMBOL(read_code);
875
876 static int exec_mmap(struct mm_struct *mm)
877 {
878 struct task_struct *tsk;
879 struct mm_struct *old_mm, *active_mm;
880
881 /* Notify parent that we're no longer interested in the old VM */
882 tsk = current;
883 old_mm = current->mm;
884 mm_release(tsk, old_mm);
885
886 if (old_mm) {
887 sync_mm_rss(old_mm);
888 /*
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.
893 */
894 down_read(&old_mm->mmap_sem);
895 if (unlikely(old_mm->core_state)) {
896 up_read(&old_mm->mmap_sem);
897 return -EINTR;
898 }
899 }
900 task_lock(tsk);
901 active_mm = tsk->active_mm;
902 tsk->mm = mm;
903 tsk->active_mm = mm;
904 activate_mm(active_mm, mm);
905 tsk->mm->vmacache_seqnum = 0;
906 vmacache_flush(tsk);
907 #ifdef CONFIG_RKP_KDP
908 if(rkp_cred_enable){
909 rkp_call(RKP_CMDID(0x43),(unsigned long long)current_cred(), (unsigned long long)mm->pgd,0,0,0);
910 }
911 #endif /*CONFIG_RKP_KDP*/
912 task_unlock(tsk);
913 if (old_mm) {
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);
918 mmput(old_mm);
919 return 0;
920 }
921 mmdrop(active_mm);
922 return 0;
923 }
924
925 /*
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().)
930 */
931 static int de_thread(struct task_struct *tsk)
932 {
933 struct signal_struct *sig = tsk->signal;
934 struct sighand_struct *oldsighand = tsk->sighand;
935 spinlock_t *lock = &oldsighand->siglock;
936
937 if (thread_group_empty(tsk))
938 goto no_thread_group;
939
940 /*
941 * Kill all other threads in the thread group.
942 */
943 spin_lock_irq(lock);
944 if (signal_group_exit(sig)) {
945 /*
946 * Another group action in progress, just
947 * return so that the signal is processed.
948 */
949 spin_unlock_irq(lock);
950 return -EAGAIN;
951 }
952
953 sig->group_exit_task = tsk;
954 sig->notify_count = zap_other_threads(tsk);
955 if (!thread_group_leader(tsk))
956 sig->notify_count--;
957
958 while (sig->notify_count) {
959 __set_current_state(TASK_KILLABLE);
960 spin_unlock_irq(lock);
961 schedule();
962 if (unlikely(__fatal_signal_pending(tsk)))
963 goto killed;
964 spin_lock_irq(lock);
965 }
966 spin_unlock_irq(lock);
967
968 /*
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:
972 */
973 if (!thread_group_leader(tsk)) {
974 struct task_struct *leader = tsk->group_leader;
975
976 for (;;) {
977 threadgroup_change_begin(tsk);
978 write_lock_irq(&tasklist_lock);
979 /*
980 * Do this under tasklist_lock to ensure that
981 * exit_notify() can't miss ->group_exit_task
982 */
983 sig->notify_count = -1;
984 if (likely(leader->exit_state))
985 break;
986 __set_current_state(TASK_KILLABLE);
987 write_unlock_irq(&tasklist_lock);
988 threadgroup_change_end(tsk);
989 schedule();
990 if (unlikely(__fatal_signal_pending(tsk)))
991 goto killed;
992 }
993
994 /*
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).
1003 */
1004 tsk->start_time = leader->start_time;
1005 tsk->real_start_time = leader->real_start_time;
1006
1007 BUG_ON(!same_thread_group(leader, tsk));
1008 BUG_ON(has_group_leader_pid(tsk));
1009 /*
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:
1014 */
1015
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.
1020 */
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);
1025
1026 list_replace_rcu(&leader->tasks, &tsk->tasks);
1027 list_replace_init(&leader->sibling, &tsk->sibling);
1028
1029 tsk->group_leader = tsk;
1030 leader->group_leader = tsk;
1031
1032 tsk->exit_signal = SIGCHLD;
1033 leader->exit_signal = -1;
1034
1035 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
1036 leader->exit_state = EXIT_DEAD;
1037
1038 /*
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.
1042 */
1043 if (unlikely(leader->ptrace))
1044 __wake_up_parent(leader, leader->parent);
1045 write_unlock_irq(&tasklist_lock);
1046 threadgroup_change_end(tsk);
1047
1048 release_task(leader);
1049 }
1050
1051 sig->group_exit_task = NULL;
1052 sig->notify_count = 0;
1053
1054 no_thread_group:
1055 /* we have changed execution domain */
1056 tsk->exit_signal = SIGCHLD;
1057
1058 exit_itimers(sig);
1059 flush_itimer_signals();
1060
1061 if (atomic_read(&oldsighand->count) != 1) {
1062 struct sighand_struct *newsighand;
1063 /*
1064 * This ->sighand is shared with the CLONE_SIGHAND
1065 * but not CLONE_THREAD task, switch to the new one.
1066 */
1067 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1068 if (!newsighand)
1069 return -ENOMEM;
1070
1071 atomic_set(&newsighand->count, 1);
1072 memcpy(newsighand->action, oldsighand->action,
1073 sizeof(newsighand->action));
1074
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);
1080
1081 __cleanup_sighand(oldsighand);
1082 }
1083
1084 BUG_ON(!thread_group_leader(tsk));
1085 return 0;
1086
1087 killed:
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);
1093 return -EAGAIN;
1094 }
1095
1096 char *get_task_comm(char *buf, struct task_struct *tsk)
1097 {
1098 /* buf must be at least sizeof(tsk->comm) in size */
1099 task_lock(tsk);
1100 strncpy(buf, tsk->comm, sizeof(tsk->comm));
1101 task_unlock(tsk);
1102 return buf;
1103 }
1104 EXPORT_SYMBOL_GPL(get_task_comm);
1105
1106 /*
1107 * These functions flushes out all traces of the currently running executable
1108 * so that a new one can be started
1109 */
1110
1111 void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
1112 {
1113 task_lock(tsk);
1114 trace_task_rename(tsk, buf);
1115 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
1116 task_unlock(tsk);
1117 perf_event_comm(tsk, exec);
1118 }
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;
1123
1124 static int invalid_drive(struct linux_binprm * bprm)
1125 {
1126 struct super_block *sb = NULL;
1127 struct vfsmount *vfsmnt = NULL;
1128
1129 vfsmnt = bprm->file->f_path.mnt;
1130 if(!vfsmnt ||
1131 !rkp_ro_page((unsigned long)vfsmnt)) {
1132 printk("\nInvalid Drive #%s# #%p#\n",bprm->filename,vfsmnt);
1133 return 1;
1134 }
1135 sb = vfsmnt->mnt_sb;
1136
1137 if((!is_boot_recovery) &&
1138 sb != rootfs_sb
1139 && sb != sys_sb) {
1140 printk("\n Superblock Mismatch #%s# vfsmnt #%p#sb #%p:%p:%p#\n",
1141 bprm->filename,vfsmnt,sb,rootfs_sb,sys_sb);
1142 return 1;
1143 }
1144
1145 return 0;
1146 }
1147 #define RKP_CRED_SYS_ID 1000
1148
1149 static int is_rkp_priv_task(void)
1150 {
1151 struct cred *cred = (struct cred *)current_cred();
1152
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 ){
1155 return 1;
1156 }
1157 return 0;
1158 }
1159 #endif
1160
1161 int flush_old_exec(struct linux_binprm * bprm)
1162 {
1163 int retval;
1164
1165 /*
1166 * Make sure we have a private signal table and that
1167 * we are unassociated from the previous thread group.
1168 */
1169 retval = de_thread(current);
1170 if (retval)
1171 goto out;
1172
1173 /*
1174 * Must be called _before_ exec_mmap() as bprm->mm is
1175 * not visibile until then. This also enables the update
1176 * to be lockless.
1177 */
1178 set_mm_exe_file(bprm->mm, bprm->file);
1179
1180 /*
1181 * Release all of the old mmap stuff
1182 */
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);
1189 }
1190 #endif /*CONFIG_RKP_NS_PROT*/
1191 retval = exec_mmap(bprm->mm);
1192 if (retval)
1193 goto out;
1194
1195 bprm->mm = NULL; /* We're using it now */
1196
1197 set_fs(USER_DS);
1198 current->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_KTHREAD |
1199 PF_NOFREEZE | PF_NO_SETAFFINITY);
1200 flush_thread();
1201 current->personality &= ~bprm->per_clear;
1202
1203 /*
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).
1208 */
1209 do_close_on_exec(current->files);
1210 return 0;
1211
1212 out:
1213 return retval;
1214 }
1215 EXPORT_SYMBOL(flush_old_exec);
1216
1217 void would_dump(struct linux_binprm *bprm, struct file *file)
1218 {
1219 struct inode *inode = file_inode(file);
1220
1221 if (inode_permission2(file->f_path.mnt, inode, MAY_READ) < 0) {
1222 struct user_namespace *old, *user_ns;
1223
1224 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
1225
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;
1231
1232 if (old != user_ns) {
1233 bprm->mm->user_ns = get_user_ns(user_ns);
1234 put_user_ns(old);
1235 }
1236 }
1237 }
1238 EXPORT_SYMBOL(would_dump);
1239
1240 void setup_new_exec(struct linux_binprm * bprm)
1241 {
1242 arch_pick_mmap_layout(current->mm);
1243
1244 /* This is the point of no return */
1245 current->sas_ss_sp = current->sas_ss_size = 0;
1246
1247 if (uid_eq(current_euid(), current_uid()) && gid_eq(current_egid(), current_gid()))
1248 set_dumpable(current->mm, SUID_DUMP_USER);
1249 else
1250 set_dumpable(current->mm, suid_dumpable);
1251
1252 perf_event_exec();
1253 __set_task_comm(current, kbasename(bprm->filename), true);
1254
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
1258 */
1259 current->mm->task_size = TASK_SIZE;
1260
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;
1265 } else {
1266 if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
1267 set_dumpable(current->mm, suid_dumpable);
1268 }
1269
1270 /* An exec changes our domain. We are no longer part of the thread
1271 group */
1272 current->self_exec_id++;
1273 flush_signal_handlers(current, 0);
1274 }
1275 EXPORT_SYMBOL(setup_new_exec);
1276
1277 /*
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
1281 * and unlock.
1282 */
1283 int prepare_bprm_creds(struct linux_binprm *bprm)
1284 {
1285 if (mutex_lock_interruptible(&current->signal->cred_guard_mutex))
1286 return -ERESTARTNOINTR;
1287
1288 bprm->cred = prepare_exec_creds();
1289 if (likely(bprm->cred))
1290 return 0;
1291
1292 mutex_unlock(&current->signal->cred_guard_mutex);
1293 return -ENOMEM;
1294 }
1295
1296 static void free_bprm(struct linux_binprm *bprm)
1297 {
1298 free_arg_pages(bprm);
1299 if (bprm->cred) {
1300 mutex_unlock(&current->signal->cred_guard_mutex);
1301 abort_creds(bprm->cred);
1302 }
1303 if (bprm->file) {
1304 allow_write_access(bprm->file);
1305 fput(bprm->file);
1306 }
1307 /* If a binfmt changed the interp, free it. */
1308 if (bprm->interp != bprm->filename)
1309 kfree(bprm->interp);
1310 kfree(bprm);
1311 }
1312
1313 int bprm_change_interp(char *interp, struct linux_binprm *bprm)
1314 {
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);
1319 if (!bprm->interp)
1320 return -ENOMEM;
1321 return 0;
1322 }
1323 EXPORT_SYMBOL(bprm_change_interp);
1324
1325 /*
1326 * install the new credentials for this executable
1327 */
1328 void install_exec_creds(struct linux_binprm *bprm)
1329 {
1330 security_bprm_committing_creds(bprm);
1331
1332 commit_creds(bprm->cred);
1333 bprm->cred = NULL;
1334
1335 /*
1336 * Disable monitoring for regular users
1337 * when executing setuid binaries. Must
1338 * wait until new credentials are committed
1339 * by commit_creds() above
1340 */
1341 if (get_dumpable(current->mm) != SUID_DUMP_USER)
1342 perf_event_exit_task(current);
1343 /*
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.
1347 */
1348 security_bprm_committed_creds(bprm);
1349 mutex_unlock(&current->signal->cred_guard_mutex);
1350 }
1351 EXPORT_SYMBOL(install_exec_creds);
1352
1353 /*
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
1357 */
1358 static void check_unsafe_exec(struct linux_binprm *bprm)
1359 {
1360 struct task_struct *p = current, *t;
1361 unsigned n_fs;
1362
1363 if (p->ptrace) {
1364 if (ptracer_capable(p, current_user_ns()))
1365 bprm->unsafe |= LSM_UNSAFE_PTRACE_CAP;
1366 else
1367 bprm->unsafe |= LSM_UNSAFE_PTRACE;
1368 }
1369
1370 /*
1371 * This isn't strictly necessary, but it makes it harder for LSMs to
1372 * mess up.
1373 */
1374 if (task_no_new_privs(current))
1375 bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
1376
1377 t = p;
1378 n_fs = 1;
1379 spin_lock(&p->fs->lock);
1380 rcu_read_lock();
1381 while_each_thread(p, t) {
1382 if (t->fs == p->fs)
1383 n_fs++;
1384 }
1385 rcu_read_unlock();
1386
1387 if (p->fs->users > n_fs)
1388 bprm->unsafe |= LSM_UNSAFE_SHARE;
1389 else
1390 p->fs->in_exec = 1;
1391 spin_unlock(&p->fs->lock);
1392 }
1393
1394 static void bprm_fill_uid(struct linux_binprm *bprm)
1395 {
1396 struct inode *inode;
1397 unsigned int mode;
1398 kuid_t uid;
1399 kgid_t gid;
1400
1401 /* clear any previous set[ug]id data from a previous binary */
1402 bprm->cred->euid = current_euid();
1403 bprm->cred->egid = current_egid();
1404
1405 if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
1406 return;
1407
1408 if (task_no_new_privs(current))
1409 return;
1410
1411 inode = file_inode(bprm->file);
1412 mode = READ_ONCE(inode->i_mode);
1413 if (!(mode & (S_ISUID|S_ISGID)))
1414 return;
1415
1416 /* Be careful if suid/sgid is set */
1417 mutex_lock(&inode->i_mutex);
1418
1419 /* reload atomically mode/uid/gid now that lock held */
1420 mode = inode->i_mode;
1421 uid = inode->i_uid;
1422 gid = inode->i_gid;
1423 mutex_unlock(&inode->i_mutex);
1424
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))
1428 return;
1429
1430 if (mode & S_ISUID) {
1431 bprm->per_clear |= PER_CLEAR_ON_SETID;
1432 bprm->cred->euid = uid;
1433 }
1434
1435 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1436 bprm->per_clear |= PER_CLEAR_ON_SETID;
1437 bprm->cred->egid = gid;
1438 }
1439 }
1440
1441 /*
1442 * Fill the binprm structure from the inode.
1443 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
1444 *
1445 * This may be called multiple times for binary chains (scripts for example).
1446 */
1447 int prepare_binprm(struct linux_binprm *bprm)
1448 {
1449 int retval;
1450
1451 bprm_fill_uid(bprm);
1452
1453 /* fill in binprm security blob */
1454 retval = security_bprm_set_creds(bprm);
1455 if (retval)
1456 return retval;
1457 bprm->cred_prepared = 1;
1458
1459 memset(bprm->buf, 0, BINPRM_BUF_SIZE);
1460 return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
1461 }
1462
1463 EXPORT_SYMBOL(prepare_binprm);
1464
1465 /*
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.
1469 */
1470 int remove_arg_zero(struct linux_binprm *bprm)
1471 {
1472 int ret = 0;
1473 unsigned long offset;
1474 char *kaddr;
1475 struct page *page;
1476
1477 if (!bprm->argc)
1478 return 0;
1479
1480 do {
1481 offset = bprm->p & ~PAGE_MASK;
1482 page = get_arg_page(bprm, bprm->p, 0);
1483 if (!page) {
1484 ret = -EFAULT;
1485 goto out;
1486 }
1487 kaddr = kmap_atomic(page);
1488
1489 for (; offset < PAGE_SIZE && kaddr[offset];
1490 offset++, bprm->p++)
1491 ;
1492
1493 kunmap_atomic(kaddr);
1494 put_arg_page(page);
1495
1496 if (offset == PAGE_SIZE)
1497 free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
1498 } while (offset == PAGE_SIZE);
1499
1500 bprm->p++;
1501 bprm->argc--;
1502 ret = 0;
1503
1504 out:
1505 return ret;
1506 }
1507 EXPORT_SYMBOL(remove_arg_zero);
1508
1509 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1510 /*
1511 * cycle the list of binary formats handler, until one recognizes the image
1512 */
1513 int search_binary_handler(struct linux_binprm *bprm)
1514 {
1515 bool need_retry = IS_ENABLED(CONFIG_MODULES);
1516 struct linux_binfmt *fmt;
1517 int retval;
1518
1519 /* This allows 4 levels of binfmt rewrites before failing hard. */
1520 if (bprm->recursion_depth > 5)
1521 return -ELOOP;
1522
1523 retval = security_bprm_check(bprm);
1524 if (retval)
1525 return retval;
1526
1527 retval = -ENOENT;
1528 retry:
1529 read_lock(&binfmt_lock);
1530 list_for_each_entry(fmt, &formats, lh) {
1531 if (!try_module_get(fmt->module))
1532 continue;
1533 read_unlock(&binfmt_lock);
1534 bprm->recursion_depth++;
1535 retval = fmt->load_binary(bprm);
1536 read_lock(&binfmt_lock);
1537 put_binfmt(fmt);
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);
1543 return retval;
1544 }
1545 if (retval != -ENOEXEC || !bprm->file) {
1546 read_unlock(&binfmt_lock);
1547 return retval;
1548 }
1549 }
1550 read_unlock(&binfmt_lock);
1551
1552 if (need_retry) {
1553 if (printable(bprm->buf[0]) && printable(bprm->buf[1]) &&
1554 printable(bprm->buf[2]) && printable(bprm->buf[3]))
1555 return retval;
1556 if (request_module("binfmt-%04x", *(ushort *)(bprm->buf + 2)) < 0)
1557 return retval;
1558 need_retry = false;
1559 goto retry;
1560 }
1561
1562 return retval;
1563 }
1564 EXPORT_SYMBOL(search_binary_handler);
1565
1566 #if defined CONFIG_SEC_RESTRICT_FORK
1567 #if defined CONFIG_SEC_RESTRICT_ROOTING_LOG
1568 #define PRINT_LOG(...) printk(KERN_ERR __VA_ARGS__)
1569 #else
1570 #define PRINT_LOG(...)
1571 #endif // End of CONFIG_SEC_RESTRICT_ROOTING_LOG
1572
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)
1576
1577 /* sec_check_execpath
1578 return value : give task's exec path is matched or not
1579 */
1580 int sec_check_execpath(struct mm_struct *mm, char *denypath)
1581 {
1582 struct file *exe_file;
1583 char *path, *pathbuf = NULL;
1584 unsigned int path_length = 0, denypath_length = 0;
1585 int ret = 0;
1586
1587 if (mm == NULL)
1588 return 0;
1589
1590 if (!(exe_file = get_mm_exe_file(mm))) {
1591 PRINT_LOG("Cannot get exe from task->mm.\n");
1592 goto out_nofile;
1593 }
1594
1595 if (!(pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY))) {
1596 PRINT_LOG("failed to kmalloc for pathbuf\n");
1597 goto out;
1598 }
1599
1600 path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
1601 if (IS_ERR(path)) {
1602 PRINT_LOG("Error get path..\n");
1603 goto out;
1604 }
1605
1606 path_length = strlen(path);
1607 denypath_length = strlen(denypath);
1608
1609 if (!strncmp(path, denypath, (path_length < denypath_length) ?
1610 path_length : denypath_length)) {
1611 ret = 1;
1612 }
1613 out:
1614 fput(exe_file);
1615 out_nofile:
1616 if (pathbuf)
1617 kfree(pathbuf);
1618
1619 return ret;
1620 }
1621 EXPORT_SYMBOL(sec_check_execpath);
1622 #ifdef CONFIG_RKP_KDP
1623 static int rkp_restrict_fork(struct filename *path)
1624 {
1625 struct cred *shellcred;
1626
1627 if(!strcmp(path->name,"/system/bin/patchoat")){
1628 return 0 ;
1629 }
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)){
1634 return 0;
1635 }
1636 #endif
1637
1638 if(rkp_is_nonroot(current)){
1639 shellcred = prepare_creds();
1640 if (!shellcred) {
1641 return 1;
1642 }
1643 shellcred->uid.val = 2000;
1644 shellcred->gid.val = 2000;
1645 shellcred->euid.val = 2000;
1646 shellcred->egid.val = 2000;
1647
1648 commit_creds(shellcred);
1649 }
1650 return 0;
1651 }
1652 #endif /*CONFIG_RKP_KDP*/
1653 static int sec_restrict_fork(void)
1654 {
1655 struct cred *shellcred;
1656 int ret = 0;
1657 struct task_struct *parent_tsk;
1658 struct mm_struct *parent_mm = NULL;
1659 const struct cred *parent_cred;
1660
1661 read_lock(&tasklist_lock);
1662 parent_tsk = current->parent;
1663 if (!parent_tsk) {
1664 read_unlock(&tasklist_lock);
1665 return 0;
1666 }
1667
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);
1672
1673 if (current->pid == 1 || parent_tsk->pid == 1)
1674 goto out;
1675
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);
1679
1680 if (current->mm == NULL || parent_mm == NULL)
1681 goto out;
1682
1683 if (sec_check_execpath(parent_mm, "/sbin/adbd")) {
1684 shellcred = prepare_creds();
1685 if (!shellcred) {
1686 ret = 1;
1687 goto out;
1688 }
1689
1690 shellcred->uid.val = 2000;
1691 shellcred->gid.val = 2000;
1692 shellcred->euid.val = 2000;
1693 shellcred->egid.val = 2000;
1694 commit_creds(shellcred);
1695 ret = 0;
1696 goto out;
1697 }
1698
1699 if (sec_check_execpath(current->mm, "/data/")) {
1700 ret = 1;
1701 goto out;
1702 }
1703
1704 parent_cred = get_task_cred(parent_tsk);
1705 if (!parent_cred)
1706 goto out;
1707 if (!CHECK_ROOT_UID(parent_tsk))
1708 {
1709 ret = 1;
1710 }
1711 put_cred(parent_cred);
1712 out:
1713 if (parent_mm)
1714 mmput(parent_mm);
1715 put_task_struct(parent_tsk);
1716
1717 return ret;
1718 }
1719 #endif /* End of CONFIG_SEC_RESTRICT_FORK */
1720
1721 static int exec_binprm(struct linux_binprm *bprm)
1722 {
1723 pid_t old_pid, old_vpid;
1724 int ret;
1725
1726 /* Need to fetch pid before load_binary changes it */
1727 old_pid = current->pid;
1728 rcu_read_lock();
1729 old_vpid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
1730 rcu_read_unlock();
1731
1732 ret = search_binary_handler(bprm);
1733 if (ret >= 0) {
1734 audit_bprm(bprm);
1735 trace_sched_process_exec(current, old_pid, bprm);
1736 ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
1737 proc_exec_connector(current);
1738 }
1739
1740 return ret;
1741 }
1742
1743 /*
1744 * sys_execve() executes a new program.
1745 */
1746 static int do_execveat_common(int fd, struct filename *filename,
1747 struct user_arg_ptr argv,
1748 struct user_arg_ptr envp,
1749 int flags)
1750 {
1751 char *pathbuf = NULL;
1752 struct linux_binprm *bprm;
1753 struct file *file;
1754 struct files_struct *displaced;
1755 int retval;
1756
1757 if (IS_ERR(filename))
1758 return PTR_ERR(filename);
1759
1760 /*
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.
1765 */
1766 if ((current->flags & PF_NPROC_EXCEEDED) &&
1767 atomic_read(&current_user()->processes) > rlimit(RLIMIT_NPROC)) {
1768 retval = -EAGAIN;
1769 goto out_ret;
1770 }
1771
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;
1775
1776 retval = unshare_files(&displaced);
1777 if (retval)
1778 goto out_ret;
1779
1780 retval = -ENOMEM;
1781 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1782 if (!bprm)
1783 goto out_files;
1784
1785 retval = prepare_bprm_creds(bprm);
1786 if (retval)
1787 goto out_free;
1788
1789 check_unsafe_exec(bprm);
1790 current->in_execve = 1;
1791
1792 file = do_open_execat(fd, filename, flags);
1793 retval = PTR_ERR(file);
1794 if (IS_ERR(file))
1795 goto out_unmark;
1796
1797 #ifdef CONFIG_SECURITY_DEFEX
1798 retval = task_defex_enforce(current, file, -__NR_execve);
1799 if (retval < 0) {
1800 bprm->file = file;
1801 retval = -EPERM;
1802 goto out_unmark;
1803 }
1804 #endif
1805
1806 sched_exec();
1807
1808 bprm->file = file;
1809 if (fd == AT_FDCWD || filename->name[0] == '/') {
1810 bprm->filename = filename->name;
1811 } else {
1812 if (filename->name[0] == '\0')
1813 pathbuf = kasprintf(GFP_TEMPORARY, "/dev/fd/%d", fd);
1814 else
1815 pathbuf = kasprintf(GFP_TEMPORARY, "/dev/fd/%d/%s",
1816 fd, filename->name);
1817 if (!pathbuf) {
1818 retval = -ENOMEM;
1819 goto out_unmark;
1820 }
1821 /*
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).
1825 */
1826 if (close_on_exec(fd, rcu_dereference_raw(current->files->fdt)))
1827 bprm->interp_flags |= BINPRM_FLAGS_PATH_INACCESSIBLE;
1828 bprm->filename = pathbuf;
1829 }
1830 bprm->interp = bprm->filename;
1831
1832 retval = bprm_mm_init(bprm);
1833 if (retval)
1834 goto out_unmark;
1835
1836 bprm->argc = count(argv, MAX_ARG_STRINGS);
1837 if ((retval = bprm->argc) < 0)
1838 goto out;
1839
1840 bprm->envc = count(envp, MAX_ARG_STRINGS);
1841 if ((retval = bprm->envc) < 0)
1842 goto out;
1843
1844 retval = prepare_binprm(bprm);
1845 if (retval < 0)
1846 goto out;
1847
1848 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1849 if (retval < 0)
1850 goto out;
1851
1852 bprm->exec = bprm->p;
1853 retval = copy_strings(bprm->envc, envp, bprm);
1854 if (retval < 0)
1855 goto out;
1856
1857 retval = copy_strings(bprm->argc, argv, bprm);
1858 if (retval < 0)
1859 goto out;
1860
1861 would_dump(bprm, bprm->file);
1862
1863 retval = exec_binprm(bprm);
1864 if (retval < 0)
1865 goto out;
1866
1867 /* execve succeeded */
1868 current->fs->in_exec = 0;
1869 current->in_execve = 0;
1870 acct_update_integrals(current);
1871 task_numa_free(current);
1872 free_bprm(bprm);
1873 kfree(pathbuf);
1874 putname(filename);
1875 if (displaced)
1876 put_files_struct(displaced);
1877 return retval;
1878
1879 out:
1880 if (bprm->mm) {
1881 acct_arg_size(bprm, 0);
1882 mmput(bprm->mm);
1883 }
1884
1885 out_unmark:
1886 current->fs->in_exec = 0;
1887 current->in_execve = 0;
1888
1889 out_free:
1890 free_bprm(bprm);
1891 kfree(pathbuf);
1892
1893 out_files:
1894 if (displaced)
1895 reset_files_struct(displaced);
1896 out_ret:
1897 putname(filename);
1898 return retval;
1899 }
1900
1901 int do_execve(struct filename *filename,
1902 const char __user *const __user *__argv,
1903 const char __user *const __user *__envp)
1904 {
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);
1908 }
1909
1910 int do_execveat(int fd, struct filename *filename,
1911 const char __user *const __user *__argv,
1912 const char __user *const __user *__envp,
1913 int flags)
1914 {
1915 struct user_arg_ptr argv = { .ptr.native = __argv };
1916 struct user_arg_ptr envp = { .ptr.native = __envp };
1917
1918 return do_execveat_common(fd, filename, argv, envp, flags);
1919 }
1920
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)
1925 {
1926 struct user_arg_ptr argv = {
1927 .is_compat = true,
1928 .ptr.compat = __argv,
1929 };
1930 struct user_arg_ptr envp = {
1931 .is_compat = true,
1932 .ptr.compat = __envp,
1933 };
1934 return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
1935 }
1936
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,
1940 int flags)
1941 {
1942 struct user_arg_ptr argv = {
1943 .is_compat = true,
1944 .ptr.compat = __argv,
1945 };
1946 struct user_arg_ptr envp = {
1947 .is_compat = true,
1948 .ptr.compat = __envp,
1949 };
1950 return do_execveat_common(fd, filename, argv, envp, flags);
1951 }
1952 #endif
1953
1954 void set_binfmt(struct linux_binfmt *new)
1955 {
1956 struct mm_struct *mm = current->mm;
1957
1958 if (mm->binfmt)
1959 module_put(mm->binfmt->module);
1960
1961 mm->binfmt = new;
1962 if (new)
1963 __module_get(new->module);
1964 }
1965 EXPORT_SYMBOL(set_binfmt);
1966
1967 /*
1968 * set_dumpable stores three-value SUID_DUMP_* into mm->flags.
1969 */
1970 void set_dumpable(struct mm_struct *mm, int value)
1971 {
1972 unsigned long old, new;
1973
1974 if (WARN_ON((unsigned)value > SUID_DUMP_ROOT))
1975 return;
1976
1977 do {
1978 old = ACCESS_ONCE(mm->flags);
1979 new = (old & ~MMF_DUMPABLE_MASK) | value;
1980 } while (cmpxchg(&mm->flags, old, new) != old);
1981 }
1982
1983 SYSCALL_DEFINE3(execve,
1984 const char __user *, filename,
1985 const char __user *const __user *, argv,
1986 const char __user *const __user *, envp)
1987 {
1988 #ifdef CONFIG_RKP_KDP
1989 struct filename *path = getname(filename);
1990 int error = PTR_ERR(path);
1991
1992 if(IS_ERR(path))
1993 return error;
1994
1995 if(rkp_cred_enable){
1996 rkp_call(RKP_CMDID(0x4b),(u64)path->name,0,0,0,0);
1997 }
1998 #endif
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) "
2003 "PPID = %d(%s)\n",
2004 current->pid, current->comm,
2005 current->parent->pid, current->parent->comm);
2006 #ifdef CONFIG_RKP_KDP
2007 putname(path);
2008 #endif
2009 return -EACCES;
2010 }
2011 }
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) "
2016 "PPID = %d(%s)\n",
2017 current->pid, current->comm,
2018 current->parent->pid, current->parent->comm);
2019 putname(path);
2020 return -EACCES;
2021 }
2022 }
2023 #endif
2024 #endif // End of CONFIG_SEC_RESTRICT_FORK
2025 #ifdef CONFIG_RKP_KDP
2026 putname(path);
2027 #endif
2028 return do_execve(getname(filename), argv, envp);
2029 }
2030
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,
2035 int, flags)
2036 {
2037 int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
2038
2039 return do_execveat(fd,
2040 getname_flags(filename, lookup_flags, NULL),
2041 argv, envp, flags);
2042 }
2043
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)
2048 {
2049 return compat_do_execve(getname(filename), argv, envp);
2050 }
2051
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,
2056 int, flags)
2057 {
2058 int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
2059
2060 return compat_do_execveat(fd,
2061 getname_flags(filename, lookup_flags, NULL),
2062 argv, envp, flags);
2063 }
2064 #endif