Skip to contentSkip to conversation

Enable storage keys for radix - user mode execution

PATCH In review Balbir Singh 3 participants 8 messages Aug 22, 2016 – Sep 20, 2016 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Enable storage keys for radix - user mode execution

ISA 3 defines new encoded access authority that allows instruction access prevention in privileged mode and allows normal access to problem state. This patch just enables IAMR (Instruction Authority Mask Register), enabling AMR would require more work.

I've tested this with a buggy driver and a simple payload. The payload is specific to the build I've tested.

---
 arch/powerpc/mm/pgtable-radix.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/powerpc/mm/pgtable-radix.c +22 -0
index af897d9..9e25663 100644
@@ -294,6 +294,27 @@ found:
 	return;
 }
 
+/*
+ * For radix page tables we setup, the IAMR values as follows
+ * IMAR = 0100...00 (key 0 is set to 1)
+ * AMOR = 1100....00 (Mask for key 0 is 11)
+ * AMR, UAMR, UAMOR are not affected
+ */
+static void __init radix_init_iamr(void)
+{
+	unsigned long iamr_mask = 0x4000000000000000;
+	unsigned long iamr = mfspr(SPRN_IAMR);
+
+	unsigned long amor_mask = 0xc000000000000000;
+	unsigned long amor = mfspr(SPRN_AMOR);
+
+	iamr |= iamr_mask;
+	amor |= amor_mask;
+
+	mtspr(SPRN_AMOR, amor);
+	mtspr(SPRN_IAMR, iamr);
+}
+
 void __init radix__early_init_mmu(void)
 {
 	unsigned long lpcr;
@@ -350,6 +371,7 @@ void __init radix__early_init_mmu(void)
 		radix_init_partition_table();
 	}
 
+	radix_init_iamr();
 	radix_init_pgtable();
 }
 
Signature · 1 line
2.5.5
#1 lore raw

PATCH 2/2 Detect instruction fetch denied and report

ISA 3 allows for prevention of instruction fetch and execution of user mode pages. If such an error occurs, SRR1 bit 35 reports the error. We catch and report the error in do_page_fault()

---
 arch/powerpc/mm/fault.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/mm/fault.c +4 -0
index a4db22f..f162e77 100644
@@ -404,6 +404,10 @@ good_area:
 		    (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
 		     !(vma->vm_flags & (VM_READ | VM_WRITE))))
 			goto bad_area;
