d2787b
From 87b7689f7727a542c5afa22bdebd3781dd650a2f Mon Sep 17 00:00:00 2001
d2787b
From: Csaba Henk <csaba@redhat.com>
d2787b
Date: Fri, 17 Jul 2020 11:33:36 +0200
d2787b
Subject: [PATCH 508/511] fuse: fetch arbitrary number of groups from
d2787b
 /proc/[pid]/status
d2787b
d2787b
Glusterfs so far constrained itself with an arbitrary limit (32)
d2787b
for the number of groups read from /proc/[pid]/status (this was
d2787b
the number of groups shown there prior to Linux commit
d2787b
v3.7-9553-g8d238027b87e (v3.8-rc1~74^2~59); since this commit, all
d2787b
groups are shown).
d2787b
d2787b
With this change we'll read groups up to the number Glusterfs
d2787b
supports in general (64k).
d2787b
d2787b
Note: the actual number of groups that are made use of in a
d2787b
regular Glusterfs setup shall still be capped at ~93 due to limitations
d2787b
of the RPC transport. To be able to handle more groups than that,
d2787b
brick side gid resolution (server.manage-gids option) can be used along
d2787b
with NIS, LDAP or other such networked directory service (see
d2787b
https://github.com/gluster/glusterdocs/blob/5ba15a2/docs/Administrator%20Guide/Handling-of-users-with-many-groups.md#limit-in-the-glusterfs-protocol
d2787b
).
d2787b
d2787b
Also adding some diagnostic messages to frame_fill_groups().
d2787b
d2787b
Upstream:
d2787b
> Reviewed-on: https://review.gluster.org/c/glusterfs/+/24721
d2787b
> Change-Id: I271f3dc3e6d3c44d6d989c7a2073ea5f16c26ee0
d2787b
> fixes: #1075
d2787b
> Signed-off-by: Csaba Henk <csaba@redhat.com>
d2787b
d2787b
BUG: 1749304
d2787b
Change-Id: I80bf99d34087fb95768bf2259d8c4774d9f5d0c5
d2787b
Signed-off-by: Csaba Henk <csaba@redhat.com>
d2787b
Reviewed-on: https://code.engineering.redhat.com/gerrit/220920
d2787b
Tested-by: RHGS Build Bot <nigelb@redhat.com>
d2787b
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
d2787b
---
d2787b
 libglusterfs/src/glusterfs/stack.h    |  7 ++++
d2787b
 tests/bugs/fuse/many-groups-for-acl.t | 13 ++++++-
d2787b
 xlators/mount/fuse/src/fuse-helpers.c | 71 +++++++++++++++++++++++------------
d2787b
 3 files changed, 65 insertions(+), 26 deletions(-)
d2787b
d2787b
diff --git a/libglusterfs/src/glusterfs/stack.h b/libglusterfs/src/glusterfs/stack.h
d2787b
index 1758550..bd466d8 100644
d2787b
--- a/libglusterfs/src/glusterfs/stack.h
d2787b
+++ b/libglusterfs/src/glusterfs/stack.h
d2787b
@@ -429,6 +429,7 @@ call_stack_alloc_groups(call_stack_t *stack, int ngrps)
d2787b
     if (ngrps <= SMALL_GROUP_COUNT) {
d2787b
         stack->groups = stack->groups_small;
d2787b
     } else {
d2787b
+        GF_FREE(stack->groups_large);
d2787b
         stack->groups_large = GF_CALLOC(ngrps, sizeof(gid_t),
d2787b
                                         gf_common_mt_groups_t);
d2787b
         if (!stack->groups_large)
d2787b
@@ -442,6 +443,12 @@ call_stack_alloc_groups(call_stack_t *stack, int ngrps)
d2787b
 }
d2787b
 
d2787b
 static inline int
d2787b
+call_stack_groups_capacity(call_stack_t *stack)
d2787b
+{
d2787b
+    return max(stack->ngrps, SMALL_GROUP_COUNT);
d2787b
+}
d2787b
+
d2787b
+static inline int
d2787b
 call_frames_count(call_stack_t *call_stack)
