Hello!

In coreutils/id.c get_groups() gets groups list for further printing out
on screen. According to id's main function - return value from
get_groups is expected even < 0 in order to extend (xrealloc) the
list size for the groups in case if there's more than 64:
>     123 int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
>     124 int id_main(int argc UNUSED_PARAM, char **argv)
>     125 {
...  
>     180                 n = 64;
>     181                 if (get_groups(username, rgid, groups, &n) < 0) {
>     182                         /* Need bigger buffer after all */
>     183                         groups = xrealloc(groups, n * sizeof(gid_t));
>     184                         get_groups(username, rgid, groups, &n);
>     185                 }
>     186                 if (n > 0) {
But get_groups() allowed to return only >=0 value:
>     96 static int get_groups(const char *username, gid_t rgid, gid_t *groups, 
> int *n)
...
>     118         if (*n < 0)
>     119                 return 0; /* error, don't return < 0! */
>     120         return m;
>     121 }
And as a result - no way to get more than 64 groups, it goes strait to the 
bb_err_msg:
>     196                 } else if (n < 0) { /* error in get_groups() */
>     197                         if (ENABLE_DESKTOP)
>     198                                 bb_error_msg_and_die("can't get 
> groups");
>     199                         return EXIT_FAILURE;
>     200                 }
Is there any reason for such kind of restriction in get_groups? I'm suggesting
to substitute this restriction with the call to "getgroups(0, groups)", to get 
an 
actual amount of groups before returning -1. In this case we're calling 
getgroups()
3 times. But only if we have more than 64 groups, it's a rare case. Another way 
-
is to call getgroups() twice but always, first time to get amount of groups and
then the actual groups list. I guess to call three times but in rare cases is
better than twice but always.

Patch is in attachment:

>From 3a99379834788c6169a7dd992473524c9652e6a4 Mon Sep 17 00:00:00 2001

Alexey Fomenko (1):
  id: fix return value when trying to get more than 64 groups

 coreutils/id.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

-- 
1.7.3.4

Br,
Alex F
>From 3a99379834788c6169a7dd992473524c9652e6a4 Mon Sep 17 00:00:00 2001
From: Alexey Fomenko <[email protected]>
Date: Mon, 13 Jun 2011 14:55:10 +0300
Subject: [PATCH] id: fix return value when trying to get more than 64 groups

Signed-off-by: Alexey Fomenko <[email protected]>
---
 coreutils/id.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/coreutils/id.c b/coreutils/id.c
index aa6ae47..dd29fe6 100644
--- a/coreutils/id.c
+++ b/coreutils/id.c
@@ -115,8 +115,9 @@ static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
 		m = - (nn > *n);
 		*n = nn;
 	}
-	if (*n < 0)
-		return 0; /* error, don't return < 0! */
+	if (*n < 0) {
+		*n = getgroups(0, groups); /* error, get the actual amount ot groups */
+	}
 	return m;
 }
 
-- 
1.7.3.4

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to