]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-4.19/tracing-use-strncpy-instead-of-memcpy-for-string-keys-in-hist-triggers.patch
7d2ebaf58b2c4e789bb2b5b177ce2575dec3df70
[thirdparty/kernel/stable-queue.git] / queue-4.19 / tracing-use-strncpy-instead-of-memcpy-for-string-keys-in-hist-triggers.patch
1 From 9f0bbf3115ca9f91f43b7c74e9ac7d79f47fc6c2 Mon Sep 17 00:00:00 2001
2 From: Tom Zanussi <tom.zanussi@linux.intel.com>
3 Date: Mon, 4 Feb 2019 15:07:24 -0600
4 Subject: tracing: Use strncpy instead of memcpy for string keys in hist triggers
5
6 From: Tom Zanussi <tom.zanussi@linux.intel.com>
7
8 commit 9f0bbf3115ca9f91f43b7c74e9ac7d79f47fc6c2 upstream.
9
10 Because there may be random garbage beyond a string's null terminator,
11 it's not correct to copy the the complete character array for use as a
12 hist trigger key. This results in multiple histogram entries for the
13 'same' string key.
14
15 So, in the case of a string key, use strncpy instead of memcpy to
16 avoid copying in the extra bytes.
17
18 Before, using the gdbus entries in the following hist trigger as an
19 example:
20
21 # echo 'hist:key=comm' > /sys/kernel/debug/tracing/events/sched/sched_waking/trigger
22 # cat /sys/kernel/debug/tracing/events/sched/sched_waking/hist
23
24 ...
25
26 { comm: ImgDecoder #4 } hitcount: 203
27 { comm: gmain } hitcount: 213
28 { comm: gmain } hitcount: 216
29 { comm: StreamTrans #73 } hitcount: 221
30 { comm: mozStorage #3 } hitcount: 230
31 { comm: gdbus } hitcount: 233
32 { comm: StyleThread#5 } hitcount: 253
33 { comm: gdbus } hitcount: 256
34 { comm: gdbus } hitcount: 260
35 { comm: StyleThread#4 } hitcount: 271
36
37 ...
38
39 # cat /sys/kernel/debug/tracing/events/sched/sched_waking/hist | egrep gdbus | wc -l
40 51
41
42 After:
43
44 # cat /sys/kernel/debug/tracing/events/sched/sched_waking/hist | egrep gdbus | wc -l
45 1
46
47 Link: http://lkml.kernel.org/r/50c35ae1267d64eee975b8125e151e600071d4dc.1549309756.git.tom.zanussi@linux.intel.com
48
49 Cc: Namhyung Kim <namhyung@kernel.org>
50 Cc: stable@vger.kernel.org
51 Fixes: 79e577cbce4c4 ("tracing: Support string type key properly")
52 Signed-off-by: Tom Zanussi <tom.zanussi@linux.intel.com>
53 Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
54 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
55
56 ---
57 kernel/trace/trace_events_hist.c | 5 +++--
58 1 file changed, 3 insertions(+), 2 deletions(-)
59
60 --- a/kernel/trace/trace_events_hist.c
61 +++ b/kernel/trace/trace_events_hist.c
62 @@ -4621,9 +4621,10 @@ static inline void add_to_key(char *comp
63 /* ensure NULL-termination */
64 if (size > key_field->size - 1)
65 size = key_field->size - 1;
66 - }
67
68 - memcpy(compound_key + key_field->offset, key, size);
69 + strncpy(compound_key + key_field->offset, (char *)key, size);
70 + } else
71 + memcpy(compound_key + key_field->offset, key, size);
72 }
73
74 static void