Skip to contentSkip to conversation

selftests/memfd: fix test_sysctl

PATCH Applied [email protected] 2 participants 2 messages Apr 14, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/memfd: fix test_sysctl

From: Jeff Xu <[email protected]>

sysctl memfd_noexec is pid-namespaced, non-reservable, and inherent to the child process.
Moving the inherence test from init ns to child ns, so init ns can keep the default value.

---
 tools/testing/selftests/memfd/memfd_test.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
tools/testing/selftests/memfd/memfd_test.c +8 -6
index ae71f15f790d..dba0e8ba002f 100644
@@ -43,6 +43,9 @@
  */
 static size_t mfd_def_size = MFD_DEF_SIZE;
 static const char *memfd_str = MEMFD_STR;
+static pid_t spawn_newpid_thread(unsigned int flags, int (*fn)(void *));
+static int newpid_thread_fn2(void *arg);
+static void join_newpid_thread(pid_t pid);
 
 static ssize_t fd2name(int fd, char *buf, size_t bufsize)
 {
@@ -1111,6 +1114,7 @@ static void test_noexec_seal(void)
 static void test_sysctl_child(void)
 {
 	int fd;
+	int pid;
 
 	printf("%s sysctl 0\n", memfd_str);
 	sysctl_assert_write("0");
@@ -1129,6 +1133,10 @@ static void test_sysctl_child(void)
 			    mfd_def_size,
 			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
 
+	printf("%s child ns\n", memfd_str);
+	pid = spawn_newpid_thread(CLONE_NEWPID, newpid_thread_fn2);
+	join_newpid_thread(pid);
+
 	mfd_assert_mode(fd, 0666);
 	mfd_assert_has_seals(fd, F_SEAL_EXEC);
 	mfd_fail_chmod(fd, 0777);
@@ -1206,12 +1214,6 @@ static void test_sysctl(void)
 	int pid = spawn_newpid_thread(CLONE_NEWPID, newpid_thread_fn);
 
 	join_newpid_thread(pid);
-
-	printf("%s child ns\n", memfd_str);
-	sysctl_assert_write("1");
-
-	pid = spawn_newpid_thread(CLONE_NEWPID, newpid_thread_fn2);
-	join_newpid_thread(pid);
 }
 
 /*
Signature · 1 line
2.40.0.577.gac1e443424-goog
Tested-by on v1 1/1
#1 lore raw
Quoted 10 lines · On Fri, Apr 14, 2023 at 02:28:01AM +0000, [email protected] wrote:

From: Jeff Xu <[email protected]>

sysctl memfd_noexec is pid-namespaced, non-reservable, and inherent to the child process.
Moving the inherence test from init ns to child ns, so init ns can keep the default value.

The reported issue is gone after applying this patch. Thanks.

=========================================================================================
compiler/group/kconfig/rootfs/tbox_group/testcase:
gcc-11/group-02/x86_64-rhel-8.3-kselftests/debian-12-x86_64-20220629.cgz/lkp-skl-d06/kernel-selftests

commit:
c4f75bc8bd6b3 ("mm/memfd: add write seals when apply SEAL_EXEC to executable memfd") 11f75a01448f1 ("selftests/memfd: add tests for MFD_NOEXEC_SEAL MFD_EXEC") 395874d592d77 ("selftests/memfd: fix test_sysctl")

c4f75bc8bd6b3d62 11f75a01448f1b7a739e75dbd8f 395874d592d775d999d18ef9cae ---------------- --------------------------- ---------------------------
fail:runs %reproduction fail:runs %reproduction fail:runs
| | | | |
:6 233% 14:20 0% :5 kernel-selftests.memfd.run_fuse_test.sh.fail

Quoted 53 lines
---
 tools/testing/selftests/memfd/memfd_test.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
tools/testing/selftests/memfd/memfd_test.c +8 -6
index ae71f15f790d..dba0e8ba002f 100644
@@ -43,6 +43,9 @@
  */
 static size_t mfd_def_size = MFD_DEF_SIZE;
 static const char *memfd_str = MEMFD_STR;
+static pid_t spawn_newpid_thread(unsigned int flags, int (*fn)(void *));
+static int newpid_thread_fn2(void *arg);
+static void join_newpid_thread(pid_t pid);
 
 static ssize_t fd2name(int fd, char *buf, size_t bufsize)
 {
@@ -1111,6 +1114,7 @@ static void test_noexec_seal(void)
 static void test_sysctl_child(void)
 {
 	int fd;
+	int pid;
 
 	printf("%s sysctl 0\n", memfd_str);
 	sysctl_assert_write("0");
@@ -1129,6 +1133,10 @@ static void test_sysctl_child(void)
 			    mfd_def_size,
 			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
 
+	printf("%s child ns\n", memfd_str);
+	pid = spawn_newpid_thread(CLONE_NEWPID, newpid_thread_fn2);
+	join_newpid_thread(pid);
+
 	mfd_assert_mode(fd, 0666);
 	mfd_assert_has_seals(fd, F_SEAL_EXEC);
 	mfd_fail_chmod(fd, 0777);
@@ -1206,12 +1214,6 @@ static void test_sysctl(void)
 	int pid = spawn_newpid_thread(CLONE_NEWPID, newpid_thread_fn);
 
 	join_newpid_thread(pid);
-
-	printf("%s child ns\n", memfd_str);
-	sysctl_assert_write("1");
-
-	pid = spawn_newpid_thread(CLONE_NEWPID, newpid_thread_fn2);
-	join_newpid_thread(pid);
 }
 
 /*
Signature · 1 line
2.40.0.577.gac1e443424-goog