From patchwork Mon Jan 6 15:57:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 1218279 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=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-108463-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="ZbB3NPt/"; dkim=pass (2048-bit key; unprotected) header.d=efficios.com header.i=@efficios.com header.b="VdaRCGF9"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47s0XG0mNVz9s29 for ; Tue, 7 Jan 2020 02:57:49 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=slp2rBN1+k6dsPNaBachvSmHDudjK7w FRF16kfVUqSQDrCFtkioxABvWo1ofZO4NN0BtUBG8I0BvMcktXxez1o8qogQ4YbU U2ATBUjSwYv4GWoUQL1KlyZCtmyJ+2tajJc1QMJB/OIm7m5pZD3iNgbTMPo4Q4tC 681hQKsjwjL0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=nQJ9vVqa+sOkN1ROgv+hzmxQcIc=; b=ZbB3N Pt/alcD0dj7h5uEGWZOS18uijiABejyeiPc95nBaCh/WM2LnM5W9p4LXBHXwzpD9 V9z57Z66qxflNWuvuEyJOojkFyXDKctAaSkbl/rkAh+tipNkBQVOKQOBz6LUz7l3 TwlLQXix/LD3IJe32z7+ghtvlIlIxbz7tcQBb8= Received: (qmail 94432 invoked by alias); 6 Jan 2020 15:57:25 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 94354 invoked by uid 89); 6 Jan 2020 15:57:25 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=NOTES, Local, cancel, formerly X-HELO: mail.efficios.com DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 427E51EA17C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1578326238; bh=BJ5M7IBGK9K+rzpolsPdt5cqMU4qJ74RY3HSNcYtXI0=; h=From:To:Date:Message-Id; b=VdaRCGF94Wj0H3+xk8AqMfzFpYOM5uf6x/8sptyRpb8ajsDN2E7AsCzP3rDLHPTXQ g+Uzd1R/SQZlPQz82Oma6uscCzpsInETS4B+RKFgdfPz5YaD1rtbL8R1vXdfsyoxpM 0S4YycK54ZeiBG+RB5sutieTLYZFi9F++LN4TL+fLpXWafYIvD6KE8JsKj8hnRKkA2 oVoed145D7773jib2HKR/5GdvRFvDi7nY7w89elkRv+6ojLd1NdP+B+v3HmvS0Rxjh /YO8SFbQ//zexTeyE0fvz1dcuTH5Bp2ZhXrsYl9BL0lUHVmGWBBh2mo3D9pG5HLXdl MdekVoJwOtJVQ== From: Mathieu Desnoyers To: Carlos O'Donell Cc: Florian Weimer , Joseph Myers , Szabolcs Nagy , libc-alpha@sourceware.org Subject: [RFC PATCH glibc 03/13] nptl: Start new threads with all signals blocked [BZ #25098] Date: Mon, 6 Jan 2020 10:57:03 -0500 Message-Id: <20200106155713.397-4-mathieu.desnoyers@efficios.com> In-Reply-To: <20200106155713.397-1-mathieu.desnoyers@efficios.com> References: <20200106155713.397-1-mathieu.desnoyers@efficios.com> 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 | 47 +++++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/nptl/descr.h b/nptl/descr.h index d3f863aa18..70d76bc63b 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 5682c9c2c0..b220ec526b 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); - #ifdef __NR_set_robust_list # ifndef __ASSUME_SET_ROBUST_LIST if (__set_robust_list_avail >= 0) @@ -402,19 +397,6 @@ START_THREAD_DEFN } #endif - /* 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)) - { - INTERNAL_SYSCALL_DECL (err); - sigset_t mask; - __sigemptyset (&mask); - __sigaddset (&mask, SIGCANCEL); - (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, 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; @@ -436,6 +418,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. */ @@ -726,10 +714,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) @@ -775,6 +759,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))) { @@ -817,6 +816,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)