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