bionic: fix pthread_{create, exit}/signal race condition
(1) in pthread_create:
If the one signal is received before esp is subtracted by 16 and
__thread_entry( ) is called, the stack will be cleared by kernel
when it tries to contruct the signal stack frame. That will cause
that __thread_entry will get a wrong tls pointer from the stack
which leads to the segment fault when trying to access tls content.
(2) in pthread_exit
After pthread_exit called system call unmap(), its stack will be
freed. If one signal is received at that time, there is no stack
available for it.
Fixed by subtracting the child's esp by 16 before the clone system
call and by blocking signal handling before pthread_exit is started.
Author: Jack Ren <jack.ren@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
diff --git a/libc/bionic/pthread.c b/libc/bionic/pthread.c
index 1da2ec9..e8f1052 100644
--- a/libc/bionic/pthread.c
+++ b/libc/bionic/pthread.c
@@ -571,6 +571,7 @@
void* stack_base = thread->attr.stack_base;
int stack_size = thread->attr.stack_size;
int user_stack = (thread->attr.flags & PTHREAD_ATTR_FLAG_USER_STACK) != 0;
+ sigset_t mask;
// call the cleanup handlers first
while (thread->cleanup_stack) {
@@ -613,6 +614,10 @@
pthread_mutex_unlock(&gThreadListLock);
}
+ sigfillset(&mask);
+ sigdelset(&mask, SIGSEGV);
+ (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
+
// destroy the thread stack
if (user_stack)
_exit_thread((int)retval);