public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] sysdeps: Clear O_CREAT|O_ACCMODE when trying again on sem_open
@ 2023-08-23  4:21 Sergio Durigan Junior
  2023-08-23  7:40 ` Andreas Schwab
  2023-08-23 13:46 ` Adhemerval Zanella Netto
  0 siblings, 2 replies; 15+ messages in thread
From: Sergio Durigan Junior @ 2023-08-23  4:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: Sergio Durigan Junior

When invoking sem_open with O_CREAT as one of its flags, we'll end up
in the second part of sem_open's "if ((oflag & O_CREAT) == 0 || (oflag
& O_EXCL) == 0)", which means that we don't expect the semaphore file
to exist.

In that part, open_flags is initialized as "O_RDWR | O_CREAT | O_EXCL
| O_CLOEXEC" and there's an attempt to open(2) the file, which will
likely fail because it won't exist.  After that first (expected)
failure, some cleanup is done and we go back to the label "try_again",
which lives in the first part of the aforementioned "if".

The problem is that, in that part of the code, we expect the semaphore
file to exist, and as such O_CREAT (this time the flag we pass to
open(2)) needs to be cleaned from open_flags, otherwise we'll see
another failure (this time unexpected) when trying to open the file,
which will lead the call to sem_open to fail as well.

This can cause very strange bugs, especially with OpenMPI, which makes
extensive use of semaphores.

The fix here is to actually make sure that the O_CREAT|O_ACCMODE flags
are clear after we enter "try_again".

See also: https://bugs.launchpad.net/ubuntu/+source/h5py/+bug/2031912
---
 sysdeps/pthread/sem_open.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sysdeps/pthread/sem_open.c b/sysdeps/pthread/sem_open.c
index e5db929d20..ba91f89d57 100644
--- a/sysdeps/pthread/sem_open.c
+++ b/sysdeps/pthread/sem_open.c
@@ -66,8 +66,8 @@ __sem_open (const char *name, int oflag, ...)
   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:
+      open_flags |= (oflag & ~(O_CREAT|O_ACCMODE));
       fd = __open (dirname.name, open_flags);
 
       if (fd == -1)
-- 
2.39.2


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-11-03 13:04 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-23  4:21 [PATCH] sysdeps: Clear O_CREAT|O_ACCMODE when trying again on sem_open Sergio Durigan Junior
2023-08-23  7:40 ` Andreas Schwab
2023-08-23 14:10   ` Sergio Durigan Junior
2023-08-23 14:45     ` Adhemerval Zanella Netto
2023-08-23 19:08       ` Sergio Durigan Junior
2023-08-23 21:53         ` Joseph Myers
2023-08-23 13:46 ` Adhemerval Zanella Netto
2023-08-23 14:16   ` Sergio Durigan Junior
2023-08-23 14:23     ` Sergio Durigan Junior
2023-10-28 20:30   ` Sergio Durigan Junior
2023-11-01 13:27     ` Adhemerval Zanella Netto
2023-11-01 13:53       ` Joseph Myers
2023-11-01 22:14       ` Sergio Durigan Junior
2023-11-01 22:15       ` [PATCH] sysdeps: sem_open: Clear O_CREAT when semaphore file is expected to exist [BZ #30789] Sergio Durigan Junior
2023-11-03 13:04         ` Adhemerval Zanella Netto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).