From patchwork Thu Feb 11 17:45:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Palethorpe X-Patchwork-Id: 1439598 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=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=susede1 header.b=PP790CBg; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Dc3wX6fklz9rx8 for ; Fri, 12 Feb 2021 04:46:52 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id EAE8C3C6CF3 for ; Thu, 11 Feb 2021 18:46:49 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [217.194.8.6]) by picard.linux.it (Postfix) with ESMTP id C2D3D3C6CFE for ; Thu, 11 Feb 2021 18:46:05 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-6.smtp.seeweb.it (Postfix) with ESMTPS id 3BFA3140035F for ; Thu, 11 Feb 2021 18:46:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1613065561; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=y2th6nVajPWW6fkyRMKvXLOHS6JWi6lkRoW7gXfdmoE=; b=PP790CBgKSQV9o9igYei84hdD+MGMCwHDzw09uBdWg4Oj2TxJV8mnukBWXWOcIhoq1g96T FHj+L1+5nZDPsk69+PAhq7zRPLItzLyioBlkFte5HEpg6HXSIr2wKpwIkLm5QT1BAzZIbL CwgZ18onv4RHdMsKnumgJ5crIVL6pQE= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id C6B0CAC69; Thu, 11 Feb 2021 17:46:01 +0000 (UTC) To: ltp@lists.linux.it Date: Thu, 11 Feb 2021 17:45:39 +0000 Message-Id: <20210211174543.25003-2-rpalethorpe@suse.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210211174543.25003-1-rpalethorpe@suse.com> References: <20210211174543.25003-1-rpalethorpe@suse.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-6.smtp.seeweb.it Subject: [LTP] [PATCH v2 1/5] API: Add close_range syscall wrapper X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Palethorpe via ltp From: Richard Palethorpe Reply-To: Richard Palethorpe Cc: Richard Palethorpe Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Add the syscall number and a wrapper function. Presently there is no wrapper function in libc. Signed-off-by: Richard Palethorpe --- configure.ac | 2 ++ include/lapi/close_range.h | 28 ++++++++++++++++++++++++++++ include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/arc.in | 1 + include/lapi/syscalls/arm.in | 1 + include/lapi/syscalls/hppa.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/ia64.in | 1 + include/lapi/syscalls/mips_n32.in | 1 + include/lapi/syscalls/mips_n64.in | 1 + include/lapi/syscalls/mips_o32.in | 1 + include/lapi/syscalls/powerpc.in | 1 + include/lapi/syscalls/powerpc64.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/s390x.in | 1 + include/lapi/syscalls/sh.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/sparc64.in | 1 + include/lapi/syscalls/x86_64.in | 1 + 19 files changed, 47 insertions(+) create mode 100644 include/lapi/close_range.h diff --git a/configure.ac b/configure.ac index 9fb50c173..d4bef5e45 100644 --- a/configure.ac +++ b/configure.ac @@ -43,6 +43,7 @@ AC_CHECK_HEADERS_ONCE([ \ linux/can.h \ linux/cgroupstats.h \ linux/cryptouser.h \ + linux/close_range.h \ linux/dccp.h \ linux/fs.h \ linux/genetlink.h \ @@ -77,6 +78,7 @@ AC_SUBST(HAVE_FTS_H, $have_fts) AC_CHECK_FUNCS_ONCE([ \ clone3 \ + close_range \ copy_file_range \ epoll_pwait \ execveat \ diff --git a/include/lapi/close_range.h b/include/lapi/close_range.h new file mode 100644 index 000000000..19db52d3d --- /dev/null +++ b/include/lapi/close_range.h @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* Copyright (c) 2021 SUSE LLC */ + +#ifndef LAPI_CLOSE_RANGE__ +# define LAPI_CLOSE_RANGE__ + +# include "lapi/syscalls.h" + +# ifdef HAVE_LINUX_CLOSE_RANGE_H +# include +# endif + +# ifndef CLOSE_RANGE_UNSHARE +# define CLOSE_RANGE_UNSHARE (1U << 1) +# endif + +# ifndef CLOSE_RANGE_CLOEXEC +# define CLOSE_RANGE_CLOEXEC (1U << 2) +# endif + +# ifndef HAVE_CLOSE_RANGE +static inline int close_range(unsigned int fd, unsigned int max_fd, + unsigned int flags) +{ + return tst_syscall(__NR_close_range, fd, max_fd, flags); +} +# endif +#endif /* LAPI_CLOSE_RANGE_H__ */ diff --git a/include/lapi/syscalls/aarch64.in b/include/lapi/syscalls/aarch64.in index 07556933f..2def6ba3d 100644 --- a/include/lapi/syscalls/aarch64.in +++ b/include/lapi/syscalls/aarch64.in @@ -310,6 +310,7 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 _sysctl 1078 diff --git a/include/lapi/syscalls/arc.in b/include/lapi/syscalls/arc.in index 0cadb150c..9bcd84706 100644 --- a/include/lapi/syscalls/arc.in +++ b/include/lapi/syscalls/arc.in @@ -310,5 +310,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/arm.in b/include/lapi/syscalls/arm.in index 395d53a68..98c840cb8 100644 --- a/include/lapi/syscalls/arm.in +++ b/include/lapi/syscalls/arm.in @@ -388,5 +388,6 @@ fsmount (__NR_SYSCALL_BASE+432) fspick (__NR_SYSCALL_BASE+433) pidfd_open (__NR_SYSCALL_BASE+434) clone3 (__NR_SYSCALL_BASE+435) +close_range (__NR_SYSCALL_BASE+436) openat2 (__NR_SYSCALL_BASE+437) pidfd_getfd (__NR_SYSCALL_BASE+438) diff --git a/include/lapi/syscalls/hppa.in b/include/lapi/syscalls/hppa.in index 7f9b5e0a0..e1628c4b1 100644 --- a/include/lapi/syscalls/hppa.in +++ b/include/lapi/syscalls/hppa.in @@ -39,3 +39,4 @@ fsconfig 431 fsmount 432 fspick 433 pidfd_open 434 +close_range 436 \ No newline at end of file diff --git a/include/lapi/syscalls/i386.in b/include/lapi/syscalls/i386.in index d6773abcb..aaa02c7bf 100644 --- a/include/lapi/syscalls/i386.in +++ b/include/lapi/syscalls/i386.in @@ -424,5 +424,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/ia64.in b/include/lapi/syscalls/ia64.in index 427b711b1..5467f80f2 100644 --- a/include/lapi/syscalls/ia64.in +++ b/include/lapi/syscalls/ia64.in @@ -337,5 +337,6 @@ fsconfig 1455 fsmount 1456 fspick 1457 pidfd_open 1458 +close_range 1460 openat2 1461 pidfd_getfd 1462 diff --git a/include/lapi/syscalls/mips_n32.in b/include/lapi/syscalls/mips_n32.in index eb6140ebc..a509ca5c6 100644 --- a/include/lapi/syscalls/mips_n32.in +++ b/include/lapi/syscalls/mips_n32.in @@ -362,5 +362,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/mips_n64.in b/include/lapi/syscalls/mips_n64.in index 5480aa3c9..532c69dd8 100644 --- a/include/lapi/syscalls/mips_n64.in +++ b/include/lapi/syscalls/mips_n64.in @@ -338,5 +338,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/mips_o32.in b/include/lapi/syscalls/mips_o32.in index feee8fbf3..c71a62a35 100644 --- a/include/lapi/syscalls/mips_o32.in +++ b/include/lapi/syscalls/mips_o32.in @@ -408,5 +408,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/powerpc.in b/include/lapi/syscalls/powerpc.in index cdbebc62d..2d287a606 100644 --- a/include/lapi/syscalls/powerpc.in +++ b/include/lapi/syscalls/powerpc.in @@ -417,5 +417,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/powerpc64.in b/include/lapi/syscalls/powerpc64.in index cdbebc62d..2d287a606 100644 --- a/include/lapi/syscalls/powerpc64.in +++ b/include/lapi/syscalls/powerpc64.in @@ -417,5 +417,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/s390.in b/include/lapi/syscalls/s390.in index c3f249aed..c978b6660 100644 --- a/include/lapi/syscalls/s390.in +++ b/include/lapi/syscalls/s390.in @@ -404,5 +404,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/s390x.in b/include/lapi/syscalls/s390x.in index 88cc9b86b..d123db6cb 100644 --- a/include/lapi/syscalls/s390x.in +++ b/include/lapi/syscalls/s390x.in @@ -352,5 +352,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/sh.in b/include/lapi/syscalls/sh.in index 06055ed10..22da7d6ff 100644 --- a/include/lapi/syscalls/sh.in +++ b/include/lapi/syscalls/sh.in @@ -398,5 +398,6 @@ fsconfig 431 fsmount 432 fspick 433 pidfd_open 434 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/sparc.in b/include/lapi/syscalls/sparc.in index 522e3c997..7324b4ac6 100644 --- a/include/lapi/syscalls/sparc.in +++ b/include/lapi/syscalls/sparc.in @@ -403,5 +403,6 @@ fsconfig 431 fsmount 432 fspick 433 pidfd_open 434 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/sparc64.in b/include/lapi/syscalls/sparc64.in index 6f884a7bf..862d806fa 100644 --- a/include/lapi/syscalls/sparc64.in +++ b/include/lapi/syscalls/sparc64.in @@ -368,5 +368,6 @@ fsconfig 431 fsmount 432 fspick 433 pidfd_open 434 +close_range 436 openat2 437 pidfd_getfd 438 diff --git a/include/lapi/syscalls/x86_64.in b/include/lapi/syscalls/x86_64.in index c76328c93..1345002f5 100644 --- a/include/lapi/syscalls/x86_64.in +++ b/include/lapi/syscalls/x86_64.in @@ -345,6 +345,7 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 rt_sigaction 512 From patchwork Thu Feb 11 17:45:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Palethorpe X-Patchwork-Id: 1439593 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=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=susede1 header.b=KzxDD+qv; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Dc3vj5Spvz9sBy for ; Fri, 12 Feb 2021 04:46:08 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 098C33C690D for ; Thu, 11 Feb 2021 18:46:05 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [217.194.8.6]) by picard.linux.it (Postfix) with ESMTP id D98BA3C5DDB for ; Thu, 11 Feb 2021 18:46:02 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-6.smtp.seeweb.it (Postfix) with ESMTPS id 733651400752 for ; Thu, 11 Feb 2021 18:46:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1613065562; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WDP7U1R0iouOpm2jdCa7/4KWIoN5syrQDewpfToIJCU=; b=KzxDD+qv+LRoW5ZJrjLu/hN/sTw5bCr1mHA6pfdH6gIK+ydCfNU3cmhPFP3mgrWmVUs53u 39TuQUCFD8DwYUD6xsbGiZbdUevjEiGfs3/nO8RPO7MESqXYb/9SOdYWqqEETCskbi+FFL YKozoSt/Rl5m/4tblWoaZc5xRQA8vtQ= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 10E65B12C; Thu, 11 Feb 2021 17:46:02 +0000 (UTC) To: ltp@lists.linux.it Date: Thu, 11 Feb 2021 17:45:40 +0000 Message-Id: <20210211174543.25003-3-rpalethorpe@suse.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210211174543.25003-1-rpalethorpe@suse.com> References: <20210211174543.25003-1-rpalethorpe@suse.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-6.smtp.seeweb.it Subject: [LTP] [PATCH v2 2/5] API: Add SAFE_DUP2 X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Palethorpe via ltp From: Richard Palethorpe Reply-To: Richard Palethorpe Cc: Richard Palethorpe Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Richard Palethorpe --- include/tst_safe_macros.h | 5 +++++ lib/tst_safe_macros.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h index ee3df4142..2a2b0088a 100644 --- a/include/tst_safe_macros.h +++ b/include/tst_safe_macros.h @@ -50,6 +50,11 @@ int safe_dup(const char *file, const int lineno, int oldfd); #define SAFE_DUP(oldfd) \ safe_dup(__FILE__, __LINE__, (oldfd)) +int safe_dup2(const char *file, const int lineno, int oldfd, int newfd); + +#define SAFE_DUP2(oldfd, newfd) \ + safe_dup2(__FILE__, __LINE__, (oldfd), (newfd)) + #define SAFE_GETCWD(buf, size) \ safe_getcwd(__FILE__, __LINE__, NULL, (buf), (size)) diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c index aa03a6d5c..182b690bb 100644 --- a/lib/tst_safe_macros.c +++ b/lib/tst_safe_macros.c @@ -415,6 +415,24 @@ int safe_dup(const char *file, const int lineno, int oldfd) return rval; } +int safe_dup2(const char *file, const int lineno, int oldfd, int newfd) +{ + int rval; + + rval = dup2(oldfd, newfd); + + if (rval == -1) { + tst_brk_(file, lineno, TBROK | TERRNO, + "dup2(%i, %i) failed", oldfd, newfd); + } else if (rval != newfd) { + tst_brk_(file, lineno, TBROK | TERRNO, + "Invalid dup2(%i, %i) return value %d", + oldfd, newfd, rval); + } + + return rval; +} + sighandler_t safe_signal(const char *file, const int lineno, int signum, sighandler_t handler) { From patchwork Thu Feb 11 17:45:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Palethorpe X-Patchwork-Id: 1439594 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=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=susede1 header.b=dl7wsJtp; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Dc3vt5rPMz9rx8 for ; Fri, 12 Feb 2021 04:46:16 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id AE8243C6CEE for ; Thu, 11 Feb 2021 18:46:13 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-7.smtp.seeweb.it (in-7.smtp.seeweb.it [217.194.8.7]) by picard.linux.it (Postfix) with ESMTP id 1B4D13C5DDB for ; Thu, 11 Feb 2021 18:46:03 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-7.smtp.seeweb.it (Postfix) with ESMTPS id BDF7D2010B3 for ; Thu, 11 Feb 2021 18:46:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1613065562; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mShDfRhg1S1vWH9mNng3V0ngX52FJyVadm2Zu8mcYOQ=; b=dl7wsJtpewDJ+z+PUiTXPGsipAX2Ko1k1gGslFkDxBQ41ZKL3FDx6eYdQ54MG+BxkzLDiE GcFELVorbKbPJ4zniQ9ddxAgrmJLtt02U6doMQI+/zNaxmucfQCY275pMlALND80zpqYXE b3riPVzriN19+Qr/Zh3ZMp+v6Y0sMgA= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 65D80B12D; Thu, 11 Feb 2021 17:46:02 +0000 (UTC) To: ltp@lists.linux.it Date: Thu, 11 Feb 2021 17:45:41 +0000 Message-Id: <20210211174543.25003-4-rpalethorpe@suse.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210211174543.25003-1-rpalethorpe@suse.com> References: <20210211174543.25003-1-rpalethorpe@suse.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-7.smtp.seeweb.it Subject: [LTP] [PATCH v2 3/5] API: Fix clone.h X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Palethorpe via ltp From: Richard Palethorpe Reply-To: Richard Palethorpe Cc: Richard Palethorpe Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Functions in headers should be static inline. Also add stdint.h because it results in confusing error messages if it is missing. Signed-off-by: Richard Palethorpe --- include/lapi/clone.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/lapi/clone.h b/include/lapi/clone.h index 2b8cbdbe0..81db443c9 100644 --- a/include/lapi/clone.h +++ b/include/lapi/clone.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "config.h" #include "lapi/syscalls.h" @@ -26,7 +27,7 @@ struct clone_args { uint64_t __attribute__((aligned(8))) tls; }; -int clone3(struct clone_args *args, size_t size) +static inline int clone3(struct clone_args *args, size_t size) { return tst_syscall(__NR_clone3, args, size); } @@ -36,7 +37,7 @@ int clone3(struct clone_args *args, size_t size) #define CLONE_PIDFD 0x00001000 /* set if a pidfd should be placed in parent */ #endif -void clone3_supported_by_kernel(void) +static inline void clone3_supported_by_kernel(void) { if ((tst_kvercmp(5, 3, 0)) < 0) { /* Check if the syscall is backported on an older kernel */ From patchwork Thu Feb 11 17:45:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Palethorpe X-Patchwork-Id: 1439595 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=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=susede1 header.b=HLbhgJ5X; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Dc3w06XB9z9rx8 for ; Fri, 12 Feb 2021 04:46:24 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 1360E3C6CFE for ; Thu, 11 Feb 2021 18:46:22 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [IPv6:2001:4b78:1:20::5]) by picard.linux.it (Postfix) with ESMTP id 979EB3C5DDB for ; Thu, 11 Feb 2021 18:46:03 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-5.smtp.seeweb.it (Postfix) with ESMTPS id 13C9E600C20 for ; Thu, 11 Feb 2021 18:46:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1613065562; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0TEhYW2cAWG2iWcVoy+JyyBlFaMzwfMAAZts0WR3QXA=; b=HLbhgJ5Xs3tRcHB8f6gXKfzpkio5jTielkDy7QKuf4yBRTZwubNGwjEWBff7DlpxpF87yS F7/tgsASn/WRv/dagLeI9TOV3uiNujGj7S1aSvvaYNPNtsKjHy+T5tGBxdrxglA4smSsXj FhbgmjExG4LCbTbj09cdMIzdYHfQ/F4= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id A6724B12E; Thu, 11 Feb 2021 17:46:02 +0000 (UTC) To: ltp@lists.linux.it Date: Thu, 11 Feb 2021 17:45:42 +0000 Message-Id: <20210211174543.25003-5-rpalethorpe@suse.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210211174543.25003-1-rpalethorpe@suse.com> References: <20210211174543.25003-1-rpalethorpe@suse.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-5.smtp.seeweb.it Subject: [LTP] [PATCH v2 4/5] API: Add tst_clone X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Palethorpe via ltp From: Richard Palethorpe Reply-To: Richard Palethorpe Cc: Richard Palethorpe Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" The raw clone system call and clone3 have relatively simple interfaces if the stack pointer is set to NULL. The libc wrapper complicates things hugely. So introduce an interface similar to clone3, but that falls back to clone. Not all features of clone3 are implemented in clone, but we could either return TCONF or implement them in user land. Signed-off-by: Richard Palethorpe --- doc/test-writing-guidelines.txt | 27 +++++++++++++++---- include/tst_clone.h | 28 ++++++++++++++++++++ include/tst_test.h | 2 +- lib/tst_clone.c | 46 +++++++++++++++++++++++++++++++++ lib/tst_test.c | 25 ++++++++++++++++++ 5 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 lib/tst_clone.c diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt index f3a55cf26..dd1911ceb 100644 --- a/doc/test-writing-guidelines.txt +++ b/doc/test-writing-guidelines.txt @@ -547,9 +547,9 @@ void tst_flush(void); Flush output streams, handling errors appropriately. This function is rarely needed when you have to flush the output streams -before calling 'fork()' or 'clone()'. Note that the 'SAFE_FORK()' calls this -function automatically. See 3.4 FILE buffers and fork() for explanation why is -this needed. +before calling 'fork()' or 'clone()'. Note that the 'SAFE_FORK()' and 'SAFE_CLONE()' +calls this function automatically. See 3.4 FILE buffers and fork() for explanation +why is this needed. 2.2.3 Test temporary directory ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -732,10 +732,10 @@ This happens when 'stdout' gets redirected to a file. In this case, the 'stdout' is not line buffered, but block buffered. Hence after a fork content of the buffers will be printed by the parent and each of the children. -To avoid that you should use 'SAFE_FORK()'. +To avoid that you should use 'SAFE_FORK()', 'SAFE_CLONE()' or 'tst_clone()'. IMPORTANT: You have to set the '.forks_child' flag in the test structure - if your testcase forks. + if your testcase forks or calls 'SAFE_CLONE()'. 2.2.8 Doing the test in the child process ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -769,6 +769,23 @@ The 'tst_reap_children()' function makes the process wait for all of its children and exits with 'tst_brk(TBROK, ...)' if any of them returned a non zero exit code. +When using 'SAFE_CLONE' or 'tst_clone', this may not work depending on +the parameters passed to clone. The following call to 'SAFE_CLONE' is +identical to 'fork()', so will work as expected. + +[source,c] +-------------------------------------------------------------------------------- +const struct tst_clone_args args = { + .exit_signal = SIGCHLD, +}; + +SAFE_CLONE(&args); +-------------------------------------------------------------------------------- + +If 'exit_signal' is set to something else, then this will break +'tst_reap_children'. It's not expected that all parameters to clone will +work with the LTP library unless specific action is taken by the test code. + .Using 'tst_res()' from binaries started by 'exec()' [source,c] ------------------------------------------------------------------------------- diff --git a/include/tst_clone.h b/include/tst_clone.h index 88188525d..9ffdc68d1 100644 --- a/include/tst_clone.h +++ b/include/tst_clone.h @@ -5,6 +5,34 @@ #ifndef TST_CLONE_H__ #define TST_CLONE_H__ +#ifdef TST_TEST_H__ + +/* The parts of clone3's clone_args we support */ +struct tst_clone_args { + uint64_t flags; + uint64_t exit_signal; +}; + +/* clone3 with fallbacks to clone when possible. Be aware that it + * returns -1 if clone3 fails (except ENOSYS), but -2 if clone fails. + * + * Without CLONE_VM this acts like fork so you may want to set + * tst_test.forks_child (safe_clone requires this). + * + * You should set exit_signal to SIGCHLD for + * tst_reap_children. Otherwise you must call wait with the + * appropriate parameters. + */ +pid_t tst_clone(const struct tst_clone_args *args); + +pid_t safe_clone(const char *file, const int lineno, + const struct tst_clone_args *args); + +/* "Safe" version of tst_clone */ +#define SAFE_CLONE(args) safe_clone(__FILE__, __LINE__, args) + +#endif /* TST_TEST_H__ */ + /* Functions from lib/cloner.c */ int ltp_clone(unsigned long flags, int (*fn)(void *arg), void *arg, size_t stack_size, void *stack); diff --git a/include/tst_test.h b/include/tst_test.h index c87251870..7dab5f761 100644 --- a/include/tst_test.h +++ b/include/tst_test.h @@ -29,7 +29,6 @@ #include "tst_process_state.h" #include "tst_atomic.h" #include "tst_kvercmp.h" -#include "tst_clone.h" #include "tst_kernel.h" #include "tst_minmax.h" #include "tst_get_bad_addr.h" @@ -94,6 +93,7 @@ pid_t safe_fork(const char *filename, unsigned int lineno); #include "tst_safe_macros.h" #include "tst_safe_file_ops.h" #include "tst_safe_net.h" +#include "tst_clone.h" /* * Wait for all children and exit with TBROK if diff --git a/lib/tst_clone.c b/lib/tst_clone.c new file mode 100644 index 000000000..07e7f0767 --- /dev/null +++ b/lib/tst_clone.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* Copyright (c) 2021 SUSE LLC + * Richard Palethorpe + */ + +#define TST_NO_DEFAULT_MAIN + +#include + +#include "tst_test.h" +#include "lapi/clone.h" + +pid_t tst_clone(const struct tst_clone_args *tst_args) +{ + struct clone_args args = { + .flags = tst_args->flags, + .exit_signal = tst_args->exit_signal, + }; + int flags; + pid_t pid = -1; + + tst_flush(); + + errno = ENOSYS; + if (__NR_clone3 != __LTP__NR_INVALID_SYSCALL) + pid = syscall(__NR_clone3, &args, sizeof(args)); + + if (pid == -1 && errno != ENOSYS) + return -1; + + if (pid != -1) + return pid; + + flags = args.exit_signal | args.flags; + +#ifdef __s390x__ + pid = syscall(__NR_clone, NULL, flags); +#else + pid = syscall(__NR_clone, flags, NULL); +#endif + + if (pid == -1) + return -2; + + return pid; +} diff --git a/lib/tst_test.c b/lib/tst_test.c index 0714f0a0e..6bbee030b 100644 --- a/lib/tst_test.c +++ b/lib/tst_test.c @@ -424,6 +424,31 @@ pid_t safe_fork(const char *filename, unsigned int lineno) return pid; } +pid_t safe_clone(const char *file, const int lineno, + const struct tst_clone_args *args) +{ + pid_t pid; + + if (!tst_test->forks_child) + tst_brk(TBROK, "test.forks_child must be set!"); + + pid = tst_clone(args); + + switch (pid) { + case -1: + tst_brk_(file, lineno, TBROK | TERRNO, "clone3 failed"); + break; + case -2: + tst_brk_(file, lineno, TBROK | TERRNO, "clone failed"); + return -1; + } + + if (!pid) + atexit(tst_free_all); + + return pid; +} + static struct option { char *optstr; char *help; From patchwork Thu Feb 11 17:45:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Palethorpe X-Patchwork-Id: 1439596 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=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=susede1 header.b=fWp9sB9c; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Dc3w96Gpbz9rx8 for ; Fri, 12 Feb 2021 04:46:33 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 448793C6903 for ; Thu, 11 Feb 2021 18:46:31 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [IPv6:2001:4b78:1:20::5]) by picard.linux.it (Postfix) with ESMTP id A69F63C5E23 for ; Thu, 11 Feb 2021 18:46:03 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-5.smtp.seeweb.it (Postfix) with ESMTPS id 4BEFB600C77 for ; Thu, 11 Feb 2021 18:46:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1613065563; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xJosqfT/7+4oOfYRNqDZEtXZu9un0l11DYUBPjb+Smo=; b=fWp9sB9ciaCzz6dmaEPC79gUQFq9YUp2UawA98mef7xT4oMNOnyDnWLUhR6whL7HkY0lXX RXXNDnxvP585wtyJww9LeYWfORGKccvEUTkYk00ceze3E6ODl1/DfXGNSm8e3B1kxIKTu4 +tU6KqSXsVfyRLrNZcYhYPmZJgnw9TQ= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id E530FB130; Thu, 11 Feb 2021 17:46:02 +0000 (UTC) To: ltp@lists.linux.it Date: Thu, 11 Feb 2021 17:45:43 +0000 Message-Id: <20210211174543.25003-6-rpalethorpe@suse.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210211174543.25003-1-rpalethorpe@suse.com> References: <20210211174543.25003-1-rpalethorpe@suse.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-5.smtp.seeweb.it Subject: [LTP] [PATCH v2 5/5] close_range: Add test X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Palethorpe via ltp From: Richard Palethorpe Reply-To: Richard Palethorpe Cc: Richard Palethorpe Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Richard Palethorpe --- .../kernel/syscalls/close_range/.gitignore | 1 + .../kernel/syscalls/close_range/Makefile | 10 + .../syscalls/close_range/close_range01.c | 200 ++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 testcases/kernel/syscalls/close_range/.gitignore create mode 100644 testcases/kernel/syscalls/close_range/Makefile create mode 100644 testcases/kernel/syscalls/close_range/close_range01.c diff --git a/testcases/kernel/syscalls/close_range/.gitignore b/testcases/kernel/syscalls/close_range/.gitignore new file mode 100644 index 000000000..291a0379c --- /dev/null +++ b/testcases/kernel/syscalls/close_range/.gitignore @@ -0,0 +1 @@ +close_range01 \ No newline at end of file diff --git a/testcases/kernel/syscalls/close_range/Makefile b/testcases/kernel/syscalls/close_range/Makefile new file mode 100644 index 000000000..dc6413b10 --- /dev/null +++ b/testcases/kernel/syscalls/close_range/Makefile @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2019-2021 Linux Test Project + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk + +CFLAGS += -D_GNU_SOURCE + +include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/kernel/syscalls/close_range/close_range01.c b/testcases/kernel/syscalls/close_range/close_range01.c new file mode 100644 index 000000000..ed3f6b416 --- /dev/null +++ b/testcases/kernel/syscalls/close_range/close_range01.c @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Taken from the kernel self tests, which in turn were based on + * a Syzkaller reproducer. + * + * Self test author and close_range author: + * Christian Brauner + * + * LTP Author: Richard Palethorpe + * Copyright (c) 2021 SUSE LLC, other copyrights may apply. + * + * - First we test if close_range closes some FDs. + * - Then we test if it UNSHARES some FDs before closing them. + * - Then we test if it sets CLOEXEC (in both cloned process and parent). + * - Finally we test a combination of CLOEXEC and UNSHARE. + * + * The final test is the actual bug reproducer. Note that we call + * clone directly to share the file table. + */ + +#include + +#include "tst_test.h" +#include "tst_clone.h" + +#include "lapi/clone.h" +#include "lapi/close_range.h" + +static int fd[3]; + +static inline void do_close_range(unsigned int fd, unsigned int max_fd, + unsigned int flags) +{ + int ret = close_range(fd, max_fd, flags); + + if (!ret) + return; + + if (errno == EINVAL) { + if (flags & CLOSE_RANGE_UNSHARE) + tst_brk(TCONF | TERRNO, "No CLOSE_RANGE_UNSHARE"); + if (flags & CLOSE_RANGE_CLOEXEC) + tst_brk(TCONF | TERRNO, "No CLOSE_RANGE_CLOEXEC"); + } + + tst_brk(TBROK | TERRNO, "close_range(%d, %d, %d)", fd, max_fd, flags); +} + +static void setup(void) +{ + struct rlimit nfd; + + SAFE_GETRLIMIT(RLIMIT_NOFILE, &nfd); + + if (nfd.rlim_max < 1000) { + tst_brk(TCONF, "NOFILE limit max too low: %lu < 1000", + nfd.rlim_max); + } + + nfd.rlim_cur = nfd.rlim_max; + SAFE_SETRLIMIT(RLIMIT_NOFILE, &nfd); +} + +static void check_cloexec(int i, int expected) +{ + int present = SAFE_FCNTL(fd[i], F_GETFD) & FD_CLOEXEC; + + if (expected && !present) + tst_res(TFAIL, "fd[%d] flags do not contain FD_CLOEXEC", i); + + if (!expected && present) + tst_res(TFAIL, "fd[%d] flags contain FD_CLOEXEC", i); +} + +static void check_closed(int min) +{ + int i; + + for (i = min; i < 3; i++) { + if (fcntl(fd[i], F_GETFD) > -1) + tst_res(TFAIL, "fd[%d] is still open", i); + } +} + +static void child(unsigned int n) +{ + switch (n) { + case 0: + SAFE_DUP2(fd[1], fd[2]); + do_close_range(3, ~0U, 0); + check_closed(0); + break; + case 1: + SAFE_DUP2(fd[1], fd[2]); + do_close_range(3, ~0U, CLOSE_RANGE_UNSHARE); + check_closed(0); + break; + case 2: + do_close_range(3, ~0U, CLOSE_RANGE_CLOEXEC); + check_cloexec(0, 1); + check_cloexec(1, 1); + + SAFE_DUP2(fd[1], fd[2]); + check_cloexec(2, 0); + break; + case 3: + do_close_range(3, ~0U, + CLOSE_RANGE_CLOEXEC | CLOSE_RANGE_UNSHARE); + check_cloexec(0, 1); + check_cloexec(1, 1); + + SAFE_DUP2(fd[1], fd[2]); + check_cloexec(2, 0); + break; + } + + exit(0); +} + +static void run(unsigned int n) +{ + const struct tst_clone_args args = { + .flags = CLONE_FILES, + .exit_signal = SIGCHLD, + }; + + switch (n) { + case 0: + tst_res(TINFO, "Plain close range"); + do_close_range(3, ~0U, 0); + break; + case 1: + tst_res(TINFO, "Set UNSHARE and close range"); + do_close_range(3, ~0U, CLOSE_RANGE_UNSHARE); + break; + case 2: + tst_res(TINFO, "Set CLOEXEC on range"); + do_close_range(3, ~0U, CLOSE_RANGE_CLOEXEC); + break; + case 3: + tst_res(TINFO, "Set UNSHARE and CLOEXEC on range"); + do_close_range(3, ~0U, + CLOSE_RANGE_CLOEXEC | CLOSE_RANGE_UNSHARE); + break; + } + + fd[0] = SAFE_OPEN("mnt/tmpfile", O_RDWR | O_CREAT, 0644); + fd[1] = SAFE_DUP2(fd[0], 1000); + fd[2] = 42; + + if (!SAFE_CLONE(&args)) + child(n); + + tst_reap_children(); + + switch (n) { + case 0: + check_closed(0); + break; + case 1: + check_cloexec(0, 0); + check_cloexec(1, 0); + check_cloexec(2, 0); + break; + case 2: + check_cloexec(0, 1); + check_cloexec(1, 1); + check_cloexec(2, 0); + break; + case 3: + check_cloexec(0, 0); + check_cloexec(1, 0); + check_closed(2); + break; + } + + do_close_range(3, ~0U, 0); + check_closed(0); + + if (tst_taint_check()) + tst_res(TFAIL, "Kernel tainted"); + else + tst_res(TPASS, "No kernel taints"); +} + +static struct tst_test test = { + .tcnt = 4, + .needs_tmpdir = 1, + .forks_child = 1, + .mount_device = 1, + .mntpoint = "mnt", + .all_filesystems = 1, + .test = run, + .taint_check = TST_TAINT_W | TST_TAINT_D, + .setup = setup, + .tags = (const struct tst_tag[]) { + {"linux-git", "fec8a6a691033f2538cd46848f17f337f0739923"}, + {}, + }, +};