Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 7e94cac

Browse files
authored
Merge pull request #12032 from stephentoub/port11988
release/2.0: Ensure several runtime file descriptors are CLOEXEC
2 parents 7c1c0c5 + acf47ad commit 7e94cac

File tree

6 files changed

+30
-8
lines changed

6 files changed

+30
-8
lines changed

src/pal/src/config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#cmakedefine01 HAS_SYSV_SEMAPHORES
6262
#cmakedefine01 HAS_PTHREAD_MUTEXES
6363
#cmakedefine01 HAVE_TTRACE
64+
#cmakedefine01 HAVE_PIPE2
6465
#cmakedefine01 HAVE_SCHED_GETAFFINITY
6566
#cmakedefine HAVE_UNW_GET_SAVE_LOC
6667
#cmakedefine HAVE_UNW_GET_ACCESSORS

src/pal/src/configure.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ check_function_exists(directio HAVE_DIRECTIO)
9999
check_function_exists(semget HAS_SYSV_SEMAPHORES)
100100
check_function_exists(pthread_mutex_init HAS_PTHREAD_MUTEXES)
101101
check_function_exists(ttrace HAVE_TTRACE)
102+
check_function_exists(pipe2 HAVE_PIPE2)
102103
set(CMAKE_REQUIRED_LIBRARIES unwind unwind-generic)
103104
check_cxx_source_compiles("
104105
#include <libunwind.h>

src/pal/src/file/file.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4056,14 +4056,14 @@ CorUnix::InternalCreatePipe(
40564056

40574057
/* enable close-on-exec for both pipes; if one gets passed to CreateProcess
40584058
it will be "uncloseonexeced" in order to be inherited */
4059-
if(-1 == fcntl(readWritePipeDes[0],F_SETFD,1))
4059+
if(-1 == fcntl(readWritePipeDes[0],F_SETFD,FD_CLOEXEC))
40604060
{
40614061
ASSERT("can't set close-on-exec flag; fcntl() failed. errno is %d "
40624062
"(%s)\n", errno, strerror(errno));
40634063
palError = ERROR_INTERNAL_ERROR;
40644064
goto InternalCreatePipeExit;
40654065
}
4066-
if(-1 == fcntl(readWritePipeDes[1],F_SETFD,1))
4066+
if(-1 == fcntl(readWritePipeDes[1],F_SETFD,FD_CLOEXEC))
40674067
{
40684068
ASSERT("can't set close-on-exec flag; fcntl() failed. errno is %d "
40694069
"(%s)\n", errno, strerror(errno));
@@ -4564,7 +4564,7 @@ static HANDLE init_std_handle(HANDLE * pStd, FILE *stream)
45644564

45654565
/* duplicate the FILE *, so that we can fclose() in FILECloseHandle without
45664566
closing the original */
4567-
new_fd = dup(fileno(stream));
4567+
new_fd = fcntl(fileno(stream), F_DUPFD_CLOEXEC, 0); // dup, but with CLOEXEC
45684568
if(-1 == new_fd)
45694569
{
45704570
ERROR("dup() failed; errno is %d (%s)\n", errno, strerror(errno));

src/pal/src/map/map.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ FileMappingInitializationRoutine(
246246

247247
pProcessLocalData->UnixFd = InternalOpen(
248248
pImmutableData->szFileName,
249-
MAPProtectionToFileOpenFlags(pImmutableData->flProtect)
249+
MAPProtectionToFileOpenFlags(pImmutableData->flProtect) | O_CLOEXEC
250250
);
251251

252252
if (-1 == pProcessLocalData->UnixFd)
@@ -510,7 +510,7 @@ CorUnix::InternalCreateFileMapping(
510510

511511
#if HAVE_MMAP_DEV_ZERO
512512

513-
UnixFd = InternalOpen(pImmutableData->szFileName, O_RDWR);
513+
UnixFd = InternalOpen(pImmutableData->szFileName, O_RDWR | O_CLOEXEC);
514514
if ( -1 == UnixFd )
515515
{
516516
ERROR( "Unable to open the file.\n");
@@ -587,7 +587,7 @@ CorUnix::InternalCreateFileMapping(
587587
// information, though...
588588
//
589589

590-
UnixFd = dup(pFileLocalData->unix_fd);
590+
UnixFd = fcntl(pFileLocalData->unix_fd, F_DUPFD_CLOEXEC, 0); // dup, but with CLOEXEC
591591
if (-1 == UnixFd)
592592
{
593593
ERROR( "Unable to duplicate the Unix file descriptor!\n" );

src/pal/src/synchmgr/synchmanager.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3525,12 +3525,22 @@ namespace CorUnix
35253525
}
35263526
#else // !CORECLR
35273527
int rgiPipe[] = { -1, -1 };
3528-
if (pipe(rgiPipe) == -1)
3528+
int pipeRv =
3529+
#if HAVE_PIPE2
3530+
pipe2(rgiPipe, O_CLOEXEC);
3531+
#else
3532+
pipe(rgiPipe);
3533+
#endif // HAVE_PIPE2
3534+
if (pipeRv == -1)
35293535
{
35303536
ERROR("Unable to create the process pipe\n");
35313537
fRet = false;
35323538
goto CPP_exit;
35333539
}
3540+
#if !HAVE_PIPE2
3541+
fcntl(rgiPipe[0], F_SETFD, FD_CLOEXEC); // make pipe non-inheritable, if possible
3542+
fcntl(rgiPipe[1], F_SETFD, FD_CLOEXEC);
3543+
#endif // !HAVE_PIPE2
35343544
#endif // !CORECLR
35353545

35363546
#if HAVE_KQUEUE && !HAVE_BROKEN_FIFO_KEVENT

src/pal/src/thread/threadsusp.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,21 @@ CThreadSuspensionInfo::InternalSuspendNewThreadFromData(
7474
ReleaseSuspensionLock(pThread);
7575

7676
int pipe_descs[2];
77-
if (pipe(pipe_descs) == -1)
77+
int pipeRv =
78+
#if HAVE_PIPE2
79+
pipe2(pipe_descs, O_CLOEXEC);
80+
#else
81+
pipe(pipe_descs);
82+
#endif // HAVE_PIPE2
83+
if (pipeRv == -1)
7884
{
7985
ERROR("pipe() failed! error is %d (%s)\n", errno, strerror(errno));
8086
return ERROR_NOT_ENOUGH_MEMORY;
8187
}
88+
#if !HAVE_PIPE2
89+
fcntl(pipe_descs[0], F_SETFD, FD_CLOEXEC); // make pipe non-inheritable, if possible
90+
fcntl(pipe_descs[1], F_SETFD, FD_CLOEXEC);
91+
#endif // !HAVE_PIPE2
8292

8393
// [0] is the read end of the pipe, and [1] is the write end.
8494
pThread->suspensionInfo.SetBlockingPipe(pipe_descs[1]);

0 commit comments

Comments
 (0)