From c8d3fa13cc644791c829ea50dac6471fd0c5e0d7 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Fri, 13 Nov 2026 13:45:03 -0800 Subject: [PATCH 1/3] mm: never give the brk area execute permission under OpenPaX MPROTECT MPROTECT is enforced in map_deny_write_exec(), which mmap_region() and mprotect() call, but the brk path never does. do_brk_flags() ORs in VMA_DATA_DEFAULT_FLAGS, which carries VM_EXEC whenever the task's personality has READ_IMPLIES_EXEC, and personality(2) lets any task set that for itself: the ELF loader only clears it at exec. So under MPROTECT, where mmap(PROT_READ|PROT_WRITE) with READ_IMPLIES_EXEC is correctly refused, two syscalls still hand out a fresh writable and executable mapping: personality(READ_IMPLIES_EXEC); brk(cur + len); /* new RWX VMA; it does not merge with the RW heap */ Write code there and jump to it. The same path makes the bss tail of an executable PT_LOAD segment RWX, via vm_brk_flags(..., prot & PROT_EXEC) in the ELF loader, for the binary and its interpreter. Original PaX masked VM_EXEC and VM_MAYEXEC in do_brk() for exactly this reason. Do the same in do_brk_flags() when the mm runs under MPROTECT: strip the execute bits rather than fail, so brk(2) keeps working for such tasks and extensions still merge with the existing heap. Signed-off-by: Ariadne Conill --- mm/vma.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mm/vma.c b/mm/vma.c index 6c35f0d775ab6e..8316da4ae0af7a 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -3024,6 +3024,20 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, vma_flags_set(&vma_flags, VMA_ACCOUNT_BIT); vma_flags_set_mask(&vma_flags, mm->def_vma_flags); +#ifdef CONFIG_OPENPAX_MPROTECT + /* + * Under MPROTECT the brk area is never executable. Its only routes + * to VM_EXEC are READ_IMPLIES_EXEC, which personality(2) lets any + * task set for itself, and the ELF loader's vm_brk_flags() for the + * bss of an executable segment. Every other path refuses the + * resulting writable and executable mapping in map_deny_write_exec(); + * strip the execute bits here rather than fail, as PaX did, so brk(2) + * keeps working for such tasks. + */ + if (test_bit(PAXF_MPROTECT, &mm->pax_flags)) + vma_flags_clear(&vma_flags, VMA_EXEC_BIT, VMA_MAYEXEC_BIT); +#endif + vma_flags = ksm_vma_flags(mm, NULL, vma_flags); if (!may_expand_vm(mm, &vma_flags, len >> PAGE_SHIFT)) return -ENOMEM; From 0a97dd0897f990d35a01b2071d9c34b709a8901f Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Fri, 13 Nov 2026 13:45:03 -0800 Subject: [PATCH 2/3] x86/mm: emulate OpenPaX trampolines only when the fault would be SIGSEGV The trampoline emulator was hooked at the top of do_user_addr_fault(), before the VMA lookup, and openpax_fault_is_trampoline() treats any instruction fetch fault as a candidate. So in every PAGEEXEC+EMUTRAMP task every instruction fetch fault, including each demand-paged, NUMA hinted or userfaultfd fetch of legitimate text, first did up to seven get_user() reads of the faulting code, and if that text happened to match one of the patterns it was emulated instead of being faulted in. PaX only ran the emulator from the bad-area path, for a fault that was otherwise about to kill the task. Move the hook to __bad_area_nosemaphore(), after the user-mode and X86_PF_USER checks and with interrupts enabled, so it runs only for an access the task's mappings do not permit: a fetch from non-executable memory. A fetch from executable memory that merely needs its page brought in is handled as an ordinary fault and never reaches it. A fetch from a non-executable page that is not yet populated now takes two faults, one to bring the page in and one to be emulated, instead of being emulated straight from the first; the result is the same. Signed-off-by: Ariadne Conill --- arch/x86/mm/fault.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index ba856edaa9727c..37719cb69573a6 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -782,6 +782,13 @@ show_signal_msg(struct pt_regs *regs, unsigned long error_code, show_opcodes(regs, loglvl); } +#ifdef CONFIG_OPENPAX_EMUTRAMP +static inline bool openpax_fault_is_trampoline(unsigned long error_code, + struct pt_regs *regs, + unsigned long address); +static inline bool openpax_emulate_trampoline(struct pt_regs *regs); +#endif + static void __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, unsigned long address, u32 pkey, int si_code) @@ -806,6 +813,19 @@ __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, */ local_irq_enable(); +#ifdef CONFIG_OPENPAX_EMUTRAMP + /* + * A trampoline in non-executable memory is an instruction fetch the + * task's mappings do not permit, so it is only here, with the fault + * otherwise about to become SIGSEGV, that one can be emulated. A + * fetch from executable memory that merely needs its page brought + * in is resolved as an ordinary fault above and never reaches this. + */ + if (openpax_fault_is_trampoline(error_code, regs, address) && + openpax_emulate_trampoline(regs)) + return; +#endif + /* * Valid to do another page fault here because this one came * from user space: @@ -1554,13 +1574,6 @@ void do_user_addr_fault(struct pt_regs *regs, } #endif -#ifdef CONFIG_OPENPAX_EMUTRAMP - if (openpax_fault_is_trampoline(error_code, regs, address)) { - if (openpax_emulate_trampoline(regs)) - return; - } -#endif - if (!(flags & FAULT_FLAG_USER)) goto lock_mmap; From e3301b72d73f635b04c91a22a50c9f858a877f16 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Fri, 13 Nov 2026 13:45:04 -0800 Subject: [PATCH 3/3] openpax: do not enable trampoline emulation for every program by default With OPENPAX_EMUTRAMP_DEFAULT the ELF loader sets PAXF_EMUTRAMP on every task. The emulator then lets any task with a write primitive into some readable memory and control of the instruction pointer load chosen values into two registers and jump to a chosen address, from the stack, the heap, .rodata or a noexec-mount file mapping, without any of that memory being executable; a bad target just faults again, so such sequences chain. That weakens PAGEEXEC and MPROTECT for every program on the system to help the few that still use stack or heap trampolines. PaX recommends enabling emulation per binary for this reason. Default the option to n and say so in its help text; the programs that need emulation get it with paxmark -E, through the user.pax.flags xattr. Signed-off-by: Ariadne Conill --- security/Kconfig.openpax | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/security/Kconfig.openpax b/security/Kconfig.openpax index df65b38ed396e4..b673c88f8d3874 100644 --- a/security/Kconfig.openpax +++ b/security/Kconfig.openpax @@ -76,7 +76,7 @@ config OPENPAX_EMUTRAMP config OPENPAX_EMUTRAMP_DEFAULT bool "Enable trampoline emulation by default" depends on OPENPAX_EMUTRAMP - default y + default n help Enabling this option allows programs which require trampolines to be emulated to continue working by default. @@ -84,7 +84,12 @@ config OPENPAX_EMUTRAMP_DEFAULT Otherwise, the emulation flag must be enabled in a binary's PaX marking, e.g. with paxmark -E . - If you do not say 'y' here, you will have to manually mark - all programs which require trampoline emulation. + Trampoline emulation lets a task load fixed values into two + registers and jump to an arbitrary address from any readable + memory, without that memory being executable. Enabling it + for every program weakens PAGEEXEC and MPROTECT for all of + them, so prefer marking only the programs which need it. + + If unsure, say 'n'. endmenu