[PATCH v3] close_range: fd and max_fd are unsigned

Alyssa Ross hi at alyssa.is
Wed May 5 17:53:07 UTC 2021


The distinction is important, because close_range(2) says that having
max_fd < fd is an error, but if you give the kernel 1U + INT_MAX as
max_fd, it'll interpret it as unsigned.

Since we're working with ranges of file descriptors here, the
particular properties of only the start and end file descriptors
aren't really relevant, so it doesn't matter that we're losing fancy
file descriptor printing in making this change.
---
v1: https://lists.strace.io/pipermail/strace-devel/2021-March/010459.html
v2: https://lists.strace.io/pipermail/strace-devel/2021-May/010535.html

Changes since v2: use e.g. -1U in place of 4294967295 in test.

 src/close_range.c                     |   4 +-
 src/linux/generic/syscallent-common.h |   2 +-
 src/pathtrace.c                       |   1 -
 tests/.gitignore                      |   3 -
 tests/Makefile.am                     |   3 -
 tests/close_range-P.c                 |   4 -
 tests/close_range-y.c                 |   5 --
 tests/close_range-yy.c                |   5 --
 tests/close_range.c                   | 111 +++++++-------------------
 tests/gen_tests.in                    |   3 -
 10 files changed, 30 insertions(+), 111 deletions(-)
 delete mode 100644 tests/close_range-P.c
 delete mode 100644 tests/close_range-y.c
 delete mode 100644 tests/close_range-yy.c

