chiark / gitweb /
socket-util: slight rework of getpeersec()
authorLennart Poettering <lennart@poettering.net>
Sat, 30 Dec 2017 14:15:44 +0000 (15:15 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:49:53 +0000 (07:49 +0200)
Let's call getsockopt() in a loop, so that we can deal correctly with
the label changing while we are trying to read it.

(also, while we are at it, let's make sure that there's always one
trailing NUL byte at the end of the buffer, after all SO_PEERSEC has
zero documentation, and multiple implementing backends, hence let's
better be safe than sorry)

src/basic/socket-util.c

index 860c8fbfcca4681277276568c9436032346852a2..c89e5c8c7f60c186ffe83eb763be0223cf99b03d 100644 (file)
@@ -987,41 +987,32 @@ int getpeercred(int fd, struct ucred *ucred) {
 }
 
 int getpeersec(int fd, char **ret) {
+        _cleanup_free_ char *s = NULL;
         socklen_t n = 64;
-        char *s;
-        int r;
 
         assert(fd >= 0);
         assert(ret);
 
-        s = new0(char, n);
-        if (!s)
-                return -ENOMEM;
+        for (;;) {
+                s = new0(char, n+1);
+                if (!s)
+                        return -ENOMEM;
 
-        r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n);
-        if (r < 0) {
-                free(s);
+                if (getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n) >= 0)
+                        break;
 
                 if (errno != ERANGE)
                         return -errno;
 
-                s = new0(char, n);
-                if (!s)
-                        return -ENOMEM;
-
-                r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n);
-                if (r < 0) {
-                        free(s);
-                        return -errno;
-                }
+                s = mfree(s);
         }
 
-        if (isempty(s)) {
-                free(s);
+        if (isempty(s))
                 return -EOPNOTSUPP;
-        }
 
         *ret = s;
+        s = NULL;
+
         return 0;
 }