From patchwork Tue May 31 17:52:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 54589 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BEEF6395B455 for ; Tue, 31 May 2022 18:00:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BEEF6395B455 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654020029; bh=DTFzqPLdagPT+8RGAV8TKFTqEZhXf59f9o6IZlUtR6g=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=OS0PO/GTbY33xTs1bjXBKldcoolsvR06eUYznrMF1BJ88xJ6ItR7blJPhzL5MzzGk aA5AW9l9c1Wod/JEMf4sjZN9mco2bKPOYJAk1MloF1gFcj98nzljiBqW/e6Zja9Tya tklskOJ5fcpV09caEZFVF4JUvp+lQU1fpdAJ360c= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id 37B31396D828 for ; Tue, 31 May 2022 17:53:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 37B31396D828 Received: by mail-oi1-x22f.google.com with SMTP id m82so4422239oif.13 for ; Tue, 31 May 2022 10:53:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DTFzqPLdagPT+8RGAV8TKFTqEZhXf59f9o6IZlUtR6g=; b=vJHOGQR4H9Q0yi5enm3xCSWBYeVZqWz6uKkHfSHNYsk7nlC/pQsE8VN6x0RWhRx65x yYoZ6zin/OJCUosAIba2ymtgbpDA7vN18isImTQ7lUll14uQM5sukZvqY7bOvDqdDriN Mw0eIKLy5aVgZAou70P0hfGFTa++92doMAMWOL5W05vknSUvP2WwEemjwgwwXnpdDjXq KuYIEYBJoOCnvjcmqmLIEqbK5XUZGWkAgZ6TpBrlkztWM+WZXL8Mmo9hMncgV/DXbSOq MkhQcmG+UKd7xYaTaSNq82e8qbJ0A6z0KBUQ9KIn0vpLhru/KtQt9wAD/EV8ZauWk75A MUKw== X-Gm-Message-State: AOAM5303/rVScHgEpKYNA45abIQbRIXSvncFxVhCAhfIus6vfy74RYv8 dhRe2ROD/xjnkSlrmnmtE7sds0clM4qyIw== X-Google-Smtp-Source: ABdhPJxC8GMVfbn3sqEQrAo5BFQiBVuQ5MorqqYDavd3taZ5O7RJqOrR4YxeZmAMPtXQ9kn9MqMdCA== X-Received: by 2002:a05:6808:1a8c:b0:32b:df97:b1fa with SMTP id bm12-20020a0568081a8c00b0032bdf97b1famr9736464oib.184.1654019590762; Tue, 31 May 2022 10:53:10 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:e39c:980f:eaf1:7a30:10ef]) by smtp.gmail.com with ESMTPSA id f30-20020a4a8921000000b0035f627ac898sm6402626ooi.10.2022.05.31.10.53.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 10:53:10 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 10/12] nptl: Use exit_lock when accessing TID on pthread_sigqueue Date: Tue, 31 May 2022 14:52:53 -0300 Message-Id: <20220531175255.1513396-11-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220531175255.1513396-1-adhemerval.zanella@linaro.org> References: <20220531175255.1513396-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Also return EINVAL if the thread is already terminated at the time of the call. Checked on x86_64-linux-gnu. --- nptl/pthread_sigqueue.c | 56 ++++++++++++++-------------- sysdeps/pthread/tst-pthread-exited.c | 6 +++ 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/nptl/pthread_sigqueue.c b/nptl/pthread_sigqueue.c index 48dc3ca4ee..e13cf9fe12 100644 --- a/nptl/pthread_sigqueue.c +++ b/nptl/pthread_sigqueue.c @@ -16,6 +16,7 @@ . */ #include +#include #include #include #include @@ -27,41 +28,40 @@ int __pthread_sigqueue (pthread_t threadid, int signo, const union sigval value) { -#ifdef __NR_rt_tgsigqueueinfo - struct pthread *pd = (struct pthread *) threadid; - - /* Force load of pd->tid into local variable or register. Otherwise - if a thread exits between ESRCH test and tgkill, we might return - EINVAL, because pd->tid would be cleared by the kernel. */ - pid_t tid = atomic_forced_read (pd->tid); - if (__glibc_unlikely (tid <= 0)) - /* Not a valid thread handle. */ - return ESRCH; - /* Disallow sending the signal we use for cancellation, timers, for the setxid implementation. */ if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID) return EINVAL; - pid_t pid = getpid (); + struct pthread *pd = (struct pthread *) threadid; - /* Set up the siginfo_t structure. */ - siginfo_t info; - memset (&info, '\0', sizeof (siginfo_t)); - info.si_signo = signo; - info.si_code = SI_QUEUE; - info.si_pid = pid; - info.si_uid = __getuid (); - info.si_value = value; + /* Block all signals, as required by pd->exit_lock. */ + sigset_t old_mask; + __libc_signal_block_all (&old_mask); + __libc_lock_lock (pd->exit_lock); - /* We have a special syscall to do the work. */ - int val = INTERNAL_SYSCALL_CALL (rt_tgsigqueueinfo, pid, tid, signo, - &info); - return (INTERNAL_SYSCALL_ERROR_P (val) - ? INTERNAL_SYSCALL_ERRNO (val) : 0); -#else - return ENOSYS; -#endif + int res; + if (pd->tid != 0) + { + pid_t pid = getpid (); + + siginfo_t info = { 0 }; + info.si_signo = signo; + info.si_code = SI_QUEUE; + info.si_pid = pid; + info.si_uid = __getuid (); + info.si_value = value; + + res = -INTERNAL_SYSCALL_CALL (rt_tgsigqueueinfo, pid, pd->tid, signo, + &info); + } + else + res = EINVAL; + + __libc_lock_unlock (pd->exit_lock); + __libc_signal_restore_set (&old_mask); + + return res; } versioned_symbol (libc, __pthread_sigqueue, pthread_sigqueue, GLIBC_2_34); diff --git a/sysdeps/pthread/tst-pthread-exited.c b/sysdeps/pthread/tst-pthread-exited.c index 7f5f339b3e..c4d8492f8c 100644 --- a/sysdeps/pthread/tst-pthread-exited.c +++ b/sysdeps/pthread/tst-pthread-exited.c @@ -75,6 +75,12 @@ do_test (void) TEST_COMPARE (r, EINVAL); } + { + union sigval value = { 0 }; + int r = pthread_sigqueue (thr, SIGUSR1, value); + TEST_COMPARE (r, EINVAL); + } + xpthread_join (thr); return 0;