aedd00
diff -up openssh-7.4p1/channels.c.x11max openssh-7.4p1/channels.c
aedd00
--- openssh-7.4p1/channels.c.x11max	2016-12-23 15:46:32.071506625 +0100
aedd00
+++ openssh-7.4p1/channels.c	2016-12-23 15:46:32.139506636 +0100
aedd00
@@ -152,8 +152,8 @@ static int all_opens_permitted = 0;
aedd00
 #define FWD_PERMIT_ANY_HOST	"*"
aedd00
 
aedd00
 /* -- X11 forwarding */
aedd00
-/* Maximum number of fake X11 displays to try. */
aedd00
-#define MAX_DISPLAYS  1000
aedd00
+/* Minimum port number for X11 forwarding */
aedd00
+#define X11_PORT_MIN 6000
aedd00
 
aedd00
 /* Per-channel callback for pre/post select() actions */
aedd00
 typedef void chan_fn(struct ssh *, Channel *c,
aedd00
@@ -4228,7 +4228,7 @@ channel_send_window_changes(void)
aedd00
  */
aedd00
 int
aedd00
 x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
aedd00
-    int x11_use_localhost, int single_connection,
aedd00
+    int x11_use_localhost, int x11_max_displays, int single_connection,
aedd00
     u_int *display_numberp, int **chanids)
