Re: [patch] POSIX-compliant handling of getpwnam_r()/getgrnam_r() behavior

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mar 28, 2013, at 17:01 , Jan Friesse <jfriesse@xxxxxxxxxx> wrote:

> Andrei,
> thanks for patch. Sadly, this part was changed in
> 52f88d04eaf2ad6c65df34a3401417d0583c6a45 (and I believe error you are
> pointing out is still existing there). Can you please send a patch
> applied on master git version?

Sure, please find it attached.

By the way, current code seems to be incorrect: using errno
with getpwnam_r()/getgrnam_r() functions is meaningless
(patch fixed this also).


> 
> Thanks,
>  Honza
> 
> Andrei Belov napsal(a):
>> Hello corosync users,
>> 
>> I've spent some time building and trying corosync 2.3.0 on SunOS
>> (Joyent's SmartOS-based VM), and noticed one issue with uidgid {}
>> configuration blocks parsing, e.g.:
>> 
>> uidgid {
>>        uid: user|uid
>>        gid: group|gid
>> }
>> 
>> Corosync won't display any errors in case of non-existent username/uid
>> or groupname/gid in configuration, and then we'll get something like
>> that:
>> 
>> # corosync-cmapctl uidgid
>> uidgid.gid.4096 (u8) = 1
>> uidgid.uid.4292862832 (u8) = 1
>> 
>> I'm attaching the patch that fixes this issue by performing additional
>> checks according to the following POSIX documents:
>> 
>> http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwnam_r.html
>> http://pubs.opengroup.org/onlinepubs/9699919799/functions/getgrnam_r.html
>> 
>> [..]
>> A null pointer is returned at the location pointed to by result on error or if the requested entry is not found.
>> [..]
>> 
>> 
>> Hope this would be helpful for someone.
>> 
>> Best regards,
>> Andrei.


From 2a38326ab171caf66e490893d7633938cc310c42 Mon Sep 17 00:00:00 2001
From: Andrei Belov <defanator@xxxxxxxxx>
Date: Thu, 28 Mar 2013 14:24:41 +0000
Subject: Improved POSIX-compliant handling of getpwnam_r() and getgrnam_r().

---
 exec/coroparse.c | 56 ++++++++++++++++++++++++++++++++------------------------
 1 file changed, 32 insertions(+), 24 deletions(-)

diff --git a/exec/coroparse.c b/exec/coroparse.c
index a1fd315..9439649 100644
--- a/exec/coroparse.c
+++ b/exec/coroparse.c
@@ -130,7 +130,7 @@ static int uid_determine (const char *req_user)
 	struct passwd* pwdptr = &passwd;
 	struct passwd* temp_pwd_pt;
 	char *pwdbuffer;
-	int  pwdlinelen;
+	int  pwdlinelen, rc;
 
 	pwdlinelen = sysconf (_SC_GETPW_R_SIZE_MAX);
 
@@ -140,20 +140,24 @@ static int uid_determine (const char *req_user)
 
 	pwdbuffer = malloc (pwdlinelen);
 
-	while ((getpwnam_r (req_user, pwdptr, pwdbuffer, pwdlinelen, &temp_pwd_pt)) != 0) {
-		if (errno == ERANGE) {
-			char *n;
+	while ((rc = getpwnam_r (req_user, pwdptr, pwdbuffer, pwdlinelen, &temp_pwd_pt)) == ERANGE) {
+		char *n;
 
-			pwdlinelen *= 2;
-			if (pwdlinelen <= 32678) {
-				n = realloc (pwdbuffer, pwdlinelen);
-				if (n != NULL) {
-					pwdbuffer = n;
-					continue;
-				}
+		pwdlinelen *= 2;
+		if (pwdlinelen <= 32678) {
+			n = realloc (pwdbuffer, pwdlinelen);
+			if (n != NULL) {
+				pwdbuffer = n;
+				continue;
 			}
 		}
-
+	}
+	if (rc != 0) {
+		free (pwdbuffer);
+	        sprintf (error_string_response, "getpwnam_r(): %s", strerror(rc));
+	        return (-1);
+	}
+	if (temp_pwd_pt == NULL) {
 		free (pwdbuffer);
 	        sprintf (error_string_response,
 	                "The '%s' user is not found in /etc/passwd, please read the documentation.",
@@ -173,7 +177,7 @@ static int gid_determine (const char *req_group)
 	struct group * grpptr = &group;
 	struct group * temp_grp_pt;
 	char *grpbuffer;
-	int  grplinelen;
+	int  grplinelen, rc;
 
 	grplinelen = sysconf (_SC_GETGR_R_SIZE_MAX);
 
@@ -183,20 +187,24 @@ static int gid_determine (const char *req_group)
 
 	grpbuffer = malloc (grplinelen);
 
-	while ((getgrnam_r (req_group, grpptr, grpbuffer, grplinelen, &temp_grp_pt)) != 0) {
-		if (errno == ERANGE) {
-			char *n;
+	while ((rc = getgrnam_r (req_group, grpptr, grpbuffer, grplinelen, &temp_grp_pt)) == ERANGE) {
+		char *n;
 
-			grplinelen *= 2;
-			if (grplinelen <= 32678) {
-				n = realloc (grpbuffer, grplinelen);
-				if (n != NULL) {
-					grpbuffer = n;
-					continue;
-				}
+		grplinelen *= 2;
+		if (grplinelen <= 32678) {
+			n = realloc (grpbuffer, grplinelen);
+			if (n != NULL) {
+				grpbuffer = n;
+				continue;
 			}
 		}
-
+	}
+	if (rc != 0) {
+		free (grpbuffer);
+	        sprintf (error_string_response, "getgrnam_r(): %s", strerror(rc));
+	        return (-1);
+	}
+	if (temp_grp_pt == NULL) {
 		free (grpbuffer);
 	        sprintf (error_string_response,
 	                "The '%s' group is not found in /etc/group, please read the documentation.",
-- 
1.8.0.1

_______________________________________________
discuss mailing list
discuss@xxxxxxxxxxxx
http://lists.corosync.org/mailman/listinfo/discuss

[Index of Archives]     [Linux Clusters]     [Corosync Project]     [Linux USB Devel]     [Linux Audio Users]     [Photo]     [Yosemite News]    [Yosemite Photos]    [Linux Kernel]     [Linux SCSI]     [X.Org]

  Powered by Linux