Skip to content

Commit

Permalink
libsemanage: Always set errno to 0 before calling getpwent()
Browse files Browse the repository at this point in the history
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.

SELinuxProject#121

Signed-off-by: Laurent Bigonville <bigon@bigon.be>
  • Loading branch information
bigon authored and fishilico committed Jan 5, 2019
1 parent 1015aef commit 9ac345e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions libsemanage/src/genhomedircon.c
Expand Up @@ -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))
Expand Down Expand Up @@ -403,7 +407,6 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s)
}
free(path);
path = NULL;
errno = 0;
}

if (errno) {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9ac345e

Please sign in to comment.