From patchwork Mon Mar 23 13:16:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: develop--- via Libc-alpha X-Patchwork-Id: 1260018 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=RGFtbL78; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48mFLH5bsFz9sSd for ; Tue, 24 Mar 2020 00:17:59 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B09153940CDF; Mon, 23 Mar 2020 13:17:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B09153940CDF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1584969463; bh=VwXN29tBWGIJl9aX3MORqxeL9Z6uEEBtKgUWX3HG+nc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=RGFtbL78vvdxczUDVUsmLGH4dP3R4Hpq2BGiBXGrGjGMavYPetSbUmphC0uJ0RIH1 bWR8BwPy+gAb9CYNXqyjwZM29RjIqA3+k+xuzSLOF6nm0cg3cbu8jxlupVh+uHbaHD C7c5dLa2Ah9MJ0qKqBf9wXQGLrJ81Pq4FhuFIvzA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.efficios.com (mail.efficios.com [167.114.26.124]) by sourceware.org (Postfix) with ESMTPS id 4C1873898C45 for ; Mon, 23 Mar 2020 13:17:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4C1873898C45 Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 0B6F61B7D17; Mon, 23 Mar 2020 09:17:41 -0400 (EDT) Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id V9jL-h0_5Z9q; Mon, 23 Mar 2020 09:17:40 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 7C0721B7ABE; Mon, 23 Mar 2020 09:17:38 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 7C0721B7ABE X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id Okuf_tRMoX25; Mon, 23 Mar 2020 09:17:38 -0400 (EDT) Received: from localhost.localdomain (192-222-181-218.qc.cable.ebox.net [192.222.181.218]) by mail.efficios.com (Postfix) with ESMTPSA id 2A5461B7ABA; Mon, 23 Mar 2020 09:17:38 -0400 (EDT) To: Carlos O'Donell Subject: [RFC PATCH glibc 3/8] nptl: Start new threads with all signals blocked [BZ #25098] Date: Mon, 23 Mar 2020 09:16:02 -0400 Message-Id: <20200323131607.15185-4-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200323131607.15185-1-mathieu.desnoyers@efficios.com> References: <20200323131607.15185-1-mathieu.desnoyers@efficios.com> X-Spam-Status: No, score=-31.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Mathieu Desnoyers via Libc-alpha From: develop--- via Libc-alpha Reply-To: Mathieu Desnoyers Cc: libc-alpha@sourceware.org, Joseph Myers Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" From: Florian Weimer New threads inherit the signal mask from the current thread. This means that signal handlers can run on the newly created thread immediately after the kernel has created the userspace thread, even before glibc has initialized the TCB. Consequently, new threads can observe uninitialized ctype data, among other things. To address this, block all signals before starting the thread, and pass the original signal mask to the start routine wrapper. On the new thread, first perform all thread initialization, and then unblock signals. The cost of doing this is two rt_sigprocmask system calls on the old thread, and one rt_sigprocmask system call on the new thread. (If there was a way to clone a new thread with a signals disabled, this could be brought down to one system call each.) The thread descriptor increases in size, too, and sigset_t is fairly large. This increase could be brought down by reusing space the in the descriptor which is not needed before running user code, or by switching to an internal sigset_t definition which only covers the signals supported by the kernel definition. (Part of the thread descriptor size increase is already offset by reduced stack usage in the thread start wrapper routine after this commit.) ----- nptl/descr.h | 10 +++++++--- nptl/pthread_create.c | 47 +++++++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 25 deletions(-) --- nptl/descr.h | 10 +++++++--- nptl/pthread_create.c | 46 +++++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/nptl/descr.h b/nptl/descr.h index 9dcf480bdf..e1c7db5473 100644 --- a/nptl/descr.h +++ b/nptl/descr.h @@ -332,9 +332,8 @@ struct pthread /* True if thread must stop at startup time. */ bool stopped_start; - /* The parent's cancel handling at the time of the pthread_create - call. This might be needed to undo the effects of a cancellation. */ - int parent_cancelhandling; + /* Formerly used for dealing with cancellation. */ + int parent_cancelhandling_unsed; /* Lock to synchronize access to the descriptor. */ int lock; @@ -391,6 +390,11 @@ struct pthread /* Resolver state. */ struct __res_state res; + /* Signal mask for the new thread. Used during thread startup to + restore the signal mask. (Threads are launched with all signals + masked.) */ + sigset_t sigmask; + /* Indicates whether is a C11 thread created by thrd_creat. */ bool c11; diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index 7c752d0f99..afd379e89a 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -369,7 +369,6 @@ __free_tcb (struct pthread *pd) } } - /* Local function to start thread and handle cleanup. createthread.c defines the macro START_THREAD_DEFN to the declaration that its create_thread function will refer to, and @@ -385,10 +384,6 @@ START_THREAD_DEFN /* Initialize pointers to locale data. */ __ctype_init (); - /* Allow setxid from now onwards. */ - if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0) == -2)) - futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE); - #ifndef __ASSUME_SET_ROBUST_LIST if (__set_robust_list_avail >= 0) #endif @@ -399,18 +394,6 @@ START_THREAD_DEFN sizeof (struct robust_list_head)); } - /* If the parent was running cancellation handlers while creating - the thread the new thread inherited the signal mask. Reset the - cancellation signal mask. */ - if (__glibc_unlikely (pd->parent_cancelhandling & CANCELING_BITMASK)) - { - sigset_t mask; - __sigemptyset (&mask); - __sigaddset (&mask, SIGCANCEL); - INTERNAL_SYSCALL_CALL (rt_sigprocmask, SIG_UNBLOCK, &mask, - NULL, _NSIG / 8); - } - /* This is where the try/finally block should be created. For compilers without that support we do use setjmp. */ struct pthread_unwind_buf unwind_buf; @@ -432,6 +415,12 @@ START_THREAD_DEFN unwind_buf.priv.data.prev = NULL; unwind_buf.priv.data.cleanup = NULL; + __libc_signal_restore_set (&pd->sigmask); + + /* Allow setxid from now onwards. */ + if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0) == -2)) + futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE); + if (__glibc_likely (! not_first_call)) { /* Store the new cleanup handler info. */ @@ -722,10 +711,6 @@ __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr, CHECK_THREAD_SYSINFO (pd); #endif - /* Inform start_thread (above) about cancellation state that might - translate into inherited signal state. */ - pd->parent_cancelhandling = THREAD_GETMEM (THREAD_SELF, cancelhandling); - /* Determine scheduling parameters for the thread. */ if (__builtin_expect ((iattr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0, 0) && (iattr->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) != 0) @@ -771,6 +756,21 @@ __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr, ownership of PD (see CONCURRENCY NOTES above). */ bool stopped_start = false; bool thread_ran = false; + /* Block all signals, so that the new thread starts out with + signals disabled. This avoids race conditions in the thread + startup. */ + sigset_t original_sigmask; + __libc_signal_block_all (&original_sigmask); + + /* Conceptually, the new thread needs to inherit the signal mask of + this thread. Therefore, it needs to restore the saved signal + mask of this thread, so save it in the startup information. */ + pd->sigmask = original_sigmask; + + /* Reset the cancellation signal mask in case this thread is running + cancellation. */ + __sigdelset (&pd->sigmask, SIGCANCEL); + /* Start the thread. */ if (__glibc_unlikely (report_thread_creation (pd))) { @@ -813,6 +813,10 @@ __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr, retval = create_thread (pd, iattr, &stopped_start, STACK_VARIABLES_ARGS, &thread_ran); + /* Return to the previous signal mask, after creating the new + thread. */ + __libc_signal_restore_set (&original_sigmask); + if (__glibc_unlikely (retval != 0)) { if (thread_ran)