d1681e
From b72f9f0d7da441db7e144f58459d98aa4838d032 Mon Sep 17 00:00:00 2001
d1681e
From: Csaba Henk <csaba@redhat.com>
d1681e
Date: Fri, 15 Dec 2017 08:02:30 +0100
d1681e
Subject: [PATCH 102/128] libglusterfs: fix the call_stack_set_group() function
d1681e
d1681e
- call_stack_set_group() will take the ownership of passed
d1681e
  buffer from caller;
d1681e
- to indicate the change, its signature is changed from
d1681e
  including the buffer directly to take a pointer to it;
d1681e
- either the content of the buffer is copied to the
d1681e
  groups_small embedded buffer of the call stack, or
d1681e
  the buffer is set as groups_large member of the call
d1681e
  stack;
d1681e
- the groups member of the call stack is set to,
d1681e
  respectively, groups_small or groups_large, according
d1681e
  to the memory management conventions of the call stack;
d1681e
- the buffer address is overwritten with junk to effectively
d1681e
  prevent the caller from using it further on.
d1681e
d1681e
Also move call_stack_set_group to stack.c from stack.h
d1681e
to prevent "defined but not used [-Wunused-function]"
d1681e
warnings (not using it anymore in call_stack_alloc_group()
d1681e
implementation, which saved us from this so far).
d1681e
d1681e
protocol/server: refactor gid_resolve()
d1681e
d1681e
In gid_resolve there are two cases:
d1681e
either the gid_cache_lookup() call returns
d1681e
a value or not. The result is caputured in
d1681e
the agl variable, and throughout the function,
d1681e
each particular stage of the implementation
d1681e
comes with an agl and a no-agl variant.
d1681e
d1681e
In most cases this is explicitly indicated
d1681e
via an
d1681e
d1681e
   if (agl) {
d1681e
      ...
d1681e
   } else {
d1681e
      ...
d1681e
   }
d1681e
d1681e
but some of this branching are expressed via
d1681e
goto constructs (obfuscating the fact we stated
d1681e
above, that is, each particular stage having
d1681e
an agl/no-agl variant).
d1681e
d1681e
In the current refactor, we bring the agl
d1681e
conditional to the top, and present the
d1681e
agl/non-agl implementations sequentially.
d1681e
d1681e
Also we take the opportunity to clean up and
d1681e
fix the agl case:
d1681e
- remove the spurious
d1681e
    gl.gl_list = agl->gl_list;
d1681e
  setting, as gl is not used in the agl caae
d1681e
- populate the group list of call stack from
d1681e
  agl, fixing thus referred BUG.
d1681e
d1681e
Also fixes BUG: 1513920
d1681e
d1681e
> Change-Id: I61f4574ba21969f7661b9ff0c9dce202b874025d
d1681e
> BUG: 1513928
d1681e
> Signed-off-by: Csaba Henk <csaba@redhat.com>
d1681e
> Reviewed-on: https://review.gluster.org/18789
d1681e
d1681e
Change-Id: I61f4574ba21969f7661b9ff0c9dce202b874025d
d1681e
BUG: 1512963
d1681e
Signed-off-by: Csaba Henk <csaba@redhat.com>
d1681e
Reviewed-on: https://code.engineering.redhat.com/gerrit/125931
d1681e
Tested-by: RHGS Build Bot <nigelb@redhat.com>
d1681e
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
d1681e
---
d1681e
 libglusterfs/src/stack.c                     | 20 +++++++++
d1681e
 libglusterfs/src/stack.h                     | 14 +++---
d1681e
 xlators/mount/fuse/src/fuse-helpers.c        |  2 +-
d1681e
 xlators/protocol/server/src/server-helpers.c | 65 +++++++++++++---------------
d1681e
 4 files changed, 57 insertions(+), 44 deletions(-)
d1681e
d1681e
diff --git a/libglusterfs/src/stack.c b/libglusterfs/src/stack.c
d1681e
index 6977814..d64ac8a 100644
d1681e
--- a/libglusterfs/src/stack.c
d1681e
+++ b/libglusterfs/src/stack.c
d1681e
@@ -65,6 +65,26 @@ create_frame (xlator_t *xl, call_pool_t *pool)
d1681e
 }
d1681e
 
d1681e
 void
