Linux userland API discussions
 help / color / mirror / Atom feed
* [PATCH 0/2] exec: don't honor PT_GNU_STACK PF_X under MDWE
@ 2026-12-05 10:55 Hang Zhengyang
  2026-12-05 10:55 ` [PATCH 1/2] " Hang Zhengyang
  2026-12-05 10:55 ` [PATCH 2/2] selftests/mm: test exec of a PT_GNU_STACK PF_X program " Hang Zhengyang
  0 siblings, 2 replies; 4+ messages in thread
From: Hang Zhengyang @ 2026-12-05 10:55 UTC (permalink / raw)
  To: Andrew Morton, Kees Cook
  Cc: Lorenzo Stoakes, Chris Mason, Catalin Marinas, Joey Gouly,
	Christian Brauner, Alexander Viro, Jan Kara, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Suren Baghdasaryan, Shuah Khan, Gregory Price, Jann Horn,
	Pedro Falcato, Florent Revest, linux-mm, linux-fsdevel,
	linux-kselftest, linux-hardening, linux-api, linux-kernel

Under MDWE, a program whose PT_GNU_STACK has PF_X still gets a writable
and executable stack at execve(). Patch 1 drops VM_EXEC there, and
patch 2 adds a test.

Failing the exec is the other choice, and the one Kees Cook suggested
for such programs on the RFC that led to MDWE [1]. I chose to drop
VM_EXEC because the marking can be an accident, and today such a
program runs under MDWE unless it creates threads with glibc. The cost
is that a program that runs code from its stack gets SIGSEGV instead of
failing to start. I can switch to failing the exec.

Patch 1 leaves the arch default alone, so a program without
PT_GNU_STACK can still get an executable stack where that is the
default. "mm/vma: make brk mappings non-executable under MDWE" [2] in
mm-unstable drops VM_EXEC for brk() in that case too. I can do the same
for the stack.

Tested on mm-unstable in an x86_64 KVM guest: under MDWE, the stack of
a -z execstack program goes from rwxp to rw-p, and the new test cases
under MDWE fail without patch 1 and pass with it.

[1] https://lore.kernel.org/all/202204200952.F2B1F58D6B@keescook/
[2] https://patch.msgid.link/20261121-some-more-ai-fixes-v1-7-236e925d9b64@kernel.org

I used Claude Code to find the issue, write the patches and run the
tests. I reviewed the patches.

Hang Zhengyang (2):
  exec: don't honor PT_GNU_STACK PF_X under MDWE
  selftests/mm: test exec of a PT_GNU_STACK PF_X program under MDWE

 fs/exec.c                                   | 17 ++++++--
 tools/testing/selftests/mm/Makefile         |  3 ++
 tools/testing/selftests/mm/mdwe_execstack.c | 44 +++++++++++++++++++++
 tools/testing/selftests/mm/mdwe_test.c      | 43 ++++++++++++++++++++
 4 files changed, 103 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/mm/mdwe_execstack.c


base-commit: 2d2d9f22c9c7ae457ba93480ba82ebf57cbf0df8
-- 
2.53.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] exec: don't honor PT_GNU_STACK PF_X under MDWE
  2026-12-05 10:55 [PATCH 0/2] exec: don't honor PT_GNU_STACK PF_X under MDWE Hang Zhengyang
@ 2026-12-05 10:55 ` Hang Zhengyang
  2026-12-05 10:55 ` [PATCH 2/2] selftests/mm: test exec of a PT_GNU_STACK PF_X program " Hang Zhengyang
  1 sibling, 0 replies; 4+ messages in thread
From: Hang Zhengyang @ 2026-12-05 10:55 UTC (permalink / raw)
  To: Andrew Morton, Kees Cook
  Cc: Lorenzo Stoakes, Chris Mason, Catalin Marinas, Joey Gouly,
	Christian Brauner, Alexander Viro, Jan Kara, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Suren Baghdasaryan, Shuah Khan, Gregory Price, Jann Horn,
	Pedro Falcato, Florent Revest, linux-mm, linux-fsdevel,
	linux-kselftest, linux-hardening, linux-api, linux-kernel

With MDWE (PR_MDWE_REFUSE_EXEC_GAIN), mmap() and mprotect() refuse to
make memory writable and executable, and MDWE is inherited across
execve() by default. But a program whose PT_GNU_STACK has PF_X still
gets a writable and executable stack: setup_arg_pages() adds VM_EXEC
with mprotect_fixup(), which doesn't check map_deny_write_exec().

