summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2020-09-20 23:31:46 +0000
committerDamien Miller <djm@mindrot.org>2020-09-21 09:32:48 +1000
commitd14fe25e6c3b89f8af17e2894046164ac3b45688 (patch)
tree7e8098bc71059950dd23948749dab94390ba336e
parent53a33a0d745179c02108589e1722457ca8ae4372 (diff)
upstream: close stdout/stderr after "ssh -f ..." forking
bz#3137, ok markus OpenBSD-Commit-ID: e2d83cc4dea1665651a7aa924ad1ed6bcaaab3e2
-rw-r--r--ssh.c27
1 files changed, 21 insertions, 6 deletions
diff --git a/ssh.c b/ssh.c
index 9c6a6278b..6202e3c09 100644
--- a/ssh.c
+++ b/ssh.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssh.c,v 1.534 2020/07/31 04:19:37 dtucker Exp $ */ 1/* $OpenBSD: ssh.c,v 1.535 2020/09/20 23:31:46 djm Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -1745,12 +1745,25 @@ control_persist_detach(void)
1745static void 1745static void
1746fork_postauth(void) 1746fork_postauth(void)
1747{ 1747{
1748 int devnull, keep_stderr;
1749
1748 if (need_controlpersist_detach) 1750 if (need_controlpersist_detach)
1749 control_persist_detach(); 1751 control_persist_detach();
1750 debug("forking to background"); 1752 debug("forking to background");
1751 fork_after_authentication_flag = 0; 1753 fork_after_authentication_flag = 0;
1752 if (daemon(1, 1) == -1) 1754 if (daemon(1, 1) == -1)
1753 fatal("daemon() failed: %.200s", strerror(errno)); 1755 fatal("daemon() failed: %.200s", strerror(errno));
1756 if ((devnull = open(_PATH_DEVNULL, O_WRONLY)) == -1)
1757 error("%s: open %s: %s", __func__,
1758 _PATH_DEVNULL, strerror(errno));
1759 else {
1760 keep_stderr = log_is_on_stderr() && debug_flag;
1761 if (dup2(devnull, STDOUT_FILENO) == -1 ||
1762 (!keep_stderr && dup2(devnull, STDOUT_FILENO) == -1))
1763 fatal("%s: dup2() stdio failed", __func__);
1764 if (devnull > STDERR_FILENO)
1765 close(devnull);
1766 }
1754} 1767}
1755 1768
1756static void 1769static void
@@ -2149,13 +2162,15 @@ ssh_session2(struct ssh *ssh, struct passwd *pw)
2149 * as it may want to write to stdout. 2162 * as it may want to write to stdout.
2150 */ 2163 */
2151 if (!need_controlpersist_detach) { 2164 if (!need_controlpersist_detach) {
2152 if ((devnull = open(_PATH_DEVNULL, O_WRONLY)) == -1) 2165 if ((devnull = open(_PATH_DEVNULL, O_WRONLY)) == -1) {
2153 error("%s: open %s: %s", __func__, 2166 error("%s: open %s: %s", __func__,
2154 _PATH_DEVNULL, strerror(errno)); 2167 _PATH_DEVNULL, strerror(errno));
2155 if (dup2(devnull, STDOUT_FILENO) == -1) 2168 } else {
2156 fatal("%s: dup2() stdout failed", __func__); 2169 if (dup2(devnull, STDOUT_FILENO) == -1)
2157 if (devnull > STDERR_FILENO) 2170 fatal("%s: dup2() stdout failed", __func__);
2158 close(devnull); 2171 if (devnull > STDERR_FILENO)
2172 close(devnull);
2173 }
2159 } 2174 }
2160 2175
2161 /* 2176 /*