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 bprm->exec -= stack_shift;
759
760 if (mmap_write_lock_killable(mm))
761 return -EINTR;
762
763 vm_flags = VM_STACK_FLAGS;
764
765 /*
766 * Adjust stack execute permissions; explicitly enable for
767 * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
768 * (arch default) otherwise.
769 */
770 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
771 vm_flags |= VM_EXEC;
772 else if (executable_stack == EXSTACK_DISABLE_X)
773 vm_flags &= ~VM_EXEC;
774 vm_flags |= mm->def_flags;
775 vm_flags |= VM_STACK_INCOMPLETE_SETUP;
776
777 vma_iter_init(&vmi, mm, vma->vm_start);
778
779 tlb_gather_mmu(&tlb, mm);
780 ret = mprotect_fixup(&vmi, &tlb, vma, &prev, vma->vm_start, vma->vm_end,
781 vm_flags);
782 tlb_finish_mmu(&tlb);
783
784 if (ret)
785 goto out_unlock;
786 BUG_ON(prev != vma);
787
788 if (unlikely(vm_flags & VM_EXEC)) {
789 pr_warn_once("process '%pD4' started with executable stack\n",
790 bprm->file);
791 }
792
793 /* Move stack pages down in memory. */
794 if (stack_shift) {
795 /*
796 * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
797 * the binfmt code determines where the new stack should reside, we shift it to
798 * its final location.
799 */
800 ret = relocate_vma_down(vma, stack_shift);
801 if (ret)
802 goto out_unlock;
803 }
804
805 /* mprotect_fixup is overkill to remove the temporary stack flags */
806 vm_flags_clear(vma, VM_STACK_INCOMPLETE_SETUP);
807
808 stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
809 stack_size = vma->vm_end - vma->vm_start;
810 /*
811 * Align this down to a page boundary as expand_stack
812 * will align it up.
813 */
814 rlim_stack = bprm->rlim_stack.rlim_cur & PAGE_MASK;
815
816 stack_expand = min(rlim_stack, stack_size + stack_expand);
817
818 #ifdef CONFIG_STACK_GROWSUP
819 stack_base = vma->vm_start + stack_expand;
820 #else
821 stack_base = vma->vm_end - stack_expand;
822 #endif
823 current->mm->start_stack = bprm->p;
824 ret = expand_stack_locked(vma, stack_base);
825 if (ret)
826 ret = -EFAULT;
827
828 out_unlock:
829 mmap_write_unlock(mm);
830 return ret;
831 }
832 EXPORT_SYMBOL(setup_arg_pages);
833
834 #else
835
836 /*
837 * Transfer the program arguments and environment from the holding pages
838 * onto the stack. The provided stack pointer is adjusted accordingly.
839 */
transfer_args_to_stack(struct linux_binprm * bprm,unsigned long * sp_location)840 int transfer_args_to_stack(struct linux_binprm *bprm,
841 unsigned long *sp_location)
842 {
843 unsigned long index, stop, sp;
844 int ret = 0;
845
846 stop = bprm->p >> PAGE_SHIFT;
847 sp = *sp_location;
848
849 for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
850 unsigned int offset = index == stop ? bprm->p & ~PAGE_MASK : 0;
851 char *src = kmap_local_page(bprm->page[index]) + offset;
852 sp -= PAGE_SIZE - offset;
853 if (copy_to_user((void *) sp, src, PAGE_SIZE - offset) != 0)
854 ret = -EFAULT;
855 kunmap_local(src);
856 if (ret)
857 goto out;
858 }
859
860 bprm->exec += *sp_location - MAX_ARG_PAGES * PAGE_SIZE;
861 *sp_location = sp;
862
863 out:
864 return ret;
865 }
866 EXPORT_SYMBOL(transfer_args_to_stack);
867
868 #endif /* CONFIG_MMU */
869
870 /*
871 * On success, caller must call do_close_execat() on the returned
872 * struct file to close it.
873 */
do_open_execat(int fd,struct filename * name,int flags)874 static struct file *do_open_execat(int fd, struct filename *name, int flags)
875 {
876 int err;
877 struct file *file __free(fput) = NULL;
878 struct open_flags open_exec_flags = {
879 .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
880 .acc_mode = MAY_EXEC,
881 .intent = LOOKUP_OPEN,
882 .lookup_flags = LOOKUP_FOLLOW,
883 };
884
885 if ((flags &
886 ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH | AT_EXECVE_CHECK)) != 0)
887 return ERR_PTR(-EINVAL);
888 if (flags & AT_SYMLINK_NOFOLLOW)
889 open_exec_flags.lookup_flags &= ~LOOKUP_FOLLOW;
890 if (flags & AT_EMPTY_PATH)
891 open_exec_flags.lookup_flags |= LOOKUP_EMPTY;
892
893 file = do_filp_open(fd, name, &open_exec_flags);
894 if (IS_ERR(file))
895 return file;
896
897 /*
898 * In the past the regular type check was here. It moved to may_open() in
899 * 633fb6ac3980 ("exec: move S_ISREG() check earlier"). Since then it is
900 * an invariant that all non-regular files error out before we get here.
901 */
902 if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode)) ||
903 path_noexec(&file->f_path))
904 return ERR_PTR(-EACCES);
905
906 err = exe_file_deny_write_access(file);
907 if (err)
908 return ERR_PTR(err);
909
910 return no_free_ptr(file);
911 }
912
913 /**
914 * open_exec - Open a path name for execution
915 *
916 * @name: path name to open with the intent of executing it.
917 *
918 * Returns ERR_PTR on failure or allocated struct file on success.
919 *
920 * As this is a wrapper for the internal do_open_execat(), callers
921 * must call exe_file_allow_write_access() before fput() on release. Also see
922 * do_close_execat().
923 */
open_exec(const char * name)924 struct file *open_exec(const char *name)
925 {
926 struct filename *filename = getname_kernel(name);
927 struct file *f = ERR_CAST(filename);
928
929 if (!IS_ERR(filename)) {
930 f = do_open_execat(AT_FDCWD, filename, 0);
931 putname(filename);
932 }
933 return f;
934 }
935 EXPORT_SYMBOL(open_exec);
936
937 #if defined(CONFIG_BINFMT_FLAT) || defined(CONFIG_BINFMT_ELF_FDPIC)
read_code(struct file * file,unsigned long addr,loff_t pos,size_t len)938 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
939 {
940 ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
941 if (res > 0)
942 flush_icache_user_range(addr, addr + len);
943 return res;
944 }
945 EXPORT_SYMBOL(read_code);
946 #endif
947
948 /*
949 * Maps the mm_struct mm into the current task struct.
950 * On success, this function returns with exec_update_lock
951 * held for writing.
952 */
exec_mmap(struct mm_struct * mm)953 static int exec_mmap(struct mm_struct *mm)
954 {
955 struct task_struct *tsk;
956 struct mm_struct *old_mm, *active_mm;
957 int ret;
958
959 /* Notify parent that we're no longer interested in the old VM */
960 tsk = current;
961 old_mm = current->mm;
962 exec_mm_release(tsk, old_mm);
963
964 ret = down_write_killable(&tsk->signal->exec_update_lock);
965 if (ret)
966 return ret;
967
968 if (old_mm) {
969 /*
970 * If there is a pending fatal signal perhaps a signal
971 * whose default action is to create a coredump get
972 * out and die instead of going through with the exec.
973 */
974 ret = mmap_read_lock_killable(old_mm);
975 if (ret) {
976 up_write(&tsk->signal->exec_update_lock);
977 return ret;
978 }
979 }
980
981 task_lock(tsk);
982 membarrier_exec_mmap(mm);
983
984 local_irq_disable();
985 active_mm = tsk->active_mm;
986 tsk->active_mm = mm;
987 tsk->mm = mm;
988 mm_init_cid(mm, tsk);
989 /*
990 * This prevents preemption while active_mm is being loaded and
991 * it and mm are being updated, which could cause problems for
992 * lazy tlb mm refcounting when these are updated by context
993 * switches. Not all architectures can handle irqs off over
994 * activate_mm yet.
995 */
996 if (!IS_ENABLED(CONFIG_ARCH_WANT_IRQS_OFF_ACTIVATE_MM))
997 local_irq_enable();
998 activate_mm(active_mm, mm);
999 if (IS_ENABLED(CONFIG_ARCH_WANT_IRQS_OFF_ACTIVATE_MM))
1000 local_irq_enable();
1001 lru_gen_add_mm(mm);
1002 task_unlock(tsk);
1003 lru_gen_use_mm(mm);
1004 if (old_mm) {
1005 mmap_read_unlock(old_mm);
1006 BUG_ON(active_mm != old_mm);
1007 setmax_mm_hiwater_rss(&tsk->signal->maxrss, old_mm);
1008 mm_update_next_owner(old_mm);
1009 mmput(old_mm);
1010 return 0;
1011 }
1012 mmdrop_lazy_tlb(active_mm);
1013 return 0;
1014 }
1015
de_thread(struct task_struct * tsk)1016 static int de_thread(struct task_struct *tsk)
1017 {
1018 struct signal_struct *sig = tsk->signal;
1019 struct sighand_struct *oldsighand = tsk->sighand;
1020 spinlock_t *lock = &oldsighand->siglock;
1021
1022 if (thread_group_empty(tsk))
1023 goto no_thread_group;
1024
1025 /*
1026 * Kill all other threads in the thread group.
1027 */
1028 spin_lock_irq(lock);
1029 if ((sig->flags & SIGNAL_GROUP_EXIT) || sig->group_exec_task) {
1030 /*
1031 * Another group action in progress, just
1032 * return so that the signal is processed.
1033 */
1034 spin_unlock_irq(lock);
1035 return -EAGAIN;
1036 }
1037
1038 sig->group_exec_task = tsk;
1039 sig->notify_count = zap_other_threads(tsk);
1040 if (!thread_group_leader(tsk))
1041 sig->notify_count--;
1042
1043 while (sig->notify_count) {
1044 __set_current_state(TASK_KILLABLE);
1045 spin_unlock_irq(lock);
1046 schedule();
1047 if (__fatal_signal_pending(tsk))
1048 goto killed;
1049 spin_lock_irq(lock);
1050 }
1051 spin_unlock_irq(lock);
1052
1053 /*
1054 * At this point all other threads have exited, all we have to
1055 * do is to wait for the thread group leader to become inactive,
1056 * and to assume its PID:
1057 */
1058 if (!thread_group_leader(tsk)) {
1059 struct task_struct *leader = tsk->group_leader;
1060
1061 for (;;) {
1062 cgroup_threadgroup_change_begin(tsk);
1063 write_lock_irq(&tasklist_lock);
1064 /*
1065 * Do this under tasklist_lock to ensure that
1066 * exit_notify() can't miss ->group_exec_task
1067 */
1068 sig->notify_count = -1;
1069 if (likely(leader->exit_state))
1070 break;
1071 __set_current_state(TASK_KILLABLE);
1072 write_unlock_irq(&tasklist_lock);
1073 cgroup_threadgroup_change_end(tsk);
1074 schedule();
1075 if (__fatal_signal_pending(tsk))
1076 goto killed;
1077 }
1078
1079 /*
1080 * The only record we have of the real-time age of a
1081 * process, regardless of execs it's done, is start_time.
1082 * All the past CPU time is accumulated in signal_struct
1083 * from sister threads now dead. But in this non-leader
1084 * exec, nothing survives from the original leader thread,
1085 * whose birth marks the true age of this process now.
1086 * When we take on its identity by switching to its PID, we
1087 * also take its birthdate (always earlier than our own).
1088 */
1089 tsk->start_time = leader->start_time;
1090 tsk->start_boottime = leader->start_boottime;
1091
1092 BUG_ON(!same_thread_group(leader, tsk));
1093 /*
1094 * An exec() starts a new thread group with the
1095 * TGID of the previous thread group. Rehash the
1096 * two threads with a switched PID, and release
1097 * the former thread group leader:
1098 */
1099
1100 /* Become a process group leader with the old leader's pid.
1101 * The old leader becomes a thread of the this thread group.
1102 */
1103 exchange_tids(tsk, leader);
1104 transfer_pid(leader, tsk, PIDTYPE_TGID);
1105 transfer_pid(leader, tsk, PIDTYPE_PGID);
1106 transfer_pid(leader, tsk, PIDTYPE_SID);
1107
1108 list_replace_rcu(&leader->tasks, &tsk->tasks);
1109 list_replace_init(&leader->sibling, &tsk->sibling);
1110
1111 tsk->group_leader = tsk;
1112 leader->group_leader = tsk;
1113
1114 tsk->exit_signal = SIGCHLD;
1115 leader->exit_signal = -1;
1116
1117 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
1118 leader->exit_state = EXIT_DEAD;
1119 /*
1120 * We are going to release_task()->ptrace_unlink() silently,
1121 * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
1122 * the tracer won't block again waiting for this thread.
1123 */
1124 if (unlikely(leader->ptrace))
1125 __wake_up_parent(leader, leader->parent);
1126 write_unlock_irq(&tasklist_lock);
1127 cgroup_threadgroup_change_end(tsk);
1128
1129 release_task(leader);
1130 }
1131
1132 sig->group_exec_task = NULL;
1133 sig->notify_count = 0;
1134
1135 no_thread_group:
1136 /* we have changed execution domain */
1137 tsk->exit_signal = SIGCHLD;
1138
1139 BUG_ON(!thread_group_leader(tsk));
1140 return 0;
1141
1142 killed:
1143 /* protects against exit_notify() and __exit_signal() */
1144 read_lock(&tasklist_lock);
1145 sig->group_exec_task = NULL;
1146 sig->notify_count = 0;
1147 read_unlock(&tasklist_lock);
1148 return -EAGAIN;
1149 }
1150
1151
1152 /*
1153 * This function makes sure the current process has its own signal table,
1154 * so that flush_signal_handlers can later reset the handlers without
1155 * disturbing other processes. (Other processes might share the signal
1156 * table via the CLONE_SIGHAND option to clone().)
1157 */
unshare_sighand(struct task_struct * me)1158 static int unshare_sighand(struct task_struct *me)
1159 {
1160 struct sighand_struct *oldsighand = me->sighand;
1161
1162 if (refcount_read(&oldsighand->count) != 1) {
1163 struct sighand_struct *newsighand;
1164 /*
1165 * This ->sighand is shared with the CLONE_SIGHAND
1166 * but not CLONE_THREAD task, switch to the new one.
1167 */
1168 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1169 if (!newsighand)
1170 return -ENOMEM;
1171
1172 refcount_set(&newsighand->count, 1);
1173
1174 write_lock_irq(&tasklist_lock);
1175 spin_lock(&oldsighand->siglock);
1176 memcpy(newsighand->action, oldsighand->action,
1177 sizeof(newsighand->action));
1178 rcu_assign_pointer(me->sighand, newsighand);
1179 spin_unlock(&oldsighand->siglock);
1180 write_unlock_irq(&tasklist_lock);
1181
1182 __cleanup_sighand(oldsighand);
1183 }
1184 return 0;
1185 }
1186
1187 /*
1188 * This is unlocked -- the string will always be NUL-terminated, but
1189 * may show overlapping contents if racing concurrent reads.
1190 */
__set_task_comm(struct task_struct * tsk,const char * buf,bool exec)1191 void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
1192 {
1193 size_t len = min(strlen(buf), sizeof(tsk->comm) - 1);
1194
1195 trace_task_rename(tsk, buf);
1196 memcpy(tsk->comm, buf, len);
1197 memset(&tsk->comm[len], 0, sizeof(tsk->comm) - len);
1198 perf_event_comm(tsk, exec);
1199 }
1200
1201 /*
1202 * Calling this is the point of no return. None of the failures will be
1203 * seen by userspace since either the process is already taking a fatal
1204 * signal (via de_thread() or coredump), or will have SEGV raised
1205 * (after exec_mmap()) by search_binary_handler (see below).
1206 */
begin_new_exec(struct linux_binprm * bprm)1207 int begin_new_exec(struct linux_binprm * bprm)
1208 {
1209 struct task_struct *me = current;
1210 int retval;
1211
1212 /* Once we are committed compute the creds */
1213 retval = bprm_creds_from_file(bprm);
1214 if (retval)
1215 return retval;
1216
1217 /*
1218 * This tracepoint marks the point before flushing the old exec where
1219 * the current task is still unchanged, but errors are fatal (point of
1220 * no return). The later "sched_process_exec" tracepoint is called after
1221 * the current task has successfully switched to the new exec.
1222 */
1223 trace_sched_prepare_exec(current, bprm);
1224
1225 /*
1226 * Ensure all future errors are fatal.
1227 */
1228 bprm->point_of_no_return = true;
1229
1230 /* Make this the only thread in the thread group */
1231 retval = de_thread(me);
1232 if (retval)
1233 goto out;
1234 /* see the comment in check_unsafe_exec() */
1235 current->fs->in_exec = 0;
1236 /*
1237 * Cancel any io_uring activity across execve
1238 */
1239 io_uring_task_cancel();
1240
1241 /* Ensure the files table is not shared. */
1242 retval = unshare_files();
1243 if (retval)
1244 goto out;
1245
1246 /*
1247 * Must be called _before_ exec_mmap() as bprm->mm is
1248 * not visible until then. Doing it here also ensures
1249 * we don't race against replace_mm_exe_file().
1250 */
1251 retval = set_mm_exe_file(bprm->mm, bprm->file);
1252 if (retval)
1253 goto out;
1254
1255 /* If the binary is not readable then enforce mm->dumpable=0 */
1256 would_dump(bprm, bprm->file);
1257 if (bprm->have_execfd)
1258 would_dump(bprm, bprm->executable);
1259
1260 /*
1261 * Release all of the old mmap stuff
1262 */
1263 acct_arg_size(bprm, 0);
1264 retval = exec_mmap(bprm->mm);
1265 if (retval)
1266 goto out;
1267
1268 bprm->mm = NULL;
1269
1270 retval = exec_task_namespaces();
1271 if (retval)
1272 goto out_unlock;
1273
1274 #ifdef CONFIG_POSIX_TIMERS
1275 spin_lock_irq(&me->sighand->siglock);
1276 posix_cpu_timers_exit(me);
1277 spin_unlock_irq(&me->sighand->siglock);
1278 exit_itimers(me);
1279 flush_itimer_signals();
1280 #endif
1281
1282 /*
1283 * Make the signal table private.
1284 */
1285 retval = unshare_sighand(me);
1286 if (retval)
1287 goto out_unlock;
1288
1289 me->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC |
1290 PF_NOFREEZE | PF_NO_SETAFFINITY);
1291 flush_thread();
1292 me->personality &= ~bprm->per_clear;
1293
1294 clear_syscall_work_syscall_user_dispatch(me);
1295
1296 /*
1297 * We have to apply CLOEXEC before we change whether the process is
1298 * dumpable (in setup_new_exec) to avoid a race with a process in userspace
1299 * trying to access the should-be-closed file descriptors of a process
1300 * undergoing exec(2).
1301 */
1302 do_close_on_exec(me->files);
1303
1304 if (bprm->secureexec) {
1305 /* Make sure parent cannot signal privileged process. */
1306 me->pdeath_signal = 0;
1307
1308 /*
1309 * For secureexec, reset the stack limit to sane default to
1310 * avoid bad behavior from the prior rlimits. This has to
1311 * happen before arch_pick_mmap_layout(), which examines
1312 * RLIMIT_STACK, but after the point of no return to avoid
1313 * needing to clean up the change on failure.
1314 */
1315 if (bprm->rlim_stack.rlim_cur > _STK_LIM)
1316 bprm->rlim_stack.rlim_cur = _STK_LIM;
1317 }
1318
1319 me->sas_ss_sp = me->sas_ss_size = 0;
1320
1321 /*
1322 * Figure out dumpability. Note that this checking only of current
1323 * is wrong, but userspace depends on it. This should be testing
1324 * bprm->secureexec instead.
1325 */
1326 if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP ||
1327 !(uid_eq(current_euid(), current_uid()) &&
1328 gid_eq(current_egid(), current_gid())))
1329 set_dumpable(current->mm, suid_dumpable);
1330 else
1331 set_dumpable(current->mm, SUID_DUMP_USER);
1332
1333 perf_event_exec();
1334
1335 /*
1336 * If the original filename was empty, alloc_bprm() made up a path
1337 * that will probably not be useful to admins running ps or similar.
1338 * Let's fix it up to be something reasonable.
1339 */
1340 if (bprm->comm_from_dentry) {
1341 /*
1342 * Hold RCU lock to keep the name from being freed behind our back.
1343 * Use acquire semantics to make sure the terminating NUL from
1344 * __d_alloc() is seen.
1345 *
1346 * Note, we're deliberately sloppy here. We don't need to care about
1347 * detecting a concurrent rename and just want a terminated name.
1348 */
1349 rcu_read_lock();
1350 __set_task_comm(me, smp_load_acquire(&bprm->file->f_path.dentry->d_name.name),
1351 true);
1352 rcu_read_unlock();
1353 } else {
1354 __set_task_comm(me, kbasename(bprm->filename), true);
1355 }
1356
1357 /* An exec changes our domain. We are no longer part of the thread
1358 group */
1359 WRITE_ONCE(me->self_exec_id, me->self_exec_id + 1);
1360 flush_signal_handlers(me, 0);
1361
1362 retval = set_cred_ucounts(bprm->cred);
1363 if (retval < 0)
1364 goto out_unlock;
1365
1366 /*
1367 * install the new credentials for this executable
1368 */
1369 security_bprm_committing_creds(bprm);
1370
1371 commit_creds(bprm->cred);
1372 bprm->cred = NULL;
1373
1374 /*
1375 * Disable monitoring for regular users
1376 * when executing setuid binaries. Must
1377 * wait until new credentials are committed
1378 * by commit_creds() above
1379 */
1380 if (get_dumpable(me->mm) != SUID_DUMP_USER)
1381 perf_event_exit_task(me);
1382 /*
1383 * cred_guard_mutex must be held at least to this point to prevent
1384 * ptrace_attach() from altering our determination of the task's
1385 * credentials; any time after this it may be unlocked.
1386 */
1387 security_bprm_committed_creds(bprm);
1388
1389 /* Pass the opened binary to the interpreter. */
1390 if (bprm->have_execfd) {
1391 retval = get_unused_fd_flags(0);
1392 if (retval < 0)
1393 goto out_unlock;
1394 fd_install(retval, bprm->executable);
1395 bprm->executable = NULL;
1396 bprm->execfd = retval;
1397 }
1398 return 0;
1399
1400 out_unlock:
1401 up_write(&me->signal->exec_update_lock);
1402 if (!bprm->cred)
1403 mutex_unlock(&me->signal->cred_guard_mutex);
1404
1405 out:
1406 return retval;
1407 }
1408 EXPORT_SYMBOL(begin_new_exec);
1409
would_dump(struct linux_binprm * bprm,struct file * file)1410 void would_dump(struct linux_binprm *bprm, struct file *file)
1411 {
1412 struct inode *inode = file_inode(file);
1413 struct mnt_idmap *idmap = file_mnt_idmap(file);
1414 if (inode_permission(idmap, inode, MAY_READ) < 0) {
1415 struct user_namespace *old, *user_ns;
1416 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
1417
1418 /* Ensure mm->user_ns contains the executable */
1419 user_ns = old = bprm->mm->user_ns;
1420 while ((user_ns != &init_user_ns) &&
1421 !privileged_wrt_inode_uidgid(user_ns, idmap, inode))
1422 user_ns = user_ns->parent;
1423
1424 if (old != user_ns) {
1425 bprm->mm->user_ns = get_user_ns(user_ns);
1426 put_user_ns(old);
1427 }
1428 }
1429 }
1430 EXPORT_SYMBOL(would_dump);
1431
setup_new_exec(struct linux_binprm * bprm)1432 void setup_new_exec(struct linux_binprm * bprm)
1433 {
1434 /* Setup things that can depend upon the personality */
1435 struct task_struct *me = current;
1436
1437 arch_pick_mmap_layout(me->mm, &bprm->rlim_stack);
1438
1439 arch_setup_new_exec();
1440
1441 /* Set the new mm task size. We have to do that late because it may
1442 * depend on TIF_32BIT which is only updated in flush_thread() on
1443 * some architectures like powerpc
1444 */
1445 me->mm->task_size = TASK_SIZE;
1446 up_write(&me->signal->exec_update_lock);
1447 mutex_unlock(&me->signal->cred_guard_mutex);
1448 }
1449 EXPORT_SYMBOL(setup_new_exec);
1450
1451 /* Runs immediately before start_thread() takes over. */
finalize_exec(struct linux_binprm * bprm)1452 void finalize_exec(struct linux_binprm *bprm)
1453 {
1454 /* Store any stack rlimit changes before starting thread. */
1455 task_lock(current->group_leader);
1456 current->signal->rlim[RLIMIT_STACK] = bprm->rlim_stack;
1457 task_unlock(current->group_leader);
1458 }
1459 EXPORT_SYMBOL(finalize_exec);
1460
1461 /*
1462 * Prepare credentials and lock ->cred_guard_mutex.
1463 * setup_new_exec() commits the new creds and drops the lock.
1464 * Or, if exec fails before, free_bprm() should release ->cred
1465 * and unlock.
1466 */
prepare_bprm_creds(struct linux_binprm * bprm)1467 static int prepare_bprm_creds(struct linux_binprm *bprm)
1468 {
1469 if (mutex_lock_interruptible(¤t->signal->cred_guard_mutex))
1470 return -ERESTARTNOINTR;
1471
1472 bprm->cred = prepare_exec_creds();
1473 if (likely(bprm->cred))
1474 return 0;
1475
1476 mutex_unlock(¤t->signal->cred_guard_mutex);
1477 return -ENOMEM;
1478 }
1479
1480 /* Matches do_open_execat() */
do_close_execat(struct file * file)1481 static void do_close_execat(struct file *file)
1482 {
1483 if (!file)
1484 return;
1485 exe_file_allow_write_access(file);
1486 fput(file);
1487 }
1488
free_bprm(struct linux_binprm * bprm)1489 static void free_bprm(struct linux_binprm *bprm)
1490 {
1491 if (bprm->mm) {
1492 acct_arg_size(bprm, 0);
1493 mmput(bprm->mm);
1494 }
1495 free_arg_pages(bprm);
1496 if (bprm->cred) {
1497 /* in case exec fails before de_thread() succeeds */
1498 current->fs->in_exec = 0;
1499 mutex_unlock(¤t->signal->cred_guard_mutex);
1500 abort_creds(bprm->cred);
1501 }
1502 do_close_execat(bprm->file);
1503 if (bprm->executable)
1504 fput(bprm->executable);
1505 /* If a binfmt changed the interp, free it. */
1506 if (bprm->interp != bprm->filename)
1507 kfree(bprm->interp);
1508 kfree(bprm->fdpath);
1509 kfree(bprm);
1510 }
1511
alloc_bprm(int fd,struct filename * filename,int flags)1512 static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int flags)
1513 {
1514 struct linux_binprm *bprm;
1515 struct file *file;
1516 int retval = -ENOMEM;
1517
1518 file = do_open_execat(fd, filename, flags);
1519 if (IS_ERR(file))
1520 return ERR_CAST(file);
1521
1522 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1523 if (!bprm) {
1524 do_close_execat(file);
1525 return ERR_PTR(-ENOMEM);
1526 }
1527
1528 bprm->file = file;
1529
1530 if (fd == AT_FDCWD || filename->name[0] == '/') {
1531 bprm->filename = filename->name;
1532 } else {
1533 if (filename->name[0] == '\0') {
1534 bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d", fd);
1535 bprm->comm_from_dentry = 1;
1536 } else {
1537 bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d/%s",
1538 fd, filename->name);
1539 }
1540 if (!bprm->fdpath)
1541 goto out_free;
1542
1543 /*
1544 * Record that a name derived from an O_CLOEXEC fd will be
1545 * inaccessible after exec. This allows the code in exec to
1546 * choose to fail when the executable is not mmaped into the
1547 * interpreter and an open file descriptor is not passed to
1548 * the interpreter. This makes for a better user experience
1549 * than having the interpreter start and then immediately fail
1550 * when it finds the executable is inaccessible.
1551 */
1552 if (get_close_on_exec(fd))
1553 bprm->interp_flags |= BINPRM_FLAGS_PATH_INACCESSIBLE;
1554
1555 bprm->filename = bprm->fdpath;
1556 }
1557 bprm->interp = bprm->filename;
1558
1559 /*
1560 * At this point, security_file_open() has already been called (with
1561 * __FMODE_EXEC) and access control checks for AT_EXECVE_CHECK will
1562 * stop just after the security_bprm_creds_for_exec() call in
1563 * bprm_execve(). Indeed, the kernel should not try to parse the
1564 * content of the file with exec_binprm() nor change the calling
1565 * thread, which means that the following security functions will not
1566 * be called:
1567 * - security_bprm_check()
1568 * - security_bprm_creds_from_file()
1569 * - security_bprm_committing_creds()
1570 * - security_bprm_committed_creds()
1571 */
1572 bprm->is_check = !!(flags & AT_EXECVE_CHECK);
1573
1574 retval = bprm_mm_init(bprm);
1575 if (!retval)
1576 return bprm;
1577
1578 out_free:
1579 free_bprm(bprm);
1580 return ERR_PTR(retval);
1581 }
1582
bprm_change_interp(const char * interp,struct linux_binprm * bprm)1583 int bprm_change_interp(const char *interp, struct linux_binprm *bprm)
1584 {
1585 /* If a binfmt changed the interp, free it first. */
1586 if (bprm->interp != bprm->filename)
1587 kfree(bprm->interp);
1588 bprm->interp = kstrdup(interp, GFP_KERNEL);
1589 if (!bprm->interp)
1590 return -ENOMEM;
1591 return 0;
1592 }
1593 EXPORT_SYMBOL(bprm_change_interp);
1594
1595 /*
1596 * determine how safe it is to execute the proposed program
1597 * - the caller must hold ->cred_guard_mutex to protect against
1598 * PTRACE_ATTACH or seccomp thread-sync
1599 */
check_unsafe_exec(struct linux_binprm * bprm)1600 static void check_unsafe_exec(struct linux_binprm *bprm)
1601 {
1602 struct task_struct *p = current, *t;
1603 unsigned n_fs;
1604
1605 if (p->ptrace)
1606 bprm->unsafe |= LSM_UNSAFE_PTRACE;
1607
1608 /*
1609 * This isn't strictly necessary, but it makes it harder for LSMs to
1610 * mess up.
1611 */
1612 if (task_no_new_privs(current))
1613 bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
1614
1615 /*
1616 * If another task is sharing our fs, we cannot safely
1617 * suid exec because the differently privileged task
1618 * will be able to manipulate the current directory, etc.
1619 * It would be nice to force an unshare instead...
1620 *
1621 * Otherwise we set fs->in_exec = 1 to deny clone(CLONE_FS)
1622 * from another sub-thread until de_thread() succeeds, this
1623 * state is protected by cred_guard_mutex we hold.
1624 */
1625 n_fs = 1;
1626 spin_lock(&p->fs->lock);
1627 rcu_read_lock();
1628 for_other_threads(p, t) {
1629 if (t->fs == p->fs)
1630 n_fs++;
1631 }
1632 rcu_read_unlock();
1633
1634 /* "users" and "in_exec" locked for copy_fs() */
1635 if (p->fs->users > n_fs)
1636 bprm->unsafe |= LSM_UNSAFE_SHARE;
1637 else
1638 p->fs->in_exec = 1;
1639 spin_unlock(&p->fs->lock);
1640 }
1641
bprm_fill_uid(struct linux_binprm * bprm,struct file * file)1642 static void bprm_fill_uid(struct linux_binprm *bprm, struct file *file)
1643 {
1644 /* Handle suid and sgid on files */
1645 struct mnt_idmap *idmap;
1646 struct inode *inode = file_inode(file);
1647 unsigned int mode;
1648 vfsuid_t vfsuid;
1649 vfsgid_t vfsgid;
1650 int err;
1651
1652 if (!mnt_may_suid(file->f_path.mnt))
1653 return;
1654
1655 if (task_no_new_privs(current))
1656 return;
1657
1658 mode = READ_ONCE(inode->i_mode);
1659 if (!(mode & (S_ISUID|S_ISGID)))
1660 return;
1661
1662 idmap = file_mnt_idmap(file);
1663
1664 /* Be careful if suid/sgid is set */
1665 inode_lock(inode);
1666
1667 /* Atomically reload and check mode/uid/gid now that lock held. */
1668 mode = inode->i_mode;
1669 vfsuid = i_uid_into_vfsuid(idmap, inode);
1670 vfsgid = i_gid_into_vfsgid(idmap, inode);
1671 err = inode_permission(idmap, inode, MAY_EXEC);
1672 inode_unlock(inode);
1673
1674 /* Did the exec bit vanish out from under us? Give up. */
1675 if (err)
1676 return;
1677
1678 /* We ignore suid/sgid if there are no mappings for them in the ns */
1679 if (!vfsuid_has_mapping(bprm->cred->user_ns, vfsuid) ||
1680 !vfsgid_has_mapping(bprm->cred->user_ns, vfsgid))
1681 return;
1682
1683 if (mode & S_ISUID) {
1684 bprm->per_clear |= PER_CLEAR_ON_SETID;
1685 bprm->cred->euid = vfsuid_into_kuid(vfsuid);
1686 }
1687
1688 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1689 bprm->per_clear |= PER_CLEAR_ON_SETID;
1690 bprm->cred->egid = vfsgid_into_kgid(vfsgid);
1691 }
1692 }
1693
1694 /*
1695 * Compute brpm->cred based upon the final binary.
1696 */
bprm_creds_from_file(struct linux_binprm * bprm)1697 static int bprm_creds_from_file(struct linux_binprm *bprm)
1698 {
1699 /* Compute creds based on which file? */
1700 struct file *file = bprm->execfd_creds ? bprm->executable : bprm->file;
1701
1702 bprm_fill_uid(bprm, file);
1703 return security_bprm_creds_from_file(bprm, file);
1704 }
1705
1706 /*
1707 * Fill the binprm structure from the inode.
1708 * Read the first BINPRM_BUF_SIZE bytes
1709 *
1710 * This may be called multiple times for binary chains (scripts for example).
1711 */
prepare_binprm(struct linux_binprm * bprm)1712 static int prepare_binprm(struct linux_binprm *bprm)
1713 {
1714 loff_t pos = 0;
1715
1716 memset(bprm->buf, 0, BINPRM_BUF_SIZE);
1717 return kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE, &pos);
1718 }
1719
1720 /*
1721 * Arguments are '\0' separated strings found at the location bprm->p
1722 * points to; chop off the first by relocating brpm->p to right after
1723 * the first '\0' encountered.
1724 */
remove_arg_zero(struct linux_binprm * bprm)1725 int remove_arg_zero(struct linux_binprm *bprm)
1726 {
1727 unsigned long offset;
1728 char *kaddr;
1729 struct page *page;
1730
1731 if (!bprm->argc)
1732 return 0;
1733
1734 do {
1735 offset = bprm->p & ~PAGE_MASK;
1736 page = get_arg_page(bprm, bprm->p, 0);
1737 if (!page)
1738 return -EFAULT;
1739 kaddr = kmap_local_page(page);
1740
1741 for (; offset < PAGE_SIZE && kaddr[offset];
1742 offset++, bprm->p++)
1743 ;
1744
1745 kunmap_local(kaddr);
1746 put_arg_page(page);
1747 } while (offset == PAGE_SIZE);
1748
1749 bprm->p++;
1750 bprm->argc--;
1751
1752 return 0;
1753 }
1754 EXPORT_SYMBOL(remove_arg_zero);
1755
1756 /*
1757 * cycle the list of binary formats handler, until one recognizes the image
1758 */
search_binary_handler(struct linux_binprm * bprm)1759 static int search_binary_handler(struct linux_binprm *bprm)
1760 {
1761 struct linux_binfmt *fmt;
1762 int retval;
1763
1764 retval = prepare_binprm(bprm);
1765 if (retval < 0)
1766 return retval;
1767
1768 retval = security_bprm_check(bprm);
1769 if (retval)
1770 return retval;
1771
1772 read_lock(&binfmt_lock);
1773 list_for_each_entry(fmt, &formats, lh) {
1774 if (!try_module_get(fmt->module))
1775 continue;
1776 read_unlock(&binfmt_lock);
1777
1778 retval = fmt->load_binary(bprm);
1779
1780 read_lock(&binfmt_lock);
1781 put_binfmt(fmt);
1782 if (bprm->point_of_no_return || (retval != -ENOEXEC)) {
1783 read_unlock(&binfmt_lock);
1784 return retval;
1785 }
1786 }
1787 read_unlock(&binfmt_lock);
1788
1789 return -ENOEXEC;
1790 }
1791
1792 /* binfmt handlers will call back into begin_new_exec() on success. */
exec_binprm(struct linux_binprm * bprm)1793 static int exec_binprm(struct linux_binprm *bprm)
1794 {
1795 pid_t old_pid, old_vpid;
1796 int ret, depth;
1797
1798 /* Need to fetch pid before load_binary changes it */
1799 old_pid = current->pid;
1800 rcu_read_lock();
1801 old_vpid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
1802 rcu_read_unlock();
1803
1804 /* This allows 4 levels of binfmt rewrites before failing hard. */
1805 for (depth = 0;; depth++) {
1806 struct file *exec;
1807 if (depth > 5)
1808 return -ELOOP;
1809
1810 ret = search_binary_handler(bprm);
1811 if (ret < 0)
1812 return ret;
1813 if (!bprm->interpreter)
1814 break;
1815
1816 exec = bprm->file;
1817 bprm->file = bprm->interpreter;
1818 bprm->interpreter = NULL;
1819
1820 exe_file_allow_write_access(exec);
1821 if (unlikely(bprm->have_execfd)) {
1822 if (bprm->executable) {
1823 fput(exec);
1824 return -ENOEXEC;
1825 }
1826 bprm->executable = exec;
1827 } else
1828 fput(exec);
1829 }
1830
1831 audit_bprm(bprm);
1832 trace_sched_process_exec(current, old_pid, bprm);
1833 ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
1834 proc_exec_connector(current);
1835 return 0;
1836 }
1837
bprm_execve(struct linux_binprm * bprm)1838 static int bprm_execve(struct linux_binprm *bprm)
1839 {
1840 int retval;
1841
1842 retval = prepare_bprm_creds(bprm);
1843 if (retval)
1844 return retval;
1845
1846 /*
1847 * Check for unsafe execution states before exec_binprm(), which
1848 * will call back into begin_new_exec(), into bprm_creds_from_file(),
1849 * where setuid-ness is evaluated.
1850 */
1851 check_unsafe_exec(bprm);
1852 current->in_execve = 1;
1853 sched_mm_cid_before_execve(current);
1854
1855 sched_exec();
1856
1857 /* Set the unchanging part of bprm->cred */
1858 retval = security_bprm_creds_for_exec(bprm);
1859 if (retval || bprm->is_check)
1860 goto out;
1861
1862 retval = exec_binprm(bprm);
1863 if (retval < 0)
1864 goto out;
1865
1866 sched_mm_cid_after_execve(current);
1867 rseq_execve(current);
1868 /* execve succeeded */
1869 current->in_execve = 0;
1870 user_events_execve(current);
1871 acct_update_integrals(current);
1872 task_numa_free(current, false);
1873 return retval;
1874
1875 out:
1876 /*
1877 * If past the point of no return ensure the code never
1878 * returns to the userspace process. Use an existing fatal
1879 * signal if present otherwise terminate the process with
1880 * SIGSEGV.
1881 */
1882 if (bprm->point_of_no_return && !fatal_signal_pending(current))
1883 force_fatal_sig(SIGSEGV);
1884
1885 sched_mm_cid_after_execve(current);
1886 rseq_set_notify_resume(current);
1887 current->in_execve = 0;
1888
1889 return retval;
1890 }
1891
do_execveat_common(int fd,struct filename * filename,struct user_arg_ptr argv,struct user_arg_ptr envp,int flags)1892 static int do_execveat_common(int fd, struct filename *filename,
1893 struct user_arg_ptr argv,
1894 struct user_arg_ptr envp,
1895 int flags)
1896 {
1897 struct linux_binprm *bprm;
1898 int retval;
1899
1900 if (IS_ERR(filename))
1901 return PTR_ERR(filename);
1902
1903 /*
1904 * We move the actual failure in case of RLIMIT_NPROC excess from
1905 * set*uid() to execve() because too many poorly written programs
1906 * don't check setuid() return code. Here we additionally recheck
1907 * whether NPROC limit is still exceeded.
1908 */
1909 if ((current->flags & PF_NPROC_EXCEEDED) &&
1910 is_rlimit_overlimit(current_ucounts(), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) {
1911 retval = -EAGAIN;
1912 goto out_ret;
1913 }
1914
1915 /* We're below the limit (still or again), so we don't want to make
1916 * further execve() calls fail. */
1917 current->flags &= ~PF_NPROC_EXCEEDED;
1918
1919 bprm = alloc_bprm(fd, filename, flags);
1920 if (IS_ERR(bprm)) {
1921 retval = PTR_ERR(bprm);
1922 goto out_ret;
1923 }
1924
1925 retval = count(argv, MAX_ARG_STRINGS);
1926 if (retval < 0)
1927 goto out_free;
1928 bprm->argc = retval;
1929
1930 retval = count(envp, MAX_ARG_STRINGS);
1931 if (retval < 0)
1932 goto out_free;
1933 bprm->envc = retval;
1934
1935 retval = bprm_stack_limits(bprm);
1936 if (retval < 0)
1937 goto out_free;
1938
1939 retval = copy_string_kernel(bprm->filename, bprm);
1940 if (retval < 0)
1941 goto out_free;
1942 bprm->exec = bprm->p;
1943
1944 retval = copy_strings(bprm->envc, envp, bprm);
1945 if (retval < 0)
1946 goto out_free;
1947
1948 retval = copy_strings(bprm->argc, argv, bprm);
1949 if (retval < 0)
1950 goto out_free;
1951
1952 /*
1953 * When argv is empty, add an empty string ("") as argv[0] to
1954 * ensure confused userspace programs that start processing
1955 * from argv[1] won't end up walking envp. See also
1956 * bprm_stack_limits().
1957 */
1958 if (bprm->argc == 0) {
1959 retval = copy_string_kernel("", bprm);
1960 if (retval < 0)
1961 goto out_free;
1962 bprm->argc = 1;
1963
1964 pr_warn_once("process '%s' launched '%s' with NULL argv: empty string added\n",
1965 current->comm, bprm->filename);
1966 }
1967
1968 retval = bprm_execve(bprm);
1969 out_free:
1970 free_bprm(bprm);
1971
1972 out_ret:
1973 putname(filename);
1974 return retval;
1975 }
1976
kernel_execve(const char * kernel_filename,const char * const * argv,const char * const * envp)1977 int kernel_execve(const char *kernel_filename,
1978 const char *const *argv, const char *const *envp)
1979 {
1980 struct filename *filename;
1981 struct linux_binprm *bprm;
1982 int fd = AT_FDCWD;
1983 int retval;
1984
1985 /* It is non-sense for kernel threads to call execve */
1986 if (WARN_ON_ONCE(current->flags & PF_KTHREAD))
1987 return -EINVAL;
1988
1989 filename = getname_kernel(kernel_filename);
1990 if (IS_ERR(filename))
1991 return PTR_ERR(filename);
1992
1993 bprm = alloc_bprm(fd, filename, 0);
1994 if (IS_ERR(bprm)) {
1995 retval = PTR_ERR(bprm);
1996 goto out_ret;
1997 }
1998
1999 retval = count_strings_kernel(argv);
2000 if (WARN_ON_ONCE(retval == 0))
2001 retval = -EINVAL;
2002 if (retval < 0)
2003 goto out_free;
2004 bprm->argc = retval;
2005
2006 retval = count_strings_kernel(envp);
2007 if (retval < 0)
2008 goto out_free;
2009 bprm->envc = retval;
2010
2011 retval = bprm_stack_limits(bprm);
2012 if (retval < 0)
2013 goto out_free;
2014
2015 retval = copy_string_kernel(bprm->filename, bprm);
2016 if (retval < 0)
2017 goto out_free;
2018 bprm->exec = bprm->p;
2019
2020 retval = copy_strings_kernel(bprm->envc, envp, bprm);
2021 if (retval < 0)
2022 goto out_free;
2023
2024 retval = copy_strings_kernel(bprm->argc, argv, bprm);
2025 if (retval < 0)
2026 goto out_free;
2027
2028 retval = bprm_execve(bprm);
2029 out_free:
2030 free_bprm(bprm);
2031 out_ret:
2032 putname(filename);
2033 return retval;
2034 }
2035
do_execve(struct filename * filename,const char __user * const __user * __argv,const char __user * const __user * __envp)2036 static int do_execve(struct filename *filename,
2037 const char __user *const __user *__argv,
2038 const char __user *const __user *__envp)
2039 {
2040 struct user_arg_ptr argv = { .ptr.native = __argv };
2041 struct user_arg_ptr envp = { .ptr.native = __envp };
2042 return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
2043 }
2044
do_execveat(int fd,struct filename * filename,const char __user * const __user * __argv,const char __user * const __user * __envp,int flags)2045 static int do_execveat(int fd, struct filename *filename,
2046 const char __user *const __user *__argv,
2047 const char __user *const __user *__envp,
2048 int flags)
2049 {
2050 struct user_arg_ptr argv = { .ptr.native = __argv };
2051 struct user_arg_ptr envp = { .ptr.native = __envp };
2052
2053 return do_execveat_common(fd, filename, argv, envp, flags);
2054 }
2055
2056 #ifdef CONFIG_COMPAT
compat_do_execve(struct filename * filename,const compat_uptr_t __user * __argv,const compat_uptr_t __user * __envp)2057 static int compat_do_execve(struct filename *filename,
2058 const compat_uptr_t __user *__argv,
2059 const compat_uptr_t __user *__envp)
2060 {
2061 struct user_arg_ptr argv = {
2062 .is_compat = true,
2063 .ptr.compat = __argv,
2064 };
2065 struct user_arg_ptr envp = {
2066 .is_compat = true,
2067 .ptr.compat = __envp,
2068 };
2069 return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
2070 }
2071
compat_do_execveat(int fd,struct filename * filename,const compat_uptr_t __user * __argv,const compat_uptr_t __user * __envp,int flags)2072 static int compat_do_execveat(int fd, struct filename *filename,
2073 const compat_uptr_t __user *__argv,
2074 const compat_uptr_t __user *__envp,
2075 int flags)
2076 {
2077 struct user_arg_ptr argv = {
2078 .is_compat = true,
2079 .ptr.compat = __argv,
2080 };
2081 struct user_arg_ptr envp = {
2082 .is_compat = true,
2083 .ptr.compat = __envp,
2084 };
2085 return do_execveat_common(fd, filename, argv, envp, flags);
2086 }
2087 #endif
2088
set_binfmt(struct linux_binfmt * new)2089 void set_binfmt(struct linux_binfmt *new)
2090 {
2091 struct mm_struct *mm = current->mm;
2092
2093 if (mm->binfmt)
2094 module_put(mm->binfmt->module);
2095
2096 mm->binfmt = new;
2097 if (new)
2098 __module_get(new->module);
2099 }
2100 EXPORT_SYMBOL(set_binfmt);
2101
2102 /*
2103 * set_dumpable stores three-value SUID_DUMP_* into mm->flags.
2104 */
set_dumpable(struct mm_struct * mm,int value)2105 void set_dumpable(struct mm_struct *mm, int value)
2106 {
2107 if (WARN_ON((unsigned)value > SUID_DUMP_ROOT))
2108 return;
2109
2110 set_mask_bits(&mm->flags, MMF_DUMPABLE_MASK, value);
2111 }
2112
SYSCALL_DEFINE3(execve,const char __user *,filename,const char __user * const __user *,argv,const char __user * const __user *,envp)2113 SYSCALL_DEFINE3(execve,
2114 const char __user *, filename,
2115 const char __user *const __user *, argv,
2116 const char __user *const __user *, envp)
2117 {
2118 return do_execve(getname(filename), argv, envp);
2119 }
2120
SYSCALL_DEFINE5(execveat,int,fd,const char __user *,filename,const char __user * const __user *,argv,const char __user * const __user *,envp,int,flags)2121 SYSCALL_DEFINE5(execveat,
2122 int, fd, const char __user *, filename,
2123 const char __user *const __user *, argv,
2124 const char __user *const __user *, envp,
2125 int, flags)
2126 {
2127 return do_execveat(fd,
2128 getname_uflags(filename, flags),
2129 argv, envp, flags);
2130 }
2131
2132 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE3(execve,const char __user *,filename,const compat_uptr_t __user *,argv,const compat_uptr_t __user *,envp)2133 COMPAT_SYSCALL_DEFINE3(execve, const char __user *, filename,
2134 const compat_uptr_t __user *, argv,
2135 const compat_uptr_t __user *, envp)
2136 {
2137 return compat_do_execve(getname(filename), argv, envp);
2138 }
2139
COMPAT_SYSCALL_DEFINE5(execveat,int,fd,const char __user *,filename,const compat_uptr_t __user *,argv,const compat_uptr_t __user *,envp,int,flags)2140 COMPAT_SYSCALL_DEFINE5(execveat, int, fd,
2141 const char __user *, filename,
2142 const compat_uptr_t __user *, argv,
2143 const compat_uptr_t __user *, envp,
2144 int, flags)
2145 {
2146 return compat_do_execveat(fd,
2147 getname_uflags(filename, flags),
2148 argv, envp, flags);
2149 }
2150 #endif
2151
2152 #ifdef CONFIG_SYSCTL
2153
proc_dointvec_minmax_coredump(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)2154 static int proc_dointvec_minmax_coredump(const struct ctl_table *table, int write,
2155 void *buffer, size_t *lenp, loff_t *ppos)
2156 {
2157 int error = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
2158
2159 if (!error)
2160 validate_coredump_safety();
2161 return error;
2162 }
2163
2164 static const struct ctl_table fs_exec_sysctls[] = {
2165 {
2166 .procname = "suid_dumpable",
2167 .data = &suid_dumpable,
2168 .maxlen = sizeof(int),
2169 .mode = 0644,
2170 .proc_handler = proc_dointvec_minmax_coredump,
2171 .extra1 = SYSCTL_ZERO,
2172 .extra2 = SYSCTL_TWO,
2173 },
2174 };
2175
init_fs_exec_sysctls(void)2176 static int __init init_fs_exec_sysctls(void)
2177 {
2178 register_sysctl_init("fs", fs_exec_sysctls);
2179 return 0;
2180 }
2181
2182 fs_initcall(init_fs_exec_sysctls);
2183 #endif /* CONFIG_SYSCTL */
2184
2185 #ifdef CONFIG_EXEC_KUNIT_TEST
2186 #include "tests/exec_kunit.c"
2187 #endif
2188