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

1c3003
diff -up openssh-8.0p1/channels.c.restore-nonblock openssh-8.0p1/channels.c
1c3003
--- openssh-8.0p1/channels.c.restore-nonblock	2021-04-26 11:31:44.037740711 +0200
1c3003
+++ openssh-8.0p1/channels.c	2021-04-26 11:43:48.429606396 +0200
1c3003
@@ -298,32 +298,38 @@ channel_lookup(struct ssh *ssh, int id)
1c3003
 }
1c3003
 
1c3003
 /*
1c3003
- * Register filedescriptors for a channel, used when allocating a channel or
1c3003
- * when the channel consumer/producer is ready, e.g. shell exec'd
1c3003
+ * Register a filedescriptor.
1c3003
  */
1c3003
 static void
1c3003
-channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd,
1c3003
-    int extusage, int nonblock, int is_tty)
1c3003
+channel_register_fd(struct ssh *ssh, int fd, int nonblock)
1c3003
 {
1c3003
 	struct ssh_channels *sc = ssh->chanctxt;
1c3003
 
1c3003
 	/* Update the maximum file descriptor value. */
1c3003
-	sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, rfd);
1c3003
-	sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, wfd);
1c3003
-	sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, efd);
1c3003
-
1c3003
-	if (rfd != -1)
1c3003
-		fcntl(rfd, F_SETFD, FD_CLOEXEC);
1c3003
-	if (wfd != -1 && wfd != rfd)
1c3003
-		fcntl(wfd, F_SETFD, FD_CLOEXEC);
1c3003
-	if (efd != -1 && efd != rfd && efd != wfd)
1c3003
-		fcntl(efd, F_SETFD, FD_CLOEXEC);
1c3003
+	sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, fd);
1c3003
+
1c3003
+	if (fd != -1)
1c3003
+		fcntl(fd, F_SETFD, FD_CLOEXEC);
1c3003
 
