protect destruction of process-shared mutexes against robust list races
authorRich Felker <dalias@aerifal.cx>
Thu, 19 Nov 2020 21:20:45 +0000 (16:20 -0500)
committerRich Felker <dalias@aerifal.cx>
Thu, 19 Nov 2020 21:36:49 +0000 (16:36 -0500)
after a non-normal-type process-shared mutex is unlocked, it's
immediately available to another thread to lock, unlock, and destroy,
but the first unlocking thread may still have a pointer to it in its
robust_list pending slot. this means, on async process termination,
the kernel may attempt to access and modify the memory that used to
contain the mutex -- memory that may have been reused for some other
purpose after the mutex was destroyed.

setting up for this kind of race to occur is difficult to begin with,
requiring dynamic use of shared memory maps, and actually hitting the
race is very difficult even with a suitable setup. so this is mostly a
theoretical fix, but in any case the cost is very low.

src/thread/pthread_mutex_destroy.c

index 6d49e68..8d1bf77 100644 (file)
@@ -1,6 +1,10 @@
-#include <pthread.h>
+#include "pthread_impl.h"
 
 int pthread_mutex_destroy(pthread_mutex_t *mutex)
 {
+       /* If the mutex being destroyed is process-shared and has nontrivial
+        * type (tracking ownership), it might be in the pending slot of a
+        * robust_list; wait for quiescence. */
+       if (mutex->_m_type > 128) __vm_wait();
        return 0;
 }