Blame SOURCES/openssh-8.0p1-restore-nonblock.patch

943807
diff --git a/channels.c b/channels.c
943807
index 32d1f617..0024f751 100644
943807
--- a/channels.c
943807
+++ b/channels.c
943807
@@ -333,7 +333,27 @@ channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd,
943807
 #endif
943807
 
943807
 	/* enable nonblocking mode */
943807
-	if (nonblock) {
943807
+	c->restore_block = 0;
943807
+	if (nonblock == CHANNEL_NONBLOCK_STDIO) {
943807
+		/*
943807
+		 * Special handling for stdio file descriptors: do not set
943807
+		 * non-blocking mode if they are TTYs. Otherwise prepare to
943807
+		 * restore their blocking state on exit to avoid interfering
943807
+		 * with other programs that follow.
943807
+		 */
943807
+		if (rfd != -1 && !isatty(rfd) && fcntl(rfd, F_GETFL) == 0) {
943807
+			c->restore_block |= CHANNEL_RESTORE_RFD;
943807
+			set_nonblock(rfd);
943807
+		}
943807
+		if (wfd != -1 && !isatty(wfd) && fcntl(wfd, F_GETFL) == 0) {
943807
+			c->restore_block |= CHANNEL_RESTORE_WFD;
943807
+			set_nonblock(wfd);
943807
+		}
943807
+		if (efd != -1 && !isatty(efd) && fcntl(efd, F_GETFL) == 0) {
943807
+			c->restore_block |= CHANNEL_RESTORE_EFD;
943807
+			set_nonblock(efd);
943807
+		}
943807
+	} else if (nonblock) {
943807
 		if (rfd != -1)
943807
 			set_nonblock(rfd);
943807
 		if (wfd != -1)
943807
@@ -422,17 +442,23 @@ channel_find_maxfd(struct ssh_channels *sc)
943807
 }
943807
 
943807
 int
943807
-channel_close_fd(struct ssh *ssh, int *fdp)
943807
+channel_close_fd(struct ssh *ssh, Channel *c, int *fdp)
943807
 {
943807
 	struct ssh_channels *sc = ssh->chanctxt;
943807
-	int ret = 0, fd = *fdp;
943807
+	int ret, fd = *fdp;
943807
 
943807
-	if (fd != -1) {
943807
-		ret = close(fd);
943807
-		*fdp = -1;
943807
-		if (fd == sc->channel_max_fd)
943807
-			channel_find_maxfd(sc);
943807
-	}
943807
+	if (fd == -1)
943807
+		return 0;
943807
+
943807
+	if ((*fdp == c->rfd && (c->restore_block & CHANNEL_RESTORE_RFD) != 0) ||
943807
+	   (*fdp == c->wfd && (c->restore_block & CHANNEL_RESTORE_WFD) != 0) ||
943807
+	   (*fdp == c->efd && (c->restore_block & CHANNEL_RESTORE_EFD) != 0))
943807
+		(void)fcntl(*fdp, F_SETFL, 0);	/* restore blocking */
943807
+
943807
+	ret = close(fd);
943807
+	*fdp = -1;
943807
+	if (fd == sc->channel_max_fd)
943807
+		channel_find_maxfd(sc);
943807
 	return ret;
943807
 }
943807
 
943807
@@ -442,13 +468,13 @@ channel_close_fds(struct ssh *ssh, Channel *c)
943807
 {
943807
 	int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd;
943807
 
943807
-	channel_close_fd(ssh, &c->sock);
943807
+	channel_close_fd(ssh, c, &c->sock);
943807
 	if (rfd != sock)
943807
-		channel_close_fd(ssh, &c->rfd);
943807
+		channel_close_fd(ssh, c, &c->rfd);
943807
 	if (wfd != sock && wfd != rfd)
943807
-		channel_close_fd(ssh, &c->wfd);
943807
+		channel_close_fd(ssh, c, &c->wfd);
943807
 	if (efd != sock && efd != rfd && efd != wfd)
943807
-		channel_close_fd(ssh, &c->efd);
943807
+		channel_close_fd(ssh, c, &c->efd);
943807
 }
943807
 
943807
 static void
943807
@@ -702,7 +728,7 @@ channel_stop_listening(struct ssh *ssh)
943807
 			case SSH_CHANNEL_X11_LISTENER:
943807
 			case SSH_CHANNEL_UNIX_LISTENER:
943807
 			case SSH_CHANNEL_RUNIX_LISTENER:
943807
-				channel_close_fd(ssh, &c->sock);
943807
+				channel_close_fd(ssh, c, &c->sock);
943807
 				channel_free(ssh, c);
943807
 				break;
943807
 			}
