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
6 From: Tom Zanussi <tom.zanussi@linux.intel.com>
8 commit 9f0bbf3115ca9f91f43b7c74e9ac7d79f47fc6c2 upstream.
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
15 So, in the case of a string key, use strncpy instead of memcpy to
16 avoid copying in the extra bytes.
18 Before, using the gdbus entries in the following hist trigger as an
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
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
39 # cat /sys/kernel/debug/tracing/events/sched/sched_waking/hist | egrep gdbus | wc -l
44 # cat /sys/kernel/debug/tracing/events/sched/sched_waking/hist | egrep gdbus | wc -l
47 Link: http://lkml.kernel.org/r/50c35ae1267d64eee975b8125e151e600071d4dc.1549309756.git.tom.zanussi@linux.intel.com
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>
57 kernel/trace/trace_events_hist.c | 5 +++--
58 1 file changed, 3 insertions(+), 2 deletions(-)
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;
68 - memcpy(compound_key + key_field->offset, key, size);
69 + strncpy(compound_key + key_field->offset, (char *)key, size);
71 + memcpy(compound_key + key_field->offset, key, size);