powerpc/book3s64/kuap: Improve error reporting with KUAP
authorAneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Fri, 27 Nov 2020 04:44:18 +0000 (10:14 +0530)
committerMichael Ellerman <mpe@ellerman.id.au>
Thu, 3 Dec 2020 14:01:26 +0000 (01:01 +1100)
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>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20201127044424.40686-17-aneesh.kumar@linux.ibm.com
arch/powerpc/include/asm/book3s/32/kup.h
arch/powerpc/include/asm/book3s/64/kup.h
arch/powerpc/include/asm/kup.h
arch/powerpc/include/asm/nohash/32/kup-8xx.h
arch/powerpc/mm/fault.c

index 32fd4452e9602952effba181d08f8892050069ae..b18cd931e32519f4f768de50087c9ad66a76f267 100644 (file)
@@ -177,8 +177,8 @@ static inline void restore_user_access(unsigned long flags)
                allow_user_access(to, to, end - addr, KUAP_READ_WRITE);
 }
 
-static inline bool
-bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
+static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
+                                 bool is_write, unsigned long error_code)
 {
        unsigned long begin = regs->kuap & 0xf0000000;
        unsigned long end = regs->kuap << 28;
index 4fa0760a47a46032e4bfcb442461a928c60b7a6f..f8f87b5c0e678f14a417a145473e9a9e7482fe5e 100644 (file)
@@ -303,12 +303,29 @@ static inline void set_kuap(unsigned long value)
        isync();
 }
 
-static inline bool
-bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
+#define RADIX_KUAP_BLOCK_READ  UL(0x4000000000000000)
+#define RADIX_KUAP_BLOCK_WRITE UL(0x8000000000000000)
+
+static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
+                                 bool is_write, unsigned long error_code)
 {
-       return WARN(mmu_has_feature(MMU_FTR_BOOK3S_KUAP) &&
-                   (regs->kuap & (is_write ? AMR_KUAP_BLOCK_WRITE : AMR_KUAP_BLOCK_READ)),
-                   "Bug: %s fault blocked by AMR!", is_write ? "Write" : "Read");
+       if (!mmu_has_feature(MMU_FTR_BOOK3S_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);
 }
 
 static __always_inline void allow_user_access(void __user *to, const void __user *from,
index a06e50b68d40aaab8feb835b8ab477830a16638b..952be0414f433b578a6d288bd6948b73e8c08040 100644 (file)
@@ -59,8 +59,8 @@ void setup_kuap(bool disabled);
 #else
 static inline void setup_kuap(bool disabled) { }
 
-static inline bool
-bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
+static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
+                                 bool is_write, unsigned long error_code)
 {
        return false;
 }
index 567cdc55740241c653c56f3e5f37b4fc2a178098..7bdd9e5b63ed28490aa166b3f47c97eb722b52ba 100644 (file)
@@ -60,8 +60,8 @@ static inline void restore_user_access(unsigned long flags)
        mtspr(SPRN_MD_AP, flags);
 }
 
-static inline bool
-bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
+static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
+                                 bool is_write, unsigned long error_code)
 {
        return WARN(!((regs->kuap ^ MD_APG_KUAP) & 0xff000000),
                    "Bug: fault blocked by AP register !");
index 0add963a849b3d6816289c6c818ea13424cb11f8..c91621df0c612c1b65786dedb6be4ed7bed35ee6 100644 (file)
@@ -227,7 +227,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