aboutsummaryrefslogtreecommitdiffstats
path: root/fs/exec.c
diff options
context:
space:
mode:
authorAlex Kelly <alex.page.kelly@gmail.com>2012-09-26 21:52:08 -0400
committerAl Viro <viro@zeniv.linux.org.uk>2012-10-02 21:35:55 -0400
commit10c28d937e2cca577c2d804106b50dd0562fb062 (patch)
tree249f1c487bf8a9cc32912e20bf9f274c650f58e9 /fs/exec.c
parentf34f9d186df35e5c39163444c43b4fc6255e39c5 (diff)
coredump: move core dump functionality into its own file
This prepares for making core dump functionality optional. The variable "suid_dumpable" and associated functions are left in fs/exec.c because they're used elsewhere, such as in ptrace. Signed-off-by: Alex Kelly <alex.page.kelly@gmail.com> Reviewed-by: Josh Triplett <josh@joshtriplett.org> Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Acked-by: Kees Cook <keescook@chromium.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Diffstat (limited to 'fs/exec.c')
-rw-r--r--fs/exec.c645
1 files changed, 1 insertions, 644 deletions
diff --git a/fs/exec.c b/fs/exec.c
index beb05a95e4a3..48fb26ef8a1b 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -66,19 +66,8 @@
66 66
67#include <trace/events/sched.h> 67#include <trace/events/sched.h>
68 68
69int core_uses_pid;
70char core_pattern[CORENAME_MAX_SIZE] = "core";
71unsigned int core_pipe_limit;
72int suid_dumpable = 0; 69int suid_dumpable = 0;
73 70
74struct core_name {
75 char *corename;
76 int used, size;
77};
78static atomic_t call_count = ATOMIC_INIT(1);
79
80/* The maximal length of core_pattern is also specified in sysctl.c */
81
82static LIST_HEAD(formats); 71static LIST_HEAD(formats);
83static DEFINE_RWLOCK(binfmt_lock); 72static DEFINE_RWLOCK(binfmt_lock);
84 73
@@ -1603,353 +1592,6 @@ void set_binfmt(struct linux_binfmt *new)
1603 1592
1604EXPORT_SYMBOL(set_binfmt); 1593EXPORT_SYMBOL(set_binfmt);
1605 1594
1606static int expand_corename(struct core_name *cn)
1607{
1608 char *old_corename = cn->corename;
1609
1610 cn->size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count);
1611 cn->corename = krealloc(old_corename, cn->size, GFP_KERNEL);
1612
1613 if (!cn->corename) {
1614 kfree(old_corename);
1615 return -ENOMEM;
1616 }
1617
1618 return 0;
1619}
1620
1621static int cn_printf(struct core_name *cn, const char *fmt, ...)
1622{
1623 char *cur;
1624 int need;
1625 int ret;
1626 va_list arg;
1627
1628 va_start(arg, fmt);
1629 need = vsnprintf(NULL, 0, fmt, arg);
1630 va_end(arg);
1631
1632 if (likely(need < cn->size - cn->used - 1))
1633 goto out_printf;
1634
1635 ret = expand_corename(cn);
1636 if (ret)
1637 goto expand_fail;
1638
1639out_printf:
1640 cur = cn->corename + cn->used;
1641 va_start(arg, fmt);
1642 vsnprintf(cur, need + 1, fmt, arg);
1643 va_end(arg);
1644 cn->used += need;
1645 return 0;
1646
1647expand_fail:
1648 return ret;
1649}
1650
1651static void cn_escape(char *str)
1652{
1653 for (; *str; str++)
1654 if (*str == '/')
1655 *str = '!';
1656}
1657
1658static int cn_print_exe_file(struct core_name *cn)
1659{
1660 struct file *exe_file;
1661 char *pathbuf, *path;
1662 int ret;
1663
1664 exe_file = get_mm_exe_file(current->mm);
1665 if (!exe_file) {
1666 char *commstart = cn->corename + cn->used;
1667 ret = cn_printf(cn, "%s (path unknown)", current->comm);
1668 cn_escape(commstart);
1669 return ret;
1670 }
1671
1672 pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
1673 if (!pathbuf) {
1674 ret = -ENOMEM;
1675 goto put_exe_file;
1676 }
1677
1678 path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
1679 if (IS_ERR(path)) {
1680 ret = PTR_ERR(path);
1681 goto free_buf;
1682 }
1683
1684 cn_escape(path);
1685
1686 ret = cn_printf(cn, "%s", path);
1687
1688free_buf:
1689 kfree(pathbuf);
1690put_exe_file:
1691 fput(exe_file);
1692 return ret;
1693}
1694
1695/* format_corename will inspect the pattern parameter, and output a
1696 * name into corename, which must have space for at least
1697 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1698 */
1699static int format_corename(struct core_name *cn, long signr)
1700{
1701 const struct cred *cred = current_cred();
1702 const char *pat_ptr = core_pattern;
1703 int ispipe = (*pat_ptr == '|');
1704 int pid_in_pattern = 0;
1705 int err = 0;
1706
1707 cn->size = CORENAME_MAX_SIZE * atomic_read(&call_count);
1708 cn->corename = kmalloc(cn->size, GFP_KERNEL);
1709 cn->used = 0;
1710
1711 if (!cn->corename)
1712 return -ENOMEM;
1713
1714 /* Repeat as long as we have more pattern to process and more output
1715 space */
1716 while (*pat_ptr) {
1717 if (*pat_ptr != '%') {
1718 if (*pat_ptr == 0)
1719 goto out;
1720 err = cn_printf(cn, "%c", *pat_ptr++);
1721 } else {
1722 switch (*++pat_ptr) {
1723 /* single % at the end, drop that */
1724 case 0:
1725 goto out;
1726 /* Double percent, output one percent */
1727 case '%':
1728 err = cn_printf(cn, "%c", '%');
1729 break;
1730 /* pid */
1731 case 'p':
1732 pid_in_pattern = 1;
1733 err = cn_printf(cn, "%d",
1734 task_tgid_vnr(current));
1735 break;
1736 /* uid */
1737 case 'u':
1738 err = cn_printf(cn, "%d", cred->uid);
1739 break;
1740 /* gid */
1741 case 'g':
1742 err = cn_printf(cn, "%d", cred->gid);
1743 break;
1744 /* signal that caused the coredump */
1745 case 's':
1746 err = cn_printf(cn, "%ld", signr);
1747 break;
1748 /* UNIX time of coredump */
1749 case 't': {
1750 struct timeval tv;
1751 do_gettimeofday(&tv);
1752 err = cn_printf(cn, "%lu", tv.tv_sec);
1753 break;
1754 }
1755 /* hostname */
1756 case 'h': {
1757 char *namestart = cn->corename + cn->used;
1758 down_read(&uts_sem);
1759 err = cn_printf(cn, "%s",
1760 utsname()->nodename);
1761 up_read(&uts_sem);
1762 cn_escape(namestart);
1763 break;
1764 }
1765 /* executable */
1766 case 'e': {
1767 char *commstart = cn->corename + cn->used;
1768 err = cn_printf(cn, "%s", current->comm);
1769 cn_escape(commstart);
1770 break;
1771 }
1772 case 'E':
1773 err = cn_print_exe_file(cn);
1774 break;
1775 /* core limit size */
1776 case 'c':
1777 err = cn_printf(cn, "%lu",
1778 rlimit(RLIMIT_CORE));
1779 break;
1780 default:
1781 break;
1782 }
1783 ++pat_ptr;
1784 }
1785
1786 if (err)
1787 return err;
1788 }
1789
1790 /* Backward compatibility with core_uses_pid:
1791 *
1792 * If core_pattern does not include a %p (as is the default)
1793 * and core_uses_pid is set, then .%pid will be appended to
1794 * the filename. Do not do this for piped commands. */
1795 if (!ispipe && !pid_in_pattern && core_uses_pid) {
1796 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
1797 if (err)
1798 return err;
1799 }
1800out:
1801 return ispipe;
1802}
1803
1804static int zap_process(struct task_struct *start, int exit_code)
1805{
1806 struct task_struct *t;
1807 int nr = 0;
1808
1809 start->signal->flags = SIGNAL_GROUP_EXIT;
1810 start->signal->group_exit_code = exit_code;
1811 start->signal->group_stop_count = 0;
1812
1813 t = start;
1814 do {
1815 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1816 if (t != current && t->mm) {
1817 sigaddset(&t->pending.signal, SIGKILL);
1818 signal_wake_up(t, 1);
1819 nr++;
1820 }
1821 } while_each_thread(start, t);
1822
1823 return nr;
1824}
1825
1826static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
1827 struct core_state *core_state, int exit_code)
1828{
1829 struct task_struct *g, *p;
1830 unsigned long flags;
1831 int nr = -EAGAIN;
1832
1833 spin_lock_irq(&tsk->sighand->siglock);
1834 if (!signal_group_exit(tsk->signal)) {
1835 mm->core_state = core_state;
1836 nr = zap_process(tsk, exit_code);
1837 }
1838 spin_unlock_irq(&tsk->sighand->siglock);
1839 if (unlikely(nr < 0))
1840 return nr;
1841
1842 if (atomic_read(&mm->mm_users) == nr + 1)
1843 goto done;
1844 /*
1845 * We should find and kill all tasks which use this mm, and we should
1846 * count them correctly into ->nr_threads. We don't take tasklist
1847 * lock, but this is safe wrt:
1848 *
1849 * fork:
1850 * None of sub-threads can fork after zap_process(leader). All
1851 * processes which were created before this point should be
1852 * visible to zap_threads() because copy_process() adds the new
1853 * process to the tail of init_task.tasks list, and lock/unlock
1854 * of ->siglock provides a memory barrier.
1855 *
1856 * do_exit:
1857 * The caller holds mm->mmap_sem. This means that the task which
1858 * uses this mm can't pass exit_mm(), so it can't exit or clear
1859 * its ->mm.
1860 *
1861 * de_thread:
1862 * It does list_replace_rcu(&leader->tasks, &current->tasks),
1863 * we must see either old or new leader, this does not matter.
1864 * However, it can change p->sighand, so lock_task_sighand(p)
1865 * must be used. Since p->mm != NULL and we hold ->mmap_sem
1866 * it can't fail.
1867 *
1868 * Note also that "g" can be the old leader with ->mm == NULL
1869 * and already unhashed and thus removed from ->thread_group.
1870 * This is OK, __unhash_process()->list_del_rcu() does not
1871 * clear the ->next pointer, we will find the new leader via
1872 * next_thread().
1873 */
1874 rcu_read_lock();
1875 for_each_process(g) {
1876 if (g == tsk->group_leader)
1877 continue;
1878 if (g->flags & PF_KTHREAD)
1879 continue;
1880 p = g;
1881 do {
1882 if (p->mm) {
1883 if (unlikely(p->mm == mm)) {
1884 lock_task_sighand(p, &flags);
1885 nr += zap_process(p, exit_code);
1886 unlock_task_sighand(p, &flags);
1887 }
1888 break;
1889 }
1890 } while_each_thread(g, p);
1891 }
1892 rcu_read_unlock();
1893done:
1894 atomic_set(&core_state->nr_threads, nr);
1895 return nr;
1896}
1897
1898static int coredump_wait(int exit_code, struct core_state *core_state)
1899{
1900 struct task_struct *tsk = current;
1901 struct mm_struct *mm = tsk->mm;
1902 int core_waiters = -EBUSY;
1903
1904 init_completion(&core_state->startup);
1905 core_state->dumper.task = tsk;
1906 core_state->dumper.next = NULL;
1907
1908 down_write(&mm->mmap_sem);
1909 if (!mm->core_state)
1910 core_waiters = zap_threads(tsk, mm, core_state, exit_code);
1911 up_write(&mm->mmap_sem);
1912
1913 if (core_waiters > 0) {
1914 struct core_thread *ptr;
1915
1916 wait_for_completion(&core_state->startup);
1917 /*
1918 * Wait for all the threads to become inactive, so that
1919 * all the thread context (extended register state, like
1920 * fpu etc) gets copied to the memory.
1921 */
1922 ptr = core_state->dumper.next;
1923 while (ptr != NULL) {
1924 wait_task_inactive(ptr->task, 0);
1925 ptr = ptr->next;
1926 }
1927 }
1928
1929 return core_waiters;
1930}
1931
1932static void coredump_finish(struct mm_struct *mm)
1933{
1934 struct core_thread *curr, *next;
1935 struct task_struct *task;
1936
1937 next = mm->core_state->dumper.next;
1938 while ((curr = next) != NULL) {
1939 next = curr->next;
1940 task = curr->task;
1941 /*
1942 * see exit_mm(), curr->task must not see
1943 * ->task == NULL before we read ->next.
1944 */
1945 smp_mb();
1946 curr->task = NULL;
1947 wake_up_process(task);
1948 }
1949
1950 mm->core_state = NULL;
1951}
1952
1953/* 1595/*
1954 * set_dumpable converts traditional three-value dumpable to two flags and 1596 * set_dumpable converts traditional three-value dumpable to two flags and
1955 * 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
@@ -1991,7 +1633,7 @@ void set_dumpable(struct mm_struct *mm, int value)
1991 } 1633 }
1992} 1634}
1993 1635
1994static int __get_dumpable(unsigned long mm_flags) 1636int __get_dumpable(unsigned long mm_flags)
1995{ 1637{
1996 int ret; 1638 int ret;
1997 1639
@@ -2003,288 +1645,3 @@ int get_dumpable(struct mm_struct *mm)
2003{ 1645{
2004 return __get_dumpable(mm->flags); 1646 return __get_dumpable(mm->flags);
2005} 1647}
2006
2007static void wait_for_dump_helpers(struct file *file)
2008{
2009 struct pipe_inode_info *pipe;
2010
2011 pipe = file->f_path.dentry->d_inode->i_pipe;
2012
2013 pipe_lock(pipe);
2014 pipe->readers++;
2015 pipe->writers--;
2016
2017 while ((pipe->readers > 1) && (!signal_pending(current))) {
2018 wake_up_interruptible_sync(&pipe->wait);
2019 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
2020 pipe_wait(pipe);
2021 }
2022
2023 pipe->readers--;
2024 pipe->writers++;
2025 pipe_unlock(pipe);
2026
2027}
2028
2029
2030/*
2031 * umh_pipe_setup
2032 * helper function to customize the process used
2033 * to collect the core in userspace. Specifically
2034 * it sets up a pipe and installs it as fd 0 (stdin)
2035 * for the process. Returns 0 on success, or
2036 * PTR_ERR on failure.
2037 * Note that it also sets the core limit to 1. This
2038 * is a special value that we use to trap recursive
2039 * core dumps
2040 */
2041static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
2042{
2043 struct file *files[2];
2044 struct coredump_params *cp = (struct coredump_params *)info->data;
2045 int err = create_pipe_files(files, 0);
2046 if (err)
2047 return err;
2048
2049 cp->file = files[1];
2050
2051 replace_fd(0, files[0], 0);
2052 /* and disallow core files too */
2053 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
2054
2055 return 0;
2056}
2057
2058void do_coredump(long signr, int exit_code, struct pt_regs *regs)
2059{
2060 struct core_state core_state;
2061 struct core_name cn;
2062 struct mm_struct *mm = current->mm;
2063 struct linux_binfmt * binfmt;
2064 const struct cred *old_cred;
2065 struct cred *cred;
2066 int retval = 0;
2067 int flag = 0;
2068 int ispipe;
2069 struct files_struct *displaced;
2070 bool need_nonrelative = false;
2071 static atomic_t core_dump_count = ATOMIC_INIT(0);
2072 struct coredump_params cprm = {
2073 .signr = signr,
2074 .regs = regs,
2075 .limit = rlimit(RLIMIT_CORE),
2076 /*
2077 * We must use the same mm->flags while dumping core to avoid
2078 * inconsistency of bit flags, since this flag is not protected
2079 * by any locks.
2080 */
2081 .mm_flags = mm->flags,
2082 };
2083
2084 audit_core_dumps(signr);
2085
2086 binfmt = mm->binfmt;
2087 if (!binfmt || !binfmt->core_dump)
2088 goto fail;
2089 if (!__get_dumpable(cprm.mm_flags))
2090 goto fail;
2091
2092 cred = prepare_creds();
2093 if (!cred)
2094 goto fail;
2095 /*
2096 * We cannot trust fsuid as being the "true" uid of the process
2097 * nor do we know its entire history. We only know it was tainted
2098 * so we dump it as root in mode 2, and only into a controlled
2099 * environment (pipe handler or fully qualified path).
2100 */
2101 if (__get_dumpable(cprm.mm_flags) == SUID_DUMPABLE_SAFE) {
2102 /* Setuid core dump mode */
2103 flag = O_EXCL; /* Stop rewrite attacks */
2104 cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
2105 need_nonrelative = true;
2106 }
2107
2108 retval = coredump_wait(exit_code, &core_state);
2109 if (retval < 0)
2110 goto fail_creds;
2111
2112 old_cred = override_creds(cred);
2113
2114 /*
2115 * Clear any false indication of pending signals that might
2116 * be seen by the filesystem code called to write the core file.
2117 */
2118 clear_thread_flag(TIF_SIGPENDING);
2119
2120 ispipe = format_corename(&cn, signr);
2121
2122 if (ispipe) {
2123 int dump_count;
2124 char **helper_argv;
2125
2126 if (ispipe < 0) {
2127 printk(KERN_WARNING "format_corename failed\n");
2128 printk(KERN_WARNING "Aborting core\n");
2129 goto fail_corename;
2130 }
2131
2132 if (cprm.limit == 1) {
2133 /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
2134 *
2135 * Normally core limits are irrelevant to pipes, since
2136 * we're not writing to the file system, but we use
2137 * cprm.limit of 1 here as a speacial value, this is a
2138 * consistent way to catch recursive crashes.
2139 * We can still crash if the core_pattern binary sets
2140 * RLIM_CORE = !1, but it runs as root, and can do
2141 * lots of stupid things.
2142 *
2143 * Note that we use task_tgid_vnr here to grab the pid
2144 * of the process group leader. That way we get the
2145 * right pid if a thread in a multi-threaded
2146 * core_pattern process dies.
2147 */
2148 printk(KERN_WARNING
2149 "Process %d(%s) has RLIMIT_CORE set to 1\n",
2150 task_tgid_vnr(current), current->comm);
2151 printk(KERN_WARNING "Aborting core\n");
2152 goto fail_unlock;
2153 }
2154 cprm.limit = RLIM_INFINITY;
2155
2156 dump_count = atomic_inc_return(&core_dump_count);
2157 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
2158 printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
2159 task_tgid_vnr(current), current->comm);
2160 printk(KERN_WARNING "Skipping core dump\n");
2161 goto fail_dropcount;
2162 }
2163
2164 helper_argv = argv_split(GFP_KERNEL, cn.corename+1, NULL);
2165 if (!helper_argv) {
2166 printk(KERN_WARNING "%s failed to allocate memory\n",
2167 __func__);
2168 goto fail_dropcount;
2169 }
2170
2171 retval = call_usermodehelper_fns(helper_argv[0], helper_argv,
2172 NULL, UMH_WAIT_EXEC, umh_pipe_setup,
2173 NULL, &cprm);
2174 argv_free(helper_argv);
2175 if (retval) {
2176 printk(KERN_INFO "Core dump to %s pipe failed\n",
2177 cn.corename);
2178 goto close_fail;
2179 }
2180 } else {
2181 struct inode *inode;
2182
2183 if (cprm.limit < binfmt->min_coredump)
2184 goto fail_unlock;
2185
2186 if (need_nonrelative && cn.corename[0] != '/') {
2187 printk(KERN_WARNING "Pid %d(%s) can only dump core "\
2188 "to fully qualified path!\n",
2189 task_tgid_vnr(current), current->comm);
2190 printk(KERN_WARNING "Skipping core dump\n");
2191 goto fail_unlock;
2192 }
2193
2194 cprm.file = filp_open(cn.corename,
2195 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
2196 0600);
2197 if (IS_ERR(cprm.file))
2198 goto fail_unlock;
2199
2200 inode = cprm.file->f_path.dentry->d_inode;
2201 if (inode->i_nlink > 1)
2202 goto close_fail;
2203 if (d_unhashed(cprm.file->f_path.dentry))
2204 goto close_fail;
2205 /*
2206 * AK: actually i see no reason to not allow this for named
2207 * pipes etc, but keep the previous behaviour for now.
2208 */
2209 if (!S_ISREG(inode->i_mode))
2210 goto close_fail;
2211 /*
2212 * Dont allow local users get cute and trick others to coredump
2213 * into their pre-created files.
2214 */
2215 if (!uid_eq(inode->i_uid, current_fsuid()))
2216 goto close_fail;
2217 if (!cprm.file->f_op || !cprm.file->f_op->write)
2218 goto close_fail;
2219 if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
2220 goto close_fail;
2221 }
2222
2223 /* get us an unshared descriptor table; almost always a no-op */
2224 retval = unshare_files(&displaced);
2225 if (retval)
2226 goto close_fail;
2227 if (displaced)
2228 put_files_struct(displaced);
2229 retval = binfmt->core_dump(&cprm);
2230 if (retval)
2231 current->signal->group_exit_code |= 0x80;
2232
2233 if (ispipe && core_pipe_limit)
2234 wait_for_dump_helpers(cprm.file);
2235close_fail:
2236 if (cprm.file)
2237 filp_close(cprm.file, NULL);
2238fail_dropcount:
2239 if (ispipe)
2240 atomic_dec(&core_dump_count);
2241fail_unlock:
2242 kfree(cn.corename);
2243fail_corename:
2244 coredump_finish(mm);
2245 revert_creds(old_cred);
2246fail_creds:
2247 put_cred(cred);
2248fail:
2249 return;
2250}
2251
2252/*
2253 * Core dumping helper functions. These are the only things you should
2254 * do on a core-file: use only these functions to write out all the
2255 * necessary info.
2256 */
2257int dump_write(struct file *file, const void *addr, int nr)
2258{
2259 return access_ok(VERIFY_READ, addr, nr) && file->f_op->write(file, addr, nr, &file->f_pos) == nr;
2260}
2261EXPORT_SYMBOL(dump_write);
2262
2263int dump_seek(struct file *file, loff_t off)
2264{
2265 int ret = 1;
2266
2267 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
2268 if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
2269 return 0;
2270 } else {
2271 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
2272
2273 if (!buf)
2274 return 0;
2275 while (off > 0) {
2276 unsigned long n = off;
2277
2278 if (n > PAGE_SIZE)
2279 n = PAGE_SIZE;
2280 if (!dump_write(file, buf, n)) {
2281 ret = 0;
2282 break;
2283 }
2284 off -= n;
2285 }
2286 free_page((unsigned long)buf);
2287 }
2288 return ret;
2289}
2290EXPORT_SYMBOL(dump_seek);