Skip to content

Commit 475c874

Browse files
kvaneeshmpe
authored andcommitted
powerpc/book3s64/kuap: Improve error reporting with KUAP
This partially reverts commit eb232b1 ("powerpc/book3s64/kuap: Improve error reporting with KUAP") and update the fault handler to print [ 55.022514] Kernel attempted to access user page (7e6725b70000) - exploit attempt? (uid: 0) [ 55.022528] BUG: Unable to handle kernel data access on read at 0x7e6725b70000 [ 55.022533] Faulting instruction address: 0xc000000000e8b9bc [ 55.022540] Oops: Kernel access of bad area, sig: 11 [#1] .... when the kernel access userspace address without unlocking AMR. bad_kuap_fault() is added as part of commit 5e5be3a ("powerpc/mm: Detect bad KUAP faults") to catch userspace access incorrectly blocked by AMR. Hence retain the full stack dump there even with hash translation. Also, add a comment explaining the difference between hash and radix. 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 250ad7a commit 475c874

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
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 bad_kuap_fault(struct pt_regs *regs, unsigned long address,
181-
bool is_write, unsigned long error_code)
180+
static inline bool
181+
bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
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: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -353,29 +353,29 @@ static inline void set_kuap(unsigned long value)
353353
isync();
354354
}
355355

356-
#define RADIX_KUAP_BLOCK_READ UL(0x4000000000000000)
357-
#define RADIX_KUAP_BLOCK_WRITE UL(0x8000000000000000)
358-
359356
static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
360-
bool is_write, unsigned long error_code)
357+
bool is_write)
361358
{
362359
if (!mmu_has_feature(MMU_FTR_BOOK3S_KUAP))
363360
return false;
364-
365-
if (radix_enabled()) {
366-
/*
367-
* Will be a storage protection fault.
368-
* Only check the details of AMR[0]
369-
*/
370-
return WARN((regs->kuap & (is_write ? RADIX_KUAP_BLOCK_WRITE : RADIX_KUAP_BLOCK_READ)),
371-
"Bug: %s fault blocked by AMR!", is_write ? "Write" : "Read");
372-
}
373361
/*
374-
* We don't want to WARN here because userspace can setup
375-
* keys such that a kernel access to user address can cause
376-
* fault
362+
* For radix this will be a storage protection fault (DSISR_PROTFAULT).
363+
* For hash this will be a key fault (DSISR_KEYFAULT)
377364
*/
378-
return !!(error_code & DSISR_KEYFAULT);
365+
/*
366+
* We do have exception table entry, but accessing the
367+
* userspace results in fault. This could be because we
368+
* didn't unlock the AMR or access is denied by userspace
369+
* using a key value that blocks access. We are only interested
370+
* in catching the use case of accessing without unlocking
371+
* the AMR. Hence check for BLOCK_WRITE/READ against AMR.
372+
*/
373+
if (is_write) {
374+
return WARN(((regs->amr & AMR_KUAP_BLOCK_WRITE) == AMR_KUAP_BLOCK_WRITE),
375+
"Bug: Write fault blocked by AMR!");
376+
}
377+
return WARN(((regs->amr & AMR_KUAP_BLOCK_READ) == AMR_KUAP_BLOCK_READ),
378+
"Bug: Read fault blocked by AMR!");
379379
}
380380

381381
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
@@ -62,8 +62,8 @@ void setup_kuap(bool disabled);
6262
#else
6363
static inline void setup_kuap(bool disabled) { }
6464

65-
static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
66-
bool is_write, unsigned long error_code)
65+
static inline bool
66+
bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
6767
{
6868
return false;
6969
}

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

arch/powerpc/mm/fault.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static bool bad_kernel_fault(struct pt_regs *regs, unsigned long error_code,
210210
return true;
211211
}
212212

213-
if (!is_exec && address < TASK_SIZE && (error_code & DSISR_PROTFAULT) &&
213+
if (!is_exec && address < TASK_SIZE && (error_code & (DSISR_PROTFAULT | DSISR_KEYFAULT)) &&
214214
!search_exception_tables(regs->nip)) {
215215
pr_crit_ratelimited("Kernel attempted to access user page (%lx) - exploit attempt? (uid: %d)\n",
216216
address,
@@ -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, error_code))
230+
if (bad_kuap_fault(regs, address, is_write))
231231
return true;
232232

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

0 commit comments

Comments
 (0)