From patchwork Thu Oct 17 23:20:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1998808 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dztqlsvg; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XV3kR0FZnz1xw2 for ; Fri, 18 Oct 2024 10:21:34 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E35E43858401 for ; Thu, 17 Oct 2024 23:21:32 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id EF85B3858D20 for ; Thu, 17 Oct 2024 23:21:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EF85B3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EF85B3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729207275; cv=none; b=Grc8rtHlI++CBC6ZXb6v8kgBIWPStKjpULhRMHjZ3yLa3gSgslFzJJw/W7zxnqT8JsZYGD36yUxWkI6fwbPtULDm3JHJKNIb7ykUxUpjNEMrAnaftWxJW0DusZWKtgFSKqcdwppm73MbSAx5+6Ml7sdugkPl0ZDph7f5QV7ofO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729207275; c=relaxed/simple; bh=kmYxerOv+PKx7hnL5Ql6w1U2urUtg8f7yBgnPaYKR6c=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=qxwqpoMKELMXRB1RRIbG/Q6VdDOE2gZ22V89XozMpT6O/2V5+Gkk2GoCu8hUSKCBVGNYUsDhtobkQyS9leLtpytN3/9BEzb57dsiwIvuWWCr/cpVff18kA1EtRLCZawCgSGdjw5ugBcaA4b9s5WjuybObpmroBiygksFoTlfO2o= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729207272; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=KZOwpaOl/zgRlClwCaU8p3lzixLADW4403ai76qTFgQ=; b=dztqlsvgGSx97CrUXSpWLOQshoWO78eydpCjf6nIDkFv0d6/Jt6TwPXXGNdJcAKZ1GnJ8b csmBR/Z0fts6nmRPwYVyJk1W+t/YBnt3Ycc4fFYUrvpoT8REaPpcWz9szeGJ5TiiuLjxEH 5HWf2678UmTqJub5eI0YBE2iOn/G5PI= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-190-xdIywlMGNE24stle3boloQ-1; Thu, 17 Oct 2024 19:21:11 -0400 X-MC-Unique: xdIywlMGNE24stle3boloQ-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4314a22ed8bso10063045e9.1 for ; Thu, 17 Oct 2024 16:21:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729207270; x=1729812070; h=mime-version:message-id:subject:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=KZOwpaOl/zgRlClwCaU8p3lzixLADW4403ai76qTFgQ=; b=CboqqLSJncvuAMIwes4isl5tiZzVw5WeQhWdaPAWTAwTrMBmAw/CRHjBNnjwawEKNh PWerfmvwv/cFq+B1U9fTDcvCbe+f11AfbLW/bKkkDRceOfoTu5KXbLCnJoxngwaFi+Kd auJ0EMHz7WXwgBilKk5n95Yol+3qkXY/msEZg1eKYed2ysQZUn3KSF6m7T7mqB27095e q1ZAczensFcVDeyJ6zaoc5eP+JiFr6bw4puqyrgs+w/Q42FnkDiNKkOdWz1ngZwEtZUi BG3jlhBUnkBv7qt6bS1cKnlxKrTkX8uVw5eWhKhz7UFTJT3vsCatULJl51Nyt1nX5Mzs IykQ== X-Gm-Message-State: AOJu0YxKKp5768DTnunVbr4pt5cc+RzxDByfALkhqnD7+Xf2sfQyYoZk iz9c+wstDMHt98HJCHTjKvs/nnzova0VNVLJJ43mDNuqRdYCyJdfgmrhMSFghruWJwNdTFtcolY shcvaDhsuHwR2ln86rIRIqJvRpqsv9atcVMDRC++WB++4V+ybxTXO7h3S2bhZs+ycY8+mDDBHxV 5sHEF4RYq++Kvix0tu+8lzvvhR9jzqSpxOB3HBqZlkSg== X-Received: by 2002:a05:600c:4f4a:b0:42c:bae0:f05b with SMTP id 5b1f17b1804b1-43161636fd6mr2401205e9.1.1729207269823; Thu, 17 Oct 2024 16:21:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH2Fer8VJObzRLeihLAU+7iJ1Q8XZVAl3FiowLgYvHajeCNagWKSV0NVrKWLjUi1RuqPwthFg== X-Received: by 2002:a05:600c:4f4a:b0:42c:bae0:f05b with SMTP id 5b1f17b1804b1-43161636fd6mr2401045e9.1.1729207269227; Thu, 17 Oct 2024 16:21:09 -0700 (PDT) Received: from digraph.polyomino.org.uk (digraph.polyomino.org.uk. [2001:8b0:bf73:93f7::51bb:e332]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-431606c696fsm9878645e9.34.2024.10.17.16.21.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 16:21:08 -0700 (PDT) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.97) (envelope-from ) id 1t1ZnA-0000000FtAH-0igN for libc-alpha@sourceware.org; Thu, 17 Oct 2024 23:20:28 +0000 Date: Thu, 17 Oct 2024 23:20:28 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: Check time arguments to pthread_timedjoin_np and pthread_clockjoin_np Message-ID: <86a3a04f-d503-cb59-f520-59bf76eeb0bb@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org The pthread_timedjoin_np and pthread_clockjoin_np functions do not check that a valid time has been specified. The documentation for these functions in the glibc manual isn't sufficiently detailed to say if they should, but consistency with POSIX functions such as pthread_mutex_timedlock and pthread_cond_timedwait strongly indicates that an EINVAL error is appropriate (even if there might be some ambiguity about exactly where such a check should go in relation to other checks for whether the thread exists, whether it's immediately joinable, etc.). Copy the logic for such a check used in pthread_rwlock_common.c. pthread_join_common had some logic calling valid_nanoseconds before commit 9e92278ffad441daf588ff1ff5bd8094aa33fbfd, "nptl: Remove clockwait_tid"; I haven't checked exactly what cases that detected. Tested for x86_64 and x86. diff --git a/nptl/pthread_join_common.c b/nptl/pthread_join_common.c index 9c685c79cf..273db80543 100644 --- a/nptl/pthread_join_common.c +++ b/nptl/pthread_join_common.c @@ -49,6 +49,12 @@ __pthread_clockjoin_ex (pthread_t threadid, void **thread_return, /* We cannot wait for the thread. */ return EINVAL; + /* Make sure the clock and time specified are valid. */ + if (abstime + && __glibc_unlikely (!futex_abstimed_supported_clockid (clockid) + || ! valid_nanoseconds (abstime->tv_nsec))) + return EINVAL; + struct pthread *self = THREAD_SELF; int result = 0; diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 04ea56559e..939438fffc 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -174,6 +174,7 @@ tests += \ tst-join13 \ tst-join14 \ tst-join15 \ + tst-join16 \ tst-key1 \ tst-key2 \ tst-key3 \ @@ -291,6 +292,7 @@ tests-time64 += \ tst-cnd-timedwait-time64 \ tst-cond11-time64 \ tst-join14-time64 \ + tst-join16-time64 \ tst-mtx-timedlock-time64 \ tst-rwlock14-time64 \ tst-sem5-time64 \ diff --git a/sysdeps/pthread/tst-join16-time64.c b/sysdeps/pthread/tst-join16-time64.c new file mode 100644 index 0000000000..730cc56563 --- /dev/null +++ b/sysdeps/pthread/tst-join16-time64.c @@ -0,0 +1 @@ +#include "tst-join16.c" diff --git a/sysdeps/pthread/tst-join16.c b/sysdeps/pthread/tst-join16.c new file mode 100644 index 0000000000..8bf37b5e42 --- /dev/null +++ b/sysdeps/pthread/tst-join16.c @@ -0,0 +1,87 @@ +/* Test pthread_timedjoin_np and pthread_clockjoin_np with an invalid timeout. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include + + +#define CLOCK_USE_TIMEDJOIN (-1) + +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; + +static void * +tf (void *arg) +{ + xpthread_mutex_lock (&lock); + xpthread_mutex_unlock (&lock); + return (void *) 42l; +} + +static int +do_test_clock (clockid_t clockid) +{ + const clockid_t clockid_for_get = + (clockid == CLOCK_USE_TIMEDJOIN) ? CLOCK_REALTIME : clockid; + + xpthread_mutex_lock (&lock); + pthread_t th = xpthread_create (NULL, tf, NULL); + + void *status; + int ret; + struct timespec timeout = xclock_now (clockid_for_get); + timeout.tv_sec += 2; + timeout.tv_nsec = -1; + if (clockid == CLOCK_USE_TIMEDJOIN) + ret = pthread_timedjoin_np (th, &status, &timeout); + else + ret = pthread_clockjoin_np (th, &status, clockid, &timeout); + TEST_COMPARE (ret, EINVAL); + timeout.tv_nsec = 1000000000; + if (clockid == CLOCK_USE_TIMEDJOIN) + ret = pthread_timedjoin_np (th, &status, &timeout); + else + ret = pthread_clockjoin_np (th, &status, clockid, &timeout); + TEST_COMPARE (ret, EINVAL); + xpthread_mutex_unlock (&lock); + timeout.tv_nsec = 0; + ret = pthread_join (th, &status); + TEST_COMPARE (ret, 0); + if (status != (void *) 42l) + FAIL_EXIT1 ("return value %p, expected %p\n", status, (void *) 42l); + + return 0; +} + +static int +do_test (void) +{ + puts ("testing pthread_timedjoin_np"); + do_test_clock (CLOCK_USE_TIMEDJOIN); + puts ("testing CLOCK_REALTIME"); + do_test_clock (CLOCK_REALTIME); + puts ("testing CLOCK_MONOTONIC"); + do_test_clock (CLOCK_MONOTONIC); + return 0; +} + +#include