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