Re: [PATCH] x86/mm: Set NX bit when making pages present
From: Yujie Liu
Date: Mon Sep 19 2022 - 23:13:55 EST
On Mon, Sep 19, 2022 at 11:30:09AM -0700, Dave Hansen wrote:
> On 9/19/22 11:14, Edgecombe, Rick P wrote:
> > Clearly somehow it is though. The original report has this in the log:
> > Notice: NX (Execute Disable) protection cannot be enabled: non-PAE
> > kernel!
>
> Ah, crud. Nice catch, btw.
>
> So, the CPU has NX, making cpu_feature_enabled(X86_FEATURE_NX)==1, but
> the page table mode does not have support.
>
> I guess we can either clear X86_FEATURE_NX around the "protection cannot
> be enabled" message, or do something like the attached patch and just do
> the check at runtime.
>
> I'm not sure we want to mess with X86_FEATURE_NX itself. It seems to
> get used for a few different things, including on the KVM side.
>
> 0day folks, can you see if _this_ one (totally untested) helps the
> situation? At least this is a real oddball case. It's not something
> that folks are very likely to hit at all.
This patch fixes the issues in below two reports. Thanks.
https://lore.kernel.org/all/fcf89147-440b-e478-40c9-228c9fe56691@xxxxxxxxx/
https://lore.kernel.org/all/202209141701.e293ea84-yujie.liu@xxxxxxxxx/
Comparison of test results:
=========================================================================================
compiler/kconfig/rootfs/runtime/scale_type/tbox_group/testcase:
gcc-11/i386-randconfig-a013-20211012/debian-i386-20191205.cgz/300s/srcu/vm-snb/rcuscale
commit:
652c5bf380ad01 ("x86/mm: Refuse W^X violations")
0718e6a68e2a7c ("nx.patch")
652c5bf380ad018e 0718e6a68e2a7cb20800b1b1372
---------------- ---------------------------
fail:runs %reproduction fail:runs
| | |
6:6 -100% :6 dmesg.BUG:unable_to_handle_page_fault_for_address
6:6 -100% :6 dmesg.EIP:__change_page_attr
6:6 -100% :6 dmesg.Kernel_panic-not_syncing:Fatal_exception
6:6 -100% :6 dmesg.Oops:#[##]
6:6 -100% :6 dmesg.WARNING:at_arch/x86/mm/pat/set_memory.c:#__change_page_attr
=========================================================================================
compiler/kconfig/rootfs/runtime/tbox_group/test/testcase/torture_type:
gcc-11/i386-randconfig-a004-20211104/debian-i386-20191205.cgz/300s/vm-snb/cpuhotplug/rcutorture/tasks-tracing
commit:
652c5bf380ad01 ("x86/mm: Refuse W^X violations")
0718e6a68e2a7c ("nx.patch")
652c5bf380ad018e 0718e6a68e2a7cb20800b1b1372
---------------- ---------------------------
fail:runs %reproduction fail:runs
| | |
6:6 -100% :6 dmesg.BUG:unable_to_handle_page_fault_for_address
6:6 -100% :6 dmesg.EIP:verify_rwx
6:6 -100% :6 dmesg.Kernel_panic-not_syncing:Fatal_exception
6:6 -100% :6 dmesg.Oops:#[##]
6:6 -100% :6 dmesg.WARNING:at_arch/x86/mm/pat/set_memory.c:#verify_rwx
> diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> index 216fee7144ee..005492257abb 100644
> --- a/arch/x86/kernel/setup.c
> +++ b/arch/x86/kernel/setup.c
> @@ -844,6 +844,7 @@ static void __init x86_report_nx(void)
> /* 32bit non-PAE kernel, NX cannot be used */
> printk(KERN_NOTICE "Notice: NX (Execute Disable) protection "
> "cannot be enabled: non-PAE kernel!\n");
> + __supported_pte_mask &= ~_PAGE_NX;
> #endif
> }
> }
> diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
> index 1a2d6376251c..f8162fe94bd0 100644
> --- a/arch/x86/mm/pat/set_memory.c
> +++ b/arch/x86/mm/pat/set_memory.c
> @@ -587,7 +587,7 @@ static inline pgprot_t verify_rwx(pgprot_t old, pgprot_t new, unsigned long star
> {
> unsigned long end;
>
> - if (!cpu_feature_enabled(X86_FEATURE_NX))
> + if (!(__supported_pte_mask & _PAGE_NX))
> return new;
>
> if (!((pgprot_val(old) ^ pgprot_val(new)) & (_PAGE_RW | _PAGE_NX)))