Change in libosmocore[master]: ensure unix socket paths are NUL-terminated for bind/connect

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/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Wed Sep 26 07:25:53 UTC 2018


Harald Welte has submitted this change and it was merged. ( https://gerrit.osmocom.org/11044 )

Change subject: ensure unix socket paths are NUL-terminated for bind/connect
......................................................................

ensure unix socket paths are NUL-terminated for bind/connect

The unix(7) man page recommends that sun_path is NUL-terminated
when struct sockaddr_un is passed to a bind() or connect() call.
Non-NUL-terminated paths only need to be dealt with at the
receiving end of a UNIX domain socket.

Commit 896ff6d erroneously assumed otherwise.
This commit almost reverts 896ff6d: It only leaves the added
osmo_strlcpy() overflow check in place.

Change-Id: I6c4ac6b0a0eef4842beae4107f6f09f6cd29172a
Fixes: 896ff6db161465d506bb9bb5bee2cdeef220dd2e
Related: OS#2673
---
M src/socket.c
1 file changed, 5 insertions(+), 7 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/src/socket.c b/src/socket.c
index 6f56efb..a85edb7 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -605,29 +605,27 @@
 	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;
-	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)) {
+	/* When an AF_UNIX socket is bound, sun_path should be NUL-terminated. See unix(7) man page. */
+	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 = socket_path_len;
+	local.sun_len = strlen(local.sun_path);
 #endif
 #if defined(BSD44SOCKETS) || defined(SUN_LEN)
 	namelen = SUN_LEN(&local);
 #else
-	namelen = socket_path_len + offsetof(struct sockaddr_un, sun_path);
+	namelen = strlen(local.sun_path) +
+		  offsetof(struct sockaddr_un, sun_path);
 #endif
 
 	sfd = socket(AF_UNIX, type, proto);

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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I6c4ac6b0a0eef4842beae4107f6f09f6cd29172a
Gerrit-Change-Number: 11044
Gerrit-PatchSet: 2
Gerrit-Owner: Stefan Sperling <ssperling at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180926/8db27e1f/attachment.htm>


More information about the gerrit-log mailing list