[7.3] 1a3cf99f9 fix bug in abstract sockets

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Oct 25 14:33:08 UTC 2023


commit 1a3cf99f9fb7fb2d64e7cbd9f4d102ad59aa1674
Author: Walid Boudebouda <walid.boudebouda at gmail.com>
Date:   Thu Mar 16 18:32:35 2023 +0100

    fix bug in abstract sockets
    
    with uds abstract sockets, sun_path should start with a NULL character followed
    by the socket's name. The name is not considered to be NULL terminated and can
    contain NULL bytes which have no special meaning. socklen is used to determine
    the length of name and must be set to the length of the struct sockaddr_un up to
    the last character of name, otherwise the 108 characters of sun_path will be
    treated as the name of the socket, including NULL bytes.

diff --git a/bin/varnishtest/vtc_client.c b/bin/varnishtest/vtc_client.c
index fb7277d5c..8fb3bdd3e 100644
--- a/bin/varnishtest/vtc_client.c
+++ b/bin/varnishtest/vtc_client.c
@@ -127,7 +127,9 @@ uds_open(void *priv, const struct sockaddr_un *uds)
 	double *p;
 	int s, i, tmo;
 	struct pollfd fds[1];
-	socklen_t sl = sizeof(*uds);
+	socklen_t sl;
+
+	sl = VUS_socklen(uds);
 
 	AN(priv);
 	AN(uds);
diff --git a/include/vus.h b/include/vus.h
index 20d56fd15..c818f3939 100644
--- a/include/vus.h
+++ b/include/vus.h
@@ -36,6 +36,7 @@ int VUS_resolver(const char *path, vus_resolved_f *func, void *priv,
 		 const char **err);
 int VUS_bind(const struct sockaddr_un *uds, const char **errp);
 int VUS_connect(const char *path, int msec);
+unsigned int VUS_socklen(const struct sockaddr_un *uds);
 
 static inline int
 VUS_is(const char *path)
diff --git a/lib/libvarnish/vus.c b/lib/libvarnish/vus.c
index c9920ef28..a110a1ec1 100644
--- a/lib/libvarnish/vus.c
+++ b/lib/libvarnish/vus.c
@@ -86,6 +86,8 @@ VUS_resolver(const char *path, vus_resolved_f *func, void *priv,
 	if (ret)
 		return (ret);
 
+	assert(uds.sun_path[1] != '\0');
+
 	if (func != NULL)
 		ret = func(priv, &uds);
 	return (ret);
@@ -95,7 +97,9 @@ int
 VUS_bind(const struct sockaddr_un *uds, const char **errp)
 {
 	int sd, e;
-	socklen_t sl = sizeof(*uds);
+	socklen_t sl;
+
+	sl = VUS_socklen(uds);
 
 	if (errp != NULL)
 		*errp = NULL;
@@ -133,13 +137,18 @@ VUS_connect(const char *path, int msec)
 	int s, i;
 	struct pollfd fds[1];
 	struct sockaddr_un uds;
-	socklen_t sl = (socklen_t) sizeof(uds);
+	socklen_t sl;
 
 	if (path == NULL)
 		return (-1);
 	i = sun_init(&uds, path, NULL);
 	if (i)
 		return (i);
+
+	assert(uds.sun_path[1] != '\0');
+
+	sl = VUS_socklen(&uds);
+
 	AN(sl);
 
 	s = socket(PF_UNIX, SOCK_STREAM, 0);
@@ -182,3 +191,19 @@ VUS_connect(const char *path, int msec)
 
 	return (VTCP_connected(s));
 }
+
+socklen_t
+VUS_socklen(const struct sockaddr_un *uds)
+{
+	socklen_t sl;
+	char *p;
+	if (*uds->sun_path)
+		sl = sizeof(*uds);
+	else {
+		p = strchr(uds->sun_path + 1, '\0');
+		assert(p != NULL);
+		sl = p - (const char*)uds;
+	}
+	assert(sl <= sizeof(*uds));
+	return sl;
+}


More information about the varnish-commit mailing list