From patchwork Tue Aug 30 13:53:02 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: 1671901 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.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=PEXtaYV2; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MH80Z1mtsz1ynk for ; Tue, 30 Aug 2022 23:53:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AF7A43839072 for ; Tue, 30 Aug 2022 13:53:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AF7A43839072 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1661867609; bh=zd6VBik8NiSnoBuqSA/Lssh//wlWTIRpa6GdwEImZ1k=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=PEXtaYV2GyqvCBKuRBM9W1piFFaKk8CkabzKoSH4Eo9CZ5zH+nP23VqeYX1+Sx3S8 48buo0BwG2veyyvOiL86h++lI+I+KSUy8JFg3XlZilKDm2BO41OOR7hZsD6WXm/YoO mOSfqv/ZGT5Xk34QTHs7+RurVyrhMMOXnKIGsews= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x29.google.com (mail-oa1-x29.google.com [IPv6:2001:4860:4864:20::29]) by sourceware.org (Postfix) with ESMTPS id 6FD6E3851C19 for ; Tue, 30 Aug 2022 13:53:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6FD6E3851C19 Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-11f4e634072so2579553fac.13 for ; Tue, 30 Aug 2022 06:53:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=zd6VBik8NiSnoBuqSA/Lssh//wlWTIRpa6GdwEImZ1k=; b=0ARPJGU1i9WQlNujCV9UxuxOWH6Nwl1DsFNsbJoDDyhP12TcZFeIaU0AmqVc5D0HTH WCrXKek6GVMYF/8EjsdO2kwAEhpWqAvmyYWO1yTmwA5sKNrjeaDES46oB1ete6YxdJ2v W32NlXQ5o0C+wg3RYP+2j9nHZLU6jfOX4f1uct2rrO0VbPxNFN1iDuTi1f7pgP8UzP+R SEdgAEJloW5h8s46wju9cJUA7ptjbn3hMWfycfYS0iExwPNPFGalYWcJnS6SbZTlh01A fTANWVBnq6vI8RY1GN3jWWLI06U5o/Uy47Bxaf+TEqQE9zNdjPmt54KxT0Gq/kd2sPGr 4MoQ== X-Gm-Message-State: ACgBeo1HuvUWgSBDEWTgehaZOr0lSCU605vwPctz5gb1BwA9tKL5mC0y BC2XZgiY17xb5cajT7QXMcn9WCX9sBN0Vg== X-Google-Smtp-Source: AA6agR4C1EE7G7vP33dToYQJlKxJWPLoCj8UrtXY2Wxcj9QfPMxxzOcbSeHr6x1b0J0OMWEbcOMvvg== X-Received: by 2002:a05:6870:349:b0:f1:928d:c803 with SMTP id n9-20020a056870034900b000f1928dc803mr9903552oaf.19.1661867588135; Tue, 30 Aug 2022 06:53:08 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:745e:31d8:f463:7d91:9d28]) by smtp.gmail.com with ESMTPSA id a6-20020a05680804c600b00344cc0c4606sm6015966oie.58.2022.08.30.06.53.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Aug 2022 06:53:07 -0700 (PDT) To: libc-alpha@sourceware.org, Carlos O'Donell Subject: [PATCH v4] debug: test for more required cacellation points (BZ# 29274) Date: Tue, 30 Aug 2022 10:53:02 -0300 Message-Id: <20220830135302.2750242-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 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 Cc: Andreas Schwab Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Generalize the test for cancellation point in __read_chk to also test the other fortified functions with required cancellation points. Since there is not easy way to force some syscalls to block (for instance pread) the test tests two modes: cancellation on blocked syscalls and early cancellation on pending request. Checked on x86_64-linux-gnu and aarch64-linux-gnu. Co-authored-by: Andreas Schwab Reviewed-by: Carlos O'Donell --- debug/Makefile | 6 +- debug/tst-chk-cancel.c | 263 ++++++++++++++++++++++++++++++++++++ debug/tst-read-chk-cancel.c | 50 ------- 3 files changed, 266 insertions(+), 53 deletions(-) create mode 100644 debug/tst-chk-cancel.c delete mode 100644 debug/tst-read-chk-cancel.c diff --git a/debug/Makefile b/debug/Makefile index 456b349c4d..99cdf3221b 100644 --- a/debug/Makefile +++ b/debug/Makefile @@ -110,7 +110,7 @@ CPPFLAGS-tst-longjmp_chk2.c += -D_FORTIFY_SOURCE=1 CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1 CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2 -CPPFLAGS-tst-read-chk-cancel.c += -D_FORTIFY_SOURCE=2 +CPPFLAGS-tst-chk-cancel.c += -D_FORTIFY_SOURCE=2 # _FORTIFY_SOURCE tests. # Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and @@ -206,7 +206,7 @@ tests += tst-ssp-1 endif ifeq ($(have-thread-library), yes) -tests += tst-read-chk-cancel +tests += tst-chk-cancel endif ifeq (,$(CXX)) @@ -248,4 +248,4 @@ $(objpfx)xtrace: xtrace.sh -e 's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^ > $@.new \ && rm -f $@ && mv $@.new $@ && chmod +x $@ -$(objpfx)tst-read-chk-cancel: $(shared-thread-library) +$(objpfx)tst-chk-cancel: $(shared-thread-library) diff --git a/debug/tst-chk-cancel.c b/debug/tst-chk-cancel.c new file mode 100644 index 0000000000..5c6e22697f --- /dev/null +++ b/debug/tst-chk-cancel.c @@ -0,0 +1,263 @@ +/* Test for required cancellation points in fortified functions (BZ #29274) + Copyright (C) 2022 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 +#include +#include +#include + +/* Cleanup handling test. */ +static int cl_called; + +static void +cl (void *arg) +{ + ++cl_called; +} + +static int fds[2]; +static pthread_barrier_t barrier; + +static void * +tf_read (void *n) +{ + pthread_cleanup_push (cl, NULL); + + xpthread_barrier_wait (&barrier); + + /* This call should be forwarded to __read_chk because the buffer size + is known, but the read length is non-constant. */ + char c; + if (read (fds[0], &c, (uintptr_t) n) != 1) + return (void *) -1L; + + pthread_cleanup_pop (0); + return 0; +} + +static void * +tf_pread (void *n) +{ + pthread_cleanup_push (cl, NULL); + + xpthread_barrier_wait (&barrier); + + /* This call should be forwarded to __pread_chk because the buffer size + is known, but the read length is non-constant. */ + char c; + if (pread (fds[0], &c, (uintptr_t) n, 0) != 1) + return (void *) -1L; + + pthread_cleanup_pop (0); + return 0; +} + +static void * +tf_pread64 (void *n) +{ + pthread_cleanup_push (cl, NULL); + + xpthread_barrier_wait (&barrier); + + /* This call should be forwarded to __pread64_chk because the buffer size + is known, but the read length is non-constant. */ + char c; + if (pread64 (fds[0], &c, (uintptr_t) n, 0) != 1) + return (void *) -1L; + + pthread_cleanup_pop (0); + return 0; +} + +static void * +tf_poll (void *n) +{ + pthread_cleanup_push (cl, NULL); + + xpthread_barrier_wait (&barrier); + + /* This call should be forwarded to __poll_chk because the pollfd size + is known, but the number of entries is non-constant. */ + struct pollfd pfd = { fds[0], POLLIN, 0 }; + if (poll (&pfd, (uintptr_t) n, -1) != 1) + return (void *) -1L; + + pthread_cleanup_pop (0); + return 0; +} + +static void * +tf_ppoll (void *n) +{ + pthread_cleanup_push (cl, NULL); + + xpthread_barrier_wait (&barrier); + + /* This call should be forwarded to __ppoll_chk because the pollfd size + is known, but the number of entries is non-constant. */ + struct pollfd pfd = { fds[0], POLLIN, 0 }; + if (ppoll (&pfd, (uintptr_t) n, 0, 0) != 1) + return (void *) -1L; + + pthread_cleanup_pop (0); + return 0; +} + +static void * +tf_recv (void *n) +{ + pthread_cleanup_push (cl, NULL); + + xpthread_barrier_wait (&barrier); + + /* This call should be forwarded to __ppoll_chk because the pollfd size + is known, but the number of entries is non-constant. */ + char c; + if (recv (fds[0], &c, (uintptr_t) n, 0) != 1) + return (void *) -1L; + + pthread_cleanup_pop (0); + return 0; +} + +static void * +tf_recvfrom (void *n) +{ + pthread_cleanup_push (cl, NULL); + + xpthread_barrier_wait (&barrier); + + /* This call should be forwarded to __ppoll_chk because the pollfd size + is known, but the number of entries is non-constant. */ + char c; + if (recvfrom (fds[0], &c, (uintptr_t) n, 0, NULL, NULL) != 1) + return (void *) -1L; + + pthread_cleanup_pop (0); + return 0; +} + +static struct cancel_tests +{ + const char *name; + void *(*tf) (void *); + bool only_early; +#define ADD_TEST(name, early) { #name, tf_##name, early } +} tests[] = +{ + ADD_TEST (poll, false), + ADD_TEST (ppoll, false), + ADD_TEST (pread, true), + ADD_TEST (pread64, true), + ADD_TEST (read, false), + ADD_TEST (recv, false), + ADD_TEST (recvfrom, false), +}; + +/* Set the send buffer of socket S to 1 byte so any send operation + done with WRITE_BUFFER_SIZE bytes will force syscall blocking. */ +static void +set_socket_buffer (int s) +{ + int val = 1; + socklen_t len = sizeof (val); + + TEST_VERIFY_EXIT (setsockopt (s, SOL_SOCKET, SO_SNDBUF, &val, + sizeof (val)) == 0); + TEST_VERIFY_EXIT (getsockopt (s, SOL_SOCKET, SO_SNDBUF, &val, &len) == 0); + printf ("%s: got size %d\n", __func__, val); +} + +static int +do_test (void) +{ + xpthread_barrier_init (&barrier, 0, 2); + + if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) != 0) + FAIL_EXIT1 ("socketpair: %m"); + set_socket_buffer (fds[1]); + + /* This is the !only_early test. It is a late cancel test that has a sleep + in the main thread in an attempt to allow the child thread to reach and + block on the syscall. The cancellation should happen with high + probability when the child thread blocked on the syscall, and that is + the intent of the test (syscall cancellation registration complete). */ + for (int i = 0; i < array_length (tests); i++) + { + if (tests[i].only_early) + continue; + + xpthread_barrier_init (&barrier, NULL, 2); + /* Reset the counter for the cleanup handler. */ + cl_called = 0; + + pthread_t thr = xpthread_create (0, tests[i].tf, (void *) 1L); + /* After this wait the threads cancellation handler is installed. */ + xpthread_barrier_wait (&barrier); + + struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 }; + TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME, 0, &ts, &ts)); + + xpthread_cancel (thr); + + void *status = xpthread_join (thr); + TEST_VERIFY (status == PTHREAD_CANCELED); + TEST_COMPARE (cl_called, 1); + + printf ("in-time cancel test of '%s' successful\n", tests[i].name); + } + + /* This is a early cancel test that happens before the syscall is issued. + In this case there is no signal involved, pthread_cancel will just mark + the target thread canceled, since asynchronous mode is not set, and the + cancellable entrypoint will check if the thread is set as cancelled and + exit early. + + Keep in mind that neither pthread_barrier_wait nor pthread_cleanup_push + act as cancellation entrypoints. */ + for (int i = 0; i < array_length (tests); i++) + { + xpthread_barrier_init (&barrier, NULL, 2); + /* Reset the counter for the cleanup handler. */ + cl_called = 0; + + /* After this wait the cancellation handler is in place. */ + pthread_t thr = xpthread_create (0, tests[i].tf, NULL); + + xpthread_cancel (thr); + xpthread_barrier_wait (&barrier); + + void *status = xpthread_join (thr); + TEST_VERIFY (status == PTHREAD_CANCELED); + TEST_COMPARE (cl_called, 1); + + printf ("early cancel test of '%s' successful\n", tests[i].name); + } + + xpthread_barrier_destroy (&barrier); + + return 0; +} + +#include diff --git a/debug/tst-read-chk-cancel.c b/debug/tst-read-chk-cancel.c deleted file mode 100644 index 7e06afb596..0000000000 --- a/debug/tst-read-chk-cancel.c +++ /dev/null @@ -1,50 +0,0 @@ -/* Test that __read_chk is a cancellation point (BZ #29274) - Copyright (C) 2022 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 - -static int pipe_fds[2]; -static pthread_barrier_t barrier; - -static void * -read_thread (void *n) -{ - xpthread_barrier_wait (&barrier); - char c; - /* This call should be forwarded to __read_chk because the buffer size - is known, but the read length is non-constant. */ - if (read (pipe_fds[0], &c, (uintptr_t) n) != 1) - return (void *) -1L; - return 0; -} - -static int -do_test (void) -{ - xpthread_barrier_init (&barrier, 0, 2); - xpipe (pipe_fds); - pthread_t thr = xpthread_create (0, read_thread, (void *) 1L); - xpthread_barrier_wait (&barrier); - xpthread_cancel (thr); - xpthread_join (thr); - return 0; -} - -#include