]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
fix getgrgid() thread safe issue
authorDonghwa Jeong <dh48.jeong@samsung.com>
Thu, 14 Jun 2018 10:06:11 +0000 (19:06 +0900)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 10 Dec 2018 08:16:02 +0000 (09:16 +0100)
Signed-off-by: Donghwa Jeong <dh48.jeong@samsung.com>
src/lxc/conf.c
src/lxc/lxc_user_nic.c

index fb61d285afadba91c79635c8ee62bc229d14cc19..807f59fa87477ef42e195119c9f28b75d2714871 100644 (file)
@@ -4275,13 +4275,35 @@ static char* getuname(void)
 /* not thread-safe, do not use from api without first forking */
 static char *getgname(void)
 {
-       struct group *result;
+       struct group grent;
+       struct group *grentp = NULL;
+       char *buf;
+       char *grname;
+       size_t bufsize;
+       int ret;
+
+       bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
+       if (bufsize == -1)
+               bufsize = 1024;
+
+       buf = malloc(bufsize);
+       if (!buf)
+               return NULL;
+
+       ret = getgrgid_r(getegid(), &grent, buf, bufsize, &grentp);
+       if (!grentp) {
+               if (ret == 0)
+                       WARN("Could not find matched group record");
 
-       result = getgrgid(getegid());
-       if (!result)
+               ERROR("Failed to get group record - %u", getegid());
+               free(buf);
                return NULL;
+       }
+
+       grname = strdup(grent.gr_name);
+       free(buf);
 
-       return strdup(result->gr_name);
+       return grname;
 }
 
 /* not thread-safe, do not use from api without first forking */
index 032c4310b077b283a4789e623e274960d6d11964..3b9a3eb5413c9e7a15d264940b2c368a7a17be94 100644 (file)
@@ -152,7 +152,10 @@ static char **get_groupnames(void)
        gid_t *group_ids;
        int ret, i;
        char **groupnames;
-       struct group *gr;
+       struct group grent;
+       struct group *grentp = NULL;
+       char *buf;
+       size_t bufsize;
 
        ngroups = getgroups(0, NULL);
        if (ngroups < 0) {
@@ -188,28 +191,48 @@ static char **get_groupnames(void)
                return NULL;
        }
 
+       bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
+       if (bufsize == -1)
+               bufsize = 1024;
+
+       buf = malloc(bufsize);
+       if (!buf) {
+               free(group_ids);
+               free_groupnames(groupnames);
+               usernic_error("Failed to allocate memory while getting group "
+                             "names: %s\n",
+                             strerror(errno));
+               return NULL;
+       }
+
        memset(groupnames, 0, sizeof(char *) * (ngroups + 1));
 
        for (i = 0; i < ngroups; i++) {
-               gr = getgrgid(group_ids[i]);
-               if (!gr) {
-                       usernic_error("Failed to get group name: %s\n",
-                                     strerror(errno));
+               ret = getgrgid_r(group_ids[i], &grent, buf, bufsize, &grentp);
+               if (!grentp) {
+                       if (ret == 0)
+                               usernic_error("%s", "Could not find matched group record\n");
+
+                       usernic_error("Failed to get group name: %s(%u)\n",
+                             strerror(errno), group_ids[i]);
+                       free(buf);
                        free(group_ids);
                        free_groupnames(groupnames);
                        return NULL;
                }
 
-               groupnames[i] = strdup(gr->gr_name);
+               groupnames[i] = strdup(grent.gr_name);
                if (!groupnames[i]) {
                        usernic_error("Failed to copy group name \"%s\"",
-                                     gr->gr_name);
+                                     grent.gr_name);
+                       free(buf);
                        free(group_ids);
                        free_groupnames(groupnames);
                        return NULL;
                }
        }
 
+       free(buf);
        free(group_ids);
 
        return groupnames;