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