You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2026/08/03 13:00:38 UTC

(nuttx) branch releases/13.0 updated (8541b57e16c -> 32f578f37a1)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a change to branch releases/13.0
in repository https://gitbox.apache.org/repos/asf/nuttx.git


    from 8541b57e16c drivers/usbdev/cdcncm: fix TX corruption/wedge under write buffers
     new 6bfb85c96f6 arch/riscv: reject overlapping leaf PTE in riscv_fillpage
     new 41837f06995 arch/risc-v: kill faulting user task instead of panicking kernel
     new 32f578f37a1 arch/risc-v: fix fault handler misattributing kernel faults to user tasks

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 arch/risc-v/src/common/riscv_exception.c | 88 ++++++++++++++++++++++++--------
 1 file changed, 66 insertions(+), 22 deletions(-)


(nuttx) 03/03: arch/risc-v: fix fault handler misattributing kernel faults to user tasks

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch releases/13.0
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 32f578f37a1cc411745e6dce78e7bebde9d53769
Author: liang.huang <li...@houmo.ai>
AuthorDate: Sat Jul 18 11:33:30 2026 +0800

    arch/risc-v: fix fault handler misattributing kernel faults to user tasks
    
    riscv_fault_handler() only checked the task type and SYSCALL flag, so a
    fault taken inside an interrupt handler (running in kernel mode) was
    blamed on the user task that happened to be interrupted and killed with
    SIGSEGV, hiding the real kernel bug.  Use the STATUS_PPP bit of the trap
    frame to tell whether the fault originated from user mode: only then is
    it safe to kill the task.
    
    Signed-off-by: liang.huang <li...@houmo.ai>
---
 arch/risc-v/src/common/riscv_exception.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/arch/risc-v/src/common/riscv_exception.c b/arch/risc-v/src/common/riscv_exception.c
index 8196444c983..6f5c81b2fd8 100644
--- a/arch/risc-v/src/common/riscv_exception.c
+++ b/arch/risc-v/src/common/riscv_exception.c
@@ -87,10 +87,11 @@ static const char *g_reasons_str[RISCV_MAX_EXCEPTION + 1] =
  *
  * Description:
  *   Handle a fault caused by the running task. If the task is a user task
- *   not currently in a syscall, kill it with SIGSEGV instead of
- *   taking down the whole system. Otherwise (kernel thread, or a fault
- *   while already in kernel context on behalf of a syscall) there is no
- *   safe task to kill, so panic.
+ *   not currently in a syscall or interrupt context, kill it with SIGSEGV
+ *   instead of taking down the whole system. Otherwise (kernel thread, a
+ *   fault while already in kernel context on behalf of a syscall, or a
+ *   fault while handling an interrupt) there is no safe task to kill, so
+ *   panic.
  *
  * Input Parameters:
  *   cause - The (masked) machine cause of the exception, used for the
@@ -104,8 +105,14 @@ static void riscv_fault_handler(uintreg_t cause, void *regs)
 #ifdef CONFIG_ARCH_KERNEL_STACK
   struct tcb_s *tcb = this_task();
 
