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