Skip to contentSkip to conversation

Let illegal access to user-space memory die

PATCH In review Eric Lin 3 participants 6 messages Dec 4, 2020 – Feb 1, 2021 RISC-V

lore
Share & tools
lore

Eric Lin

author
#0 lore raw

v3 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 v3:
-Let no_context() use die_kernel_fault() helper

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 | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
Signature · 7 lines
2.17.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#1 lore raw

PATCH v3 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 | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
arch/riscv/mm/fault.c +17 -6
index 3c8b9e433c67..0d5f06d6e3c7 100644
@@ -19,8 +19,23 @@
 
 #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)
 {
+	const char *msg;
+
 	/* Are we prepared to handle this kernel fault? */
 	if (fixup_exception(regs))
 		return;
@@ -29,12 +44,8 @@ static inline void no_context(struct pt_regs *regs, unsigned long addr)
 	 * Oops. The kernel tried to access some bad page. We'll have to
 	 * terminate things with extreme prejudice.
 	 */
-	bust_spinlocks(1);
-	pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n",
-		(addr < PAGE_SIZE) ? "NULL pointer dereference" :
-		"paging request", addr);
-	die(regs, "Oops");
-	do_exit(SIGKILL);
+	msg = (addr < PAGE_SIZE) ? "NULL pointer dereference" : "paging request";
+	die_kernel_fault(msg, addr, regs);
 }
 
 static inline void mm_fault_error(struct pt_regs *regs, unsigned long addr, vm_fault_t fault)
Signature · 7 lines
2.17.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#2 lore raw

PATCH v3 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 0d5f06d6e3c7..33d284188f9a 100644
@@ -243,6 +243,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
author
#3 lore raw
In reply to Eric Lin
Quoted 3 lines · Eric Te-Sheng Lin(?L?w??)

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.

Hi Palmer, kindly ping.

Thanks.

Quoted 8 lines

Changes in v3:
-Let no_context() use die_kernel_fault() helper

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

Quoted 11 lines

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 | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
Signature · 1 line
2.17.0

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

maintainer
#4 lore raw
In reply to Eric Lin
Quoted 11 lines · On Thu, 03 Dec 2020 21:42:57 PST (-0800), [email protected] wrote:

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 v3:
-Let no_context() use die_kernel_fault() helper

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

Quoted 7 lines

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 | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

Thanks, these will be on for-next when the merge window ends.

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

#5 lore raw
Quoted 17 lines · Palmer Dabbelt
Quoted 16 lines · On Thu, 03 Dec 2020 21:42:57 PST (-0800), [email protected] wrote:

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 v3:
    -Let no_context() use die_kernel_fault() helper

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

Quoted 4 lines
Quoted 2 lines

 arch/riscv/mm/fault.c | 28 ++++++++++++++++++++++------

 1 file changed, 22 insertions(+), 6 deletions(-)

Thanks, these will be on for-next when the merge window ends.

Just tested this and it seems to be working.

Signature · 9 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

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