From patchwork Fri Nov 29 11:08:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1202462 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-107545-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="RVhBKh65"; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="VAP4pr7V"; 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 47PWvk6Rmzz9sPj for ; Fri, 29 Nov 2019 22:08:18 +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:subject:date:message-id:mime-version :content-type:content-transfer-encoding; q=dns; s=default; b=EZd zvTU/52DY7kIf80Vfm0rgq0mbsge/44YsEn/r5OsorsvVUG5BTsv8pPg+xqq3WAy WGRLrkAg+kg7Ard9tNY8o+f084Pqk6MokVRZAa1YcOAz4QnEqpJMjQhV93cEqkjI 31R5rCG02pCVNZGDH+tm0ZVPFTFYoWFdgEIog8oM= 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:subject:date:message-id:mime-version :content-type:content-transfer-encoding; s=default; bh=Em92eW7xm Nvgia5E0ykX1gKwwug=; b=RVhBKh65IgDion5oxvizT6jpmnG95i/VDmTG0GAOj 25EfIBLUPqQVT/+/FZm7+1pu6gzLk2TETEvD/lJiAQ7V1AZeTKVwLSLKQLwbsv7u +PNe3hdCX0seMhznaeQRKpuw1SuIqZgNU6mfMBP847ueGqCyw+h4fRaQXAe/8XbQ Go= Received: (qmail 36896 invoked by alias); 29 Nov 2019 11:08:12 -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 36887 invoked by uid 89); 29 Nov 2019 11:08:11 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, SPF_PASS autolearn=ham version=3.3.1 spammy=Lock, Reset, race X-HELO: us-smtp-delivery-1.mimecast.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575025688; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=sDAUg6j5Hy2JhvgJaptsaT2BZeWO9c3LHvE+NA1K+y8=; b=VAP4pr7V50RFMhOGrVcZRJpDzw7o6odpr5vaY9tJbeNnEQogAo3p5KWqiWkVIb0iJh8VaC hHpSUNqh0UyZ17XlDb9/YA0Newfz2E0sqhTEGqTVUWYShE6IfyMu7ij8nc6dbxATzmhbky CTFuH5uNvYrHIEfSk1W4iXvTkWd4kCg= From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH] nptl: Start new threads with all signals blocked [BZ #25098] Date: Fri, 29 Nov 2019 12:08:02 +0100 Message-ID: <87y2vz2b59.fsf@oldenburg2.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 Rebased on master for the #ifdef SIGCANCEL changes. Fixed a bunch of typos. Re-tested on x86_64-linux-gnu and i686-linux-gnu. This is one of the patches needed for rseq enablement. Thanks, Florian 8<------------------------------------------------------------------8< 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(-) 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)