daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
Brian Stinson 2593d8
From e1bd03e75860fb349a6de589bbb1274acc454aef Mon Sep 17 00:00:00 2001
Brian Stinson 2593d8
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
Brian Stinson 2593d8
Date: Fri, 13 Sep 2019 11:18:18 +0200
Brian Stinson 2593d8
Subject: [PATCH] Call getgroups() to know size of supplementary groups array
Brian Stinson 2593d8
 to allocate
Brian Stinson 2593d8
Brian Stinson 2593d8
Resolves RHBZ #1743230 - journalctl dumps core when stack limit is reduced to 256 KB
Brian Stinson 2593d8
Brian Stinson 2593d8
(cherry picked from commit f5e0b942af1e86993c21f4e5c84342bb10403dac)
Brian Stinson 2593d8
Brian Stinson 2593d8
Resolves: #1743235
Brian Stinson 2593d8
---
Brian Stinson 2593d8
 src/basic/user-util.c | 14 ++++++++------
Brian Stinson 2593d8
 1 file changed, 8 insertions(+), 6 deletions(-)
Brian Stinson 2593d8
Brian Stinson 2593d8
diff --git a/src/basic/user-util.c b/src/basic/user-util.c
Brian Stinson 2593d8
index a562a397c7..c533f67025 100644
Brian Stinson 2593d8
--- a/src/basic/user-util.c
Brian Stinson 2593d8
+++ b/src/basic/user-util.c
Brian Stinson 2593d8
@@ -358,9 +358,8 @@ char* gid_to_name(gid_t gid) {
Brian Stinson 2593d8
 }
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 int in_gid(gid_t gid) {
Brian Stinson 2593d8
-        long ngroups_max;
Brian Stinson 2593d8
         gid_t *gids;
Brian Stinson 2593d8
-        int r, i;
Brian Stinson 2593d8
+        int ngroups, r, i;
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         if (getgid() == gid)
Brian Stinson 2593d8
                 return 1;
Brian Stinson 2593d8
@@ -371,12 +370,15 @@ int in_gid(gid_t gid) {
Brian Stinson 2593d8
         if (!gid_is_valid(gid))
Brian Stinson 2593d8
                 return -EINVAL;
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-        ngroups_max = sysconf(_SC_NGROUPS_MAX);
Brian Stinson 2593d8
-        assert(ngroups_max > 0);
Brian Stinson 2593d8
+        ngroups = getgroups(0, NULL);
Brian Stinson 2593d8
+        if (ngroups < 0)
Brian Stinson 2593d8
+                return -errno;
Brian Stinson 2593d8
+        if (ngroups == 0)
Brian Stinson 2593d8
+                return 0;
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-        gids = newa(gid_t, ngroups_max);
Brian Stinson 2593d8
+        gids = newa(gid_t, ngroups);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-        r = getgroups(ngroups_max, gids);
Brian Stinson 2593d8
+        r = getgroups(ngroups, gids);
Brian Stinson 2593d8
         if (r < 0)
Brian Stinson 2593d8
                 return -errno;
Brian Stinson 2593d8