Commit 3cc0c373 authored by Jeff Xu's avatar Jeff Xu Committed by Andrew Morton
Browse files

selftests/memfd: fix test_sysctl

sysctl memfd_noexec is pid-namespaced, non-reservable, and inherent to the
child process.

Move the inherence test from init ns to child ns, so init ns can keep the
default value.

Link: https://lkml.kernel.org/r/20230414022801.2545257-1-jeffxu@google.com


Signed-off-by: default avatarJeff Xu <jeffxu@google.com>
Reported-by: default avatarkernel test robot <yujie.liu@intel.com>
  Link: https://lore.kernel.org/oe-lkp/202303312259.441e35db-yujie.liu@intel.com


Tested-by: default avatarYujie Liu <yujie.liu@intel.com>
Cc: Daniel Verkamp <dverkamp@chromium.org>
Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jann Horn <jannh@google.com>
Cc: Jorge Lucangeli Obes <jorgelo@chromium.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent c025da0f
Loading
Loading
Loading
Loading
+8 −6
Original line number Original line Diff line number Diff line
@@ -43,6 +43,9 @@
 */
 */
static size_t mfd_def_size = MFD_DEF_SIZE;
static size_t mfd_def_size = MFD_DEF_SIZE;
static const char *memfd_str = MEMFD_STR;
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)
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)
static void test_sysctl_child(void)
{
{
	int fd;
	int fd;
	int pid;


	printf("%s sysctl 0\n", memfd_str);
	printf("%s sysctl 0\n", memfd_str);
	sysctl_assert_write("0");
	sysctl_assert_write("0");
@@ -1129,6 +1133,10 @@ static void test_sysctl_child(void)
			    mfd_def_size,
			    mfd_def_size,
			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
			    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_mode(fd, 0666);
	mfd_assert_has_seals(fd, F_SEAL_EXEC);
	mfd_assert_has_seals(fd, F_SEAL_EXEC);
	mfd_fail_chmod(fd, 0777);
	mfd_fail_chmod(fd, 0777);
@@ -1206,12 +1214,6 @@ static void test_sysctl(void)
	int pid = spawn_newpid_thread(CLONE_NEWPID, newpid_thread_fn);
	int pid = spawn_newpid_thread(CLONE_NEWPID, newpid_thread_fn);


	join_newpid_thread(pid);
	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);
}
}


/*
/*