Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errno should be reset to 0 before each call to getpwent() in genhomedircon #121

Closed
bigon opened this issue Jan 2, 2019 · 1 comment
Closed

Comments

@bigon
Copy link
Contributor

bigon commented Jan 2, 2019

Hello,

In genhomedircon, it seems that errno should be reset to 0 before each call to getpwent()

The manpage of getpwent() states:

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 error when using the files module:

libsemanage.get_home_dirs: Error while fetching users. Returning list so far.

bigon added a commit to bigon/selinux that referenced this issue Jan 2, 2019
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>
fishilico pushed a commit to fishilico/selinux that referenced this issue Jan 5, 2019
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>
@fishilico
Copy link
Member

The catch is this issue is that getpwent can set errno = EINVAL when returning entries, which is why errno needs to be set to 0 every time getpwent is called.

For future reference, here is a simple test program to illustrate this behavior:

#include <sys/types.h>
#include <pwd.h>
#include <errno.h>
#include <stdio.h>

int main(void)
{
    struct passwd *pw;
    setpwent();
    errno = 0;
    while ((pw = getpwent()) != NULL) {
        printf("%d: %s (errno %d)\n", pw->pw_uid, pw->pw_name, errno);
	errno = 0;
    }
    printf("getpwent errno = %d (%m)\n", errno);
    endpwent();
    return 0;
}

Its output (in a Vagrant virtual machine with glibc 2.28 and systemd 239.370 is:

0: root (errno 22)
1: bin (errno 22)
2: daemon (errno 22)
8: mail (errno 22)
14: ftp (errno 22)
33: http (errno 22)
68: uuidd (errno 22)
81: dbus (errno 22)
99: nobody (errno 22)
191: systemd-journal-gateway (errno 22)
192: systemd-timesync (errno 22)
193: systemd-network (errno 22)
194: systemd-bus-proxy (errno 22)
195: systemd-resolve (errno 22)
998: systemd-journal-remote (errno 22)
999: systemd-coredump (errno 22)
997: systemd-journal-upload (errno 22)
1000: vagrant (errno 22)
994: git (errno 22)
993: avahi (errno 22)
992: colord (errno 22)
102: polkitd (errno 22)
getpwent errno = 0 (Success)

I merged your commit. Thanks!

bachradsusi pushed a commit to bachradsusi/selinux that referenced this issue Jan 7, 2019
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/selinux#121

Signed-off-by: Laurent Bigonville <bigon@bigon.be>
charleseb pushed a commit to MotorolaMobilityLLC/external-selinux that referenced this issue Jan 21, 2020
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/selinux#121

Signed-off-by: Laurent Bigonville <bigon@bigon.be>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants