jonathancammack / rpms / openssh

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