Richard Ipsum <richardip...@fastmail.co.uk> wrote:

> I was working on an implementation of chown and got an error when
> running it on OpenBSD. It looks like a change[1] might have
> introduced unintended side-effects to getgrnam.
> 
> Without the patch supplied below some chown implementations
> may fail with ENOENT when they should succeed.
> 
> I've tested this change by rebuilding libc from the 6.5 source tree
> installing it on my system and verifying that chown no longer fails.
> Since I'm running 6.5 I'm not able to test 'current', but I did checkout
> the 'current' source tree and the patch applies cleanly.

Yes, that is a problem.  Same problem applies to two other files;
here are the diffs I propose.

Index: gen/getgrent.c
===================================================================
RCS file: /cvs/src/lib/libc/gen/getgrent.c,v
retrieving revision 1.47
diff -u -p -u -r1.47 getgrent.c
--- gen/getgrent.c      13 Sep 2018 12:31:15 -0000      1.47
+++ gen/getgrent.c      2 Jul 2019 15:39:57 -0000
@@ -197,6 +197,10 @@ DEF_WEAK(getgrgid_r);
 static int
 start_gr(void)
 {
+#ifdef YP
+       int saved_errno = errno;
+#endif
+
        if (_gr_fp) {
                rewind(_gr_fp);
 #ifdef YP
@@ -214,7 +218,9 @@ start_gr(void)
        /*
         * Hint to the kernel that a passwd database operation is happening.
         */
+       saved_errno = errno;
        (void)access("/var/run/ypbind.lock", R_OK);
+       errno = saved_errno;
 #endif
 
        return((_gr_fp = fopen(_PATH_GROUP, "re")) ? 1 : 0);
Index: gen/getgrouplist.c
===================================================================
RCS file: /cvs/src/lib/libc/gen/getgrouplist.c,v
retrieving revision 1.27
diff -u -p -u -r1.27 getgrouplist.c
--- gen/getgrouplist.c  1 Dec 2015 15:08:25 -0000       1.27
+++ gen/getgrouplist.c  2 Jul 2019 15:40:57 -0000
@@ -40,6 +40,7 @@
 #include <stdlib.h>
 #include <grp.h>
 #include <pwd.h>
+#include <errno.h>
 
 #include <rpc/rpc.h>
 #include <rpcsvc/yp.h>
@@ -148,6 +149,9 @@ getgrouplist(const char *uname, gid_t ag
        int *skipyp = &foundyp;
        extern struct group *_getgrent_yp(int *);
        struct group *grp;
+#ifdef YP
+       int saved_errno;
+#endif
 
        /*
         * install primary group
@@ -162,7 +166,9 @@ getgrouplist(const char *uname, gid_t ag
        /*
         * Hint to the kernel that a passwd database operation is happening.
         */
+       saved_errno = errno;
        (void)access("/var/run/ypbind.lock", R_OK);
+       errno = saved_errno;
 #endif
 
        /*
Index: gen/getpwent.c
===================================================================
RCS file: /cvs/src/lib/libc/gen/getpwent.c,v
retrieving revision 1.62
diff -u -p -u -r1.62 getpwent.c
--- gen/getpwent.c      21 Aug 2018 20:20:04 -0000      1.62
+++ gen/getpwent.c      2 Jul 2019 15:38:45 -0000
@@ -981,6 +981,7 @@ __initdb(int shadow)
         * Hint to the kernel that a passwd database operation is happening.
         */
        (void)access("/var/run/ypbind.lock", R_OK);
+       errno = saved_errno;
 
        __ypmode = YPMODE_NONE;
        __getpwent_has_yppw = -1;

Reply via email to