[Bug nss/24696] New: endgrent() clobbers errno=ERRNO for 'group: db files' entry in /etc/nsswitch.conf

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

[Bug nss/24696] New: endgrent() clobbers errno=ERRNO for 'group: db files' entry in /etc/nsswitch.conf

fche at redhat dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=24696

            Bug ID: 24696
           Summary: endgrent() clobbers errno=ERRNO for 'group: db files'
                    entry in /etc/nsswitch.conf
           Product: glibc
           Version: 2.29
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: nss
          Assignee: unassigned at sourceware dot org
          Reporter: slyfox at inbox dot ru
  Target Milestone: ---

The bug is originally reported by Guillermo D. H. at
https://bugs.gentoo.org/688246.

Here is the reproducer:

$ cat /etc/nsswitch.conf
    passwd:         files
    #group:         files
    #https://bugs.gentoo.org/688246
    group:          db files
    initgroups:     files
    shadow:         files
    gshadow:        files
    hosts:          files dns
    networks:       files dns
    protocols:      db files
    services:       db files
    ethers:         db files
    rpc:            db files
    netgroup:       db files

$ cat a.c
    #include <errno.h>
    #include <stdio.h>
    #include <string.h>
    #include <grp.h>
    int main() {
     int saved_errno;
     for (;;) {
      errno = 0;
      struct group *gr = getgrent();
      saved_errno = errno;
      if (!gr)
       break;
     }
     printf("(errno = %s)\n", strerror(saved_errno));
     printf("errno before endgrent() = %s\n", strerror(errno));
     endgrent();
     printf("errno after endgrent() = %s\n", strerror(errno));
    }

$ gcc a.c -o a
$ ./a
    (errno = Success)
    errno before endgrent() = Success
    errno after endgrent() = Invalid argument <- the bug

The following workaround seems to be enough:

--- a/nss/nss_db/db-open.c
+++ b/nss/nss_db/db-open.c
@@ -63,5 +63,9 @@ internal_setent (const char *file, struct nss_db_map
*mapping)
 void
 internal_endent (struct nss_db_map *mapping)
 {
-  munmap (mapping->header, mapping->len);
+  /* Avoid clobbering errno if 'header' was never allocated.  */
+  if (mapping->header != NULL)
+    {
+      munmap (mapping->header, mapping->len);
+    }
 }

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug nss/24696] endgrent() clobbers errno=ERRNO for 'group: db files' entry in /etc/nsswitch.conf

fche at redhat dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=24696

--- Comment #1 from Sergei Trofimovich <slyfox at inbox dot ru> ---
Created attachment 11843
  --> https://sourceware.org/bugzilla/attachment.cgi?id=11843&action=edit
a.c

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug nss/24696] endgrent() clobbers errno=ERRNO for 'group: db files' entry in /etc/nsswitch.conf

fche at redhat dot com
In reply to this post by fche at redhat dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=24696

Carlos O'Donell <carlos at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |carlos at redhat dot com,
                   |                            |dj at redhat dot com

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug nss/24696] endgrent() clobbers errno=ERRNO for 'group: db files' entry in /etc/nsswitch.conf

fche at redhat dot com
In reply to this post by fche at redhat dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=24696

dj at redhat dot com <dj at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at sourceware dot org   |dj at redhat dot com

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug nss/24696] endgrent() clobbers errno=ERRNO for 'group: db files' entry in /etc/nsswitch.conf

fche at redhat dot com
In reply to this post by fche at redhat dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=24696

--- Comment #2 from Andreas Schwab <[hidden email]> ---
Any library function is allowed to modify errno unless explicitly documented
otherwise.

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug nss/24696] endgrent() clobbers errno=ERRNO for 'group: db files' entry in /etc/nsswitch.conf

fche at redhat dot com
In reply to this post by fche at redhat dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=24696

Florian Weimer <fweimer at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fweimer at redhat dot com

--- Comment #3 from Florian Weimer <fweimer at redhat dot com> ---
Yes, I agree that this is not a bug.

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug nss/24696] endgrent() clobbers errno=ERRNO for 'group: db files' entry in /etc/nsswitch.conf

fche at redhat dot com
In reply to this post by fche at redhat dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=24696

--- Comment #4 from Andreas Schwab <[hidden email]> ---
Actually, endgrent *is* one of those exceptions:

  The setgrent() and endgrent() functions shall not change the setting of errno
if  
  successful.

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug nss/24696] endgrent() clobbers errno=ERRNO for 'group: db files' entry in /etc/nsswitch.conf

fche at redhat dot com
In reply to this post by fche at redhat dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=24696

--- Comment #5 from cvs-commit at gcc dot gnu.org <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by DJ Delorie <[hidden email]>:

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=99135114ba23c3110b7e4e650fabdc5e639746b7

commit 99135114ba23c3110b7e4e650fabdc5e639746b7
Author: DJ Delorie <[hidden email]>
Date:   Fri Jun 28 18:30:00 2019 -0500

    nss_db: fix endent wrt NULL mappings [BZ #24695] [BZ #24696]

    nss_db allows for getpwent et al to be called without a set*ent,
    but it only works once.  After the last get*ent a set*ent is
    required to restart, because the end*ent did not properly reset
    the module.  Resetting it to NULL allows for a proper restart.

    If the database doesn't exist, however, end*ent erroniously called
    munmap which set errno.

    The test case runs "makedb" inside the testroot, so needs selinux
    DSOs installed.

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug nss/24696] endgrent() clobbers errno=ERRNO for 'group: db files' entry in /etc/nsswitch.conf

fche at redhat dot com
In reply to this post by fche at redhat dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=24696

dj at redhat dot com <dj at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |FIXED
   Target Milestone|---                         |2.30

--- Comment #6 from dj at redhat dot com <dj at redhat dot com> ---
Fix committed.

--
You are receiving this mail because:
You are on the CC list for the bug.