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