]> Savannah Git Hosting - gnulib.git/commitdiff
dirent-safer: fix cloexec race
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 12 Aug 2017 18:34:50 +0000 (11:34 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 12 Aug 2017 18:36:11 +0000 (11:36 -0700)
* lib/opendir-safer.c: Include fcntl.h instead of unistd-safer.h.
(opendir_safer): Use F_DUPFD_CLOEXEC.
* modules/dirent-safer (Depends-on): Add fcntl.  Remove unistd-safer.
* tests/test-dirent-safer.c: Do not include unistd-safer.h,
as it is no longer a prerequisite.  Use F_DUPFD_CLOEXEC
instead of dup_safer.

ChangeLog
lib/opendir-safer.c
modules/dirent-safer
tests/test-dirent-safer.c

index c725f5d91f61682325bb6fd2ad5fe484ad83cd5e..514b503e5fafeb2c48eeef0999145bebd81896f6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2017-08-12  Paul Eggert  <eggert@cs.ucla.edu>
 
+       dirent-safer: fix cloexec race
+       * lib/opendir-safer.c: Include fcntl.h instead of unistd-safer.h.
+       (opendir_safer): Use F_DUPFD_CLOEXEC.
+       * modules/dirent-safer (Depends-on): Add fcntl.  Remove unistd-safer.
+       * tests/test-dirent-safer.c: Do not include unistd-safer.h,
+       as it is no longer a prerequisite.  Use F_DUPFD_CLOEXEC
+       instead of dup_safer.
+
        fts: fix cloexec races
        * lib/fts.c [!_LIBC]: Do not include dirent--.h, unistd--.h, cloexec.h.
        (opendirat, diropen): Use O_CLOEXEC instead of set_cloexec_flag.
index b05ff549c3d6e3c9d2f91b4cbeda55aacb0b33f1..5f1f49e320382f7073a1c6a51c9969fa99cabbbe 100644 (file)
@@ -22,8 +22,8 @@
 #include "dirent-safer.h"
 
 #include <errno.h>
+#include <fcntl.h>
 #include <unistd.h>
-#include "unistd-safer.h"
 
 /* Like opendir, but do not clobber stdin, stdout, or stderr.  */
 
@@ -49,7 +49,7 @@ opendir_safer (char const *name)
           DIR *newdp;
           int e;
 #if HAVE_FDOPENDIR || GNULIB_FDOPENDIR
-          int f = dup_safer (fd);
+          int f = fcntl (fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
           if (f < 0)
             {
               e = errno;
index b38c58e6411fde70df8622eb99375f5bf29c174f..4ea4a25612834070977a15a73916d7dad1740255 100644 (file)
@@ -11,8 +11,8 @@ Depends-on:
 dirent
 closedir
 dirfd
+fcntl
 opendir
-unistd-safer
 
 configure.ac:
 gl_DIRENT_SAFER
index 2f8c3cde257b3ea2f099bfdce998d10e7f1406de..8c4e4e35813b892467a568b16b4dc45ec40e1cc8 100644 (file)
@@ -25,8 +25,6 @@
 #include <stdio.h>
 #include <unistd.h>
 
-#include "unistd-safer.h"
-
 /* This test intentionally closes stderr.  So, we arrange to have fd 10
    (outside the range of interesting fd's during the test) set up to
    duplicate the original stderr.  */
@@ -75,7 +73,7 @@ main (void)
 
 #if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR
       {
-        int fd = dup_safer (dfd);
+        int fd = fcntl (dfd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
         ASSERT (STDERR_FILENO < fd);
         dp = fdopendir (fd);
         ASSERT (dp);