[PATCH 1/2] exec: don't honor PT_GNU_STACK PF_X under MDWE
From: Hang Zhengyang
Date: Sat Dec 05 2026 - 05:56:11 EST
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@xxxxxxxxxx> # code run from the stack gets SIGSEGV under MDWE
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@xxxxxxxxx>
---
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