d1681e
+call_stack_set_groups (call_stack_t *stack, int ngrps, gid_t **groupbuf_p)
d1681e
+{
d1681e
+        /* We take the ownership of the passed group buffer. */
d1681e
+
d1681e
+        if (ngrps <= SMALL_GROUP_COUNT) {
d1681e
+                memcpy (stack->groups_small, *groupbuf_p,
d1681e
+                        sizeof (gid_t) * ngrps);
d1681e
+                stack->groups = stack->groups_small;
d1681e
+                GF_FREE (*groupbuf_p);
d1681e
+        } else {
d1681e
+                stack->groups_large = *groupbuf_p;
d1681e
+                stack->groups = stack->groups_large;
d1681e
+        }
d1681e
+
d1681e
+        stack->ngrps = ngrps;
d1681e
+        /* Set a canary. */
d1681e
+        *groupbuf_p = (void *)0xdeadf00d;
d1681e
+}
d1681e
+
d1681e
+void
d1681e
 gf_proc_dump_call_frame (call_frame_t *call_frame, const char *key_buf,...)
d1681e
 {
d1681e
 
d1681e
diff --git a/libglusterfs/src/stack.h b/libglusterfs/src/stack.h
d1681e
index eb5848e..50a6fc7 100644
d1681e
--- a/libglusterfs/src/stack.h
d1681e
+++ b/libglusterfs/src/stack.h
d1681e
@@ -357,26 +357,21 @@ STACK_RESET (call_stack_t *stack)
d1681e
         } while (0)
d1681e
 
d1681e
 
d1681e
-static void
d1681e
-call_stack_set_groups (call_stack_t *stack, int ngrps, gid_t *groupbuf)
d1681e
-{
d1681e
-        stack->groups = groupbuf;
d1681e
-        stack->ngrps = ngrps;
d1681e
-}
d1681e
-
d1681e
 static inline int
d1681e
 call_stack_alloc_groups (call_stack_t *stack, int ngrps)
d1681e
 {
d1681e
 	if (ngrps <= SMALL_GROUP_COUNT) {
d1681e
-		call_stack_set_groups (stack, ngrps, stack->groups_small);
d1681e
+		stack->groups = stack->groups_small;
d1681e
 	} else {
d1681e
 		stack->groups_large = GF_CALLOC (ngrps, sizeof (gid_t),
d1681e
 						 gf_common_mt_groups_t);
d1681e
 		if (!stack->groups_large)
d1681e
 			return -1;
d1681e
-		call_stack_set_groups (stack, ngrps, stack->groups_large);
d1681e
+		stack->groups = stack->groups_large;
d1681e
 	}
d1681e
 
d1681e
+	stack->ngrps = ngrps;
d1681e
+
d1681e
 	return 0;
d1681e
 }
d1681e
 
d1681e
@@ -465,6 +460,7 @@ copy_frame (call_frame_t *frame)
d1681e
         return newframe;
d1681e
 }
d1681e
 
d1681e
+void call_stack_set_groups (call_stack_t *stack, int ngrps, gid_t **groupbuf_p);
d1681e
 void gf_proc_dump_pending_frames(call_pool_t *call_pool);
d1681e
 void gf_proc_dump_pending_frames_to_dict (call_pool_t *call_pool,
d1681e
                                           dict_t *dict);
d1681e
diff --git a/xlators/mount/fuse/src/fuse-helpers.c b/xlators/mount/fuse/src/fuse-helpers.c
d1681e
index 3fc6b16..c59ff77 100644
d1681e
--- a/xlators/mount/fuse/src/fuse-helpers.c
d1681e
+++ b/xlators/mount/fuse/src/fuse-helpers.c
d1681e
@@ -181,7 +181,7 @@ frame_fill_groups (call_frame_t *frame)
d1681e
                         return;
d1681e
                 }
d1681e
 
