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