diff --git a/src/close_range.c b/src/close_range.c
index 3b11c170..a701384f 100644
--- a/src/close_range.c
+++ b/src/close_range.c
@@ -12,11 +12,11 @@
 SYS_FUNC(close_range)
 {
 	/* fd */
-	printfd(tcp, tcp->u_arg[0]);
+	PRINT_VAL_U((unsigned int) tcp->u_arg[0]);
 	tprint_arg_next();
 
 	/* max_fd */
-	printfd(tcp, tcp->u_arg[1]);
+	PRINT_VAL_U((unsigned int) tcp->u_arg[1]);
 	tprint_arg_next();
 
 	/* flags */
diff --git a/src/linux/generic/syscallent-common.h b/src/linux/generic/syscallent-common.h
index 494d350f..d4c094e8 100644
--- a/src/linux/generic/syscallent-common.h
+++ b/src/linux/generic/syscallent-common.h
@@ -20,7 +20,7 @@
 [BASE_NR + 433] = { 3,	TD|TF,		SEN(fspick),			"fspick"		},
 [BASE_NR + 434] = { 2,	TD,		SEN(pidfd_open),		"pidfd_open"		},
 [BASE_NR + 435] = { 2,	TP,		SEN(clone3),			"clone3"		},
-[BASE_NR + 436] = { 3,	TD,		SEN(close_range),		"close_range"		},
+[BASE_NR + 436] = { 3,	0,		SEN(close_range),		"close_range"		},
 [BASE_NR + 437] = { 4,	TD|TF,		SEN(openat2),			"openat2"		},
 [BASE_NR + 438] = { 3,	TD,		SEN(pidfd_getfd),		"pidfd_getfd"		},
 [BASE_NR + 439] = { 4,	TD|TF,		SEN(faccessat2),		"faccessat2"		},
diff --git a/src/pathtrace.c b/src/pathtrace.c
index 2d8fba54..89d725bd 100644
--- a/src/pathtrace.c
+++ b/src/pathtrace.c
@@ -198,7 +198,6 @@ pathtrace_match_set(struct tcb *tcp, struct path_set *set)
 	 */
 
 	switch (s->sen) {
-	case SEN_close_range:
 	case SEN_dup2:
 	case SEN_dup3:
 	case SEN_kexec_file_load:
diff --git a/tests/.gitignore b/tests/.gitignore
index e66e1c70..8f2472b6 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -69,9 +69,6 @@ clone_ptrace--quiet-exit
 clone_ptrace-q
 clone_ptrace-qq
 close_range
-close_range-P
-close_range-y
-close_range-yy
 copy_file_range
 count-f
 creat
diff --git a/tests/Makefile.am b/tests/Makefile.am
index cb6e7ef1..126e8436 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -123,9 +123,6 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
 	clone_ptrace-q \
 	clone_ptrace-qq \
 	close_range \
-	close_range-P \
-	close_range-y \
-	close_range-yy \
 	count-f \
 	delay \
 	execve-v \
diff --git a/tests/close_range-P.c b/tests/close_range-P.c
deleted file mode 100644
index 2c757b07..00000000
--- a/tests/close_range-P.c
+++ /dev/null
@@ -1,4 +0,0 @@
-#define PATH_TRACING
-#define SKIP_IF_PROC_IS_UNAVAILABLE skip_if_unavailable("/proc/self/fd/")
-
-#include "close_range.c"
diff --git a/tests/close_range-y.c b/tests/close_range-y.c
deleted file mode 100644
index 61b481b4..00000000
--- a/tests/close_range-y.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#define FD0_PATH "</dev/null>"
-#define FD7_PATH "</dev/full>"
-#define SKIP_IF_PROC_IS_UNAVAILABLE skip_if_unavailable("/proc/self/fd/")
-
-#include "close_range.c"
diff --git a/tests/close_range-yy.c b/tests/close_range-yy.c
deleted file mode 100644
index 64997fc8..00000000
--- a/tests/close_range-yy.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#define FD0_PATH "</dev/null<char 1:3>>"
-#define FD7_PATH "</dev/full<char 1:7>>"
-#define SKIP_IF_PROC_IS_UNAVAILABLE skip_if_unavailable("/proc/self/fd/")
-
-#include "close_range.c"
diff --git a/tests/close_range.c b/tests/close_range.c
index 5f0e0d41..3a5d289f 100644
--- a/tests/close_range.c
+++ b/tests/close_range.c
@@ -15,16 +15,6 @@
 #include <unistd.h>
 #include <linux/close_range.h>
 
-#ifndef FD0_PATH
-# define FD0_PATH ""
-#endif
-#ifndef FD7_PATH
-# define FD7_PATH ""
-#endif
-#ifndef SKIP_IF_PROC_IS_UNAVAILABLE
-# define SKIP_IF_PROC_IS_UNAVAILABLE
-#endif
-
 static const char *errstr;
 
 static long
@@ -40,96 +30,49 @@ k_close_range(const unsigned int fd1, const unsigned int fd2, const unsigned int
 	return rc;
 }
 
-static void
-xdup2(int fd1, int fd2)
-{
-	if (dup2(fd1, fd2) != fd2)
-		perror_msg_and_fail("dup2(%d, %d)", fd1, fd2);
-}
-
 int
 main(void)
 {
-	SKIP_IF_PROC_IS_UNAVAILABLE;
+	k_close_range(-2U, -1U, 1);
+	printf("close_range(%u, %u, 0x1 /* CLOSE_RANGE_??? */) = %s\n",
+	       -2U, -1U, errstr);
 
-	const int fd0 = dup(0);
-	const int fd7 = dup(7);
-	const int fd7_min = MIN(7, fd7);
-	const int fd7_max = MAX(7, fd7);
+	k_close_range(-1U, -2U, 2);
+	printf("close_range(%u, %u, CLOSE_RANGE_UNSHARE) = %s\n",
+	       -1U, -2U, errstr);
 
-	k_close_range(-2, -1, 1);
-#ifndef PATH_TRACING
-	printf("close_range(-2, -1, 0x1 /* CLOSE_RANGE_??? */) = %s\n", errstr);
-#endif
+	k_close_range(-3U, 0, 4);
+	printf("close_range(%u, 0, CLOSE_RANGE_CLOEXEC) = %s\n", -3U, errstr);
 
-	k_close_range(-1, -2, 2);
-#ifndef PATH_TRACING
-	printf("close_range(-1, -2, CLOSE_RANGE_UNSHARE) = %s\n", errstr);
-#endif
-
-	k_close_range(-3, 0, 4);
-#ifndef PATH_TRACING
-	printf("close_range(-3, 0" FD0_PATH ", CLOSE_RANGE_CLOEXEC)"
-	       " = %s\n", errstr);
-#endif
-
-	k_close_range(0, -4, -1);
-#ifndef PATH_TRACING
-	printf("close_range(0" FD0_PATH ", -4"
+	k_close_range(0, -4U, -1);
+	printf("close_range(0, %u"
 	       ", CLOSE_RANGE_UNSHARE|CLOSE_RANGE_CLOEXEC|%#x) = %s\n",
-	       (-1U & ~(CLOSE_RANGE_UNSHARE | CLOSE_RANGE_CLOEXEC)), errstr);
-#endif
+	       -4U, (-1U & ~(CLOSE_RANGE_UNSHARE | CLOSE_RANGE_CLOEXEC)),
+	       errstr);
 
-	k_close_range(-5, 7, 0);
-	printf("close_range(-5, 7" FD7_PATH ", 0) = %s\n", errstr);
+	k_close_range(-5U, 7, 0);
+	printf("close_range(%u, 7, 0) = %s\n", -5U, errstr);
 
-	k_close_range(7, -6, 1);
-	printf("close_range(7" FD7_PATH ", -6, 0x1 /* CLOSE_RANGE_??? */)"
-	       " = %s\n", errstr);
+	k_close_range(7, -6U, 1);
+	printf("close_range(7, %u, 0x1 /* CLOSE_RANGE_??? */) = %s\n",
+	       -6U, errstr);
 
 	k_close_range(7, 7, 8);
-	printf("close_range(7" FD7_PATH ", 7" FD7_PATH
-	       ", 0x8 /* CLOSE_RANGE_??? */) = %s\n", errstr);
+	printf("close_range(7, 7, 0x8 /* CLOSE_RANGE_??? */) = %s\n", errstr);
 
-	k_close_range(-7, -7, 7);
-#ifndef PATH_TRACING
-	printf("close_range(-7, -7"
-	       ", CLOSE_RANGE_UNSHARE|CLOSE_RANGE_CLOEXEC|0x1) = %s\n", errstr);
-#endif
+	k_close_range(-7U, -7U, 7);
+	printf("close_range(%u, %u"
+	       ", CLOSE_RANGE_UNSHARE|CLOSE_RANGE_CLOEXEC|0x1) = %s\n",
+	       -7U, -7U, errstr);
 
 	k_close_range(7, 0, 0);
-	printf("close_range(7" FD7_PATH ", 0" FD0_PATH ", 0) = %s\n", errstr);
+	printf("close_range(7, 0, 0) = %s\n", errstr);
 
-	k_close_range(fd7, fd0, 0);
-	printf("close_range(%d" FD7_PATH ", %d" FD0_PATH ", 0) = %s\n",
-	       fd7, fd0, errstr);
+	k_close_range(0, 0, 0);
+	printf("close_range(0, 0, 0) = %s\n", errstr);
 
-	if (k_close_range(0, 0, 0) == 0)
-		xdup2(fd0, 0);
-#ifndef PATH_TRACING
-	printf("close_range(0" FD0_PATH ", 0" FD0_PATH ", 0) = %s\n", errstr);
-#endif
-
-	if (k_close_range(fd0, fd0, 0) == 0)
-		xdup2(0, fd0);
-#ifndef PATH_TRACING
-	printf("close_range(%d" FD0_PATH ", %d" FD0_PATH ", 0) = %s\n",
-	       fd0, fd0, errstr);
-#endif
-
-	if (k_close_range(7, 7, 0) == 0)
-		xdup2(fd7, 7);
-	printf("close_range(7" FD7_PATH ", 7" FD7_PATH ", 0) = %s\n", errstr);
-
-	if (k_close_range(fd7, fd7, 0) == 0)
-		xdup2(7, fd7);
-	printf("close_range(%d" FD7_PATH ", %d" FD7_PATH ", 0) = %s\n",
-	       fd7, fd7, errstr);
-
-	if (k_close_range(fd7_max, -1, 0) == 0)
-		xdup2(fd7_min, fd7_max);
-	printf("close_range(%d" FD7_PATH ", -1, 0) = %s\n",
-	       fd7_max, errstr);
+	k_close_range(7, -1U, 0);
+	printf("close_range(7, %u, 0) = %s\n", -1U, errstr);
 
 	puts("+++ exited with 0 +++");
 	return 0;
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index f6fce547..97b47b98 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -54,9 +54,6 @@ clone_ptrace--quiet-exit +clone_ptrace.test --quiet=exit,personality
 clone_ptrace-q +clone_ptrace.test -q
 clone_ptrace-qq +clone_ptrace.test -qq
 close_range	-a21 7>>/dev/full
-close_range-P	-a21 --trace=close_range -P /dev/full 7>>/dev/full
-close_range-y	-a33 --trace=close_range -y 7>>/dev/full
-close_range-yy	-a33 --trace=close_range -yy 7>>/dev/full
 copy_file_range
 creat	-a20
 delete_module	-a23
-- 
2.31.0



More information about the Strace-devel mailing list