[PATCH 2/2] selftests/mm: test exec of a PT_GNU_STACK PF_X program under MDWE
From: Hang Zhengyang
Date: Sat Dec 05 2026 - 05:56:15 EST
Add mdwe_execstack, a helper linked with -z execstack. It exits 1 if
its stack is writable and executable, and 0 if not. Add an exec_stack
test that runs it in each mdwe variant. Where MDWE is inherited across
exec ("enabled" and "inherited"), the stack must not be writable and
executable. In the other variants it must be, as the marking asks.
The helper checks that its own PT_GNU_STACK has PF_X, and the test is
skipped if not. So a build that loses -z execstack can't pass the test.
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@xxxxxxxxx>
---
tools/testing/selftests/mm/Makefile | 3 ++
tools/testing/selftests/mm/mdwe_execstack.c | 44 +++++++++++++++++++++
tools/testing/selftests/mm/mdwe_test.c | 43 ++++++++++++++++++++
3 files changed, 90 insertions(+)
create mode 100644 tools/testing/selftests/mm/mdwe_execstack.c
diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index 675ec57d47de..5e0de1666e2f 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -98,6 +98,7 @@ TEST_GEN_FILES += split_huge_page_test
TEST_GEN_FILES += ksm_tests
TEST_GEN_FILES += ksm_functional_tests
TEST_GEN_FILES += mdwe_test
+TEST_GEN_FILES += mdwe_execstack
TEST_GEN_FILES += hugetlb_fault_after_madv
TEST_GEN_FILES += hugetlb_madv_vs_map
TEST_GEN_FILES += hugetlb_dio
@@ -267,6 +268,8 @@ $(OUTPUT)/migration: LDLIBS += -lnuma
$(OUTPUT)/rmap: LDLIBS += -lnuma
+$(OUTPUT)/mdwe_execstack: LDFLAGS += -z execstack
+
local_config.mk local_config.h_gen: check_config.sh
@{ $(patsubst @%,%,$(call msg,CHK,config,$@)) :; } >&2
$(Q)CC="$(CC)" CFLAGS="$(CFLAGS)" ./check_config.sh
diff --git a/tools/testing/selftests/mm/mdwe_execstack.c b/tools/testing/selftests/mm/mdwe_execstack.c
new file mode 100644
index 000000000000..820d8821d650
--- /dev/null
+++ b/tools/testing/selftests/mm/mdwe_execstack.c
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Helper for mdwe_test, linked with -z execstack so that its PT_GNU_STACK
+ * asks for an executable stack. Exits 1 if its stack is writable and
+ * executable, 0 if not, 2 if it can't tell, and 4 (KSFT_SKIP) if it wasn't
+ * linked with -z execstack after all.
+ */
+#include <link.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/auxv.h>
+
+static int asks_for_exec_stack(void)
+{
+ const ElfW(Phdr) *phdr = (const void *)getauxval(AT_PHDR);
+ unsigned long i, n = getauxval(AT_PHNUM);
+
+ for (i = 0; phdr && i < n; i++)
+ if (phdr[i].p_type == PT_GNU_STACK)
+ return !!(phdr[i].p_flags & PF_X);
+ return 0;
+}
+
+int main(void)
+{
+ char line[256], perms[5];
+ FILE *f;
+
+ if (!asks_for_exec_stack())
+ return 4;
+ f = fopen("/proc/self/maps", "r");
+ if (!f)
+ return 2;
+ while (fgets(line, sizeof(line), f)) {
+ if (!strstr(line, "[stack]"))
+ continue;
+ fclose(f);
+ if (sscanf(line, "%*s %4s", perms) != 1)
+ return 2;
+ return perms[1] == 'w' && perms[2] == 'x';
+ }
+ fclose(f);
+ return 2;
+}
diff --git a/tools/testing/selftests/mm/mdwe_test.c b/tools/testing/selftests/mm/mdwe_test.c
index 647779653da0..3d79d370d702 100644
--- a/tools/testing/selftests/mm/mdwe_test.c
+++ b/tools/testing/selftests/mm/mdwe_test.c
@@ -7,8 +7,10 @@
#include <linux/mman.h>
#include <linux/prctl.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/auxv.h>
#include <sys/prctl.h>
#include <sys/wait.h>
@@ -284,6 +286,47 @@ TEST_F(mdwe, mmap_FIXED)
EXPECT_EQ(p, self->p);
}
+TEST_F(mdwe, exec_stack)
+{
+ /* Without MDWE after exec, the marking gives a writable executable stack */
+ int expect_wx = !(variant->enabled && variant->inherit);
+ char helper[PATH_MAX], *slash;
+ ssize_t len;
+ pid_t pid;
+ int status;
+
+ len = readlink("/proc/self/exe", helper, sizeof(helper) - sizeof("mdwe_execstack"));
+ if (len <= 0)
+ SKIP(return, "can't read /proc/self/exe");
+ helper[len] = '\0';
+ slash = strrchr(helper, '/');
+ ASSERT_NE(slash, NULL);
+ strcpy(slash + 1, "mdwe_execstack");
+ if (access(helper, X_OK))
+ SKIP(return, "mdwe_execstack not found");
+
+ pid = fork();
+ ASSERT_GE(pid, 0);
+ if (pid == 0) {
+ execl(helper, helper, (char *)NULL);
+ perror("execl mdwe_execstack");
+ _exit(126);
+ }
+ ASSERT_EQ(waitpid(pid, &status, 0), pid);
+ ASSERT_TRUE(WIFEXITED(status)) {
+ TH_LOG("mdwe_execstack killed by signal %d", WTERMSIG(status));
+ }
+ if (WEXITSTATUS(status) == KSFT_SKIP)
+ SKIP(return, "mdwe_execstack wasn't linked with -z execstack");
+ ASSERT_LT(WEXITSTATUS(status), 2) {
+ TH_LOG("mdwe_execstack failed with %d", WEXITSTATUS(status));
+ }
+ EXPECT_EQ(WEXITSTATUS(status), expect_wx) {
+ TH_LOG("the stack is %swritable and executable after exec",
+ WEXITSTATUS(status) ? "" : "not ");
+ }
+}
+
TEST_F(mdwe, arm64_BTI)
{
int ret;
--
2.53.0