943807
@@ -1491,7 +1517,8 @@ channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output)
943807
 
943807
 Channel *
943807
 channel_connect_stdio_fwd(struct ssh *ssh,
943807
-    const char *host_to_connect, u_short port_to_connect, int in, int out)
943807
+    const char *host_to_connect, u_short port_to_connect,
943807
+    int in, int out, int nonblock)
943807
 {
943807
 	Channel *c;
943807
 
943807
@@ -1499,7 +1526,7 @@ channel_connect_stdio_fwd(struct ssh *ssh,
943807
 
943807
 	c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
943807
 	    -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
943807
-	    0, "stdio-forward", /*nonblock*/0);
943807
+	    0, "stdio-forward", nonblock);
943807
 
943807
 	c->path = xstrdup(host_to_connect);
943807
 	c->host_port = port_to_connect;
943807
@@ -1649,7 +1676,7 @@ channel_post_x11_listener(struct ssh *ssh, Channel *c,
943807
 	if (c->single_connection) {
943807
 		oerrno = errno;
943807
 		debug2("single_connection: closing X11 listener.");
943807
-		channel_close_fd(ssh, &c->sock);
943807
+		channel_close_fd(ssh, c, &c->sock);
943807
 		chan_mark_dead(ssh, c);
943807
 		errno = oerrno;
943807
 	}
943807
@@ -2058,7 +2085,7 @@ channel_handle_efd_write(struct ssh *ssh, Channel *c,
943807
 		return 1;
943807
 	if (len <= 0) {
943807
 		debug2("channel %d: closing write-efd %d", c->self, c->efd);
943807
-		channel_close_fd(ssh, &c->efd);
943807
+		channel_close_fd(ssh, c, &c->efd);
943807
 	} else {
943807
 		if ((r = sshbuf_consume(c->extended, len)) != 0)
943807
 			fatal_fr(r, "channel %i: consume", c->self);
943807
@@ -2087,7 +2114,7 @@ channel_handle_efd_read(struct ssh *ssh, Channel *c,
943807
 		return 1;
943807
 	if (len <= 0) {
943807
 		debug2("channel %d: closing read-efd %d", c->self, c->efd);
943807
-		channel_close_fd(ssh, &c->efd);
943807
+		channel_close_fd(ssh, c, &c->efd);
943807
 	} else if (c->extended_usage == CHAN_EXTENDED_IGNORE)
943807
 		debug3("channel %d: discard efd", c->self);
943807
 	else if ((r = sshbuf_put(c->extended, buf, len)) != 0)
943807
diff --git a/channels.h b/channels.h
943807
index 378d987c..6bf86b00 100644
943807
--- a/channels.h
943807
+++ b/channels.h
943807
@@ -63,6 +63,16 @@
943807
 
943807
 #define CHANNEL_CANCEL_PORT_STATIC	-1
943807
 
943807
+/* nonblocking flags for channel_new */
943807
+#define CHANNEL_NONBLOCK_LEAVE	0 /* don't modify non-blocking state */
943807
+#define CHANNEL_NONBLOCK_SET	1 /* set non-blocking state */
943807
+#define CHANNEL_NONBLOCK_STDIO	2 /* set non-blocking and restore on close */
943807
+
943807
+/* c->restore_block mask flags */
943807
+#define CHANNEL_RESTORE_RFD	0x01
943807
+#define CHANNEL_RESTORE_WFD	0x02
943807
+#define CHANNEL_RESTORE_EFD	0x04
943807
+
943807
 /* TCP forwarding */
943807
 #define FORWARD_DENY		0
943807
 #define FORWARD_REMOTE		(1)
943807
@@ -139,6 +149,7 @@ struct Channel {
943807
 				 * to a matching pre-select handler.
943807
 				 * this way post-select handlers are not
943807
 				 * accidentally called if a FD gets reused */
943807
+	int	restore_block;	/* fd mask to restore blocking status */
943807
 	struct sshbuf *input;	/* data read from socket, to be sent over
943807
 				 * encrypted connection */
943807
 	struct sshbuf *output;	/* data received over encrypted connection for
943807
@@ -266,7 +277,7 @@ void	 channel_register_filter(struct ssh *, int, channel_infilter_fn *,
943807
 void	 channel_register_status_confirm(struct ssh *, int,
943807
 	    channel_confirm_cb *, channel_confirm_abandon_cb *, void *);
943807
 void	 channel_cancel_cleanup(struct ssh *, int);
943807
-int	 channel_close_fd(struct ssh *, int *);
943807
+int	 channel_close_fd(struct ssh *, Channel *, int *);
943807
 void	 channel_send_window_changes(struct ssh *);
943807
 
943807
 /* mux proxy support */
943807
@@ -313,7 +324,7 @@ Channel	*channel_connect_to_port(struct ssh *, const char *, u_short,
943807
 	    char *, char *, int *, const char **);
943807
 Channel *channel_connect_to_path(struct ssh *, const char *, char *, char *);
943807
 Channel	*channel_connect_stdio_fwd(struct ssh *, const char*,
943807
-	    u_short, int, int);
943807
+	    u_short, int, int, int);
943807
 Channel	*channel_connect_by_listen_address(struct ssh *, const char *,
943807
 	    u_short, char *, char *);
943807
 Channel	*channel_connect_by_listen_path(struct ssh *, const char *,
943807
diff --git a/clientloop.c b/clientloop.c
943807
index 219f0e90..bdd67686 100644
943807
--- a/clientloop.c
943807
+++ b/clientloop.c
943807
@@ -1405,14 +1405,6 @@ client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
943807
 	if (have_pty)
943807
 		leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
943807
 
943807
-	/* restore blocking io */
943807
-	if (!isatty(fileno(stdin)))
943807
-		unset_nonblock(fileno(stdin));
943807
-	if (!isatty(fileno(stdout)))
943807
-		unset_nonblock(fileno(stdout));
943807
-	if (!isatty(fileno(stderr)))
943807
-		unset_nonblock(fileno(stderr));
943807
-
943807
 	/*
943807
 	 * If there was no shell or command requested, there will be no remote
943807
 	 * exit status to be returned.  In that case, clear error code if the
943807
diff --git a/mux.c b/mux.c
943807
index faf4ef1e..9454bfed 100644
943807
--- a/mux.c
943807
+++ b/mux.c
943807
@@ -452,14 +452,6 @@ mux_master_process_new_session(struct ssh *ssh, u_int rid,
943807
 	if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
943807
 		error_f("tcgetattr: %s", strerror(errno));
943807
 
943807
-	/* enable nonblocking unless tty */
943807
-	if (!isatty(new_fd[0]))
943807
-		set_nonblock(new_fd[0]);
943807
-	if (!isatty(new_fd[1]))
943807
-		set_nonblock(new_fd[1]);
943807
-	if (!isatty(new_fd[2]))
943807
-		set_nonblock(new_fd[2]);
943807
-
943807
 	window = CHAN_SES_WINDOW_DEFAULT;
943807
 	packetmax = CHAN_SES_PACKET_DEFAULT;
943807
 	if (cctx->want_tty) {
943807
@@ -469,7 +461,7 @@ mux_master_process_new_session(struct ssh *ssh, u_int rid,
943807
 
943807
 	nc = channel_new(ssh, "session", SSH_CHANNEL_OPENING,
943807
 	    new_fd[0], new_fd[1], new_fd[2], window, packetmax,
943807
-	    CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
943807
+	    CHAN_EXTENDED_WRITE, "client-session", CHANNEL_NONBLOCK_STDIO);
943807
 
943807
 	nc->ctl_chan = c->self;		/* link session -> control channel */
943807
 	c->remote_id = nc->self;	/* link control -> session channel */
943807
@@ -1025,13 +1017,8 @@ mux_master_process_stdio_fwd(struct ssh *ssh, u_int rid,
943807
 		}
943807
 	}
943807
 
943807
-	/* enable nonblocking unless tty */
943807
-	if (!isatty(new_fd[0]))
943807
-		set_nonblock(new_fd[0]);
943807
-	if (!isatty(new_fd[1]))
943807
-		set_nonblock(new_fd[1]);
943807
-
943807
-	nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1]);
943807
+	nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1],
943807
+	    CHANNEL_NONBLOCK_STDIO);
943807
 	free(chost);
943807
 
943807
 	nc->ctl_chan = c->self;		/* link session -> control channel */
943807
diff --git a/nchan.c b/nchan.c
943807
index 4a4494b8..7ef3a350 100644
943807
--- a/nchan.c
943807
+++ b/nchan.c
943807
@@ -384,7 +384,7 @@ chan_shutdown_write(struct ssh *ssh, Channel *c)
943807
 			    c->istate, c->ostate, strerror(errno));
943807
 		}
