jonathancammack / rpms / openssh

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