public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [for-next][PATCH 10/21] tracing: Make trace_seq_putmem_hex() more robust
Date: Thu, 03 Jul 2014 12:05:13 -0400	[thread overview]
Message-ID: <20140703161225.192042202@goodmis.org> (raw)
In-Reply-To: 20140703160503.006976702@goodmis.org

[-- Attachment #1: 0010-tracing-Make-trace_seq_putmem_hex-more-robust.patch --]
[-- Type: text/plain, Size: 2944 bytes --]

From: "Steven Rostedt (Red Hat)" <rostedt@goodmis.org>

Currently trace_seq_putmem_hex() can only take as a parameter a pointer
to something that is 8 bytes or less, otherwise it will overflow the
buffer. This is protected by a macro that encompasses the call to
trace_seq_putmem_hex() that has a BUILD_BUG_ON() for the variable before
it is passed in. This is not very robust and if trace_seq_putmem_hex() ever
gets used outside that macro it will cause issues.

Instead of only being able to produce a hex output of memory that is for
a single word, change it to be more robust and allow any size input.

Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 include/linux/trace_seq.h   |  2 --
 kernel/trace/trace_output.h |  1 -
 kernel/trace/trace_seq.c    | 26 +++++++++++++++++++-------
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/include/linux/trace_seq.h b/include/linux/trace_seq.h
index 1f05317f51c4..8283762ab7ef 100644
--- a/include/linux/trace_seq.h
+++ b/include/linux/trace_seq.h
@@ -25,8 +25,6 @@ trace_seq_init(struct trace_seq *s)
 	s->full = 0;
 }
 
-#define MAX_MEMHEX_BYTES	8
-
 /*
  * Currently only defined when tracing is enabled.
  */
diff --git a/kernel/trace/trace_output.h b/kernel/trace/trace_output.h
index bf7daf2237ed..80b25b585a70 100644
--- a/kernel/trace/trace_output.h
+++ b/kernel/trace/trace_output.h
@@ -43,7 +43,6 @@ do {							\
 
 #define SEQ_PUT_HEX_FIELD_RET(s, x)			\
 do {							\
-	BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES);	\
 	if (!trace_seq_putmem_hex(s, &(x), sizeof(x)))	\
 		return TRACE_TYPE_PARTIAL_LINE;		\
 } while (0)
diff --git a/kernel/trace/trace_seq.c b/kernel/trace/trace_seq.c
index 0fabca773e51..88c0f80f0a1f 100644
--- a/kernel/trace/trace_seq.c
+++ b/kernel/trace/trace_seq.c
@@ -291,6 +291,7 @@ int trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len)
 }
 EXPORT_SYMBOL_GPL(trace_seq_putmem);
 
+#define MAX_MEMHEX_BYTES	8U
 #define HEX_CHARS		(MAX_MEMHEX_BYTES*2 + 1)
 
 /**
@@ -310,22 +311,33 @@ int trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
 {
 	unsigned char hex[HEX_CHARS];
 	const unsigned char *data = mem;
+	unsigned int start_len;
 	int i, j;
+	int cnt = 0;
 
 	if (s->full)
 		return 0;
 
+	while (len) {
+		start_len = min(len, HEX_CHARS - 1);
 #ifdef __BIG_ENDIAN
-	for (i = 0, j = 0; i < len; i++) {
+		for (i = 0, j = 0; i < start_len; i++) {
 #else
-	for (i = len-1, j = 0; i >= 0; i--) {
+		for (i = start_len-1, j = 0; i >= 0; i--) {
 #endif
-		hex[j++] = hex_asc_hi(data[i]);
-		hex[j++] = hex_asc_lo(data[i]);
-	}
-	hex[j++] = ' ';
+			hex[j++] = hex_asc_hi(data[i]);
+			hex[j++] = hex_asc_lo(data[i]);
+		}
+		if (WARN_ON_ONCE(j == 0 || j/2 > len))
+			break;
+
+		/* j increments twice per loop */
+		len -= j / 2;
+		hex[j++] = ' ';
 
-	return trace_seq_putmem(s, hex, j);
+		cnt += trace_seq_putmem(s, hex, j);
+	}
+	return cnt;
 }
 EXPORT_SYMBOL_GPL(trace_seq_putmem_hex);
 
-- 
2.0.0



  parent reply	other threads:[~2014-07-03 16:18 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-03 16:05 [for-next][PATCH 00/21] tracing: Updates for 3.17 Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 01/21] ftrace: Allow no regs if no more callbacks require it Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 02/21] ftrace: Use macros for numbers in ftrace rec shift bits Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 03/21] ftrace: Add ftrace_rec_counter() macro to simplify the code Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 04/21] ftrace: Optimize function graph to be called directly Steven Rostedt
2014-07-10 17:45   ` Tuomas Tynkkynen
2014-07-10 18:01     ` Steven Rostedt
2014-07-12  3:36     ` Steven Rostedt
2014-07-12  3:37       ` Steven Rostedt
2014-07-14 13:46         ` Tuomas Tynkkynen
2014-07-14 16:14           ` Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 05/21] ftrace: Add trampolines to enabled_functions debug file Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 06/21] tracing: Change trace event sample to use strlcpy instead of strncpy Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 07/21] ftrace: Simplify ftrace_hash_disable/enable path in ftrace_hash_move Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 08/21] tracing: Move the trace_seq_* functions into its own trace_seq.c file Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 09/21] tracing: Clean up trace_seq.c Steven Rostedt
2014-07-03 16:05 ` Steven Rostedt [this message]
2014-07-03 16:05 ` [for-next][PATCH 11/21] tracing: Remove trace_seq_reserve() Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 12/21] tracing: Remove unnecessary null test before debugfs_remove() Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 13/21] tracing: Add trace_seq_buffer_ptr() helper function Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 14/21] ftrace: Get rid of obsolete global_start_up variable Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 15/21] ftrace: Fix memory leak on failure path in ftrace_allocate_pages() Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 16/21] ftrace: Do not copy hash if O_TRUNC is set Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 17/21] tracing: Convert pr_warning() to pr_warn() in trace_events.c Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 18/21] tracing: Add ftrace_graph_notrace boot parameter Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 19/21] tracing: Improve message of empty set_graph_notrace file Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 20/21] tracing: Improve message of empty set_ftrace_notrace file Steven Rostedt
2014-07-03 16:05 ` [for-next][PATCH 21/21] tracing: Add description of set_graph_notrace to tracing/README Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140703161225.192042202@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox