Lines Matching refs:bprm

79 static int bprm_creds_from_file(struct linux_binprm *bprm);
185 static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages) in acct_arg_size() argument
188 long diff = (long)(pages - bprm->vma_pages); in acct_arg_size()
193 bprm->vma_pages = pages; in acct_arg_size()
197 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos, in get_arg_page() argument
201 struct vm_area_struct *vma = bprm->vma; in get_arg_page()
202 struct mm_struct *mm = bprm->mm; in get_arg_page()
233 acct_arg_size(bprm, vma_pages(vma)); in get_arg_page()
243 static void free_arg_pages(struct linux_binprm *bprm) in free_arg_pages() argument
247 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos, in flush_arg_page() argument
250 flush_cache_page(bprm->vma, pos, page_to_pfn(page)); in flush_arg_page()
253 static int __bprm_mm_init(struct linux_binprm *bprm) in __bprm_mm_init() argument
257 struct mm_struct *mm = bprm->mm; in __bprm_mm_init()
259 bprm->vma = vma = vm_area_alloc(mm); in __bprm_mm_init()
287 bprm->p = vma->vm_end - sizeof(void *); in __bprm_mm_init()
292 bprm->vma = NULL; in __bprm_mm_init()
297 static bool valid_arg_len(struct linux_binprm *bprm, long len) in valid_arg_len() argument
304 static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages) in acct_arg_size() argument
308 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos, in get_arg_page() argument
313 page = bprm->page[pos / PAGE_SIZE]; in get_arg_page()
318 bprm->page[pos / PAGE_SIZE] = page; in get_arg_page()
328 static void free_arg_page(struct linux_binprm *bprm, int i) in free_arg_page() argument
330 if (bprm->page[i]) { in free_arg_page()
331 __free_page(bprm->page[i]); in free_arg_page()
332 bprm->page[i] = NULL; in free_arg_page()
336 static void free_arg_pages(struct linux_binprm *bprm) in free_arg_pages() argument
341 free_arg_page(bprm, i); in free_arg_pages()
344 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos, in flush_arg_page() argument
349 static int __bprm_mm_init(struct linux_binprm *bprm) in __bprm_mm_init() argument
351 bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *); in __bprm_mm_init()
355 static bool valid_arg_len(struct linux_binprm *bprm, long len) in valid_arg_len() argument
357 return len <= bprm->p; in valid_arg_len()
368 static int bprm_mm_init(struct linux_binprm *bprm) in bprm_mm_init() argument
373 bprm->mm = mm = mm_alloc(); in bprm_mm_init()
380 bprm->rlim_stack = current->signal->rlim[RLIMIT_STACK]; in bprm_mm_init()
383 err = __bprm_mm_init(bprm); in bprm_mm_init()
391 bprm->mm = NULL; in bprm_mm_init()
477 static int bprm_stack_limits(struct linux_binprm *bprm) in bprm_stack_limits() argument
490 limit = min(limit, bprm->rlim_stack.rlim_cur / 4); in bprm_stack_limits()
509 ptr_size = (max(bprm->argc, 1) + bprm->envc) * sizeof(void *); in bprm_stack_limits()
514 bprm->argmin = bprm->p - limit; in bprm_stack_limits()
524 struct linux_binprm *bprm) in copy_strings() argument
546 if (!valid_arg_len(bprm, len)) in copy_strings()
550 pos = bprm->p; in copy_strings()
552 bprm->p -= len; in copy_strings()
554 if (bprm->p < bprm->argmin) in copy_strings()
583 page = get_arg_page(bprm, pos, 1); in copy_strings()
597 flush_arg_page(bprm, kpos, kmapped_page); in copy_strings()
618 int copy_string_kernel(const char *arg, struct linux_binprm *bprm) in copy_string_kernel() argument
621 unsigned long pos = bprm->p; in copy_string_kernel()
625 if (!valid_arg_len(bprm, len)) in copy_string_kernel()
630 bprm->p -= len; in copy_string_kernel()
631 if (IS_ENABLED(CONFIG_MMU) && bprm->p < bprm->argmin) in copy_string_kernel()
643 page = get_arg_page(bprm, pos, 1); in copy_string_kernel()
646 flush_arg_page(bprm, pos & PAGE_MASK, page); in copy_string_kernel()
656 struct linux_binprm *bprm) in copy_strings_kernel() argument
659 int ret = copy_string_kernel(argv[argc], bprm); in copy_strings_kernel()
749 int setup_arg_pages(struct linux_binprm *bprm, in setup_arg_pages() argument
756 struct vm_area_struct *vma = bprm->vma; in setup_arg_pages()
768 stack_base = bprm->rlim_stack.rlim_max; in setup_arg_pages()
782 mm->arg_start = bprm->p - stack_shift; in setup_arg_pages()
783 bprm->p = vma->vm_end - stack_shift; in setup_arg_pages()
794 bprm->p -= stack_shift; in setup_arg_pages()
795 mm->arg_start = bprm->p; in setup_arg_pages()
798 if (bprm->loader) in setup_arg_pages()
799 bprm->loader -= stack_shift; in setup_arg_pages()
800 bprm->exec -= stack_shift; in setup_arg_pages()
832 bprm->file); in setup_arg_pages()
851 rlim_stack = bprm->rlim_stack.rlim_cur & PAGE_MASK; in setup_arg_pages()
860 current->mm->start_stack = bprm->p; in setup_arg_pages()
877 int transfer_args_to_stack(struct linux_binprm *bprm, in transfer_args_to_stack() argument
883 stop = bprm->p >> PAGE_SHIFT; in transfer_args_to_stack()
887 unsigned int offset = index == stop ? bprm->p & ~PAGE_MASK : 0; in transfer_args_to_stack()
888 char *src = kmap_local_page(bprm->page[index]) + offset; in transfer_args_to_stack()
1246 int begin_new_exec(struct linux_binprm * bprm) in begin_new_exec() argument
1252 retval = bprm_creds_from_file(bprm); in begin_new_exec()
1259 bprm->point_of_no_return = true; in begin_new_exec()
1283 retval = set_mm_exe_file(bprm->mm, bprm->file); in begin_new_exec()
1288 would_dump(bprm, bprm->file); in begin_new_exec()
1289 if (bprm->have_execfd) in begin_new_exec()
1290 would_dump(bprm, bprm->executable); in begin_new_exec()
1295 acct_arg_size(bprm, 0); in begin_new_exec()
1296 retval = exec_mmap(bprm->mm); in begin_new_exec()
1300 bprm->mm = NULL; in begin_new_exec()
1324 me->personality &= ~bprm->per_clear; in begin_new_exec()
1336 if (bprm->secureexec) { in begin_new_exec()
1347 if (bprm->rlim_stack.rlim_cur > _STK_LIM) in begin_new_exec()
1348 bprm->rlim_stack.rlim_cur = _STK_LIM; in begin_new_exec()
1358 if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP || in begin_new_exec()
1366 __set_task_comm(me, kbasename(bprm->filename), true); in begin_new_exec()
1373 retval = set_cred_ucounts(bprm->cred); in begin_new_exec()
1380 security_bprm_committing_creds(bprm); in begin_new_exec()
1382 commit_creds(bprm->cred); in begin_new_exec()
1383 bprm->cred = NULL; in begin_new_exec()
1398 security_bprm_committed_creds(bprm); in begin_new_exec()
1401 if (bprm->have_execfd) { in begin_new_exec()
1405 fd_install(retval, bprm->executable); in begin_new_exec()
1406 bprm->executable = NULL; in begin_new_exec()
1407 bprm->execfd = retval; in begin_new_exec()
1413 if (!bprm->cred) in begin_new_exec()
1421 void would_dump(struct linux_binprm *bprm, struct file *file) in would_dump() argument
1427 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP; in would_dump()
1430 user_ns = old = bprm->mm->user_ns; in would_dump()
1436 bprm->mm->user_ns = get_user_ns(user_ns); in would_dump()
1443 void setup_new_exec(struct linux_binprm * bprm) in setup_new_exec() argument
1448 arch_pick_mmap_layout(me->mm, &bprm->rlim_stack); in setup_new_exec()
1463 void finalize_exec(struct linux_binprm *bprm) in finalize_exec() argument
1467 current->signal->rlim[RLIMIT_STACK] = bprm->rlim_stack; in finalize_exec()
1478 static int prepare_bprm_creds(struct linux_binprm *bprm) in prepare_bprm_creds() argument
1483 bprm->cred = prepare_exec_creds(); in prepare_bprm_creds()
1484 if (likely(bprm->cred)) in prepare_bprm_creds()
1491 static void free_bprm(struct linux_binprm *bprm) in free_bprm() argument
1493 if (bprm->mm) { in free_bprm()
1494 acct_arg_size(bprm, 0); in free_bprm()
1495 mmput(bprm->mm); in free_bprm()
1497 free_arg_pages(bprm); in free_bprm()
1498 if (bprm->cred) { in free_bprm()
1500 abort_creds(bprm->cred); in free_bprm()
1502 if (bprm->file) { in free_bprm()
1503 allow_write_access(bprm->file); in free_bprm()
1504 fput(bprm->file); in free_bprm()
1506 if (bprm->executable) in free_bprm()
1507 fput(bprm->executable); in free_bprm()
1509 if (bprm->interp != bprm->filename) in free_bprm()
1510 kfree(bprm->interp); in free_bprm()
1511 kfree(bprm->fdpath); in free_bprm()
1512 kfree(bprm); in free_bprm()
1517 struct linux_binprm *bprm = kzalloc(sizeof(*bprm), GFP_KERNEL); in alloc_bprm() local
1519 if (!bprm) in alloc_bprm()
1523 bprm->filename = filename->name; in alloc_bprm()
1526 bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d", fd); in alloc_bprm()
1528 bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d/%s", in alloc_bprm()
1530 if (!bprm->fdpath) in alloc_bprm()
1533 bprm->filename = bprm->fdpath; in alloc_bprm()
1535 bprm->interp = bprm->filename; in alloc_bprm()
1537 retval = bprm_mm_init(bprm); in alloc_bprm()
1540 return bprm; in alloc_bprm()
1543 free_bprm(bprm); in alloc_bprm()
1548 int bprm_change_interp(const char *interp, struct linux_binprm *bprm) in bprm_change_interp() argument
1551 if (bprm->interp != bprm->filename) in bprm_change_interp()
1552 kfree(bprm->interp); in bprm_change_interp()
1553 bprm->interp = kstrdup(interp, GFP_KERNEL); in bprm_change_interp()
1554 if (!bprm->interp) in bprm_change_interp()
1565 static void check_unsafe_exec(struct linux_binprm *bprm) in check_unsafe_exec() argument
1571 bprm->unsafe |= LSM_UNSAFE_PTRACE; in check_unsafe_exec()
1578 bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS; in check_unsafe_exec()
1597 bprm->unsafe |= LSM_UNSAFE_SHARE; in check_unsafe_exec()
1603 static void bprm_fill_uid(struct linux_binprm *bprm, struct file *file) in bprm_fill_uid() argument
1634 if (!vfsuid_has_mapping(bprm->cred->user_ns, vfsuid) || in bprm_fill_uid()
1635 !vfsgid_has_mapping(bprm->cred->user_ns, vfsgid)) in bprm_fill_uid()
1639 bprm->per_clear |= PER_CLEAR_ON_SETID; in bprm_fill_uid()
1640 bprm->cred->euid = vfsuid_into_kuid(vfsuid); in bprm_fill_uid()
1644 bprm->per_clear |= PER_CLEAR_ON_SETID; in bprm_fill_uid()
1645 bprm->cred->egid = vfsgid_into_kgid(vfsgid); in bprm_fill_uid()
1652 static int bprm_creds_from_file(struct linux_binprm *bprm) in bprm_creds_from_file() argument
1655 struct file *file = bprm->execfd_creds ? bprm->executable : bprm->file; in bprm_creds_from_file()
1657 bprm_fill_uid(bprm, file); in bprm_creds_from_file()
1658 return security_bprm_creds_from_file(bprm, file); in bprm_creds_from_file()
1667 static int prepare_binprm(struct linux_binprm *bprm) in prepare_binprm() argument
1671 memset(bprm->buf, 0, BINPRM_BUF_SIZE); in prepare_binprm()
1672 return kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE, &pos); in prepare_binprm()
1680 int remove_arg_zero(struct linux_binprm *bprm) in remove_arg_zero() argument
1687 if (!bprm->argc) in remove_arg_zero()
1691 offset = bprm->p & ~PAGE_MASK; in remove_arg_zero()
1692 page = get_arg_page(bprm, bprm->p, 0); in remove_arg_zero()
1700 offset++, bprm->p++) in remove_arg_zero()
1707 bprm->p++; in remove_arg_zero()
1708 bprm->argc--; in remove_arg_zero()
1720 static int search_binary_handler(struct linux_binprm *bprm) in search_binary_handler() argument
1726 retval = prepare_binprm(bprm); in search_binary_handler()
1730 retval = security_bprm_check(bprm); in search_binary_handler()
1742 retval = fmt->load_binary(bprm); in search_binary_handler()
1746 if (bprm->point_of_no_return || (retval != -ENOEXEC)) { in search_binary_handler()
1754 if (printable(bprm->buf[0]) && printable(bprm->buf[1]) && in search_binary_handler()
1755 printable(bprm->buf[2]) && printable(bprm->buf[3])) in search_binary_handler()
1757 if (request_module("binfmt-%04x", *(ushort *)(bprm->buf + 2)) < 0) in search_binary_handler()
1767 static int exec_binprm(struct linux_binprm *bprm) in exec_binprm() argument
1784 ret = search_binary_handler(bprm); in exec_binprm()
1787 if (!bprm->interpreter) in exec_binprm()
1790 exec = bprm->file; in exec_binprm()
1791 bprm->file = bprm->interpreter; in exec_binprm()
1792 bprm->interpreter = NULL; in exec_binprm()
1795 if (unlikely(bprm->have_execfd)) { in exec_binprm()
1796 if (bprm->executable) { in exec_binprm()
1800 bprm->executable = exec; in exec_binprm()
1805 audit_bprm(bprm); in exec_binprm()
1806 trace_sched_process_exec(current, old_pid, bprm); in exec_binprm()
1815 static int bprm_execve(struct linux_binprm *bprm, in bprm_execve() argument
1821 retval = prepare_bprm_creds(bprm); in bprm_execve()
1830 check_unsafe_exec(bprm); in bprm_execve()
1841 bprm->file = file; in bprm_execve()
1851 if (bprm->fdpath && get_close_on_exec(fd)) in bprm_execve()
1852 bprm->interp_flags |= BINPRM_FLAGS_PATH_INACCESSIBLE; in bprm_execve()
1855 retval = security_bprm_creds_for_exec(bprm); in bprm_execve()
1859 retval = exec_binprm(bprm); in bprm_execve()
1880 if (bprm->point_of_no_return && !fatal_signal_pending(current)) in bprm_execve()
1896 struct linux_binprm *bprm; in do_execveat_common() local
1918 bprm = alloc_bprm(fd, filename); in do_execveat_common()
1919 if (IS_ERR(bprm)) { in do_execveat_common()
1920 retval = PTR_ERR(bprm); in do_execveat_common()
1927 current->comm, bprm->filename); in do_execveat_common()
1930 bprm->argc = retval; in do_execveat_common()
1935 bprm->envc = retval; in do_execveat_common()
1937 retval = bprm_stack_limits(bprm); in do_execveat_common()
1941 retval = copy_string_kernel(bprm->filename, bprm); in do_execveat_common()
1944 bprm->exec = bprm->p; in do_execveat_common()
1946 retval = copy_strings(bprm->envc, envp, bprm); in do_execveat_common()
1950 retval = copy_strings(bprm->argc, argv, bprm); in do_execveat_common()
1960 if (bprm->argc == 0) { in do_execveat_common()
1961 retval = copy_string_kernel("", bprm); in do_execveat_common()
1964 bprm->argc = 1; in do_execveat_common()
1967 retval = bprm_execve(bprm, fd, filename, flags); in do_execveat_common()
1969 free_bprm(bprm); in do_execveat_common()
1980 struct linux_binprm *bprm; in kernel_execve() local
1992 bprm = alloc_bprm(fd, filename); in kernel_execve()
1993 if (IS_ERR(bprm)) { in kernel_execve()
1994 retval = PTR_ERR(bprm); in kernel_execve()
2003 bprm->argc = retval; in kernel_execve()
2008 bprm->envc = retval; in kernel_execve()
2010 retval = bprm_stack_limits(bprm); in kernel_execve()
2014 retval = copy_string_kernel(bprm->filename, bprm); in kernel_execve()
2017 bprm->exec = bprm->p; in kernel_execve()
2019 retval = copy_strings_kernel(bprm->envc, envp, bprm); in kernel_execve()
2023 retval = copy_strings_kernel(bprm->argc, argv, bprm); in kernel_execve()
2027 retval = bprm_execve(bprm, fd, filename, 0); in kernel_execve()
2029 free_bprm(bprm); in kernel_execve()