Skip to contentSkip to conversation

riscv/mm: Prevent kernel module access user-space memory without uaccess routines

PATCH In review Eric Lin 3 participants 5 messages Nov 30, 2020 – Dec 1, 2020 RISC-V

lore
Share & tools
lore

Eric Lin

author
#0 lore raw

PATCH 1/1 riscv/mm: Prevent kernel module access user-space memory without uaccess routines

In the page fault handler, an access to user-space memory without get/put_user() or copy_from/to_user() routines is not resolved properly. Like arm and other architectures, we need to let it die earlier in page fault handler.

---
 arch/riscv/mm/fault.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/mm/fault.c +3 -0
index 3c8b9e433c67..a452cfa266a2 100644
@@ -232,6 +232,9 @@ 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(regs, "Accessing user space memory without uaccess routines\n");
+
 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
 
 	if (cause == EXC_STORE_PAGE_FAULT)
Signature · 7 lines
2.17.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw
Quoted 5 lines · Eric Lin

In the page fault handler, an access to user-space memory without get/put_user() or copy_from/to_user() routines is not resolved properly. Like arm and other architectures, we need to let it die earlier in page fault handler.

Fix looks good to me. Can you elaborate on how you found the issue and how the bug manifests itself?

Quoted 12 lines
---
 arch/riscv/mm/fault.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/mm/fault.c +0 -0
index 3c8b9e433c67..a452cfa266a2 100644

arch/riscv/mm/fault.c asmlinkage void do_page_fault(struct pt_regs *regs) · line 236

Quoted 5 lines
arch/riscv/mm/fault.c +2 -0
@@ -232,6 +232,9 @@ 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(regs, "Accessing user space memory without uaccess routines\n");

Let's introduce a die_kernel_fault() helper (similar to arm64, for example) to ensure same semantics for the different kernel faults. You can extract the helper from no_context().

Quoted 8 lines
arch/riscv/mm/fault.c +1 -1
+
        perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
 
        if (cause == EXC_STORE_PAGE_FAULT)
--

2.17.0

Quoted 4 lines

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

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

author
#2 lore raw

On Mon, Nov 30, 2020 at 04:07:03PM +0800, Pekka Enberg wrote:

Hi Pekka,

Quoted 9 lines · Eric Lin
Quoted 5 lines

In the page fault handler, an access to user-space memory without get/put_user() or copy_from/to_user() routines is not resolved properly. Like arm and other architectures, we need to let it die earlier in page fault handler.

Fix looks good to me. Can you elaborate on how you found the issue and how the bug manifests itself?

OK, I'll elaborate more on the commit message.

Quoted 16 lines
Quoted 16 lines
---
 arch/riscv/mm/fault.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/mm/fault.c +0 -0
index 3c8b9e433c67..a452cfa266a2 100644
@@ -232,6 +232,9 @@ asmlinkage void do_page_fault(struct pt_regs *regs)
        if (user_mode(regs))
                flags |= FAULT_FLAG_USER;
 
Quoted 6 lines

arch/riscv/mm/fault.c asmlinkage void do_page_fault(struct pt_regs *regs) · line 236

Quoted 2 lines
arch/riscv/mm/fault.c +2 -0
+       if (!user_mode(regs) && addr < TASK_SIZE && unlikely(!(regs->status & SR_SUM)))
+               die(regs, "Accessing user space memory without uaccess routines\n");

Let's introduce a die_kernel_fault() helper (similar to arm64, for example) to ensure same semantics for the different kernel faults. You can extract the helper from no_context().

OK, I'll add a die_kernel_fault() helper function in v2.

Thanks for your review.

Quoted 9 lines
Quoted 9 lines
arch/riscv/mm/fault.c +1 -1
+
        perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
 
        if (cause == EXC_STORE_PAGE_FAULT)
--

2.17.0

Quoted 4 lines
Quoted 4 lines

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

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

author
#4 lore raw

On Mon, Nov 30, 2020 at 04:30:15PM +0800, Christoph Hellwig wrote:
Hi Christoph,

Quoted 3 lines

arch/riscv/mm/fault.c asmlinkage void do_page_fault(struct pt_regs *regs) · line 235

Quoted 1 line
arch/riscv/mm/fault.c +1 -0
+	if (!user_mode(regs) && addr < TASK_SIZE && unlikely(!(regs->status & SR_SUM)))

Please avoid the overly long line.

OK, I'll modify it in v2. Thanks for your review.