+#ifdef CONFIG_PPC_RADIX_MMU
+		if (radix_enabled() && regs->msr & PPC_BIT(35))
+			goto bad_area;
+#endif
 #ifdef CONFIG_PPC_STD_MMU
 		/*
 		 * protfault should only happen due to us
Signature · 1 line
2.5.5
#2 lore raw

Re: [PATCH 2/2] Detect instruction fetch denied and report

Quoted 4 lines · Balbir Singh

ISA 3 allows for prevention of instruction fetch and execution of user mode pages. If such an error occurs, SRR1 bit 35 reports the error. We catch and report the error in do_page_fault()

But what does the error mean ? A buggy application ? IIUC, it indicate a buggy kernel isn't it ?. So should we kill the application or panic() ?

Quoted 10 lines
---
 arch/powerpc/mm/fault.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/mm/fault.c +0 -0
index a4db22f..f162e77 100644

arch/powerpc/mm/fault.c good_area: · line 413

Quoted 10 lines
arch/powerpc/mm/fault.c +4 -0
@@ -404,6 +404,10 @@ good_area:
 		    (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
 		     !(vma->vm_flags & (VM_READ | VM_WRITE))))
 			goto bad_area;
+#ifdef CONFIG_PPC_RADIX_MMU
+		if (radix_enabled() && regs->msr & PPC_BIT(35))
+			goto bad_area;
+#endif
 #ifdef CONFIG_PPC_STD_MMU
 		/*
 		 * protfault should only happen due to us

-aneesh

#3 lore raw

Re: [PATCH 2/2] Detect instruction fetch denied and report

Quoted 10 lines · Aneesh Kumar K.V
Quoted 4 lines · Balbir Singh

ISA 3 allows for prevention of instruction fetch and execution of user mode pages. If such an error occurs, SRR1 bit 35 reports the error. We catch and report the error in do_page_fault()

But what does the error mean ? A buggy application ? IIUC, it indicate a buggy kernel isn't it ?. So should we kill the application or panic() ?

We oops.. basically we get a kernel fault. We handle it the same way we handle other kernel faults.

Quoted 24 lines
Quoted 11 lines
---
 arch/powerpc/mm/fault.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/mm/fault.c +0 -0
index a4db22f..f162e77 100644

arch/powerpc/mm/fault.c good_area: · line 413

Quoted 10 lines
arch/powerpc/mm/fault.c +4 -0
@@ -404,6 +404,10 @@ good_area:
 		    (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
 		     !(vma->vm_flags & (VM_READ | VM_WRITE))))
 			goto bad_area;
+#ifdef CONFIG_PPC_RADIX_MMU
+		if (radix_enabled() && regs->msr & PPC_BIT(35))
+			goto bad_area;
+#endif
 #ifdef CONFIG_PPC_STD_MMU
 		/*
 		 * protfault should only happen due to us

-aneesh

#4 lore raw

Re: [2/2] Detect instruction fetch denied and report

In reply to Balbir Singh

arch/powerpc/mm/fault.c good_area: · line 407

Quoted 9 lines · Balbir Singh
arch/powerpc/mm/fault.c +1 -0
index a4db22f..f162e77 100644
@@ -404,6 +404,10 @@ good_area:
 		    (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
 		     !(vma->vm_flags & (VM_READ | VM_WRITE))))
 			goto bad_area;
+#ifdef CONFIG_PPC_RADIX_MMU

We shouldn't need the #ifdef, radix_enabled() will be false.

arch/powerpc/mm/fault.c good_area: · line 409

Quoted 2 lines
arch/powerpc/mm/fault.c +2 -0
+		if (radix_enabled() && regs->msr & PPC_BIT(35))
+			goto bad_area;

Is it really architected as radix only?

Personally I dislike PPC_BIT(), I'd rather you just used 0x10000000. That way when I'm staring at a register dump I have some chance of spotting that mask.

Also brackets around the bitwise & would make me feel more comfortable.

cheers

#5 lore raw

Re: [2/2] Detect instruction fetch denied and report

Quoted 18 lines · Michael Ellerman

arch/powerpc/mm/fault.c good_area: · line 407

Quoted 9 lines · Balbir Singh
arch/powerpc/mm/fault.c +1 -0
index a4db22f..f162e77 100644
@@ -404,6 +404,10 @@ good_area:
 		    (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
 		     !(vma->vm_flags & (VM_READ | VM_WRITE))))
 			goto bad_area;
+#ifdef CONFIG_PPC_RADIX_MMU

We shouldn't need the #ifdef, radix_enabled() will be false.

arch/powerpc/mm/fault.c good_area: · line 409

Quoted 2 lines
arch/powerpc/mm/fault.c +2 -0
+		if (radix_enabled() && regs->msr & PPC_BIT(35))
+			goto bad_area;

Is it really architected as radix only?

Quoted 7 lines

Personally I dislike PPC_BIT(), I'd rather you just used 0x10000000. That way when I'm staring at a register dump I have some chance of spotting that mask.

Also brackets around the bitwise & would make me feel more comfortable.

cheers

Will make the changes and submit

Balbir

#6 lore raw
In reply to Balbir Singh
Quoted 5 lines · Balbir Singh

ISA 3 defines new encoded access authority that allows instruction access prevention in privileged mode and allows normal access to problem state. This patch just enables IAMR (Instruction Authority Mask Register), enabling AMR would require more work.

Don't we need to do them in hypervisor mode. Ie, the hypervisor setup things such that guest privileged mode cannot execute guest userspace.

Quoted 19 lines

I've tested this with a buggy driver and a simple payload. The payload is specific to the build I've tested.

---
 arch/powerpc/mm/pgtable-radix.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/powerpc/mm/pgtable-radix.c +3 -0
index af897d9..9e25663 100644
@@ -294,6 +294,27 @@ found:
 	return;
 }
 
+/*
+ * For radix page tables we setup, the IAMR values as follows
+ * IMAR = 0100...00 (key 0 is set to 1)

arch/powerpc/mm/pgtable-radix.c found: · line 309

Quoted 10 lines
arch/powerpc/mm/pgtable-radix.c +10 -0
+ * AMOR = 1100....00 (Mask for key 0 is 11)
+ * AMR, UAMR, UAMOR are not affected
+ */
+static void __init radix_init_iamr(void)
+{
+	unsigned long iamr_mask = 0x4000000000000000;
+	unsigned long iamr = mfspr(SPRN_IAMR);
+
+	unsigned long amor_mask = 0xc000000000000000;
+	unsigned long amor = mfspr(SPRN_AMOR);

Isn't AMOR hypervisor privileged ?.

Quoted 21 lines
arch/powerpc/mm/pgtable-radix.c +9 -0
+
+	iamr |= iamr_mask;
+	amor |= amor_mask;
+
+	mtspr(SPRN_AMOR, amor);
+	mtspr(SPRN_IAMR, iamr);
+}
+
 void __init radix__early_init_mmu(void)
 {
 	unsigned long lpcr;
@@ -350,6 +371,7 @@ void __init radix__early_init_mmu(void)
 		radix_init_partition_table();
 	}
 
+	radix_init_iamr();
 	radix_init_pgtable();
 }
 
Signature · 1 line
2.5.5
#7 lore raw
Quoted 10 lines · Aneesh Kumar K.V
Quoted 5 lines · Balbir Singh

ISA 3 defines new encoded access authority that allows instruction access prevention in privileged mode and allows normal access to problem state. This patch just enables IAMR (Instruction Authority Mask Register), enabling AMR would require more work.

Don't we need to do them in hypervisor mode. Ie, the hypervisor setup things such that guest privileged mode cannot execute guest userspace.

Yes, true!

Quoted 17 lines
Quoted 17 lines

I've tested this with a buggy driver and a simple payload. The payload is specific to the build I've tested.

---
 arch/powerpc/mm/pgtable-radix.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/powerpc/mm/pgtable-radix.c +0 -0
index af897d9..9e25663 100644
@@ -294,6 +294,27 @@ found:
 	return;
 }
 
Quoted 16 lines

arch/powerpc/mm/pgtable-radix.c found: · line 309

Quoted 13 lines
arch/powerpc/mm/pgtable-radix.c +13 -0
+/*
+ * For radix page tables we setup, the IAMR values as follows
+ * IMAR = 0100...00 (key 0 is set to 1)
+ * AMOR = 1100....00 (Mask for key 0 is 11)
+ * AMR, UAMR, UAMOR are not affected
+ */
+static void __init radix_init_iamr(void)
+{
+	unsigned long iamr_mask = 0x4000000000000000;
+	unsigned long iamr = mfspr(SPRN_IAMR);
+
+	unsigned long amor_mask = 0xc000000000000000;
+	unsigned long amor = mfspr(SPRN_AMOR);

Isn't AMOR hypervisor privileged ?.

You are right, I should split the AMOR initialization to be HV only. IAMR is saved/restored during guest exit/entry.

So, the AMOR initialization needs to move.

I'll post a v2

Balbir Singh.