[master] 1e1493bdc Refactor: Centralize (struct sockaddr_un) initialization
Nils Goroll
nils.goroll at uplex.de
Mon Nov 14 15:53:07 UTC 2022
commit 1e1493bdc7ba4f8ef6dba35281fb2b18c688423c
Author: Nils Goroll <nils.goroll at uplex.de>
Date: Mon Nov 14 16:45:22 2022 +0100
Refactor: Centralize (struct sockaddr_un) initialization
We had three call sites, one with and two without size checking.
Other improvements:
- zero (struct sockaddr_un).sun_path
- set errno if the path argument does not fit
Motivated by: #3863
diff --git a/bin/varnishd/mgt/mgt_acceptor.c b/bin/varnishd/mgt/mgt_acceptor.c
index 612198672..9cbeb81aa 100644
--- a/bin/varnishd/mgt/mgt_acceptor.c
+++ b/bin/varnishd/mgt/mgt_acceptor.c
@@ -76,11 +76,17 @@ struct uds_perms {
static VTAILQ_HEAD(,listen_arg) listen_args =
VTAILQ_HEAD_INITIALIZER(listen_args);
+static int
+mac_vus_bind(void *priv, const struct sockaddr_un *uds)
+{
+ return (VUS_bind(uds, priv));
+}
+
static int
mac_opensocket(struct listen_sock *ls)
{
int fail;
- struct sockaddr_un uds;
+ const char *err;
CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC);
if (ls->sock > 0) {
@@ -89,11 +95,8 @@ mac_opensocket(struct listen_sock *ls)
}
if (!ls->uds)
ls->sock = VTCP_bind(ls->addr, NULL);
- else {
- uds.sun_family = PF_UNIX;
- bprintf(uds.sun_path, "%s", ls->endpoint);
- ls->sock = VUS_bind(&uds, NULL);
- }
+ else
+ ls->sock = VUS_resolver(ls->endpoint, mac_vus_bind, NULL, &err);
fail = errno;
if (ls->sock < 0) {
AN(fail);
diff --git a/lib/libvarnish/vus.c b/lib/libvarnish/vus.c
index ba803b08d..0f39fb601 100644
--- a/lib/libvarnish/vus.c
+++ b/lib/libvarnish/vus.c
@@ -41,24 +41,41 @@
#include "vus.h"
#include "vtcp.h"
+static int
+sun_init(struct sockaddr_un *uds, const char *path, const char **err)
+{
+ AN(uds);
+ AN(path);
+ assert(*path == '/');
+
+ if (err)
+ *err = NULL;
+
+ if (strlen(path) + 1 > sizeof(uds->sun_path)) {
+ errno = ENAMETOOLONG;
+ if (err)
+ *err = "Path too long for a Unix domain socket";
+ return (-1);
+ }
+ memset(uds->sun_path, 0, sizeof(uds->sun_path));
+ bprintf(uds->sun_path, "%s", path);
+ uds->sun_family = PF_UNIX;
+ return (0);
+}
+
int
VUS_resolver(const char *path, vus_resolved_f *func, void *priv,
const char **err)
{
struct sockaddr_un uds;
- int ret = 0;
-
- AN(path);
- assert(*path == '/');
+ int ret;
AN(err);
- *err = NULL;
- if (strlen(path) + 1 > sizeof(uds.sun_path)) {
- *err = "Path too long for a Unix domain socket";
- return (-1);
- }
- bprintf(uds.sun_path, "%s", path);
- uds.sun_family = PF_UNIX;
+
+ ret = sun_init(&uds, path, err);
+ if (ret)
+ return (ret);
+
if (func != NULL)
ret = func(priv, &uds);
return (ret);
@@ -110,8 +127,9 @@ VUS_connect(const char *path, int msec)
if (path == NULL)
return (-1);
- uds.sun_family = PF_UNIX;
- bprintf(uds.sun_path, "%s", path);
+ i = sun_init(&uds, path, NULL);
+ if (i)
+ return (i);
AN(sl);
s = socket(PF_UNIX, SOCK_STREAM, 0);
More information about the varnish-commit
mailing list