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

152122
diff -up openssh-8.0p1/channels.c.restore-nonblock openssh-8.0p1/channels.c
152122
--- openssh-8.0p1/channels.c.restore-nonblock	2021-06-21 10:44:26.380559612 +0200
152122
+++ openssh-8.0p1/channels.c	2021-06-21 10:48:47.754579151 +0200
152122
@@ -333,7 +333,27 @@ channel_register_fds(struct ssh *ssh, Ch
152122
 #endif
152122
 
152122
 	/* enable nonblocking mode */
152122
-	if (nonblock) {
152122
+	c->restore_block = 0;
152122
+	if (nonblock == CHANNEL_NONBLOCK_STDIO) {
152122
+		/*
152122
+		 * Special handling for stdio file descriptors: do not set
152122
+		 * non-blocking mode if they are TTYs. Otherwise prepare to
152122
+		 * restore their blocking state on exit to avoid interfering
152122
+		 * with other programs that follow.
152122
+		 */
152122
+		if (rfd != -1 && !isatty(rfd) && fcntl(rfd, F_GETFL) == 0) {
152122
+			c->restore_block |= CHANNEL_RESTORE_RFD;
152122
+			set_nonblock(rfd);
152122
+		}
152122
+		if (wfd != -1 && !isatty(wfd) && fcntl(wfd, F_GETFL) == 0) {
152122
+			c->restore_block |= CHANNEL_RESTORE_WFD;
152122
+			set_nonblock(wfd);
152122
+		}
152122
+		if (efd != -1 && !isatty(efd) && fcntl(efd, F_GETFL) == 0) {
152122
+			c->restore_block |= CHANNEL_RESTORE_EFD;
152122
+			set_nonblock(efd);
152122
+		}
152122
+	} else if (nonblock) {
152122
 		if (rfd != -1)
152122
 			set_nonblock(rfd);
152122
 		if (wfd != -1)
152122
@@ -422,17 +442,23 @@ channel_find_maxfd(struct ssh_channels *
152122
 }
152122
 
152122
 int
152122
-channel_close_fd(struct ssh *ssh, int *fdp)
152122
+channel_close_fd(struct ssh *ssh, Channel *c, int *fdp)
152122
 {
152122
 	struct ssh_channels *sc = ssh->chanctxt;
152122
-	int ret = 0, fd = *fdp;
152122
+	int ret, fd = *fdp;
152122
 
152122
-	if (fd != -1) {
152122
-		ret = close(fd);
152122
-		*fdp = -1;
152122
-		if (fd == sc->channel_max_fd)
152122
-			channel_find_maxfd(sc);
152122
-	}
152122
+	if (fd == -1)
152122
+		return 0;
152122
+
152122
+	if ((*fdp == c->rfd && (c->restore_block & CHANNEL_RESTORE_RFD) != 0) ||
152122
+	   (*fdp == c->wfd && (c->restore_block & CHANNEL_RESTORE_WFD) != 0) ||
152122
+	   (*fdp == c->efd && (c->restore_block & CHANNEL_RESTORE_EFD) != 0))
152122
+		(void)fcntl(*fdp, F_SETFL, 0);	/* restore blocking */
152122
+
152122
+	ret = close(fd);
152122
+	*fdp = -1;
152122
+	if (fd == sc->channel_max_fd)
152122
+		channel_find_maxfd(sc);
152122
 	return ret;
152122
 }
152122
 
152122
@@ -442,13 +468,13 @@ channel_close_fds(struct ssh *ssh, Chann
152122
 {
152122
 	int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd;
152122
 
152122
-	channel_close_fd(ssh, &c->sock);
152122
+	channel_close_fd(ssh, c, &c->sock);
152122
 	if (rfd != sock)
152122
-		channel_close_fd(ssh, &c->rfd);
152122
+		channel_close_fd(ssh, c, &c->rfd);
152122
 	if (wfd != sock && wfd != rfd)
152122
-		channel_close_fd(ssh, &c->wfd);
152122
+		channel_close_fd(ssh, c, &c->wfd);
152122
 	if (efd != sock && efd != rfd && efd != wfd)
152122
-		channel_close_fd(ssh, &c->efd);
152122
+		channel_close_fd(ssh, c, &c->efd);
152122
 }
152122
 
152122
 static void
152122
@@ -681,7 +707,7 @@ channel_stop_listening(struct ssh *ssh)
152122
 			case SSH_CHANNEL_X11_LISTENER:
152122
 			case SSH_CHANNEL_UNIX_LISTENER:
152122
 			case SSH_CHANNEL_RUNIX_LISTENER:
152122
-				channel_close_fd(ssh, &c->sock);
152122
+				channel_close_fd(ssh, c, &c->sock);
152122
 				channel_free(ssh, c);
152122
 				break;
152122
 			}
152122
@@ -1487,7 +1513,8 @@ channel_decode_socks5(Channel *c, struct
152122
 
152122
 Channel *
152122
 channel_connect_stdio_fwd(struct ssh *ssh,
152122
-    const char *host_to_connect, u_short port_to_connect, int in, int out)
152122
+    const char *host_to_connect, u_short port_to_connect,
152122
+    int in, int out, int nonblock)
152122
 {
152122
 	Channel *c;
152122
 
152122
@@ -1495,7 +1522,7 @@ channel_connect_stdio_fwd(struct ssh *ss
152122
 
152122
 	c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
152122
 	    -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
152122
-	    0, "stdio-forward", /*nonblock*/0);
152122
+	    0, "stdio-forward", nonblock);
152122
 
152122
 	c->path = xstrdup(host_to_connect);
152122
 	c->host_port = port_to_connect;
152122
@@ -1650,7 +1677,7 @@ channel_post_x11_listener(struct ssh *ss
152122
 	if (c->single_connection) {
152122
 		oerrno = errno;
152122
 		debug2("single_connection: closing X11 listener.");
152122
-		channel_close_fd(ssh, &c->sock);
152122
+		channel_close_fd(ssh, c, &c->sock);
152122
 		chan_mark_dead(ssh, c);
152122
 		errno = oerrno;
152122
 	}
152122
@@ -2087,7 +2114,7 @@ channel_handle_efd_write(struct ssh *ssh
152122
 		return 1;
152122
 	if (len <= 0) {
152122
 		debug2("channel %d: closing write-efd %d", c->self, c->efd);
152122
-		channel_close_fd(ssh, &c->efd);
152122
+		channel_close_fd(ssh, c, &c->efd);
152122
 	} else {
152122
 		if ((r = sshbuf_consume(c->extended, len)) != 0) {
152122
 			fatal("%s: channel %d: consume: %s",
152122
@@ -2119,7 +2146,7 @@ channel_handle_efd_read(struct ssh *ssh,
152122
 	if (len <= 0) {
152122
 		debug2("channel %d: closing read-efd %d",
152122
 		    c->self, c->efd);
152122
-		channel_close_fd(ssh, &c->efd);
152122
+		channel_close_fd(ssh, c, &c->efd);
152122
 	} else {
152122
 		if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
152122
 			debug3("channel %d: discard efd",
152122
diff -up openssh-8.0p1/channels.h.restore-nonblock openssh-8.0p1/channels.h
152122
--- openssh-8.0p1/channels.h.restore-nonblock	2021-06-21 10:44:26.380559612 +0200
152122
+++ openssh-8.0p1/channels.h	2021-06-21 10:44:26.387559665 +0200
152122
@@ -63,6 +63,16 @@
152122
 
152122
 #define CHANNEL_CANCEL_PORT_STATIC	-1
152122
 
152122
+/* nonblocking flags for channel_new */
152122
+#define CHANNEL_NONBLOCK_LEAVE	0 /* don't modify non-blocking state */
152122
+#define CHANNEL_NONBLOCK_SET	1 /* set non-blocking state */
152122
+#define CHANNEL_NONBLOCK_STDIO	2 /* set non-blocking and restore on close */
152122
+
152122
+/* c->restore_block mask flags */
152122
+#define CHANNEL_RESTORE_RFD	0x01
152122
+#define CHANNEL_RESTORE_WFD	0x02
152122
+#define CHANNEL_RESTORE_EFD	0x04
152122
+
152122
 /* TCP forwarding */
152122
 #define FORWARD_DENY		0
152122
 #define FORWARD_REMOTE		(1)
152122
@@ -131,6 +141,7 @@ struct Channel {
152122
 				 * to a matching pre-select handler.
152122
 				 * this way post-select handlers are not
152122
 				 * accidentally called if a FD gets reused */
152122
+	int	restore_block;	/* fd mask to restore blocking status */
152122
 	struct sshbuf *input;	/* data read from socket, to be sent over
152122
 				 * encrypted connection */
152122
 	struct sshbuf *output;	/* data received over encrypted connection for
152122
@@ -258,7 +269,7 @@ void	 channel_register_filter(struct ssh
152122
 void	 channel_register_status_confirm(struct ssh *, int,
152122
 	    channel_confirm_cb *, channel_confirm_abandon_cb *, void *);
152122
 void	 channel_cancel_cleanup(struct ssh *, int);
152122
-int	 channel_close_fd(struct ssh *, int *);
152122
+int	 channel_close_fd(struct ssh *, Channel *, int *);
152122
 void	 channel_send_window_changes(struct ssh *);
152122
 
152122
 /* mux proxy support */
152122
@@ -305,7 +316,7 @@ Channel	*channel_connect_to_port(struct
152122
 	    char *, char *, int *, const char **);
152122
 Channel *channel_connect_to_path(struct ssh *, const char *, char *, char *);
152122
 Channel	*channel_connect_stdio_fwd(struct ssh *, const char*,
152122
-	    u_short, int, int);
152122
+	    u_short, int, int, int);
152122
 Channel	*channel_connect_by_listen_address(struct ssh *, const char *,
152122
 	    u_short, char *, char *);
152122
 Channel	*channel_connect_by_listen_path(struct ssh *, const char *,
152122
diff -up openssh-8.0p1/clientloop.c.restore-nonblock openssh-8.0p1/clientloop.c
152122
--- openssh-8.0p1/clientloop.c.restore-nonblock	2021-06-21 10:44:26.290558923 +0200
152122
+++ openssh-8.0p1/clientloop.c	2021-06-21 10:44:26.387559665 +0200
152122
@@ -1436,14 +1436,6 @@ client_loop(struct ssh *ssh, int have_pt
152122
 	if (have_pty)
152122
 		leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
152122
 
152122
-	/* restore blocking io */
152122
-	if (!isatty(fileno(stdin)))
152122
-		unset_nonblock(fileno(stdin));
152122
-	if (!isatty(fileno(stdout)))
152122
-		unset_nonblock(fileno(stdout));
152122
-	if (!isatty(fileno(stderr)))
152122
-		unset_nonblock(fileno(stderr));
152122
-
152122
 	/*
152122
 	 * If there was no shell or command requested, there will be no remote
152122
 	 * exit status to be returned.  In that case, clear error code if the
152122
diff -up openssh-8.0p1/mux.c.restore-nonblock openssh-8.0p1/mux.c
152122
--- openssh-8.0p1/mux.c.restore-nonblock	2019-04-18 00:52:57.000000000 +0200
152122
+++ openssh-8.0p1/mux.c	2021-06-21 10:50:51.007537336 +0200
152122
@@ -454,14 +454,6 @@ mux_master_process_new_session(struct ss
152122
 	if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
152122
 		error("%s: tcgetattr: %s", __func__, strerror(errno));
152122
 
152122
-	/* enable nonblocking unless tty */
152122
-	if (!isatty(new_fd[0]))
152122
-		set_nonblock(new_fd[0]);
152122
-	if (!isatty(new_fd[1]))
152122
-		set_nonblock(new_fd[1]);
152122
-	if (!isatty(new_fd[2]))
152122
-		set_nonblock(new_fd[2]);
152122
-
152122
 	window = CHAN_SES_WINDOW_DEFAULT;
152122
 	packetmax = CHAN_SES_PACKET_DEFAULT;
152122
 	if (cctx->want_tty) {
152122
@@ -471,7 +463,7 @@ mux_master_process_new_session(struct ss
152122
 
152122
 	nc = channel_new(ssh, "session", SSH_CHANNEL_OPENING,
152122
 	    new_fd[0], new_fd[1], new_fd[2], window, packetmax,
152122
-	    CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
152122
+	    CHAN_EXTENDED_WRITE, "client-session", CHANNEL_NONBLOCK_STDIO);
152122
 
152122
 	nc->ctl_chan = c->self;		/* link session -> control channel */
152122
 	c->remote_id = nc->self; 	/* link control -> session channel */
152122
@@ -1033,13 +1025,8 @@ mux_master_process_stdio_fwd(struct ssh
152122
 		}
152122
 	}
152122
 
152122
-	/* enable nonblocking unless tty */
152122
-	if (!isatty(new_fd[0]))
152122
-		set_nonblock(new_fd[0]);
152122
-	if (!isatty(new_fd[1]))
152122
-		set_nonblock(new_fd[1]);
152122
-
152122
-	nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1]);
152122
+	nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1],
152122
+	    CHANNEL_NONBLOCK_STDIO);
152122
 	free(chost);
152122
 
152122
 	nc->ctl_chan = c->self;		/* link session -> control channel */
152122
diff -up openssh-8.0p1/nchan.c.restore-nonblock openssh-8.0p1/nchan.c
152122
--- openssh-8.0p1/nchan.c.restore-nonblock	2021-06-21 10:44:26.388559673 +0200
152122
+++ openssh-8.0p1/nchan.c	2021-06-21 10:52:42.685405537 +0200
152122
@@ -387,7 +387,7 @@ chan_shutdown_write(struct ssh *ssh, Cha
152122
 			    strerror(errno));
152122
 		}
152122
 	} else {
152122
-		if (channel_close_fd(ssh, &c->wfd) < 0) {
152122
+		if (channel_close_fd(ssh, c, &c->wfd) < 0) {
152122
 			logit("channel %d: %s: close() failed for "
152122
 			    "fd %d [i%d o%d]: %.100s",
152122
 			    c->self, __func__, c->wfd, c->istate, c->ostate,
152122
@@ -417,7 +417,7 @@ chan_shutdown_read(struct ssh *ssh, Chan
152122
  			    strerror(errno));
152122
 		}
152122
 	} else {
152122
-		if (channel_close_fd(ssh, &c->rfd) < 0) {
152122
+		if (channel_close_fd(ssh, c, &c->rfd) < 0) {
152122
 			logit("channel %d: %s: close() failed for "
152122
 			    "fd %d [i%d o%d]: %.100s",
152122
 			    c->self, __func__, c->rfd, c->istate, c->ostate,
152122
@@ -437,7 +437,7 @@ chan_shutdown_extended_read(struct ssh *
152122
 	debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
152122
 	    c->self, __func__, c->istate, c->ostate, c->sock, c->rfd, c->efd,
152122
 	    channel_format_extended_usage(c));
152122
-	if (channel_close_fd(ssh, &c->efd) < 0) {
152122
+	if (channel_close_fd(ssh, c, &c->efd) < 0) {
152122
 		logit("channel %d: %s: close() failed for "
152122
 		    "extended fd %d [i%d o%d]: %.100s",
152122
 		    c->self, __func__, c->efd, c->istate, c->ostate,
152122
diff -up openssh-8.0p1/ssh.c.restore-nonblock openssh-8.0p1/ssh.c
152122
--- openssh-8.0p1/ssh.c.restore-nonblock	2021-06-21 10:44:26.389559681 +0200
152122
+++ openssh-8.0p1/ssh.c	2021-06-21 10:54:47.651377045 +0200
152122
@@ -1709,7 +1709,8 @@ ssh_init_stdio_forwarding(struct ssh *ss
152122
 	    (out = dup(STDOUT_FILENO)) < 0)
152122
 		fatal("channel_connect_stdio_fwd: dup() in/out failed");
152122
 	if ((c = channel_connect_stdio_fwd(ssh, options.stdio_forward_host,
152122
-	    options.stdio_forward_port, in, out)) == NULL)
152122
+	    options.stdio_forward_port, in, out,
152122
+	    CHANNEL_NONBLOCK_STDIO)) == NULL)
152122
 		fatal("%s: channel_connect_stdio_fwd failed", __func__);
152122
 	channel_register_cleanup(ssh, c->self, client_cleanup_stdio_fwd, 0);
152122
 	channel_register_open_confirm(ssh, c->self, ssh_stdio_confirm, NULL);
152122
@@ -1862,14 +1863,6 @@ ssh_session2_open(struct ssh *ssh)
152122
 	if (in < 0 || out < 0 || err < 0)
152122
 		fatal("dup() in/out/err failed");
152122
 
152122
-	/* enable nonblocking unless tty */
152122
-	if (!isatty(in))
152122
-		set_nonblock(in);
152122
-	if (!isatty(out))
152122
-		set_nonblock(out);
152122
-	if (!isatty(err))
152122
-		set_nonblock(err);
152122
-
152122
 	window = CHAN_SES_WINDOW_DEFAULT;
152122
 	packetmax = CHAN_SES_PACKET_DEFAULT;
152122
 	if (tty_flag) {
152122
@@ -1879,7 +1872,7 @@ ssh_session2_open(struct ssh *ssh)
152122
 	c = channel_new(ssh,
152122
 	    "session", SSH_CHANNEL_OPENING, in, out, err,
152122
 	    window, packetmax, CHAN_EXTENDED_WRITE,
152122
-	    "client-session", /*nonblock*/0);
152122
+	    "client-session", CHANNEL_NONBLOCK_STDIO);
152122
 
152122
 	debug3("%s: channel_new: %d", __func__, c->self);
152122