25 #include <linux/slab.h>
29 #include <linux/stat.h>
30 #include <linux/fcntl.h>
32 #include <linux/string.h>
35 #include <linux/perf_event.h>
39 #include <linux/personality.h>
40 #include <linux/binfmts.h>
41 #include <linux/utsname.h>
43 #include <linux/module.h>
49 #include <linux/cn_proc.h>
50 #include <linux/audit.h>
56 #include <linux/oom.h>
59 #include <asm/uaccess.h>
60 #include <asm/mmu_context.h>
109 int error = PTR_ERR(tmp);
110 static const struct open_flags uselib_flags = {
121 error = PTR_ERR(file);
143 if (!try_module_get(fmt->
module))
170 long diff = (
long)(pages - bprm->vma_pages);
175 bprm->vma_pages =
pages;
185 #ifdef CONFIG_STACK_GROWSUP
193 1, write, 1, &page,
NULL);
198 unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
227 static void put_arg_page(
struct page *page)
240 static void flush_arg_page(
struct linux_binprm *bprm,
unsigned long pos,
265 BUILD_BUG_ON(VM_STACK_FLAGS & VM_STACK_INCOMPLETE_SETUP);
268 vma->
vm_flags = VM_STACK_FLAGS | VM_STACK_INCOMPLETE_SETUP;
278 bprm->
p = vma->
vm_end -
sizeof(
void *);
287 static bool valid_arg_len(
struct linux_binprm *bprm,
long len)
294 static inline void acct_arg_size(
struct linux_binprm *bprm,
unsigned long pages)
298 static struct page *get_arg_page(
struct linux_binprm *bprm,
unsigned long pos,
304 if (!page && write) {
314 static void put_arg_page(
struct page *page)
331 free_arg_page(bprm, i);
334 static void flush_arg_page(
struct linux_binprm *bprm,
unsigned long pos,
341 bprm->
p =
PAGE_SIZE * MAX_ARG_PAGES -
sizeof(
void *);
345 static bool valid_arg_len(
struct linux_binprm *bprm,
long len)
347 return len <= bprm->
p;
372 err = __bprm_mm_init(bprm);
401 const char __user *native;
410 return compat_ptr(compat);
429 const char __user *
p = get_user_arg_ptr(argv, i);
440 if (fatal_signal_pending(
current))
456 struct page *kmapped_page =
NULL;
458 unsigned long kpos = 0;
467 str = get_user_arg_ptr(argv, argc);
476 if (!valid_arg_len(bprm, len))
485 int offset, bytes_to_copy;
487 if (fatal_signal_pending(
current)) {
498 if (bytes_to_copy > len)
501 offset -= bytes_to_copy;
502 pos -= bytes_to_copy;
503 str -= bytes_to_copy;
504 len -= bytes_to_copy;
506 if (!kmapped_page || kpos != (pos &
PAGE_MASK)) {
509 page = get_arg_page(bprm, pos, 1);
516 flush_kernel_dcache_page(kmapped_page);
518 put_arg_page(kmapped_page);
521 kaddr =
kmap(kmapped_page);
523 flush_arg_page(bprm, kpos, kmapped_page);
534 flush_kernel_dcache_page(kmapped_page);
536 put_arg_page(kmapped_page);
554 r = copy_strings(argc, argv, bprm);
575 static int shift_arg_pages(
struct vm_area_struct *vma,
unsigned long shift)
578 unsigned long old_start = vma->
vm_start;
579 unsigned long old_end = vma->
vm_end;
580 unsigned long length = old_end - old_start;
581 unsigned long new_start = old_start - shift;
582 unsigned long new_end = old_end - shift;
585 BUG_ON(new_start > new_end);
605 vma, new_start, length,
false))
610 if (new_end > old_start) {
642 int executable_stack)
645 unsigned long stack_shift;
650 unsigned long stack_base;
651 unsigned long stack_size;
652 unsigned long stack_expand;
653 unsigned long rlim_stack;
655 #ifdef CONFIG_STACK_GROWSUP
658 if (stack_base > (1 << 30))
659 stack_base = 1 << 30;
665 stack_base =
PAGE_ALIGN(stack_top - stack_base);
667 stack_shift = vma->
vm_start - stack_base;
669 bprm->
p = vma->
vm_end - stack_shift;
680 bprm->
p -= stack_shift;
685 bprm->
loader -= stack_shift;
686 bprm->
exec -= stack_shift;
689 vm_flags = VM_STACK_FLAGS;
699 vm_flags &= ~VM_EXEC;
701 vm_flags |= VM_STACK_INCOMPLETE_SETUP;
711 ret = shift_arg_pages(vma, stack_shift);
717 vma->
vm_flags &= ~VM_STACK_INCOMPLETE_SETUP;
719 stack_expand = 131072
UL;
726 #ifdef CONFIG_STACK_GROWSUP
727 if (stack_size + stack_expand > rlim_stack)
728 stack_base = vma->
vm_start + rlim_stack;
730 stack_base = vma->
vm_end + stack_expand;
732 if (stack_size + stack_expand > rlim_stack)
733 stack_base = vma->
vm_end - rlim_stack;
735 stack_base = vma->
vm_start - stack_expand;
755 static const struct open_flags open_exec_flags = {
774 err = deny_write_access(file);
804 static int exec_mmap(
struct mm_struct *mm)
837 BUG_ON(active_mm != old_mm);
838 setmax_mm_hiwater_rss(&tsk->
signal->maxrss, old_mm);
839 mm_update_next_owner(old_mm);
859 if (thread_group_empty(tsk))
860 goto no_thread_group;
866 if (signal_group_exit(sig)) {
871 spin_unlock_irq(lock);
877 if (!thread_group_leader(tsk))
882 spin_unlock_irq(lock);
884 if (
unlikely(__fatal_signal_pending(tsk)))
888 spin_unlock_irq(lock);
895 if (!thread_group_leader(tsk)) {
906 if (
unlikely(__fatal_signal_pending(tsk)))
922 BUG_ON(!same_thread_group(leader, tsk));
923 BUG_ON(has_group_leader_pid(tsk));
942 list_replace_rcu(&leader->
tasks, &tsk->
tasks);
988 sizeof(newsighand->
action));
991 spin_lock(&oldsighand->
siglock);
993 spin_unlock(&oldsighand->
siglock);
999 BUG_ON(!thread_group_leader(tsk));
1030 trace_task_rename(tsk, buf);
1045 static void filename_to_taskname(
char *tcomm,
const char *
fn,
unsigned int len)
1050 for (i = 0; (ch = *(fn++)) !=
'\0';) {
1078 acct_arg_size(bprm, 0);
1079 retval = exec_mmap(bprm->
mm);
1173 free_arg_pages(bprm);
1205 static int check_unsafe_exec(
struct linux_binprm *bprm)
1226 spin_lock(&p->
fs->lock);
1228 for (t = next_thread(p); t !=
p; t = next_thread(t)) {
1234 if (p->
fs->users > n_fs) {
1238 if (!p->
fs->in_exec) {
1243 spin_unlock(&p->
fs->lock);
1272 if (!kuid_has_mapping(bprm->
cred->user_ns, inode->
i_uid))
1286 if (!kgid_has_mapping(bprm->
cred->user_ns, inode->
i_gid))
1321 offset = bprm->
p & ~PAGE_MASK;
1322 page = get_arg_page(bprm, bprm->
p, 0);
1330 offset++, bprm->
p++)
1357 pid_t old_pid, old_vpid;
1363 retval = audit_bprm(bprm);
1374 for (
try=0;
try<2;
try++) {
1380 if (!try_module_get(fmt->
module))
1383 retval =
fn(bprm, regs);
1392 trace_sched_process_exec(
current, old_pid, bprm);
1396 allow_write_access(bprm->
file);
1414 #ifdef CONFIG_MODULES
1418 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1419 if (printable(bprm->
buf[0]) &&
1420 printable(bprm->
buf[1]) &&
1421 printable(bprm->
buf[2]) &&
1422 printable(bprm->
buf[3]))
1426 request_module(
"binfmt-%04x", *(
unsigned short *)(&bprm->
buf[2]));
1440 static int do_execve_common(
const char *
filename,
1481 retval = check_unsafe_exec(bprm);
1488 retval = PTR_ERR(file);
1503 if ((retval = bprm->
argc) < 0)
1507 if ((retval = bprm->
envc) < 0)
1518 bprm->
exec = bprm->
p;
1519 retval = copy_strings(bprm->
envc, envp, bprm);
1523 retval = copy_strings(bprm->
argc, argv, bprm);
1534 acct_update_integrals(
current);
1542 acct_arg_size(bprm, 0);
1548 allow_write_access(bprm->
file);
1568 const char __user *
const __user *__argv,
1569 const char __user *
const __user *__envp,
1574 return do_execve_common(filename, argv, envp, regs);
1577 #ifdef CONFIG_COMPAT
1578 int compat_do_execve(
const char *filename,
1585 .
ptr.compat = __argv,
1589 .
ptr.compat = __envp,
1591 return do_execve_common(filename, argv, envp, regs);
1600 module_put(mm->
binfmt->module);
1604 __module_get(new->module);
1663 #ifdef __ARCH_WANT_SYS_EXECVE
1665 const char __user *, filename,
1666 const char __user *
const __user *, argv,
1667 const char __user *
const __user *, envp)
1670 int error = PTR_ERR(path);
1671 if (!IS_ERR(path)) {
1677 #ifdef CONFIG_COMPAT
1682 struct filename *path =
getname(filename);
1683 int error = PTR_ERR(path);
1684 if (!IS_ERR(path)) {
1685 error = compat_do_execve(path->
name, argv, envp,
1694 #ifdef __ARCH_WANT_KERNEL_EXECVE
1696 const char *
const argv[],
1697 const char *
const envp[])
1703 (
const char __user *
const __user *)argv,
1704 (
const char __user *
const __user *)envp, p);