kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
943807
diff -up openssh-7.2p2/channels.c.x11 openssh-7.2p2/channels.c
943807
--- openssh-7.2p2/channels.c.x11	2016-03-09 19:04:48.000000000 +0100
943807
+++ openssh-7.2p2/channels.c	2016-06-03 10:42:04.775164520 +0200
943807
@@ -3990,21 +3990,24 @@ x11_create_display_inet(int x11_display_
943807
 }
943807
 
943807
 static int
943807
-connect_local_xsocket_path(const char *pathname)
943807
+connect_local_xsocket_path(const char *pathname, int len)
943807
 {
943807
 	int sock;
943807
 	struct sockaddr_un addr;
943807
 
943807
+	if (len <= 0)
943807
+		return -1;
943807
 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
943807
 	if (sock == -1)
943807
 		error("socket: %.100s", strerror(errno));
943807
 	memset(&addr, 0, sizeof(addr));
943807
 	addr.sun_family = AF_UNIX;
943807
-	strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
943807
-	if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
943807
+	if (len > sizeof addr.sun_path)
943807
+		len = sizeof addr.sun_path;
943807
+	memcpy(addr.sun_path, pathname, len);
943807
+	if (connect(sock, (struct sockaddr *)&addr, sizeof addr - (sizeof addr.sun_path - len) ) == 0)
943807
 		return sock;
943807
 	close(sock);
943807
-	error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
943807
 	return -1;
943807
 }
943807
 
943807
@@ -4012,8 +4015,18 @@ static int
943807
 connect_local_xsocket(u_int dnr)
943807
 {
943807
 	char buf[1024];
943807
-	snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
943807
-	return connect_local_xsocket_path(buf);
943807
+	int len, ret;
943807
+	len = snprintf(buf + 1, sizeof (buf) - 1, _PATH_UNIX_X, dnr);
943807
+#ifdef linux
943807
+	/* try abstract socket first */
943807
+	buf[0] = '\0';
943807
+	if ((ret = connect_local_xsocket_path(buf, len + 1)) >= 0)
943807
+		return ret;
943807
+#endif
943807
+	if ((ret = connect_local_xsocket_path(buf + 1, len)) >= 0)
943807
+		return ret;
943807
+	error("connect %.100s: %.100s", buf + 1, strerror(errno));
943807
+	return -1;
943807
 }
943807
 
943807
 #ifdef __APPLE__