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