Skip to content

Commit eb232b1

Browse files
kvaneeshmpe
authored andcommitted
powerpc/book3s64/kuap: Improve error reporting with KUAP
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 <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4d6c551 commit eb232b1

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

arch/powerpc/include/asm/book3s/32/kup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ static inline void restore_user_access(unsigned long flags)
177177
allow_user_access(to, to, end - addr, KUAP_READ_WRITE);
178178
}
179179

180-
static inline bool
181-
bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
180+
static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
181+
bool is_write, unsigned long error_code)
182182
{
183183
unsigned long begin = regs->kuap & 0xf0000000;
184184
unsigned long end = regs->kuap << 28;

arch/powerpc/include/asm/book3s/64/kup.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,29 @@ static inline void set_kuap(unsigned long value)
303303
isync();
304304
}
305305

306-
static inline bool
307-
bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
306+
#define RADIX_KUAP_BLOCK_READ UL(0x4000000000000000)
307+
#define RADIX_KUAP_BLOCK_WRITE UL(0x8000000000000000)
308+
309+
static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
310+
bool is_write, unsigned long error_code)
308311
{
309-
return WARN(mmu_has_feature(MMU_FTR_BOOK3S_KUAP) &&
310-
(regs->kuap & (is_write ? AMR_KUAP_BLOCK_WRITE : AMR_KUAP_BLOCK_READ)),
311-
"Bug: %s fault blocked by AMR!", is_write ? "Write" : "Read");
312+
if (!mmu_has_feature(MMU_FTR_BOOK3S_KUAP))
313+
return false;
314+
315+
if (radix_enabled()) {
316+
/*
317+
* Will be a storage protection fault.
318+
* Only check the details of AMR[0]
319+
*/
320+
return WARN((regs->kuap & (is_write ? RADIX_KUAP_BLOCK_WRITE : RADIX_KUAP_BLOCK_READ)),
321+
"Bug: %s fault blocked by AMR!", is_write ? "Write" : "Read");
322+
}
323+
/*
324+
* We don't want to WARN here because userspace can setup
325+
* keys such that a kernel access to user address can cause
326+
* fault
327+
*/
328+
return !!(error_code & DSISR_KEYFAULT);
312329
}
313330

314331
static __always_inline void allow_user_access(void __user *to, const void __user *from,

arch/powerpc/include/asm/kup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ void setup_kuap(bool disabled);
5959
#else
6060
static inline void setup_kuap(bool disabled) { }
6161

62-
static inline bool
63-
bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
62+
static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
63+
bool is_write, unsigned long error_code)
6464
{
6565
return false;
6666
}

arch/powerpc/include/asm/nohash/32/kup-8xx.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ static inline void restore_user_access(unsigned long flags)
6060
mtspr(SPRN_MD_AP, flags);
6161
}
6262

63-
static inline bool
64-
bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
63+
static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
64+
bool is_write, unsigned long error_code)
6565
{
6666
return WARN(!((regs->kuap ^ MD_APG_KUAP) & 0xff000000),
6767
"Bug: fault blocked by AP register !");

arch/powerpc/mm/fault.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ static bool bad_kernel_fault(struct pt_regs *regs, unsigned long error_code,
227227

228228
// Read/write fault in a valid region (the exception table search passed
229229
// above), but blocked by KUAP is bad, it can never succeed.
230-
if (bad_kuap_fault(regs, address, is_write))
230+
if (bad_kuap_fault(regs, address, is_write, error_code))
231231
return true;
232232

233233
// What's left? Kernel fault on user in well defined regions (extable

0 commit comments

Comments
 (0)