This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 10c0d09e2ac arch: add stack underflow check in up_schedule_sigaction
10c0d09e2ac is described below
commit 10c0d09e2accd836bf71496bd44b5a74cd8281d7
Author: anjiahao <[email protected]>
AuthorDate: Wed Nov 25 17:19:09 2026 +0800
arch: add stack underflow check in up_schedule_sigaction
up_schedule_sigaction() places the duplicated context frame
XCPTCONTEXT_SIZE bytes below the current regs with no bounds check,
so a near-full task stack makes the memcpy write past the stack
allocation. Add a PANIC() to catch this underflow for the
architectures that share the same regs-shift pattern: arm (Cortex-M
and Cortex-A/R), RISC-V, and tc32.
Only apply the check when the saved context is inside the task's stack
allocation. Kernel and protected builds can keep the interrupt context
on the separate interrupt stack, where comparing it against the task
stack base would panic on a valid signal delivery.
Signed-off-by: anjiahao <[email protected]>
---
arch/arm/src/arm_a_r/arm_schedulesigaction.c | 17 +++++++++++++++++
arch/arm/src/arm_m/arm_schedulesigaction.c | 17 +++++++++++++++++
arch/arm/src/tlsr82/tc32/tc32_schedulesigaction.c | 18 ++++++++++++++++++
arch/risc-v/src/common/riscv_schedulesigaction.c | 17 +++++++++++++++++
4 files changed, 69 insertions(+)
diff --git a/arch/arm/src/arm_a_r/arm_schedulesigaction.c
b/arch/arm/src/arm_a_r/arm_schedulesigaction.c
index 905bb940c84..36a646cfc69 100644
--- a/arch/arm/src/arm_a_r/arm_schedulesigaction.c
+++ b/arch/arm/src/arm_a_r/arm_schedulesigaction.c
@@ -93,6 +93,23 @@ void up_schedule_sigaction(struct tcb_s *tcb)
tcb->xcp.saved_regs = tcb->xcp.regs;
+ /* Verify that duplicating the context frame will not underflow the
+ * task stack. The new frame is placed XCPTCONTEXT_SIZE below the
+ * current one; if it drops below the stack allocation, the memcpy
+ * below would write outside the stack. Only check when the context
+ * was saved on this task's stack: kernel and protected builds keep
+ * the interrupt context on a separate interrupt stack.
+ */
+
+ if ((uint32_t)tcb->xcp.regs >= (uint32_t)tcb->stack_alloc_ptr &&
+ (uint32_t)tcb->xcp.regs <
+ (uint32_t)tcb->stack_alloc_ptr + tcb->adj_stack_size &&
+ (uint32_t)tcb->xcp.regs - XCPTCONTEXT_SIZE <
+ (uint32_t)tcb->stack_alloc_ptr)
+ {
+ PANIC();
+ }
+
/* Duplicate the register context. These will be
* restored by the signal trampoline after the signal has been
* delivered.
diff --git a/arch/arm/src/arm_m/arm_schedulesigaction.c
b/arch/arm/src/arm_m/arm_schedulesigaction.c
index a005c7630f1..cd9d878122f 100644
--- a/arch/arm/src/arm_m/arm_schedulesigaction.c
+++ b/arch/arm/src/arm_m/arm_schedulesigaction.c
@@ -120,6 +120,23 @@ void up_schedule_sigaction(struct tcb_s *tcb)
tcb->xcp.regs = (void *)STACKFRAME_ALIGN_DOWN(
(uint32_t)tcb->xcp.regs);
+ /* Verify that duplicating the context frame will not underflow the
+ * task stack. The new frame is placed XCPTCONTEXT_SIZE below the
+ * current one; if it drops below the stack allocation, the memcpy
+ * below would write outside the stack. Only check when the context
+ * was saved on this task's stack: kernel and protected builds keep
+ * the interrupt context on a separate interrupt stack.
+ */
+
+ if ((uint32_t)tcb->xcp.regs >= (uint32_t)tcb->stack_alloc_ptr &&
+ (uint32_t)tcb->xcp.regs <
+ (uint32_t)tcb->stack_alloc_ptr + tcb->adj_stack_size &&
+ (uint32_t)tcb->xcp.regs - XCPTCONTEXT_SIZE <
+ (uint32_t)tcb->stack_alloc_ptr)
+ {
+ PANIC();
+ }
+
/* Duplicate the register context. These will be
* restored by the signal trampoline after the signal has been
* delivered.
diff --git a/arch/arm/src/tlsr82/tc32/tc32_schedulesigaction.c
b/arch/arm/src/tlsr82/tc32/tc32_schedulesigaction.c
index 430e6d9510e..3092df8b879 100644
--- a/arch/arm/src/tlsr82/tc32/tc32_schedulesigaction.c
+++ b/arch/arm/src/tlsr82/tc32/tc32_schedulesigaction.c
@@ -28,6 +28,7 @@
#include <stdint.h>
#include <sched.h>
+#include <assert.h>
#include <nuttx/debug.h>
#include <nuttx/irq.h>
@@ -91,6 +92,23 @@ void up_schedule_sigaction(struct tcb_s *tcb)
tcb->xcp.saved_regs = tcb->xcp.regs;
+ /* Verify that duplicating the context frame will not underflow the
+ * task stack. The new frame is placed XCPTCONTEXT_SIZE below the
+ * current one; if it drops below the stack allocation, the memcpy
+ * below would write outside the stack. Only check when the context
+ * was saved on this task's stack: kernel and protected builds keep
+ * the interrupt context on a separate interrupt stack.
+ */
+
+ if ((uint32_t)tcb->xcp.regs >= (uint32_t)tcb->stack_alloc_ptr &&
+ (uint32_t)tcb->xcp.regs <
+ (uint32_t)tcb->stack_alloc_ptr + tcb->adj_stack_size &&
+ (uint32_t)tcb->xcp.regs - XCPTCONTEXT_SIZE <
+ (uint32_t)tcb->stack_alloc_ptr)
+ {
+ PANIC();
+ }
+
/* Duplicate the register context. These will be
* restored by the signal trampoline after the signal has been
* delivered.
diff --git a/arch/risc-v/src/common/riscv_schedulesigaction.c
b/arch/risc-v/src/common/riscv_schedulesigaction.c
index af492350aa6..03cfba6266a 100644
--- a/arch/risc-v/src/common/riscv_schedulesigaction.c
+++ b/arch/risc-v/src/common/riscv_schedulesigaction.c
@@ -93,6 +93,23 @@ void up_schedule_sigaction(struct tcb_s *tcb)
tcb->xcp.saved_regs = tcb->xcp.regs;
+ /* Verify that duplicating the context frame will not underflow the
+ * task stack. The new frame is placed XCPTCONTEXT_SIZE below the
+ * current one; if it drops below the stack allocation, the memcpy
+ * below would write outside the stack. Only check when the context
+ * was saved on this task's stack: kernel and protected builds keep
+ * the interrupt context on a separate interrupt stack.
+ */
+
+ if ((uintptr_t)tcb->xcp.regs >= (uintptr_t)tcb->stack_alloc_ptr &&
+ (uintptr_t)tcb->xcp.regs <
+ (uintptr_t)tcb->stack_alloc_ptr + tcb->adj_stack_size &&
+ (uintptr_t)tcb->xcp.regs - XCPTCONTEXT_SIZE <
+ (uintptr_t)tcb->stack_alloc_ptr)
+ {
+ PANIC();
+ }
+
/* Duplicate the register context. These will be
* restored by the signal trampoline after the signal has been
* delivered.