]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Fixed memory checks and faulty loop in get_alloted according to comments
authorHenrik Kjölhede <hkjolhede@gmail.com>
Tue, 9 Jun 2015 20:25:16 +0000 (22:25 +0200)
committerHenrik Kjölhede <hkjolhede@gmail.com>
Tue, 9 Jun 2015 20:25:16 +0000 (22:25 +0200)
Signed-off-by: Henrik Kjölhede <hkjolhede@gmail.com>
doc/lxc-usernet.sgml.in
src/lxc/lxc_user_nic.c

index aa9340798bd0764a9ffd189eab399e1bbd575178..8d3e9eb2cd0a04c0f32d12f7087aa819f97004fb 100644 (file)
@@ -133,12 +133,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
       </variablelist>
 
       <para>
-        Since a user can be be specified both by username as well as one or 
-        more usergroups, it is possible that several configuration lines 
-        enable that user to create network interfaces. In such cases, any 
-        interfaces create are counted towards the quotas of the user or group 
-        in the order in which they appear in the file. If the quota of one 
-        line is full, the rest will be parsed until one is found or the end of 
+        Since a user can be be specified both by username as well as one or
+        more usergroups, it is possible that several configuration lines
+        enable that user to create network interfaces. In such cases, any
+        interfaces create are counted towards the quotas of the user or group
+        in the order in which they appear in the file. If the quota of one
+        line is full, the rest will be parsed until one is found or the end of
         the file.
       </para>
     </refsect2>
index 49d1ed75aa273c3aee73c84fc7a26b4a251b09e6..ca4bea57b7bbdfc0b334b07e021fd3accc192437 100644 (file)
@@ -97,42 +97,63 @@ static char *get_username(void)
        return pwd->pw_name;
 }
 
+static void free_groupnames(char **groupnames)
+{
+       char **group;
+       for (group=groupnames; group != NULL; group++)
+               free(*group);
+       free(groupnames);
+}
+
 static char **get_groupnames(void)
 {
        int ngroups;
        gid_t *group_ids;
-       int ret, i, j;
+       int ret, i;
        char **groupnames;
        struct group *gr;
 
        ngroups = getgroups(0, NULL);
 
        if (ngroups == -1) {
-               fprintf(stderr, "Failed to get number of groups user belongs to\n");
+               fprintf(stderr, "Failed to get number of groups user belongs to: %s\n", strerror(errno));
                return NULL;
        }
+       if (ngroups == 0)
+               return NULL;
 
        group_ids = (gid_t *)malloc(sizeof(gid_t)*ngroups);
+
+       if (group_ids == NULL) {
+               fprintf(stderr, "Out of memory while getting groups the user belongs to\n");
+               return NULL;
+       }
+
        ret = getgroups(ngroups, group_ids);
 
        if (ret < 0) {
                free(group_ids);
-               fprintf(stderr, "Failed to get process groups\n");
+               fprintf(stderr, "Failed to get process groups: %s\n", strerror(errno));
                return NULL;
        }
 
        groupnames = (char **)malloc(sizeof(char *)*(ngroups+1));
 
+       if (groupnames == NULL) {
+               free(group_ids);
+               fprintf(stderr, "Out of memory while getting group names\n");
+               return NULL;
+       }
+
+       memset(groupnames, 0, sizeof(char *)*(ngroups+1));
+
        for (i=0; i<ngroups; i++ ) {
                gr = getgrgid(group_ids[i]);
 
                if (gr == NULL) {
                        fprintf(stderr, "Failed to get group name\n");
                        free(group_ids);
-                       for (j=0; j<i; j++) {
-                               free(groupnames[j]);
-                       }
-                       free(groupnames);
+                       free_groupnames(groupnames);
                        return NULL;
                }
 
@@ -141,29 +162,16 @@ static char **get_groupnames(void)
                if (groupnames[i] == NULL) {
                        fprintf(stderr, "Failed to copy group name: %s", gr->gr_name);
                        free(group_ids);
-                       for (j=0; j<i; j++) {
-                               free(groupnames[j]);
-                       }
-                       free(groupnames);
+                       free_groupnames(groupnames);
                        return NULL;
                }
        }
 
-       groupnames[ngroups] = NULL;
-
        free(group_ids);
 
        return groupnames;
 }
 
-static void free_groupnames(char **groupnames)
-{
-       char **group;
-       for (group=groupnames; group != NULL; group++)
-               free(*group);
-       free(groupnames);
-}
-
 static bool name_is_in_groupnames(char *name, char **groupnames)
 {
        while (groupnames != NULL) {
@@ -197,6 +205,12 @@ static struct alloted_s *append_alloted(struct alloted_s **head, char *name, int
        }
 
        al->name = strdup(name);
+
+       if (al->name == NULL) {
+               free(al);
+               return NULL;
+       }
+
        al->allowed = n;
        al->next = NULL;
 
@@ -283,12 +297,13 @@ static int get_alloted(char *me, char *intype, char *link, struct alloted_s **al
                 */
                append_alloted(alloted, name, n);
                count += n;
-               break;
        }
 
        free_groupnames(groups);
        fclose(fin);
        free(line);
+
+       // now return the total number of nics that this user can create
        return count;
 }