d1681e
-                call_stack_set_groups (frame->root, ngroups, mygroups);
d1681e
+                call_stack_set_groups (frame->root, ngroups, &mygroups);
d1681e
         } else {
d1681e
                 ret = snprintf (filename, sizeof filename, "/proc/%d/status",
d1681e
                                 frame->root->pid);
d1681e
diff --git a/xlators/protocol/server/src/server-helpers.c b/xlators/protocol/server/src/server-helpers.c
d1681e
index 51eb491..c8f5382 100644
d1681e
--- a/xlators/protocol/server/src/server-helpers.c
d1681e
+++ b/xlators/protocol/server/src/server-helpers.c
d1681e
@@ -31,13 +31,24 @@ gid_resolve (server_conf_t *conf, call_stack_t *root)
d1681e
         struct passwd    *result;
d1681e
         gid_t            *mygroups;
d1681e
         gid_list_t        gl;
d1681e
-        const gid_list_t *agl;
d1681e
         int               ngroups;
d1681e
+        const gid_list_t *agl;
d1681e
 
d1681e
         agl = gid_cache_lookup (&conf->gid_cache, root->uid, 0, 0);
d1681e
         if (agl) {
d1681e
                 root->ngrps = agl->gl_count;
d1681e
-                goto fill_groups;
d1681e
+
d1681e
+                if (root->ngrps > 0) {
d1681e
+                        ret = call_stack_alloc_groups (root, agl->gl_count);
d1681e
+                        if (ret == 0) {
d1681e
+                                memcpy (root->groups, agl->gl_list,
d1681e
+                                        sizeof (gid_t) * agl->gl_count);
d1681e
+                        }
d1681e
+                }
d1681e
+
d1681e
+                gid_cache_release (&conf->gid_cache, agl);
d1681e
+
d1681e
+                return ret;
d1681e
         }
d1681e
 
d1681e
         ret = getpwuid_r (root->uid, &mypw, mystrs, sizeof(mystrs), &result);
d1681e
@@ -66,42 +77,28 @@ gid_resolve (server_conf_t *conf, call_stack_t *root)
d1681e
         }
d1681e
         root->ngrps = (uint16_t) ngroups;
d1681e
 
d1681e
-fill_groups:
d1681e
-        if (agl) {
d1681e
-                /* the gl is not complete, we only use gl.gl_list later on */
d1681e
-                gl.gl_list = agl->gl_list;
d1681e
-        } else {
d1681e
-                /* setup a full gid_list_t to add it to the gid_cache */
d1681e
-                gl.gl_id = root->uid;
d1681e
-                gl.gl_uid = root->uid;
d1681e
-                gl.gl_gid = root->gid;
d1681e
-                gl.gl_count = root->ngrps;
d1681e
-
d1681e
-                gl.gl_list = GF_MALLOC (root->ngrps * sizeof(gid_t),
d1681e
-                                        gf_common_mt_groups_t);
d1681e
-                if (gl.gl_list)
d1681e
-                        memcpy (gl.gl_list, mygroups,
d1681e
-                                sizeof(gid_t) * root->ngrps);
d1681e
-                else {
d1681e
-                        GF_FREE (mygroups);
d1681e
-                        return -1;
d1681e
-                }
d1681e
+        /* setup a full gid_list_t to add it to the gid_cache */
d1681e
+        gl.gl_id = root->uid;
d1681e
+        gl.gl_uid = root->uid;
d1681e
+        gl.gl_gid = root->gid;
d1681e
+        gl.gl_count = root->ngrps;
d1681e
+
d1681e
+        gl.gl_list = GF_MALLOC (root->ngrps * sizeof(gid_t),
d1681e
+                                gf_common_mt_groups_t);
d1681e
+        if (gl.gl_list)
d1681e
+                memcpy (gl.gl_list, mygroups,
d1681e
+                        sizeof(gid_t) * root->ngrps);
d1681e
+        else {
d1681e
+                GF_FREE (mygroups);
d1681e
+                return -1;
d1681e
         }
d1681e
 
d1681e
-        if (root->ngrps == 0) {
d1681e
-                ret = 0;
d1681e
-                goto out;
d1681e
+        if (root->ngrps > 0) {
d1681e
+                call_stack_set_groups (root, root->ngrps, &mygroups);
d1681e
         }
d1681e
 
d1681e
-        call_stack_set_groups (root, root->ngrps, mygroups);
d1681e
-
d1681e
-out:
d1681e
-        if (agl) {
d1681e
-                gid_cache_release (&conf->gid_cache, agl);
d1681e
-        } else {
d1681e
-                if (gid_cache_add (&conf->gid_cache, &gl) != 1)
d1681e
-                        GF_FREE (gl.gl_list);
d1681e
-        }
d1681e
+        if (gid_cache_add (&conf->gid_cache, &gl) != 1)
d1681e
+                GF_FREE (gl.gl_list);
d1681e
 
d1681e
         return ret;
d1681e
 }
d1681e
-- 
d1681e
1.8.3.1
d1681e