locks: pass the cmd value to fcntl_getlk/getlk64
[GitHub/LineageOS/android_kernel_samsung_universal7580.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/resource.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 #include "coredump.h"
68
69 #include <trace/events/sched.h>
70
71 #ifdef CONFIG_RKP_KDP
72 #define rkp_is_nonroot(x) ((x->cred->type)>>1 & 1)
73 #endif /*CONFIG_RKP_KDP*/
74
75 int suid_dumpable = 0;
76
77 static LIST_HEAD(formats);
78 static DEFINE_RWLOCK(binfmt_lock);
79
80 void __register_binfmt(struct linux_binfmt * fmt, int insert)
81 {
82 BUG_ON(!fmt);
83 write_lock(&binfmt_lock);
84 insert ? list_add(&fmt->lh, &formats) :
85 list_add_tail(&fmt->lh, &formats);
86 write_unlock(&binfmt_lock);
87 }
88
89 EXPORT_SYMBOL(__register_binfmt);
90
91 void unregister_binfmt(struct linux_binfmt * fmt)
92 {
93 write_lock(&binfmt_lock);
94 list_del(&fmt->lh);
95 write_unlock(&binfmt_lock);
96 }
97
98 EXPORT_SYMBOL(unregister_binfmt);
99
100 static inline void put_binfmt(struct linux_binfmt * fmt)
101 {
102 module_put(fmt->module);
103 }
104
105 /*
106 * Note that a shared library must be both readable and executable due to
107 * security reasons.
108 *
109 * Also note that we take the address to load from from the file itself.
110 */
111 SYSCALL_DEFINE1(uselib, const char __user *, library)
112 {
113 struct file *file;
114 struct filename *tmp = getname(library);
115 int error = PTR_ERR(tmp);
116 static const struct open_flags uselib_flags = {
117 .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
118 .acc_mode = MAY_READ | MAY_EXEC | MAY_OPEN,
119 .intent = LOOKUP_OPEN,
120 .lookup_flags = LOOKUP_FOLLOW,
121 };
122
123 if (IS_ERR(tmp))
124 goto out;
125
126 file = do_filp_open(AT_FDCWD, tmp, &uselib_flags);
127 putname(tmp);
128 error = PTR_ERR(file);
129 if (IS_ERR(file))
130 goto out;
131
132 error = -EINVAL;
133 if (!S_ISREG(file_inode(file)->i_mode))
134 goto exit;
135
136 error = -EACCES;
137 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
138 goto exit;
139
140 fsnotify_open(file);
141
142 error = -ENOEXEC;
143 if(file->f_op) {
144 struct linux_binfmt * fmt;
145
146 read_lock(&binfmt_lock);
147 list_for_each_entry(fmt, &formats, lh) {
148 if (!fmt->load_shlib)
149 continue;
150 if (!try_module_get(fmt->module))
151 continue;
152 read_unlock(&binfmt_lock);
153 error = fmt->load_shlib(file);
154 read_lock(&binfmt_lock);
155 put_binfmt(fmt);
156 if (error != -ENOEXEC)
157 break;
158 }
159 read_unlock(&binfmt_lock);
160 }
161 exit:
162 fput(file);
163 out:
164 return error;
165 }
166
167 #ifdef CONFIG_MMU
168 /*
169 * The nascent bprm->mm is not visible until exec_mmap() but it can
170 * use a lot of memory, account these pages in current->mm temporary
171 * for oom_badness()->get_mm_rss(). Once exec succeeds or fails, we
172 * change the counter back via acct_arg_size(0).
173 */
174 static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
175 {
176 struct mm_struct *mm = current->mm;
177 long diff = (long)(pages - bprm->vma_pages);
178
179 if (!mm || !diff)
180 return;
181
182 bprm->vma_pages = pages;
183 add_mm_counter(mm, MM_ANONPAGES, diff);
184 }
185
186 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
187 int write)
188 {
189 struct page *page;
190 int ret;
191
192 #ifdef CONFIG_STACK_GROWSUP
193 if (write) {
194 ret = expand_downwards(bprm->vma, pos);
195 if (ret < 0)
196 return NULL;
197 }
198 #endif
199 ret = get_user_pages(current, bprm->mm, pos,
200 1, write, 1, &page, NULL);
201 if (ret <= 0)
202 return NULL;
203
204 if (write) {
205 unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
206 unsigned long ptr_size;
207 struct rlimit *rlim;
208
209 /*
210 * Since the stack will hold pointers to the strings, we
211 * must account for them as well.
212 *
213 * The size calculation is the entire vma while each arg page is
214 * built, so each time we get here it's calculating how far it
215 * is currently (rather than each call being just the newly
216 * added size from the arg page). As a result, we need to
217 * always add the entire size of the pointers, so that on the
218 * last call to get_arg_page() we'll actually have the entire
219 * correct size.
220 */
221 ptr_size = (bprm->argc + bprm->envc) * sizeof(void *);
222 if (ptr_size > ULONG_MAX - size)
223 goto fail;
224 size += ptr_size;
225
226 acct_arg_size(bprm, size / PAGE_SIZE);
227
228 /*
229 * We've historically supported up to 32 pages (ARG_MAX)
230 * of argument strings even with small stacks
231 */
232 if (size <= ARG_MAX)
233 return page;
234
235 /*
236 * Limit to 1/4-th the stack size for the argv+env strings.
237 * This ensures that:
238 * - the remaining binfmt code will not run out of stack space,
239 * - the program will have a reasonable amount of stack left
240 * to work from.
241 */
242 rlim = current->signal->rlim;
243 if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4)
244 goto fail;
245 }
246
247 return page;
248
249 fail:
250 put_page(page);
251 return NULL;
252 }
253
254 static void put_arg_page(struct page *page)
255 {
256 put_page(page);
257 }
258
259 static void free_arg_page(struct linux_binprm *bprm, int i)
260 {
261 }
262
263 static void free_arg_pages(struct linux_binprm *bprm)
264 {
265 }
266
267 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
268 struct page *page)
269 {
270 flush_cache_page(bprm->vma, pos, page_to_pfn(page));
271 }
272
273 static int __bprm_mm_init(struct linux_binprm *bprm)
274 {
275 int err;
276 struct vm_area_struct *vma = NULL;
277 struct mm_struct *mm = bprm->mm;
278
279 bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
280 if (!vma)
281 return -ENOMEM;
282
283 down_write(&mm->mmap_sem);
284 vma->vm_mm = mm;
285
286 /*
287 * Place the stack at the largest stack address the architecture
288 * supports. Later, we'll move this to an appropriate place. We don't
289 * use STACK_TOP because that can depend on attributes which aren't
290 * configured yet.
291 */
292 BUILD_BUG_ON(VM_STACK_FLAGS & VM_STACK_INCOMPLETE_SETUP);
293 vma->vm_end = STACK_TOP_MAX;
294 vma->vm_start = vma->vm_end - PAGE_SIZE;
295 vma->vm_flags = VM_STACK_FLAGS | VM_STACK_INCOMPLETE_SETUP;
296 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
297 INIT_LIST_HEAD(&vma->anon_vma_chain);
298
299 err = insert_vm_struct(mm, vma);
300 if (err)
301 goto err;
302
303 mm->stack_vm = mm->total_vm = 1;
304 up_write(&mm->mmap_sem);
305 bprm->p = vma->vm_end - sizeof(void *);
306 return 0;
307 err:
308 up_write(&mm->mmap_sem);
309 bprm->vma = NULL;
310 kmem_cache_free(vm_area_cachep, vma);
311 return err;
312 }
313
314 static bool valid_arg_len(struct linux_binprm *bprm, long len)
315 {
316 return len <= MAX_ARG_STRLEN;
317 }
318
319 #else
320
321 static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
322 {
323 }
324
325 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
326 int write)
327 {
328 struct page *page;
329
330 page = bprm->page[pos / PAGE_SIZE];
331 if (!page && write) {
332 page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
333 if (!page)
334 return NULL;
335 bprm->page[pos / PAGE_SIZE] = page;
336 }
337
338 return page;
339 }
340
341 static void put_arg_page(struct page *page)
342 {
343 }
344
345 static void free_arg_page(struct linux_binprm *bprm, int i)
346 {
347 if (bprm->page[i]) {
348 __free_page(bprm->page[i]);
349 bprm->page[i] = NULL;
350 }
351 }
352
353 static void free_arg_pages(struct linux_binprm *bprm)
354 {
355 int i;
356
357 for (i = 0; i < MAX_ARG_PAGES; i++)
358 free_arg_page(bprm, i);
359 }
360
361 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
362 struct page *page)
363 {
364 }
365
366 static int __bprm_mm_init(struct linux_binprm *bprm)
367 {
368 bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
369 return 0;
370 }
371
372 static bool valid_arg_len(struct linux_binprm *bprm, long len)
373 {
374 return len <= bprm->p;
375 }
376
377 #endif /* CONFIG_MMU */
378
379 /*
380 * Create a new mm_struct and populate it with a temporary stack
381 * vm_area_struct. We don't have enough context at this point to set the stack
382 * flags, permissions, and offset, so we use temporary values. We'll update
383 * them later in setup_arg_pages().
384 */
385 static int bprm_mm_init(struct linux_binprm *bprm)
386 {
387 int err;
388 struct mm_struct *mm = NULL;
389
390 bprm->mm = mm = mm_alloc();
391 err = -ENOMEM;
392 if (!mm)
393 goto err;
394
395 err = init_new_context(current, mm);
396 if (err)
397 goto err;
398
399 err = __bprm_mm_init(bprm);
400 if (err)
401 goto err;
402
403 return 0;
404
405 err:
406 if (mm) {
407 bprm->mm = NULL;
408 mmdrop(mm);
409 }
410
411 return err;
412 }
413
414 struct user_arg_ptr {
415 #ifdef CONFIG_COMPAT
416 bool is_compat;
417 #endif
418 union {
419 const char __user *const __user *native;
420 #ifdef CONFIG_COMPAT
421 const compat_uptr_t __user *compat;
422 #endif
423 } ptr;
424 };
425
426 static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr)
427 {
428 const char __user *native;
429
430 #ifdef CONFIG_COMPAT
431 if (unlikely(argv.is_compat)) {
432 compat_uptr_t compat;
433
434 if (get_user(compat, argv.ptr.compat + nr))
435 return ERR_PTR(-EFAULT);
436
437 return compat_ptr(compat);
438 }
439 #endif
440
441 if (get_user(native, argv.ptr.native + nr))
442 return ERR_PTR(-EFAULT);
443
444 return native;
445 }
446
447 /*
448 * count() counts the number of strings in array ARGV.
449 */
450 static int count(struct user_arg_ptr argv, int max)
451 {
452 int i = 0;
453
454 if (argv.ptr.native != NULL) {
455 for (;;) {
456 const char __user *p = get_user_arg_ptr(argv, i);
457
458 if (!p)
459 break;
460
461 if (IS_ERR(p))
462 return -EFAULT;
463
464 if (i >= max)
465 return -E2BIG;
466 ++i;
467
468 if (fatal_signal_pending(current))
469 return -ERESTARTNOHAND;
470 cond_resched();
471 }
472 }
473 return i;
474 }
475
476 /*
477 * 'copy_strings()' copies argument/environment strings from the old
478 * processes's memory to the new process's stack. The call to get_user_pages()
479 * ensures the destination page is created and not swapped out.
480 */
481 static int copy_strings(int argc, struct user_arg_ptr argv,
482 struct linux_binprm *bprm)
483 {
484 struct page *kmapped_page = NULL;
485 char *kaddr = NULL;
486 unsigned long kpos = 0;
487 int ret;
488
489 while (argc-- > 0) {
490 const char __user *str;
491 int len;
492 unsigned long pos;
493
494 ret = -EFAULT;
495 str = get_user_arg_ptr(argv, argc);
496 if (IS_ERR(str))
497 goto out;
498
499 len = strnlen_user(str, MAX_ARG_STRLEN);
500 if (!len)
501 goto out;
502
503 ret = -E2BIG;
504 if (!valid_arg_len(bprm, len))
505 goto out;
506
507 /* We're going to work our way backwords. */
508 pos = bprm->p;
509 str += len;
510 bprm->p -= len;
511
512 while (len > 0) {
513 int offset, bytes_to_copy;
514
515 if (fatal_signal_pending(current)) {
516 ret = -ERESTARTNOHAND;
517 goto out;
518 }
519 cond_resched();
520
521 offset = pos % PAGE_SIZE;
522 if (offset == 0)
523 offset = PAGE_SIZE;
524
525 bytes_to_copy = offset;
526 if (bytes_to_copy > len)
527 bytes_to_copy = len;
528
529 offset -= bytes_to_copy;
530 pos -= bytes_to_copy;
531 str -= bytes_to_copy;
532 len -= bytes_to_copy;
533
534 if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
535 struct page *page;
536
537 page = get_arg_page(bprm, pos, 1);
538 if (!page) {
539 ret = -E2BIG;
540 goto out;
541 }
542
543 if (kmapped_page) {
544 flush_kernel_dcache_page(kmapped_page);
545 kunmap(kmapped_page);
546 put_arg_page(kmapped_page);
547 }
548 kmapped_page = page;
549 kaddr = kmap(kmapped_page);
550 kpos = pos & PAGE_MASK;
551 flush_arg_page(bprm, kpos, kmapped_page);
552 }
553 if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
554 ret = -EFAULT;
555 goto out;
556 }
557 }
558 }
559 ret = 0;
560 out:
561 if (kmapped_page) {
562 flush_kernel_dcache_page(kmapped_page);
563 kunmap(kmapped_page);
564 put_arg_page(kmapped_page);
565 }
566 return ret;
567 }
568
569 /*
570 * Like copy_strings, but get argv and its values from kernel memory.
571 */
572 int copy_strings_kernel(int argc, const char *const *__argv,
573 struct linux_binprm *bprm)
574 {
575 int r;
576 mm_segment_t oldfs = get_fs();
577 struct user_arg_ptr argv = {
578 .ptr.native = (const char __user *const __user *)__argv,
579 };
580
581 set_fs(KERNEL_DS);
582 r = copy_strings(argc, argv, bprm);
583 set_fs(oldfs);
584
585 return r;
586 }
587 EXPORT_SYMBOL(copy_strings_kernel);
588
589 #ifdef CONFIG_MMU
590
591 /*
592 * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
593 * the binfmt code determines where the new stack should reside, we shift it to
594 * its final location. The process proceeds as follows:
595 *
596 * 1) Use shift to calculate the new vma endpoints.
597 * 2) Extend vma to cover both the old and new ranges. This ensures the
598 * arguments passed to subsequent functions are consistent.
599 * 3) Move vma's page tables to the new range.
600 * 4) Free up any cleared pgd range.
601 * 5) Shrink the vma to cover only the new range.
602 */
603 static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
604 {
605 struct mm_struct *mm = vma->vm_mm;
606 unsigned long old_start = vma->vm_start;
607 unsigned long old_end = vma->vm_end;
608 unsigned long length = old_end - old_start;
609 unsigned long new_start = old_start - shift;
610 unsigned long new_end = old_end - shift;
611 struct mmu_gather tlb;
612
613 BUG_ON(new_start > new_end);
614
615 /*
616 * ensure there are no vmas between where we want to go
617 * and where we are
618 */
619 if (vma != find_vma(mm, new_start))
620 return -EFAULT;
621
622 /*
623 * cover the whole range: [new_start, old_end)
624 */
625 if (vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL))
626 return -ENOMEM;
627
628 /*
629 * move the page tables downwards, on failure we rely on
630 * process cleanup to remove whatever mess we made.
631 */
632 if (length != move_page_tables(vma, old_start,
633 vma, new_start, length, false))
634 return -ENOMEM;
635
636 lru_add_drain();
637 tlb_gather_mmu(&tlb, mm, old_start, old_end);
638 if (new_end > old_start) {
639 /*
640 * when the old and new regions overlap clear from new_end.
641 */
642 free_pgd_range(&tlb, new_end, old_end, new_end,
643 vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING);
644 } else {
645 /*
646 * otherwise, clean from old_start; this is done to not touch
647 * the address space in [new_end, old_start) some architectures
648 * have constraints on va-space that make this illegal (IA64) -
649 * for the others its just a little faster.
650 */
651 free_pgd_range(&tlb, old_start, old_end, new_end,
652 vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING);
653 }
654 tlb_finish_mmu(&tlb, old_start, old_end);
655
656 /*
657 * Shrink the vma to just the new range. Always succeeds.
658 */
659 vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
660
661 return 0;
662 }
663
664 /*
665 * Finalizes the stack vm_area_struct. The flags and permissions are updated,
666 * the stack is optionally relocated, and some extra space is added.
667 */
668 int setup_arg_pages(struct linux_binprm *bprm,
669 unsigned long stack_top,
670 int executable_stack)
671 {
672 unsigned long ret;
673 unsigned long stack_shift;
674 struct mm_struct *mm = current->mm;
675 struct vm_area_struct *vma = bprm->vma;
676 struct vm_area_struct *prev = NULL;
677 unsigned long vm_flags;
678 unsigned long stack_base;
679 unsigned long stack_size;
680 unsigned long stack_expand;
681 unsigned long rlim_stack;
682
683 #ifdef CONFIG_STACK_GROWSUP
684 /* Limit stack size */
685 stack_base = rlimit_max(RLIMIT_STACK);
686 if (stack_base > STACK_SIZE_MAX)
687 stack_base = STACK_SIZE_MAX;
688
689 /* Make sure we didn't let the argument array grow too large. */
690 if (vma->vm_end - vma->vm_start > stack_base)
691 return -ENOMEM;
692
693 stack_base = PAGE_ALIGN(stack_top - stack_base);
694
695 stack_shift = vma->vm_start - stack_base;
696 mm->arg_start = bprm->p - stack_shift;
697 bprm->p = vma->vm_end - stack_shift;
698 #else
699 stack_top = arch_align_stack(stack_top);
700 stack_top = PAGE_ALIGN(stack_top);
701
702 if (unlikely(stack_top < mmap_min_addr) ||
703 unlikely(vma->vm_end - vma->vm_start >= stack_top - mmap_min_addr))
704 return -ENOMEM;
705
706 stack_shift = vma->vm_end - stack_top;
707
708 bprm->p -= stack_shift;
709 mm->arg_start = bprm->p;
710 #endif
711
712 if (bprm->loader)
713 bprm->loader -= stack_shift;
714 bprm->exec -= stack_shift;
715
716 down_write(&mm->mmap_sem);
717 vm_flags = VM_STACK_FLAGS;
718
719 /*
720 * Adjust stack execute permissions; explicitly enable for
721 * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
722 * (arch default) otherwise.
723 */
724 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
725 vm_flags |= VM_EXEC;
726 else if (executable_stack == EXSTACK_DISABLE_X)
727 vm_flags &= ~VM_EXEC;
728 vm_flags |= mm->def_flags;
729 vm_flags |= VM_STACK_INCOMPLETE_SETUP;
730
731 ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
732 vm_flags);
733 if (ret)
734 goto out_unlock;
735 BUG_ON(prev != vma);
736
737 /* Move stack pages down in memory. */
738 if (stack_shift) {
739 ret = shift_arg_pages(vma, stack_shift);
740 if (ret)
741 goto out_unlock;
742 }
743
744 /* mprotect_fixup is overkill to remove the temporary stack flags */
745 vma->vm_flags &= ~VM_STACK_INCOMPLETE_SETUP;
746
747 #ifdef _STK_LIM
748 /* save all the virtual space we're allowed */
749 stack_expand = ((_STK_LIM) & (PAGE_MASK));
750 #else
751 stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
752 #endif
753 stack_size = vma->vm_end - vma->vm_start;
754 /*
755 * Align this down to a page boundary as expand_stack
756 * will align it up.
757 */
758 rlim_stack = rlimit(RLIMIT_STACK) & PAGE_MASK;
759 if (stack_size + stack_expand > rlim_stack)
760 stack_expand = rlim_stack - stack_size;
761 #ifdef CONFIG_STACK_GROWSUP
762 stack_base = vma->vm_end + stack_expand;
763 #else
764 stack_base = vma->vm_start - stack_expand;
765 #endif
766 current->mm->start_stack = bprm->p;
767 ret = expand_stack(vma, stack_base);
768 if (ret)
769 ret = -EFAULT;
770
771 out_unlock:
772 up_write(&mm->mmap_sem);
773 return ret;
774 }
775 EXPORT_SYMBOL(setup_arg_pages);
776
777 #endif /* CONFIG_MMU */
778
779 struct file *open_exec(const char *name)
780 {
781 struct file *file;
782 int err;
783 struct filename tmp = { .name = name };
784 static const struct open_flags open_exec_flags = {
785 .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
786 .acc_mode = MAY_EXEC | MAY_OPEN,
787 .intent = LOOKUP_OPEN,
788 .lookup_flags = LOOKUP_FOLLOW,
789 };
790
791 file = do_filp_open(AT_FDCWD, &tmp, &open_exec_flags);
792 if (IS_ERR(file))
793 goto out;
794
795 err = -EACCES;
796 if (!S_ISREG(file_inode(file)->i_mode))
797 goto exit;
798
799 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
800 goto exit;
801
802 fsnotify_open(file);
803
804 err = deny_write_access(file);
805 if (err)
806 goto exit;
807
808 out:
809 return file;
810
811 exit:
812 fput(file);
813 return ERR_PTR(err);
814 }
815 EXPORT_SYMBOL(open_exec);
816
817 int kernel_read(struct file *file, loff_t offset,
818 char *addr, unsigned long count)
819 {
820 mm_segment_t old_fs;
821 loff_t pos = offset;
822 int result;
823
824 old_fs = get_fs();
825 set_fs(get_ds());
826 /* The cast to a user pointer is valid due to the set_fs() */
827 result = vfs_read(file, (void __user *)addr, count, &pos);
828 set_fs(old_fs);
829 return result;
830 }
831
832 EXPORT_SYMBOL(kernel_read);
833
834 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
835 {
836 ssize_t res = file->f_op->read(file, (void __user *)addr, len, &pos);
837 if (res > 0)
838 flush_icache_range(addr, addr + len);
839 return res;
840 }
841 EXPORT_SYMBOL(read_code);
842
843 static int exec_mmap(struct mm_struct *mm)
844 {
845 struct task_struct *tsk;
846 struct mm_struct *old_mm, *active_mm;
847
848 /* Notify parent that we're no longer interested in the old VM */
849 tsk = current;
850 old_mm = current->mm;
851 mm_release(tsk, old_mm);
852
853 if (old_mm) {
854 sync_mm_rss(old_mm);
855 /*
856 * Make sure that if there is a core dump in progress
857 * for the old mm, we get out and die instead of going
858 * through with the exec. We must hold mmap_sem around
859 * checking core_state and changing tsk->mm.
860 */
861 down_read(&old_mm->mmap_sem);
862 if (unlikely(old_mm->core_state)) {
863 up_read(&old_mm->mmap_sem);
864 return -EINTR;
865 }
866 }
867 task_lock(tsk);
868 active_mm = tsk->active_mm;
869 tsk->mm = mm;
870 tsk->active_mm = mm;
871 activate_mm(active_mm, mm);
872 tsk->mm->vmacache_seqnum = 0;
873 vmacache_flush(tsk);
874 task_unlock(tsk);
875 arch_pick_mmap_layout(mm);
876 if (old_mm) {
877 up_read(&old_mm->mmap_sem);
878 BUG_ON(active_mm != old_mm);
879 setmax_mm_hiwater_rss(&tsk->signal->maxrss, old_mm);
880 mm_update_next_owner(old_mm);
881 mmput(old_mm);
882 return 0;
883 }
884 mmdrop(active_mm);
885 return 0;
886 }
887
888 /*
889 * This function makes sure the current process has its own signal table,
890 * so that flush_signal_handlers can later reset the handlers without
891 * disturbing other processes. (Other processes might share the signal
892 * table via the CLONE_SIGHAND option to clone().)
893 */
894 static int de_thread(struct task_struct *tsk)
895 {
896 struct signal_struct *sig = tsk->signal;
897 struct sighand_struct *oldsighand = tsk->sighand;
898 spinlock_t *lock = &oldsighand->siglock;
899
900 if (thread_group_empty(tsk))
901 goto no_thread_group;
902
903 /*
904 * Kill all other threads in the thread group.
905 */
906 spin_lock_irq(lock);
907 if (signal_group_exit(sig)) {
908 /*
909 * Another group action in progress, just
910 * return so that the signal is processed.
911 */
912 spin_unlock_irq(lock);
913 return -EAGAIN;
914 }
915
916 sig->group_exit_task = tsk;
917 sig->notify_count = zap_other_threads(tsk);
918 if (!thread_group_leader(tsk))
919 sig->notify_count--;
920
921 while (sig->notify_count) {
922 __set_current_state(TASK_KILLABLE);
923 spin_unlock_irq(lock);
924 schedule();
925 if (unlikely(__fatal_signal_pending(tsk)))
926 goto killed;
927 spin_lock_irq(lock);
928 }
929 spin_unlock_irq(lock);
930
931 /*
932 * At this point all other threads have exited, all we have to
933 * do is to wait for the thread group leader to become inactive,
934 * and to assume its PID:
935 */
936 if (!thread_group_leader(tsk)) {
937 struct task_struct *leader = tsk->group_leader;
938
939 sig->notify_count = -1; /* for exit_notify() */
940 for (;;) {
941 threadgroup_change_begin(tsk);
942 write_lock_irq(&tasklist_lock);
943 if (likely(leader->exit_state))
944 break;
945 __set_current_state(TASK_KILLABLE);
946 write_unlock_irq(&tasklist_lock);
947 threadgroup_change_end(tsk);
948 schedule();
949 if (unlikely(__fatal_signal_pending(tsk)))
950 goto killed;
951 }
952
953 /*
954 * The only record we have of the real-time age of a
955 * process, regardless of execs it's done, is start_time.
956 * All the past CPU time is accumulated in signal_struct
957 * from sister threads now dead. But in this non-leader
958 * exec, nothing survives from the original leader thread,
959 * whose birth marks the true age of this process now.
960 * When we take on its identity by switching to its PID, we
961 * also take its birthdate (always earlier than our own).
962 */
963 tsk->start_time = leader->start_time;
964
965 BUG_ON(!same_thread_group(leader, tsk));
966 BUG_ON(has_group_leader_pid(tsk));
967 /*
968 * An exec() starts a new thread group with the
969 * TGID of the previous thread group. Rehash the
970 * two threads with a switched PID, and release
971 * the former thread group leader:
972 */
973
974 /* Become a process group leader with the old leader's pid.
975 * The old leader becomes a thread of the this thread group.
976 * Note: The old leader also uses this pid until release_task
977 * is called. Odd but simple and correct.
978 */
979 detach_pid(tsk, PIDTYPE_PID);
980 tsk->pid = leader->pid;
981 attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
982 transfer_pid(leader, tsk, PIDTYPE_PGID);
983 transfer_pid(leader, tsk, PIDTYPE_SID);
984
985 list_replace_rcu(&leader->tasks, &tsk->tasks);
986 list_replace_init(&leader->sibling, &tsk->sibling);
987
988 tsk->group_leader = tsk;
989 leader->group_leader = tsk;
990
991 tsk->exit_signal = SIGCHLD;
992 leader->exit_signal = -1;
993
994 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
995 leader->exit_state = EXIT_DEAD;
996
997 /*
998 * We are going to release_task()->ptrace_unlink() silently,
999 * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
1000 * the tracer wont't block again waiting for this thread.
1001 */
1002 if (unlikely(leader->ptrace))
1003 __wake_up_parent(leader, leader->parent);
1004 write_unlock_irq(&tasklist_lock);
1005 threadgroup_change_end(tsk);
1006
1007 release_task(leader);
1008 }
1009
1010 sig->group_exit_task = NULL;
1011 sig->notify_count = 0;
1012
1013 no_thread_group:
1014 /* we have changed execution domain */
1015 tsk->exit_signal = SIGCHLD;
1016
1017 exit_itimers(sig);
1018 flush_itimer_signals();
1019
1020 if (atomic_read(&oldsighand->count) != 1) {
1021 struct sighand_struct *newsighand;
1022 /*
1023 * This ->sighand is shared with the CLONE_SIGHAND
1024 * but not CLONE_THREAD task, switch to the new one.
1025 */
1026 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1027 if (!newsighand)
1028 return -ENOMEM;
1029
1030 atomic_set(&newsighand->count, 1);
1031 memcpy(newsighand->action, oldsighand->action,
1032 sizeof(newsighand->action));
1033
1034 write_lock_irq(&tasklist_lock);
1035 spin_lock(&oldsighand->siglock);
1036 rcu_assign_pointer(tsk->sighand, newsighand);
1037 spin_unlock(&oldsighand->siglock);
1038 write_unlock_irq(&tasklist_lock);
1039
1040 __cleanup_sighand(oldsighand);
1041 }
1042
1043 BUG_ON(!thread_group_leader(tsk));
1044 return 0;
1045
1046 killed:
1047 /* protects against exit_notify() and __exit_signal() */
1048 read_lock(&tasklist_lock);
1049 sig->group_exit_task = NULL;
1050 sig->notify_count = 0;
1051 read_unlock(&tasklist_lock);
1052 return -EAGAIN;
1053 }
1054
1055 char *get_task_comm(char *buf, struct task_struct *tsk)
1056 {
1057 /* buf must be at least sizeof(tsk->comm) in size */
1058 task_lock(tsk);
1059 strncpy(buf, tsk->comm, sizeof(tsk->comm));
1060 task_unlock(tsk);
1061 return buf;
1062 }
1063 EXPORT_SYMBOL_GPL(get_task_comm);
1064
1065 /*
1066 * These functions flushes out all traces of the currently running executable
1067 * so that a new one can be started
1068 */
1069
1070 void set_task_comm(struct task_struct *tsk, char *buf)
1071 {
1072 task_lock(tsk);
1073 trace_task_rename(tsk, buf);
1074 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
1075 task_unlock(tsk);
1076 perf_event_comm(tsk);
1077 }
1078
1079 static void filename_to_taskname(char *tcomm, const char *fn, unsigned int len)
1080 {
1081 int i, ch;
1082
1083 /* Copies the binary name from after last slash */
1084 for (i = 0; (ch = *(fn++)) != '\0';) {
1085 if (ch == '/')
1086 i = 0; /* overwrite what we wrote */
1087 else
1088 if (i < len - 1)
1089 tcomm[i++] = ch;
1090 }
1091 tcomm[i] = '\0';
1092 }
1093
1094 int flush_old_exec(struct linux_binprm * bprm)
1095 {
1096 int retval;
1097
1098 /*
1099 * Make sure we have a private signal table and that
1100 * we are unassociated from the previous thread group.
1101 */
1102 retval = de_thread(current);
1103 if (retval)
1104 goto out;
1105
1106 set_mm_exe_file(bprm->mm, bprm->file);
1107
1108 filename_to_taskname(bprm->tcomm, bprm->filename, sizeof(bprm->tcomm));
1109 /*
1110 * Release all of the old mmap stuff
1111 */
1112 acct_arg_size(bprm, 0);
1113 retval = exec_mmap(bprm->mm);
1114 if (retval)
1115 goto out;
1116 #ifdef CONFIG_RKP_KDP
1117 if(rkp_cred_enable){
1118 rkp_call(RKP_CMDID(0x43),(unsigned long long)current_cred(), (unsigned long long)bprm->mm->pgd,0,0,0);
1119 }
1120 #endif /*CONFIG_RKP_KDP*/
1121
1122 bprm->mm = NULL; /* We're using it now */
1123
1124 set_fs(USER_DS);
1125 current->flags &=
1126 ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_KTHREAD | PF_NOFREEZE);
1127 flush_thread();
1128 current->personality &= ~bprm->per_clear;
1129
1130 /*
1131 * We have to apply CLOEXEC before we change whether the process is
1132 * dumpable (in setup_new_exec) to avoid a race with a process in userspace
1133 * trying to access the should-be-closed file descriptors of a process
1134 * undergoing exec(2).
1135 */
1136 do_close_on_exec(current->files);
1137 return 0;
1138
1139 out:
1140 return retval;
1141 }
1142 EXPORT_SYMBOL(flush_old_exec);
1143
1144 void would_dump(struct linux_binprm *bprm, struct file *file)
1145 {
1146 if (inode_permission2(file->f_path.mnt, file_inode(file), MAY_READ) < 0)
1147 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
1148 }
1149 EXPORT_SYMBOL(would_dump);
1150
1151 void setup_new_exec(struct linux_binprm * bprm)
1152 {
1153 arch_pick_mmap_layout(current->mm);
1154
1155 /* This is the point of no return */
1156 current->sas_ss_sp = current->sas_ss_size = 0;
1157
1158 if (uid_eq(current_euid(), current_uid()) && gid_eq(current_egid(), current_gid()))
1159 set_dumpable(current->mm, SUID_DUMP_USER);
1160 else
1161 set_dumpable(current->mm, suid_dumpable);
1162
1163 set_task_comm(current, bprm->tcomm);
1164
1165 /* Set the new mm task size. We have to do that late because it may
1166 * depend on TIF_32BIT which is only updated in flush_thread() on
1167 * some architectures like powerpc
1168 */
1169 current->mm->task_size = TASK_SIZE;
1170
1171 /* install the new credentials */
1172 if (!uid_eq(bprm->cred->uid, current_euid()) ||
1173 !gid_eq(bprm->cred->gid, current_egid())) {
1174 current->pdeath_signal = 0;
1175 } else {
1176 would_dump(bprm, bprm->file);
1177 if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
1178 set_dumpable(current->mm, suid_dumpable);
1179 }
1180
1181 /* An exec changes our domain. We are no longer part of the thread
1182 group */
1183
1184 current->self_exec_id++;
1185
1186 flush_signal_handlers(current, 0);
1187 }
1188 EXPORT_SYMBOL(setup_new_exec);
1189
1190 /*
1191 * Prepare credentials and lock ->cred_guard_mutex.
1192 * install_exec_creds() commits the new creds and drops the lock.
1193 * Or, if exec fails before, free_bprm() should release ->cred and
1194 * and unlock.
1195 */
1196 int prepare_bprm_creds(struct linux_binprm *bprm)
1197 {
1198 if (mutex_lock_interruptible(&current->signal->cred_guard_mutex))
1199 return -ERESTARTNOINTR;
1200
1201 bprm->cred = prepare_exec_creds();
1202 if (likely(bprm->cred))
1203 return 0;
1204
1205 mutex_unlock(&current->signal->cred_guard_mutex);
1206 return -ENOMEM;
1207 }
1208
1209 void free_bprm(struct linux_binprm *bprm)
1210 {
1211 free_arg_pages(bprm);
1212 if (bprm->cred) {
1213 mutex_unlock(&current->signal->cred_guard_mutex);
1214 abort_creds(bprm->cred);
1215 }
1216 /* If a binfmt changed the interp, free it. */
1217 if (bprm->interp != bprm->filename)
1218 kfree(bprm->interp);
1219 kfree(bprm);
1220 }
1221
1222 int bprm_change_interp(char *interp, struct linux_binprm *bprm)
1223 {
1224 /* If a binfmt changed the interp, free it first. */
1225 if (bprm->interp != bprm->filename)
1226 kfree(bprm->interp);
1227 bprm->interp = kstrdup(interp, GFP_KERNEL);
1228 if (!bprm->interp)
1229 return -ENOMEM;
1230 return 0;
1231 }
1232 EXPORT_SYMBOL(bprm_change_interp);
1233
1234 /*
1235 * install the new credentials for this executable
1236 */
1237 void install_exec_creds(struct linux_binprm *bprm)
1238 {
1239 security_bprm_committing_creds(bprm);
1240
1241 commit_creds(bprm->cred);
1242 bprm->cred = NULL;
1243
1244 /*
1245 * Disable monitoring for regular users
1246 * when executing setuid binaries. Must
1247 * wait until new credentials are committed
1248 * by commit_creds() above
1249 */
1250 if (get_dumpable(current->mm) != SUID_DUMP_USER)
1251 perf_event_exit_task(current);
1252 /*
1253 * cred_guard_mutex must be held at least to this point to prevent
1254 * ptrace_attach() from altering our determination of the task's
1255 * credentials; any time after this it may be unlocked.
1256 */
1257 security_bprm_committed_creds(bprm);
1258 mutex_unlock(&current->signal->cred_guard_mutex);
1259 }
1260 EXPORT_SYMBOL(install_exec_creds);
1261
1262 /*
1263 * determine how safe it is to execute the proposed program
1264 * - the caller must hold ->cred_guard_mutex to protect against
1265 * PTRACE_ATTACH or seccomp thread-sync
1266 */
1267 static int check_unsafe_exec(struct linux_binprm *bprm)
1268 {
1269 struct task_struct *p = current, *t;
1270 unsigned n_fs;
1271 int res = 0;
1272
1273 if (p->ptrace) {
1274 if (p->ptrace & PT_PTRACE_CAP)
1275 bprm->unsafe |= LSM_UNSAFE_PTRACE_CAP;
1276 else
1277 bprm->unsafe |= LSM_UNSAFE_PTRACE;
1278 }
1279
1280 /*
1281 * This isn't strictly necessary, but it makes it harder for LSMs to
1282 * mess up.
1283 */
1284 if (task_no_new_privs(current))
1285 bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
1286
1287 n_fs = 1;
1288 spin_lock(&p->fs->lock);
1289 rcu_read_lock();
1290 for (t = next_thread(p); t != p; t = next_thread(t)) {
1291 if (t->fs == p->fs)
1292 n_fs++;
1293 }
1294 rcu_read_unlock();
1295
1296 if (p->fs->users > n_fs) {
1297 bprm->unsafe |= LSM_UNSAFE_SHARE;
1298 } else {
1299 res = -EAGAIN;
1300 if (!p->fs->in_exec) {
1301 p->fs->in_exec = 1;
1302 res = 1;
1303 }
1304 }
1305 spin_unlock(&p->fs->lock);
1306
1307 return res;
1308 }
1309
1310 static void bprm_fill_uid(struct linux_binprm *bprm)
1311 {
1312 struct inode *inode;
1313 unsigned int mode;
1314 kuid_t uid;
1315 kgid_t gid;
1316
1317 /* clear any previous set[ug]id data from a previous binary */
1318 bprm->cred->euid = current_euid();
1319 bprm->cred->egid = current_egid();
1320
1321 if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
1322 return;
1323
1324 if (task_no_new_privs(current))
1325 return;
1326
1327 inode = file_inode(bprm->file);
1328 mode = ACCESS_ONCE(inode->i_mode);
1329 if (!(mode & (S_ISUID|S_ISGID)))
1330 return;
1331
1332 /* Be careful if suid/sgid is set */
1333 mutex_lock(&inode->i_mutex);
1334
1335 /* reload atomically mode/uid/gid now that lock held */
1336 mode = inode->i_mode;
1337 uid = inode->i_uid;
1338 gid = inode->i_gid;
1339 mutex_unlock(&inode->i_mutex);
1340
1341 /* We ignore suid/sgid if there are no mappings for them in the ns */
1342 if (!kuid_has_mapping(bprm->cred->user_ns, uid) ||
1343 !kgid_has_mapping(bprm->cred->user_ns, gid))
1344 return;
1345
1346 if (mode & S_ISUID) {
1347 bprm->per_clear |= PER_CLEAR_ON_SETID;
1348 bprm->cred->euid = uid;
1349 }
1350
1351 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1352 bprm->per_clear |= PER_CLEAR_ON_SETID;
1353 bprm->cred->egid = gid;
1354 }
1355 }
1356
1357 /*
1358 * Fill the binprm structure from the inode.
1359 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
1360 *
1361 * This may be called multiple times for binary chains (scripts for example).
1362 */
1363 int prepare_binprm(struct linux_binprm *bprm)
1364 {
1365 int retval;
1366
1367 if (bprm->file->f_op == NULL)
1368 return -EACCES;
1369
1370 bprm_fill_uid(bprm);
1371
1372 /* fill in binprm security blob */
1373 retval = security_bprm_set_creds(bprm);
1374 if (retval)
1375 return retval;
1376 bprm->cred_prepared = 1;
1377
1378 memset(bprm->buf, 0, BINPRM_BUF_SIZE);
1379 return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
1380 }
1381
1382 EXPORT_SYMBOL(prepare_binprm);
1383
1384 /*
1385 * Arguments are '\0' separated strings found at the location bprm->p
1386 * points to; chop off the first by relocating brpm->p to right after
1387 * the first '\0' encountered.
1388 */
1389 int remove_arg_zero(struct linux_binprm *bprm)
1390 {
1391 int ret = 0;
1392 unsigned long offset;
1393 char *kaddr;
1394 struct page *page;
1395
1396 if (!bprm->argc)
1397 return 0;
1398
1399 do {
1400 offset = bprm->p & ~PAGE_MASK;
1401 page = get_arg_page(bprm, bprm->p, 0);
1402 if (!page) {
1403 ret = -EFAULT;
1404 goto out;
1405 }
1406 kaddr = kmap_atomic(page);
1407
1408 for (; offset < PAGE_SIZE && kaddr[offset];
1409 offset++, bprm->p++)
1410 ;
1411
1412 kunmap_atomic(kaddr);
1413 put_arg_page(page);
1414
1415 if (offset == PAGE_SIZE)
1416 free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
1417 } while (offset == PAGE_SIZE);
1418
1419 bprm->p++;
1420 bprm->argc--;
1421 ret = 0;
1422
1423 out:
1424 return ret;
1425 }
1426 EXPORT_SYMBOL(remove_arg_zero);
1427
1428 /*
1429 * cycle the list of binary formats handler, until one recognizes the image
1430 */
1431 int search_binary_handler(struct linux_binprm *bprm)
1432 {
1433 unsigned int depth = bprm->recursion_depth;
1434 int try,retval;
1435 struct linux_binfmt *fmt;
1436 pid_t old_pid, old_vpid;
1437
1438 /* This allows 4 levels of binfmt rewrites before failing hard. */
1439 if (depth > 5)
1440 return -ELOOP;
1441
1442 retval = security_bprm_check(bprm);
1443 if (retval)
1444 return retval;
1445
1446 retval = audit_bprm(bprm);
1447 if (retval)
1448 return retval;
1449
1450 /* Need to fetch pid before load_binary changes it */
1451 old_pid = current->pid;
1452 rcu_read_lock();
1453 old_vpid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
1454 rcu_read_unlock();
1455
1456 retval = -ENOENT;
1457 for (try=0; try<2; try++) {
1458 read_lock(&binfmt_lock);
1459 list_for_each_entry(fmt, &formats, lh) {
1460 int (*fn)(struct linux_binprm *) = fmt->load_binary;
1461 if (!fn)
1462 continue;
1463 if (!try_module_get(fmt->module))
1464 continue;
1465 read_unlock(&binfmt_lock);
1466 bprm->recursion_depth = depth + 1;
1467 retval = fn(bprm);
1468 bprm->recursion_depth = depth;
1469 if (retval >= 0) {
1470 if (depth == 0) {
1471 trace_sched_process_exec(current, old_pid, bprm);
1472 ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
1473 }
1474 put_binfmt(fmt);
1475 allow_write_access(bprm->file);
1476 if (bprm->file)
1477 fput(bprm->file);
1478 bprm->file = NULL;
1479 current->did_exec = 1;
1480 proc_exec_connector(current);
1481 return retval;
1482 }
1483 read_lock(&binfmt_lock);
1484 put_binfmt(fmt);
1485 if (retval != -ENOEXEC || bprm->mm == NULL)
1486 break;
1487 if (!bprm->file) {
1488 read_unlock(&binfmt_lock);
1489 return retval;
1490 }
1491 }
1492 read_unlock(&binfmt_lock);
1493 #ifdef CONFIG_MODULES
1494 if (retval != -ENOEXEC || bprm->mm == NULL) {
1495 break;
1496 } else {
1497 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1498 if (printable(bprm->buf[0]) &&
1499 printable(bprm->buf[1]) &&
1500 printable(bprm->buf[2]) &&
1501 printable(bprm->buf[3]))
1502 break; /* -ENOEXEC */
1503 if (try)
1504 break; /* -ENOEXEC */
1505 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1506 }
1507 #else
1508 break;
1509 #endif
1510 }
1511 return retval;
1512 }
1513
1514 EXPORT_SYMBOL(search_binary_handler);
1515
1516 #if defined CONFIG_SEC_RESTRICT_FORK
1517 #if defined CONFIG_SEC_RESTRICT_ROOTING_LOG
1518 #define PRINT_LOG(...) printk(KERN_ERR __VA_ARGS__)
1519 #else
1520 #define PRINT_LOG(...)
1521 #endif // End of CONFIG_SEC_RESTRICT_ROOTING_LOG
1522
1523 #define CHECK_ROOT_UID(x) (x->cred->uid == 0 || x->cred->gid == 0 || \
1524 x->cred->euid == 0 || x->cred->egid == 0 || \
1525 x->cred->suid == 0 || x->cred->sgid == 0)
1526
1527 /* sec_check_execpath
1528 return value : give task's exec path is matched or not
1529 */
1530 int sec_check_execpath(struct mm_struct *mm, char *denypath)
1531 {
1532 struct file *exe_file;
1533 char *path, *pathbuf = NULL;
1534 unsigned int path_length = 0, denypath_length = 0;
1535 int ret = 0;
1536
1537 if (mm == NULL)
1538 return 0;
1539
1540 if (!(exe_file = get_mm_exe_file(mm))) {
1541 PRINT_LOG("Cannot get exe from task->mm.\n");
1542 goto out_nofile;
1543 }
1544
1545 if (!(pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY))) {
1546 PRINT_LOG("failed to kmalloc for pathbuf\n");
1547 goto out;
1548 }
1549
1550 path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
1551 if (IS_ERR(path)) {
1552 PRINT_LOG("Error get path..\n");
1553 goto out;
1554 }
1555
1556 path_length = strlen(path);
1557 denypath_length = strlen(denypath);
1558
1559 if (!strncmp(path, denypath, (path_length < denypath_length) ?
1560 path_length : denypath_length)) {
1561 ret = 1;
1562 }
1563 out:
1564 fput(exe_file);
1565 out_nofile:
1566 if (pathbuf)
1567 kfree(pathbuf);
1568
1569 return ret;
1570 }
1571 EXPORT_SYMBOL(sec_check_execpath);
1572
1573 #ifdef CONFIG_RKP_KDP
1574 static int rkp_restrict_fork(void)
1575 {
1576 struct cred *shellcred;
1577
1578 if(rkp_is_nonroot(current)){
1579 shellcred = prepare_creds();
1580 if (!shellcred) {
1581 return 1;
1582 }
1583 shellcred->uid = 2000;
1584 shellcred->gid = 2000;
1585 shellcred->euid = 2000;
1586 shellcred->egid = 2000;
1587 commit_creds(shellcred);
1588 }
1589 return 0;
1590 }
1591 #endif /*CONFIG_RKP_KDP*/
1592 static int sec_restrict_fork(void)
1593 {
1594 struct cred *shellcred;
1595 int ret = 0;
1596 struct task_struct *parent_tsk;
1597 struct mm_struct *parent_mm = NULL;
1598 const struct cred *parent_cred;
1599
1600 read_lock(&tasklist_lock);
1601 parent_tsk = current->parent;
1602 if (!parent_tsk) {
1603 read_unlock(&tasklist_lock);
1604 return 0;
1605 }
1606
1607 get_task_struct(parent_tsk);
1608 /* holding on to the task struct is enough so just release
1609 * the tasklist lock here */
1610 read_unlock(&tasklist_lock);
1611
1612 if (current->pid == 1 || parent_tsk->pid == 1)
1613 goto out;
1614
1615 /* get current->parent's mm struct to access it's mm
1616 * and to keep it alive */
1617 parent_mm = get_task_mm(parent_tsk);
1618
1619 if (current->mm == NULL || parent_mm == NULL)
1620 goto out;
1621
1622 if (sec_check_execpath(parent_mm, "/sbin/adbd")) {
1623 shellcred = prepare_creds();
1624 if (!shellcred) {
1625 ret = 1;
1626 goto out;
1627 }
1628
1629 shellcred->uid = 2000;
1630 shellcred->gid = 2000;
1631 shellcred->euid = 2000;
1632 shellcred->egid = 2000;
1633 commit_creds(shellcred);
1634 ret = 0;
1635 goto out;
1636 }
1637
1638 if (sec_check_execpath(current->mm, "/data/")) {
1639 ret = 1;
1640 goto out;
1641 }
1642
1643 parent_cred = get_task_cred(parent_tsk);
1644 if (!parent_cred)
1645 goto out;
1646 if (!CHECK_ROOT_UID(parent_tsk))
1647 {
1648 ret = 1;
1649 }
1650 put_cred(parent_cred);
1651 out:
1652 if (parent_mm)
1653 mmput(parent_mm);
1654 put_task_struct(parent_tsk);
1655
1656 return ret;
1657 }
1658 #endif /* End of CONFIG_SEC_RESTRICT_FORK */
1659
1660 /*
1661 * sys_execve() executes a new program.
1662 */
1663 static int do_execve_common(const char *filename,
1664 struct user_arg_ptr argv,
1665 struct user_arg_ptr envp)
1666 {
1667 struct linux_binprm *bprm;
1668 struct file *file;
1669 struct files_struct *displaced;
1670 bool clear_in_exec;
1671 int retval;
1672 const struct cred *cred = current_cred();
1673 bool is_su;
1674
1675 /*
1676 * We move the actual failure in case of RLIMIT_NPROC excess from
1677 * set*uid() to execve() because too many poorly written programs
1678 * don't check setuid() return code. Here we additionally recheck
1679 * whether NPROC limit is still exceeded.
1680 */
1681 if ((current->flags & PF_NPROC_EXCEEDED) &&
1682 atomic_read(&cred->user->processes) > rlimit(RLIMIT_NPROC)) {
1683 retval = -EAGAIN;
1684 goto out_ret;
1685 }
1686
1687 /* We're below the limit (still or again), so we don't want to make
1688 * further execve() calls fail. */
1689 current->flags &= ~PF_NPROC_EXCEEDED;
1690
1691 retval = unshare_files(&displaced);
1692 if (retval)
1693 goto out_ret;
1694
1695 retval = -ENOMEM;
1696 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1697 if (!bprm)
1698 goto out_files;
1699
1700 retval = prepare_bprm_creds(bprm);
1701 if (retval)
1702 goto out_free;
1703
1704 retval = check_unsafe_exec(bprm);
1705 if (retval < 0)
1706 goto out_free;
1707 clear_in_exec = retval;
1708 current->in_execve = 1;
1709
1710 file = open_exec(filename);
1711 retval = PTR_ERR(file);
1712 if (IS_ERR(file))
1713 goto out_unmark;
1714
1715 sched_exec();
1716
1717 bprm->file = file;
1718 bprm->filename = filename;
1719 bprm->interp = filename;
1720
1721 retval = bprm_mm_init(bprm);
1722 if (retval)
1723 goto out_file;
1724
1725 bprm->argc = count(argv, MAX_ARG_STRINGS);
1726 if ((retval = bprm->argc) < 0)
1727 goto out;
1728
1729 bprm->envc = count(envp, MAX_ARG_STRINGS);
1730 if ((retval = bprm->envc) < 0)
1731 goto out;
1732
1733 retval = prepare_binprm(bprm);
1734 if (retval < 0)
1735 goto out;
1736
1737 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1738 if (retval < 0)
1739 goto out;
1740
1741 bprm->exec = bprm->p;
1742 retval = copy_strings(bprm->envc, envp, bprm);
1743 if (retval < 0)
1744 goto out;
1745
1746 retval = copy_strings(bprm->argc, argv, bprm);
1747 if (retval < 0)
1748 goto out;
1749
1750 /* search_binary_handler can release file and it may be freed */
1751 is_su = d_is_su(file->f_dentry);
1752
1753 retval = search_binary_handler(bprm);
1754 if (retval < 0)
1755 goto out;
1756
1757 if (is_su && capable(CAP_SYS_ADMIN)) {
1758 current->flags |= PF_SU;
1759 su_exec();
1760 }
1761
1762 /* execve succeeded */
1763 current->fs->in_exec = 0;
1764 current->in_execve = 0;
1765 acct_update_integrals(current);
1766 free_bprm(bprm);
1767 if (displaced)
1768 put_files_struct(displaced);
1769 return retval;
1770
1771 out:
1772 if (bprm->mm) {
1773 acct_arg_size(bprm, 0);
1774 mmput(bprm->mm);
1775 }
1776
1777 out_file:
1778 if (bprm->file) {
1779 allow_write_access(bprm->file);
1780 fput(bprm->file);
1781 }
1782
1783 out_unmark:
1784 if (clear_in_exec)
1785 current->fs->in_exec = 0;
1786 current->in_execve = 0;
1787
1788 out_free:
1789 free_bprm(bprm);
1790
1791 out_files:
1792 if (displaced)
1793 reset_files_struct(displaced);
1794 out_ret:
1795 return retval;
1796 }
1797
1798 int do_execve(const char *filename,
1799 const char __user *const __user *__argv,
1800 const char __user *const __user *__envp)
1801 {
1802 struct user_arg_ptr argv = { .ptr.native = __argv };
1803 struct user_arg_ptr envp = { .ptr.native = __envp };
1804 return do_execve_common(filename, argv, envp);
1805 }
1806
1807 #ifdef CONFIG_COMPAT
1808 static int compat_do_execve(const char *filename,
1809 const compat_uptr_t __user *__argv,
1810 const compat_uptr_t __user *__envp)
1811 {
1812 struct user_arg_ptr argv = {
1813 .is_compat = true,
1814 .ptr.compat = __argv,
1815 };
1816 struct user_arg_ptr envp = {
1817 .is_compat = true,
1818 .ptr.compat = __envp,
1819 };
1820 return do_execve_common(filename, argv, envp);
1821 }
1822 #endif
1823
1824 void set_binfmt(struct linux_binfmt *new)
1825 {
1826 struct mm_struct *mm = current->mm;
1827
1828 if (mm->binfmt)
1829 module_put(mm->binfmt->module);
1830
1831 mm->binfmt = new;
1832 if (new)
1833 __module_get(new->module);
1834 }
1835
1836 EXPORT_SYMBOL(set_binfmt);
1837
1838 /*
1839 * set_dumpable converts traditional three-value dumpable to two flags and
1840 * stores them into mm->flags. It modifies lower two bits of mm->flags, but
1841 * these bits are not changed atomically. So get_dumpable can observe the
1842 * intermediate state. To avoid doing unexpected behavior, get get_dumpable
1843 * return either old dumpable or new one by paying attention to the order of
1844 * modifying the bits.
1845 *
1846 * dumpable | mm->flags (binary)
1847 * old new | initial interim final
1848 * ---------+-----------------------
1849 * 0 1 | 00 01 01
1850 * 0 2 | 00 10(*) 11
1851 * 1 0 | 01 00 00
1852 * 1 2 | 01 11 11
1853 * 2 0 | 11 10(*) 00
1854 * 2 1 | 11 11 01
1855 *
1856 * (*) get_dumpable regards interim value of 10 as 11.
1857 */
1858 void set_dumpable(struct mm_struct *mm, int value)
1859 {
1860 switch (value) {
1861 case SUID_DUMP_DISABLE:
1862 clear_bit(MMF_DUMPABLE, &mm->flags);
1863 smp_wmb();
1864 clear_bit(MMF_DUMP_SECURELY, &mm->flags);
1865 break;
1866 case SUID_DUMP_USER:
1867 set_bit(MMF_DUMPABLE, &mm->flags);
1868 smp_wmb();
1869 clear_bit(MMF_DUMP_SECURELY, &mm->flags);
1870 break;
1871 case SUID_DUMP_ROOT:
1872 set_bit(MMF_DUMP_SECURELY, &mm->flags);
1873 smp_wmb();
1874 set_bit(MMF_DUMPABLE, &mm->flags);
1875 break;
1876 }
1877 }
1878
1879 int __get_dumpable(unsigned long mm_flags)
1880 {
1881 int ret;
1882
1883 ret = mm_flags & MMF_DUMPABLE_MASK;
1884 return (ret > SUID_DUMP_USER) ? SUID_DUMP_ROOT : ret;
1885 }
1886
1887 /*
1888 * This returns the actual value of the suid_dumpable flag. For things
1889 * that are using this for checking for privilege transitions, it must
1890 * test against SUID_DUMP_USER rather than treating it as a boolean
1891 * value.
1892 */
1893 int get_dumpable(struct mm_struct *mm)
1894 {
1895 return __get_dumpable(mm->flags);
1896 }
1897
1898 SYSCALL_DEFINE3(execve,
1899 const char __user *, filename,
1900 const char __user *const __user *, argv,
1901 const char __user *const __user *, envp)
1902 {
1903 struct filename *path = getname(filename);
1904 int error = PTR_ERR(path);
1905 if (!IS_ERR(path)) {
1906 #ifdef CONFIG_RKP_KDP
1907 if(rkp_cred_enable){
1908 rkp_call(RKP_CMDID(0x4b),(u64)path->name,0,0,0,0);
1909 }
1910 #endif
1911 #if defined CONFIG_SEC_RESTRICT_FORK
1912 if(CHECK_ROOT_UID(current)){
1913 if(sec_restrict_fork()){
1914 PRINT_LOG("Restricted making process. PID = %d(%s) "
1915 "PPID = %d(%s)\n",
1916 current->pid, current->comm,
1917 current->parent->pid, current->parent->comm);
1918 return -EACCES;
1919 }
1920 }
1921 #ifdef CONFIG_RKP_KDP
1922 if(CHECK_ROOT_UID(current) && rkp_cred_enable) {
1923 if(rkp_restrict_fork()){
1924 PRINT_LOG("RKP_KDP Restricted making process. PID = %d(%s) "
1925 "PPID = %d(%s)\n",
1926 current->pid, current->comm,
1927 current->parent->pid, current->parent->comm);
1928 return -EACCES;
1929 }
1930 }
1931 #endif
1932 #endif // End of CONFIG_SEC_RESTRICT_FORK
1933 error = do_execve(path->name, argv, envp);
1934 putname(path);
1935 }
1936 return error;
1937 }
1938 #ifdef CONFIG_COMPAT
1939 asmlinkage long compat_sys_execve(const char __user * filename,
1940 const compat_uptr_t __user * argv,
1941 const compat_uptr_t __user * envp)
1942 {
1943 struct filename *path = getname(filename);
1944 int error = PTR_ERR(path);
1945 if (!IS_ERR(path)) {
1946 error = compat_do_execve(path->name, argv, envp);
1947 putname(path);
1948 }
1949 return error;
1950 }
1951 #endif