aedd00
 {
aedd00
 	Channel *nc = NULL;
aedd00
@@ -4240,10 +4241,15 @@ x11_create_display_inet(int x11_display_
aedd00
 	if (chanids == NULL)
aedd00
 		return -1;
aedd00
 
aedd00
+	/* Try to bind ports starting at 6000+X11DisplayOffset */
aedd00
+	x11_max_displays = x11_max_displays + x11_display_offset;
aedd00
+
aedd00
 	for (display_number = x11_display_offset;
aedd00
-	    display_number < MAX_DISPLAYS;
aedd00
+	    display_number < x11_max_displays;
aedd00
 	    display_number++) {
aedd00
-		port = 6000 + display_number;
aedd00
+		port = X11_PORT_MIN + display_number;
aedd00
+		if (port < X11_PORT_MIN) /* overflow */
aedd00
+			break;
aedd00
 		memset(&hints, 0, sizeof(hints));
aedd00
 		hints.ai_family = ssh->chanctxt->IPv4or6;
aedd00
 		hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
aedd00
@@ -4295,7 +4301,7 @@ x11_create_display_inet(int x11_display_
aedd00
 		if (num_socks > 0)
aedd00
 			break;
aedd00
 	}
aedd00
-	if (display_number >= MAX_DISPLAYS) {
aedd00
+	if (display_number >= x11_max_displays || port < X11_PORT_MIN ) {
aedd00
 		error("Failed to allocate internet-domain X11 display socket.");
aedd00
 		return -1;
aedd00
 	}
aedd00
@@ -4441,7 +4447,7 @@ x11_connect_display(void)
aedd00
 	memset(&hints, 0, sizeof(hints));
aedd00
 	hints.ai_family = ssh->chanctxt->IPv4or6;
aedd00
 	hints.ai_socktype = SOCK_STREAM;
aedd00
-	snprintf(strport, sizeof strport, "%u", 6000 + display_number);
aedd00
+	snprintf(strport, sizeof strport, "%u", X11_PORT_MIN + display_number);
aedd00
 	if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
aedd00
 		error("%.100s: unknown host. (%s)", buf,
aedd00
 		ssh_gai_strerror(gaierr));
aedd00
@@ -4457,7 +4463,7 @@ x11_connect_display(void)
aedd00
 		/* Connect it to the display. */
aedd00
 		if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
aedd00
 			debug2("connect %.100s port %u: %.100s", buf,
aedd00
-			    6000 + display_number, strerror(errno));
aedd00
+			    X11_PORT_MIN + display_number, strerror(errno));
aedd00
 			close(sock);
aedd00
 			continue;
aedd00
 		}
aedd00
@@ -4466,8 +4472,8 @@ x11_connect_display(void)
aedd00
 	}
aedd00
 	freeaddrinfo(aitop);
aedd00
 	if (!ai) {
aedd00
-		error("connect %.100s port %u: %.100s", buf,
aedd00
-		    6000 + display_number, strerror(errno));
aedd00
+		error("connect %.100s port %u: %.100s", buf,
aedd00
+		    X11_PORT_MIN + display_number, strerror(errno));
aedd00
 		return -1;
aedd00
 	}
aedd00
 	set_nodelay(sock);
aedd00
diff -up openssh-7.4p1/channels.h.x11max openssh-7.4p1/channels.h
aedd00
--- openssh-7.4p1/channels.h.x11max	2016-12-19 05:59:41.000000000 +0100
aedd00
+++ openssh-7.4p1/channels.h	2016-12-23 15:46:32.139506636 +0100
aedd00
@@ -293,7 +293,7 @@ int	 permitopen_port(const char *);
aedd00
 
aedd00
 void	 channel_set_x11_refuse_time(struct ssh *, u_int);
aedd00
 int	 x11_connect_display(struct ssh *);
aedd00
-int	 x11_create_display_inet(struct ssh *, int, int, int, u_int *, int **);
aedd00
+int	 x11_create_display_inet(struct ssh *, int, int, int, int, u_int *, int **);
aedd00
 void	 x11_request_forwarding_with_spoofing(struct ssh *, int,
aedd00
 	    const char *, const char *, const char *, int);
aedd00
 
aedd00
diff -up openssh-7.4p1/servconf.c.x11max openssh-7.4p1/servconf.c
aedd00
--- openssh-7.4p1/servconf.c.x11max	2016-12-23 15:46:32.133506635 +0100
aedd00
+++ openssh-7.4p1/servconf.c	2016-12-23 15:47:27.320519121 +0100
aedd00
@@ -95,6 +95,7 @@ initialize_server_options(ServerOptions
aedd00
 	options->print_lastlog = -1;
aedd00
 	options->x11_forwarding = -1;
aedd00
 	options->x11_display_offset = -1;
aedd00
+	options->x11_max_displays = -1;
aedd00
 	options->x11_use_localhost = -1;
aedd00
 	options->permit_tty = -1;
aedd00
 	options->permit_user_rc = -1;
aedd00
@@ -243,6 +244,8 @@ fill_default_server_options(ServerOption
aedd00
 		options->x11_forwarding = 0;
aedd00
 	if (options->x11_display_offset == -1)
aedd00
 		options->x11_display_offset = 10;
aedd00
+	if (options->x11_max_displays == -1)
aedd00
+		options->x11_max_displays = DEFAULT_MAX_DISPLAYS;
aedd00
 	if (options->x11_use_localhost == -1)
aedd00
 		options->x11_use_localhost = 1;
aedd00
 	if (options->xauth_location == NULL)
aedd00
@@ -419,7 +422,7 @@ typedef enum {
aedd00
 	sPasswordAuthentication, sKbdInteractiveAuthentication,
aedd00
 	sListenAddress, sAddressFamily,
aedd00
 	sPrintMotd, sPrintLastLog, sIgnoreRhosts,
aedd00
-	sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
aedd00
+	sX11Forwarding, sX11DisplayOffset, sX11MaxDisplays, sX11UseLocalhost,
aedd00
 	sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
aedd00
 	sPermitUserEnvironment, sAllowTcpForwarding, sCompression,
aedd00
 	sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
aedd00
@@ -540,6 +543,7 @@ static struct {
aedd00
 	{ "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
aedd00
 	{ "x11forwarding", sX11Forwarding, SSHCFG_ALL },
aedd00
 	{ "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
aedd00
+	{ "x11maxdisplays", sX11MaxDisplays, SSHCFG_ALL },
aedd00
 	{ "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
aedd00
 	{ "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
aedd00
 	{ "strictmodes", sStrictModes, SSHCFG_GLOBAL },
aedd00
@@ -1316,6 +1320,10 @@ process_server_config_line(ServerOptions
aedd00
 			*intptr = value;
aedd00
 		break;
aedd00
 
aedd00
+	case sX11MaxDisplays:
aedd00
+		intptr = &options->x11_max_displays;
aedd00
+		goto parse_int;
aedd00
+
aedd00
 	case sX11UseLocalhost:
aedd00
 		intptr = &options->x11_use_localhost;
aedd00
 		goto parse_flag;
aedd00
@@ -2063,6 +2071,7 @@ copy_set_server_options(ServerOptions *d
aedd00
 	M_CP_INTOPT(fwd_opts.streamlocal_bind_unlink);
aedd00
 	M_CP_INTOPT(x11_display_offset);
aedd00
 	M_CP_INTOPT(x11_forwarding);
aedd00
+	M_CP_INTOPT(x11_max_displays);
aedd00
 	M_CP_INTOPT(x11_use_localhost);
aedd00
 	M_CP_INTOPT(permit_tty);
aedd00
 	M_CP_INTOPT(permit_user_rc);
aedd00
@@ -2315,6 +2324,7 @@ dump_config(ServerOptions *o)
aedd00
 #endif
aedd00
 	dump_cfg_int(sLoginGraceTime, o->login_grace_time);
aedd00
 	dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
aedd00
+	dump_cfg_int(sX11MaxDisplays, o->x11_max_displays);
aedd00
 	dump_cfg_int(sMaxAuthTries, o->max_authtries);
aedd00
 	dump_cfg_int(sMaxSessions, o->max_sessions);
aedd00
 	dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
aedd00
diff -up openssh-7.4p1/servconf.h.x11max openssh-7.4p1/servconf.h
aedd00
--- openssh-7.4p1/servconf.h.x11max	2016-12-23 15:46:32.133506635 +0100
aedd00
+++ openssh-7.4p1/servconf.h	2016-12-23 15:46:32.140506636 +0100
aedd00
@@ -55,6 +55,7 @@
aedd00
 
aedd00
 #define DEFAULT_AUTH_FAIL_MAX	6	/* Default for MaxAuthTries */
aedd00
 #define DEFAULT_SESSIONS_MAX	10	/* Default for MaxSessions */
aedd00
+#define DEFAULT_MAX_DISPLAYS	1000 /* Maximum number of fake X11 displays to try. */
aedd00
 
aedd00
 /* Magic name for internal sftp-server */
aedd00
 #define INTERNAL_SFTP_NAME	"internal-sftp"
aedd00
@@ -85,6 +86,7 @@ typedef struct {
aedd00
 	int     x11_forwarding;	/* If true, permit inet (spoofing) X11 fwd. */
aedd00
 	int     x11_display_offset;	/* What DISPLAY number to start
aedd00
 					 * searching at */
aedd00
+	int 	x11_max_displays; /* Number of displays to search */
aedd00
 	int     x11_use_localhost;	/* If true, use localhost for fake X11 server. */
aedd00
 	char   *xauth_location;	/* Location of xauth program */
aedd00
 	int	permit_tty;	/* If false, deny pty allocation */
aedd00
diff -up openssh-7.4p1/session.c.x11max openssh-7.4p1/session.c
aedd00
--- openssh-7.4p1/session.c.x11max	2016-12-23 15:46:32.136506636 +0100
aedd00
+++ openssh-7.4p1/session.c	2016-12-23 15:46:32.141506636 +0100
aedd00
@@ -2518,8 +2518,9 @@ session_setup_x11fwd(Session *s)
aedd00
 		return 0;
aedd00
 	}
aedd00
	if (x11_create_display_inet(ssh, options.x11_display_offset,
aedd00
-	    options.x11_use_localhost, s->single_connection,
aedd00
-	    &s->display_number, &s->x11_chanids) == -1) {
aedd00
+	    options.x11_use_localhost, options.x11_max_displays,
aedd00
+	    s->single_connection, &s->display_number,
aedd00
+	    &s->x11_chanids) == -1) {
aedd00
 		debug("x11_create_display_inet failed.");
aedd00
 		return 0;
aedd00
 	}
aedd00
diff -up openssh-7.4p1/sshd_config.5.x11max openssh-7.4p1/sshd_config.5
aedd00
--- openssh-7.4p1/sshd_config.5.x11max	2016-12-23 15:46:32.134506635 +0100
aedd00
+++ openssh-7.4p1/sshd_config.5	2016-12-23 15:46:32.141506636 +0100
aedd00
@@ -1133,6 +1133,7 @@ Available keywords are
aedd00
 .Cm StreamLocalBindUnlink ,
aedd00
 .Cm TrustedUserCAKeys ,
aedd00
 .Cm X11DisplayOffset ,
aedd00
+.Cm X11MaxDisplays ,
aedd00
 .Cm X11Forwarding
aedd00
 and
aedd00
 .Cm X11UseLocalHost .
aedd00
@@ -1566,6 +1567,12 @@ Specifies the first display number avail
aedd00
 X11 forwarding.
aedd00
 This prevents sshd from interfering with real X11 servers.
aedd00
 The default is 10.
aedd00
+.It Cm X11MaxDisplays
aedd00
+Specifies the maximum number of displays available for
aedd00
+.Xr sshd 8 Ns 's
aedd00
+X11 forwarding.
aedd00
+This prevents sshd from exhausting local ports.
aedd00
+The default is 1000.
aedd00
 .It Cm X11Forwarding
aedd00
 Specifies whether X11 forwarding is permitted.
aedd00
 The argument must be