From: Paul Eggert Date: Sat, 12 Aug 2017 18:34:50 +0000 (-0700) Subject: dirent-safer: fix cloexec race X-Git-Tag: v1.0~6011 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=0474f8e6a85621c276dfc47099b9863a47f96a0d;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index c725f5d91f..514b503e5f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2017-08-12 Paul Eggert + 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. diff --git a/lib/opendir-safer.c b/lib/opendir-safer.c index b05ff549c3..5f1f49e320 100644 --- a/lib/opendir-safer.c +++ b/lib/opendir-safer.c @@ -22,8 +22,8 @@ #include "dirent-safer.h" #include +#include #include -#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; diff --git a/modules/dirent-safer b/modules/dirent-safer index b38c58e641..4ea4a25612 100644 --- a/modules/dirent-safer +++ b/modules/dirent-safer @@ -11,8 +11,8 @@ Depends-on: dirent closedir dirfd +fcntl opendir -unistd-safer configure.ac: gl_DIRENT_SAFER diff --git a/tests/test-dirent-safer.c b/tests/test-dirent-safer.c index 2f8c3cde25..8c4e4e3581 100644 --- a/tests/test-dirent-safer.c +++ b/tests/test-dirent-safer.c @@ -25,8 +25,6 @@ #include #include -#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);