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