libsemanage: Always set errno to 0 before calling getpwent()
PATCH 1/1 libsemanage: Always set errno to 0 before calling getpwent()
From: Laurent Bigonville <[email protected]>
The manpage explicitly states that:
The getpwent() function returns a pointer to a passwd structure, or NULL if there are no more entries or an error occurred. If an error occurs, errno is set appropriately. If one wants to check errno after the call, it should be set to zero before the call.
Without this, genhomedircon can wrongly return the following:
libsemanage.get_home_dirs: Error while fetching users. Returning list so far.
https://github.com/SELinuxProject/selinux/issues/121
--- libsemanage/src/genhomedircon.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
libsemanage/src/genhomedircon.c +10 -3
@@ -361,7 +361,11 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) errno =0 ;setpwent (); -while ((pwbuf =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pwbuf =getpwent (); +if (pwbuf ==NULL ) +break ;if (pwbuf->pw_uid < minuid || pwbuf->pw_uid > maxuid)continue ;if (!semanage_list_find (shells, pwbuf->pw_shell))
@@ -403,7 +407,6 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) }free (path); path =NULL ; - errno =0 ; }if (errno) {
@@ -1101,7 +1104,11 @@ static int get_group_users(genhomedircon_settings_t * s, }setpwent (); -while ((pw =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pw =getpwent (); +if (pw ==NULL ) +break ;// skip users who also have this group as their // primary group if (lfind(pw->pw_name, group->gr_mem, &nmembers,
Signature · 1 line
2.20.1
Le 2/01/19 à 14:46, Laurent Bigonville a écrit :
Quoted 37 lines
From: Laurent Bigonville <[email protected]>
The manpage explicitly states that:
The
getpwent()function returns a pointer to a passwd structure, or NULL if there are no more entries or an error occurred. If an error occurs, errno is set appropriately. If one wants to check errno after the call, it should be set to zero before the call.Without this, genhomedircon can wrongly return the following:
libsemanage.get_home_dirs: Error while fetching users. Returning list so far.https://github.com/SELinuxProject/selinux/issues/121
Signed-off-by Laurent Bigonville <[email protected]>--- libsemanage/src/genhomedircon.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)libsemanage/src/genhomedircon.c +5
-1@@ -361,7 +361,11 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) errno =0 ;setpwent (); -while ((pwbuf =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pwbuf =getpwent (); +if (pwbuf ==NULL ) +break ;if (pwbuf->pw_uid < minuid || pwbuf->pw_uid > maxuid)continue ;if (!semanage_list_find (shells, pwbuf->pw_shell))
libsemanage/src/genhomedircon.c static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) · line 406
Quoted 4 lines
libsemanage/src/genhomedircon.c +0
-1@@ -403,7 +407,6 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) }free (path); path =NULL ; - errno =0 ;
Actually I'm wondering if this shouldn't stay there
Quoted 16 lines
libsemanage/src/genhomedircon.c +5
-1}if (errno) {@@ -1101,7 +1104,11 @@ static int get_group_users(genhomedircon_settings_t * s, }setpwent (); -while ((pw =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pw =getpwent (); +if (pw ==NULL ) +break ;// skip users who also have this group as their // primary group if (lfind(pw->pw_name, group->gr_mem, &nmembers,
Quoted 43 lines · Laurent Bigonville
Quoted 42 lines · Laurent Bigonville
From: Laurent Bigonville <[email protected]>
The manpage explicitly states that:
The
getpwent()function returns a pointer to a passwd
structure, or
NULL if there are no more entries or an error occurred. If an
error
occurs, errno is set appropriately. If one wants to check errno
after
the call, it should be set to zero before the call.Without this, genhomedircon can wrongly return the following:
libsemanage.get_home_dirs: Error while fetching
users. Returning list so far.https://github.com/SELinuxProject/selinux/issues/121
Signed-off-by Laurent Bigonville <[email protected]>--- libsemanage/src/genhomedircon.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)libsemanage/src/genhomedircon.c +5
-1@@ -361,7 +361,11 @@ static semanage_list_t *get_home_dirs (genhomedircon_settings_t * s) errno = 0;setpwent (); -while ((pwbuf =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pwbuf =getpwent (); +if (pwbuf ==NULL ) +break ;if (pwbuf->pw_uid < minuid || pwbuf->pw_uid > maxuid)continue ;if (!semanage_list_find(shells,
pwbuf->pw_shell))
Quoted 8 lines
libsemanage/src/genhomedircon.c static semanage_list_t · line 407
Quoted 6 lines
libsemanage/src/genhomedircon.c +0
-1@@ -403,7 +407,6 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) }free (path); path =NULL ; - errno =0 ;Actually I'm wondering if this shouldn't stay there
Why?
Quoted 18 lines
Quoted 18 lines
libsemanage/src/genhomedircon.c +0
-0}if (errno) {libsemanage/src/genhomedircon.c +5
-1@@ -1101,7 +1104,11 @@ static intget_group_users (genhomedircon_settings_t * s, } setpwent(); -while ((pw =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pw =getpwent (); +if (pw ==NULL ) +break ;// skip users who also have this group as their // primary group if (lfind(pw->pw_name,
group->gr_mem, &nmembers,
Le 4/01/19 à 16:11, Stephen Smalley a écrit :
Quoted 51 lines · Laurent Bigonville
Quoted 42 lines · Laurent Bigonville
Quoted 41 lines
From: Laurent Bigonville <[email protected]>
The manpage explicitly states that:
The
getpwent()function returns a pointer to a passwd
structure, or
NULL if there are no more entries or an error occurred. If an
error
occurs, errno is set appropriately. If one wants to check errno
after
the call, it should be set to zero before the call.Without this, genhomedircon can wrongly return the following:
libsemanage.get_home_dirs: Error while fetching
users. Returning list so far.https://github.com/SELinuxProject/selinux/issues/121
Signed-off-by Laurent Bigonville <[email protected]>--- libsemanage/src/genhomedircon.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)libsemanage/src/genhomedircon.c +5
-1@@ -361,7 +361,11 @@ static semanage_list_t *get_home_dirs (genhomedircon_settings_t * s) errno = 0;setpwent (); -while ((pwbuf =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pwbuf =getpwent (); +if (pwbuf ==NULL ) +break ;if (pwbuf->pw_uid < minuid || pwbuf->pw_uid > maxuid)continue ;Quoted 8 lines
libsemanage/src/genhomedircon.c static semanage_list_t · line 407
Quoted 7 lines
if (!semanage_list_find(shells, pwbuf->pw_shell))libsemanage/src/genhomedircon.c +0
-1@@ -403,7 +407,6 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) }free (path); path =NULL ; - errno =0 ;Actually I'm wondering if this shouldn't stay there
Quoted 1 line
Why?
According to strdup() manpage, it can also change errno, isn't there a risk that errno is changed to an non-zero value and then causing the warning below to be printed?
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
libsemanage/src/genhomedircon.c +0
-0}if (errno) {libsemanage/src/genhomedircon.c +5
-1@@ -1101,7 +1104,11 @@ static intget_group_users (genhomedircon_settings_t * s, } setpwent(); -while ((pw =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pw =getpwent (); +if (pw ==NULL ) +break ;// skip users who also have this group as their // primary group if (lfind(pw->pw_name, group->gr_mem, &nmembers,
Quoted 56 lines · Laurent Bigonville
Quoted 55 lines · Stephen Smalley
Quoted 54 lines · Laurent Bigonville
Quoted 47 lines · Laurent Bigonville
From: Laurent Bigonville <[email protected]>
The manpage explicitly states that:
The
getpwent()function returns a pointer to a passwd
structure, or
NULL if there are no more entries or an error occurred. If
an
error
occurs, errno is set appropriately. If one wants to check
errno
after
the call, it should be set to zero before the call.Without this, genhomedircon can wrongly return the following:
libsemanage.get_home_dirs: Error while fetching
users. Returning list so far.https://github.com/SELinuxProject/selinux/issues/121
Signed-off-by Laurent Bigonville <[email protected]>--- libsemanage/src/genhomedircon.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)libsemanage/src/genhomedircon.c +5
-1@@ -361,7 +361,11 @@ static semanage_list_t *get_home_dirs (genhomedircon_settings_t * s) errno = 0;setpwent (); -while ((pwbuf =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pwbuf =getpwent (); +if (pwbuf ==NULL ) +break ;if (pwbuf->pw_uid < minuid || pwbuf->pw_uid > maxuid)continue;
if (!semanage_list_find(shells, pwbuf-Quoted 1 line
pw_shell))libsemanage/src/genhomedircon.c static semanage_list_t · line 407
Quoted 5 lines
libsemanage/src/genhomedircon.c +0
-1@@ -403,7 +407,6 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) }free (path); path =NULL ; - errno =0 ;Actually I'm wondering if this shouldn't stay there
Quoted 6 lines
Quoted 1 line
Why?
According to
strdup()manpage, it can also change errno, isn't there a
risk that errno is changed to an non-zero value and then causing the warning below to be printed?
On strdup(pwbuf->pw_dir) failure, we'll break out of the loop with non- zero errno and then correctly report that there was an error that truncated the list. That's correct behavior. The errno = 0; were are removing is within the loop and is obsoleted by your setting of it at the beginning of the loop. So I think your patch is fine as is, although I have not tested it.
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
libsemanage/src/genhomedircon.c +0
-0}if (errno) {libsemanage/src/genhomedircon.c +5
-1@@ -1101,7 +1104,11 @@ static intget_group_users (genhomedircon_settings_t * s, } setpwent(); -while ((pw =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pw =getpwent (); +if (pw ==NULL ) +break ;// skip users who also have this group as their // primary group if (lfind(pw->pw_name, group->gr_mem,&nmembers,
Quoted 65 lines · Stephen Smalley
Quoted 56 lines · Laurent Bigonville
Quoted 55 lines · Stephen Smalley
Quoted 54 lines · Laurent Bigonville
Quoted 47 lines · Laurent Bigonville
From: Laurent Bigonville <[email protected]>
The manpage explicitly states that:
The
getpwent()function returns a pointer to a passwd
structure, or
NULL if there are no more entries or an error occurred. If
an
error
occurs, errno is set appropriately. If one wants to check
errno
after
the call, it should be set to zero before the call.Without this, genhomedircon can wrongly return the following:
libsemanage.get_home_dirs: Error while fetching
users. Returning list so far.https://github.com/SELinuxProject/selinux/issues/121
Signed-off-by Laurent Bigonville <[email protected]>--- libsemanage/src/genhomedircon.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)libsemanage/src/genhomedircon.c +5
-1@@ -361,7 +361,11 @@ static semanage_list_t *get_home_dirs (genhomedircon_settings_t * s) errno = 0;setpwent (); -while ((pwbuf =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pwbuf =getpwent (); +if (pwbuf ==NULL ) +break ;if (pwbuf->pw_uid < minuid || pwbuf->pw_uid > maxuid)continue;
if (!semanage_list_find(shells, pwbuf-Quoted 1 line
pw_shell))libsemanage/src/genhomedircon.c static semanage_list_t · line 407
Quoted 5 lines
libsemanage/src/genhomedircon.c +0
-1@@ -403,7 +407,6 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) }free (path); path =NULL ; - errno =0 ;Actually I'm wondering if this shouldn't stay there
Quoted 6 lines
Quoted 1 line
Why?
According to
strdup()manpage, it can also change errno, isn't there a
risk that errno is changed to an non-zero value and then causing the warning below to be printed?
Quoted 6 lines
On
strdup(pwbuf->pw_dir)failure, we'll break out of the loop with non- zero errno and then correctly report that there was an error that truncated the list. That's correct behavior. The errno = 0; were are removing is within the loop and is obsoleted by your setting of it at the beginning of the loop. So I think your patch is fine as is, although I have not tested it.
I have tested it on my test system. Before applying the patch, "semanage fcontext -m ..." displayed "libsemanage.get_home_dirs: Error while fetching users. Returning list so far.". This message disappears as expected with the patch.
Merged. Thanks!
Nicolas
Quoted 22 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
libsemanage/src/genhomedircon.c +0
-0}if (errno) {libsemanage/src/genhomedircon.c +5
-1@@ -1101,7 +1104,11 @@ static intget_group_users (genhomedircon_settings_t * s, } setpwent(); -while ((pw =getpwent ()) !=NULL ) { +while (1 ) { + errno =0 ; + pw =getpwent (); +if (pw ==NULL ) +break ;// skip users who also have this group as their // primary group if (lfind(pw->pw_name, group->gr_mem,&nmembers,