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