dcavalca / rpms / systemd

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