[lxc-devel] [lxc/master] lxc_user_nic: fixes

brauner on Github lxc-bot at linuxcontainers.org
Tue Apr 7 19:29:42 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 379 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200407/e6b30a2a/attachment.bin>
-------------- next part --------------
From c0408754ae4a981c4671f679e6c90a8c04280d9a Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 7 Apr 2020 21:28:17 +0200
Subject: [PATCH 1/2] lxc_user_nic: simplify group retrieval

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/cmd/lxc_user_nic.c | 47 ++++++++++----------------------------
 1 file changed, 12 insertions(+), 35 deletions(-)

diff --git a/src/lxc/cmd/lxc_user_nic.c b/src/lxc/cmd/lxc_user_nic.c
index fd34559031..cb5d1d2be0 100644
--- a/src/lxc/cmd/lxc_user_nic.c
+++ b/src/lxc/cmd/lxc_user_nic.c
@@ -133,26 +133,14 @@ static char *get_username(void)
 	return strdup(pwent.pw_name);
 }
 
-static void free_groupnames(char **groupnames)
-{
-	int i;
-
-	if (!groupnames)
-		return;
-
-	for (i = 0; groupnames[i]; i++)
-		free(groupnames[i]);
-
-	free(groupnames);
-}
 
 static char **get_groupnames(void)
 {
 	__do_free char *buf = NULL;
 	__do_free gid_t *group_ids = NULL;
+	__do_free_string_list char **groupnames = NULL;
 	int ngroups;
 	int ret, i;
-	char **groupnames;
 	struct group grent;
 	struct group *grentp = NULL;
 	size_t bufsize;
@@ -161,10 +149,11 @@ static char **get_groupnames(void)
 	if (ngroups < 0) {
 		CMD_SYSERROR("Failed to get number of groups the user belongs to\n");
 		return NULL;
-	} else if (ngroups == 0) {
-		return NULL;
 	}
 
+	if (ngroups == 0)
+		return NULL;
+
 	group_ids = malloc(sizeof(gid_t) * ngroups);
 	if (!group_ids) {
 		CMD_SYSERROR("Failed to allocate memory while getting groups the user belongs to\n");
@@ -177,44 +166,36 @@ static char **get_groupnames(void)
 		return NULL;
 	}
 
-	groupnames = malloc(sizeof(char *) * (ngroups + 1));
+	groupnames = zalloc(sizeof(char *) * (ngroups + 1));
 	if (!groupnames) {
 		CMD_SYSERROR("Failed to allocate memory while getting group names\n");
 		return NULL;
 	}
 
-	memset(groupnames, 0, sizeof(char *) * (ngroups + 1));
-
 	bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
 	if (bufsize == -1)
 		bufsize = 1024;
 
 	buf = malloc(bufsize);
 	if (!buf) {
-		free_groupnames(groupnames);
 		CMD_SYSERROR("Failed to allocate memory while getting group names\n");
 		return NULL;
 	}
 
 	for (i = 0; i < ngroups; i++) {
 		while ((ret = getgrgid_r(group_ids[i], &grent, buf, bufsize, &grentp)) == ERANGE) {
+			char *new_buf;
+
 			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);
+				usernic_error("Failed to get group members: %u\n", group_ids[i]);
 				return NULL;
 			}
-			char *new_buf = realloc(buf, bufsize);
+
+			new_buf = realloc(buf, bufsize);
 			if (!new_buf) {
-				usernic_error("Failed to allocate memory while getting group "
-					      "names: %s\n",
+				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;
@@ -224,14 +205,12 @@ static char **get_groupnames(void)
 				usernic_error("%s", "Could not find matched group record\n");
 
 			CMD_SYSERROR("Failed to get group name: %u\n", group_ids[i]);
-			free_groupnames(groupnames);
 			return NULL;
 		}
 
 		groupnames[i] = strdup(grent.gr_name);
 		if (!groupnames[i]) {
 			usernic_error("Failed to copy group name \"%s\"", grent.gr_name);
-			free_groupnames(groupnames);
 			return NULL;
 		}
 	}
@@ -325,9 +304,9 @@ static int get_alloted(char *me, char *intype, char *link,
 {
 	__do_free char *line = NULL;
 	__do_fclose FILE *fin = NULL;
+	__do_free_string_list char **groups = NULL;
 	int n, ret;
 	char name[100], type[100], br[100];
-	char **groups;
 	int count = 0;
 	size_t len = 0;
 
@@ -379,8 +358,6 @@ static int get_alloted(char *me, char *intype, char *link,
 		count += n;
 	}
 
-	free_groupnames(groups);
-
 	/* Now return the total number of nics that this user can create. */
 	return count;
 }

From 6b533a4acfd751bb850135a9bb05d27d64b70f93 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 7 Apr 2020 21:28:32 +0200
Subject: [PATCH 2/2] lxc_user_nic: continue when we failed to find a group

Closes #3361.
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/cmd/lxc_user_nic.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/src/lxc/cmd/lxc_user_nic.c b/src/lxc/cmd/lxc_user_nic.c
index cb5d1d2be0..ad46af35c9 100644
--- a/src/lxc/cmd/lxc_user_nic.c
+++ b/src/lxc/cmd/lxc_user_nic.c
@@ -200,13 +200,10 @@ static char **get_groupnames(void)
 			}
 			buf = new_buf;
 		}
-		if (!grentp) {
-			if (ret == 0)
-				usernic_error("%s", "Could not find matched group record\n");
 
-			CMD_SYSERROR("Failed to get group name: %u\n", group_ids[i]);
-			return NULL;
-		}
+		/* If a group is not found, just ignore it. */
+		if (!grentp)
+			continue;
 
 		groupnames[i] = strdup(grent.gr_name);
 		if (!groupnames[i]) {


More information about the lxc-devel mailing list