diff mbox series

[RFC,20/25] powerpc/book3s64/kuap: Improve error reporting with KUAP

Message ID 20200427080507.1626327-21-aneesh.kumar@linux.ibm.com (mailing list archive)
State Superseded
Headers show
Series Kernel userspace access/execution prevention with hash translation | expand

Checks

Context Check Description
snowpatch_ozlabs/apply_patch warning Failed to apply on branch powerpc/merge (54dc28ff5e0b3585224d49a31b53e030342ca5c3)
snowpatch_ozlabs/apply_patch warning Failed to apply on branch powerpc/next (45591da765885f7320a111d290b3a28a23eed359)
snowpatch_ozlabs/apply_patch warning Failed to apply on branch linus/master (6a8b55ed4056ea5559ebe4f6a4b247f627870d4c)
snowpatch_ozlabs/apply_patch warning Failed to apply on branch powerpc/fixes (5990cdee689c6885b27c6d969a3d58b09002b0bc)
snowpatch_ozlabs/apply_patch warning Failed to apply on branch linux-next (8bdabd09ec86a993419c8c98a4f34c12bc902c6c)
snowpatch_ozlabs/apply_patch fail Failed to apply to any branch

Commit Message

Aneesh Kumar K V April 27, 2020, 8:05 a.m. UTC
With hash translation use DSISR_KEYFAULT to identify a wrong access.
With Radix we look at the AMR value and type of fault.

Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
---
 arch/powerpc/include/asm/book3s/64/kup.h |  7 ------
 arch/powerpc/mm/fault.c                  | 27 +++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/arch/powerpc/include/asm/book3s/64/kup.h b/arch/powerpc/include/asm/book3s/64/kup.h
index 590c1d9455d1..f564d909e195 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -280,13 +280,6 @@  static inline void restore_user_access(unsigned long flags)
 	set_kuap(flags);
 }
 
-static inline bool
-bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
-{
-	return WARN(mmu_has_feature(MMU_FTR_KUAP) &&
-		    (regs->kuap & (is_write ? AMR_KUAP_BLOCK_WRITE : AMR_KUAP_BLOCK_READ)),
-		    "Bug: %s fault blocked by AMR!", is_write ? "Write" : "Read");
-}
 #else /* CONFIG_PPC_KUAP */
 static inline void kuap_restore_amr(struct pt_regs *regs)
 {
diff --git a/arch/powerpc/mm/fault.c b/arch/powerpc/mm/fault.c
index 84af6c8eecf7..4f866b11512c 100644
--- a/arch/powerpc/mm/fault.c
+++ b/arch/powerpc/mm/fault.c
@@ -198,6 +198,31 @@  static int mm_fault_error(struct pt_regs *regs, unsigned long addr,
 	return 0;
 }
 
+#define RADIX_KUAP_BLOCK_READ	UL(0x4000000000000000)
+#define RADIX_KUAP_BLOCK_WRITE	UL(0x8000000000000000)
+
+static bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
+			   bool is_write, unsigned long error_code)
+{
+	if (!mmu_has_feature(MMU_FTR_KUAP))
+		return false;
+
+	if (radix_enabled()) {
+		/*
+		 * Will be a storage protection fault.
+		 * Only check the details of AMR[0]
+		 */
+		return WARN((regs->kuap & (is_write ? RADIX_KUAP_BLOCK_WRITE : RADIX_KUAP_BLOCK_READ)),
+			    "Bug: %s fault blocked by AMR!", is_write ? "Write" : "Read");
+	}
+	/*
+	 * We don't want to WARN here because userspace can setup
+	 * keys such that a kernel access to user address can cause
+	 * fault
+	 */
+	return !!(error_code & DSISR_KEYFAULT);
+}
+
 /* Is this a bad kernel fault ? */
 static bool bad_kernel_fault(struct pt_regs *regs, unsigned long error_code,
 			     unsigned long address, bool is_write)
@@ -233,7 +258,7 @@  static bool bad_kernel_fault(struct pt_regs *regs, unsigned long error_code,
 
 	// Read/write fault in a valid region (the exception table search passed
 	// above), but blocked by KUAP is bad, it can never succeed.
-	if (bad_kuap_fault(regs, address, is_write))
+	if (bad_kuap_fault(regs, address, is_write, error_code))
 		return true;
 
 	// What's left? Kernel fault on user in well defined regions (extable