Skip to contentSkip to conversation

Let illegal access to user-space memory die

PATCH Superseded Eric Lin 2 participants 5 messages Dec 3, 2020 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Eric Lin

author
#0 lore raw

v2 cover Let illegal access to user-space memory die

Accesses to user-space memory without calling uaccess routine leads to hanging in page fault handler. Like arm64, we let it die earlier in page fault handler.

Changes in v2:
-Add a die_kernel_fault() helper
-Split one long line code into two

Eric Lin (2):
riscv/mm: Introduce a die_kernel_fault() helper function riscv/mm: Prevent kernel module to access user memory without uaccess
routines

 arch/riscv/mm/fault.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
Signature · 1 line
2.17.0
author
#1 lore raw

PATCH v2 1/2 riscv/mm: Introduce a die_kernel_fault() helper function

Like arm64, this patch adds a die_kernel_fault() helper to ensure the same semantics for the different kernel faults.

---
 arch/riscv/mm/fault.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
arch/riscv/mm/fault.c +13 -0
index 3c8b9e433c67..0bcfd0e1b39e 100644
@@ -19,6 +19,19 @@
 
 #include "../kernel/head.h"
 
+static void die_kernel_fault(const char *msg, unsigned long addr,
+		struct pt_regs *regs)
+{
+	bust_spinlocks(1);
+
+	pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n", msg,
+			addr);
+
+	bust_spinlocks(0);
+	die(regs, "Oops");
+	do_exit(SIGKILL);
+}
+
 static inline void no_context(struct pt_regs *regs, unsigned long addr)
 {
 	/* Are we prepared to handle this kernel fault? */
Signature · 1 line
2.17.0
author
#2 lore raw

PATCH v2 2/2 riscv/mm: Prevent kernel module to access user memory without uaccess routines

In reply to Eric Lin

We found this issue in an legacy out-of-tree kernel module which didn't properly access user space pointer by get/put_user(). Such an illegal access loops in the page fault handler. To resolve this, let it die here.

---
 arch/riscv/mm/fault.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/mm/fault.c +5 -0
index 0bcfd0e1b39e..00884c1bac28 100644
@@ -245,6 +245,11 @@ asmlinkage void do_page_fault(struct pt_regs *regs)
 	if (user_mode(regs))
 		flags |= FAULT_FLAG_USER;
 
+	if (!user_mode(regs) && addr < TASK_SIZE &&
+			unlikely(!(regs->status & SR_SUM)))
+		die_kernel_fault("access to user memory without uaccess routines",
+				addr, regs);
+
 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
 
 	if (cause == EXC_STORE_PAGE_FAULT)
Signature · 1 line
2.17.0
Reviewed-by on the v2 series
#3 lore raw
In reply to Eric Lin

Hi Eric,

Quoted 8 lines · Eric Lin

Accesses to user-space memory without calling uaccess routine leads to hanging in page fault handler. Like arm64, we let it die earlier in page fault handler.

Changes in v2:
-Add a die_kernel_fault() helper
-Split one long line code into two

Please also make no_context() use the new helper. Other than that:

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author
#4 lore raw

On Thu, Dec 03, 2020 at 03:29:57PM +0800, Pekka Enberg wrote:

Hi Pekka,

Quoted 8 lines

Hi Eric,

Quoted 5 lines · Eric Lin

Accesses to user-space memory without calling uaccess routine leads to hanging in page fault handler. Like arm64, we let it die earlier in page fault handler.

Quoted 6 lines
Quoted 3 lines

Changes in v2:
-Add a die_kernel_fault() helper
-Split one long line code into two

Please also make no_context() use the new helper. Other than that:

OK, I'll make no_context() use the new helper in v3. Thanks for your review.

Quoted 1 line