kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
b58e57
diff -up openssh-7.4p1/channels.c.x11max openssh-7.4p1/channels.c
b58e57
--- openssh-7.4p1/channels.c.x11max	2017-02-09 12:49:04.690996627 +0100
b58e57
+++ openssh-7.4p1/channels.c	2017-02-09 12:49:04.744996547 +0100
b58e57
@@ -152,8 +152,8 @@ static int all_opens_permitted = 0;
b58e57
 
b58e57
 /* -- X11 forwarding */
b58e57
 
b58e57
-/* Maximum number of fake X11 displays to try. */
b58e57
-#define MAX_DISPLAYS  1000
b58e57
+/* Minimum port number for X11 forwarding */
b58e57
+#define X11_PORT_MIN 6000
b58e57
 
b58e57
 /* Saved X11 local (client) display. */
b58e57
 static char *x11_saved_display = NULL;
b58e57
@@ -4228,7 +4228,8 @@ channel_send_window_changes(void)
b58e57
  */
b58e57
 int
b58e57
 x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
b58e57
-    int single_connection, u_int *display_numberp, int **chanids)
b58e57
+    int x11_max_displays, int single_connection, u_int *display_numberp,
b58e57
+    int **chanids)
b58e57
 {
b58e57
 	Channel *nc = NULL;
b58e57
 	int display_number, sock;
b58e57
@@ -4240,10 +4241,15 @@ x11_create_display_inet(int x11_display_
b58e57
 	if (chanids == NULL)
b58e57
 		return -1;
b58e57
 
b58e57
+	/* Try to bind ports starting at 6000+X11DisplayOffset */
b58e57
+	x11_max_displays = x11_max_displays + x11_display_offset;
b58e57
+
b58e57
 	for (display_number = x11_display_offset;
b58e57
-	    display_number < MAX_DISPLAYS;
b58e57
+	    display_number < x11_max_displays;
b58e57
 	    display_number++) {
b58e57
-		port = 6000 + display_number;
b58e57
+		port = X11_PORT_MIN + display_number;
b58e57
+		if (port < X11_PORT_MIN) /* overflow */
b58e57
+			break;
b58e57
 		memset(&hints, 0, sizeof(hints));
b58e57
 		hints.ai_family = IPv4or6;
b58e57
 		hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
b58e57
@@ -4295,7 +4301,7 @@ x11_create_display_inet(int x11_display_
b58e57
 		if (num_socks > 0)
b58e57
 			break;
b58e57
 	}
b58e57
-	if (display_number >= MAX_DISPLAYS) {
b58e57
+	if (display_number >= x11_max_displays || port < X11_PORT_MIN ) {
b58e57
 		error("Failed to allocate internet-domain X11 display socket.");
b58e57
 		return -1;
b58e57
 	}
b58e57
@@ -4441,7 +4447,7 @@ x11_connect_display(void)
b58e57
 	memset(&hints, 0, sizeof(hints));
b58e57
 	hints.ai_family = IPv4or6;
b58e57
 	hints.ai_socktype = SOCK_STREAM;
b58e57
-	snprintf(strport, sizeof strport, "%u", 6000 + display_number);
b58e57
+	snprintf(strport, sizeof strport, "%u", X11_PORT_MIN + display_number);
b58e57
 	if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
b58e57
 		error("%.100s: unknown host. (%s)", buf,
b58e57
 		ssh_gai_strerror(gaierr));
b58e57
@@ -4457,7 +4463,7 @@ x11_connect_display(void)
b58e57
 		/* Connect it to the display. */
b58e57
 		if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
b58e57
 			debug2("connect %.100s port %u: %.100s", buf,
b58e57
-			    6000 + display_number, strerror(errno));
b58e57
+			    X11_PORT_MIN + display_number, strerror(errno));
b58e57
 			close(sock);
b58e57
 			continue;
b58e57
 		}
b58e57
@@ -4466,8 +4472,8 @@ x11_connect_display(void)
b58e57
 	}
b58e57
 	freeaddrinfo(aitop);
b58e57
 	if (!ai) {
b58e57
-		error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
b58e57
-		    strerror(errno));
b58e57
+		error("connect %.100s port %u: %.100s", buf,
b58e57
+		    X11_PORT_MIN + display_number, strerror(errno));
b58e57
 		return -1;
b58e57
 	}
b58e57
 	set_nodelay(sock);