Under MDWE, clear VM_EXEC for EXSTACK_ENABLE_X as for EXSTACK_DISABLE_X,
and warn once. Don't fail the exec: the marking can be an accident, as
in the first example of commit 47a2ebb7f505 ("execve: warn if process
starts with executable stack"). A marked program that runs code from
its stack gets SIGSEGV instead.

Fixes: b507808ebce2 ("mm: implement memory-deny-write-execute as a prctl")
Cc: <stable+noautosel@kernel.org> # code run from the stack gets SIGSEGV under MDWE
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@gmail.com>
---
 fs/exec.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 8d2b14e1d455..d23f87301abf 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -660,12 +660,21 @@ int setup_arg_pages(struct linux_binprm *bprm,
 	/*
 	 * Adjust stack execute permissions; explicitly enable for
 	 * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
-	 * (arch default) otherwise.
+	 * (arch default) otherwise. MDWE refuses writable and executable
+	 * memory, so under MDWE treat EXSTACK_ENABLE_X as EXSTACK_DISABLE_X.
+	 * The marking can be an accident, so don't fail the exec.
 	 */
-	if (unlikely(executable_stack == EXSTACK_ENABLE_X))
-		vm_flags |= VM_EXEC;
-	else if (executable_stack == EXSTACK_DISABLE_X)
+	if (unlikely(executable_stack == EXSTACK_ENABLE_X)) {
+		if (mm_flags_test(MMF_HAS_MDWE, mm)) {
+			pr_warn_once("process '%pD4' asked for an executable stack, not granted under MDWE\n",
+				     bprm->file);
+			vm_flags &= ~VM_EXEC;
+		} else {
+			vm_flags |= VM_EXEC;
+		}
+	} else if (executable_stack == EXSTACK_DISABLE_X) {
 		vm_flags &= ~VM_EXEC;
+	}
 	vm_flags |= mm->def_flags;
 	vm_flags |= VM_STACK_INCOMPLETE_SETUP;
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] selftests/mm: test exec of a PT_GNU_STACK PF_X program under MDWE
  2026-12-05 10:55 [PATCH 0/2] exec: don't honor PT_GNU_STACK PF_X under MDWE Hang Zhengyang
  2026-12-05 10:55 ` [PATCH 1/2] " Hang Zhengyang
@ 2026-12-05 10:55 ` Hang Zhengyang
  2026-12-05 11:27   ` Alejandro Colomar
  1 sibling, 1 reply; 4+ messages in thread
From: Hang Zhengyang @ 2026-12-05 10:55 UTC (permalink / raw)
  To: Andrew Morton, Kees Cook
  Cc: Lorenzo Stoakes, Chris Mason, Catalin Marinas, Joey Gouly,
	Christian Brauner, Alexander Viro, Jan Kara, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Suren Baghdasaryan, Shuah Khan, Gregory Price, Jann Horn,
	Pedro Falcato, Florent Revest, linux-mm, linux-fsdevel,
	linux-kselftest, linux-hardening, linux-api, linux-kernel

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@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] selftests/mm: test exec of a PT_GNU_STACK PF_X program under MDWE
  2026-12-05 10:55 ` [PATCH 2/2] selftests/mm: test exec of a PT_GNU_STACK PF_X program " Hang Zhengyang
@ 2026-12-05 11:27   ` Alejandro Colomar
  0 siblings, 0 replies; 4+ messages in thread
From: Alejandro Colomar @ 2026-12-05 11:27 UTC (permalink / raw)
  To: Hang Zhengyang
  Cc: Andrew Morton, Kees Cook, Lorenzo Stoakes, Chris Mason,
	Catalin Marinas, Joey Gouly, Christian Brauner, Alexander Viro,
	Jan Kara, David Hildenbrand, Liam R. Howlett, Vlastimil Babka,
	Mike Rapoport, Michal Hocko, Suren Baghdasaryan, Shuah Khan,
	Gregory Price, Jann Horn, Pedro Falcato, Florent Revest, linux-mm,
	linux-fsdevel, linux-kselftest, linux-hardening, linux-api,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 6081 bytes --]

Hi Hang,

> Date: 2026-12-05 10:55:47+0000
> From: Hang Zhengyang <hang.zhengyang1010@gmail.com>
>
> 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@gmail.com>
> ---
>  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)) {

Lines returned by fgets(3) should almost always be verified.  If you end
up with a truncated line, you might end up in a weird situation.  This
should do it:

		if (!strchr(line, '\n'))
			return 2;

> +		if (!strstr(line, "[stack]"))
> +			continue;
> +		fclose(f);
> +		if (sscanf(line, "%*s %4s", perms) != 1)
> +			return 2;

sscanf(3) is rather complex, and easy to misuse.  How about using simple
string functions?:

		const char  *p;

		p = strpbrk(line, " \t");
		if (p == NULL)
			return 2;
		p += strspn(line, " \t");

This should also result in faster tests, since sscanf(3) is slower than
<string.h> functions.

> +		return perms[1] == 'w' && perms[2] == 'x';

And how about this to avoid manual byte operations?:

		return strstr(p, "wx") == p+1;


Have a lovely day!
Alex

> +	}
> +	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
> 
> 

-- 
<https://www.alejandro-colomar.es>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-05 11:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 10:55 [PATCH 0/2] exec: don't honor PT_GNU_STACK PF_X under MDWE Hang Zhengyang
2026-12-05 10:55 ` [PATCH 1/2] " Hang Zhengyang
2026-12-05 10:55 ` [PATCH 2/2] selftests/mm: test exec of a PT_GNU_STACK PF_X program " Hang Zhengyang
2026-12-05 11:27   ` Alejandro Colomar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox