Skip to content

Commit 6a75f19

Browse files
isaacmanjarresakpm00
authored andcommitted
selftests/memfd: run sysctl tests when PID namespace support is enabled
The sysctl tests for vm.memfd_noexec rely on the kernel to support PID namespaces (i.e. the kernel is built with CONFIG_PID_NS=y). If the kernel the test runs on does not support PID namespaces, the first sysctl test will fail when attempting to spawn a new thread in a new PID namespace, abort the test, preventing the remaining tests from being run. This is not desirable, as not all kernels need PID namespaces, but can still use the other features provided by memfd. Therefore, only run the sysctl tests if the kernel supports PID namespaces. Otherwise, skip those tests and emit an informative message to let the user know why the sysctl tests are not being run. Link: https://lkml.kernel.org/r/20241205192943.3228757-1-isaacmanjarres@google.com Fixes: 11f75a0 ("selftests/memfd: add tests for MFD_NOEXEC_SEAL MFD_EXEC") Signed-off-by: Isaac J. Manjarres <isaacmanjarres@google.com> Reviewed-by: Jeff Xu <jeffxu@google.com> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Kalesh Singh <kaleshsingh@google.com> Cc: <stable@vger.kernel.org> [6.6+] Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent dbf8be8 commit 6a75f19

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

tools/testing/selftests/memfd/memfd_test.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <fcntl.h>
1010
#include <linux/memfd.h>
1111
#include <sched.h>
12+
#include <stdbool.h>
1213
#include <stdio.h>
1314
#include <stdlib.h>
1415
#include <signal.h>
@@ -1557,6 +1558,11 @@ static void test_share_fork(char *banner, char *b_suffix)
15571558
close(fd);
15581559
}
15591560

1561+
static bool pid_ns_supported(void)
1562+
{
1563+
return access("/proc/self/ns/pid", F_OK) == 0;
1564+
}
1565+
15601566
int main(int argc, char **argv)
15611567
{
15621568
pid_t pid;
@@ -1591,8 +1597,12 @@ int main(int argc, char **argv)
15911597
test_seal_grow();
15921598
test_seal_resize();
15931599

1594-
test_sysctl_simple();
1595-
test_sysctl_nested();
1600+
if (pid_ns_supported()) {
1601+
test_sysctl_simple();
1602+
test_sysctl_nested();
1603+
} else {
1604+
printf("PID namespaces are not supported; skipping sysctl tests\n");
1605+
}
15961606

15971607
test_share_dup("SHARE-DUP", "");
15981608
test_share_mmap("SHARE-MMAP", "");

0 commit comments

Comments
 (0)