+  /* The STATUS_PPP check alone is enough: any kernel-mode fault (kernel
+   * thread, syscall body, or interrupt) has STATUS_PPP != 0.  The other
+   * two checks are kept as defensive redundancy.
+   */
+
   if (((tcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL) &&
-      ((tcb->flags & TCB_FLAG_SYSCALL) == false))
+      ((tcb->flags & TCB_FLAG_SYSCALL) == false) &&
+      !(((uintreg_t *)regs)[REG_INT_CTX] & STATUS_PPP))
     {
       struct tcb_s *ptcb = nxsched_get_tcb(tcb->group->tg_pid);
 


(nuttx) 01/03: arch/riscv: reject overlapping leaf PTE in riscv_fillpage

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch releases/13.0
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 6bfb85c96f65ad46a0401aa8b306f5e54ecac36c
Author: liang.huang <li...@houmo.ai>
AuthorDate: Wed Jul 8 09:09:41 2026 +0800

    arch/riscv: reject overlapping leaf PTE in riscv_fillpage
    
    riscv_fillpage() is the LOADPF/STOREPF handler used under
    CONFIG_PAGING. It checked whether intermediate page table levels were
    already allocated, but never checked the final leaf PTE before
    installing a new mapping.
    
    RISC-V raises the same LOADPF/STOREPF cause both when a leaf PTE is
    absent (a real fault) and when it is present but its permission bits
    don't satisfy the access, e.g. a store to a .text page whose write
    access was revoked after ELF loading. The two cases are
    indistinguishable from mcause alone.
    
    Treating both cases as "page missing" let riscv_fillpage silently
    allocate a fresh, zeroed physical page over an existing mapping,
    discarding the old page (a leak) and defeating whatever permission
    that mapping was enforcing. Reproduced on real hardware: a user-space
    store to an already-loaded .text page got a fresh writable page
    instead of being rejected.
    
    Check the leaf PTE's valid bit before allocating; if a mapping already
    exists, panic instead of overwriting it.
    
    Signed-off-by: liang.huang <li...@houmo.ai>
---
 arch/risc-v/src/common/riscv_exception.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/arch/risc-v/src/common/riscv_exception.c b/arch/risc-v/src/common/riscv_exception.c
index bcfcd34f872..584db45c025 100644
--- a/arch/risc-v/src/common/riscv_exception.c
+++ b/arch/risc-v/src/common/riscv_exception.c
@@ -201,9 +201,7 @@ int riscv_fillpage(int mcause, void *regs, void *args)
   else
     {
       _alert("PANIC!!! virtual address not mappable: %" PRIxPTR "\n", vaddr);
-      up_irq_save();
-      up_set_interrupt_context(true);
-      PANIC_WITH_REGS("panic", regs);
+      goto access_fault;
     }
 
   satp    = READ_CSR(CSR_SATP);
@@ -234,6 +232,21 @@ int riscv_fillpage(int mcause, void *regs, void *args)
     }
 
   ptlast = riscv_pgvaddr(paddr);
+
+  /* LOADPF/STOREPF is also raised when the leaf PTE already exists but its
+   * permission bits don't satisfy the access (e.g. a store to a .text page
+   * whose write access was revoked).  That is not a fault this function
+   * should handle: allocating a fresh page here would silently discard the
+   * existing mapping's page.
+   */
+
+  if (mmu_ln_getentry(ARCH_PGT_MAX_LEVELS, ptlast, vaddr) & PTE_VALID)
+    {
+      _alert("PANIC!!! page already mapped, permission violation: %"
+             PRIxPTR "\n", vaddr);
+      goto access_fault;
+    }
+
   paddr = mm_pgalloc(1);
   if (!paddr)
     {
@@ -249,6 +262,12 @@ int riscv_fillpage(int mcause, void *regs, void *args)
   mmu_ln_setentry(ARCH_PGT_MAX_LEVELS, ptlast, paddr, vaddr, mmuflags);
 
   return 0;
+
+access_fault:
+  up_irq_save();
+  up_set_interrupt_context(true);
+  PANIC_WITH_REGS("panic", regs);
+  return -EINVAL;
 }
 #endif /* CONFIG_PAGING */
 


(nuttx) 02/03: arch/risc-v: kill faulting user task instead of panicking kernel

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch releases/13.0
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 41837f06995908e8da7da1490730e8a8875a97db
Author: liang.huang <li...@houmo.ai>
AuthorDate: Thu Jul 9 11:32:03 2026 +0800

    arch/risc-v: kill faulting user task instead of panicking kernel
    
    riscv_fillpage() unconditionally panics the kernel on an unmappable
    or invalid access, even when the fault is caused by a user task. A
    user-space fault should terminate that task with SIGSEGV instead of
    taking down the whole system, matching the existing behavior for
    other exceptions in riscv_exception().
    
    Signed-off-by: liang.huang <li...@houmo.ai>
---
 arch/risc-v/src/common/riscv_exception.c | 66 ++++++++++++++++++++------------
 1 file changed, 42 insertions(+), 24 deletions(-)

