glibc/sysdeps/pthread
Sergio Durigan Junior f957f47df7 sysdeps: sem_open: Clear O_CREAT when semaphore file is expected to exist [BZ #30789]
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.

Fix the bug by simplifying the logic when choosing open(2) flags and
making sure O_CREAT is not set when the semaphore file is expected to
exist.

A regression test for this issue would require a complex and cpu time
consuming logic, since to trigger the wrong code path is not
straightforward due the racy condition.  There is a somewhat reliable
reproducer in the bug, but it requires using OpenMPI.

This resolves BZ #30789.

See also: https://bugs.launchpad.net/ubuntu/+source/h5py/+bug/2031912

Signed-off-by: Sergio Durigan Junior <sergiodj@sergiodj.net>
Co-Authored-By: Simon Chopin <simon.chopin@canonical.com>
Co-Authored-By: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
Fixes: 533deafbdf ("Use O_CLOEXEC in more places (BZ #15722)")
2023-11-03 15:19:38 -03:00
..
bits
allocalim.h
call_once.c
cnd_broadcast.c
cnd_destroy.c
cnd_init.c
cnd_signal.c
cnd_timedwait.c
cnd_wait.c Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
eintr.c sysdeps/pthread/eintr.c: fix warn unused result 2023-05-24 21:52:31 -04:00
elision-conf.h
Makeconfig
Makefile Exclude routines from fortification 2023-07-05 16:59:48 +02:00
mtx_destroy.c
mtx_init.c
mtx_lock.c
mtx_timedlock.c
mtx_trylock.c
mtx_unlock.c
posix-timer.h Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
pthread_atfork_compat.c
pthread_atfork.c
pthread_early_init.h
pthread_mutex_conf.h
sem_close.c
sem_open.c sysdeps: sem_open: Clear O_CREAT when semaphore file is expected to exist [BZ #30789] 2023-11-03 15:19:38 -03:00
sem_routines.c
sem_routines.h
sem_unlink.c
semaphore.h
thrd_create.c
thrd_detach.c
thrd_equal.c
thrd_exit.c
thrd_join.c
thrd_priv.h
thrd_sleep.c
threads.h Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
tss_create.c
tss_delete.c
tss_get.c
tss_set.c
tst-_res1.c
tst-_res1mod1.c
tst-_res1mod2.c
tst-abstime-time64.c
tst-abstime.c
tst-atfork1.c
tst-atfork2.c
tst-atfork2mod.c
tst-atfork3.c
tst-atfork3mod.c
tst-atfork4.c
tst-atfork4mod.c
tst-attr1.c
tst-backtrace1.c
tst-bad-schedattr.c
tst-barrier1.c
tst-barrier2.c
tst-barrier3.c
tst-barrier4.c
tst-basic1.c
tst-basic2.c
tst-basic3.c
tst-basic4.c
tst-basic5.c
tst-basic6.c
tst-basic7.c
tst-call-once.c
tst-cancel1.c
tst-cancel2.c
tst-cancel3.c
tst-cancel4-common.c
tst-cancel4-common.h
tst-cancel4.c tests: fix warn unused results 2023-06-01 13:01:32 -04:00
tst-cancel5.c
tst-cancel6.c tests: replace fgets by xfgets 2023-06-13 19:59:08 -04:00
tst-cancel8.c
tst-cancel9.c
tst-cancel10.c
tst-cancel11.c tests: replace read by xread 2023-06-19 09:14:56 -04:00
tst-cancel12.c Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
tst-cancel13.c
tst-cancel14.c
tst-cancel15.c
tst-cancel16.c tests: fix warn unused results 2023-06-01 13:01:32 -04:00
tst-cancel18.c
tst-cancel19.c
tst-cancel20.c tests: replace read by xread 2023-06-19 09:14:56 -04:00
tst-cancel21-static.c
tst-cancel21.c tests: replace read by xread 2023-06-19 09:14:56 -04:00
tst-cancel22.c
tst-cancel23.c
tst-cancel25.c
tst-cancel26.c
tst-cancel27.c
tst-cancel28.c
tst-cancel29.c
tst-cancel30.c nptl: Fix tst-cancel30 on sparc64 2023-04-03 17:41:59 -03:00
tst-cancel-self-cancelstate.c
tst-cancel-self-canceltype.c
tst-cancel-self-cleanup.c
tst-cancel-self-testcancel.c
tst-cancel-self.c
tst-cancelx1.c
tst-cancelx2.c
tst-cancelx3.c
tst-cancelx4.c
tst-cancelx5.c
tst-cancelx6.c
tst-cancelx8.c
tst-cancelx9.c
tst-cancelx10.c
tst-cancelx11.c
tst-cancelx12.c
tst-cancelx13.c
tst-cancelx14.c
tst-cancelx15.c
tst-cancelx16.c
tst-cancelx18.c
tst-cancelx20.c
tst-cancelx21.c
tst-cleanup0.c
tst-cleanup0.expect
tst-cleanup1.c
tst-cleanup2.c
tst-cleanup3.c
tst-cleanupx0.c
tst-cleanupx0.expect
tst-cleanupx1.c
tst-cleanupx2.c
tst-cleanupx3.c
tst-clock1.c Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
tst-cnd-basic.c
tst-cnd-broadcast.c
tst-cnd-timedwait-time64.c
tst-cnd-timedwait.c
tst-cond1.c
tst-cond2.c Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
tst-cond3.c
tst-cond4.c
tst-cond5.c
tst-cond6.c
tst-cond7.c
tst-cond8-static.c
tst-cond8.c
tst-cond9.c
tst-cond10.c
tst-cond11-static.c
tst-cond11-time64.c
tst-cond11.c
tst-cond12.c
tst-cond13.c
tst-cond14.c
tst-cond15.c
tst-cond16.c
tst-cond17.c
tst-cond18.c tests: replace write by xwrite 2023-06-01 12:40:05 -04:00
tst-cond19.c
tst-cond20.c
tst-cond21.c
tst-cond23.c
tst-cond24.c
tst-cond25.c
tst-cond27.c
tst-cond-except.c
tst-create1.c
tst-create1mod.c
tst-create-detached.c
tst-detach1.c
tst-eintr2.c
tst-eintr3.c
tst-eintr4.c
tst-eintr5.c
tst-exec1.c
tst-exec2.c
tst-exec3.c
tst-exit1.c
tst-exit2.c
tst-exit3.c
tst-fini1.c
tst-fini1mod.c tests: replace read by xread 2023-06-19 09:14:56 -04:00
tst-flock1.c tests: replace write by xwrite 2023-06-01 12:40:05 -04:00
tst-flock2.c tests: replace write by xwrite 2023-06-01 12:40:05 -04:00
tst-fork1.c
tst-fork2.c
tst-fork3.c
tst-fork4.c
tst-getpid3.c
tst-join1.c
tst-join2.c
tst-join3.c
tst-join4.c
tst-join5.c
tst-join6.c
tst-join7.c
tst-join7mod.c
tst-join8.c
tst-join9.c
tst-join10.c
tst-join11.c
tst-join12.c
tst-join13.c
tst-join14-time64.c
tst-join14.c
tst-join15.c
tst-key1.c tests: replace write by xwrite 2023-06-01 12:40:05 -04:00
tst-key2.c Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
tst-key3.c
tst-key4.c
tst-kill1.c
tst-kill2.c
tst-kill3.c
tst-kill5.c
tst-kill6.c
tst-locale1.c
tst-locale2.c
tst-memstream.c
tst-mtx-basic.c
tst-mtx-recursive.c
tst-mtx-timedlock-time64.c
tst-mtx-timedlock.c
tst-mtx-trylock.c
tst-mutex1.c
tst-mutex2.c Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
tst-mutex3.c
tst-mutex4.c
tst-mutex5.c
tst-mutex6.c
tst-mutex7.c
tst-mutex9.c
tst-mutex10.c Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
tst-mutex11.c
tst-mutex-errorcheck.c
tst-once1.c
tst-once2.c
tst-once3.c
tst-once4.c
tst-oncex3.c
tst-oncex4.c
tst-oncey3.c
tst-oncey4.c
tst-pt-align3.c
tst-pt-align.c
tst-pt-popen1.c
tst-pt-sysconf.c
tst-pt-tls1.c
tst-pt-tls2.c
tst-pt-tls4.c
tst-pt-vfork1.c
tst-pt-vfork2.c
tst-pthread_cancel-exited.c
tst-pthread_cancel-select-loop.c
tst-pthread_kill-exited.c
tst-pthread_kill-exiting.c
tst-pthread-exit-signal.c
tst-pthread-mutexattr.c
tst-pthread-raise-blocked-self.c
tst-pthread-setuid-loop.c
tst-raise1.c
tst-robust1.c
tst-robust2.c
tst-robust3.c
tst-robust4.c
tst-robust5.c
tst-robust6.c
tst-robust7.c
tst-robust8.c
tst-robust9.c
tst-robust10.c
tst-robust11.c nptl: Reformat Makefile. 2023-05-10 13:15:13 -04:00
tst-rwlock1.c
tst-rwlock4.c
tst-rwlock5.c
tst-rwlock12.c
tst-rwlock13.c
tst-rwlock14-time64.c
tst-rwlock14.c
tst-rwlock16.c
tst-rwlock-tryrdlock-stall.c Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
tst-rwlock-trywrlock-stall.c Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
tst-sem1.c
tst-sem2.c
tst-sem3.c
tst-sem4.c
tst-sem5-time64.c
tst-sem5.c
tst-sem6.c
tst-sem7.c
tst-sem8.c
tst-sem9.c
tst-sem10.c
tst-sem14.c
tst-sem15.c
tst-sem16.c
tst-setuid3.c
tst-signal1.c tests: replace write by xwrite 2023-06-01 12:40:05 -04:00
tst-signal2.c tests: replace write by xwrite 2023-06-01 12:40:05 -04:00
tst-signal4.c
tst-signal5.c
tst-signal6.c
tst-signal8.c
tst-spin1.c
tst-spin2.c
tst-spin3.c
tst-spin4.c
tst-stack1.c
tst-stdio1.c pthreads: Use _exit to terminate the tst-stdio1 test 2023-06-06 11:39:06 +02:00
tst-stdio2.c
tst-thrd-detach.c
tst-thrd-sleep-time64.c
tst-thrd-sleep.c
tst-timer.c tests: replace write by xwrite 2023-06-01 12:40:05 -04:00
tst-tls4moda.c
tst-tls4modb.c
tst-tsd1.c
tst-tsd2.c Fix misspellings in sysdeps/ -- BZ 25337 2023-05-30 23:02:29 +00:00
tst-tsd5.c
tst-tsd6.c
tst-tss-basic.c
tst-umask1.c
tst-unload.c
tst-unwind-thread.c
tst-vfork1x.c
tst-vfork2x.c