1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/fs/exec.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8 /*
9 * #!-checking implemented by tytso.
10 */
11 /*
12 * Demand-loading implemented 01.12.91 - no need to read anything but
13 * the header into memory. The inode of the executable is put into
14 * "current->executable", and page faults do the actual loading. Clean.
15 *
16 * Once more I can proudly say that linux stood up to being changed: it
17 * was less than 2 hours work to get demand-loading completely implemented.
18 *
19 * Demand loading changed July 1993 by Eric Youngdale. Use mmap instead,
20 * current->executable is only used by the procfs. This allows a dispatch
21 * table to check for several different types of binary formats. We keep
22 * trying until we recognize the file or we run out of supported binary
23 * formats.
24 */
25
26 #include <linux/kernel_read_file.h>
27 #include <linux/slab.h>
28 #include <linux/file.h>
29 #include <linux/fdtable.h>
30 #include <linux/mm.h>
31 #include <linux/vmacache.h>
32 #include <linux/stat.h>
33 #include <linux/fcntl.h>
34 #include <linux/swap.h>
35 #include <linux/string.h>
36 #include <linux/init.h>
37 #include <linux/sched/mm.h>
38 #include <linux/sched/coredump.h>
39 #include <linux/sched/signal.h>
40 #include <linux/sched/numa_balancing.h>
41 #include <linux/sched/task.h>
42 #include <linux/pagemap.h>
43 #include <linux/perf_event.h>
44 #include <linux/highmem.h>
45 #include <linux/spinlock.h>
46 #include <linux/key.h>
47 #include <linux/personality.h>
48 #include <linux/binfmts.h>
49 #include <linux/utsname.h>
50 #include <linux/pid_namespace.h>
51 #include <linux/module.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/security.h>
55 #include <linux/syscalls.h>
56 #include <linux/tsacct_kern.h>
57 #include <linux/cn_proc.h>
58 #include <linux/audit.h>
59 #include <linux/tracehook.h>
60 #include <linux/kmod.h>
61 #include <linux/fsnotify.h>
62 #include <linux/fs_struct.h>
63 #include <linux/oom.h>
64 #include <linux/compat.h>
65 #include <linux/vmalloc.h>
66 #include <linux/io_uring.h>
67
68 #include <linux/uaccess.h>
69 #include <asm/mmu_context.h>
70 #include <asm/tlb.h>
71
72 #include <trace/events/task.h>
73 #include "internal.h"
74
75 #include <trace/events/sched.h>
76 #include <linux/hck/lite_hck_ced.h>
77
78 static int bprm_creds_from_file(struct linux_binprm *bprm);
79
80 int suid_dumpable = 0;
81
82 static LIST_HEAD(formats);
83 static DEFINE_RWLOCK(binfmt_lock);
84
__register_binfmt(struct linux_binfmt * fmt,int insert)85 void __register_binfmt(struct linux_binfmt * fmt, int insert)
86 {
87 BUG_ON(!fmt);
88 if (WARN_ON(!fmt->load_binary))
89 return;
90 write_lock(&binfmt_lock);
91 insert ? list_add(&fmt->lh, &formats) :
92 list_add_tail(&fmt->lh, &formats);
93 write_unlock(&binfmt_lock);
94 }
95
96 EXPORT_SYMBOL(__register_binfmt);
97
unregister_binfmt(struct linux_binfmt * fmt)98 void unregister_binfmt(struct linux_binfmt * fmt)
99 {
100 write_lock(&binfmt_lock);
101 list_del(&fmt->lh);
102 write_unlock(&binfmt_lock);
103 }
104
105 EXPORT_SYMBOL(unregister_binfmt);
106
put_binfmt(struct linux_binfmt * fmt)107 static inline void put_binfmt(struct linux_binfmt * fmt)
108 {
109 module_put(fmt->module);
110 }
111
path_noexec(const struct path * path)112 bool path_noexec(const struct path *path)
113 {
114 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
115 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
116 }
117
118 #ifdef CONFIG_USELIB
119 /*
120 * Note that a shared library must be both readable and executable due to
121 * security reasons.
122 *
123 * Also note that we take the address to load from from the file itself.
124 */
SYSCALL_DEFINE1(uselib,const char __user *,library)125 SYSCALL_DEFINE1(uselib, const char __user *, library)
126 {
127 struct linux_binfmt *fmt;
128 struct file *file;
129 struct filename *tmp = getname(library);
130 int error = PTR_ERR(tmp);
131 static const struct open_flags uselib_flags = {
132 .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
133 .acc_mode = MAY_READ | MAY_EXEC,
134 .intent = LOOKUP_OPEN,
135 .lookup_flags = LOOKUP_FOLLOW,
136 };
137
138 if (IS_ERR(tmp))
139 goto out;
140
141 file = do_filp_open(AT_FDCWD, tmp, &uselib_flags);
142 putname(tmp);
143 error = PTR_ERR(file);
144 if (IS_ERR(file))
145 goto out;
146
147 /*
148 * Check do_open_execat() for an explanation.
149 */
150 error = -EACCES;
151 if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode)) ||
152 path_noexec(&file->f_path))
153 goto exit;
154
155 fsnotify_open(file);
156
157 error = -ENOEXEC;
158
159 read_lock(&binfmt_lock);
160 list_for_each_entry(fmt, &formats, lh) {
161 if (!fmt->load_shlib)
162 continue;
163 if (!try_module_get(fmt->module))
164 continue;
165 read_unlock(&binfmt_lock);
166 error = fmt->load_shlib(file);
167 read_lock(&binfmt_lock);
168 put_binfmt(fmt);
169 if (error != -ENOEXEC)
170 break;
171 }
172 read_unlock(&binfmt_lock);
173 exit:
174 fput(file);
175 out:
176 return error;
177 }
178 #endif /* #ifdef CONFIG_USELIB */
179
180 #ifdef CONFIG_MMU
181 /*
182 * The nascent bprm->mm is not visible until exec_mmap() but it can
183 * use a lot of memory, account these pages in current->mm temporary
184 * for oom_badness()->get_mm_rss(). Once exec succeeds or fails, we
185 * change the counter back via acct_arg_size(0).
186 */
acct_arg_size(struct linux_binprm * bprm,unsigned long pages)187 static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
188 {
189 struct mm_struct *mm = current->mm;
190 long diff = (long)(pages - bprm->vma_pages);
191
192 if (!mm || !diff)
193 return;
194
195 bprm->vma_pages = pages;
196 add_mm_counter(mm, MM_ANONPAGES, diff);
197 }
198
get_arg_page(struct linux_binprm * bprm,unsigned long pos,int write)199 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
200 int write)
201 {
202 struct page *page;
203 int ret;
204 unsigned int gup_flags = FOLL_FORCE;
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
214 if (write)
215 gup_flags |= FOLL_WRITE;
216
217 /*
218 * We are doing an exec(). 'current' is the process
219 * doing the exec and bprm->mm is the new process's mm.
220 */
221 ret = get_user_pages_remote(bprm->mm, pos, 1, gup_flags,
222 &page, NULL, NULL);
223 if (ret <= 0)
224 return NULL;
225
226 if (write)
227 acct_arg_size(bprm, vma_pages(bprm->vma));
228
229 return page;
230 }
231
put_arg_page(struct page * page)232 static void put_arg_page(struct page *page)
233 {
234 put_page(page);
235 }
236
free_arg_pages(struct linux_binprm * bprm)237 static void free_arg_pages(struct linux_binprm *bprm)
238 {
239 }
240
flush_arg_page(struct linux_binprm * bprm,unsigned long pos,struct page * page)241 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
242 struct page *page)
243 {
244 flush_cache_page(bprm->vma, pos, page_to_pfn(page));
245 }
246
__bprm_mm_init(struct linux_binprm * bprm)247 static int __bprm_mm_init(struct linux_binprm *bprm)
248 {
249 int err;
250 struct vm_area_struct *vma = NULL;
251 struct mm_struct *mm = bprm->mm;
252
253 bprm->vma = vma = vm_area_alloc(mm);
254 if (!vma)
255 return -ENOMEM;
256 vma_set_anonymous(vma);
257
258 if (mmap_write_lock_killable(mm)) {
259 err = -EINTR;
260 goto err_free;
261 }
262
263 /*
264 * Place the stack at the largest stack address the architecture
265 * supports. Later, we'll move this to an appropriate place. We don't
266 * use STACK_TOP because that can depend on attributes which aren't
267 * configured yet.
268 */
269 BUILD_BUG_ON(VM_STACK_FLAGS & VM_STACK_INCOMPLETE_SETUP);
270 vma->vm_end = STACK_TOP_MAX;
271 vma->vm_start = vma->vm_end - PAGE_SIZE;
272 vma->vm_flags = VM_SOFTDIRTY | VM_STACK_FLAGS | VM_STACK_INCOMPLETE_SETUP;
273 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
274
275 err = insert_vm_struct(mm, vma);
276 if (err)
277 goto err;
278
279 mm->stack_vm = mm->total_vm = 1;
280 mmap_write_unlock(mm);
281 bprm->p = vma->vm_end - sizeof(void *);
282 return 0;
283 err:
284 mmap_write_unlock(mm);
285 err_free:
286 bprm->vma = NULL;
287 vm_area_free(vma);
288 return err;
289 }
290
valid_arg_len(struct linux_binprm * bprm,long len)291 static bool valid_arg_len(struct linux_binprm *bprm, long len)
292 {
293 return len <= MAX_ARG_STRLEN;
294 }
295
296 #else
297
acct_arg_size(struct linux_binprm * bprm,unsigned long pages)298 static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
299 {
300 }
301
get_arg_page(struct linux_binprm * bprm,unsigned long pos,int write)302 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
303 int write)
304 {
305 struct page *page;
306
307 page = bprm->page[pos / PAGE_SIZE];
308 if (!page && write) {
309 page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
310 if (!page)
311 return NULL;
312 bprm->page[pos / PAGE_SIZE] = page;
313 }
314
315 return page;
316 }
317
put_arg_page(struct page * page)318 static void put_arg_page(struct page *page)
319 {
320 }
321
free_arg_page(struct linux_binprm * bprm,int i)322 static void free_arg_page(struct linux_binprm *bprm, int i)
323 {
324 if (bprm->page[i]) {
325 __free_page(bprm->page[i]);
326 bprm->page[i] = NULL;
327 }
328 }
329
free_arg_pages(struct linux_binprm * bprm)330 static void free_arg_pages(struct linux_binprm *bprm)
331 {
332 int i;
333
334 for (i = 0; i < MAX_ARG_PAGES; i++)
335 free_arg_page(bprm, i);
336 }
337
flush_arg_page(struct linux_binprm * bprm,unsigned long pos,struct page * page)338 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
339 struct page *page)
340 {
341 }
342
__bprm_mm_init(struct linux_binprm * bprm)343 static int __bprm_mm_init(struct linux_binprm *bprm)
344 {
345 bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
346 return 0;
347 }
348
valid_arg_len(struct linux_binprm * bprm,long len)349 static bool valid_arg_len(struct linux_binprm *bprm, long len)
350 {
351 return len <= bprm->p;
352 }
353
354 #endif /* CONFIG_MMU */
355
356 /*
357 * Create a new mm_struct and populate it with a temporary stack
358 * vm_area_struct. We don't have enough context at this point to set the stack
359 * flags, permissions, and offset, so we use temporary values. We'll update
360 * them later in setup_arg_pages().
361 */
bprm_mm_init(struct linux_binprm * bprm)362 static int bprm_mm_init(struct linux_binprm *bprm)
363 {
364 int err;
365 struct mm_struct *mm = NULL;
366
367 bprm->mm = mm = mm_alloc();
368 err = -ENOMEM;
369 if (!mm)
370 goto err;
371
372 /* Save current stack limit for all calculations made during exec. */
373 task_lock(current->group_leader);
374 bprm->rlim_stack = current->signal->rlim[RLIMIT_STACK];
375 task_unlock(current->group_leader);
376
377 err = __bprm_mm_init(bprm);
378 if (err)
379 goto err;
380
381 return 0;
382
383 err:
384 if (mm) {
385 bprm->mm = NULL;
386 mmdrop(mm);
387 }
388
389 return err;
390 }
391
392 struct user_arg_ptr {
393 #ifdef CONFIG_COMPAT
394 bool is_compat;
395 #endif
396 union {
397 const char __user *const __user *native;
398 #ifdef CONFIG_COMPAT
399 const compat_uptr_t __user *compat;
400 #endif
401 } ptr;
402 };
403
get_user_arg_ptr(struct user_arg_ptr argv,int nr)404 static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr)
405 {
406 const char __user *native;
407
408 #ifdef CONFIG_COMPAT
409 if (unlikely(argv.is_compat)) {
410 compat_uptr_t compat;
411
412 if (get_user(compat, argv.ptr.compat + nr))
413 return ERR_PTR(-EFAULT);
414
415 return compat_ptr(compat);
416 }
417 #endif
418
419 if (get_user(native, argv.ptr.native + nr))
420 return ERR_PTR(-EFAULT);
421
422 return native;
423 }
424
425 /*
426 * count() counts the number of strings in array ARGV.
427 */
count(struct user_arg_ptr argv,int max)428 static int count(struct user_arg_ptr argv, int max)
429 {
430 int i = 0;
431
432 if (argv.ptr.native != NULL) {
433 for (;;) {
434 const char __user *p = get_user_arg_ptr(argv, i);
435
436 if (!p)
437 break;
438
439 if (IS_ERR(p))
440 return -EFAULT;
441
442 if (i >= max)
443 return -E2BIG;
444 ++i;
445
446 if (fatal_signal_pending(current))
447 return -ERESTARTNOHAND;
448 cond_resched();
449 }
450 }
451 return i;
452 }
453
count_strings_kernel(const char * const * argv)454 static int count_strings_kernel(const char *const *argv)
455 {
456 int i;
457
458 if (!argv)
459 return 0;
460
461 for (i = 0; argv[i]; ++i) {
462 if (i >= MAX_ARG_STRINGS)
463 return -E2BIG;
464 if (fatal_signal_pending(current))
465 return -ERESTARTNOHAND;
466 cond_resched();
467 }
468 return i;
469 }
470
bprm_stack_limits(struct linux_binprm * bprm)471 static int bprm_stack_limits(struct linux_binprm *bprm)
472 {
473 unsigned long limit, ptr_size;
474
475 /*
476 * Limit to 1/4 of the max stack size or 3/4 of _STK_LIM
477 * (whichever is smaller) for the argv+env strings.
478 * This ensures that:
479 * - the remaining binfmt code will not run out of stack space,
480 * - the program will have a reasonable amount of stack left
481 * to work from.
482 */
483 limit = _STK_LIM / 4 * 3;
484 limit = min(limit, bprm->rlim_stack.rlim_cur / 4);
485 /*
486 * We've historically supported up to 32 pages (ARG_MAX)
487 * of argument strings even with small stacks
488 */
489 limit = max_t(unsigned long, limit, ARG_MAX);
490 /*
491 * We must account for the size of all the argv and envp pointers to
492 * the argv and envp strings, since they will also take up space in
493 * the stack. They aren't stored until much later when we can't
494 * signal to the parent that the child has run out of stack space.
495 * Instead, calculate it here so it's possible to fail gracefully.
496 *
497 * In the case of argc = 0, make sure there is space for adding a
498 * empty string (which will bump argc to 1), to ensure confused
499 * userspace programs don't start processing from argv[1], thinking
500 * argc can never be 0, to keep them from walking envp by accident.
501 * See do_execveat_common().
502 */
503 ptr_size = (max(bprm->argc, 1) + bprm->envc) * sizeof(void *);
504 if (limit <= ptr_size)
505 return -E2BIG;
506 limit -= ptr_size;
507
508 bprm->argmin = bprm->p - limit;
509 return 0;
510 }
511
512 /*
513 * 'copy_strings()' copies argument/environment strings from the old
514 * processes's memory to the new process's stack. The call to get_user_pages()
515 * ensures the destination page is created and not swapped out.
516 */
copy_strings(int argc,struct user_arg_ptr argv,struct linux_binprm * bprm)517 static int copy_strings(int argc, struct user_arg_ptr argv,
518 struct linux_binprm *bprm)
519 {
520 struct page *kmapped_page = NULL;
521 char *kaddr = NULL;
522 unsigned long kpos = 0;
523 int ret;
524
525 while (argc-- > 0) {
526 const char __user *str;
527 int len;
528 unsigned long pos;
529
530 ret = -EFAULT;
531 str = get_user_arg_ptr(argv, argc);
532 if (IS_ERR(str))
533 goto out;
534
535 len = strnlen_user(str, MAX_ARG_STRLEN);
536 if (!len)
537 goto out;
538
539 ret = -E2BIG;
540 if (!valid_arg_len(bprm, len))
541 goto out;
542
543 /* We're going to work our way backwords. */
544 pos = bprm->p;
545 str += len;
546 bprm->p -= len;
547 #ifdef CONFIG_MMU
548 if (bprm->p < bprm->argmin)
549 goto out;
550 #endif
551
552 while (len > 0) {
553 int offset, bytes_to_copy;
554
555 if (fatal_signal_pending(current)) {
556 ret = -ERESTARTNOHAND;
557 goto out;
558 }
559 cond_resched();
560
561 offset = pos % PAGE_SIZE;
562 if (offset == 0)
563 offset = PAGE_SIZE;
564
565 bytes_to_copy = offset;
566 if (bytes_to_copy > len)
567 bytes_to_copy = len;
568
569 offset -= bytes_to_copy;
570 pos -= bytes_to_copy;
571 str -= bytes_to_copy;
572 len -= bytes_to_copy;
573
574 if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
575 struct page *page;
576
577 page = get_arg_page(bprm, pos, 1);
578 if (!page) {
579 ret = -E2BIG;
580 goto out;
581 }
582
583 if (kmapped_page) {
584 flush_kernel_dcache_page(kmapped_page);
585 kunmap(kmapped_page);
586 put_arg_page(kmapped_page);
587 }
588 kmapped_page = page;
589 kaddr = kmap(kmapped_page);
590 kpos = pos & PAGE_MASK;
591 flush_arg_page(bprm, kpos, kmapped_page);
592 }
593 if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
594 ret = -EFAULT;
595 goto out;
596 }
597 }
598 }
599 ret = 0;
600 out:
601 if (kmapped_page) {
602 flush_kernel_dcache_page(kmapped_page);
603 kunmap(kmapped_page);
604 put_arg_page(kmapped_page);
605 }
606 return ret;
607 }
608
609 /*
610 * Copy and argument/environment string from the kernel to the processes stack.
611 */
copy_string_kernel(const char * arg,struct linux_binprm * bprm)612 int copy_string_kernel(const char *arg, struct linux_binprm *bprm)
613 {
614 int len = strnlen(arg, MAX_ARG_STRLEN) + 1 /* terminating NUL */;
615 unsigned long pos = bprm->p;
616
617 if (len == 0)
618 return -EFAULT;
619 if (!valid_arg_len(bprm, len))
620 return -E2BIG;
621
622 /* We're going to work our way backwards. */
623 arg += len;
624 bprm->p -= len;
625 if (IS_ENABLED(CONFIG_MMU) && bprm->p < bprm->argmin)
626 return -E2BIG;
627
628 while (len > 0) {
629 unsigned int bytes_to_copy = min_t(unsigned int, len,
630 min_not_zero(offset_in_page(pos), PAGE_SIZE));
631 struct page *page;
632 char *kaddr;
633
634 pos -= bytes_to_copy;
635 arg -= bytes_to_copy;
636 len -= bytes_to_copy;
637
638 page = get_arg_page(bprm, pos, 1);
639 if (!page)
640 return -E2BIG;
641 kaddr = kmap_atomic(page);
642 flush_arg_page(bprm, pos & PAGE_MASK, page);
643 memcpy(kaddr + offset_in_page(pos), arg, bytes_to_copy);
644 flush_kernel_dcache_page(page);
645 kunmap_atomic(kaddr);
646 put_arg_page(page);
647 }
648
649 return 0;
650 }
651 EXPORT_SYMBOL(copy_string_kernel);
652
copy_strings_kernel(int argc,const char * const * argv,struct linux_binprm * bprm)653 static int copy_strings_kernel(int argc, const char *const *argv,
654 struct linux_binprm *bprm)
655 {
656 while (argc-- > 0) {
657 int ret = copy_string_kernel(argv[argc], bprm);
658 if (ret < 0)
659 return ret;
660 if (fatal_signal_pending(current))
661 return -ERESTARTNOHAND;
662 cond_resched();
663 }
664 return 0;
665 }
666
667 #ifdef CONFIG_MMU
668
669 /*
670 * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
671 * the binfmt code determines where the new stack should reside, we shift it to
672 * its final location. The process proceeds as follows:
673 *
674 * 1) Use shift to calculate the new vma endpoints.
675 * 2) Extend vma to cover both the old and new ranges. This ensures the
676 * arguments passed to subsequent functions are consistent.
677 * 3) Move vma's page tables to the new range.
678 * 4) Free up any cleared pgd range.
679 * 5) Shrink the vma to cover only the new range.
680 */
shift_arg_pages(struct vm_area_struct * vma,unsigned long shift)681 static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
682 {
683 struct mm_struct *mm = vma->vm_mm;
684 unsigned long old_start = vma->vm_start;
685 unsigned long old_end = vma->vm_end;
686 unsigned long length = old_end - old_start;
687 unsigned long new_start = old_start - shift;
688 unsigned long new_end = old_end - shift;
689 struct mmu_gather tlb;
690
691 BUG_ON(new_start > new_end);
692
693 /*
694 * ensure there are no vmas between where we want to go
695 * and where we are
696 */
697 if (vma != find_vma(mm, new_start))
698 return -EFAULT;
699
700 /*
701 * cover the whole range: [new_start, old_end)
702 */
703 if (vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL))
704 return -ENOMEM;
705
706 /*
707 * move the page tables downwards, on failure we rely on
708 * process cleanup to remove whatever mess we made.
709 */
710 if (length != move_page_tables(vma, old_start,
711 vma, new_start, length, false))
712 return -ENOMEM;
713
714 lru_add_drain();
715 tlb_gather_mmu(&tlb, mm, old_start, old_end);
716 if (new_end > old_start) {
717 /*
718 * when the old and new regions overlap clear from new_end.
719 */
720 free_pgd_range(&tlb, new_end, old_end, new_end,
721 vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING);
722 } else {
723 /*
724 * otherwise, clean from old_start; this is done to not touch
725 * the address space in [new_end, old_start) some architectures
726 * have constraints on va-space that make this illegal (IA64) -
727 * for the others its just a little faster.
728 */
729 free_pgd_range(&tlb, old_start, old_end, new_end,
730 vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING);
731 }
732 tlb_finish_mmu(&tlb, old_start, old_end);
733
734 /*
735 * Shrink the vma to just the new range. Always succeeds.
736 */
737 vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
738
739 return 0;
740 }
741
742 /*
743 * Finalizes the stack vm_area_struct. The flags and permissions are updated,
744 * the stack is optionally relocated, and some extra space is added.
745 */
setup_arg_pages(struct linux_binprm * bprm,unsigned long stack_top,int executable_stack)746 int setup_arg_pages(struct linux_binprm *bprm,
747 unsigned long stack_top,
748 int executable_stack)
749 {
750 unsigned long ret;
751 unsigned long stack_shift;
752 struct mm_struct *mm = current->mm;
753 struct vm_area_struct *vma = bprm->vma;
754 struct vm_area_struct *prev = NULL;
755 unsigned long vm_flags;
756 unsigned long stack_base;
757 unsigned long stack_size;
758 unsigned long stack_expand;
759 unsigned long rlim_stack;
760
761 #ifdef CONFIG_STACK_GROWSUP
762 /* Limit stack size */
763 stack_base = bprm->rlim_stack.rlim_max;
764 if (stack_base > STACK_SIZE_MAX)
765 stack_base = STACK_SIZE_MAX;
766
767 /* Add space for stack randomization. */
768 stack_base += (STACK_RND_MASK << PAGE_SHIFT);
769
770 /* Make sure we didn't let the argument array grow too large. */
771 if (vma->vm_end - vma->vm_start > stack_base)
772 return -ENOMEM;
773
774 stack_base = PAGE_ALIGN(stack_top - stack_base);
775
776 stack_shift = vma->vm_start - stack_base;
777 mm->arg_start = bprm->p - stack_shift;
778 bprm->p = vma->vm_end - stack_shift;
779 #else
780 stack_top = arch_align_stack(stack_top);
781 stack_top = PAGE_ALIGN(stack_top);
782
783 if (unlikely(stack_top < mmap_min_addr) ||
784 unlikely(vma->vm_end - vma->vm_start >= stack_top - mmap_min_addr))
785 return -ENOMEM;
786
787 stack_shift = vma->vm_end - stack_top;
788
789 bprm->p -= stack_shift;
790 mm->arg_start = bprm->p;
791 #endif
792
793 if (bprm->loader)
794 bprm->loader -= stack_shift;
795 bprm->exec -= stack_shift;
796
797 if (mmap_write_lock_killable(mm))
798 return -EINTR;
799
800 vm_flags = VM_STACK_FLAGS;
801
802 /*
803 * Adjust stack execute permissions; explicitly enable for
804 * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
805 * (arch default) otherwise.
806 */
807 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
808 vm_flags |= VM_EXEC;
809 else if (executable_stack == EXSTACK_DISABLE_X)
810 vm_flags &= ~VM_EXEC;
811 vm_flags |= mm->def_flags;
812 vm_flags |= VM_STACK_INCOMPLETE_SETUP;
813
814 ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
815 vm_flags);
816 if (ret)
817 goto out_unlock;
818 BUG_ON(prev != vma);
819
820 if (unlikely(vm_flags & VM_EXEC)) {
821 pr_warn_once("process '%pD4' started with executable stack\n",
822 bprm->file);
823 }
824
825 /* Move stack pages down in memory. */
826 if (stack_shift) {
827 ret = shift_arg_pages(vma, stack_shift);
828 if (ret)
829 goto out_unlock;
830 }
831
832 /* mprotect_fixup is overkill to remove the temporary stack flags */
833 vma->vm_flags &= ~VM_STACK_INCOMPLETE_SETUP;
834
835 stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
836 stack_size = vma->vm_end - vma->vm_start;
837 /*
838 * Align this down to a page boundary as expand_stack
839 * will align it up.
840 */
841 rlim_stack = bprm->rlim_stack.rlim_cur & PAGE_MASK;
842 #ifdef CONFIG_STACK_GROWSUP
843 if (stack_size + stack_expand > rlim_stack)
844 stack_base = vma->vm_start + rlim_stack;
845 else
846 stack_base = vma->vm_end + stack_expand;
847 #else
848 if (stack_size + stack_expand > rlim_stack)
849 stack_base = vma->vm_end - rlim_stack;
850 else
851 stack_base = vma->vm_start - stack_expand;
852 #endif
853 current->mm->start_stack = bprm->p;
854 ret = expand_stack(vma, stack_base);
855 if (ret)
856 ret = -EFAULT;
857
858 out_unlock:
859 mmap_write_unlock(mm);
860 return ret;
861 }
862 EXPORT_SYMBOL(setup_arg_pages);
863
864 #else
865
866 /*
867 * Transfer the program arguments and environment from the holding pages
868 * onto the stack. The provided stack pointer is adjusted accordingly.
869 */
transfer_args_to_stack(struct linux_binprm * bprm,unsigned long * sp_location)870 int transfer_args_to_stack(struct linux_binprm *bprm,
871 unsigned long *sp_location)
872 {
873 unsigned long index, stop, sp;
874 int ret = 0;
875
876 stop = bprm->p >> PAGE_SHIFT;
877 sp = *sp_location;
878
879 for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
880 unsigned int offset = index == stop ? bprm->p & ~PAGE_MASK : 0;
881 char *src = kmap(bprm->page[index]) + offset;
882 sp -= PAGE_SIZE - offset;
883 if (copy_to_user((void *) sp, src, PAGE_SIZE - offset) != 0)
884 ret = -EFAULT;
885 kunmap(bprm->page[index]);
886 if (ret)
887 goto out;
888 }
889
890 *sp_location = sp;
891
892 out:
893 return ret;
894 }
895 EXPORT_SYMBOL(transfer_args_to_stack);
896
897 #endif /* CONFIG_MMU */
898
do_open_execat(int fd,struct filename * name,int flags)899 static struct file *do_open_execat(int fd, struct filename *name, int flags)
900 {
901 struct file *file;
902 int err;
903 struct open_flags open_exec_flags = {
904 .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
905 .acc_mode = MAY_EXEC,
906 .intent = LOOKUP_OPEN,
907 .lookup_flags = LOOKUP_FOLLOW,
908 };
909
910 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
911 return ERR_PTR(-EINVAL);
912 if (flags & AT_SYMLINK_NOFOLLOW)
913 open_exec_flags.lookup_flags &= ~LOOKUP_FOLLOW;
914 if (flags & AT_EMPTY_PATH)
915 open_exec_flags.lookup_flags |= LOOKUP_EMPTY;
916
917 file = do_filp_open(fd, name, &open_exec_flags);
918 if (IS_ERR(file))
919 return file;
920
921 /*
922 * In the past the regular type check was here. It moved to may_open() in
923 * 633fb6ac3980 ("exec: move S_ISREG() check earlier"). Since then it is
924 * an invariant that all non-regular files error out before we get here.
925 */
926 err = -EACCES;
927 if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode)) ||
928 path_noexec(&file->f_path))
929 goto exit;
930
931 err = deny_write_access(file);
932 if (err)
933 goto exit;
934
935 if (name->name[0] != '\0')
936 fsnotify_open(file);
937
938 return file;
939
940 exit:
941 fput(file);
942 return ERR_PTR(err);
943 }
944
open_exec(const char * name)945 struct file *open_exec(const char *name)
946 {
947 struct filename *filename = getname_kernel(name);
948 struct file *f = ERR_CAST(filename);
949
950 if (!IS_ERR(filename)) {
951 f = do_open_execat(AT_FDCWD, filename, 0);
952 putname(filename);
953 }
954 return f;
955 }
956 EXPORT_SYMBOL(open_exec);
957
958 #if defined(CONFIG_HAVE_AOUT) || defined(CONFIG_BINFMT_FLAT) || \
959 defined(CONFIG_BINFMT_ELF_FDPIC)
read_code(struct file * file,unsigned long addr,loff_t pos,size_t len)960 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
961 {
962 ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
963 if (res > 0)
964 flush_icache_user_range(addr, addr + len);
965 return res;
966 }
967 EXPORT_SYMBOL(read_code);
968 #endif
969
970 /*
971 * Maps the mm_struct mm into the current task struct.
972 * On success, this function returns with exec_update_lock
973 * held for writing.
974 */
exec_mmap(struct mm_struct * mm)975 static int exec_mmap(struct mm_struct *mm)
976 {
977 struct task_struct *tsk;
978 struct mm_struct *old_mm, *active_mm;
979 int ret;
980
981 /* Notify parent that we're no longer interested in the old VM */
982 tsk = current;
983 old_mm = current->mm;
984 exec_mm_release(tsk, old_mm);
985 if (old_mm)
986 sync_mm_rss(old_mm);
987
988 ret = down_write_killable(&tsk->signal->exec_update_lock);
989 if (ret)
990 return ret;
991
992 if (old_mm) {
993 /*
994 * Make sure that if there is a core dump in progress
995 * for the old mm, we get out and die instead of going
996 * through with the exec. We must hold mmap_lock around
997 * checking core_state and changing tsk->mm.
998 */
999 mmap_read_lock(old_mm);
1000 if (unlikely(old_mm->core_state)) {
1001 mmap_read_unlock(old_mm);
1002 up_write(&tsk->signal->exec_update_lock);
1003 return -EINTR;
1004 }
1005 }
1006
1007 task_lock(tsk);
1008 membarrier_exec_mmap(mm);
1009
1010 local_irq_disable();
1011 active_mm = tsk->active_mm;
1012 tsk->active_mm = mm;
1013 tsk->mm = mm;
1014 /*
1015 * This prevents preemption while active_mm is being loaded and
1016 * it and mm are being updated, which could cause problems for
1017 * lazy tlb mm refcounting when these are updated by context
1018 * switches. Not all architectures can handle irqs off over
1019 * activate_mm yet.
1020 */
1021 if (!IS_ENABLED(CONFIG_ARCH_WANT_IRQS_OFF_ACTIVATE_MM))
1022 local_irq_enable();
1023 activate_mm(active_mm, mm);
1024 if (IS_ENABLED(CONFIG_ARCH_WANT_IRQS_OFF_ACTIVATE_MM))
1025 local_irq_enable();
1026 tsk->mm->vmacache_seqnum = 0;
1027 vmacache_flush(tsk);
1028 task_unlock(tsk);
1029 if (old_mm) {
1030 mmap_read_unlock(old_mm);
1031 BUG_ON(active_mm != old_mm);
1032 setmax_mm_hiwater_rss(&tsk->signal->maxrss, old_mm);
1033 mm_update_next_owner(old_mm);
1034 mmput(old_mm);
1035 return 0;
1036 }
1037 mmdrop(active_mm);
1038 return 0;
1039 }
1040
de_thread(struct task_struct * tsk)1041 static int de_thread(struct task_struct *tsk)
1042 {
1043 struct signal_struct *sig = tsk->signal;
1044 struct sighand_struct *oldsighand = tsk->sighand;
1045 spinlock_t *lock = &oldsighand->siglock;
1046
1047 if (thread_group_empty(tsk))
1048 goto no_thread_group;
1049
1050 /*
1051 * Kill all other threads in the thread group.
1052 */
1053 spin_lock_irq(lock);
1054 if (signal_group_exit(sig)) {
1055 /*
1056 * Another group action in progress, just
1057 * return so that the signal is processed.
1058 */
1059 spin_unlock_irq(lock);
1060 return -EAGAIN;
1061 }
1062
1063 sig->group_exit_task = tsk;
1064 sig->notify_count = zap_other_threads(tsk);
1065 if (!thread_group_leader(tsk))
1066 sig->notify_count--;
1067
1068 while (sig->notify_count) {
1069 __set_current_state(TASK_KILLABLE);
1070 spin_unlock_irq(lock);
1071 schedule();
1072 if (__fatal_signal_pending(tsk))
1073 goto killed;
1074 spin_lock_irq(lock);
1075 }
1076 spin_unlock_irq(lock);
1077
1078 /*
1079 * At this point all other threads have exited, all we have to
1080 * do is to wait for the thread group leader to become inactive,
1081 * and to assume its PID:
1082 */
1083 if (!thread_group_leader(tsk)) {
1084 struct task_struct *leader = tsk->group_leader;
1085
1086 for (;;) {
1087 cgroup_threadgroup_change_begin(tsk);
1088 write_lock_irq(&tasklist_lock);
1089 /*
1090 * Do this under tasklist_lock to ensure that
1091 * exit_notify() can't miss ->group_exit_task
1092 */
1093 sig->notify_count = -1;
1094 if (likely(leader->exit_state))
1095 break;
1096 __set_current_state(TASK_KILLABLE);
1097 write_unlock_irq(&tasklist_lock);
1098 cgroup_threadgroup_change_end(tsk);
1099 schedule();
1100 if (__fatal_signal_pending(tsk))
1101 goto killed;
1102 }
1103
1104 /*
1105 * The only record we have of the real-time age of a
1106 * process, regardless of execs it's done, is start_time.
1107 * All the past CPU time is accumulated in signal_struct
1108 * from sister threads now dead. But in this non-leader
1109 * exec, nothing survives from the original leader thread,
1110 * whose birth marks the true age of this process now.
1111 * When we take on its identity by switching to its PID, we
1112 * also take its birthdate (always earlier than our own).
1113 */
1114 tsk->start_time = leader->start_time;
1115 tsk->start_boottime = leader->start_boottime;
1116
1117 BUG_ON(!same_thread_group(leader, tsk));
1118 /*
1119 * An exec() starts a new thread group with the
1120 * TGID of the previous thread group. Rehash the
1121 * two threads with a switched PID, and release
1122 * the former thread group leader:
1123 */
1124
1125 /* Become a process group leader with the old leader's pid.
1126 * The old leader becomes a thread of the this thread group.
1127 */
1128 exchange_tids(tsk, leader);
1129 transfer_pid(leader, tsk, PIDTYPE_TGID);
1130 transfer_pid(leader, tsk, PIDTYPE_PGID);
1131 transfer_pid(leader, tsk, PIDTYPE_SID);
1132
1133 list_replace_rcu(&leader->tasks, &tsk->tasks);
1134 list_replace_init(&leader->sibling, &tsk->sibling);
1135
1136 tsk->group_leader = tsk;
1137 leader->group_leader = tsk;
1138
1139 tsk->exit_signal = SIGCHLD;
1140 leader->exit_signal = -1;
1141
1142 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
1143 leader->exit_state = EXIT_DEAD;
1144
1145 /*
1146 * We are going to release_task()->ptrace_unlink() silently,
1147 * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
1148 * the tracer wont't block again waiting for this thread.
1149 */
1150 if (unlikely(leader->ptrace))
1151 __wake_up_parent(leader, leader->parent);
1152 write_unlock_irq(&tasklist_lock);
1153 cgroup_threadgroup_change_end(tsk);
1154
1155 release_task(leader);
1156 }
1157
1158 sig->group_exit_task = NULL;
1159 sig->notify_count = 0;
1160
1161 no_thread_group:
1162 /* we have changed execution domain */
1163 tsk->exit_signal = SIGCHLD;
1164
1165 BUG_ON(!thread_group_leader(tsk));
1166 return 0;
1167
1168 killed:
1169 /* protects against exit_notify() and __exit_signal() */
1170 read_lock(&tasklist_lock);
1171 sig->group_exit_task = NULL;
1172 sig->notify_count = 0;
1173 read_unlock(&tasklist_lock);
1174 return -EAGAIN;
1175 }
1176
1177
1178 /*
1179 * This function makes sure the current process has its own signal table,
1180 * so that flush_signal_handlers can later reset the handlers without
1181 * disturbing other processes. (Other processes might share the signal
1182 * table via the CLONE_SIGHAND option to clone().)
1183 */
unshare_sighand(struct task_struct * me)1184 static int unshare_sighand(struct task_struct *me)
1185 {
1186 struct sighand_struct *oldsighand = me->sighand;
1187
1188 if (refcount_read(&oldsighand->count) != 1) {
1189 struct sighand_struct *newsighand;
1190 /*
1191 * This ->sighand is shared with the CLONE_SIGHAND
1192 * but not CLONE_THREAD task, switch to the new one.
1193 */
1194 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1195 if (!newsighand)
1196 return -ENOMEM;
1197
1198 refcount_set(&newsighand->count, 1);
1199
1200 write_lock_irq(&tasklist_lock);
1201 spin_lock(&oldsighand->siglock);
1202 memcpy(newsighand->action, oldsighand->action,
1203 sizeof(newsighand->action));
1204 rcu_assign_pointer(me->sighand, newsighand);
1205 spin_unlock(&oldsighand->siglock);
1206 write_unlock_irq(&tasklist_lock);
1207
1208 __cleanup_sighand(oldsighand);
1209 }
1210 return 0;
1211 }
1212
__get_task_comm(char * buf,size_t buf_size,struct task_struct * tsk)1213 char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk)
1214 {
1215 task_lock(tsk);
1216 strncpy(buf, tsk->comm, buf_size);
1217 task_unlock(tsk);
1218 return buf;
1219 }
1220 EXPORT_SYMBOL_GPL(__get_task_comm);
1221
1222 /*
1223 * These functions flushes out all traces of the currently running executable
1224 * so that a new one can be started
1225 */
1226
__set_task_comm(struct task_struct * tsk,const char * buf,bool exec)1227 void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
1228 {
1229 task_lock(tsk);
1230 trace_task_rename(tsk, buf);
1231 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
1232 task_unlock(tsk);
1233 perf_event_comm(tsk, exec);
1234 }
1235
1236 /*
1237 * Calling this is the point of no return. None of the failures will be
1238 * seen by userspace since either the process is already taking a fatal
1239 * signal (via de_thread() or coredump), or will have SEGV raised
1240 * (after exec_mmap()) by search_binary_handler (see below).
1241 */
begin_new_exec(struct linux_binprm * bprm)1242 int begin_new_exec(struct linux_binprm * bprm)
1243 {
1244 struct task_struct *me = current;
1245 struct files_struct *displaced;
1246 int retval;
1247
1248 /* Once we are committed compute the creds */
1249 retval = bprm_creds_from_file(bprm);
1250 if (retval)
1251 return retval;
1252
1253 /*
1254 * Ensure all future errors are fatal.
1255 */
1256 bprm->point_of_no_return = true;
1257
1258 /*
1259 * Make this the only thread in the thread group.
1260 */
1261 retval = de_thread(me);
1262 if (retval)
1263 goto out;
1264
1265 /* Ensure the files table is not shared. */
1266 retval = unshare_files(&displaced);
1267 if (retval)
1268 goto out;
1269 if (displaced)
1270 put_files_struct(displaced);
1271
1272 /*
1273 * Must be called _before_ exec_mmap() as bprm->mm is
1274 * not visibile until then. This also enables the update
1275 * to be lockless.
1276 */
1277 set_mm_exe_file(bprm->mm, bprm->file);
1278
1279 /* If the binary is not readable then enforce mm->dumpable=0 */
1280 would_dump(bprm, bprm->file);
1281 if (bprm->have_execfd)
1282 would_dump(bprm, bprm->executable);
1283
1284 /*
1285 * Release all of the old mmap stuff
1286 */
1287 acct_arg_size(bprm, 0);
1288 retval = exec_mmap(bprm->mm);
1289 if (retval)
1290 goto out;
1291
1292 bprm->mm = NULL;
1293
1294 #ifdef CONFIG_POSIX_TIMERS
1295 spin_lock_irq(&me->sighand->siglock);
1296 posix_cpu_timers_exit(me);
1297 spin_unlock_irq(&me->sighand->siglock);
1298 exit_itimers(me);
1299 flush_itimer_signals();
1300 #endif
1301
1302 /*
1303 * Make the signal table private.
1304 */
1305 retval = unshare_sighand(me);
1306 if (retval)
1307 goto out_unlock;
1308
1309 /*
1310 * Ensure that the uaccess routines can actually operate on userspace
1311 * pointers:
1312 */
1313 force_uaccess_begin();
1314
1315 me->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_KTHREAD |
1316 PF_NOFREEZE | PF_NO_SETAFFINITY);
1317 flush_thread();
1318 me->personality &= ~bprm->per_clear;
1319
1320 /*
1321 * We have to apply CLOEXEC before we change whether the process is
1322 * dumpable (in setup_new_exec) to avoid a race with a process in userspace
1323 * trying to access the should-be-closed file descriptors of a process
1324 * undergoing exec(2).
1325 */
1326 do_close_on_exec(me->files);
1327
1328 if (bprm->secureexec) {
1329 /* Make sure parent cannot signal privileged process. */
1330 me->pdeath_signal = 0;
1331
1332 /*
1333 * For secureexec, reset the stack limit to sane default to
1334 * avoid bad behavior from the prior rlimits. This has to
1335 * happen before arch_pick_mmap_layout(), which examines
1336 * RLIMIT_STACK, but after the point of no return to avoid
1337 * needing to clean up the change on failure.
1338 */
1339 if (bprm->rlim_stack.rlim_cur > _STK_LIM)
1340 bprm->rlim_stack.rlim_cur = _STK_LIM;
1341 }
1342
1343 me->sas_ss_sp = me->sas_ss_size = 0;
1344
1345 /*
1346 * Figure out dumpability. Note that this checking only of current
1347 * is wrong, but userspace depends on it. This should be testing
1348 * bprm->secureexec instead.
1349 */
1350 if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP ||
1351 !(uid_eq(current_euid(), current_uid()) &&
1352 gid_eq(current_egid(), current_gid())))
1353 set_dumpable(current->mm, suid_dumpable);
1354 else
1355 set_dumpable(current->mm, SUID_DUMP_USER);
1356
1357 perf_event_exec();
1358 __set_task_comm(me, kbasename(bprm->filename), true);
1359
1360 /* An exec changes our domain. We are no longer part of the thread
1361 group */
1362 WRITE_ONCE(me->self_exec_id, me->self_exec_id + 1);
1363 flush_signal_handlers(me, 0);
1364
1365 /*
1366 * install the new credentials for this executable
1367 */
1368 security_bprm_committing_creds(bprm);
1369
1370 commit_creds(bprm->cred);
1371 bprm->cred = NULL;
1372
1373 /*
1374 * Disable monitoring for regular users
1375 * when executing setuid binaries. Must
1376 * wait until new credentials are committed
1377 * by commit_creds() above
1378 */
1379 if (get_dumpable(me->mm) != SUID_DUMP_USER)
1380 perf_event_exit_task(me);
1381 /*
1382 * cred_guard_mutex must be held at least to this point to prevent
1383 * ptrace_attach() from altering our determination of the task's
1384 * credentials; any time after this it may be unlocked.
1385 */
1386 security_bprm_committed_creds(bprm);
1387
1388 /* Pass the opened binary to the interpreter. */
1389 if (bprm->have_execfd) {
1390 retval = get_unused_fd_flags(0);
1391 if (retval < 0)
1392 goto out_unlock;
1393 fd_install(retval, bprm->executable);
1394 bprm->executable = NULL;
1395 bprm->execfd = retval;
1396 }
1397 return 0;
1398
1399 out_unlock:
1400 up_write(&me->signal->exec_update_lock);
1401 if (!bprm->cred)
1402 mutex_unlock(&me->signal->cred_guard_mutex);
1403
1404 out:
1405 return retval;
1406 }
1407 EXPORT_SYMBOL(begin_new_exec);
1408
would_dump(struct linux_binprm * bprm,struct file * file)1409 void would_dump(struct linux_binprm *bprm, struct file *file)
1410 {
1411 struct inode *inode = file_inode(file);
1412 if (inode_permission(inode, MAY_READ) < 0) {
1413 struct user_namespace *old, *user_ns;
1414 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
1415
1416 /* Ensure mm->user_ns contains the executable */
1417 user_ns = old = bprm->mm->user_ns;
1418 while ((user_ns != &init_user_ns) &&
1419 !privileged_wrt_inode_uidgid(user_ns, inode))
1420 user_ns = user_ns->parent;
1421
1422 if (old != user_ns) {
1423 bprm->mm->user_ns = get_user_ns(user_ns);
1424 put_user_ns(old);
1425 }
1426 }
1427 }
1428 EXPORT_SYMBOL(would_dump);
1429
setup_new_exec(struct linux_binprm * bprm)1430 void setup_new_exec(struct linux_binprm * bprm)
1431 {
1432 /* Setup things that can depend upon the personality */
1433 struct task_struct *me = current;
1434
1435 arch_pick_mmap_layout(me->mm, &bprm->rlim_stack);
1436
1437 arch_setup_new_exec();
1438
1439 /* Set the new mm task size. We have to do that late because it may
1440 * depend on TIF_32BIT which is only updated in flush_thread() on
1441 * some architectures like powerpc
1442 */
1443 me->mm->task_size = TASK_SIZE;
1444 up_write(&me->signal->exec_update_lock);
1445 mutex_unlock(&me->signal->cred_guard_mutex);
1446 }
1447 EXPORT_SYMBOL(setup_new_exec);
1448
1449 /* Runs immediately before start_thread() takes over. */
finalize_exec(struct linux_binprm * bprm)1450 void finalize_exec(struct linux_binprm *bprm)
1451 {
1452 /* Store any stack rlimit changes before starting thread. */
1453 task_lock(current->group_leader);
1454 current->signal->rlim[RLIMIT_STACK] = bprm->rlim_stack;
1455 task_unlock(current->group_leader);
1456 }
1457 EXPORT_SYMBOL(finalize_exec);
1458
1459 /*
1460 * Prepare credentials and lock ->cred_guard_mutex.
1461 * setup_new_exec() commits the new creds and drops the lock.
1462 * Or, if exec fails before, free_bprm() should release ->cred and
1463 * and unlock.
1464 */
prepare_bprm_creds(struct linux_binprm * bprm)1465 static int prepare_bprm_creds(struct linux_binprm *bprm)
1466 {
1467 if (mutex_lock_interruptible(¤t->signal->cred_guard_mutex))
1468 return -ERESTARTNOINTR;
1469
1470 bprm->cred = prepare_exec_creds();
1471 if (likely(bprm->cred))
1472 return 0;
1473
1474 mutex_unlock(¤t->signal->cred_guard_mutex);
1475 return -ENOMEM;
1476 }
1477
free_bprm(struct linux_binprm * bprm)1478 static void free_bprm(struct linux_binprm *bprm)
1479 {
1480 if (bprm->mm) {
1481 acct_arg_size(bprm, 0);
1482 mmput(bprm->mm);
1483 }
1484 free_arg_pages(bprm);
1485 if (bprm->cred) {
1486 mutex_unlock(¤t->signal->cred_guard_mutex);
1487 abort_creds(bprm->cred);
1488 }
1489 if (bprm->file) {
1490 allow_write_access(bprm->file);
1491 fput(bprm->file);
1492 }
1493 if (bprm->executable)
1494 fput(bprm->executable);
1495 /* If a binfmt changed the interp, free it. */
1496 if (bprm->interp != bprm->filename)
1497 kfree(bprm->interp);
1498 kfree(bprm->fdpath);
1499 kfree(bprm);
1500 }
1501
alloc_bprm(int fd,struct filename * filename)1502 static struct linux_binprm *alloc_bprm(int fd, struct filename *filename)
1503 {
1504 struct linux_binprm *bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1505 int retval = -ENOMEM;
1506 if (!bprm)
1507 goto out;
1508
1509 if (fd == AT_FDCWD || filename->name[0] == '/') {
1510 bprm->filename = filename->name;
1511 } else {
1512 if (filename->name[0] == '\0')
1513 bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d", fd);
1514 else
1515 bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d/%s",
1516 fd, filename->name);
1517 if (!bprm->fdpath)
1518 goto out_free;
1519
1520 bprm->filename = bprm->fdpath;
1521 }
1522 bprm->interp = bprm->filename;
1523
1524 retval = bprm_mm_init(bprm);
1525 if (retval)
1526 goto out_free;
1527 return bprm;
1528
1529 out_free:
1530 free_bprm(bprm);
1531 out:
1532 return ERR_PTR(retval);
1533 }
1534
bprm_change_interp(const char * interp,struct linux_binprm * bprm)1535 int bprm_change_interp(const char *interp, struct linux_binprm *bprm)
1536 {
1537 /* If a binfmt changed the interp, free it first. */
1538 if (bprm->interp != bprm->filename)
1539 kfree(bprm->interp);
1540 bprm->interp = kstrdup(interp, GFP_KERNEL);
1541 if (!bprm->interp)
1542 return -ENOMEM;
1543 return 0;
1544 }
1545 EXPORT_SYMBOL(bprm_change_interp);
1546
1547 /*
1548 * determine how safe it is to execute the proposed program
1549 * - the caller must hold ->cred_guard_mutex to protect against
1550 * PTRACE_ATTACH or seccomp thread-sync
1551 */
check_unsafe_exec(struct linux_binprm * bprm)1552 static void check_unsafe_exec(struct linux_binprm *bprm)
1553 {
1554 struct task_struct *p = current, *t;
1555 unsigned n_fs;
1556
1557 if (p->ptrace)
1558 bprm->unsafe |= LSM_UNSAFE_PTRACE;
1559
1560 /*
1561 * This isn't strictly necessary, but it makes it harder for LSMs to
1562 * mess up.
1563 */
1564 if (task_no_new_privs(current))
1565 bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
1566
1567 t = p;
1568 n_fs = 1;
1569 spin_lock(&p->fs->lock);
1570 rcu_read_lock();
1571 while_each_thread(p, t) {
1572 if (t->fs == p->fs)
1573 n_fs++;
1574 }
1575 rcu_read_unlock();
1576
1577 if (p->fs->users > n_fs)
1578 bprm->unsafe |= LSM_UNSAFE_SHARE;
1579 else
1580 p->fs->in_exec = 1;
1581 spin_unlock(&p->fs->lock);
1582 }
1583
bprm_fill_uid(struct linux_binprm * bprm,struct file * file)1584 static void bprm_fill_uid(struct linux_binprm *bprm, struct file *file)
1585 {
1586 /* Handle suid and sgid on files */
1587 struct inode *inode;
1588 unsigned int mode;
1589 kuid_t uid;
1590 kgid_t gid;
1591 int err;
1592
1593 if (!mnt_may_suid(file->f_path.mnt))
1594 return;
1595
1596 if (task_no_new_privs(current))
1597 return;
1598
1599 inode = file->f_path.dentry->d_inode;
1600 mode = READ_ONCE(inode->i_mode);
1601 if (!(mode & (S_ISUID|S_ISGID)))
1602 return;
1603
1604 /* Be careful if suid/sgid is set */
1605 inode_lock(inode);
1606
1607 /* Atomically reload and check mode/uid/gid now that lock held. */
1608 mode = inode->i_mode;
1609 uid = inode->i_uid;
1610 gid = inode->i_gid;
1611 err = inode_permission(inode, MAY_EXEC);
1612 inode_unlock(inode);
1613
1614 /* Did the exec bit vanish out from under us? Give up. */
1615 if (err)
1616 return;
1617
1618 /* We ignore suid/sgid if there are no mappings for them in the ns */
1619 if (!kuid_has_mapping(bprm->cred->user_ns, uid) ||
1620 !kgid_has_mapping(bprm->cred->user_ns, gid))
1621 return;
1622
1623 if (mode & S_ISUID) {
1624 bprm->per_clear |= PER_CLEAR_ON_SETID;
1625 bprm->cred->euid = uid;
1626 }
1627
1628 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1629 bprm->per_clear |= PER_CLEAR_ON_SETID;
1630 bprm->cred->egid = gid;
1631 }
1632 }
1633
1634 /*
1635 * Compute brpm->cred based upon the final binary.
1636 */
bprm_creds_from_file(struct linux_binprm * bprm)1637 static int bprm_creds_from_file(struct linux_binprm *bprm)
1638 {
1639 /* Compute creds based on which file? */
1640 struct file *file = bprm->execfd_creds ? bprm->executable : bprm->file;
1641
1642 bprm_fill_uid(bprm, file);
1643 return security_bprm_creds_from_file(bprm, file);
1644 }
1645
1646 /*
1647 * Fill the binprm structure from the inode.
1648 * Read the first BINPRM_BUF_SIZE bytes
1649 *
1650 * This may be called multiple times for binary chains (scripts for example).
1651 */
prepare_binprm(struct linux_binprm * bprm)1652 static int prepare_binprm(struct linux_binprm *bprm)
1653 {
1654 loff_t pos = 0;
1655
1656 memset(bprm->buf, 0, BINPRM_BUF_SIZE);
1657 return kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE, &pos);
1658 }
1659
1660 /*
1661 * Arguments are '\0' separated strings found at the location bprm->p
1662 * points to; chop off the first by relocating brpm->p to right after
1663 * the first '\0' encountered.
1664 */
remove_arg_zero(struct linux_binprm * bprm)1665 int remove_arg_zero(struct linux_binprm *bprm)
1666 {
1667 int ret = 0;
1668 unsigned long offset;
1669 char *kaddr;
1670 struct page *page;
1671
1672 if (!bprm->argc)
1673 return 0;
1674
1675 do {
1676 offset = bprm->p & ~PAGE_MASK;
1677 page = get_arg_page(bprm, bprm->p, 0);
1678 if (!page) {
1679 ret = -EFAULT;
1680 goto out;
1681 }
1682 kaddr = kmap_atomic(page);
1683
1684 for (; offset < PAGE_SIZE && kaddr[offset];
1685 offset++, bprm->p++)
1686 ;
1687
1688 kunmap_atomic(kaddr);
1689 put_arg_page(page);
1690 } while (offset == PAGE_SIZE);
1691
1692 bprm->p++;
1693 bprm->argc--;
1694 ret = 0;
1695
1696 out:
1697 return ret;
1698 }
1699 EXPORT_SYMBOL(remove_arg_zero);
1700
1701 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1702 /*
1703 * cycle the list of binary formats handler, until one recognizes the image
1704 */
search_binary_handler(struct linux_binprm * bprm)1705 static int search_binary_handler(struct linux_binprm *bprm)
1706 {
1707 bool need_retry = IS_ENABLED(CONFIG_MODULES);
1708 struct linux_binfmt *fmt;
1709 int retval;
1710
1711 retval = prepare_binprm(bprm);
1712 if (retval < 0)
1713 return retval;
1714
1715 retval = security_bprm_check(bprm);
1716 if (retval)
1717 return retval;
1718
1719 retval = -ENOENT;
1720 retry:
1721 read_lock(&binfmt_lock);
1722 list_for_each_entry(fmt, &formats, lh) {
1723 if (!try_module_get(fmt->module))
1724 continue;
1725 read_unlock(&binfmt_lock);
1726
1727 retval = fmt->load_binary(bprm);
1728
1729 read_lock(&binfmt_lock);
1730 put_binfmt(fmt);
1731 if (bprm->point_of_no_return || (retval != -ENOEXEC)) {
1732 read_unlock(&binfmt_lock);
1733 return retval;
1734 }
1735 }
1736 read_unlock(&binfmt_lock);
1737
1738 if (need_retry) {
1739 if (printable(bprm->buf[0]) && printable(bprm->buf[1]) &&
1740 printable(bprm->buf[2]) && printable(bprm->buf[3]))
1741 return retval;
1742 if (request_module("binfmt-%04x", *(ushort *)(bprm->buf + 2)) < 0)
1743 return retval;
1744 need_retry = false;
1745 goto retry;
1746 }
1747
1748 return retval;
1749 }
1750
exec_binprm(struct linux_binprm * bprm)1751 static int exec_binprm(struct linux_binprm *bprm)
1752 {
1753 pid_t old_pid, old_vpid;
1754 int ret, depth;
1755
1756 /* Need to fetch pid before load_binary changes it */
1757 old_pid = current->pid;
1758 rcu_read_lock();
1759 old_vpid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
1760 rcu_read_unlock();
1761
1762 /* This allows 4 levels of binfmt rewrites before failing hard. */
1763 for (depth = 0;; depth++) {
1764 struct file *exec;
1765 if (depth > 5)
1766 return -ELOOP;
1767
1768 ret = search_binary_handler(bprm);
1769 if (ret < 0)
1770 return ret;
1771 if (!bprm->interpreter)
1772 break;
1773
1774 exec = bprm->file;
1775 bprm->file = bprm->interpreter;
1776 bprm->interpreter = NULL;
1777
1778 allow_write_access(exec);
1779 if (unlikely(bprm->have_execfd)) {
1780 if (bprm->executable) {
1781 fput(exec);
1782 return -ENOEXEC;
1783 }
1784 bprm->executable = exec;
1785 } else
1786 fput(exec);
1787 }
1788
1789 audit_bprm(bprm);
1790 trace_sched_process_exec(current, old_pid, bprm);
1791 ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
1792 proc_exec_connector(current);
1793 return 0;
1794 }
1795
1796 /*
1797 * sys_execve() executes a new program.
1798 */
bprm_execve(struct linux_binprm * bprm,int fd,struct filename * filename,int flags)1799 static int bprm_execve(struct linux_binprm *bprm,
1800 int fd, struct filename *filename, int flags)
1801 {
1802 struct file *file;
1803 int retval;
1804
1805 /*
1806 * Cancel any io_uring activity across execve
1807 */
1808 io_uring_task_cancel();
1809
1810 retval = prepare_bprm_creds(bprm);
1811 if (retval)
1812 return retval;
1813
1814 check_unsafe_exec(bprm);
1815 current->in_execve = 1;
1816
1817 file = do_open_execat(fd, filename, flags);
1818 retval = PTR_ERR(file);
1819 if (IS_ERR(file))
1820 goto out_unmark;
1821
1822 sched_exec();
1823
1824 bprm->file = file;
1825 /*
1826 * Record that a name derived from an O_CLOEXEC fd will be
1827 * inaccessible after exec. This allows the code in exec to
1828 * choose to fail when the executable is not mmaped into the
1829 * interpreter and an open file descriptor is not passed to
1830 * the interpreter. This makes for a better user experience
1831 * than having the interpreter start and then immediately fail
1832 * when it finds the executable is inaccessible.
1833 */
1834 if (bprm->fdpath && get_close_on_exec(fd))
1835 bprm->interp_flags |= BINPRM_FLAGS_PATH_INACCESSIBLE;
1836
1837 /* Set the unchanging part of bprm->cred */
1838 retval = security_bprm_creds_for_exec(bprm);
1839 if (retval)
1840 goto out;
1841
1842 retval = exec_binprm(bprm);
1843 if (retval < 0)
1844 goto out;
1845
1846 /* execve succeeded */
1847 current->fs->in_exec = 0;
1848 current->in_execve = 0;
1849 rseq_execve(current);
1850 acct_update_integrals(current);
1851 task_numa_free(current, false);
1852 CALL_HCK_LITE_HOOK(ced_detection_lhck, current);
1853 return retval;
1854
1855 out:
1856 /*
1857 * If past the point of no return ensure the the code never
1858 * returns to the userspace process. Use an existing fatal
1859 * signal if present otherwise terminate the process with
1860 * SIGSEGV.
1861 */
1862 if (bprm->point_of_no_return && !fatal_signal_pending(current))
1863 force_sigsegv(SIGSEGV);
1864
1865 out_unmark:
1866 current->fs->in_exec = 0;
1867 current->in_execve = 0;
1868
1869 return retval;
1870 }
1871
do_execveat_common(int fd,struct filename * filename,struct user_arg_ptr argv,struct user_arg_ptr envp,int flags)1872 static int do_execveat_common(int fd, struct filename *filename,
1873 struct user_arg_ptr argv,
1874 struct user_arg_ptr envp,
1875 int flags)
1876 {
1877 struct linux_binprm *bprm;
1878 int retval;
1879
1880 if (IS_ERR(filename))
1881 return PTR_ERR(filename);
1882
1883 /*
1884 * We move the actual failure in case of RLIMIT_NPROC excess from
1885 * set*uid() to execve() because too many poorly written programs
1886 * don't check setuid() return code. Here we additionally recheck
1887 * whether NPROC limit is still exceeded.
1888 */
1889 if ((current->flags & PF_NPROC_EXCEEDED) &&
1890 atomic_read(¤t_user()->processes) > rlimit(RLIMIT_NPROC)) {
1891 retval = -EAGAIN;
1892 goto out_ret;
1893 }
1894
1895 /* We're below the limit (still or again), so we don't want to make
1896 * further execve() calls fail. */
1897 current->flags &= ~PF_NPROC_EXCEEDED;
1898
1899 bprm = alloc_bprm(fd, filename);
1900 if (IS_ERR(bprm)) {
1901 retval = PTR_ERR(bprm);
1902 goto out_ret;
1903 }
1904
1905 retval = count(argv, MAX_ARG_STRINGS);
1906 if (retval == 0)
1907 pr_warn_once("process '%s' launched '%s' with NULL argv: empty string added\n",
1908 current->comm, bprm->filename);
1909 if (retval < 0)
1910 goto out_free;
1911 bprm->argc = retval;
1912
1913 retval = count(envp, MAX_ARG_STRINGS);
1914 if (retval < 0)
1915 goto out_free;
1916 bprm->envc = retval;
1917
1918 retval = bprm_stack_limits(bprm);
1919 if (retval < 0)
1920 goto out_free;
1921
1922 retval = copy_string_kernel(bprm->filename, bprm);
1923 if (retval < 0)
1924 goto out_free;
1925 bprm->exec = bprm->p;
1926
1927 retval = copy_strings(bprm->envc, envp, bprm);
1928 if (retval < 0)
1929 goto out_free;
1930
1931 retval = copy_strings(bprm->argc, argv, bprm);
1932 if (retval < 0)
1933 goto out_free;
1934
1935 /*
1936 * When argv is empty, add an empty string ("") as argv[0] to
1937 * ensure confused userspace programs that start processing
1938 * from argv[1] won't end up walking envp. See also
1939 * bprm_stack_limits().
1940 */
1941 if (bprm->argc == 0) {
1942 retval = copy_string_kernel("", bprm);
1943 if (retval < 0)
1944 goto out_free;
1945 bprm->argc = 1;
1946 }
1947
1948 retval = bprm_execve(bprm, fd, filename, flags);
1949 out_free:
1950 free_bprm(bprm);
1951
1952 out_ret:
1953 putname(filename);
1954 return retval;
1955 }
1956
kernel_execve(const char * kernel_filename,const char * const * argv,const char * const * envp)1957 int kernel_execve(const char *kernel_filename,
1958 const char *const *argv, const char *const *envp)
1959 {
1960 struct filename *filename;
1961 struct linux_binprm *bprm;
1962 int fd = AT_FDCWD;
1963 int retval;
1964
1965 filename = getname_kernel(kernel_filename);
1966 if (IS_ERR(filename))
1967 return PTR_ERR(filename);
1968
1969 bprm = alloc_bprm(fd, filename);
1970 if (IS_ERR(bprm)) {
1971 retval = PTR_ERR(bprm);
1972 goto out_ret;
1973 }
1974
1975 retval = count_strings_kernel(argv);
1976 if (WARN_ON_ONCE(retval == 0))
1977 retval = -EINVAL;
1978 if (retval < 0)
1979 goto out_free;
1980 bprm->argc = retval;
1981
1982 retval = count_strings_kernel(envp);
1983 if (retval < 0)
1984 goto out_free;
1985 bprm->envc = retval;
1986
1987 retval = bprm_stack_limits(bprm);
1988 if (retval < 0)
1989 goto out_free;
1990
1991 retval = copy_string_kernel(bprm->filename, bprm);
1992 if (retval < 0)
1993 goto out_free;
1994 bprm->exec = bprm->p;
1995
1996 retval = copy_strings_kernel(bprm->envc, envp, bprm);
1997 if (retval < 0)
1998 goto out_free;
1999
2000 retval = copy_strings_kernel(bprm->argc, argv, bprm);
2001 if (retval < 0)
2002 goto out_free;
2003
2004 retval = bprm_execve(bprm, fd, filename, 0);
2005 out_free:
2006 free_bprm(bprm);
2007 out_ret:
2008 putname(filename);
2009 return retval;
2010 }
2011
do_execve(struct filename * filename,const char __user * const __user * __argv,const char __user * const __user * __envp)2012 static int do_execve(struct filename *filename,
2013 const char __user *const __user *__argv,
2014 const char __user *const __user *__envp)
2015 {
2016 struct user_arg_ptr argv = { .ptr.native = __argv };
2017 struct user_arg_ptr envp = { .ptr.native = __envp };
2018 return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
2019 }
2020
do_execveat(int fd,struct filename * filename,const char __user * const __user * __argv,const char __user * const __user * __envp,int flags)2021 static int do_execveat(int fd, struct filename *filename,
2022 const char __user *const __user *__argv,
2023 const char __user *const __user *__envp,
2024 int flags)
2025 {
2026 struct user_arg_ptr argv = { .ptr.native = __argv };
2027 struct user_arg_ptr envp = { .ptr.native = __envp };
2028
2029 return do_execveat_common(fd, filename, argv, envp, flags);
2030 }
2031
2032 #ifdef CONFIG_COMPAT
compat_do_execve(struct filename * filename,const compat_uptr_t __user * __argv,const compat_uptr_t __user * __envp)2033 static int compat_do_execve(struct filename *filename,
2034 const compat_uptr_t __user *__argv,
2035 const compat_uptr_t __user *__envp)
2036 {
2037 struct user_arg_ptr argv = {
2038 .is_compat = true,
2039 .ptr.compat = __argv,
2040 };
2041 struct user_arg_ptr envp = {
2042 .is_compat = true,
2043 .ptr.compat = __envp,
2044 };
2045 return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
2046 }
2047
compat_do_execveat(int fd,struct filename * filename,const compat_uptr_t __user * __argv,const compat_uptr_t __user * __envp,int flags)2048 static int compat_do_execveat(int fd, struct filename *filename,
2049 const compat_uptr_t __user *__argv,
2050 const compat_uptr_t __user *__envp,
2051 int flags)
2052 {
2053 struct user_arg_ptr argv = {
2054 .is_compat = true,
2055 .ptr.compat = __argv,
2056 };
2057 struct user_arg_ptr envp = {
2058 .is_compat = true,
2059 .ptr.compat = __envp,
2060 };
2061 return do_execveat_common(fd, filename, argv, envp, flags);
2062 }
2063 #endif
2064
set_binfmt(struct linux_binfmt * new)2065 void set_binfmt(struct linux_binfmt *new)
2066 {
2067 struct mm_struct *mm = current->mm;
2068
2069 if (mm->binfmt)
2070 module_put(mm->binfmt->module);
2071
2072 mm->binfmt = new;
2073 if (new)
2074 __module_get(new->module);
2075 }
2076 EXPORT_SYMBOL(set_binfmt);
2077
2078 /*
2079 * set_dumpable stores three-value SUID_DUMP_* into mm->flags.
2080 */
set_dumpable(struct mm_struct * mm,int value)2081 void set_dumpable(struct mm_struct *mm, int value)
2082 {
2083 if (WARN_ON((unsigned)value > SUID_DUMP_ROOT))
2084 return;
2085
2086 set_mask_bits(&mm->flags, MMF_DUMPABLE_MASK, value);
2087 }
2088
SYSCALL_DEFINE3(execve,const char __user *,filename,const char __user * const __user *,argv,const char __user * const __user *,envp)2089 SYSCALL_DEFINE3(execve,
2090 const char __user *, filename,
2091 const char __user *const __user *, argv,
2092 const char __user *const __user *, envp)
2093 {
2094 return do_execve(getname(filename), argv, envp);
2095 }
2096
SYSCALL_DEFINE5(execveat,int,fd,const char __user *,filename,const char __user * const __user *,argv,const char __user * const __user *,envp,int,flags)2097 SYSCALL_DEFINE5(execveat,
2098 int, fd, const char __user *, filename,
2099 const char __user *const __user *, argv,
2100 const char __user *const __user *, envp,
2101 int, flags)
2102 {
2103 int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
2104
2105 return do_execveat(fd,
2106 getname_flags(filename, lookup_flags, NULL),
2107 argv, envp, flags);
2108 }
2109
2110 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE3(execve,const char __user *,filename,const compat_uptr_t __user *,argv,const compat_uptr_t __user *,envp)2111 COMPAT_SYSCALL_DEFINE3(execve, const char __user *, filename,
2112 const compat_uptr_t __user *, argv,
2113 const compat_uptr_t __user *, envp)
2114 {
2115 return compat_do_execve(getname(filename), argv, envp);
2116 }
2117
COMPAT_SYSCALL_DEFINE5(execveat,int,fd,const char __user *,filename,const compat_uptr_t __user *,argv,const compat_uptr_t __user *,envp,int,flags)2118 COMPAT_SYSCALL_DEFINE5(execveat, int, fd,
2119 const char __user *, filename,
2120 const compat_uptr_t __user *, argv,
2121 const compat_uptr_t __user *, envp,
2122 int, flags)
2123 {
2124 int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
2125
2126 return compat_do_execveat(fd,
2127 getname_flags(filename, lookup_flags, NULL),
2128 argv, envp, flags);
2129 }
2130 #endif
2131