Disable inheritance of several common file descriptors
authorStephen Toub <stoub@microsoft.com>
Wed, 31 May 2017 21:18:24 +0000 (17:18 -0400)
committerStephen Toub <stoub@microsoft.com>
Wed, 31 May 2017 22:52:30 +0000 (18:52 -0400)
src/pal/src/config.h.in
src/pal/src/configure.cmake
src/pal/src/file/file.cpp
src/pal/src/map/map.cpp
src/pal/src/synchmgr/synchmanager.cpp
src/pal/src/thread/threadsusp.cpp

index 48677df..03513a1 100644 (file)
@@ -61,6 +61,7 @@
 #cmakedefine01 HAS_SYSV_SEMAPHORES
 #cmakedefine01 HAS_PTHREAD_MUTEXES
 #cmakedefine01 HAVE_TTRACE
+#cmakedefine01 HAVE_PIPE2
 #cmakedefine01 HAVE_SCHED_GETAFFINITY
 #cmakedefine HAVE_UNW_GET_SAVE_LOC
 #cmakedefine HAVE_UNW_GET_ACCESSORS
index 6cf7705..d305081 100644 (file)
@@ -100,6 +100,7 @@ check_function_exists(directio HAVE_DIRECTIO)
 check_function_exists(semget HAS_SYSV_SEMAPHORES)
 check_function_exists(pthread_mutex_init HAS_PTHREAD_MUTEXES)
 check_function_exists(ttrace HAVE_TTRACE)
+check_function_exists(pipe2 HAVE_PIPE2)
 set(CMAKE_REQUIRED_LIBRARIES unwind unwind-generic)
 check_cxx_source_compiles("
 #include <libunwind.h>
index af42549..feec655 100644 (file)
@@ -4564,7 +4564,7 @@ static HANDLE init_std_handle(HANDLE * pStd, FILE *stream)
 
     /* duplicate the FILE *, so that we can fclose() in FILECloseHandle without
        closing the original */
-    new_fd = dup(fileno(stream));
+    new_fd = fcntl(fileno(stream), F_DUPFD_CLOEXEC, 0); // dup, but with CLOEXEC
     if(-1 == new_fd)
     {
         ERROR("dup() failed; errno is %d (%s)\n", errno, strerror(errno));
index f26293b..b8ffc84 100644 (file)
@@ -246,7 +246,7 @@ FileMappingInitializationRoutine(
 
     pProcessLocalData->UnixFd = InternalOpen(
         pImmutableData->szFileName,
-        MAPProtectionToFileOpenFlags(pImmutableData->flProtect)
+        MAPProtectionToFileOpenFlags(pImmutableData->flProtect) | O_CLOEXEC
         );
 
     if (-1 == pProcessLocalData->UnixFd)
@@ -510,7 +510,7 @@ CorUnix::InternalCreateFileMapping(
 
 #if HAVE_MMAP_DEV_ZERO
 
-        UnixFd = InternalOpen(pImmutableData->szFileName, O_RDWR);
+        UnixFd = InternalOpen(pImmutableData->szFileName, O_RDWR | O_CLOEXEC);
         if ( -1 == UnixFd )
         {
             ERROR( "Unable to open the file.\n");
@@ -587,7 +587,7 @@ CorUnix::InternalCreateFileMapping(
             // information, though...
             //
             
-            UnixFd = dup(pFileLocalData->unix_fd);
+            UnixFd = fcntl(pFileLocalData->unix_fd, F_DUPFD_CLOEXEC, 0); // dup, but with CLOEXEC
             if (-1 == UnixFd)
             {
                 ERROR( "Unable to duplicate the Unix file descriptor!\n" );
index d836a17..73b5644 100644 (file)
@@ -3525,12 +3525,22 @@ namespace CorUnix
         }
 #else // !CORECLR
         int rgiPipe[] = { -1, -1 };
-        if (pipe(rgiPipe) == -1)
+        int pipeRv =
+#if HAVE_PIPE2
+            pipe2(rgiPipe, O_CLOEXEC);
+#else
+            pipe(rgiPipe);
+#endif // HAVE_PIPE2
+        if (pipeRv == -1)
         {
             ERROR("Unable to create the process pipe\n");
             fRet = false;
             goto CPP_exit;
         }
+#if !HAVE_PIPE2
+        fcntl(rgiPipe[0], F_SETFD, FD_CLOEXEC); // make pipe non-inheritable, if possible
+        fcntl(rgiPipe[1], F_SETFD, FD_CLOEXEC);
+#endif // !HAVE_PIPE2
 #endif // !CORECLR
 
 #if HAVE_KQUEUE && !HAVE_BROKEN_FIFO_KEVENT
index c7787be..f8a435c 100644 (file)
@@ -74,11 +74,21 @@ CThreadSuspensionInfo::InternalSuspendNewThreadFromData(
     ReleaseSuspensionLock(pThread);
 
     int pipe_descs[2];
-    if (pipe(pipe_descs) == -1)
+    int pipeRv =
+#if HAVE_PIPE2
+        pipe2(pipe_descs, O_CLOEXEC);
+#else
+        pipe(pipe_descs);
+#endif // HAVE_PIPE2
+    if (pipeRv == -1)
     {
         ERROR("pipe() failed! error is %d (%s)\n", errno, strerror(errno));
         return ERROR_NOT_ENOUGH_MEMORY;
     }
+#if !HAVE_PIPE2
+    fcntl(pipe_descs[0], F_SETFD, FD_CLOEXEC); // make pipe non-inheritable, if possible
+    fcntl(pipe_descs[1], F_SETFD, FD_CLOEXEC);
+#endif // !HAVE_PIPE2
 
     // [0] is the read end of the pipe, and [1] is the write end.
     pThread->suspensionInfo.SetBlockingPipe(pipe_descs[1]);