1c3003
+	/* enable nonblocking mode */
1c3003
+	if (nonblock && fd != -1 && !isatty(fd))
1c3003
+		set_nonblock(fd);
1c3003
+}
1c3003
+
1c3003
+/*
1c3003
+ * Register filedescriptors for a channel, used when allocating a channel or
1c3003
+ * when the channel consumer/producer is ready, e.g. shell exec'd
1c3003
+ */
1c3003
+static void
1c3003
+channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd,
1c3003
+    int extusage, int nonblock, int is_tty)
1c3003
+{
1c3003
 	c->rfd = rfd;
1c3003
 	c->wfd = wfd;
1c3003
 	c->sock = (rfd == wfd) ? rfd : -1;
1c3003
 	c->efd = efd;
1c3003
 	c->extended_usage = extusage;
1c3003
+	c->nonblock = 0;
1c3003
 
1c3003
 	if ((c->isatty = is_tty) != 0)
1c3003
 		debug2("channel %d: rfd %d isatty", c->self, c->rfd);
1c3003
@@ -332,14 +338,20 @@ channel_register_fds(struct ssh *ssh, Ch
1c3003
 	c->wfd_isatty = is_tty || isatty(c->wfd);
1c3003
 #endif
1c3003
 
1c3003
-	/* enable nonblocking mode */
1c3003
-	if (nonblock) {
1c3003
-		if (rfd != -1)
1c3003
-			set_nonblock(rfd);
1c3003
-		if (wfd != -1)
1c3003
-			set_nonblock(wfd);
1c3003
-		if (efd != -1)
1c3003
-			set_nonblock(efd);
1c3003
+	if (rfd != -1) {
1c3003
+		if ((fcntl(rfd, F_GETFL) & O_NONBLOCK) == 0)
1c3003
+			c->nonblock |= NEED_RESTORE_STDIN_NONBLOCK;
1c3003
+		channel_register_fd(ssh, rfd, nonblock);
1c3003
+	}
1c3003
+	if (wfd != -1 && wfd != rfd) {
1c3003
+		if ((fcntl(wfd, F_GETFL) & O_NONBLOCK) == 0)
1c3003
+			c->nonblock |= NEED_RESTORE_STDOUT_NONBLOCK;
1c3003
+		channel_register_fd(ssh, wfd, nonblock);
1c3003
+	}
1c3003
+	if (efd != -1 && efd != rfd && efd != wfd) {
1c3003
+		if ((fcntl(efd, F_GETFL) & O_NONBLOCK) == 0)
1c3003
+			c->nonblock |= NEED_RESTORE_STDERR_NONBLOCK;
1c3003
+		channel_register_fd(ssh, efd, nonblock);
1c3003
 	}
1c3003
 }
1c3003
 
1c3003
@@ -422,11 +434,15 @@ channel_find_maxfd(struct ssh_channels *
1c3003
 }
1c3003
 
1c3003
 int
1c3003
-channel_close_fd(struct ssh *ssh, int *fdp)
1c3003
+channel_close_fd(struct ssh *ssh, int *fdp, int nonblock)
1c3003
 {
1c3003
 	struct ssh_channels *sc = ssh->chanctxt;
1c3003
 	int ret = 0, fd = *fdp;
1c3003
 
1c3003
+	/* As the fd is duped, restoring the block mode
1c3003
+	 * affects the original fd */
1c3003
+	if (nonblock && fd != -1 && !isatty(fd))
1c3003
+		unset_nonblock(fd);
1c3003
 	if (fd != -1) {
1c3003
 		ret = close(fd);
1c3003
 		*fdp = -1;
1c3003
@@ -442,13 +458,13 @@ channel_close_fds(struct ssh *ssh, Chann
1c3003
 {
1c3003
 	int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd;
1c3003
 
1c3003
-	channel_close_fd(ssh, &c->sock);
1c3003
+	channel_close_fd(ssh, &c->sock, 0);
1c3003
 	if (rfd != sock)
1c3003
-		channel_close_fd(ssh, &c->rfd);
1c3003
+		channel_close_fd(ssh, &c->rfd, c->nonblock & NEED_RESTORE_STDIN_NONBLOCK);
1c3003
 	if (wfd != sock && wfd != rfd)
1c3003
-		channel_close_fd(ssh, &c->wfd);
1c3003
+		channel_close_fd(ssh, &c->wfd, c->nonblock & NEED_RESTORE_STDOUT_NONBLOCK);
1c3003
 	if (efd != sock && efd != rfd && efd != wfd)
1c3003
-		channel_close_fd(ssh, &c->efd);
1c3003
+		channel_close_fd(ssh, &c->efd, c->nonblock & NEED_RESTORE_STDERR_NONBLOCK);
1c3003
 }
1c3003
 
1c3003
 static void
1c3003
@@ -681,7 +697,7 @@ channel_stop_listening(struct ssh *ssh)
1c3003
 			case SSH_CHANNEL_X11_LISTENER:
1c3003
 			case SSH_CHANNEL_UNIX_LISTENER:
1c3003
 			case SSH_CHANNEL_RUNIX_LISTENER:
1c3003
-				channel_close_fd(ssh, &c->sock);
1c3003
+				channel_close_fd(ssh, &c->sock, 0);
1c3003
 				channel_free(ssh, c);
1c3003
 				break;
1c3003
 			}
1c3003
@@ -1650,7 +1666,7 @@ channel_post_x11_listener(struct ssh *ss
1c3003
 	if (c->single_connection) {
1c3003
 		oerrno = errno;
1c3003
 		debug2("single_connection: closing X11 listener.");
1c3003
-		channel_close_fd(ssh, &c->sock);
1c3003
+		channel_close_fd(ssh, &c->sock, 0);
1c3003
 		chan_mark_dead(ssh, c);
1c3003
 		errno = oerrno;
1c3003
 	}
1c3003
@@ -2087,7 +2103,7 @@ channel_handle_efd_write(struct ssh *ssh
1c3003
 		return 1;
1c3003
 	if (len <= 0) {
1c3003
 		debug2("channel %d: closing write-efd %d", c->self, c->efd);
1c3003
-		channel_close_fd(ssh, &c->efd);
1c3003
+		channel_close_fd(ssh, &c->efd, c->nonblock & NEED_RESTORE_STDERR_NONBLOCK);
1c3003
 	} else {
1c3003
 		if ((r = sshbuf_consume(c->extended, len)) != 0) {
1c3003
 			fatal("%s: channel %d: consume: %s",
1c3003
@@ -2119,7 +2135,7 @@ channel_handle_efd_read(struct ssh *ssh,
1c3003
 	if (len <= 0) {
1c3003
 		debug2("channel %d: closing read-efd %d",
1c3003
 		    c->self, c->efd);
1c3003
-		channel_close_fd(ssh, &c->efd);
1c3003
+		channel_close_fd(ssh, &c->efd, c->nonblock & NEED_RESTORE_STDERR_NONBLOCK);
1c3003
 	} else {
1c3003
 		if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1c3003
 			debug3("channel %d: discard efd",
1c3003
diff -up openssh-8.0p1/channels.h.restore-nonblock openssh-8.0p1/channels.h
1c3003
--- openssh-8.0p1/channels.h.restore-nonblock	2021-04-26 11:31:44.038740719 +0200
1c3003
+++ openssh-8.0p1/channels.h	2021-04-26 11:38:18.151932008 +0200
1c3003
@@ -180,8 +180,15 @@ struct Channel {
1c3003
 	void			*mux_ctx;
1c3003
 	int			mux_pause;
1c3003
 	int     		mux_downstream_id;
1c3003
+
1c3003
+	/* whether non-blocking is set to descriptors */
1c3003
+	int 			nonblock;
1c3003
 };
1c3003
 
1c3003
+#define NEED_RESTORE_STDIN_NONBLOCK  1
1c3003
+#define NEED_RESTORE_STDOUT_NONBLOCK 2
1c3003
+#define NEED_RESTORE_STDERR_NONBLOCK 4
1c3003
+
1c3003
 #define CHAN_EXTENDED_IGNORE		0
1c3003
 #define CHAN_EXTENDED_READ		1
1c3003
 #define CHAN_EXTENDED_WRITE		2
1c3003
@@ -258,7 +265,7 @@ void	 channel_register_filter(struct ssh
1c3003
 void	 channel_register_status_confirm(struct ssh *, int,
1c3003
 	    channel_confirm_cb *, channel_confirm_abandon_cb *, void *);
1c3003
 void	 channel_cancel_cleanup(struct ssh *, int);
1c3003
-int	 channel_close_fd(struct ssh *, int *);
1c3003
+int	 channel_close_fd(struct ssh *, int *, int);
1c3003
 void	 channel_send_window_changes(struct ssh *);
1c3003
 
1c3003
 /* mux proxy support */
1c3003
diff -up openssh-8.0p1/nchan.c.restore-nonblock openssh-8.0p1/nchan.c
1c3003
--- openssh-8.0p1/nchan.c.restore-nonblock	2021-04-26 11:31:44.047740792 +0200
1c3003
+++ openssh-8.0p1/nchan.c	2021-04-26 11:42:33.636000753 +0200
1c3003
@@ -387,7 +387,7 @@ chan_shutdown_write(struct ssh *ssh, Cha
1c3003
 			    strerror(errno));
1c3003
 		}
1c3003
 	} else {
1c3003
-		if (channel_close_fd(ssh, &c->wfd) < 0) {
1c3003
+		if (channel_close_fd(ssh, &c->wfd, c->nonblock & NEED_RESTORE_STDOUT_NONBLOCK) < 0) {
1c3003
 			logit("channel %d: %s: close() failed for "
1c3003
 			    "fd %d [i%d o%d]: %.100s",
1c3003
 			    c->self, __func__, c->wfd, c->istate, c->ostate,
1c3003
@@ -417,7 +417,7 @@ chan_shutdown_read(struct ssh *ssh, Chan
1c3003
  			    strerror(errno));
1c3003
 		}
1c3003
 	} else {
1c3003
-		if (channel_close_fd(ssh, &c->rfd) < 0) {
1c3003
+		if (channel_close_fd(ssh, &c->rfd, c->nonblock & NEED_RESTORE_STDIN_NONBLOCK) < 0) {
1c3003
 			logit("channel %d: %s: close() failed for "
1c3003
 			    "fd %d [i%d o%d]: %.100s",
1c3003
 			    c->self, __func__, c->rfd, c->istate, c->ostate,
1c3003
@@ -437,7 +437,7 @@ chan_shutdown_extended_read(struct ssh *
1c3003
 	debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
1c3003
 	    c->self, __func__, c->istate, c->ostate, c->sock, c->rfd, c->efd,
1c3003
 	    channel_format_extended_usage(c));
1c3003
-	if (channel_close_fd(ssh, &c->efd) < 0) {
1c3003
+	if (channel_close_fd(ssh, &c->efd, c->nonblock & NEED_RESTORE_STDERR_NONBLOCK) < 0) {
1c3003
 		logit("channel %d: %s: close() failed for "
1c3003
 		    "extended fd %d [i%d o%d]: %.100s",
1c3003
 		    c->self, __func__, c->efd, c->istate, c->ostate,
1c3003
diff -up openssh-8.0p1/ssh.c.restore-nonblock openssh-8.0p1/ssh.c
1c3003
--- openssh-8.0p1/ssh.c.restore-nonblock	2021-04-26 11:31:44.047740792 +0200
1c3003
+++ openssh-8.0p1/ssh.c	2021-04-26 11:39:58.081741180 +0200
1c3003
@@ -1862,14 +1862,6 @@ ssh_session2_open(struct ssh *ssh)
1c3003
 	if (in < 0 || out < 0 || err < 0)
1c3003
 		fatal("dup() in/out/err failed");
1c3003
 
1c3003
-	/* enable nonblocking unless tty */
1c3003
-	if (!isatty(in))
1c3003
-		set_nonblock(in);
1c3003
-	if (!isatty(out))
1c3003
-		set_nonblock(out);
1c3003
-	if (!isatty(err))
1c3003
-		set_nonblock(err);
1c3003
-
1c3003
 	window = CHAN_SES_WINDOW_DEFAULT;
1c3003
 	packetmax = CHAN_SES_PACKET_DEFAULT;
1c3003
 	if (tty_flag) {
1c3003
@@ -1879,7 +1871,7 @@ ssh_session2_open(struct ssh *ssh)
1c3003
 	c = channel_new(ssh,
1c3003
 	    "session", SSH_CHANNEL_OPENING, in, out, err,
1c3003
 	    window, packetmax, CHAN_EXTENDED_WRITE,
1c3003
-	    "client-session", /*nonblock*/0);
1c3003
+	    "client-session", /*nonblock*/1);
1c3003
 
1c3003
 	debug3("%s: channel_new: %d", __func__, c->self);
1c3003