dcavalca / rpms / systemd

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