From 261591e11c1d15d674182c85ef437533b9b310d3 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 18 Sep 2013 17:18:48 +0100 Subject: [PATCH 7/7] _dbus_read_credentials_socket: look at all cmsg headers, not just the first If there are no cmsg headers, don't fail: this fixes receiving credentials on TCP sockets under at least GNU/kFreeBSD, and probably FreeBSD too. If there's more than one cmsg header, ignore any that don't look like valid SCM_CREDS. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=69492 --- dbus/dbus-sysdeps-unix.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 13a0e98..1fa7819 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -1712,16 +1712,6 @@ _dbus_read_credentials_socket (int client_fd, return FALSE; } -#if defined(HAVE_CMSGCRED) - if (cmsg.hdr.cmsg_len < CMSG_LEN (sizeof (struct cmsgcred)) - || cmsg.hdr.cmsg_type != SCM_CREDS) - { - dbus_set_error (error, DBUS_ERROR_FAILED, - "Message from recvmsg() was not SCM_CREDS"); - return FALSE; - } -#endif - _dbus_verbose ("read credentials byte\n"); { @@ -1762,10 +1752,21 @@ _dbus_read_credentials_socket (int client_fd, * which makes it better than getpeereid(). */ struct cmsgcred *cred; + struct cmsghdr *cmsgp; - cred = (struct cmsgcred *) CMSG_DATA (&cmsg.hdr); - pid_read = cred->cmcred_pid; - uid_read = cred->cmcred_euid; + for (cmsgp = CMSG_FIRSTHDR (&msg); + cmsgp != NULL; + cmsgp = CMSG_NXTHDR (&msg, cmsgp)) + { + if (cmsgp->cmsg_type == SCM_CREDS && + cmsgp->cmsg_level == SOL_SOCKET && + cmsgp->cmsg_len >= CMSG_LEN (sizeof (struct cmsgcred))) + { + cred = (struct cmsgcred *) CMSG_DATA (cmsgp); + pid_read = cred->cmcred_pid; + uid_read = cred->cmcred_euid; + } + } #elif defined(HAVE_GETPEERUCRED) /* Supported in at least Solaris >= 10. It should probably be higher -- 1.8.4.rc3