b58e57
diff -up openssh-7.4p1/channels.h.x11max openssh-7.4p1/channels.h
b58e57
--- openssh-7.4p1/channels.h.x11max	2017-02-09 12:49:04.744996547 +0100
b58e57
+++ openssh-7.4p1/channels.h	2017-02-09 12:49:50.230929693 +0100
b58e57
@@ -293,7 +293,7 @@ int	 permitopen_port(const char *);
b58e57
 
b58e57
 void	 channel_set_x11_refuse_time(u_int);
b58e57
 int	 x11_connect_display(void);
b58e57
-int	 x11_create_display_inet(int, int, int, u_int *, int **);
b58e57
+int	 x11_create_display_inet(int, int, int, int, u_int *, int **);
b58e57
 int      x11_input_open(int, u_int32_t, void *);
b58e57
 void	 x11_request_forwarding_with_spoofing(int, const char *, const char *,
b58e57
 	     const char *, int);
b58e57
diff -up openssh-7.4p1/servconf.c.x11max openssh-7.4p1/servconf.c
b58e57
--- openssh-7.4p1/servconf.c.x11max	2017-02-09 12:49:04.741996552 +0100
b58e57
+++ openssh-7.4p1/servconf.c	2017-02-09 12:51:03.167822492 +0100
b58e57
@@ -94,6 +94,7 @@ initialize_server_options(ServerOptions
b58e57
 	options->print_lastlog = -1;
b58e57
 	options->x11_forwarding = -1;
b58e57
 	options->x11_display_offset = -1;
b58e57
+	options->x11_max_displays = -1;
b58e57
 	options->x11_use_localhost = -1;
b58e57
 	options->permit_tty = -1;
b58e57
 	options->permit_user_rc = -1;
b58e57
@@ -242,6 +243,8 @@ fill_default_server_options(ServerOption
b58e57
 		options->x11_forwarding = 0;
b58e57
 	if (options->x11_display_offset == -1)
b58e57
 		options->x11_display_offset = 10;
b58e57
+	if (options->x11_max_displays == -1)
b58e57
+		options->x11_max_displays = DEFAULT_MAX_DISPLAYS;
b58e57
 	if (options->x11_use_localhost == -1)
b58e57
 		options->x11_use_localhost = 1;
b58e57
 	if (options->xauth_location == NULL)
b58e57
@@ -416,7 +419,7 @@ typedef enum {
b58e57
 	sPasswordAuthentication, sKbdInteractiveAuthentication,
b58e57
 	sListenAddress, sAddressFamily,
b58e57
 	sPrintMotd, sPrintLastLog, sIgnoreRhosts,
b58e57
-	sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
b58e57
+	sX11Forwarding, sX11DisplayOffset, sX11MaxDisplays, sX11UseLocalhost,
b58e57
 	sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
b58e57
 	sPermitUserEnvironment, sAllowTcpForwarding, sCompression,
b58e57
 	sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
b58e57
@@ -537,6 +540,7 @@ static struct {
b58e57
 	{ "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
b58e57
 	{ "x11forwarding", sX11Forwarding, SSHCFG_ALL },
b58e57
 	{ "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
b58e57
+	{ "x11maxdisplays", sX11MaxDisplays, SSHCFG_ALL },
b58e57
 	{ "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
b58e57
 	{ "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
b58e57
 	{ "strictmodes", sStrictModes, SSHCFG_GLOBAL },
b58e57
@@ -1313,6 +1317,10 @@ process_server_config_line(ServerOptions
b58e57
 			*intptr = value;
b58e57
 		break;
b58e57
 
b58e57
+	case sX11MaxDisplays:
b58e57
+		intptr = &options->x11_max_displays;
b58e57
+		goto parse_int;
b58e57
+
b58e57
 	case sX11UseLocalhost:
b58e57
 		intptr = &options->x11_use_localhost;
b58e57
 		goto parse_flag;
b58e57
@@ -2060,6 +2068,7 @@ copy_set_server_options(ServerOptions *d
b58e57
 	M_CP_INTOPT(fwd_opts.streamlocal_bind_unlink);
b58e57
 	M_CP_INTOPT(x11_display_offset);
b58e57
 	M_CP_INTOPT(x11_forwarding);
b58e57
+	M_CP_INTOPT(x11_max_displays);
b58e57
 	M_CP_INTOPT(x11_use_localhost);
b58e57
 	M_CP_INTOPT(permit_tty);
b58e57
 	M_CP_INTOPT(permit_user_rc);
b58e57
@@ -2312,6 +2321,7 @@ dump_config(ServerOptions *o)
b58e57
 #endif
b58e57
 	dump_cfg_int(sLoginGraceTime, o->login_grace_time);
b58e57
 	dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
b58e57
+	dump_cfg_int(sX11MaxDisplays, o->x11_max_displays);
b58e57
 	dump_cfg_int(sMaxAuthTries, o->max_authtries);
b58e57
 	dump_cfg_int(sMaxSessions, o->max_sessions);
b58e57
 	dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
b58e57
diff -up openssh-7.4p1/servconf.h.x11max openssh-7.4p1/servconf.h
b58e57
--- openssh-7.4p1/servconf.h.x11max	2017-02-09 12:49:04.741996552 +0100
b58e57
+++ openssh-7.4p1/servconf.h	2017-02-09 12:49:04.744996547 +0100
b58e57
@@ -55,6 +55,7 @@
b58e57
 
b58e57
 #define DEFAULT_AUTH_FAIL_MAX	6	/* Default for MaxAuthTries */
b58e57
 #define DEFAULT_SESSIONS_MAX	10	/* Default for MaxSessions */
b58e57
+#define DEFAULT_MAX_DISPLAYS	1000 /* Maximum number of fake X11 displays to try. */
b58e57
 
b58e57
 /* Magic name for internal sftp-server */
b58e57
 #define INTERNAL_SFTP_NAME	"internal-sftp"
b58e57
@@ -85,6 +86,7 @@ typedef struct {
b58e57
 	int     x11_forwarding;	/* If true, permit inet (spoofing) X11 fwd. */
b58e57
 	int     x11_display_offset;	/* What DISPLAY number to start
b58e57
 					 * searching at */
b58e57
+	int 	x11_max_displays; /* Number of displays to search */
b58e57
 	int     x11_use_localhost;	/* If true, use localhost for fake X11 server. */
b58e57
 	char   *xauth_location;	/* Location of xauth program */
b58e57
 	int	permit_tty;	/* If false, deny pty allocation */
b58e57
diff -up openssh-7.4p1/session.c.x11max openssh-7.4p1/session.c
b58e57
--- openssh-7.4p1/session.c.x11max	2017-02-09 12:49:04.742996550 +0100
b58e57
+++ openssh-7.4p1/session.c	2017-02-09 12:49:04.745996546 +0100
b58e57
@@ -2502,8 +2502,9 @@ session_setup_x11fwd(Session *s)
b58e57
 		return 0;
b58e57
 	}
b58e57
 	if (x11_create_display_inet(options.x11_display_offset,
b58e57
-	    options.x11_use_localhost, s->single_connection,
b58e57
-	    &s->display_number, &s->x11_chanids) == -1) {
b58e57
+	    options.x11_use_localhost, options.x11_max_displays,
b58e57
+	    s->single_connection, &s->display_number,
b58e57
+	    &s->x11_chanids) == -1) {
b58e57
 		debug("x11_create_display_inet failed.");
b58e57
 		return 0;
b58e57
 	}
b58e57
diff -up openssh-7.4p1/sshd_config.5.x11max openssh-7.4p1/sshd_config.5
b58e57
--- openssh-7.4p1/sshd_config.5.x11max	2017-02-09 12:49:04.742996550 +0100
b58e57
+++ openssh-7.4p1/sshd_config.5	2017-02-09 12:51:24.656790909 +0100
b58e57
@@ -1137,6 +1137,7 @@ Available keywords are
b58e57
 .Cm StreamLocalBindUnlink ,
b58e57
 .Cm TrustedUserCAKeys ,
b58e57
 .Cm X11DisplayOffset ,
b58e57
+.Cm X11MaxDisplays ,
b58e57
 .Cm X11Forwarding
b58e57
 and
b58e57
 .Cm X11UseLocalHost .
b58e57
@@ -1563,6 +1564,12 @@ Specifies the first display number avail
b58e57
 X11 forwarding.
b58e57
 This prevents sshd from interfering with real X11 servers.
b58e57
 The default is 10.
b58e57
+.It Cm X11MaxDisplays
b58e57
+Specifies the maximum number of displays available for
b58e57
+.Xr sshd 8 Ns 's
b58e57
+X11 forwarding.
b58e57
+This prevents sshd from exhausting local ports.
b58e57
+The default is 1000.
b58e57
 .It Cm X11Forwarding
b58e57
 Specifies whether X11 forwarding is permitted.
b58e57
 The argument must be