Skip to contentSkip to conversation

x86_64: NX bit handling in change_page_attr

BUGFIX Huang, Ying 3 participants 5 messages Aug 17, 2007 – Sep 13, 2007

lore
Share & tools
lore
#0 lore raw

This patch fixes a bug of change_page_attr/change_page_attr_addr on Intel x86_64 CPU. After changing page attribute to be executable with these functions, the page remains un-executable on Intel x86_64 CPU. Because on Intel x86_64 CPU, only if the "NX" bits of all four level page tables are cleared, the corresponding page is executable (refer to section 4.13.2 of Intel 64 and IA-32 Architectures Software Developer's Manual). So, the bug is fixed through clearing the "NX" bit of PMD when splitting the huge PMD.

---

arch/x86_64/mm/pageattr.c +1 -0
Index: linux-2.6.23-rc2-mm2/arch/x86_64/mm/pageattr.c
===================================================================
@@ -147,6 +147,7 @@
 			split = split_large_page(address, prot, ref_prot2);
 			if (!split)
 				return -ENOMEM;
+			pgprot_val(ref_prot2) &= ~_PAGE_NX;
 			set_pte(kpte, mk_pte(split, ref_prot2));
 			kpte_page = split;
 		}
#1 lore raw
Quoted 18 lines · Ying

This patch fixes a bug of change_page_attr/change_page_attr_addr on Intel x86_64 CPU. After changing page attribute to be executable with these functions, the page remains un-executable on Intel x86_64 CPU. Because on Intel x86_64 CPU, only if the "NX" bits of all four level page tables are cleared, the corresponding page is executable (refer to section 4.13.2 of Intel 64 and IA-32 Architectures Software Developer's Manual). So, the bug is fixed through clearing the "NX" bit of PMD when splitting the huge PMD.

---

Index: linux-2.6.23-rc2-mm2/arch/x86_64/mm/pageattr.c
===================================================================

arch/x86_64/mm/pageattr.c line 153

Quoted 7 lines
arch/x86_64/mm/pageattr.c +1 -0
@@ -147,6 +147,7 @@
 			split = split_large_page(address, prot, ref_prot2);
 			if (!split)
 				return -ENOMEM;
+			pgprot_val(ref_prot2) &= ~_PAGE_NX;
 			set_pte(kpte, mk_pte(split, ref_prot2));
 			kpte_page = split;
 		}

What happened with this? Still valid?

#2 lore raw
Quoted 15 lines · Andrew Morton
Quoted 13 lines · Ying

This patch fixes a bug of change_page_attr/change_page_attr_addr on Intel x86_64 CPU. After changing page attribute to be executable with these functions, the page remains un-executable on Intel x86_64 CPU. Because on Intel x86_64 CPU, only if the "NX" bits of all four level page tables are cleared, the corresponding page is executable (refer to section 4.13.2 of Intel 64 and IA-32 Architectures Software Developer's Manual). So, the bug is fixed through clearing the "NX" bit of PMD when splitting the huge PMD.

---

Quoted 14 lines

arch/x86_64/mm/pageattr.c line 153

Quoted 12 lines

Index: linux-2.6.23-rc2-mm2/arch/x86_64/mm/pageattr.c
===================================================================

arch/x86_64/mm/pageattr.c +1 -0
@@ -147,6 +147,7 @@
 			split = split_large_page(address, prot, ref_prot2);
 			if (!split)
 				return -ENOMEM;
+			pgprot_val(ref_prot2) &= ~_PAGE_NX;
 			set_pte(kpte, mk_pte(split, ref_prot2));
 			kpte_page = split;
 		}

What happened with this? Still valid?

I am waiting for reviewing or merging. And I think it is still valid.

Best Regards,
Huang Ying

#3 lore raw
In reply to Andrew Morton
Quoted 16 lines
Quoted 7 lines

Index: linux-2.6.23-rc2-mm2/arch/x86_64/mm/pageattr.c
===================================================================
--- linux-2.6.23-rc2-mm2.orig/arch/x86_64/mm/pageattr.c 2007-08-17
12:50:25.000000000 +0800 +++
linux-2.6.23-rc2-mm2/arch/x86_64/mm/pageattr.c 2007-08-17
12:50:48.000000000 +0800 @@ -147,6 +147,7 @@

arch/x86_64/mm/pageattr.c line 153

Quoted 7 lines
arch/x86_64/mm/pageattr.c +1 -0
 			split = split_large_page(address, prot, ref_prot2);
 			if (!split)
 				return -ENOMEM;
+			pgprot_val(ref_prot2) &= ~_PAGE_NX;
 			set_pte(kpte, mk_pte(split, ref_prot2));
 			kpte_page = split;
 		}

What happened with this? Still valid?

The bug is probably latent there, but I don't think it can affect anything in the kernel because nothing in the kernel should change NX status as far as I know.

Where did you see it?

Anyways I would prefer to only clear the PMD NX when NX status actually changes on the PTE.Can you do that change?

Anyways; it's really not very important.

-Andi

#4 lore raw
Quoted 16 lines · Andi Kleen
Quoted 7 lines
Quoted 7 lines

Index: linux-2.6.23-rc2-mm2/arch/x86_64/mm/pageattr.c
===================================================================
--- linux-2.6.23-rc2-mm2.orig/arch/x86_64/mm/pageattr.c 2007-08-17
12:50:25.000000000 +0800 +++
linux-2.6.23-rc2-mm2/arch/x86_64/mm/pageattr.c 2007-08-17
12:50:48.000000000 +0800 @@ -147,6 +147,7 @@
split = split_large_page(address, prot, ref_prot2);

Quoted 8 lines

arch/x86_64/mm/pageattr.c line 153

Quoted 6 lines
arch/x86_64/mm/pageattr.c +1 -0
 			if (!split)
 				return -ENOMEM;
+			pgprot_val(ref_prot2) &= ~_PAGE_NX;
 			set_pte(kpte, mk_pte(split, ref_prot2));
 			kpte_page = split;
 		}

What happened with this? Still valid?

Quoted 5 lines

The bug is probably latent there, but I don't think it can affect anything in the kernel because nothing in the kernel should change NX status as far as I know.

Where did you see it?

I found the problem for EFI runtime service supporting. Where the EFI runtime code (from firmware) need to be mapped without NX bit set.

Quoted 2 lines

Anyways I would prefer to only clear the PMD NX when NX status actually changes on the PTE.Can you do that change?

This change is sufficient for Intel CPU. Because the NX bit of PTE is still there, no page will be made executable if not been set explicitly through PTE. For AMD CPU, will the page be made executable if the NX bit of PMD is cleared and the NX bit of PTE is set? If so, I will do the change as you said.

Quoted 1 line

Anyways; it's really not very important.

It is needed by EFI runtime service supporting.

Best Regards,
Huang Ying