xref: /linux/fs/exec.c (revision 73519ded992fc9dda2807450d6931002bb93cb16)
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 
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 
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 
107 static inline void put_binfmt(struct linux_binfmt * fmt)
108 {
109 	module_put(fmt->module);
110 }
111 
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  */
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  */
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 
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 
230 static void put_arg_page(struct page *page)
231 {
232 	put_page(page);
233 }
234 
235 static void free_arg_pages(struct linux_binprm *bprm)
236 {
237 }
238 
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 
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 
299 static bool valid_arg_len(struct linux_binprm *bprm, long len)
300 {
301 	return len <= MAX_ARG_STRLEN;
302 }
303 
304 #else
305 
306 static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
307 {
308 }
309 
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 
326 static void put_arg_page(struct page *page)
327 {
328 }
329 
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 
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 
346 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
347 		struct page *page)
348 {
349 }
350 
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 
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  */
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 
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  */
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 
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 
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 }
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  */
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  */
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  */
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 
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  */
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  */
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  */
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 & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
888 		return ERR_PTR(-EINVAL);
889 	if (flags & AT_SYMLINK_NOFOLLOW)
890 		open_exec_flags.lookup_flags &= ~LOOKUP_FOLLOW;
891 	if (flags & AT_EMPTY_PATH)
892 		open_exec_flags.lookup_flags |= LOOKUP_EMPTY;
893 
894 	file = do_filp_open(fd, name, &open_exec_flags);
895 	if (IS_ERR(file))
896 		return file;
897 
898 	/*
899 	 * In the past the regular type check was here. It moved to may_open() in
900 	 * 633fb6ac3980 ("exec: move S_ISREG() check earlier"). Since then it is
901 	 * an invariant that all non-regular files error out before we get here.
902 	 */
903 	if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode)) ||
904 	    path_noexec(&file->f_path))
905 		return ERR_PTR(-EACCES);
906 
907 	err = deny_write_access(file);
908 	if (err)
909 		return ERR_PTR(err);
910 
911 	return no_free_ptr(file);
912 }
913 
914 /**
915  * open_exec - Open a path name for execution
916  *
917  * @name: path name to open with the intent of executing it.
918  *
919  * Returns ERR_PTR on failure or allocated struct file on success.
920  *
921  * As this is a wrapper for the internal do_open_execat(), callers
922  * must call allow_write_access() before fput() on release. Also see
923  * do_close_execat().
924  */
925 struct file *open_exec(const char *name)
926 {
927 	struct filename *filename = getname_kernel(name);
928 	struct file *f = ERR_CAST(filename);
929 
930 	if (!IS_ERR(filename)) {
931 		f = do_open_execat(AT_FDCWD, filename, 0);
932 		putname(filename);
933 	}
934 	return f;
935 }
936 EXPORT_SYMBOL(open_exec);
937 
938 #if defined(CONFIG_BINFMT_FLAT) || defined(CONFIG_BINFMT_ELF_FDPIC)
939 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
940 {
941 	ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
942 	if (res > 0)
943 		flush_icache_user_range(addr, addr + len);
944 	return res;
945 }
946 EXPORT_SYMBOL(read_code);
947 #endif
948 
949 /*
950  * Maps the mm_struct mm into the current task struct.
951  * On success, this function returns with exec_update_lock
952  * held for writing.
953  */
954 static int exec_mmap(struct mm_struct *mm)
955 {
956 	struct task_struct *tsk;
957 	struct mm_struct *old_mm, *active_mm;
958 	int ret;
959 
960 	/* Notify parent that we're no longer interested in the old VM */
961 	tsk = current;
962 	old_mm = current->mm;
963 	exec_mm_release(tsk, old_mm);
964 
965 	ret = down_write_killable(&tsk->signal->exec_update_lock);
966 	if (ret)
967 		return ret;
968 
969 	if (old_mm) {
970 		/*
971 		 * If there is a pending fatal signal perhaps a signal
972 		 * whose default action is to create a coredump get
973 		 * out and die instead of going through with the exec.
974 		 */
975 		ret = mmap_read_lock_killable(old_mm);
976 		if (ret) {
977 			up_write(&tsk->signal->exec_update_lock);
978 			return ret;
979 		}
980 	}
981 
982 	task_lock(tsk);
983 	membarrier_exec_mmap(mm);
984 
985 	local_irq_disable();
986 	active_mm = tsk->active_mm;
987 	tsk->active_mm = mm;
988 	tsk->mm = mm;
989 	mm_init_cid(mm, tsk);
990 	/*
991 	 * This prevents preemption while active_mm is being loaded and
992 	 * it and mm are being updated, which could cause problems for
993 	 * lazy tlb mm refcounting when these are updated by context
994 	 * switches. Not all architectures can handle irqs off over
995 	 * activate_mm yet.
996 	 */
997 	if (!IS_ENABLED(CONFIG_ARCH_WANT_IRQS_OFF_ACTIVATE_MM))
998 		local_irq_enable();
999 	activate_mm(active_mm, mm);
1000 	if (IS_ENABLED(CONFIG_ARCH_WANT_IRQS_OFF_ACTIVATE_MM))
1001 		local_irq_enable();
1002 	lru_gen_add_mm(mm);
1003 	task_unlock(tsk);
1004 	lru_gen_use_mm(mm);
1005 	if (old_mm) {
1006 		mmap_read_unlock(old_mm);
1007 		BUG_ON(active_mm != old_mm);
1008 		setmax_mm_hiwater_rss(&tsk->signal->maxrss, old_mm);
1009 		mm_update_next_owner(old_mm);
1010 		mmput(old_mm);
1011 		return 0;
1012 	}
1013 	mmdrop_lazy_tlb(active_mm);
1014 	return 0;
1015 }
1016 
1017 static int de_thread(struct task_struct *tsk)
1018 {
1019 	struct signal_struct *sig = tsk->signal;
1020 	struct sighand_struct *oldsighand = tsk->sighand;
1021 	spinlock_t *lock = &oldsighand->siglock;
1022 
1023 	if (thread_group_empty(tsk))
1024 		goto no_thread_group;
1025 
1026 	/*
1027 	 * Kill all other threads in the thread group.
1028 	 */
1029 	spin_lock_irq(lock);
1030 	if ((sig->flags & SIGNAL_GROUP_EXIT) || sig->group_exec_task) {
1031 		/*
1032 		 * Another group action in progress, just
1033 		 * return so that the signal is processed.
1034 		 */
1035 		spin_unlock_irq(lock);
1036 		return -EAGAIN;
1037 	}
1038 
1039 	sig->group_exec_task = tsk;
1040 	sig->notify_count = zap_other_threads(tsk);
1041 	if (!thread_group_leader(tsk))
1042 		sig->notify_count--;
1043 
1044 	while (sig->notify_count) {
1045 		__set_current_state(TASK_KILLABLE);
1046 		spin_unlock_irq(lock);
1047 		schedule();
1048 		if (__fatal_signal_pending(tsk))
1049 			goto killed;
1050 		spin_lock_irq(lock);
1051 	}
1052 	spin_unlock_irq(lock);
1053 
1054 	/*
1055 	 * At this point all other threads have exited, all we have to
1056 	 * do is to wait for the thread group leader to become inactive,
1057 	 * and to assume its PID:
1058 	 */
1059 	if (!thread_group_leader(tsk)) {
1060 		struct task_struct *leader = tsk->group_leader;
1061 
1062 		for (;;) {
1063 			cgroup_threadgroup_change_begin(tsk);
1064 			write_lock_irq(&tasklist_lock);
1065 			/*
1066 			 * Do this under tasklist_lock to ensure that
1067 			 * exit_notify() can't miss ->group_exec_task
1068 			 */
1069 			sig->notify_count = -1;
1070 			if (likely(leader->exit_state))
1071 				break;
1072 			__set_current_state(TASK_KILLABLE);
1073 			write_unlock_irq(&tasklist_lock);
1074 			cgroup_threadgroup_change_end(tsk);
1075 			schedule();
1076 			if (__fatal_signal_pending(tsk))
1077 				goto killed;
1078 		}
1079 
1080 		/*
1081 		 * The only record we have of the real-time age of a
1082 		 * process, regardless of execs it's done, is start_time.
1083 		 * All the past CPU time is accumulated in signal_struct
1084 		 * from sister threads now dead.  But in this non-leader
1085 		 * exec, nothing survives from the original leader thread,
1086 		 * whose birth marks the true age of this process now.
1087 		 * When we take on its identity by switching to its PID, we
1088 		 * also take its birthdate (always earlier than our own).
1089 		 */
1090 		tsk->start_time = leader->start_time;
1091 		tsk->start_boottime = leader->start_boottime;
1092 
1093 		BUG_ON(!same_thread_group(leader, tsk));
1094 		/*
1095 		 * An exec() starts a new thread group with the
1096 		 * TGID of the previous thread group. Rehash the
1097 		 * two threads with a switched PID, and release
1098 		 * the former thread group leader:
1099 		 */
1100 
1101 		/* Become a process group leader with the old leader's pid.
1102 		 * The old leader becomes a thread of the this thread group.
1103 		 */
1104 		exchange_tids(tsk, leader);
1105 		transfer_pid(leader, tsk, PIDTYPE_TGID);
1106 		transfer_pid(leader, tsk, PIDTYPE_PGID);
1107 		transfer_pid(leader, tsk, PIDTYPE_SID);
1108 
1109 		list_replace_rcu(&leader->tasks, &tsk->tasks);
1110 		list_replace_init(&leader->sibling, &tsk->sibling);
1111 
1112 		tsk->group_leader = tsk;
1113 		leader->group_leader = tsk;
1114 
1115 		tsk->exit_signal = SIGCHLD;
1116 		leader->exit_signal = -1;
1117 
1118 		BUG_ON(leader->exit_state != EXIT_ZOMBIE);
1119 		leader->exit_state = EXIT_DEAD;
1120 		/*
1121 		 * We are going to release_task()->ptrace_unlink() silently,
1122 		 * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
1123 		 * the tracer won't block again waiting for this thread.
1124 		 */
1125 		if (unlikely(leader->ptrace))
1126 			__wake_up_parent(leader, leader->parent);
1127 		write_unlock_irq(&tasklist_lock);
1128 		cgroup_threadgroup_change_end(tsk);
1129 
1130 		release_task(leader);
1131 	}
1132 
1133 	sig->group_exec_task = NULL;
1134 	sig->notify_count = 0;
1135 
1136 no_thread_group:
1137 	/* we have changed execution domain */
1138 	tsk->exit_signal = SIGCHLD;
1139 
1140 	BUG_ON(!thread_group_leader(tsk));
1141 	return 0;
1142 
1143 killed:
1144 	/* protects against exit_notify() and __exit_signal() */
1145 	read_lock(&tasklist_lock);
1146 	sig->group_exec_task = NULL;
1147 	sig->notify_count = 0;
1148 	read_unlock(&tasklist_lock);
1149 	return -EAGAIN;
1150 }
1151 
1152 
1153 /*
1154  * This function makes sure the current process has its own signal table,
1155  * so that flush_signal_handlers can later reset the handlers without
1156  * disturbing other processes.  (Other processes might share the signal
1157  * table via the CLONE_SIGHAND option to clone().)
1158  */
1159 static int unshare_sighand(struct task_struct *me)
1160 {
1161 	struct sighand_struct *oldsighand = me->sighand;
1162 
1163 	if (refcount_read(&oldsighand->count) != 1) {
1164 		struct sighand_struct *newsighand;
1165 		/*
1166 		 * This ->sighand is shared with the CLONE_SIGHAND
1167 		 * but not CLONE_THREAD task, switch to the new one.
1168 		 */
1169 		newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1170 		if (!newsighand)
1171 			return -ENOMEM;
1172 
1173 		refcount_set(&newsighand->count, 1);
1174 
1175 		write_lock_irq(&tasklist_lock);
1176 		spin_lock(&oldsighand->siglock);
1177 		memcpy(newsighand->action, oldsighand->action,
1178 		       sizeof(newsighand->action));
1179 		rcu_assign_pointer(me->sighand, newsighand);
1180 		spin_unlock(&oldsighand->siglock);
1181 		write_unlock_irq(&tasklist_lock);
1182 
1183 		__cleanup_sighand(oldsighand);
1184 	}
1185 	return 0;
1186 }
1187 
1188 /*
1189  * These functions flushes out all traces of the currently running executable
1190  * so that a new one can be started
1191  */
1192 
1193 void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
1194 {
1195 	task_lock(tsk);
1196 	trace_task_rename(tsk, buf);
1197 	strscpy_pad(tsk->comm, buf, sizeof(tsk->comm));
1198 	task_unlock(tsk);
1199 	perf_event_comm(tsk, exec);
1200 }
1201 
1202 /*
1203  * Calling this is the point of no return. None of the failures will be
1204  * seen by userspace since either the process is already taking a fatal
1205  * signal (via de_thread() or coredump), or will have SEGV raised
1206  * (after exec_mmap()) by search_binary_handler (see below).
1207  */
1208 int begin_new_exec(struct linux_binprm * bprm)
1209 {
1210 	struct task_struct *me = current;
1211 	int retval;
1212 
1213 	/* Once we are committed compute the creds */
1214 	retval = bprm_creds_from_file(bprm);
1215 	if (retval)
1216 		return retval;
1217 
1218 	/*
1219 	 * This tracepoint marks the point before flushing the old exec where
1220 	 * the current task is still unchanged, but errors are fatal (point of
1221 	 * no return). The later "sched_process_exec" tracepoint is called after
1222 	 * the current task has successfully switched to the new exec.
1223 	 */
1224 	trace_sched_prepare_exec(current, bprm);
1225 
1226 	/*
1227 	 * Ensure all future errors are fatal.
1228 	 */
1229 	bprm->point_of_no_return = true;
1230 
1231 	/*
1232 	 * Make this the only thread in the thread group.
1233 	 */
1234 	retval = de_thread(me);
1235 	if (retval)
1236 		goto out;
1237 
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 	__set_task_comm(me, kbasename(bprm->filename), true);
1337 
1338 	/* An exec changes our domain. We are no longer part of the thread
1339 	   group */
1340 	WRITE_ONCE(me->self_exec_id, me->self_exec_id + 1);
1341 	flush_signal_handlers(me, 0);
1342 
1343 	retval = set_cred_ucounts(bprm->cred);
1344 	if (retval < 0)
1345 		goto out_unlock;
1346 
1347 	/*
1348 	 * install the new credentials for this executable
1349 	 */
1350 	security_bprm_committing_creds(bprm);
1351 
1352 	commit_creds(bprm->cred);
1353 	bprm->cred = NULL;
1354 
1355 	/*
1356 	 * Disable monitoring for regular users
1357 	 * when executing setuid binaries. Must
1358 	 * wait until new credentials are committed
1359 	 * by commit_creds() above
1360 	 */
1361 	if (get_dumpable(me->mm) != SUID_DUMP_USER)
1362 		perf_event_exit_task(me);
1363 	/*
1364 	 * cred_guard_mutex must be held at least to this point to prevent
1365 	 * ptrace_attach() from altering our determination of the task's
1366 	 * credentials; any time after this it may be unlocked.
1367 	 */
1368 	security_bprm_committed_creds(bprm);
1369 
1370 	/* Pass the opened binary to the interpreter. */
1371 	if (bprm->have_execfd) {
1372 		retval = get_unused_fd_flags(0);
1373 		if (retval < 0)
1374 			goto out_unlock;
1375 		fd_install(retval, bprm->executable);
1376 		bprm->executable = NULL;
1377 		bprm->execfd = retval;
1378 	}
1379 	return 0;
1380 
1381 out_unlock:
1382 	up_write(&me->signal->exec_update_lock);
1383 	if (!bprm->cred)
1384 		mutex_unlock(&me->signal->cred_guard_mutex);
1385 
1386 out:
1387 	return retval;
1388 }
1389 EXPORT_SYMBOL(begin_new_exec);
1390 
1391 void would_dump(struct linux_binprm *bprm, struct file *file)
1392 {
1393 	struct inode *inode = file_inode(file);
1394 	struct mnt_idmap *idmap = file_mnt_idmap(file);
1395 	if (inode_permission(idmap, inode, MAY_READ) < 0) {
1396 		struct user_namespace *old, *user_ns;
1397 		bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
1398 
1399 		/* Ensure mm->user_ns contains the executable */
1400 		user_ns = old = bprm->mm->user_ns;
1401 		while ((user_ns != &init_user_ns) &&
1402 		       !privileged_wrt_inode_uidgid(user_ns, idmap, inode))
1403 			user_ns = user_ns->parent;
1404 
1405 		if (old != user_ns) {
1406 			bprm->mm->user_ns = get_user_ns(user_ns);
1407 			put_user_ns(old);
1408 		}
1409 	}
1410 }
1411 EXPORT_SYMBOL(would_dump);
1412 
1413 void setup_new_exec(struct linux_binprm * bprm)
1414 {
1415 	/* Setup things that can depend upon the personality */
1416 	struct task_struct *me = current;
1417 
1418 	arch_pick_mmap_layout(me->mm, &bprm->rlim_stack);
1419 
1420 	arch_setup_new_exec();
1421 
1422 	/* Set the new mm task size. We have to do that late because it may
1423 	 * depend on TIF_32BIT which is only updated in flush_thread() on
1424 	 * some architectures like powerpc
1425 	 */
1426 	me->mm->task_size = TASK_SIZE;
1427 	up_write(&me->signal->exec_update_lock);
1428 	mutex_unlock(&me->signal->cred_guard_mutex);
1429 }
1430 EXPORT_SYMBOL(setup_new_exec);
1431 
1432 /* Runs immediately before start_thread() takes over. */
1433 void finalize_exec(struct linux_binprm *bprm)
1434 {
1435 	/* Store any stack rlimit changes before starting thread. */
1436 	task_lock(current->group_leader);
1437 	current->signal->rlim[RLIMIT_STACK] = bprm->rlim_stack;
1438 	task_unlock(current->group_leader);
1439 }
1440 EXPORT_SYMBOL(finalize_exec);
1441 
1442 /*
1443  * Prepare credentials and lock ->cred_guard_mutex.
1444  * setup_new_exec() commits the new creds and drops the lock.
1445  * Or, if exec fails before, free_bprm() should release ->cred
1446  * and unlock.
1447  */
1448 static int prepare_bprm_creds(struct linux_binprm *bprm)
1449 {
1450 	if (mutex_lock_interruptible(&current->signal->cred_guard_mutex))
1451 		return -ERESTARTNOINTR;
1452 
1453 	bprm->cred = prepare_exec_creds();
1454 	if (likely(bprm->cred))
1455 		return 0;
1456 
1457 	mutex_unlock(&current->signal->cred_guard_mutex);
1458 	return -ENOMEM;
1459 }
1460 
1461 /* Matches do_open_execat() */
1462 static void do_close_execat(struct file *file)
1463 {
1464 	if (!file)
1465 		return;
1466 	allow_write_access(file);
1467 	fput(file);
1468 }
1469 
1470 static void free_bprm(struct linux_binprm *bprm)
1471 {
1472 	if (bprm->mm) {
1473 		acct_arg_size(bprm, 0);
1474 		mmput(bprm->mm);
1475 	}
1476 	free_arg_pages(bprm);
1477 	if (bprm->cred) {
1478 		mutex_unlock(&current->signal->cred_guard_mutex);
1479 		abort_creds(bprm->cred);
1480 	}
1481 	do_close_execat(bprm->file);
1482 	if (bprm->executable)
1483 		fput(bprm->executable);
1484 	/* If a binfmt changed the interp, free it. */
1485 	if (bprm->interp != bprm->filename)
1486 		kfree(bprm->interp);
1487 	kfree(bprm->fdpath);
1488 	kfree(bprm);
1489 }
1490 
1491 static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int flags)
1492 {
1493 	struct linux_binprm *bprm;
1494 	struct file *file;
1495 	int retval = -ENOMEM;
1496 
1497 	file = do_open_execat(fd, filename, flags);
1498 	if (IS_ERR(file))
1499 		return ERR_CAST(file);
1500 
1501 	bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1502 	if (!bprm) {
1503 		do_close_execat(file);
1504 		return ERR_PTR(-ENOMEM);
1505 	}
1506 
1507 	bprm->file = file;
1508 
1509 	if (fd == AT_FDCWD || filename->name[0] == '/') {
1510 		bprm->filename = filename->name;
1511 	} else {
1512 		if (filename->name[0] == '\0')
1513 			bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d", fd);
1514 		else
1515 			bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d/%s",
1516 						  fd, filename->name);
1517 		if (!bprm->fdpath)
1518 			goto out_free;
1519 
1520 		/*
1521 		 * Record that a name derived from an O_CLOEXEC fd will be
1522 		 * inaccessible after exec.  This allows the code in exec to
1523 		 * choose to fail when the executable is not mmaped into the
1524 		 * interpreter and an open file descriptor is not passed to
1525 		 * the interpreter.  This makes for a better user experience
1526 		 * than having the interpreter start and then immediately fail
1527 		 * when it finds the executable is inaccessible.
1528 		 */
1529 		if (get_close_on_exec(fd))
1530 			bprm->interp_flags |= BINPRM_FLAGS_PATH_INACCESSIBLE;
1531 
1532 		bprm->filename = bprm->fdpath;
1533 	}
1534 	bprm->interp = bprm->filename;
1535 
1536 	retval = bprm_mm_init(bprm);
1537 	if (!retval)
1538 		return bprm;
1539 
1540 out_free:
1541 	free_bprm(bprm);
1542 	return ERR_PTR(retval);
1543 }
1544 
1545 int bprm_change_interp(const char *interp, struct linux_binprm *bprm)
1546 {
1547 	/* If a binfmt changed the interp, free it first. */
1548 	if (bprm->interp != bprm->filename)
1549 		kfree(bprm->interp);
1550 	bprm->interp = kstrdup(interp, GFP_KERNEL);
1551 	if (!bprm->interp)
1552 		return -ENOMEM;
1553 	return 0;
1554 }
1555 EXPORT_SYMBOL(bprm_change_interp);
1556 
1557 /*
1558  * determine how safe it is to execute the proposed program
1559  * - the caller must hold ->cred_guard_mutex to protect against
1560  *   PTRACE_ATTACH or seccomp thread-sync
1561  */
1562 static void check_unsafe_exec(struct linux_binprm *bprm)
1563 {
1564 	struct task_struct *p = current, *t;
1565 	unsigned n_fs;
1566 
1567 	if (p->ptrace)
1568 		bprm->unsafe |= LSM_UNSAFE_PTRACE;
1569 
1570 	/*
1571 	 * This isn't strictly necessary, but it makes it harder for LSMs to
1572 	 * mess up.
1573 	 */
1574 	if (task_no_new_privs(current))
1575 		bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
1576 
1577 	/*
1578 	 * If another task is sharing our fs, we cannot safely
1579 	 * suid exec because the differently privileged task
1580 	 * will be able to manipulate the current directory, etc.
1581 	 * It would be nice to force an unshare instead...
1582 	 */
1583 	n_fs = 1;
1584 	spin_lock(&p->fs->lock);
1585 	rcu_read_lock();
1586 	for_other_threads(p, t) {
1587 		if (t->fs == p->fs)
1588 			n_fs++;
1589 	}
1590 	rcu_read_unlock();
1591 
1592 	/* "users" and "in_exec" locked for copy_fs() */
1593 	if (p->fs->users > n_fs)
1594 		bprm->unsafe |= LSM_UNSAFE_SHARE;
1595 	else
1596 		p->fs->in_exec = 1;
1597 	spin_unlock(&p->fs->lock);
1598 }
1599 
1600 static void bprm_fill_uid(struct linux_binprm *bprm, struct file *file)
1601 {
1602 	/* Handle suid and sgid on files */
1603 	struct mnt_idmap *idmap;
1604 	struct inode *inode = file_inode(file);
1605 	unsigned int mode;
1606 	vfsuid_t vfsuid;
1607 	vfsgid_t vfsgid;
1608 	int err;
1609 
1610 	if (!mnt_may_suid(file->f_path.mnt))
1611 		return;
1612 
1613 	if (task_no_new_privs(current))
1614 		return;
1615 
1616 	mode = READ_ONCE(inode->i_mode);
1617 	if (!(mode & (S_ISUID|S_ISGID)))
1618 		return;
1619 
1620 	idmap = file_mnt_idmap(file);
1621 
1622 	/* Be careful if suid/sgid is set */
1623 	inode_lock(inode);
1624 
1625 	/* Atomically reload and check mode/uid/gid now that lock held. */
1626 	mode = inode->i_mode;
1627 	vfsuid = i_uid_into_vfsuid(idmap, inode);
1628 	vfsgid = i_gid_into_vfsgid(idmap, inode);
1629 	err = inode_permission(idmap, inode, MAY_EXEC);
1630 	inode_unlock(inode);
1631 
1632 	/* Did the exec bit vanish out from under us? Give up. */
1633 	if (err)
1634 		return;
1635 
1636 	/* We ignore suid/sgid if there are no mappings for them in the ns */
1637 	if (!vfsuid_has_mapping(bprm->cred->user_ns, vfsuid) ||
1638 	    !vfsgid_has_mapping(bprm->cred->user_ns, vfsgid))
1639 		return;
1640 
1641 	if (mode & S_ISUID) {
1642 		bprm->per_clear |= PER_CLEAR_ON_SETID;
1643 		bprm->cred->euid = vfsuid_into_kuid(vfsuid);
1644 	}
1645 
1646 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1647 		bprm->per_clear |= PER_CLEAR_ON_SETID;
1648 		bprm->cred->egid = vfsgid_into_kgid(vfsgid);
1649 	}
1650 }
1651 
1652 /*
1653  * Compute brpm->cred based upon the final binary.
1654  */
1655 static int bprm_creds_from_file(struct linux_binprm *bprm)
1656 {
1657 	/* Compute creds based on which file? */
1658 	struct file *file = bprm->execfd_creds ? bprm->executable : bprm->file;
1659 
1660 	bprm_fill_uid(bprm, file);
1661 	return security_bprm_creds_from_file(bprm, file);
1662 }
1663 
1664 /*
1665  * Fill the binprm structure from the inode.
1666  * Read the first BINPRM_BUF_SIZE bytes
1667  *
1668  * This may be called multiple times for binary chains (scripts for example).
1669  */
1670 static int prepare_binprm(struct linux_binprm *bprm)
1671 {
1672 	loff_t pos = 0;
1673 
1674 	memset(bprm->buf, 0, BINPRM_BUF_SIZE);
1675 	return kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE, &pos);
1676 }
1677 
1678 /*
1679  * Arguments are '\0' separated strings found at the location bprm->p
1680  * points to; chop off the first by relocating brpm->p to right after
1681  * the first '\0' encountered.
1682  */
1683 int remove_arg_zero(struct linux_binprm *bprm)
1684 {
1685 	unsigned long offset;
1686 	char *kaddr;
1687 	struct page *page;
1688 
1689 	if (!bprm->argc)
1690 		return 0;
1691 
1692 	do {
1693 		offset = bprm->p & ~PAGE_MASK;
1694 		page = get_arg_page(bprm, bprm->p, 0);
1695 		if (!page)
1696 			return -EFAULT;
1697 		kaddr = kmap_local_page(page);
1698 
1699 		for (; offset < PAGE_SIZE && kaddr[offset];
1700 				offset++, bprm->p++)
1701 			;
1702 
1703 		kunmap_local(kaddr);
1704 		put_arg_page(page);
1705 	} while (offset == PAGE_SIZE);
1706 
1707 	bprm->p++;
1708 	bprm->argc--;
1709 
1710 	return 0;
1711 }
1712 EXPORT_SYMBOL(remove_arg_zero);
1713 
1714 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1715 /*
1716  * cycle the list of binary formats handler, until one recognizes the image
1717  */
1718 static int search_binary_handler(struct linux_binprm *bprm)
1719 {
1720 	bool need_retry = IS_ENABLED(CONFIG_MODULES);
1721 	struct linux_binfmt *fmt;
1722 	int retval;
1723 
1724 	retval = prepare_binprm(bprm);
1725 	if (retval < 0)
1726 		return retval;
1727 
1728 	retval = security_bprm_check(bprm);
1729 	if (retval)
1730 		return retval;
1731 
1732 	retval = -ENOENT;
1733  retry:
1734 	read_lock(&binfmt_lock);
1735 	list_for_each_entry(fmt, &formats, lh) {
1736 		if (!try_module_get(fmt->module))
1737 			continue;
1738 		read_unlock(&binfmt_lock);
1739 
1740 		retval = fmt->load_binary(bprm);
1741 
1742 		read_lock(&binfmt_lock);
1743 		put_binfmt(fmt);
1744 		if (bprm->point_of_no_return || (retval != -ENOEXEC)) {
1745 			read_unlock(&binfmt_lock);
1746 			return retval;
1747 		}
1748 	}
1749 	read_unlock(&binfmt_lock);
1750 
1751 	if (need_retry) {
1752 		if (printable(bprm->buf[0]) && printable(bprm->buf[1]) &&
1753 		    printable(bprm->buf[2]) && printable(bprm->buf[3]))
1754 			return retval;
1755 		if (request_module("binfmt-%04x", *(ushort *)(bprm->buf + 2)) < 0)
1756 			return retval;
1757 		need_retry = false;
1758 		goto retry;
1759 	}
1760 
1761 	return retval;
1762 }
1763 
1764 /* binfmt handlers will call back into begin_new_exec() on success. */
1765 static int exec_binprm(struct linux_binprm *bprm)
1766 {
1767 	pid_t old_pid, old_vpid;
1768 	int ret, depth;
1769 
1770 	/* Need to fetch pid before load_binary changes it */
1771 	old_pid = current->pid;
1772 	rcu_read_lock();
1773 	old_vpid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
1774 	rcu_read_unlock();
1775 
1776 	/* This allows 4 levels of binfmt rewrites before failing hard. */
1777 	for (depth = 0;; depth++) {
1778 		struct file *exec;
1779 		if (depth > 5)
1780 			return -ELOOP;
1781 
1782 		ret = search_binary_handler(bprm);
1783 		if (ret < 0)
1784 			return ret;
1785 		if (!bprm->interpreter)
1786 			break;
1787 
1788 		exec = bprm->file;
1789 		bprm->file = bprm->interpreter;
1790 		bprm->interpreter = NULL;
1791 
1792 		allow_write_access(exec);
1793 		if (unlikely(bprm->have_execfd)) {
1794 			if (bprm->executable) {
1795 				fput(exec);
1796 				return -ENOEXEC;
1797 			}
1798 			bprm->executable = exec;
1799 		} else
1800 			fput(exec);
1801 	}
1802 
1803 	audit_bprm(bprm);
1804 	trace_sched_process_exec(current, old_pid, bprm);
1805 	ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
1806 	proc_exec_connector(current);
1807 	return 0;
1808 }
1809 
1810 static int bprm_execve(struct linux_binprm *bprm)
1811 {
1812 	int retval;
1813 
1814 	retval = prepare_bprm_creds(bprm);
1815 	if (retval)
1816 		return retval;
1817 
1818 	/*
1819 	 * Check for unsafe execution states before exec_binprm(), which
1820 	 * will call back into begin_new_exec(), into bprm_creds_from_file(),
1821 	 * where setuid-ness is evaluated.
1822 	 */
1823 	check_unsafe_exec(bprm);
1824 	current->in_execve = 1;
1825 	sched_mm_cid_before_execve(current);
1826 
1827 	sched_exec();
1828 
1829 	/* Set the unchanging part of bprm->cred */
1830 	retval = security_bprm_creds_for_exec(bprm);
1831 	if (retval)
1832 		goto out;
1833 
1834 	retval = exec_binprm(bprm);
1835 	if (retval < 0)
1836 		goto out;
1837 
1838 	sched_mm_cid_after_execve(current);
1839 	/* execve succeeded */
1840 	current->fs->in_exec = 0;
1841 	current->in_execve = 0;
1842 	rseq_execve(current);
1843 	user_events_execve(current);
1844 	acct_update_integrals(current);
1845 	task_numa_free(current, false);
1846 	return retval;
1847 
1848 out:
1849 	/*
1850 	 * If past the point of no return ensure the code never
1851 	 * returns to the userspace process.  Use an existing fatal
1852 	 * signal if present otherwise terminate the process with
1853 	 * SIGSEGV.
1854 	 */
1855 	if (bprm->point_of_no_return && !fatal_signal_pending(current))
1856 		force_fatal_sig(SIGSEGV);
1857 
1858 	sched_mm_cid_after_execve(current);
1859 	current->fs->in_exec = 0;
1860 	current->in_execve = 0;
1861 
1862 	return retval;
1863 }
1864 
1865 static int do_execveat_common(int fd, struct filename *filename,
1866 			      struct user_arg_ptr argv,
1867 			      struct user_arg_ptr envp,
1868 			      int flags)
1869 {
1870 	struct linux_binprm *bprm;
1871 	int retval;
1872 
1873 	if (IS_ERR(filename))
1874 		return PTR_ERR(filename);
1875 
1876 	/*
1877 	 * We move the actual failure in case of RLIMIT_NPROC excess from
1878 	 * set*uid() to execve() because too many poorly written programs
1879 	 * don't check setuid() return code.  Here we additionally recheck
1880 	 * whether NPROC limit is still exceeded.
1881 	 */
1882 	if ((current->flags & PF_NPROC_EXCEEDED) &&
1883 	    is_rlimit_overlimit(current_ucounts(), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) {
1884 		retval = -EAGAIN;
1885 		goto out_ret;
1886 	}
1887 
1888 	/* We're below the limit (still or again), so we don't want to make
1889 	 * further execve() calls fail. */
1890 	current->flags &= ~PF_NPROC_EXCEEDED;
1891 
1892 	bprm = alloc_bprm(fd, filename, flags);
1893 	if (IS_ERR(bprm)) {
1894 		retval = PTR_ERR(bprm);
1895 		goto out_ret;
1896 	}
1897 
1898 	retval = count(argv, MAX_ARG_STRINGS);
1899 	if (retval == 0)
1900 		pr_warn_once("process '%s' launched '%s' with NULL argv: empty string added\n",
1901 			     current->comm, bprm->filename);
1902 	if (retval < 0)
1903 		goto out_free;
1904 	bprm->argc = retval;
1905 
1906 	retval = count(envp, MAX_ARG_STRINGS);
1907 	if (retval < 0)
1908 		goto out_free;
1909 	bprm->envc = retval;
1910 
1911 	retval = bprm_stack_limits(bprm);
1912 	if (retval < 0)
1913 		goto out_free;
1914 
1915 	retval = copy_string_kernel(bprm->filename, bprm);
1916 	if (retval < 0)
1917 		goto out_free;
1918 	bprm->exec = bprm->p;
1919 
1920 	retval = copy_strings(bprm->envc, envp, bprm);
1921 	if (retval < 0)
1922 		goto out_free;
1923 
1924 	retval = copy_strings(bprm->argc, argv, bprm);
1925 	if (retval < 0)
1926 		goto out_free;
1927 
1928 	/*
1929 	 * When argv is empty, add an empty string ("") as argv[0] to
1930 	 * ensure confused userspace programs that start processing
1931 	 * from argv[1] won't end up walking envp. See also
1932 	 * bprm_stack_limits().
1933 	 */
1934 	if (bprm->argc == 0) {
1935 		retval = copy_string_kernel("", bprm);
1936 		if (retval < 0)
1937 			goto out_free;
1938 		bprm->argc = 1;
1939 	}
1940 
1941 	retval = bprm_execve(bprm);
1942 out_free:
1943 	free_bprm(bprm);
1944 
1945 out_ret:
1946 	putname(filename);
1947 	return retval;
1948 }
1949 
1950 int kernel_execve(const char *kernel_filename,
1951 		  const char *const *argv, const char *const *envp)
1952 {
1953 	struct filename *filename;
1954 	struct linux_binprm *bprm;
1955 	int fd = AT_FDCWD;
1956 	int retval;
1957 
1958 	/* It is non-sense for kernel threads to call execve */
1959 	if (WARN_ON_ONCE(current->flags & PF_KTHREAD))
1960 		return -EINVAL;
1961 
1962 	filename = getname_kernel(kernel_filename);
1963 	if (IS_ERR(filename))
1964 		return PTR_ERR(filename);
1965 
1966 	bprm = alloc_bprm(fd, filename, 0);
1967 	if (IS_ERR(bprm)) {
1968 		retval = PTR_ERR(bprm);
1969 		goto out_ret;
1970 	}
1971 
1972 	retval = count_strings_kernel(argv);
1973 	if (WARN_ON_ONCE(retval == 0))
1974 		retval = -EINVAL;
1975 	if (retval < 0)
1976 		goto out_free;
1977 	bprm->argc = retval;
1978 
1979 	retval = count_strings_kernel(envp);
1980 	if (retval < 0)
1981 		goto out_free;
1982 	bprm->envc = retval;
1983 
1984 	retval = bprm_stack_limits(bprm);
1985 	if (retval < 0)
1986 		goto out_free;
1987 
1988 	retval = copy_string_kernel(bprm->filename, bprm);
1989 	if (retval < 0)
1990 		goto out_free;
1991 	bprm->exec = bprm->p;
1992 
1993 	retval = copy_strings_kernel(bprm->envc, envp, bprm);
1994 	if (retval < 0)
1995 		goto out_free;
1996 
1997 	retval = copy_strings_kernel(bprm->argc, argv, bprm);
1998 	if (retval < 0)
1999 		goto out_free;
2000 
2001 	retval = bprm_execve(bprm);
2002 out_free:
2003 	free_bprm(bprm);
2004 out_ret:
2005 	putname(filename);
2006 	return retval;
2007 }
2008 
2009 static int do_execve(struct filename *filename,
2010 	const char __user *const __user *__argv,
2011 	const char __user *const __user *__envp)
2012 {
2013 	struct user_arg_ptr argv = { .ptr.native = __argv };
2014 	struct user_arg_ptr envp = { .ptr.native = __envp };
2015 	return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
2016 }
2017 
2018 static int do_execveat(int fd, struct filename *filename,
2019 		const char __user *const __user *__argv,
2020 		const char __user *const __user *__envp,
2021 		int flags)
2022 {
2023 	struct user_arg_ptr argv = { .ptr.native = __argv };
2024 	struct user_arg_ptr envp = { .ptr.native = __envp };
2025 
2026 	return do_execveat_common(fd, filename, argv, envp, flags);
2027 }
2028 
2029 #ifdef CONFIG_COMPAT
2030 static int compat_do_execve(struct filename *filename,
2031 	const compat_uptr_t __user *__argv,
2032 	const compat_uptr_t __user *__envp)
2033 {
2034 	struct user_arg_ptr argv = {
2035 		.is_compat = true,
2036 		.ptr.compat = __argv,
2037 	};
2038 	struct user_arg_ptr envp = {
2039 		.is_compat = true,
2040 		.ptr.compat = __envp,
2041 	};
2042 	return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
2043 }
2044 
2045 static int compat_do_execveat(int fd, struct filename *filename,
2046 			      const compat_uptr_t __user *__argv,
2047 			      const compat_uptr_t __user *__envp,
2048 			      int flags)
2049 {
2050 	struct user_arg_ptr argv = {
2051 		.is_compat = true,
2052 		.ptr.compat = __argv,
2053 	};
2054 	struct user_arg_ptr envp = {
2055 		.is_compat = true,
2056 		.ptr.compat = __envp,
2057 	};
2058 	return do_execveat_common(fd, filename, argv, envp, flags);
2059 }
2060 #endif
2061 
2062 void set_binfmt(struct linux_binfmt *new)
2063 {
2064 	struct mm_struct *mm = current->mm;
2065 
2066 	if (mm->binfmt)
2067 		module_put(mm->binfmt->module);
2068 
2069 	mm->binfmt = new;
2070 	if (new)
2071 		__module_get(new->module);
2072 }
2073 EXPORT_SYMBOL(set_binfmt);
2074 
2075 /*
2076  * set_dumpable stores three-value SUID_DUMP_* into mm->flags.
2077  */
2078 void set_dumpable(struct mm_struct *mm, int value)
2079 {
2080 	if (WARN_ON((unsigned)value > SUID_DUMP_ROOT))
2081 		return;
2082 
2083 	set_mask_bits(&mm->flags, MMF_DUMPABLE_MASK, value);
2084 }
2085 
2086 SYSCALL_DEFINE3(execve,
2087 		const char __user *, filename,
2088 		const char __user *const __user *, argv,
2089 		const char __user *const __user *, envp)
2090 {
2091 	return do_execve(getname(filename), argv, envp);
2092 }
2093 
2094 SYSCALL_DEFINE5(execveat,
2095 		int, fd, const char __user *, filename,
2096 		const char __user *const __user *, argv,
2097 		const char __user *const __user *, envp,
2098 		int, flags)
2099 {
2100 	return do_execveat(fd,
2101 			   getname_uflags(filename, flags),
2102 			   argv, envp, flags);
2103 }
2104 
2105 #ifdef CONFIG_COMPAT
2106 COMPAT_SYSCALL_DEFINE3(execve, const char __user *, filename,
2107 	const compat_uptr_t __user *, argv,
2108 	const compat_uptr_t __user *, envp)
2109 {
2110 	return compat_do_execve(getname(filename), argv, envp);
2111 }
2112 
2113 COMPAT_SYSCALL_DEFINE5(execveat, int, fd,
2114 		       const char __user *, filename,
2115 		       const compat_uptr_t __user *, argv,
2116 		       const compat_uptr_t __user *, envp,
2117 		       int,  flags)
2118 {
2119 	return compat_do_execveat(fd,
2120 				  getname_uflags(filename, flags),
2121 				  argv, envp, flags);
2122 }
2123 #endif
2124 
2125 #ifdef CONFIG_SYSCTL
2126 
2127 static int proc_dointvec_minmax_coredump(const struct ctl_table *table, int write,
2128 		void *buffer, size_t *lenp, loff_t *ppos)
2129 {
2130 	int error = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
2131 
2132 	if (!error)
2133 		validate_coredump_safety();
2134 	return error;
2135 }
2136 
2137 static struct ctl_table fs_exec_sysctls[] = {
2138 	{
2139 		.procname	= "suid_dumpable",
2140 		.data		= &suid_dumpable,
2141 		.maxlen		= sizeof(int),
2142 		.mode		= 0644,
2143 		.proc_handler	= proc_dointvec_minmax_coredump,
2144 		.extra1		= SYSCTL_ZERO,
2145 		.extra2		= SYSCTL_TWO,
2146 	},
2147 };
2148 
2149 static int __init init_fs_exec_sysctls(void)
2150 {
2151 	register_sysctl_init("fs", fs_exec_sysctls);
2152 	return 0;
2153 }
2154 
2155 fs_initcall(init_fs_exec_sysctls);
2156 #endif /* CONFIG_SYSCTL */
2157 
2158 #ifdef CONFIG_EXEC_KUNIT_TEST
2159 #include "tests/exec_kunit.c"
2160 #endif
2161