]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-4.19/tracing-have-trace_stack-nr_entries-compare-not-be-s.patch
autosel patches for 4.19
[thirdparty/kernel/stable-queue.git] / queue-4.19 / tracing-have-trace_stack-nr_entries-compare-not-be-s.patch
1 From af4dc639e26f2f315eeb3bb2e9f593871d7d1f8c Mon Sep 17 00:00:00 2001
2 From: Dan Carpenter <dan.carpenter@oracle.com>
3 Date: Wed, 20 Jun 2018 14:08:00 +0300
4 Subject: tracing: Have trace_stack nr_entries compare not be so subtle
5
6 [ Upstream commit ca16b0fbb05242f18da9d810c07d3882ffed831c ]
7
8 Dan Carpenter reviewed the trace_stack.c code and figured he found an off by
9 one bug.
10
11 "From reviewing the code, it seems possible for
12 stack_trace_max.nr_entries to be set to .max_entries and in that case we
13 would be reading one element beyond the end of the stack_dump_trace[]
14 array. If it's not set to .max_entries then the bug doesn't affect
15 runtime."
16
17 Although it looks to be the case, it is not. Because we have:
18
19 static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
20 { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
21
22 struct stack_trace stack_trace_max = {
23 .max_entries = STACK_TRACE_ENTRIES - 1,
24 .entries = &stack_dump_trace[0],
25 };
26
27 And:
28
29 stack_trace_max.nr_entries = x;
30 for (; x < i; x++)
31 stack_dump_trace[x] = ULONG_MAX;
32
33 Even if nr_entries equals max_entries, indexing with it into the
34 stack_dump_trace[] array will not overflow the array. But if it is the case,
35 the second part of the conditional that tests stack_dump_trace[nr_entries]
36 to ULONG_MAX will always be true.
37
38 By applying Dan's patch, it removes the subtle aspect of it and makes the if
39 conditional slightly more efficient.
40
41 Link: http://lkml.kernel.org/r/20180620110758.crunhd5bfep7zuiz@kili.mountain
42
43 Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
44 Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
45 Signed-off-by: Sasha Levin <sashal@kernel.org>
46 ---
47 kernel/trace/trace_stack.c | 2 +-
48 1 file changed, 1 insertion(+), 1 deletion(-)
49
50 diff --git a/kernel/trace/trace_stack.c b/kernel/trace/trace_stack.c
51 index 4237eba4ef20..6e3edd745c68 100644
52 --- a/kernel/trace/trace_stack.c
53 +++ b/kernel/trace/trace_stack.c
54 @@ -286,7 +286,7 @@ __next(struct seq_file *m, loff_t *pos)
55 {
56 long n = *pos - 1;
57
58 - if (n > stack_trace_max.nr_entries || stack_dump_trace[n] == ULONG_MAX)
59 + if (n >= stack_trace_max.nr_entries || stack_dump_trace[n] == ULONG_MAX)
60 return NULL;
61
62 m->private = (void *)n;
63 --
64 2.19.1
65