aboutsummaryrefslogtreecommitdiffstats
path: root/fs/exec.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/exec.c')
-rw-r--r--fs/exec.c155
1 files changed, 101 insertions, 54 deletions
diff --git a/fs/exec.c b/fs/exec.c
index ba112bd4a339..49cdaa19e5b9 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -46,7 +46,6 @@
46#include <linux/proc_fs.h> 46#include <linux/proc_fs.h>
47#include <linux/mount.h> 47#include <linux/mount.h>
48#include <linux/security.h> 48#include <linux/security.h>
49#include <linux/ima.h>
50#include <linux/syscalls.h> 49#include <linux/syscalls.h>
51#include <linux/tsacct_kern.h> 50#include <linux/tsacct_kern.h>
52#include <linux/cn_proc.h> 51#include <linux/cn_proc.h>
@@ -196,7 +195,7 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
196 * to work from. 195 * to work from.
197 */ 196 */
198 rlim = current->signal->rlim; 197 rlim = current->signal->rlim;
199 if (size > rlim[RLIMIT_STACK].rlim_cur / 4) { 198 if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4) {
200 put_page(page); 199 put_page(page);
201 return NULL; 200 return NULL;
202 } 201 }
@@ -247,6 +246,7 @@ static int __bprm_mm_init(struct linux_binprm *bprm)
247 vma->vm_start = vma->vm_end - PAGE_SIZE; 246 vma->vm_start = vma->vm_end - PAGE_SIZE;
248 vma->vm_flags = VM_STACK_FLAGS; 247 vma->vm_flags = VM_STACK_FLAGS;
249 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 248 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
249 INIT_LIST_HEAD(&vma->anon_vma_chain);
250 err = insert_vm_struct(mm, vma); 250 err = insert_vm_struct(mm, vma);
251 if (err) 251 if (err)
252 goto err; 252 goto err;
@@ -517,7 +517,8 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
517 /* 517 /*
518 * cover the whole range: [new_start, old_end) 518 * cover the whole range: [new_start, old_end)
519 */ 519 */
520 vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL); 520 if (vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL))
521 return -ENOMEM;
521 522
522 /* 523 /*
523 * move the page tables downwards, on failure we rely on 524 * move the page tables downwards, on failure we rely on
@@ -548,15 +549,13 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
548 tlb_finish_mmu(tlb, new_end, old_end); 549 tlb_finish_mmu(tlb, new_end, old_end);
549 550
550 /* 551 /*
551 * shrink the vma to just the new range. 552 * Shrink the vma to just the new range. Always succeeds.
552 */ 553 */
553 vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL); 554 vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
554 555
555 return 0; 556 return 0;
556} 557}
557 558
558#define EXTRA_STACK_VM_PAGES 20 /* random */
559
560/* 559/*
561 * Finalizes the stack vm_area_struct. The flags and permissions are updated, 560 * Finalizes the stack vm_area_struct. The flags and permissions are updated,
562 * the stack is optionally relocated, and some extra space is added. 561 * the stack is optionally relocated, and some extra space is added.
@@ -572,10 +571,13 @@ int setup_arg_pages(struct linux_binprm *bprm,
572 struct vm_area_struct *prev = NULL; 571 struct vm_area_struct *prev = NULL;
573 unsigned long vm_flags; 572 unsigned long vm_flags;
574 unsigned long stack_base; 573 unsigned long stack_base;
574 unsigned long stack_size;
575 unsigned long stack_expand;
576 unsigned long rlim_stack;
575 577
576#ifdef CONFIG_STACK_GROWSUP 578#ifdef CONFIG_STACK_GROWSUP
577 /* Limit stack size to 1GB */ 579 /* Limit stack size to 1GB */
578 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max; 580 stack_base = rlimit_max(RLIMIT_STACK);
579 if (stack_base > (1 << 30)) 581 if (stack_base > (1 << 30))
580 stack_base = 1 << 30; 582 stack_base = 1 << 30;
581 583
@@ -628,10 +630,23 @@ int setup_arg_pages(struct linux_binprm *bprm,
628 goto out_unlock; 630 goto out_unlock;
629 } 631 }
630 632
633 stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
634 stack_size = vma->vm_end - vma->vm_start;
635 /*
636 * Align this down to a page boundary as expand_stack
637 * will align it up.
638 */
639 rlim_stack = rlimit(RLIMIT_STACK) & PAGE_MASK;
631#ifdef CONFIG_STACK_GROWSUP 640#ifdef CONFIG_STACK_GROWSUP
632 stack_base = vma->vm_end + EXTRA_STACK_VM_PAGES * PAGE_SIZE; 641 if (stack_size + stack_expand > rlim_stack)
642 stack_base = vma->vm_start + rlim_stack;
643 else
644 stack_base = vma->vm_end + stack_expand;
633#else 645#else
634 stack_base = vma->vm_start - EXTRA_STACK_VM_PAGES * PAGE_SIZE; 646 if (stack_size + stack_expand > rlim_stack)
647 stack_base = vma->vm_end - rlim_stack;
648 else
649 stack_base = vma->vm_start - stack_expand;
635#endif 650#endif
636 ret = expand_stack(vma, stack_base); 651 ret = expand_stack(vma, stack_base);
637 if (ret) 652 if (ret)
@@ -703,6 +718,7 @@ static int exec_mmap(struct mm_struct *mm)
703 /* Notify parent that we're no longer interested in the old VM */ 718 /* Notify parent that we're no longer interested in the old VM */
704 tsk = current; 719 tsk = current;
705 old_mm = current->mm; 720 old_mm = current->mm;
721 sync_mm_rss(tsk, old_mm);
706 mm_release(tsk, old_mm); 722 mm_release(tsk, old_mm);
707 723
708 if (old_mm) { 724 if (old_mm) {
@@ -827,7 +843,9 @@ static int de_thread(struct task_struct *tsk)
827 attach_pid(tsk, PIDTYPE_PID, task_pid(leader)); 843 attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
828 transfer_pid(leader, tsk, PIDTYPE_PGID); 844 transfer_pid(leader, tsk, PIDTYPE_PGID);
829 transfer_pid(leader, tsk, PIDTYPE_SID); 845 transfer_pid(leader, tsk, PIDTYPE_SID);
846
830 list_replace_rcu(&leader->tasks, &tsk->tasks); 847 list_replace_rcu(&leader->tasks, &tsk->tasks);
848 list_replace_init(&leader->sibling, &tsk->sibling);
831 849
832 tsk->group_leader = tsk; 850 tsk->group_leader = tsk;
833 leader->group_leader = tsk; 851 leader->group_leader = tsk;
@@ -924,6 +942,15 @@ char *get_task_comm(char *buf, struct task_struct *tsk)
924void set_task_comm(struct task_struct *tsk, char *buf) 942void set_task_comm(struct task_struct *tsk, char *buf)
925{ 943{
926 task_lock(tsk); 944 task_lock(tsk);
945
946 /*
947 * Threads may access current->comm without holding
948 * the task lock, so write the string carefully.
949 * Readers without a lock may see incomplete new
950 * names but are safe from non-terminating string reads.
951 */
952 memset(tsk->comm, 0, TASK_COMM_LEN);
953 wmb();
927 strlcpy(tsk->comm, buf, sizeof(tsk->comm)); 954 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
928 task_unlock(tsk); 955 task_unlock(tsk);
929 perf_event_comm(tsk); 956 perf_event_comm(tsk);
@@ -931,9 +958,7 @@ void set_task_comm(struct task_struct *tsk, char *buf)
931 958
932int flush_old_exec(struct linux_binprm * bprm) 959int flush_old_exec(struct linux_binprm * bprm)
933{ 960{
934 char * name; 961 int retval;
935 int i, ch, retval;
936 char tcomm[sizeof(current->comm)];
937 962
938 /* 963 /*
939 * Make sure we have a private signal table and that 964 * Make sure we have a private signal table and that
@@ -954,6 +979,25 @@ int flush_old_exec(struct linux_binprm * bprm)
954 979
955 bprm->mm = NULL; /* We're using it now */ 980 bprm->mm = NULL; /* We're using it now */
956 981
982 current->flags &= ~PF_RANDOMIZE;
983 flush_thread();
984 current->personality &= ~bprm->per_clear;
985
986 return 0;
987
988out:
989 return retval;
990}
991EXPORT_SYMBOL(flush_old_exec);
992
993void setup_new_exec(struct linux_binprm * bprm)
994{
995 int i, ch;
996 char * name;
997 char tcomm[sizeof(current->comm)];
998
999 arch_pick_mmap_layout(current->mm);
1000
957 /* This is the point of no return */ 1001 /* This is the point of no return */
958 current->sas_ss_sp = current->sas_ss_size = 0; 1002 current->sas_ss_sp = current->sas_ss_size = 0;
959 1003
@@ -975,9 +1019,6 @@ int flush_old_exec(struct linux_binprm * bprm)
975 tcomm[i] = '\0'; 1019 tcomm[i] = '\0';
976 set_task_comm(current, tcomm); 1020 set_task_comm(current, tcomm);
977 1021
978 current->flags &= ~PF_RANDOMIZE;
979 flush_thread();
980
981 /* Set the new mm task size. We have to do that late because it may 1022 /* Set the new mm task size. We have to do that late because it may
982 * depend on TIF_32BIT which is only updated in flush_thread() on 1023 * depend on TIF_32BIT which is only updated in flush_thread() on
983 * some architectures like powerpc 1024 * some architectures like powerpc
@@ -993,8 +1034,6 @@ int flush_old_exec(struct linux_binprm * bprm)
993 set_dumpable(current->mm, suid_dumpable); 1034 set_dumpable(current->mm, suid_dumpable);
994 } 1035 }
995 1036
996 current->personality &= ~bprm->per_clear;
997
998 /* 1037 /*
999 * Flush performance counters when crossing a 1038 * Flush performance counters when crossing a
1000 * security domain: 1039 * security domain:
@@ -1009,14 +1048,8 @@ int flush_old_exec(struct linux_binprm * bprm)
1009 1048
1010 flush_signal_handlers(current, 0); 1049 flush_signal_handlers(current, 0);
1011 flush_old_files(current->files); 1050 flush_old_files(current->files);
1012
1013 return 0;
1014
1015out:
1016 return retval;
1017} 1051}
1018 1052EXPORT_SYMBOL(setup_new_exec);
1019EXPORT_SYMBOL(flush_old_exec);
1020 1053
1021/* 1054/*
1022 * Prepare credentials and lock ->cred_guard_mutex. 1055 * Prepare credentials and lock ->cred_guard_mutex.
@@ -1209,9 +1242,6 @@ int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1209 retval = security_bprm_check(bprm); 1242 retval = security_bprm_check(bprm);
1210 if (retval) 1243 if (retval)
1211 return retval; 1244 return retval;
1212 retval = ima_bprm_check(bprm);
1213 if (retval)
1214 return retval;
1215 1245
1216 /* kernel module loader fixup */ 1246 /* kernel module loader fixup */
1217 /* so we don't try to load run modprobe in kernel space. */ 1247 /* so we don't try to load run modprobe in kernel space. */
@@ -1503,7 +1533,7 @@ static int format_corename(char *corename, long signr)
1503 /* core limit size */ 1533 /* core limit size */
1504 case 'c': 1534 case 'c':
1505 rc = snprintf(out_ptr, out_end - out_ptr, 1535 rc = snprintf(out_ptr, out_end - out_ptr,
1506 "%lu", current->signal->rlim[RLIMIT_CORE].rlim_cur); 1536 "%lu", rlimit(RLIMIT_CORE));
1507 if (rc > out_end - out_ptr) 1537 if (rc > out_end - out_ptr)
1508 goto out; 1538 goto out;
1509 out_ptr += rc; 1539 out_ptr += rc;
@@ -1531,12 +1561,13 @@ out:
1531 return ispipe; 1561 return ispipe;
1532} 1562}
1533 1563
1534static int zap_process(struct task_struct *start) 1564static int zap_process(struct task_struct *start, int exit_code)
1535{ 1565{
1536 struct task_struct *t; 1566 struct task_struct *t;
1537 int nr = 0; 1567 int nr = 0;
1538 1568
1539 start->signal->flags = SIGNAL_GROUP_EXIT; 1569 start->signal->flags = SIGNAL_GROUP_EXIT;
1570 start->signal->group_exit_code = exit_code;
1540 start->signal->group_stop_count = 0; 1571 start->signal->group_stop_count = 0;
1541 1572
1542 t = start; 1573 t = start;
@@ -1561,8 +1592,7 @@ static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
1561 spin_lock_irq(&tsk->sighand->siglock); 1592 spin_lock_irq(&tsk->sighand->siglock);
1562 if (!signal_group_exit(tsk->signal)) { 1593 if (!signal_group_exit(tsk->signal)) {
1563 mm->core_state = core_state; 1594 mm->core_state = core_state;
1564 tsk->signal->group_exit_code = exit_code; 1595 nr = zap_process(tsk, exit_code);
1565 nr = zap_process(tsk);
1566 } 1596 }
1567 spin_unlock_irq(&tsk->sighand->siglock); 1597 spin_unlock_irq(&tsk->sighand->siglock);
1568 if (unlikely(nr < 0)) 1598 if (unlikely(nr < 0))
@@ -1611,7 +1641,7 @@ static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
1611 if (p->mm) { 1641 if (p->mm) {
1612 if (unlikely(p->mm == mm)) { 1642 if (unlikely(p->mm == mm)) {
1613 lock_task_sighand(p, &flags); 1643 lock_task_sighand(p, &flags);
1614 nr += zap_process(p); 1644 nr += zap_process(p, exit_code);
1615 unlock_task_sighand(p, &flags); 1645 unlock_task_sighand(p, &flags);
1616 } 1646 }
1617 break; 1647 break;
@@ -1718,14 +1748,19 @@ void set_dumpable(struct mm_struct *mm, int value)
1718 } 1748 }
1719} 1749}
1720 1750
1721int get_dumpable(struct mm_struct *mm) 1751static int __get_dumpable(unsigned long mm_flags)
1722{ 1752{
1723 int ret; 1753 int ret;
1724 1754
1725 ret = mm->flags & 0x3; 1755 ret = mm_flags & MMF_DUMPABLE_MASK;
1726 return (ret >= 2) ? 2 : ret; 1756 return (ret >= 2) ? 2 : ret;
1727} 1757}
1728 1758
1759int get_dumpable(struct mm_struct *mm)
1760{
1761 return __get_dumpable(mm->flags);
1762}
1763
1729static void wait_for_dump_helpers(struct file *file) 1764static void wait_for_dump_helpers(struct file *file)
1730{ 1765{
1731 struct pipe_inode_info *pipe; 1766 struct pipe_inode_info *pipe;
@@ -1756,17 +1791,26 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
1756 struct mm_struct *mm = current->mm; 1791 struct mm_struct *mm = current->mm;
1757 struct linux_binfmt * binfmt; 1792 struct linux_binfmt * binfmt;
1758 struct inode * inode; 1793 struct inode * inode;
1759 struct file * file;
1760 const struct cred *old_cred; 1794 const struct cred *old_cred;
1761 struct cred *cred; 1795 struct cred *cred;
1762 int retval = 0; 1796 int retval = 0;
1763 int flag = 0; 1797 int flag = 0;
1764 int ispipe = 0; 1798 int ispipe = 0;
1765 unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1766 char **helper_argv = NULL; 1799 char **helper_argv = NULL;
1767 int helper_argc = 0; 1800 int helper_argc = 0;
1768 int dump_count = 0; 1801 int dump_count = 0;
1769 static atomic_t core_dump_count = ATOMIC_INIT(0); 1802 static atomic_t core_dump_count = ATOMIC_INIT(0);
1803 struct coredump_params cprm = {
1804 .signr = signr,
1805 .regs = regs,
1806 .limit = rlimit(RLIMIT_CORE),
1807 /*
1808 * We must use the same mm->flags while dumping core to avoid
1809 * inconsistency of bit flags, since this flag is not protected
1810 * by any locks.
1811 */
1812 .mm_flags = mm->flags,
1813 };
1770 1814
1771 audit_core_dumps(signr); 1815 audit_core_dumps(signr);
1772 1816
@@ -1784,7 +1828,7 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
1784 /* 1828 /*
1785 * If another thread got here first, or we are not dumpable, bail out. 1829 * If another thread got here first, or we are not dumpable, bail out.
1786 */ 1830 */
1787 if (mm->core_state || !get_dumpable(mm)) { 1831 if (mm->core_state || !__get_dumpable(cprm.mm_flags)) {
1788 up_write(&mm->mmap_sem); 1832 up_write(&mm->mmap_sem);
1789 put_cred(cred); 1833 put_cred(cred);
1790 goto fail; 1834 goto fail;
@@ -1795,7 +1839,8 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
1795 * process nor do we know its entire history. We only know it 1839 * process nor do we know its entire history. We only know it
1796 * was tainted so we dump it as root in mode 2. 1840 * was tainted so we dump it as root in mode 2.
1797 */ 1841 */
1798 if (get_dumpable(mm) == 2) { /* Setuid core dump mode */ 1842 if (__get_dumpable(cprm.mm_flags) == 2) {
1843 /* Setuid core dump mode */
1799 flag = O_EXCL; /* Stop rewrite attacks */ 1844 flag = O_EXCL; /* Stop rewrite attacks */
1800 cred->fsuid = 0; /* Dump root private */ 1845 cred->fsuid = 0; /* Dump root private */
1801 } 1846 }
@@ -1822,15 +1867,15 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
1822 ispipe = format_corename(corename, signr); 1867 ispipe = format_corename(corename, signr);
1823 unlock_kernel(); 1868 unlock_kernel();
1824 1869
1825 if ((!ispipe) && (core_limit < binfmt->min_coredump)) 1870 if ((!ispipe) && (cprm.limit < binfmt->min_coredump))
1826 goto fail_unlock; 1871 goto fail_unlock;
1827 1872
1828 if (ispipe) { 1873 if (ispipe) {
1829 if (core_limit == 0) { 1874 if (cprm.limit == 0) {
1830 /* 1875 /*
1831 * Normally core limits are irrelevant to pipes, since 1876 * Normally core limits are irrelevant to pipes, since
1832 * we're not writing to the file system, but we use 1877 * we're not writing to the file system, but we use
1833 * core_limit of 0 here as a speacial value. Any 1878 * cprm.limit of 0 here as a speacial value. Any
1834 * non-zero limit gets set to RLIM_INFINITY below, but 1879 * non-zero limit gets set to RLIM_INFINITY below, but
1835 * a limit of 0 skips the dump. This is a consistent 1880 * a limit of 0 skips the dump. This is a consistent
1836 * way to catch recursive crashes. We can still crash 1881 * way to catch recursive crashes. We can still crash
@@ -1863,25 +1908,25 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
1863 goto fail_dropcount; 1908 goto fail_dropcount;
1864 } 1909 }
1865 1910
1866 core_limit = RLIM_INFINITY; 1911 cprm.limit = RLIM_INFINITY;
1867 1912
1868 /* SIGPIPE can happen, but it's just never processed */ 1913 /* SIGPIPE can happen, but it's just never processed */
1869 if (call_usermodehelper_pipe(helper_argv[0], helper_argv, NULL, 1914 if (call_usermodehelper_pipe(helper_argv[0], helper_argv, NULL,
1870 &file)) { 1915 &cprm.file)) {
1871 printk(KERN_INFO "Core dump to %s pipe failed\n", 1916 printk(KERN_INFO "Core dump to %s pipe failed\n",
1872 corename); 1917 corename);
1873 goto fail_dropcount; 1918 goto fail_dropcount;
1874 } 1919 }
1875 } else 1920 } else
1876 file = filp_open(corename, 1921 cprm.file = filp_open(corename,
1877 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 1922 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
1878 0600); 1923 0600);
1879 if (IS_ERR(file)) 1924 if (IS_ERR(cprm.file))
1880 goto fail_dropcount; 1925 goto fail_dropcount;
1881 inode = file->f_path.dentry->d_inode; 1926 inode = cprm.file->f_path.dentry->d_inode;
1882 if (inode->i_nlink > 1) 1927 if (inode->i_nlink > 1)
1883 goto close_fail; /* multiple links - don't dump */ 1928 goto close_fail; /* multiple links - don't dump */
1884 if (!ispipe && d_unhashed(file->f_path.dentry)) 1929 if (!ispipe && d_unhashed(cprm.file->f_path.dentry))
1885 goto close_fail; 1930 goto close_fail;
1886 1931
1887 /* AK: actually i see no reason to not allow this for named pipes etc., 1932 /* AK: actually i see no reason to not allow this for named pipes etc.,
@@ -1891,24 +1936,26 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
1891 /* 1936 /*
1892 * Dont allow local users get cute and trick others to coredump 1937 * Dont allow local users get cute and trick others to coredump
1893 * into their pre-created files: 1938 * into their pre-created files:
1939 * Note, this is not relevant for pipes
1894 */ 1940 */
1895 if (inode->i_uid != current_fsuid()) 1941 if (!ispipe && (inode->i_uid != current_fsuid()))
1896 goto close_fail; 1942 goto close_fail;
1897 if (!file->f_op) 1943 if (!cprm.file->f_op)
1898 goto close_fail; 1944 goto close_fail;
1899 if (!file->f_op->write) 1945 if (!cprm.file->f_op->write)
1900 goto close_fail; 1946 goto close_fail;
1901 if (!ispipe && do_truncate(file->f_path.dentry, 0, 0, file) != 0) 1947 if (!ispipe &&
1948 do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file) != 0)
1902 goto close_fail; 1949 goto close_fail;
1903 1950
1904 retval = binfmt->core_dump(signr, regs, file, core_limit); 1951 retval = binfmt->core_dump(&cprm);
1905 1952
1906 if (retval) 1953 if (retval)
1907 current->signal->group_exit_code |= 0x80; 1954 current->signal->group_exit_code |= 0x80;
1908close_fail: 1955close_fail:
1909 if (ispipe && core_pipe_limit) 1956 if (ispipe && core_pipe_limit)
1910 wait_for_dump_helpers(file); 1957 wait_for_dump_helpers(cprm.file);
1911 filp_close(file, NULL); 1958 filp_close(cprm.file, NULL);
1912fail_dropcount: 1959fail_dropcount:
1913 if (dump_count) 1960 if (dump_count)
1914 atomic_dec(&core_dump_count); 1961 atomic_dec(&core_dump_count);