Change in libosmocore[master]: Properly deal with sockaddr_un socket path length limitations.

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Stefan Sperling gerrit-no-reply at lists.osmocom.org
Tue Aug 28 12:38:54 UTC 2018


Stefan Sperling has uploaded this change for review. ( https://gerrit.osmocom.org/10648


Change subject: Properly deal with sockaddr_un socket path length limitations.
......................................................................

Properly deal with sockaddr_un socket path length limitations.

In osmo_sock_unix_init(), add support for non-NUL-terminated unix
socket paths and return an error if the supplied socket path exceeds
the maximum socket path length supported by the operating system.

Change-Id: I19d935e5e3dd7928e6e153c6f5ad7044de726016
Related: OS#2673
---
M src/socket.c
1 file changed, 11 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/48/10648/1

diff --git a/src/socket.c b/src/socket.c
index 7e7f6d9..6f56efb 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -605,22 +605,29 @@
 	struct sockaddr_un local;
 	int sfd, rc, on = 1;
 	unsigned int namelen;
+	const size_t socket_path_len = strlen(socket_path);
 
 	if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) ==
 		     (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT))
 		return -EINVAL;
 
 	local.sun_family = AF_UNIX;
-	osmo_strlcpy(local.sun_path, socket_path, sizeof(local.sun_path));
+	if (socket_path_len == sizeof(local.sun_path)) {
+		/* Handle corner-case where sun_path is not NUL-terminated. See the unix(7) man page. */
+		memcpy(local.sun_path, socket_path, sizeof(local.sun_path));
+	} else if (osmo_strlcpy(local.sun_path, socket_path, sizeof(local.sun_path)) >= sizeof(local.sun_path)) {
+		LOGP(DLGLOBAL, LOGL_ERROR, "Socket path exceeds maximum length of %zd bytes: %s\n",
+		     sizeof(local.sun_path), socket_path);
+		return -ENOSPC;
+	}
 
 #if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
-	local.sun_len = strlen(local.sun_path);
+	local.sun_len = socket_path_len;
 #endif
 #if defined(BSD44SOCKETS) || defined(SUN_LEN)
 	namelen = SUN_LEN(&local);
 #else
-	namelen = strlen(local.sun_path) +
-		  offsetof(struct sockaddr_un, sun_path);
+	namelen = socket_path_len + offsetof(struct sockaddr_un, sun_path);
 #endif
 
 	sfd = socket(AF_UNIX, type, proto);

-- 
To view, visit https://gerrit.osmocom.org/10648
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I19d935e5e3dd7928e6e153c6f5ad7044de726016
Gerrit-Change-Number: 10648
Gerrit-PatchSet: 1
Gerrit-Owner: Stefan Sperling <ssperling at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180828/13de9f38/attachment.htm>


More information about the gerrit-log mailing list