vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Dmitry Belyavskiy f23830
diff --git a/channels.c b/channels.c
Dmitry Belyavskiy f23830
--- a/channels.c	(revision 8241b9c0529228b4b86d88b1a6076fb9f97e4a99)
Dmitry Belyavskiy f23830
+++ b/channels.c	(date 1703026069921)
Dmitry Belyavskiy f23830
@@ -5075,11 +5075,13 @@
Jan F. Chadima 2664a6
 }
Dmitry Belyavskiy f23830
Jan F. Chadima 2664a6
 static int
Jan F. Chadima 2664a6
-connect_local_xsocket_path(const char *pathname)
Jan F. Chadima 2664a6
+connect_local_xsocket_path(const char *pathname, int len)
Jan F. Chadima 2664a6
 {
Jan F. Chadima 2664a6
 	int sock;
Jan F. Chadima 2664a6
 	struct sockaddr_un addr;
Dmitry Belyavskiy f23830
Dmitry Belyavskiy 089d79
+	if (len <= 0)
Dmitry Belyavskiy 089d79
+		return -1;
Jakub Jelen f28682
 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
Dmitry Belyavskiy f23830
 	if (sock == -1) {
Jan F. Chadima 2664a6
 		error("socket: %.100s", strerror(errno));
Dmitry Belyavskiy f23830
@@ -5087,11 +5089,12 @@
Dmitry Belyavskiy f23830
 	}
Jan F. Chadima 2664a6
 	memset(&addr, 0, sizeof(addr));
Jan F. Chadima 2664a6
 	addr.sun_family = AF_UNIX;
Jan F. Chadima 2664a6
-	strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Jan F 5daee1
-	if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Jan F. Chadima 2664a6
+	if (len > sizeof addr.sun_path)
Jan F. Chadima 2664a6
+		len = sizeof addr.sun_path;
Jan F 5daee1
+	memcpy(addr.sun_path, pathname, len);
Jan F 5daee1
+	if (connect(sock, (struct sockaddr *)&addr, sizeof addr - (sizeof addr.sun_path - len) ) == 0)
Jan F. Chadima 2664a6
 		return sock;
Jan F. Chadima 2664a6
 	close(sock);
Jan F 5daee1
-	error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
Jan F 5daee1
 	return -1;
Jan F 5daee1
 }
Dmitry Belyavskiy f23830
Dmitry Belyavskiy f23830
@@ -5099,8 +5102,18 @@
Jan F. Chadima a3dee6
 connect_local_xsocket(u_int dnr)
Jan F. Chadima a3dee6
 {
Jan F. Chadima a3dee6
 	char buf[1024];
Jan F. Chadima 2664a6
-	snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
Jan F. Chadima 2664a6
-	return connect_local_xsocket_path(buf);
Petr Lautrbach 86f29c
+	int len, ret;
Jan F. Chadima 2664a6
+	len = snprintf(buf + 1, sizeof (buf) - 1, _PATH_UNIX_X, dnr);
Jan F. Chadima a3dee6
+#ifdef linux
Jan F. Chadima a3dee6
+	/* try abstract socket first */
Jan F. Chadima a3dee6
+	buf[0] = '\0';
Jan F. Chadima 2664a6
+	if ((ret = connect_local_xsocket_path(buf, len + 1)) >= 0)
Jan F. Chadima a3dee6
+		return ret;
Jan F. Chadima a3dee6
+#endif
Jan F 5daee1
+	if ((ret = connect_local_xsocket_path(buf + 1, len)) >= 0)
Jan F 5daee1
+		return ret;
Jan F 5daee1
+	error("connect %.100s: %.100s", buf + 1, strerror(errno));
Jan F 5daee1
+	return -1;
Jan F. Chadima a3dee6
 }
Jan F. Chadima 41a56c
 
Jakub Jelen 17b491
 #ifdef __APPLE__