943807
 	} else {
943807
-		if (channel_close_fd(ssh, &c->wfd) < 0) {
943807
+		if (channel_close_fd(ssh, c, &c->wfd) < 0) {
943807
 			logit_f("channel %d: close() failed for "
943807
 			    "fd %d [i%d o%d]: %.100s", c->self, c->wfd,
943807
 			    c->istate, c->ostate, strerror(errno));
943807
@@ -412,7 +412,7 @@ chan_shutdown_read(struct ssh *ssh, Channel *c)
943807
 			    c->istate, c->ostate, strerror(errno));
943807
 		}
943807
 	} else {
943807
-		if (channel_close_fd(ssh, &c->rfd) < 0) {
943807
+		if (channel_close_fd(ssh, c, &c->rfd) < 0) {
943807
 			logit_f("channel %d: close() failed for "
943807
 			    "fd %d [i%d o%d]: %.100s", c->self, c->rfd,
943807
 			    c->istate, c->ostate, strerror(errno));
943807
@@ -431,7 +431,7 @@ chan_shutdown_extended_read(struct ssh *ssh, Channel *c)
943807
 	debug_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
943807
 	    c->self, c->istate, c->ostate, c->sock, c->rfd, c->efd,
943807
 	    channel_format_extended_usage(c));
943807
-	if (channel_close_fd(ssh, &c->efd) < 0) {
943807
+	if (channel_close_fd(ssh, c, &c->efd) < 0) {
943807
 		logit_f("channel %d: close() failed for "
943807
 		    "extended fd %d [i%d o%d]: %.100s", c->self, c->efd,
943807
 		    c->istate, c->ostate, strerror(errno));
943807
diff --git a/ssh.c b/ssh.c
943807
index 696dc3bc..6243db76 100644
943807
--- a/ssh.c
943807
+++ b/ssh.c
943807
@@ -1876,9 +1876,10 @@ ssh_init_stdio_forwarding(struct ssh *ssh)
943807
 
943807
 	if ((in = dup(STDIN_FILENO)) == -1 ||
943807
 	    (out = dup(STDOUT_FILENO)) == -1)
943807
-		fatal("channel_connect_stdio_fwd: dup() in/out failed");
943807
+		fatal_f("dup() in/out failed");
943807
 	if ((c = channel_connect_stdio_fwd(ssh, options.stdio_forward_host,
943807
-	    options.stdio_forward_port, in, out)) == NULL)
943807
+	    options.stdio_forward_port, in, out,
943807
+	    CHANNEL_NONBLOCK_STDIO)) == NULL)
943807
 		fatal_f("channel_connect_stdio_fwd failed");
943807
 	channel_register_cleanup(ssh, c->self, client_cleanup_stdio_fwd, 0);
943807
 	channel_register_open_confirm(ssh, c->self, ssh_stdio_confirm, NULL);
943807
@@ -2074,14 +2075,6 @@ ssh_session2_open(struct ssh *ssh)
943807
 	if (in == -1 || out == -1 || err == -1)
943807
 		fatal("dup() in/out/err failed");
943807
 
943807
-	/* enable nonblocking unless tty */
943807
-	if (!isatty(in))
943807
-		set_nonblock(in);
943807
-	if (!isatty(out))
943807
-		set_nonblock(out);
943807
-	if (!isatty(err))
943807
-		set_nonblock(err);
943807
-
943807
 	window = CHAN_SES_WINDOW_DEFAULT;
943807
 	packetmax = CHAN_SES_PACKET_DEFAULT;
943807
 	if (tty_flag) {
943807
@@ -2091,7 +2084,7 @@ ssh_session2_open(struct ssh *ssh)
943807
 	c = channel_new(ssh,
943807
 	    "session", SSH_CHANNEL_OPENING, in, out, err,
943807
 	    window, packetmax, CHAN_EXTENDED_WRITE,
943807
-	    "client-session", /*nonblock*/0);
943807
+	    "client-session", CHANNEL_NONBLOCK_STDIO);
943807
 
943807
 	debug3_f("channel_new: %d", c->self);
943807