d2787b
 {
d2787b
     call_frame_t *pos;
d2787b
diff --git a/tests/bugs/fuse/many-groups-for-acl.t b/tests/bugs/fuse/many-groups-for-acl.t
d2787b
index d959f75..a51b1bc 100755
d2787b
--- a/tests/bugs/fuse/many-groups-for-acl.t
d2787b
+++ b/tests/bugs/fuse/many-groups-for-acl.t
d2787b
@@ -38,6 +38,13 @@ do
d2787b
 done
d2787b
 TEST useradd -o -M -u ${NEW_UID} -g ${NEW_GID} -G ${NEW_USER}-${NEW_GIDS} ${NEW_USER}
d2787b
 
d2787b
+# Linux < 3.8 exports only first 32 gids of pid to userspace
d2787b
+kernel_exports_few_gids=0
d2787b
+if [ "$OSTYPE" = Linux ] && \
d2787b
+   su -m ${NEW_USER} -c "grep ^Groups: /proc/self/status | wc -w | xargs -I@ expr @ - 1 '<' $LAST_GID - $NEW_GID + 1" > /dev/null; then
d2787b
+       kernel_exports_few_gids=1
d2787b
+fi
d2787b
+
d2787b
 # preparation done, start the tests
d2787b
 
d2787b
 TEST glusterd
d2787b
@@ -48,6 +55,8 @@ TEST $CLI volume set $V0 nfs.disable off
d2787b
 TEST $CLI volume set ${V0} server.manage-gids off
d2787b
 TEST $CLI volume start ${V0}
d2787b
 
d2787b
+# This is just a synchronization hack to make sure the bricks are
d2787b
+# up before going on.
d2787b
 EXPECT_WITHIN ${NFS_EXPORT_TIMEOUT} "1" is_nfs_export_available
d2787b
 
d2787b
 # mount the volume with POSIX ACL support, without --resolve-gids
d2787b
@@ -69,8 +78,8 @@ TEST [ $? -eq 0 ]
d2787b
 su -m ${NEW_USER} -c "touch ${M0}/first-32-gids-2/success > /dev/null"
d2787b
 TEST [ $? -eq 0 ]
d2787b
 
d2787b
-su -m ${NEW_USER} -c "touch ${M0}/gid-64/failure > /dev/null"
d2787b
-TEST [ $? -ne 0 ]
d2787b
+su -m ${NEW_USER} -c "touch ${M0}/gid-64/success--if-all-gids-exported > /dev/null"
d2787b
+TEST [ $? -eq $kernel_exports_few_gids ]
d2787b
 
d2787b
 su -m ${NEW_USER} -c "touch ${M0}/gid-120/failure > /dev/null"
d2787b
 TEST [ $? -ne 0 ]
d2787b
diff --git a/xlators/mount/fuse/src/fuse-helpers.c b/xlators/mount/fuse/src/fuse-helpers.c
d2787b
index 5bfc40c..6e04cd4 100644
d2787b
--- a/xlators/mount/fuse/src/fuse-helpers.c
d2787b
+++ b/xlators/mount/fuse/src/fuse-helpers.c
d2787b
@@ -139,8 +139,6 @@ get_fuse_state(xlator_t *this, fuse_in_header_t *finh)
d2787b
     return state;
d2787b
 }
d2787b
 
d2787b
-#define FUSE_MAX_AUX_GROUPS                                                    \
d2787b
-    32 /* We can get only up to 32 aux groups from /proc */
d2787b
 void
d2787b
 frame_fill_groups(call_frame_t *frame)
