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