summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authormarkus@openbsd.org <markus@openbsd.org>2018-01-08 15:21:49 +0000
committerDamien Miller <djm@mindrot.org>2018-01-23 16:32:57 +1100
commit25cf9105b849932fc3b141590c009e704f2eeba6 (patch)
tree304d9af800f3450993741611dd28712928e61792 /misc.c
parentb0d34132b3ca26fe94013f01d7b92101e70b68bb (diff)
upstream commit
move subprocess() so scp/sftp do not need uidswap.o; ok djm@ OpenBSD-Commit-ID: 6601b8360388542c2e5fef0f4085f8e54750bea8
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c154
1 files changed, 1 insertions, 153 deletions
diff --git a/misc.c b/misc.c
index 50988cefe..f6fbb5e4a 100644
--- a/misc.c
+++ b/misc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: misc.c,v 1.122 2017/12/08 02:14:33 djm Exp $ */ 1/* $OpenBSD: misc.c,v 1.123 2018/01/08 15:21:49 markus Exp $ */
2/* 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved. 4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved.
@@ -1740,158 +1740,6 @@ argv_assemble(int argc, char **argv)
1740 return ret; 1740 return ret;
1741} 1741}
1742 1742
1743/*
1744 * Runs command in a subprocess wuth a minimal environment.
1745 * Returns pid on success, 0 on failure.
1746 * The child stdout and stderr maybe captured, left attached or sent to
1747 * /dev/null depending on the contents of flags.
1748 * "tag" is prepended to log messages.
1749 * NB. "command" is only used for logging; the actual command executed is
1750 * av[0].
1751 */
1752pid_t
1753subprocess(const char *tag, struct passwd *pw, const char *command,
1754 int ac, char **av, FILE **child, u_int flags)
1755{
1756 FILE *f = NULL;
1757 struct stat st;
1758 int fd, devnull, p[2], i;
1759 pid_t pid;
1760 char *cp, errmsg[512];
1761 u_int envsize;
1762 char **child_env;
1763
1764 if (child != NULL)
1765 *child = NULL;
1766
1767 debug3("%s: %s command \"%s\" running as %s (flags 0x%x)", __func__,
1768 tag, command, pw->pw_name, flags);
1769
1770 /* Check consistency */
1771 if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0 &&
1772 (flags & SSH_SUBPROCESS_STDOUT_CAPTURE) != 0) {
1773 error("%s: inconsistent flags", __func__);
1774 return 0;
1775 }
1776 if (((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) == 0) != (child == NULL)) {
1777 error("%s: inconsistent flags/output", __func__);
1778 return 0;
1779 }
1780
1781 /*
1782 * If executing an explicit binary, then verify the it exists
1783 * and appears safe-ish to execute
1784 */
1785 if (*av[0] != '/') {
1786 error("%s path is not absolute", tag);
1787 return 0;
1788 }
1789 temporarily_use_uid(pw);
1790 if (stat(av[0], &st) < 0) {
1791 error("Could not stat %s \"%s\": %s", tag,
1792 av[0], strerror(errno));
1793 restore_uid();
1794 return 0;
1795 }
1796 if (safe_path(av[0], &st, NULL, 0, errmsg, sizeof(errmsg)) != 0) {
1797 error("Unsafe %s \"%s\": %s", tag, av[0], errmsg);
1798 restore_uid();
1799 return 0;
1800 }
1801 /* Prepare to keep the child's stdout if requested */
1802 if (pipe(p) != 0) {
1803 error("%s: pipe: %s", tag, strerror(errno));
1804 restore_uid();
1805 return 0;
1806 }
1807 restore_uid();
1808
1809 switch ((pid = fork())) {
1810 case -1: /* error */
1811 error("%s: fork: %s", tag, strerror(errno));
1812 close(p[0]);
1813 close(p[1]);
1814 return 0;
1815 case 0: /* child */
1816 /* Prepare a minimal environment for the child. */
1817 envsize = 5;
1818 child_env = xcalloc(sizeof(*child_env), envsize);
1819 child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH);
1820 child_set_env(&child_env, &envsize, "USER", pw->pw_name);
1821 child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name);
1822 child_set_env(&child_env, &envsize, "HOME", pw->pw_dir);
1823 if ((cp = getenv("LANG")) != NULL)
1824 child_set_env(&child_env, &envsize, "LANG", cp);
1825
1826 for (i = 0; i < NSIG; i++)
1827 signal(i, SIG_DFL);
1828
1829 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1830 error("%s: open %s: %s", tag, _PATH_DEVNULL,
1831 strerror(errno));
1832 _exit(1);
1833 }
1834 if (dup2(devnull, STDIN_FILENO) == -1) {
1835 error("%s: dup2: %s", tag, strerror(errno));
1836 _exit(1);
1837 }
1838
1839 /* Set up stdout as requested; leave stderr in place for now. */
1840 fd = -1;
1841 if ((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) != 0)
1842 fd = p[1];
1843 else if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0)
1844 fd = devnull;
1845 if (fd != -1 && dup2(fd, STDOUT_FILENO) == -1) {
1846 error("%s: dup2: %s", tag, strerror(errno));
1847 _exit(1);
1848 }
1849 closefrom(STDERR_FILENO + 1);
1850
1851 /* Don't use permanently_set_uid() here to avoid fatal() */
1852 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
1853 error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid,
1854 strerror(errno));
1855 _exit(1);
1856 }
1857 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
1858 error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid,
1859 strerror(errno));
1860 _exit(1);
1861 }
1862 /* stdin is pointed to /dev/null at this point */
1863 if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0 &&
1864 dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
1865 error("%s: dup2: %s", tag, strerror(errno));
1866 _exit(1);
1867 }
1868
1869 execve(av[0], av, child_env);
1870 error("%s exec \"%s\": %s", tag, command, strerror(errno));
1871 _exit(127);
1872 default: /* parent */
1873 break;
1874 }
1875
1876 close(p[1]);
1877 if ((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) == 0)
1878 close(p[0]);
1879 else if ((f = fdopen(p[0], "r")) == NULL) {
1880 error("%s: fdopen: %s", tag, strerror(errno));
1881 close(p[0]);
1882 /* Don't leave zombie child */
1883 kill(pid, SIGTERM);
1884 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
1885 ;
1886 return 0;
1887 }
1888 /* Success */
1889 debug3("%s: %s pid %ld", __func__, tag, (long)pid);
1890 if (child != NULL)
1891 *child = f;
1892 return pid;
1893}
1894
1895/* Returns 0 if pid exited cleanly, non-zero otherwise */ 1743/* Returns 0 if pid exited cleanly, non-zero otherwise */
1896int 1744int
1897exited_cleanly(pid_t pid, const char *tag, const char *cmd, int quiet) 1745exited_cleanly(pid_t pid, const char *tag, const char *cmd, int quiet)