summaryrefslogtreecommitdiff
path: root/nptl/nptl_setxid.c
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2021-06-03 14:25:54 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2021-09-10 08:22:11 -0300
commitc9246e98f3e53d07a6190f6415e03e45ea4be0b1 (patch)
tree9b48e372e9bede456f3214f7f77395e5ea9f3a9d /nptl/nptl_setxid.c
parentd42c998acb18cadf77a133e36e85e21bae5d61c4 (diff)
downloadglibc-c9246e98f3e53d07a6190f6415e03e45ea4be0b1.tar.gz
nptl: Do not use pthread set_tid_address as state synchronization (BZ #19951)
The use after free described in BZ#19951 is due the use of two different PD fields, 'joinid' and 'cancelhandling', to describe the thread state to synchronize the calls of pthread_join(), pthread_detach(), pthread_exit(), and normal thread exit. And any state change potentially requires to check for both fields atomically to handle partial state (such as pthread_join() with a cancellation handler to issue a 'joinstate' field rollback). This patch uses a different PD member with 4 possible states (JOINABLE, DETACHED, EXITING, and EXITED) instead of pthread 'tid' field: 1. On pthread_create() the inital state is set either to JOINABLE or DETACHED depending of the pthread attribute used. 2. On pthread_detach(), a CAS is issued on the state. If the CAS fails it means that thread is already detached (DETACHED) or is being terminated (EXITING). For former an EINVAL is returned, while for latter pthread_detach() should be reponsible to join the thread (and deallocate any internal resources). 3. In the exit phase of the wrapper function for the thread start routine (reached either if the thread function has returned, pthread_exit() has being called, or cancellation handled has been acted upon) we issue a CAS on state to set to EXITING mode. If the thread is previously on DETACHED mode the thread itself is responsible for arranging the deallocation of any resource, otherwise the thread needs to be joined (detached threads cannot immediately deallocate themselves) 4. The clear_tid_field on 'clone' call is changed to set the new 'state' field on thread exit (EXITED). This state ins only reached at thread termination. 5. The pthread_join() implementation is now simpler: the futex wait is done directly on thread state and there is no need to reset it in case of timeout (since the state is now set either by pthread_detach() or by the kernel on process termination). The race condition on pthread_detach is avoided with only one atomic operation on PD state: once the mode is set to THREAD_STATE_DETACHED it is up to thread itself to deallocate its memory (done on the exit phase at pthread_create()). Also, the INVALID_NOT_TERMINATED_TD_P is removed since a a negative tid is not possible and the macro is not used anywhere. This change trigger an invalid C11 thread tests: it crates a thread, which detaches itself, and after a timeout the creating thread checks if the join fails. The issue is once thrd_join() is called the thread lifetime has already expired. The test is changed so the sleep is done by the thread itself, so the creating thread will try to join a valid thread. Checked on x86_64-linux-gnu, i686-linux-gnu, aarch64-linux-gnu, arm-linux-gnueabihf, and powerpc64-linux-gnu.
Diffstat (limited to 'nptl/nptl_setxid.c')
0 files changed, 0 insertions, 0 deletions