diff options
Diffstat (limited to 'fs/exec.c')
-rw-r--r-- | fs/exec.c | 688 |
1 files changed, 7 insertions, 681 deletions
@@ -66,19 +66,8 @@ | |||
66 | 66 | ||
67 | #include <trace/events/sched.h> | 67 | #include <trace/events/sched.h> |
68 | 68 | ||
69 | int core_uses_pid; | ||
70 | char core_pattern[CORENAME_MAX_SIZE] = "core"; | ||
71 | unsigned int core_pipe_limit; | ||
72 | int suid_dumpable = 0; | 69 | int suid_dumpable = 0; |
73 | 70 | ||
74 | struct core_name { | ||
75 | char *corename; | ||
76 | int used, size; | ||
77 | }; | ||
78 | static atomic_t call_count = ATOMIC_INIT(1); | ||
79 | |||
80 | /* The maximal length of core_pattern is also specified in sysctl.c */ | ||
81 | |||
82 | static LIST_HEAD(formats); | 71 | static LIST_HEAD(formats); |
83 | static DEFINE_RWLOCK(binfmt_lock); | 72 | static DEFINE_RWLOCK(binfmt_lock); |
84 | 73 | ||
@@ -1006,40 +995,6 @@ no_thread_group: | |||
1006 | return 0; | 995 | return 0; |
1007 | } | 996 | } |
1008 | 997 | ||
1009 | /* | ||
1010 | * These functions flushes out all traces of the currently running executable | ||
1011 | * so that a new one can be started | ||
1012 | */ | ||
1013 | static void flush_old_files(struct files_struct * files) | ||
1014 | { | ||
1015 | long j = -1; | ||
1016 | struct fdtable *fdt; | ||
1017 | |||
1018 | spin_lock(&files->file_lock); | ||
1019 | for (;;) { | ||
1020 | unsigned long set, i; | ||
1021 | |||
1022 | j++; | ||
1023 | i = j * BITS_PER_LONG; | ||
1024 | fdt = files_fdtable(files); | ||
1025 | if (i >= fdt->max_fds) | ||
1026 | break; | ||
1027 | set = fdt->close_on_exec[j]; | ||
1028 | if (!set) | ||
1029 | continue; | ||
1030 | fdt->close_on_exec[j] = 0; | ||
1031 | spin_unlock(&files->file_lock); | ||
1032 | for ( ; set ; i++,set >>= 1) { | ||
1033 | if (set & 1) { | ||
1034 | sys_close(i); | ||
1035 | } | ||
1036 | } | ||
1037 | spin_lock(&files->file_lock); | ||
1038 | |||
1039 | } | ||
1040 | spin_unlock(&files->file_lock); | ||
1041 | } | ||
1042 | |||
1043 | char *get_task_comm(char *buf, struct task_struct *tsk) | 998 | char *get_task_comm(char *buf, struct task_struct *tsk) |
1044 | { | 999 | { |
1045 | /* buf must be at least sizeof(tsk->comm) in size */ | 1000 | /* buf must be at least sizeof(tsk->comm) in size */ |
@@ -1050,6 +1005,11 @@ char *get_task_comm(char *buf, struct task_struct *tsk) | |||
1050 | } | 1005 | } |
1051 | EXPORT_SYMBOL_GPL(get_task_comm); | 1006 | EXPORT_SYMBOL_GPL(get_task_comm); |
1052 | 1007 | ||
1008 | /* | ||
1009 | * These functions flushes out all traces of the currently running executable | ||
1010 | * so that a new one can be started | ||
1011 | */ | ||
1012 | |||
1053 | void set_task_comm(struct task_struct *tsk, char *buf) | 1013 | void set_task_comm(struct task_struct *tsk, char *buf) |
1054 | { | 1014 | { |
1055 | task_lock(tsk); | 1015 | task_lock(tsk); |
@@ -1171,7 +1131,7 @@ void setup_new_exec(struct linux_binprm * bprm) | |||
1171 | current->self_exec_id++; | 1131 | current->self_exec_id++; |
1172 | 1132 | ||
1173 | flush_signal_handlers(current, 0); | 1133 | flush_signal_handlers(current, 0); |
1174 | flush_old_files(current->files); | 1134 | do_close_on_exec(current->files); |
1175 | } | 1135 | } |
1176 | EXPORT_SYMBOL(setup_new_exec); | 1136 | EXPORT_SYMBOL(setup_new_exec); |
1177 | 1137 | ||
@@ -1632,353 +1592,6 @@ void set_binfmt(struct linux_binfmt *new) | |||
1632 | 1592 | ||
1633 | EXPORT_SYMBOL(set_binfmt); | 1593 | EXPORT_SYMBOL(set_binfmt); |
1634 | 1594 | ||
1635 | static int expand_corename(struct core_name *cn) | ||
1636 | { | ||
1637 | char *old_corename = cn->corename; | ||
1638 | |||
1639 | cn->size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count); | ||
1640 | cn->corename = krealloc(old_corename, cn->size, GFP_KERNEL); | ||
1641 | |||
1642 | if (!cn->corename) { | ||
1643 | kfree(old_corename); | ||
1644 | return -ENOMEM; | ||
1645 | } | ||
1646 | |||
1647 | return 0; | ||
1648 | } | ||
1649 | |||
1650 | static int cn_printf(struct core_name *cn, const char *fmt, ...) | ||
1651 | { | ||
1652 | char *cur; | ||
1653 | int need; | ||
1654 | int ret; | ||
1655 | va_list arg; | ||
1656 | |||
1657 | va_start(arg, fmt); | ||
1658 | need = vsnprintf(NULL, 0, fmt, arg); | ||
1659 | va_end(arg); | ||
1660 | |||
1661 | if (likely(need < cn->size - cn->used - 1)) | ||
1662 | goto out_printf; | ||
1663 | |||
1664 | ret = expand_corename(cn); | ||
1665 | if (ret) | ||
1666 | goto expand_fail; | ||
1667 | |||
1668 | out_printf: | ||
1669 | cur = cn->corename + cn->used; | ||
1670 | va_start(arg, fmt); | ||
1671 | vsnprintf(cur, need + 1, fmt, arg); | ||
1672 | va_end(arg); | ||
1673 | cn->used += need; | ||
1674 | return 0; | ||
1675 | |||
1676 | expand_fail: | ||
1677 | return ret; | ||
1678 | } | ||
1679 | |||
1680 | static void cn_escape(char *str) | ||
1681 | { | ||
1682 | for (; *str; str++) | ||
1683 | if (*str == '/') | ||
1684 | *str = '!'; | ||
1685 | } | ||
1686 | |||
1687 | static int cn_print_exe_file(struct core_name *cn) | ||
1688 | { | ||
1689 | struct file *exe_file; | ||
1690 | char *pathbuf, *path; | ||
1691 | int ret; | ||
1692 | |||
1693 | exe_file = get_mm_exe_file(current->mm); | ||
1694 | if (!exe_file) { | ||
1695 | char *commstart = cn->corename + cn->used; | ||
1696 | ret = cn_printf(cn, "%s (path unknown)", current->comm); | ||
1697 | cn_escape(commstart); | ||
1698 | return ret; | ||
1699 | } | ||
1700 | |||
1701 | pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY); | ||
1702 | if (!pathbuf) { | ||
1703 | ret = -ENOMEM; | ||
1704 | goto put_exe_file; | ||
1705 | } | ||
1706 | |||
1707 | path = d_path(&exe_file->f_path, pathbuf, PATH_MAX); | ||
1708 | if (IS_ERR(path)) { | ||
1709 | ret = PTR_ERR(path); | ||
1710 | goto free_buf; | ||
1711 | } | ||
1712 | |||
1713 | cn_escape(path); | ||
1714 | |||
1715 | ret = cn_printf(cn, "%s", path); | ||
1716 | |||
1717 | free_buf: | ||
1718 | kfree(pathbuf); | ||
1719 | put_exe_file: | ||
1720 | fput(exe_file); | ||
1721 | return ret; | ||
1722 | } | ||
1723 | |||
1724 | /* format_corename will inspect the pattern parameter, and output a | ||
1725 | * name into corename, which must have space for at least | ||
1726 | * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator. | ||
1727 | */ | ||
1728 | static int format_corename(struct core_name *cn, long signr) | ||
1729 | { | ||
1730 | const struct cred *cred = current_cred(); | ||
1731 | const char *pat_ptr = core_pattern; | ||
1732 | int ispipe = (*pat_ptr == '|'); | ||
1733 | int pid_in_pattern = 0; | ||
1734 | int err = 0; | ||
1735 | |||
1736 | cn->size = CORENAME_MAX_SIZE * atomic_read(&call_count); | ||
1737 | cn->corename = kmalloc(cn->size, GFP_KERNEL); | ||
1738 | cn->used = 0; | ||
1739 | |||
1740 | if (!cn->corename) | ||
1741 | return -ENOMEM; | ||
1742 | |||
1743 | /* Repeat as long as we have more pattern to process and more output | ||
1744 | space */ | ||
1745 | while (*pat_ptr) { | ||
1746 | if (*pat_ptr != '%') { | ||
1747 | if (*pat_ptr == 0) | ||
1748 | goto out; | ||
1749 | err = cn_printf(cn, "%c", *pat_ptr++); | ||
1750 | } else { | ||
1751 | switch (*++pat_ptr) { | ||
1752 | /* single % at the end, drop that */ | ||
1753 | case 0: | ||
1754 | goto out; | ||
1755 | /* Double percent, output one percent */ | ||
1756 | case '%': | ||
1757 | err = cn_printf(cn, "%c", '%'); | ||
1758 | break; | ||
1759 | /* pid */ | ||
1760 | case 'p': | ||
1761 | pid_in_pattern = 1; | ||
1762 | err = cn_printf(cn, "%d", | ||
1763 | task_tgid_vnr(current)); | ||
1764 | break; | ||
1765 | /* uid */ | ||
1766 | case 'u': | ||
1767 | err = cn_printf(cn, "%d", cred->uid); | ||
1768 | break; | ||
1769 | /* gid */ | ||
1770 | case 'g': | ||
1771 | err = cn_printf(cn, "%d", cred->gid); | ||
1772 | break; | ||
1773 | /* signal that caused the coredump */ | ||
1774 | case 's': | ||
1775 | err = cn_printf(cn, "%ld", signr); | ||
1776 | break; | ||
1777 | /* UNIX time of coredump */ | ||
1778 | case 't': { | ||
1779 | struct timeval tv; | ||
1780 | do_gettimeofday(&tv); | ||
1781 | err = cn_printf(cn, "%lu", tv.tv_sec); | ||
1782 | break; | ||
1783 | } | ||
1784 | /* hostname */ | ||
1785 | case 'h': { | ||
1786 | char *namestart = cn->corename + cn->used; | ||
1787 | down_read(&uts_sem); | ||
1788 | err = cn_printf(cn, "%s", | ||
1789 | utsname()->nodename); | ||
1790 | up_read(&uts_sem); | ||
1791 | cn_escape(namestart); | ||
1792 | break; | ||
1793 | } | ||
1794 | /* executable */ | ||
1795 | case 'e': { | ||
1796 | char *commstart = cn->corename + cn->used; | ||
1797 | err = cn_printf(cn, "%s", current->comm); | ||
1798 | cn_escape(commstart); | ||
1799 | break; | ||
1800 | } | ||
1801 | case 'E': | ||
1802 | err = cn_print_exe_file(cn); | ||
1803 | break; | ||
1804 | /* core limit size */ | ||
1805 | case 'c': | ||
1806 | err = cn_printf(cn, "%lu", | ||
1807 | rlimit(RLIMIT_CORE)); | ||
1808 | break; | ||
1809 | default: | ||
1810 | break; | ||
1811 | } | ||
1812 | ++pat_ptr; | ||
1813 | } | ||
1814 | |||
1815 | if (err) | ||
1816 | return err; | ||
1817 | } | ||
1818 | |||
1819 | /* Backward compatibility with core_uses_pid: | ||
1820 | * | ||
1821 | * If core_pattern does not include a %p (as is the default) | ||
1822 | * and core_uses_pid is set, then .%pid will be appended to | ||
1823 | * the filename. Do not do this for piped commands. */ | ||
1824 | if (!ispipe && !pid_in_pattern && core_uses_pid) { | ||
1825 | err = cn_printf(cn, ".%d", task_tgid_vnr(current)); | ||
1826 | if (err) | ||
1827 | return err; | ||
1828 | } | ||
1829 | out: | ||
1830 | return ispipe; | ||
1831 | } | ||
1832 | |||
1833 | static int zap_process(struct task_struct *start, int exit_code) | ||
1834 | { | ||
1835 | struct task_struct *t; | ||
1836 | int nr = 0; | ||
1837 | |||
1838 | start->signal->flags = SIGNAL_GROUP_EXIT; | ||
1839 | start->signal->group_exit_code = exit_code; | ||
1840 | start->signal->group_stop_count = 0; | ||
1841 | |||
1842 | t = start; | ||
1843 | do { | ||
1844 | task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK); | ||
1845 | if (t != current && t->mm) { | ||
1846 | sigaddset(&t->pending.signal, SIGKILL); | ||
1847 | signal_wake_up(t, 1); | ||
1848 | nr++; | ||
1849 | } | ||
1850 | } while_each_thread(start, t); | ||
1851 | |||
1852 | return nr; | ||
1853 | } | ||
1854 | |||
1855 | static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm, | ||
1856 | struct core_state *core_state, int exit_code) | ||
1857 | { | ||
1858 | struct task_struct *g, *p; | ||
1859 | unsigned long flags; | ||
1860 | int nr = -EAGAIN; | ||
1861 | |||
1862 | spin_lock_irq(&tsk->sighand->siglock); | ||
1863 | if (!signal_group_exit(tsk->signal)) { | ||
1864 | mm->core_state = core_state; | ||
1865 | nr = zap_process(tsk, exit_code); | ||
1866 | } | ||
1867 | spin_unlock_irq(&tsk->sighand->siglock); | ||
1868 | if (unlikely(nr < 0)) | ||
1869 | return nr; | ||
1870 | |||
1871 | if (atomic_read(&mm->mm_users) == nr + 1) | ||
1872 | goto done; | ||
1873 | /* | ||
1874 | * We should find and kill all tasks which use this mm, and we should | ||
1875 | * count them correctly into ->nr_threads. We don't take tasklist | ||
1876 | * lock, but this is safe wrt: | ||
1877 | * | ||
1878 | * fork: | ||
1879 | * None of sub-threads can fork after zap_process(leader). All | ||
1880 | * processes which were created before this point should be | ||
1881 | * visible to zap_threads() because copy_process() adds the new | ||
1882 | * process to the tail of init_task.tasks list, and lock/unlock | ||
1883 | * of ->siglock provides a memory barrier. | ||
1884 | * | ||
1885 | * do_exit: | ||
1886 | * The caller holds mm->mmap_sem. This means that the task which | ||
1887 | * uses this mm can't pass exit_mm(), so it can't exit or clear | ||
1888 | * its ->mm. | ||
1889 | * | ||
1890 | * de_thread: | ||
1891 | * It does list_replace_rcu(&leader->tasks, ¤t->tasks), | ||
1892 | * we must see either old or new leader, this does not matter. | ||
1893 | * However, it can change p->sighand, so lock_task_sighand(p) | ||
1894 | * must be used. Since p->mm != NULL and we hold ->mmap_sem | ||
1895 | * it can't fail. | ||
1896 | * | ||
1897 | * Note also that "g" can be the old leader with ->mm == NULL | ||
1898 | * and already unhashed and thus removed from ->thread_group. | ||
1899 | * This is OK, __unhash_process()->list_del_rcu() does not | ||
1900 | * clear the ->next pointer, we will find the new leader via | ||
1901 | * next_thread(). | ||
1902 | */ | ||
1903 | rcu_read_lock(); | ||
1904 | for_each_process(g) { | ||
1905 | if (g == tsk->group_leader) | ||
1906 | continue; | ||
1907 | if (g->flags & PF_KTHREAD) | ||
1908 | continue; | ||
1909 | p = g; | ||
1910 | do { | ||
1911 | if (p->mm) { | ||
1912 | if (unlikely(p->mm == mm)) { | ||
1913 | lock_task_sighand(p, &flags); | ||
1914 | nr += zap_process(p, exit_code); | ||
1915 | unlock_task_sighand(p, &flags); | ||
1916 | } | ||
1917 | break; | ||
1918 | } | ||
1919 | } while_each_thread(g, p); | ||
1920 | } | ||
1921 | rcu_read_unlock(); | ||
1922 | done: | ||
1923 | atomic_set(&core_state->nr_threads, nr); | ||
1924 | return nr; | ||
1925 | } | ||
1926 | |||
1927 | static int coredump_wait(int exit_code, struct core_state *core_state) | ||
1928 | { | ||
1929 | struct task_struct *tsk = current; | ||
1930 | struct mm_struct *mm = tsk->mm; | ||
1931 | int core_waiters = -EBUSY; | ||
1932 | |||
1933 | init_completion(&core_state->startup); | ||
1934 | core_state->dumper.task = tsk; | ||
1935 | core_state->dumper.next = NULL; | ||
1936 | |||
1937 | down_write(&mm->mmap_sem); | ||
1938 | if (!mm->core_state) | ||
1939 | core_waiters = zap_threads(tsk, mm, core_state, exit_code); | ||
1940 | up_write(&mm->mmap_sem); | ||
1941 | |||
1942 | if (core_waiters > 0) { | ||
1943 | struct core_thread *ptr; | ||
1944 | |||
1945 | wait_for_completion(&core_state->startup); | ||
1946 | /* | ||
1947 | * Wait for all the threads to become inactive, so that | ||
1948 | * all the thread context (extended register state, like | ||
1949 | * fpu etc) gets copied to the memory. | ||
1950 | */ | ||
1951 | ptr = core_state->dumper.next; | ||
1952 | while (ptr != NULL) { | ||
1953 | wait_task_inactive(ptr->task, 0); | ||
1954 | ptr = ptr->next; | ||
1955 | } | ||
1956 | } | ||
1957 | |||
1958 | return core_waiters; | ||
1959 | } | ||
1960 | |||
1961 | static void coredump_finish(struct mm_struct *mm) | ||
1962 | { | ||
1963 | struct core_thread *curr, *next; | ||
1964 | struct task_struct *task; | ||
1965 | |||
1966 | next = mm->core_state->dumper.next; | ||
1967 | while ((curr = next) != NULL) { | ||
1968 | next = curr->next; | ||
1969 | task = curr->task; | ||
1970 | /* | ||
1971 | * see exit_mm(), curr->task must not see | ||
1972 | * ->task == NULL before we read ->next. | ||
1973 | */ | ||
1974 | smp_mb(); | ||
1975 | curr->task = NULL; | ||
1976 | wake_up_process(task); | ||
1977 | } | ||
1978 | |||
1979 | mm->core_state = NULL; | ||
1980 | } | ||
1981 | |||
1982 | /* | 1595 | /* |
1983 | * set_dumpable converts traditional three-value dumpable to two flags and | 1596 | * set_dumpable converts traditional three-value dumpable to two flags and |
1984 | * stores them into mm->flags. It modifies lower two bits of mm->flags, but | 1597 | * stores them into mm->flags. It modifies lower two bits of mm->flags, but |
@@ -2020,7 +1633,7 @@ void set_dumpable(struct mm_struct *mm, int value) | |||
2020 | } | 1633 | } |
2021 | } | 1634 | } |
2022 | 1635 | ||
2023 | static int __get_dumpable(unsigned long mm_flags) | 1636 | int __get_dumpable(unsigned long mm_flags) |
2024 | { | 1637 | { |
2025 | int ret; | 1638 | int ret; |
2026 | 1639 | ||
@@ -2032,290 +1645,3 @@ int get_dumpable(struct mm_struct *mm) | |||
2032 | { | 1645 | { |
2033 | return __get_dumpable(mm->flags); | 1646 | return __get_dumpable(mm->flags); |
2034 | } | 1647 | } |
2035 | |||
2036 | static void wait_for_dump_helpers(struct file *file) | ||
2037 | { | ||
2038 | struct pipe_inode_info *pipe; | ||
2039 | |||
2040 | pipe = file->f_path.dentry->d_inode->i_pipe; | ||
2041 | |||
2042 | pipe_lock(pipe); | ||
2043 | pipe->readers++; | ||
2044 | pipe->writers--; | ||
2045 | |||
2046 | while ((pipe->readers > 1) && (!signal_pending(current))) { | ||
2047 | wake_up_interruptible_sync(&pipe->wait); | ||
2048 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); | ||
2049 | pipe_wait(pipe); | ||
2050 | } | ||
2051 | |||
2052 | pipe->readers--; | ||
2053 | pipe->writers++; | ||
2054 | pipe_unlock(pipe); | ||
2055 | |||
2056 | } | ||
2057 | |||
2058 | |||
2059 | /* | ||
2060 | * umh_pipe_setup | ||
2061 | * helper function to customize the process used | ||
2062 | * to collect the core in userspace. Specifically | ||
2063 | * it sets up a pipe and installs it as fd 0 (stdin) | ||
2064 | * for the process. Returns 0 on success, or | ||
2065 | * PTR_ERR on failure. | ||
2066 | * Note that it also sets the core limit to 1. This | ||
2067 | * is a special value that we use to trap recursive | ||
2068 | * core dumps | ||
2069 | */ | ||
2070 | static int umh_pipe_setup(struct subprocess_info *info, struct cred *new) | ||
2071 | { | ||
2072 | struct file *files[2]; | ||
2073 | struct fdtable *fdt; | ||
2074 | struct coredump_params *cp = (struct coredump_params *)info->data; | ||
2075 | struct files_struct *cf = current->files; | ||
2076 | int err = create_pipe_files(files, 0); | ||
2077 | if (err) | ||
2078 | return err; | ||
2079 | |||
2080 | cp->file = files[1]; | ||
2081 | |||
2082 | sys_close(0); | ||
2083 | fd_install(0, files[0]); | ||
2084 | spin_lock(&cf->file_lock); | ||
2085 | fdt = files_fdtable(cf); | ||
2086 | __set_open_fd(0, fdt); | ||
2087 | __clear_close_on_exec(0, fdt); | ||
2088 | spin_unlock(&cf->file_lock); | ||
2089 | |||
2090 | /* and disallow core files too */ | ||
2091 | current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1}; | ||
2092 | |||
2093 | return 0; | ||
2094 | } | ||
2095 | |||
2096 | void do_coredump(long signr, int exit_code, struct pt_regs *regs) | ||
2097 | { | ||
2098 | struct core_state core_state; | ||
2099 | struct core_name cn; | ||
2100 | struct mm_struct *mm = current->mm; | ||
2101 | struct linux_binfmt * binfmt; | ||
2102 | const struct cred *old_cred; | ||
2103 | struct cred *cred; | ||
2104 | int retval = 0; | ||
2105 | int flag = 0; | ||
2106 | int ispipe; | ||
2107 | bool need_nonrelative = false; | ||
2108 | static atomic_t core_dump_count = ATOMIC_INIT(0); | ||
2109 | struct coredump_params cprm = { | ||
2110 | .signr = signr, | ||
2111 | .regs = regs, | ||
2112 | .limit = rlimit(RLIMIT_CORE), | ||
2113 | /* | ||
2114 | * We must use the same mm->flags while dumping core to avoid | ||
2115 | * inconsistency of bit flags, since this flag is not protected | ||
2116 | * by any locks. | ||
2117 | */ | ||
2118 | .mm_flags = mm->flags, | ||
2119 | }; | ||
2120 | |||
2121 | audit_core_dumps(signr); | ||
2122 | |||
2123 | binfmt = mm->binfmt; | ||
2124 | if (!binfmt || !binfmt->core_dump) | ||
2125 | goto fail; | ||
2126 | if (!__get_dumpable(cprm.mm_flags)) | ||
2127 | goto fail; | ||
2128 | |||
2129 | cred = prepare_creds(); | ||
2130 | if (!cred) | ||
2131 | goto fail; | ||
2132 | /* | ||
2133 | * We cannot trust fsuid as being the "true" uid of the process | ||
2134 | * nor do we know its entire history. We only know it was tainted | ||
2135 | * so we dump it as root in mode 2, and only into a controlled | ||
2136 | * environment (pipe handler or fully qualified path). | ||
2137 | */ | ||
2138 | if (__get_dumpable(cprm.mm_flags) == SUID_DUMPABLE_SAFE) { | ||
2139 | /* Setuid core dump mode */ | ||
2140 | flag = O_EXCL; /* Stop rewrite attacks */ | ||
2141 | cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */ | ||
2142 | need_nonrelative = true; | ||
2143 | } | ||
2144 | |||
2145 | retval = coredump_wait(exit_code, &core_state); | ||
2146 | if (retval < 0) | ||
2147 | goto fail_creds; | ||
2148 | |||
2149 | old_cred = override_creds(cred); | ||
2150 | |||
2151 | /* | ||
2152 | * Clear any false indication of pending signals that might | ||
2153 | * be seen by the filesystem code called to write the core file. | ||
2154 | */ | ||
2155 | clear_thread_flag(TIF_SIGPENDING); | ||
2156 | |||
2157 | ispipe = format_corename(&cn, signr); | ||
2158 | |||
2159 | if (ispipe) { | ||
2160 | int dump_count; | ||
2161 | char **helper_argv; | ||
2162 | |||
2163 | if (ispipe < 0) { | ||
2164 | printk(KERN_WARNING "format_corename failed\n"); | ||
2165 | printk(KERN_WARNING "Aborting core\n"); | ||
2166 | goto fail_corename; | ||
2167 | } | ||
2168 | |||
2169 | if (cprm.limit == 1) { | ||
2170 | /* See umh_pipe_setup() which sets RLIMIT_CORE = 1. | ||
2171 | * | ||
2172 | * Normally core limits are irrelevant to pipes, since | ||
2173 | * we're not writing to the file system, but we use | ||
2174 | * cprm.limit of 1 here as a speacial value, this is a | ||
2175 | * consistent way to catch recursive crashes. | ||
2176 | * We can still crash if the core_pattern binary sets | ||
2177 | * RLIM_CORE = !1, but it runs as root, and can do | ||
2178 | * lots of stupid things. | ||
2179 | * | ||
2180 | * Note that we use task_tgid_vnr here to grab the pid | ||
2181 | * of the process group leader. That way we get the | ||
2182 | * right pid if a thread in a multi-threaded | ||
2183 | * core_pattern process dies. | ||
2184 | */ | ||
2185 | printk(KERN_WARNING | ||
2186 | "Process %d(%s) has RLIMIT_CORE set to 1\n", | ||
2187 | task_tgid_vnr(current), current->comm); | ||
2188 | printk(KERN_WARNING "Aborting core\n"); | ||
2189 | goto fail_unlock; | ||
2190 | } | ||
2191 | cprm.limit = RLIM_INFINITY; | ||
2192 | |||
2193 | dump_count = atomic_inc_return(&core_dump_count); | ||
2194 | if (core_pipe_limit && (core_pipe_limit < dump_count)) { | ||
2195 | printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n", | ||
2196 | task_tgid_vnr(current), current->comm); | ||
2197 | printk(KERN_WARNING "Skipping core dump\n"); | ||
2198 | goto fail_dropcount; | ||
2199 | } | ||
2200 | |||
2201 | helper_argv = argv_split(GFP_KERNEL, cn.corename+1, NULL); | ||
2202 | if (!helper_argv) { | ||
2203 | printk(KERN_WARNING "%s failed to allocate memory\n", | ||
2204 | __func__); | ||
2205 | goto fail_dropcount; | ||
2206 | } | ||
2207 | |||
2208 | retval = call_usermodehelper_fns(helper_argv[0], helper_argv, | ||
2209 | NULL, UMH_WAIT_EXEC, umh_pipe_setup, | ||
2210 | NULL, &cprm); | ||
2211 | argv_free(helper_argv); | ||
2212 | if (retval) { | ||
2213 | printk(KERN_INFO "Core dump to %s pipe failed\n", | ||
2214 | cn.corename); | ||
2215 | goto close_fail; | ||
2216 | } | ||
2217 | } else { | ||
2218 | struct inode *inode; | ||
2219 | |||
2220 | if (cprm.limit < binfmt->min_coredump) | ||
2221 | goto fail_unlock; | ||
2222 | |||
2223 | if (need_nonrelative && cn.corename[0] != '/') { | ||
2224 | printk(KERN_WARNING "Pid %d(%s) can only dump core "\ | ||
2225 | "to fully qualified path!\n", | ||
2226 | task_tgid_vnr(current), current->comm); | ||
2227 | printk(KERN_WARNING "Skipping core dump\n"); | ||
2228 | goto fail_unlock; | ||
2229 | } | ||
2230 | |||
2231 | cprm.file = filp_open(cn.corename, | ||
2232 | O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, | ||
2233 | 0600); | ||
2234 | if (IS_ERR(cprm.file)) | ||
2235 | goto fail_unlock; | ||
2236 | |||
2237 | inode = cprm.file->f_path.dentry->d_inode; | ||
2238 | if (inode->i_nlink > 1) | ||
2239 | goto close_fail; | ||
2240 | if (d_unhashed(cprm.file->f_path.dentry)) | ||
2241 | goto close_fail; | ||
2242 | /* | ||
2243 | * AK: actually i see no reason to not allow this for named | ||
2244 | * pipes etc, but keep the previous behaviour for now. | ||
2245 | */ | ||
2246 | if (!S_ISREG(inode->i_mode)) | ||
2247 | goto close_fail; | ||
2248 | /* | ||
2249 | * Dont allow local users get cute and trick others to coredump | ||
2250 | * into their pre-created files. | ||
2251 | */ | ||
2252 | if (!uid_eq(inode->i_uid, current_fsuid())) | ||
2253 | goto close_fail; | ||
2254 | if (!cprm.file->f_op || !cprm.file->f_op->write) | ||
2255 | goto close_fail; | ||
2256 | if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file)) | ||
2257 | goto close_fail; | ||
2258 | } | ||
2259 | |||
2260 | retval = binfmt->core_dump(&cprm); | ||
2261 | if (retval) | ||
2262 | current->signal->group_exit_code |= 0x80; | ||
2263 | |||
2264 | if (ispipe && core_pipe_limit) | ||
2265 | wait_for_dump_helpers(cprm.file); | ||
2266 | close_fail: | ||
2267 | if (cprm.file) | ||
2268 | filp_close(cprm.file, NULL); | ||
2269 | fail_dropcount: | ||
2270 | if (ispipe) | ||
2271 | atomic_dec(&core_dump_count); | ||
2272 | fail_unlock: | ||
2273 | kfree(cn.corename); | ||
2274 | fail_corename: | ||
2275 | coredump_finish(mm); | ||
2276 | revert_creds(old_cred); | ||
2277 | fail_creds: | ||
2278 | put_cred(cred); | ||
2279 | fail: | ||
2280 | return; | ||
2281 | } | ||
2282 | |||
2283 | /* | ||
2284 | * Core dumping helper functions. These are the only things you should | ||
2285 | * do on a core-file: use only these functions to write out all the | ||
2286 | * necessary info. | ||
2287 | */ | ||
2288 | int dump_write(struct file *file, const void *addr, int nr) | ||
2289 | { | ||
2290 | return access_ok(VERIFY_READ, addr, nr) && file->f_op->write(file, addr, nr, &file->f_pos) == nr; | ||
2291 | } | ||
2292 | EXPORT_SYMBOL(dump_write); | ||
2293 | |||
2294 | int dump_seek(struct file *file, loff_t off) | ||
2295 | { | ||
2296 | int ret = 1; | ||
2297 | |||
2298 | if (file->f_op->llseek && file->f_op->llseek != no_llseek) { | ||
2299 | if (file->f_op->llseek(file, off, SEEK_CUR) < 0) | ||
2300 | return 0; | ||
2301 | } else { | ||
2302 | char *buf = (char *)get_zeroed_page(GFP_KERNEL); | ||
2303 | |||
2304 | if (!buf) | ||
2305 | return 0; | ||
2306 | while (off > 0) { | ||
2307 | unsigned long n = off; | ||
2308 | |||
2309 | if (n > PAGE_SIZE) | ||
2310 | n = PAGE_SIZE; | ||
2311 | if (!dump_write(file, buf, n)) { | ||
2312 | ret = 0; | ||
2313 | break; | ||
2314 | } | ||
2315 | off -= n; | ||
2316 | } | ||
2317 | free_page((unsigned long)buf); | ||
2318 | } | ||
2319 | return ret; | ||
2320 | } | ||
2321 | EXPORT_SYMBOL(dump_seek); | ||