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 --- xlators/mount/fuse/src/fuse-helpers.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'xlators/mount') diff --git a/xlators/mount/fuse/src/fuse-helpers.c b/xlators/mount/fuse/src/fuse-helpers.c index b65e6ebfc7b..3fc6b16c41f 100644 --- a/xlators/mount/fuse/src/fuse-helpers.c +++ b/xlators/mount/fuse/src/fuse-helpers.c @@ -20,7 +20,6 @@ #include #endif #include -#include #include "fuse-bridge.h" @@ -158,8 +157,8 @@ frame_fill_groups (call_frame_t *frame) char *saveptr = NULL; char *endptr = NULL; int ret = 0; - int ngroups = FUSE_MAX_AUX_GROUPS; - gid_t mygroups[GF_MAX_AUX_GROUPS]; + int ngroups = 0; + gid_t *mygroups = NULL; if (priv->resolve_gids) { struct passwd pwent; @@ -173,23 +172,16 @@ frame_fill_groups (call_frame_t *frame) return; } - ngroups = GF_MAX_AUX_GROUPS; - if (getgrouplist (result->pw_name, frame->root->gid, mygroups, - &ngroups) == -1) { + ngroups = gf_getgrouplist (result->pw_name, frame->root->gid, + &mygroups); + if (ngroups == -1) { gf_log (this->name, GF_LOG_ERROR, "could not map %s to " "group list (ngroups %d, max %d)", result->pw_name, ngroups, GF_MAX_AUX_GROUPS); return; } - if (call_stack_alloc_groups (frame->root, ngroups) != 0) - goto out; - - /* Copy data to the frame. */ - for (idx = 0; idx < ngroups; ++idx) { - frame->root->groups[idx] = mygroups[idx]; - } - frame->root->ngrps = ngroups; + call_stack_set_groups (frame->root, ngroups, mygroups); } else { ret = snprintf (filename, sizeof filename, "/proc/%d/status", frame->root->pid); -- cgit