]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests:core: test coverage for dup_fd() failure handling in unshare_fd()
authorAl Viro <viro@zeniv.linux.org.uk>
Thu, 22 Aug 2024 04:39:32 +0000 (05:39 +0100)
committerShuah Khan <skhan@linuxfoundation.org>
Thu, 22 Aug 2024 06:45:55 +0000 (00:45 -0600)
At some point there'd been a dumb braino during the dup_fd()
calling conventions change; caught by smatch and immediately fixed.
The trouble is, there had been no test coverage for the dup_fd() failure
handling - neither in kselftests nor in LTP.  Fortunately, it can be
triggered on stock kernel - ENOMEM would require fault injection, but
EMFILE can be had with sysctl alone (fs.nr_open).

Add a test for dup_fd() failure.
Fixed up commit log and short log - Shuah Khan

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/selftests/core/Makefile
tools/testing/selftests/core/unshare_test.c [new file with mode: 0644]

index ce262d09726990e85a943c26d7e5eebbe5d7cfc5..8e99f87f5d7c212bbd18fe604e26dd2bcbaeaace 100644 (file)
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 CFLAGS += -g $(KHDR_INCLUDES)
 
-TEST_GEN_PROGS := close_range_test
+TEST_GEN_PROGS := close_range_test unshare_test
 
 include ../lib.mk
 
diff --git a/tools/testing/selftests/core/unshare_test.c b/tools/testing/selftests/core/unshare_test.c
new file mode 100644 (file)
index 0000000..7fec9df
--- /dev/null
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/kernel.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syscall.h>
+#include <unistd.h>
+#include <sys/resource.h>
+#include <linux/close_range.h>
+
+#include "../kselftest_harness.h"
+#include "../clone3/clone3_selftests.h"
+
+TEST(unshare_EMFILE)
+{
+       pid_t pid;
+       int status;
+       struct __clone_args args = {
+               .flags = CLONE_FILES,
+               .exit_signal = SIGCHLD,
+       };
+       int fd;
+       ssize_t n, n2;
+       static char buf[512], buf2[512];
+       struct rlimit rlimit;
+       int nr_open;
+
+       fd = open("/proc/sys/fs/nr_open", O_RDWR);
+       ASSERT_GE(fd, 0);
+
+       n = read(fd, buf, sizeof(buf));
+       ASSERT_GT(n, 0);
+       ASSERT_EQ(buf[n - 1], '\n');
+
+       ASSERT_EQ(sscanf(buf, "%d", &nr_open), 1);
+
+       ASSERT_EQ(0, getrlimit(RLIMIT_NOFILE, &rlimit));
+
+       /* bump fs.nr_open */
+       n2 = sprintf(buf2, "%d\n", nr_open + 1024);
+       lseek(fd, 0, SEEK_SET);
+       write(fd, buf2, n2);
+
+       /* bump ulimit -n */
+       rlimit.rlim_cur = nr_open + 1024;
+       rlimit.rlim_max = nr_open + 1024;
+       EXPECT_EQ(0, setrlimit(RLIMIT_NOFILE, &rlimit)) {
+               lseek(fd, 0, SEEK_SET);
+               write(fd, buf, n);
+               exit(EXIT_FAILURE);
+       }
+
+       /* get a descriptor past the old fs.nr_open */
+       EXPECT_GE(dup2(2, nr_open + 64), 0) {
+               lseek(fd, 0, SEEK_SET);
+               write(fd, buf, n);
+               exit(EXIT_FAILURE);
+       }
+
+       /* get descriptor table shared */
+       pid = sys_clone3(&args, sizeof(args));
+       EXPECT_GE(pid, 0) {
+               lseek(fd, 0, SEEK_SET);
+               write(fd, buf, n);
+               exit(EXIT_FAILURE);
+       }
+
+       if (pid == 0) {
+               int err;
+
+               /* restore fs.nr_open */
+               lseek(fd, 0, SEEK_SET);
+               write(fd, buf, n);
+               /* ... and now unshare(CLONE_FILES) must fail with EMFILE */
+               err = unshare(CLONE_FILES);
+               EXPECT_EQ(err, -1)
+                       exit(EXIT_FAILURE);
+               EXPECT_EQ(errno, EMFILE)
+                       exit(EXIT_FAILURE);
+               exit(EXIT_SUCCESS);
+       }
+
+       EXPECT_EQ(waitpid(pid, &status, 0), pid);
+       EXPECT_EQ(true, WIFEXITED(status));
+       EXPECT_EQ(0, WEXITSTATUS(status));
+}
+
+TEST_HARNESS_MAIN