From e14ea3f5c37475e12a3b7fb7bd3165b0a4e77c51 Mon Sep 17 00:00:00 2001 From: Csaba Henk Date: Wed, 5 Jul 2017 17:48:37 +0200 Subject: groups: don't allocate auxiliary gid list on stack When glusterfs wants to retrieve the list of auxiliary gids of a user, it typically allocates a sufficiently big gid_t array on stack and calls getgrouplist(3) with it. However, "sufficiently big" means to be of maximum supported gid list size, which in GlusterFS is GF_MAX_AUX_GROUPS = 64k. That means a 64k * sizeof(gid_t) = 256k allocation, which is big enough to overflow the stack in certain cases. A further observation is that stack allocation of the gid list brings no gain, as in all cases the content of the gid list eventually gets copied over to a heap allocated buffer. So we add a convenience wrapper of getgrouplist to libglusterfs called gf_getgrouplist which calls getgrouplist with a sufficiently big heap allocated buffer (it takes care of the allocation too). We are porting all the getgrouplist invocations to gf_getgrouplist and thus eliminate the huge stack allocation. BUG: 1464327 Change-Id: Icea76d0d74dcf2f87d26cb299acc771ca3b32d2b Signed-off-by: Csaba Henk Reviewed-on: https://review.gluster.org/17706 Smoke: Gluster Build System Reviewed-by: Niels de Vos Reviewed-by: Amar Tumballi CentOS-regression: Gluster Build System --- libglusterfs/src/stack.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'libglusterfs/src/stack.h') diff --git a/libglusterfs/src/stack.h b/libglusterfs/src/stack.h index 20fbdfabff5..eb5848e92aa 100644 --- a/libglusterfs/src/stack.h +++ b/libglusterfs/src/stack.h @@ -357,21 +357,26 @@ STACK_RESET (call_stack_t *stack) } while (0) +static void +call_stack_set_groups (call_stack_t *stack, int ngrps, gid_t *groupbuf) +{ + stack->groups = groupbuf; + stack->ngrps = ngrps; +} + static inline int call_stack_alloc_groups (call_stack_t *stack, int ngrps) { if (ngrps <= SMALL_GROUP_COUNT) { - stack->groups = stack->groups_small; + call_stack_set_groups (stack, ngrps, stack->groups_small); } else { - stack->groups_large = GF_CALLOC (sizeof (gid_t), ngrps, + stack->groups_large = GF_CALLOC (ngrps, sizeof (gid_t), gf_common_mt_groups_t); if (!stack->groups_large) return -1; - stack->groups = stack->groups_large; + call_stack_set_groups (stack, ngrps, stack->groups_large); } - stack->ngrps = ngrps; - return 0; } -- cgit