d2787b
 {
d2787b
@@ -150,8 +148,6 @@ frame_fill_groups(call_frame_t *frame)
d2787b
     char filename[32];
d2787b
     char line[4096];
d2787b
     char *ptr = NULL;
d2787b
-    FILE *fp = NULL;
d2787b
-    int idx = 0;
d2787b
     long int id = 0;
d2787b
     char *saveptr = NULL;
d2787b
     char *endptr = NULL;
d2787b
@@ -191,45 +187,72 @@ frame_fill_groups(call_frame_t *frame)
d2787b
 
d2787b
         call_stack_set_groups(frame->root, ngroups, &mygroups);
d2787b
     } else {
d2787b
+        FILE *fp = NULL;
d2787b
+
d2787b
         ret = snprintf(filename, sizeof filename, "/proc/%d/status",
d2787b
                        frame->root->pid);
d2787b
-        if (ret >= sizeof filename)
d2787b
+        if (ret >= sizeof filename) {
d2787b
+            gf_log(this->name, GF_LOG_ERROR, "procfs path exceeds buffer size");
d2787b
             goto out;
d2787b
+        }
d2787b
 
d2787b
         fp = fopen(filename, "r");
d2787b
-        if (!fp)
d2787b
+        if (!fp) {
d2787b
+            gf_log(this->name, GF_LOG_ERROR, "failed to open %s: %s", filename,
d2787b
+                   strerror(errno));
d2787b
             goto out;
d2787b
+        }
d2787b
 
d2787b
-        if (call_stack_alloc_groups(frame->root, ngroups) != 0)
d2787b
-            goto out;
d2787b
+        for (;;) {
d2787b
+            gf_boolean_t found_groups = _gf_false;
d2787b
+            int idx = 0;
d2787b
 
d2787b
-        while ((ptr = fgets(line, sizeof line, fp))) {
d2787b
-            if (strncmp(ptr, "Groups:", 7) != 0)
d2787b
-                continue;
d2787b
+            if (call_stack_alloc_groups(frame->root, ngroups) != 0) {
d2787b
+                gf_log(this->name, GF_LOG_ERROR,
d2787b
+                       "failed to allocate gid buffer");
d2787b
+                goto out;
d2787b
+            }
d2787b
 
d2787b
+            while ((ptr = fgets(line, sizeof line, fp))) {
d2787b
+                if (strncmp(ptr, "Groups:", 7) == 0) {
d2787b
+                    found_groups = _gf_true;
d2787b
+                    break;
d2787b
+                }
d2787b
+            }
d2787b
+            if (!found_groups) {
d2787b
+                gf_log(this->name, GF_LOG_ERROR, "cannot find gid list in %s",
d2787b
+                       filename);
d2787b
+                break;
d2787b
+            }
d2787b
             ptr = line + 8;
d2787b
 
d2787b
             for (ptr = strtok_r(ptr, " \t\r\n", &saveptr); ptr;
d2787b
                  ptr = strtok_r(NULL, " \t\r\n", &saveptr)) {
d2787b
                 errno = 0;
d2787b
                 id = strtol(ptr, &endptr, 0);
d2787b
-                if (errno == ERANGE)
d2787b
-                    break;
d2787b
-                if (!endptr || *endptr)
d2787b
+                if (errno == ERANGE || !endptr || *endptr) {
d2787b
+                    gf_log(this->name, GF_LOG_ERROR, "failed to parse %s",
d2787b
+                           filename);
d2787b
                     break;
d2787b
-                frame->root->groups[idx++] = id;
d2787b
-                if (idx == FUSE_MAX_AUX_GROUPS)
d2787b
+                }
d2787b
+                if (idx < call_stack_groups_capacity(frame->root))
d2787b
+                    frame->root->groups[idx] = id;
d2787b
+                idx++;
d2787b
+                if (idx == GF_MAX_AUX_GROUPS)
d2787b
                     break;
d2787b
             }
d2787b
-
d2787b
-            frame->root->ngrps = idx;
d2787b
-            break;
d2787b
+            if (idx > call_stack_groups_capacity(frame->root)) {
d2787b
+                ngroups = idx;
d2787b
+                rewind(fp);
d2787b
+            } else {
d2787b
+                frame->root->ngrps = idx;
d2787b
+                break;
d2787b
+            }
d2787b
         }
d2787b
+    out:
d2787b
+        if (fp)
d2787b
+            fclose(fp);
d2787b
     }
d2787b
-
d2787b
-out:
d2787b
-    if (fp)
d2787b
-        fclose(fp);
d2787b
 #elif defined(GF_SOLARIS_HOST_OS)
d2787b
     char filename[32];
d2787b
     char scratch[128];
d2787b
@@ -245,7 +268,7 @@ out:
d2787b
         fp = fopen(filename, "r");
d2787b
         if (fp != NULL) {
d2787b
             if (fgets(scratch, sizeof scratch, fp) != NULL) {
d2787b
-                ngrps = MIN(prcred->pr_ngroups, FUSE_MAX_AUX_GROUPS);
d2787b
+                ngrps = MIN(prcred->pr_ngroups, GF_MAX_AUX_GROUPS);
d2787b
                 if (call_stack_alloc_groups(frame->root, ngrps) != 0) {
d2787b
                     fclose(fp);
d2787b
                     return;
d2787b
-- 
d2787b
1.8.3.1
d2787b