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