mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Ingo Molnar <mingo@elte.hu>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Marcin Slusarz <marcin.slusarz@gmail.com>
Subject: [PATCH][GIT PULL] tracing: Sanitize value returned from write(trace_marker, "...", len)
Date: Fri, 13 Aug 2010 16:55:07 -0400	[thread overview]
Message-ID: <1281732907.3058.77.camel@gandalf.stny.rr.com> (raw)


Ingo,

I had this sitting in my quilt queue, and did not realize I never
committed it. This is on top of my previous two pull requests.

Thanks,

-- Steve

Please pull the latest tip/perf/urgent-3 tree, which can be found at:

  git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-2.6-trace.git
tip/perf/urgent-3


Marcin Slusarz (1):
      tracing: Sanitize value returned from write(trace_marker, "...", len)

----
 kernel/trace/trace.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
---------------------------
commit 1aa54bca6ee0d07ebcafb8ca8074b624d80724aa
Author: Marcin Slusarz <marcin.slusarz@gmail.com>
Date:   Wed Jul 28 01:18:01 2010 +0200

    tracing: Sanitize value returned from write(trace_marker, "...", len)
    
    When userspace code writes non-new-line-terminated string to trace_marker
    file, write handler appends new-line and returns number of bytes written
    to trace buffer, so
    write(fd, "abc", 3) will return 4
    
    That's unexpected and unfortunately it confuses glibc's fprintf function.
    
    Example:
    int main() {
      fprintf(stderr, "abc");
      return 0;
    }
    
    $ gcc test.c -o test
    $ echo mmiotrace > /sys/kernel/debug/tracing/current_tracer
    $ ./test 2>/sys/kernel/debug/tracing/trace_marker
    
    results in infinite loop:
    write(fd, "abc", 3) = 4
    write(fd, "", 1) = 0
    write(fd, "", 1) = 0
    write(fd, "", 1) = 0
    write(fd, "", 1) = 0
    write(fd, "", 1) = 0
    write(fd, "", 1) = 0
    write(fd, "", 1) = 0
    (...)
    
    ...and kernel trace buffer full of empty markers.
    
    Fix it by sanitizing write return value.
    
    Signed-off-by: Marcin Slusarz <marcin.slusarz@gmail.com>
    LKML-Reference: <20100727231801.GB2826@joi.lan>
    Cc: Frederic Weisbecker <fweisbec@gmail.com>
    Cc: Ingo Molnar <mingo@redhat.com>
    Signed-off-by: Steven Rostedt <rostedt@goodmis.org>

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 086d363..88b42d1 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -3498,6 +3498,7 @@ tracing_mark_write(struct file *filp, const char __user *ubuf,
 					size_t cnt, loff_t *fpos)
 {
 	char *buf;
+	size_t written;
 
 	if (tracing_disabled)
 		return -EINVAL;
@@ -3519,11 +3520,15 @@ tracing_mark_write(struct file *filp, const char __user *ubuf,
 	} else
 		buf[cnt] = '\0';
 
-	cnt = mark_printk("%s", buf);
+	written = mark_printk("%s", buf);
 	kfree(buf);
-	*fpos += cnt;
+	*fpos += written;
 
-	return cnt;
+	/* don't tell userspace we wrote more - it might confuse them */
+	if (written > cnt)
+		written = cnt;
+
+	return written;
 }
 
 static int tracing_clock_show(struct seq_file *m, void *v)



             reply	other threads:[~2010-08-13 20:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-13 20:55 Steven Rostedt [this message]
2010-08-16 17:30 ` Ingo Molnar

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=1281732907.3058.77.camel@gandalf.stny.rr.com \
    --to=rostedt@goodmis.org \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcin.slusarz@gmail.com \
    --cc=mingo@elte.hu \
    /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

all inboxes | Powered by JetHome®