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; 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; 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