jonathancammack / rpms / openssh

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