]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
getgrgid_r fails with ERANGE if buffer is too small. Retry with a larger buffer. 3024/head
authorAlexander Kriventsov <akriventsov@nic.ru>
Mon, 3 Jun 2019 15:11:56 +0000 (18:11 +0300)
committerAlexander Kriventsov <akriventsov@nic.ru>
Mon, 3 Jun 2019 15:11:56 +0000 (18:11 +0300)
Signed-off-by: Alexander Kriventsov <akriventsov@nic.ru>
src/lxc/cmd/lxc_user_nic.c
src/lxc/utils.h

index 84823bd5d8f8e4b2f27f94f7b2ce97a73c3e434a..40c9aa9d704329e688189d60e7242628f019a198 100644 (file)
@@ -206,7 +206,28 @@ static char **get_groupnames(void)
        }
 
        for (i = 0; i < ngroups; i++) {
-               ret = getgrgid_r(group_ids[i], &grent, buf, bufsize, &grentp);
+               while ((ret = getgrgid_r(group_ids[i], &grent, buf, bufsize, &grentp)) == ERANGE) {
+                       bufsize <<= 1;
+                       if (bufsize > MAX_GRBUF_SIZE) {
+                               usernic_error("Failed to get group members: %u\n",
+                                     group_ids[i]);
+                               free(buf);
+                               free(group_ids);
+                               free_groupnames(groupnames);
+                               return NULL;
+                       }
+                       char *new_buf = realloc(buf, bufsize);
+                       if (!new_buf) {
+                               usernic_error("Failed to allocate memory while getting group "
+                                             "names: %s\n",
+                                             strerror(errno));
+                               free(buf);
+                               free(group_ids);
+                               free_groupnames(groupnames);
+                               return NULL;
+                       }
+                       buf = new_buf;
+               }
                if (!grentp) {
                        if (ret == 0)
                                usernic_error("%s", "Could not find matched group record\n");
index 747e14b6ef92ae069d0c49d6aedc8d22988d9927..9f1c21dddbf558f8a0853db8ffa78b22ff77b2ab 100644 (file)
 /* Properly support loop devices on 32bit systems. */
 #define _FILE_OFFSET_BITS 64
 
+#ifndef MAX_GRBUF_SIZE
+#define MAX_GRBUF_SIZE 65536
+#endif
+
 #include <errno.h>
 #include <linux/loop.h>
 #include <linux/types.h>