From patchwork Wed Jun 22 14:44:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Schwab X-Patchwork-Id: 1646578 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=Dl9Ge06l; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LSmPH59WQz9sG2 for ; Thu, 23 Jun 2022 00:44:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ADB183851C13 for ; Wed, 22 Jun 2022 14:44:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ADB183851C13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1655909068; bh=4gohKKnYugsJ4Ayjk5TgCb9HeRhM1yUAHICI2HahXws=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Dl9Ge06l6IstrvRxYb5npV4moJDpvk7OjN9502EIIXG1w7ilx7LUtX8Pvfud2Tv0f dCY/dl3Xe/WYFj56zFW/vrEIuzGGc9hiBVIU2/PWd8auzs8ESw/nJNPm3jOSXKdR00 vxIOD1X9YAeuMPa1FK7SaETfZTEil10nJ/2veV0c= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 395A7385702E for ; Wed, 22 Jun 2022 14:44:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 395A7385702E Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 5D7071FABC for ; Wed, 22 Jun 2022 14:44:13 +0000 (UTC) Received: from hawking.suse.de (unknown [10.168.4.11]) by relay2.suse.de (Postfix) with ESMTP id 588F22C141 for ; Wed, 22 Jun 2022 14:44:13 +0000 (UTC) Received: by hawking.suse.de (Postfix, from userid 17005) id 28C13444B63; Wed, 22 Jun 2022 16:44:13 +0200 (CEST) To: libc-alpha@sourceware.org Subject: [PATCH] debug: make __read_chk a cancellation point (bug 29274) X-Yow: --``I love KATRINKA because she drives a PONTIAC. We're going away now. I fed the cat. - Zippy'' Date: Wed, 22 Jun 2022 16:44:12 +0200 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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: Andreas Schwab via Libc-alpha From: Andreas Schwab Reply-To: Andreas Schwab Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" The __read_chk function, as the implementation behind the fortified read function, must be a cancellation point, thus it cannot use INLINE_SYSCALL. Reviewed-by: Siddhesh Poyarekar --- debug/Makefile | 7 ++++++ debug/read_chk.c | 10 -------- debug/tst-read-chk-cancel.c | 50 +++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 debug/tst-read-chk-cancel.c diff --git a/debug/Makefile b/debug/Makefile index 96029f32ee..456b349c4d 100644 --- a/debug/Makefile +++ b/debug/Makefile @@ -110,6 +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 # _FORTIFY_SOURCE tests. # Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and @@ -204,6 +205,10 @@ ifeq ($(have-ssp),yes) tests += tst-ssp-1 endif +ifeq ($(have-thread-library), yes) +tests += tst-read-chk-cancel +endif + ifeq (,$(CXX)) tests-unsupported = $(tests-cc-chk) endif @@ -242,3 +247,5 @@ $(objpfx)xtrace: xtrace.sh -e 's|@BINDIR@|$(bindir)|' -e 's|@PKGVERSION@|$(PKGVERSION)|' \ -e 's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^ > $@.new \ && rm -f $@ && mv $@.new $@ && chmod +x $@ + +$(objpfx)tst-read-chk-cancel: $(shared-thread-library) diff --git a/debug/read_chk.c b/debug/read_chk.c index 0cd58db8cb..274b4f93e9 100644 --- a/debug/read_chk.c +++ b/debug/read_chk.c @@ -16,12 +16,6 @@ . */ #include -#include -#ifdef HAVE_INLINED_SYSCALLS -# include -# include -#endif - ssize_t __read_chk (int fd, void *buf, size_t nbytes, size_t buflen) @@ -29,9 +23,5 @@ __read_chk (int fd, void *buf, size_t nbytes, size_t buflen) if (nbytes > buflen) __chk_fail (); -#ifdef HAVE_INLINED_SYSCALLS - return INLINE_SYSCALL (read, 3, fd, buf, nbytes); -#else return __read (fd, buf, nbytes); -#endif } diff --git a/debug/tst-read-chk-cancel.c b/debug/tst-read-chk-cancel.c new file mode 100644 index 0000000000..7e06afb596 --- /dev/null +++ b/debug/tst-read-chk-cancel.c @@ -0,0 +1,50 @@ +/* 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