diff mbox series

[v2,07/30] libmpathcmd: use target length for unix socket sun_path

Message ID 20190624092756.7769-8-mwilck@suse.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show
Series None | expand

Commit Message

Martin Wilck June 24, 2019, 9:27 a.m. UTC
Note that sun_path doesn't necessarily need to be 0-terminated for an abstract
socket name for ux_socket_listen(), this means we need to use memcpy to avoid
a spurious warning.

Signed-off-by: Martin Wilck <mwilck@suse.com>
---
 libmpathcmd/mpath_cmd.c | 4 +++-
 libmultipath/uxsock.c   | 7 +++++--
 2 files changed, 8 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libmpathcmd/mpath_cmd.c b/libmpathcmd/mpath_cmd.c
index b681311b..f00bf7e1 100644
--- a/libmpathcmd/mpath_cmd.c
+++ b/libmpathcmd/mpath_cmd.c
@@ -103,8 +103,10 @@  int __mpath_connect(int nonblocking)
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_LOCAL;
 	addr.sun_path[0] = '\0';
+	strncpy(&addr.sun_path[1], DEFAULT_SOCKET, sizeof(addr.sun_path) - 1);
 	len = strlen(DEFAULT_SOCKET) + 1 + sizeof(sa_family_t);
-	strncpy(&addr.sun_path[1], DEFAULT_SOCKET, len);
+	if (len > sizeof(struct sockaddr_un))
+		len = sizeof(struct sockaddr_un);
 
 	fd = socket(AF_LOCAL, SOCK_STREAM, 0);
 	if (fd == -1)
diff --git a/libmultipath/uxsock.c b/libmultipath/uxsock.c
index 7e5a1449..9b4e9784 100644
--- a/libmultipath/uxsock.c
+++ b/libmultipath/uxsock.c
@@ -66,9 +66,12 @@  int ux_socket_listen(const char *name)
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_LOCAL;
 	addr.sun_path[0] = '\0';
-	len = strlen(name) + 1 + sizeof(sa_family_t);
-	strncpy(&addr.sun_path[1], name, len);
+	len = strlen(name) + 1;
+	if (len >= sizeof(addr.sun_path))
+		len = sizeof(addr.sun_path) - 1;
+	memcpy(&addr.sun_path[1], name, len);
 
+	len += sizeof(sa_family_t);
 	if (bind(fd, (struct sockaddr *)&addr, len) == -1) {
 		condlog(3, "Couldn't bind to ux_socket, error %d", errno);
 		close(fd);