]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
socket-util: start SO_PEERGROUP loop with sysconf(_SC_NGROUPS_MAX), too
authorLennart Poettering <lennart@poettering.net>
Mon, 29 Jan 2024 09:18:30 +0000 (10:18 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 29 Jan 2024 13:40:15 +0000 (14:40 +0100)
We do this for getgroups_malloc() hence we should do this here too,
after all whether we do it for a socket peer or for ourselves doesn't
make too much of a difference.

src/basic/socket-util.c

index 0a6c87b023d189c0d7b0c0ba0d9db397e7db29ef..a9ad1cc9936e7d9ff0d95503d6585ff5f96a3936 100644 (file)
@@ -930,12 +930,14 @@ int getpeersec(int fd, char **ret) {
 }
 
 int getpeergroups(int fd, gid_t **ret) {
-        socklen_t n = sizeof(gid_t) * 64;
         _cleanup_free_ gid_t *d = NULL;
 
         assert(fd >= 0);
         assert(ret);
 
+        long ngroups_max = sysconf(_SC_NGROUPS_MAX);
+        socklen_t n = sizeof(gid_t) * MAX((socklen_t) ngroups_max, 64U);
+
         for (;;) {
                 d = malloc(n);
                 if (!d)
@@ -953,7 +955,7 @@ int getpeergroups(int fd, gid_t **ret) {
         assert_se(n % sizeof(gid_t) == 0);
         n /= sizeof(gid_t);
 
-        if ((socklen_t) (int) n != n)
+        if (n > INT_MAX)
                 return -E2BIG;
 
         *ret = TAKE_PTR(d);