diff --git a/arch/risc-v/src/common/riscv_exception.c b/arch/risc-v/src/common/riscv_exception.c
index 584db45c025..8196444c983 100644
--- a/arch/risc-v/src/common/riscv_exception.c
+++ b/arch/risc-v/src/common/riscv_exception.c
@@ -83,26 +83,27 @@ static const char *g_reasons_str[RISCV_MAX_EXCEPTION + 1] =
  ****************************************************************************/
 
 /****************************************************************************
- * Name: riscv_exception
+ * Name: riscv_fault_handler
  *
  * Description:
- *   This is the exception handler.
+ *   Handle a fault caused by the running task. If the task is a user task
+ *   not currently in a syscall, kill it with SIGSEGV instead of
+ *   taking down the whole system. Otherwise (kernel thread, or a fault
+ *   while already in kernel context on behalf of a syscall) there is no
+ *   safe task to kill, so panic.
+ *
+ * Input Parameters:
+ *   cause - The (masked) machine cause of the exception, used for the
+ *     panic message only.
+ *   regs  - A pointer to the register state at the time of the exception.
  *
  ****************************************************************************/
 
-int riscv_exception(int mcause, void *regs, void *args)
+static void riscv_fault_handler(uintreg_t cause, void *regs)
 {
 #ifdef CONFIG_ARCH_KERNEL_STACK
   struct tcb_s *tcb = this_task();
-#endif
-  uintreg_t cause = mcause & RISCV_IRQ_MASK;
-
-  _alert("EXCEPTION: %s. MCAUSE: %" PRIxREG ", EPC: %" PRIxREG
-         ", MTVAL: %" PRIxREG "\n",
-         mcause > RISCV_MAX_EXCEPTION ? "Unknown" : g_reasons_str[cause],
-         cause, READ_CSR(CSR_EPC), READ_CSR(CSR_TVAL));
 
-#ifdef CONFIG_ARCH_KERNEL_STACK
   if (((tcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL) &&
       ((tcb->flags & TCB_FLAG_SYSCALL) == false))
     {
@@ -124,15 +125,34 @@ int riscv_exception(int mcause, void *regs, void *args)
        */
 
       running_regs()[REG_SP] = tcb->xcp.ktopstk;
+      return;
     }
-  else
 #endif
-    {
-      _alert("PANIC!!! Exception = %" PRIxREG "\n", cause);
-      up_irq_save();
-      up_set_interrupt_context(true);
-      PANIC_WITH_REGS("panic", regs);
-    }
+
+  _alert("PANIC!!! Exception = %" PRIxREG "\n", cause);
+  up_irq_save();
+  up_set_interrupt_context(true);
+  PANIC_WITH_REGS("panic", regs);
+}
+
+/****************************************************************************
+ * Name: riscv_exception
+ *
+ * Description:
+ *   This is the exception handler.
+ *
+ ****************************************************************************/
+
+int riscv_exception(int mcause, void *regs, void *args)
+{
+  uintreg_t cause = mcause & RISCV_IRQ_MASK;
+
+  _alert("EXCEPTION: %s. MCAUSE: %" PRIxREG ", EPC: %" PRIxREG
+         ", MTVAL: %" PRIxREG "\n",
+         mcause > RISCV_MAX_EXCEPTION ? "Unknown" : g_reasons_str[cause],
+         cause, READ_CSR(CSR_EPC), READ_CSR(CSR_TVAL));
+
+  riscv_fault_handler(cause, regs);
 
   return 0;
 }
@@ -200,7 +220,7 @@ int riscv_fillpage(int mcause, void *regs, void *args)
     }
   else
     {
-      _alert("PANIC!!! virtual address not mappable: %" PRIxPTR "\n", vaddr);
+      _alert("Virtual address not mappable: %" PRIxPTR "\n", vaddr);
       goto access_fault;
     }
 
@@ -242,7 +262,7 @@ int riscv_fillpage(int mcause, void *regs, void *args)
 
   if (mmu_ln_getentry(ARCH_PGT_MAX_LEVELS, ptlast, vaddr) & PTE_VALID)
     {
-      _alert("PANIC!!! page already mapped, permission violation: %"
+      _alert("Page already mapped, permission violation: %"
              PRIxPTR "\n", vaddr);
       goto access_fault;
     }
@@ -264,10 +284,8 @@ int riscv_fillpage(int mcause, void *regs, void *args)
   return 0;
 
 access_fault:
-  up_irq_save();
-  up_set_interrupt_context(true);
-  PANIC_WITH_REGS("panic", regs);
-  return -EINVAL;
+  riscv_fault_handler(cause, regs);
+  return 0;
 }
 #endif /* CONFIG_PAGING */