about summary refs log tree commit diff
path: root/sysdeps/pthread
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@gmail.com>2023-04-19 19:02:03 +0300
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2023-04-22 13:50:14 +0200
commit533deafbdf189f5fbb280c28562dd43ace2f4b0f (patch)
tree292d1cf07d52199a9dd247c6bb06f5566c218855 /sysdeps/pthread
parent28a441cc577e31e95742b9ec5f1737b86749b712 (diff)
downloadglibc-533deafbdf189f5fbb280c28562dd43ace2f4b0f.tar.gz
glibc-533deafbdf189f5fbb280c28562dd43ace2f4b0f.tar.xz
glibc-533deafbdf189f5fbb280c28562dd43ace2f4b0f.zip
Use O_CLOEXEC in more places (BZ #15722)
When opening a temporary file without O_CLOEXEC we risk leaking the
file descriptor if another thread calls (fork and then) exec while we
have the fd open. Fix this by consistently passing O_CLOEXEC everywhere
where we open a file for internal use (and not to return it to the user,
in which case the API defines whether or not the close-on-exec flag
shall be set on the returned fd).

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
Message-Id: <20230419160207.65988-4-bugaevc@gmail.com>
Diffstat (limited to 'sysdeps/pthread')
-rw-r--r--sysdeps/pthread/sem_open.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/sysdeps/pthread/sem_open.c b/sysdeps/pthread/sem_open.c
index 2d32a13557..e5db929d20 100644
--- a/sysdeps/pthread/sem_open.c
+++ b/sysdeps/pthread/sem_open.c
@@ -36,6 +36,7 @@ sem_t *
 __sem_open (const char *name, int oflag, ...)
 {
   int fd;
+  int open_flags;
   sem_t *result;
 
   /* Check that shared futexes are supported.  */
@@ -64,9 +65,10 @@ __sem_open (const char *name, int oflag, ...)
   /* If the semaphore object has to exist simply open it.  */
   if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0)
     {
+      open_flags = O_RDWR | O_NOFOLLOW | O_CLOEXEC;
+      open_flags |= (oflag & ~(O_CREAT|O_ACCMODE));
     try_again:
-      fd = __open (dirname.name,
-		   (oflag & ~(O_CREAT|O_ACCMODE)) | O_NOFOLLOW | O_RDWR);
+      fd = __open (dirname.name, open_flags);
 
       if (fd == -1)
 	{
@@ -133,7 +135,8 @@ __sem_open (const char *name, int oflag, ...)
 	    }
 
 	  /* Open the file.  Make sure we do not overwrite anything.  */
-	  fd = __open (tmpfname, O_RDWR | O_CREAT | O_EXCL, mode);
+	  open_flags = O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC;
+	  fd = __open (tmpfname, open_flags, mode);
 	  if (fd == -1)
 	    {
 	      if (errno == EEXIST)