Skip to contentSkip to conversation

ARM: check stack pointer in get_wchan

PATCH In review Konstantin Khlebnikov 3 participants 5 messages Dec 5, 2013 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ARM: check stack pointer in get_wchan

get_wchan() is lockless. Task may wakeup at any time and change its own stack, thus each next stack frame may be overwritten and filled with random stuff.

/proc/$pid/stack interface had been disabled for non-current tasks, see [1] But 'wchan' still allows to trigger stack frame unwinding on volatile stack.

This patch fixes oops in unwind_frame() by adding stack pointer validation on each step (as x86 code do), unwind_frame() already checks frame pointer.

Also I've found another report of this oops on stackoverflow (irony).

---
 arch/arm/kernel/process.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/arm/kernel/process.c +5 -2
index 94f6b05..92f7b15 100644
@@ -404,6 +404,7 @@ EXPORT_SYMBOL(dump_fpu);
 unsigned long get_wchan(struct task_struct *p)
 {
 	struct stackframe frame;
+	unsigned long stack_page;
 	int count = 0;
 	if (!p || p == current || p->state == TASK_RUNNING)
 		return 0;
@@ -412,9 +413,11 @@ unsigned long get_wchan(struct task_struct *p)
 	frame.sp = thread_saved_sp(p);
 	frame.lr = 0;			/* recovered from the stack */
 	frame.pc = thread_saved_pc(p);
+	stack_page = (unsigned long)task_stack_page(p);
 	do {
-		int ret = unwind_frame(&frame);
-		if (ret < 0)
+		if (frame.sp < stack_page ||
+		    frame.sp >= stack_page + THREAD_SIZE ||
+		    unwind_frame(&frame) < 0)
 			return 0;
 		if (!in_sched_functions(frame.pc))
 			return frame.pc;
#1 lore raw

PATCH 2/2 ARM: fix framepointer check in unwind_frame

This patch fixes corner case when (fp + 4) overflows unsigned long, for example: fp = 0xFFFFFFFF -> fp + 4 == 3.

---
 arch/arm/kernel/stacktrace.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/kernel/stacktrace.c +1 -1
index 00f79e5..af4e8c8 100644
@@ -31,7 +31,7 @@ int notrace unwind_frame(struct stackframe *frame)
 	high = ALIGN(low, THREAD_SIZE);
 
 	/* check current frame pointer is within bounds */
-	if (fp < (low + 12) || fp + 4 >= high)
+	if (fp < low + 12 || fp > high - 4)
 		return -EINVAL;
 
 	/* restore the registers from the stack frame */
#4 lore raw
In reply to Konstantin Khlebnikov
Quoted 10 lines · Konstantin Khlebnikov

get_wchan() is lockless. Task may wakeup at any time and change its own stack, thus each next stack frame may be overwritten and filled with random stuff.

/proc/$pid/stack interface had been disabled for non-current tasks, see [1] But 'wchan' still allows to trigger stack frame unwinding on volatile stack.

This patch fixes oops in unwind_frame() by adding stack pointer validation on each step (as x86 code do), unwind_frame() already checks frame pointer.

Also I've found another report of this oops on stackoverflow (irony).

Your two patches look fine to me. Please put them in the patch system and we'll get them merged - I think they should also be merged into stable trees too - it looks like this goes all the way back to 2.6.30 kernels.

Thanks.

#2 lore raw

Re: [PATCH 2/2] ARM: fix framepointer check in unwind_frame

In reply to Konstantin Khlebnikov
Quoted 13 lines · Konstantin Khlebnikov

This patch fixes corner case when (fp + 4) overflows unsigned long, for example: fp = 0xFFFFFFFF -> fp + 4 == 3.

---
 arch/arm/kernel/stacktrace.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/kernel/stacktrace.c +0 -0
index 00f79e5..af4e8c8 100644

arch/arm/kernel/stacktrace.c int notrace unwind_frame(struct stackframe *frame) · line 34

Quoted 5 lines
arch/arm/kernel/stacktrace.c +1 -1
@@ -31,7 +31,7 @@ int notrace unwind_frame(struct stackframe *frame)
        high = ALIGN(low, THREAD_SIZE);
 
        /* check current frame pointer is within bounds */
-       if (fp < (low + 12) || fp + 4 >= high)
+       if (fp < low + 12 || fp > high - 4)

Shouldn't that use the '>=' comparison?

Jean

Quoted 9 lines
arch/arm/kernel/stacktrace.c +0 -1
                return -EINVAL;
 
        /* restore the registers from the stack frame */
 
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#3 lore raw

Re: [PATCH 2/2] ARM: fix framepointer check in unwind_frame

Quoted 17 lines · Jean Pihet
Quoted 15 lines · Konstantin Khlebnikov

This patch fixes corner case when (fp + 4) overflows unsigned long, for example: fp = 0xFFFFFFFF -> fp + 4 == 3.

---
 arch/arm/kernel/stacktrace.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/kernel/stacktrace.c +0 -0
index 00f79e5..af4e8c8 100644
@@ -31,7 +31,7 @@ int notrace unwind_frame(struct stackframe *frame)
        high = ALIGN(low, THREAD_SIZE);
 
Quoted 4 lines

arch/arm/kernel/stacktrace.c int notrace unwind_frame(struct stackframe *frame) · line 34

Quoted 3 lines
arch/arm/kernel/stacktrace.c +1 -1
        /* check current frame pointer is within bounds */
-       if (fp < (low + 12) || fp + 4 >= high)
+       if (fp < low + 12 || fp > high - 4)

Shouldn't that use the '>=' comparison?

nope, fp == high-4 is fine:

-------------------+
... -5 -4 -3 -2 -1 | -0
-------------------+

        ^             ^
        +---- fp      +----- high

stack frame contains four 'unsigned long' values: {fp, sp, lr, pc}. 'fp' points to 'pc', so totally there are 16 bytes: fp-12..fp+3

Quoted 11 lines

Jean

Quoted 9 lines
arch/arm/kernel/stacktrace.c +0 -1
                return -EINVAL;
 
        /* restore the registers from the stack frame */
 
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/