From ffa535f58fe63d72dc9427a840727fee0d1bee9f Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 18 Aug 2016 08:01:17 +0900 Subject: [PATCH] Set list of supplementary groups IDs Change-Id: I3072df4d4e41a12ca8fedcc4b59d3c378ef3c39f Signed-off-by: Hwankyu Jhun --- src/pkgmgr-server.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/pkgmgr-server.c b/src/pkgmgr-server.c index 69a6623..22d15a8 100644 --- a/src/pkgmgr-server.c +++ b/src/pkgmgr-server.c @@ -580,24 +580,42 @@ int set_environement(user_ctx *ctx) int i = 0; int res = 0; char **env = NULL; - gid_t groups[] = {EXT_STORAGE_GROUP, - EXT_STORAGE_APPDATA_GROUP, - MEDIA_STORAGE_GROUP}; + int n; + gid_t *groups; if (!ctx) - return -1;; - if (setgid(ctx->gid)) { - ERR("setgid failed: %d", errno); + return -1; + + n = getgroups(0, NULL); + if (n < 0) { + ERR("Failed to get the number of supplementary group IDs"); return -1; } - if (setgroups(ARRAY_SIZE(groups), groups) < 0) { - ERR("setgroups failed: %d", errno); + + groups = (gid_t *)calloc(1, sizeof(gid_t) * (n + 3)); + if (groups == NULL) { + ERR("out of memory"); + return -1; + } + + n = getgroups(n, groups); + if (n < 0) { + ERR("Failed to get list of supplementary group IDs"); + free(groups); return -1; } - if (setuid(ctx->uid)) { - ERR("setuid failed: %d", errno); + + groups[n++] = EXT_STORAGE_GROUP; + groups[n++] = EXT_STORAGE_APPDATA_GROUP; + groups[n++] = MEDIA_STORAGE_GROUP; + + if (setgroups(n, groups) < 0) { + ERR("setgroups failed: %d", errno); + free(groups); return -1; } + free(groups); + env = ctx->env; /* env variable ends by NULL element */ while (env[i]) { -- 2.7.4