The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxc/pull/3024

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Hello.
If group has a lot of members getgrgid_r fails with ERANGE if buffer is too small. 
I added retry with a larger buffer and buffer limit.

Please backport this to lxc-2.0.11 release.
Thanks.

Signed-off-by: Alexander Kriventsov <[email protected]>
From b9f80409d70ea0ff6d8dcf028aa701bee5bd48d7 Mon Sep 17 00:00:00 2001
From: Alexander Kriventsov <[email protected]>
Date: Mon, 3 Jun 2019 18:11:56 +0300
Subject: [PATCH] getgrgid_r fails with ERANGE if buffer is too small. Retry
 with a larger buffer.

Signed-off-by: Alexander Kriventsov <[email protected]>
---
 src/lxc/cmd/lxc_user_nic.c | 23 ++++++++++++++++++++++-
 src/lxc/utils.h            |  4 ++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/lxc/cmd/lxc_user_nic.c b/src/lxc/cmd/lxc_user_nic.c
index 84823bd5d8..40c9aa9d70 100644
--- a/src/lxc/cmd/lxc_user_nic.c
+++ b/src/lxc/cmd/lxc_user_nic.c
@@ -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");
diff --git a/src/lxc/utils.h b/src/lxc/utils.h
index 747e14b6ef..9f1c21dddb 100644
--- a/src/lxc/utils.h
+++ b/src/lxc/utils.h
@@ -26,6 +26,10 @@
 /* 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>
_______________________________________________
lxc-devel mailing list
[email protected]
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to