Skip to contentSkip to conversation

libsemanage: genhomedircon - improve handling large groups

PATCH Superseded Petr Lautrbach 2 participants 6 messages Feb 6, 2019 – Feb 15, 2019 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsemanage: genhomedircon - improve handling large groups

getgrnam_r() uses a preallocated buffer to store a structure containing the broken-out fields of the record in the group database. The size of this buffer is usually sysconf(_SC_GETGR_R_SIZE_MAX) == 1024 and it is not enough for groups with a large number of users. In these cases, getgrnam_r() returns -1 and sets errno to ERANGE and the caller can retry with a larger buffer.

Fixes:

$ semanage login -a -s user_u -r s0-s0:c1.c2 '%largegroup'
libsemanage.semanage_direct_commit: semanage_genhomedircon returned error code -1. (Numerical result out of range).
OSError: Numerical result out of range
---
 libsemanage/src/genhomedircon.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
libsemanage/src/genhomedircon.c +13 -3
index 591941fb..ac376671 100644
@@ -1077,10 +1077,20 @@ static int get_group_users(genhomedircon_settings_t * s,
 
 	const char *grname = selogin + 1;
 
-	if (getgrnam_r(grname, &grstorage, grbuf,
-			(size_t) grbuflen, &group) != 0) {
-		goto cleanup;
+	errno = 0;
+	while (
+		(retval = getgrnam_r(grname, &grstorage, grbuf, (size_t) grbuflen, &group)) != 0 &&
+		errno == ERANGE
+	) {
+		char *new_grbuf;
+		grbuflen *= 2;
+		new_grbuf = realloc(grbuf, grbuflen);
+		if (new_grbuf == NULL)
+			goto cleanup;
+		grbuf = new_grbuf;
 	}
+	if (retval == -1)
+		goto cleanup;
 
 	if (group == NULL) {
 		ERR(s->h_semanage, "Can't find group named %s\n", grname);
Signature · 1 line
2.20.1
Acked-by on v1 1/1
#1 lore raw
Quoted 31 lines · Petr Lautrbach

getgrnam_r() uses a preallocated buffer to store a structure containing the broken-out fields of the record in the group database. The size of this buffer is usually sysconf(_SC_GETGR_R_SIZE_MAX) == 1024 and it is not enough for groups with a large number of users. In these cases, getgrnam_r() returns -1 and sets errno to ERANGE and the caller can retry with a larger buffer.

Fixes:

$ semanage login -a -s user_u -r s0-s0:c1.c2 '%largegroup'
libsemanage.semanage_direct_commit: semanage_genhomedircon returned error code -1. (Numerical result out of range).
OSError: Numerical result out of range
---
 libsemanage/src/genhomedircon.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
libsemanage/src/genhomedircon.c +2 -3
index 591941fb..ac376671 100644
@@ -1077,10 +1077,20 @@ static int get_group_users(genhomedircon_settings_t * s,
 
        const char *grname = selogin + 1;
 
-       if (getgrnam_r(grname, &grstorage, grbuf,
-                       (size_t) grbuflen, &group) != 0) {
-               goto cleanup;
+       errno = 0;
+       while (

libsemanage/src/genhomedircon.c static int get_group_users(genhomedircon_settings_t * s, · line 1091

Quoted 10 lines
libsemanage/src/genhomedircon.c +9 -0
+               (retval = getgrnam_r(grname, &grstorage, grbuf, (size_t) grbuflen, &group)) != 0 &&
+               errno == ERANGE
+       ) {
+               char *new_grbuf;
+               grbuflen *= 2;
+               new_grbuf = realloc(grbuf, grbuflen);
+               if (new_grbuf == NULL)
+                       goto cleanup;
+               grbuf = new_grbuf;
        }

Hello,
When reading this for loop, I am concerned about the case where the member list exceeds 2Go on a system with a 32-bit CPU (where sizeof(long) = 4). Even if it seems very unlikely, if this ever happens, the loop will become infinite. Would you agree with adding "&& grbuflen > 0" to the condition of the while statement?

Anyway, if you do not agree, this patch looks good to me.

Thanks,
Nicolas

Quoted 8 lines
libsemanage/src/genhomedircon.c +2 -1
+       if (retval == -1)
+               goto cleanup;
 
        if (group == NULL) {
                ERR(s->h_semanage, "Can't find group named %s\n", grname);
--

2.20.1

#2 lore raw
Quoted 43 lines · Nicolas Iooss
Quoted 42 lines · Petr Lautrbach

getgrnam_r() uses a preallocated buffer to store a structure containing the broken-out fields of the record in the group database. The size of this buffer is usually sysconf(_SC_GETGR_R_SIZE_MAX) == 1024 and it is not enough for groups with a large number of users. In these cases, getgrnam_r() returns -1 and sets errno to ERANGE and the caller can retry with a larger buffer.

Fixes:

$ semanage login -a -s user_u -r s0-s0:c1.c2 '%largegroup'
libsemanage.semanage_direct_commit: semanage_genhomedircon returned error code -1. (Numerical result out of range).
OSError: Numerical result out of range
---
 libsemanage/src/genhomedircon.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
libsemanage/src/genhomedircon.c +3 -3
index 591941fb..ac376671 100644
@@ -1077,10 +1077,20 @@ static int 
 get_group_users(genhomedircon_settings_t * s,
 
        const char *grname = selogin + 1;
 
-       if (getgrnam_r(grname, &grstorage, grbuf,
-                       (size_t) grbuflen, &group) != 0) {
-               goto cleanup;
+       errno = 0;
+       while (
+               (retval = getgrnam_r(grname, &grstorage, grbuf, 

(size_t) grbuflen, &group)) != 0 &&

libsemanage/src/genhomedircon.c +8 -0
+               errno == ERANGE
+       ) {
+               char *new_grbuf;
+               grbuflen *= 2;
+               new_grbuf = realloc(grbuf, grbuflen);
+               if (new_grbuf == NULL)
+                       goto cleanup;
+               grbuf = new_grbuf;
Quoted 8 lines
Quoted 1 line
        }

Hello,
When reading this for loop, I am concerned about the case where the member list exceeds 2Go on a system with a 32-bit CPU (where sizeof(long) = 4). Even if it seems very unlikely, if this ever happens, the loop will become infinite. Would you agree with adding "&& grbuflen > 0" to the condition of the while statement?

Makes sense, I'll add it. Thanks.

Quoted 8 lines

Anyway, if you do not agree, this patch looks good to me.

Thanks,
Nicolas

Quoted 2 lines
libsemanage/src/genhomedircon.c +2 -0
+       if (retval == -1)
+               goto cleanup;

According to the man page this check is not correct. - "In case of error, an error number is returned, and NULL is stored in *result."

I'll fix it as well.

Quoted 6 lines
Quoted 6 lines
        if (group == NULL) {
                ERR(s->h_semanage, "Can't find group named 
                %s\n", grname);

--
2.20.1

#3 lore raw

PATCH v2 1/1 libsemanage: genhomedircon - improve handling large groups

getgrnam_r() uses a preallocated buffer to store a structure containing the broken-out fields of the record in the group database. The size of this buffer is usually sysconf(_SC_GETGR_R_SIZE_MAX) == 1024 and it is not enough for groups with a large number of users. In these cases, getgrnam_r() returns -1 and sets errno to ERANGE and the caller can retry with a larger buffer.

Fixes:

$ semanage login -a -s user_u -r s0-s0:c1.c2 '%largegroup'
libsemanage.semanage_direct_commit: semanage_genhomedircon returned error code -1. (Numerical result out of range).
OSError: Numerical result out of range
---
 libsemanage/src/genhomedircon.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
libsemanage/src/genhomedircon.c +17 -3
index 591941fb..e5f8d371 100644
@@ -1077,10 +1077,24 @@ static int get_group_users(genhomedircon_settings_t * s,
 
 	const char *grname = selogin + 1;
 
-	if (getgrnam_r(grname, &grstorage, grbuf,
-			(size_t) grbuflen, &group) != 0) {
-		goto cleanup;
+	errno = 0;
+	while (
+		(retval = getgrnam_r(grname, &grstorage, grbuf, (size_t) grbuflen, &group)) != 0 &&
+		errno == ERANGE
+	) {
+		char *new_grbuf;
+		grbuflen *= 2;
+		if (grbuflen < 0)
+			/* the member list could exceed 2Gb on a system with a 32-bit CPU (where
+			 * sizeof(long) = 4) - if this ever happened, the loop would become infinite. */
+			goto cleanup;
+		new_grbuf = realloc(grbuf, grbuflen);
+		if (new_grbuf == NULL)
+			goto cleanup;
+		grbuf = new_grbuf;
 	}
+	if (retval != 0)
+		goto cleanup;
 
 	if (group == NULL) {
 		ERR(s->h_semanage, "Can't find group named %s\n", grname);
Signature · 1 line
2.20.1
Acked-by on v2 1/1
#4 lore raw
Quoted 8 lines · Petr Lautrbach

getgrnam_r() uses a preallocated buffer to store a structure containing the broken-out fields of the record in the group database. The size of this buffer is usually sysconf(_SC_GETGR_R_SIZE_MAX) == 1024 and it is not enough for groups with a large number of users. In these cases, getgrnam_r() returns -1 and sets errno to ERANGE and the caller can retry with a larger buffer.

Quoted 6 lines

Fixes:

$ semanage login -a -s user_u -r s0-s0:c1.c2 '%largegroup'
libsemanage.semanage_direct_commit: semanage_genhomedircon returned error code -1. (Numerical result out of range).
OSError: Numerical result out of range
Quoted 39 lines
---
 libsemanage/src/genhomedircon.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
libsemanage/src/genhomedircon.c +17 -4
index 591941fb..e5f8d371 100644
@@ -1077,10 +1077,24 @@ static int get_group_users(genhomedircon_settings_t * s,
 
        const char *grname = selogin + 1;
 
-       if (getgrnam_r(grname, &grstorage, grbuf,
-                       (size_t) grbuflen, &group) != 0) {
-               goto cleanup;
+       errno = 0;
+       while (
+               (retval = getgrnam_r(grname, &grstorage, grbuf, (size_t) grbuflen, &group)) != 0 &&
+               errno == ERANGE
+       ) {
+               char *new_grbuf;
+               grbuflen *= 2;
+               if (grbuflen < 0)
+                       /* the member list could exceed 2Gb on a system with a 32-bit CPU (where
+                        * sizeof(long) = 4) - if this ever happened, the loop would become infinite. */
+                       goto cleanup;
+               new_grbuf = realloc(grbuf, grbuflen);
+               if (new_grbuf == NULL)
+                       goto cleanup;
+               grbuf = new_grbuf;
        }
+       if (retval != 0)
+               goto cleanup;
 
        if (group == NULL) {
                ERR(s->h_semanage, "Can't find group named %s\n", grname);
--

2.20.1

#5 lore raw
Quoted 9 lines · Nicolas Iooss
Quoted 8 lines · Petr Lautrbach

getgrnam_r() uses a preallocated buffer to store a structure containing the broken-out fields of the record in the group database. The size of this buffer is usually sysconf(_SC_GETGR_R_SIZE_MAX) == 1024 and it is not enough for groups with a large number of users. In these cases, getgrnam_r() returns -1 and sets errno to ERANGE and the caller can retry with a larger buffer.

Quoted 8 lines
Quoted 6 lines

Fixes:

$ semanage login -a -s user_u -r s0-s0:c1.c2 '%largegroup'
libsemanage.semanage_direct_commit: semanage_genhomedircon returned error code -1. (Numerical result out of range).
OSError: Numerical result out of range

Merged.

Quoted 39 lines
Quoted 39 lines
---
 libsemanage/src/genhomedircon.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
libsemanage/src/genhomedircon.c +17 -4
index 591941fb..e5f8d371 100644
@@ -1077,10 +1077,24 @@ static int get_group_users(genhomedircon_settings_t * s,
 
        const char *grname = selogin + 1;
 
-       if (getgrnam_r(grname, &grstorage, grbuf,
-                       (size_t) grbuflen, &group) != 0) {
-               goto cleanup;
+       errno = 0;
+       while (
+               (retval = getgrnam_r(grname, &grstorage, grbuf, (size_t) grbuflen, &group)) != 0 &&
+               errno == ERANGE
+       ) {
+               char *new_grbuf;
+               grbuflen *= 2;
+               if (grbuflen < 0)
+                       /* the member list could exceed 2Gb on a system with a 32-bit CPU (where
+                        * sizeof(long) = 4) - if this ever happened, the loop would become infinite. */
+                       goto cleanup;
+               new_grbuf = realloc(grbuf, grbuflen);
+               if (new_grbuf == NULL)
+                       goto cleanup;
+               grbuf = new_grbuf;
        }
+       if (retval != 0)
+               goto cleanup;
 
        if (group == NULL) {
                ERR(s->h_semanage, "Can't find group named %s\n", grname);
--

2.20.1