Skip to content

Commit 592700f

Browse files
mrutland-armwilldeacon
authored andcommitted
arm64: stacktrace: Better handle corrupted stacks
The arm64 stacktrace code is careful to only dereference frame records in valid stack ranges, ensuring that a corrupted frame record won't result in a faulting access. However, it's still possible for corrupt frame records to result in infinite loops in the stacktrace code, which is also undesirable. This patch ensures that we complete a stacktrace in finite time, by keeping track of which stacks we have already completed unwinding, and verifying that if the next frame record is on the same stack, it is at a higher address. As this has turned out to be particularly subtle, comments are added to explain the procedure. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Reviewed-by: James Morse <james.morse@arm.com> Tested-by: James Morse <james.morse@arm.com> Acked-by: Dave Martin <Dave.Martin@arm.com> Acked-by: Catalin Marinas <catalin.marinas@arm.com> Cc: Tengfei Fan <tengfeif@codeaurora.org> Signed-off-by: Will Deacon <will@kernel.org>
1 parent f3dcbe6 commit 592700f

2 files changed

Lines changed: 88 additions & 9 deletions

File tree

‎arch/arm64/include/asm/stacktrace.h‎

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,20 @@
88
#include <linux/percpu.h>
99
#include <linux/sched.h>
1010
#include <linux/sched/task_stack.h>
11+
#include <linux/types.h>
1112

1213
#include <asm/memory.h>
1314
#include <asm/ptrace.h>
1415
#include <asm/sdei.h>
1516

16-
struct stackframe {
17-
unsigned long fp;
18-
unsigned long pc;
19-
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
20-
int graph;
21-
#endif
22-
};
23-
2417
enum stack_type {
2518
STACK_TYPE_UNKNOWN,
2619
STACK_TYPE_TASK,
2720
STACK_TYPE_IRQ,
2821
STACK_TYPE_OVERFLOW,
2922
STACK_TYPE_SDEI_NORMAL,
3023
STACK_TYPE_SDEI_CRITICAL,
24+
__NR_STACK_TYPES
3125
};
3226

3327
struct stack_info {
@@ -36,6 +30,37 @@ struct stack_info {
3630
enum stack_type type;
3731
};
3832

33+
/*
34+
* A snapshot of a frame record or fp/lr register values, along with some
35+
* accounting information necessary for robust unwinding.
36+
*
37+
* @fp: The fp value in the frame record (or the real fp)
38+
* @pc: The fp value in the frame record (or the real lr)
39+
*
40+
* @stacks_done: Stacks which have been entirely unwound, for which it is no
41+
* longer valid to unwind to.
42+
*
43+
* @prev_fp: The fp that pointed to this frame record, or a synthetic value
44+
* of 0. This is used to ensure that within a stack, each
45+
* subsequent frame record is at an increasing address.
46+
* @prev_type: The type of stack this frame record was on, or a synthetic
47+
* value of STACK_TYPE_UNKNOWN. This is used to detect a
48+
* transition from one stack to another.
49+
*
50+
* @graph: When FUNCTION_GRAPH_TRACER is selected, holds the index of a
51+
* replacement lr value in the ftrace graph stack.
52+
*/
53+
struct stackframe {
54+
unsigned long fp;
55+
unsigned long pc;
56+
DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES);
57+
unsigned long prev_fp;
58+
enum stack_type prev_type;
59+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
60+
int graph;
61+
#endif
62+
};
63+
3964
extern int unwind_frame(struct task_struct *tsk, struct stackframe *frame);
4065
extern void walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
4166
int (*fn)(struct stackframe *, void *), void *data);
@@ -117,6 +142,9 @@ static inline bool on_accessible_stack(const struct task_struct *tsk,
117142
unsigned long sp,
118143
struct stack_info *info)
119144
{
145+
if (info)
146+
info->type = STACK_TYPE_UNKNOWN;
147+
120148
if (on_task_stack(tsk, sp, info))
121149
return true;
122150
if (tsk != current || preemptible())
@@ -139,6 +167,19 @@ static inline void start_backtrace(struct stackframe *frame,
139167
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
140168
frame->graph = 0;
141169
#endif
170+
171+
/*
172+
* Prime the first unwind.
173+
*
174+
* In unwind_frame() we'll check that the FP points to a valid stack,
175+
* which can't be STACK_TYPE_UNKNOWN, and the first unwind will be
176+
* treated as a transition to whichever stack that happens to be. The
177+
* prev_fp value won't be used, but we set it to 0 such that it is
178+
* definitely not an accessible stack address.
179+
*/
180+
bitmap_zero(frame->stacks_done, __NR_STACK_TYPES);
181+
frame->prev_fp = 0;
182+
frame->prev_type = STACK_TYPE_UNKNOWN;
142183
}
143184

144185
#endif /* __ASM_STACKTRACE_H */

‎arch/arm64/kernel/stacktrace.c‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,59 @@
2929
* ldp x29, x30, [sp]
3030
* add sp, sp, #0x10
3131
*/
32+
33+
/*
34+
* Unwind from one frame record (A) to the next frame record (B).
35+
*
36+
* We terminate early if the location of B indicates a malformed chain of frame
37+
* records (e.g. a cycle), determined based on the location and fp value of A
38+
* and the location (but not the fp value) of B.
39+
*/
3240
int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
3341
{
3442
unsigned long fp = frame->fp;
43+
struct stack_info info;
3544

3645
if (fp & 0xf)
3746
return -EINVAL;
3847

3948
if (!tsk)
4049
tsk = current;
4150

42-
if (!on_accessible_stack(tsk, fp, NULL))
51+
if (!on_accessible_stack(tsk, fp, &info))
4352
return -EINVAL;
4453

54+
if (test_bit(info.type, frame->stacks_done))
55+
return -EINVAL;
56+
57+
/*
58+
* As stacks grow downward, any valid record on the same stack must be
59+
* at a strictly higher address than the prior record.
60+
*
61+
* Stacks can nest in several valid orders, e.g.
62+
*
63+
* TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
64+
* TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
65+
*
66+
* ... but the nesting itself is strict. Once we transition from one
67+
* stack to another, it's never valid to unwind back to that first
68+
* stack.
69+
*/
70+
if (info.type == frame->prev_type) {
71+
if (fp <= frame->prev_fp)
72+
return -EINVAL;
73+
} else {
74+
set_bit(frame->prev_type, frame->stacks_done);
75+
}
76+
77+
/*
78+
* Record this frame record's values and location. The prev_fp and
79+
* prev_type are only meaningful to the next unwind_frame() invocation.
80+
*/
4581
frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
4682
frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
83+
frame->prev_fp = fp;
84+
frame->prev_type = info.type;
4785

4886
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4987
if (tsk->ret_stack &&

0 commit comments

Comments
 (0)