linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Linux Trace Devel <linux-trace-devel@vger.kernel.org>
Subject: [PATCH v2] tracefs events: Do not process str_read_file() if size is zero
Date: Mon, 23 Nov 2020 15:46:07 -0500	[thread overview]
Message-ID: <20201123154607.5e43e1ff@gandalf.local.home> (raw)

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

If a file has no size (nothing is read), then str_read_file() frees the
buffer and returns zero. The problem is that all callers of str_read_file()
uses the buffer supplied if the value returned is not a negative. This
causes the freed buffer being used by the callers if the file read existed
but had no content.

This is apparent when using a copy of the tracefs directory, where some file
exist, but have no content, then loading the events would cause a segfault.

Change the callers to check the return value of str_read_file() for zero or
negative, and do not go further if it is.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
Changes since v1:

   Have the callers of str_read_file() check for less than zero, as
   the first patch would never allocate the passed in buffer.

 tracefs-events.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tracefs-events.c b/tracefs-events.c
index ca1d22b..631c310 100644
--- a/tracefs-events.c
+++ b/tracefs-events.c
@@ -411,7 +411,7 @@ char **tracefs_tracers(const char *tracing_dir)
 		goto out_free;
 
 	len = str_read_file(available_tracers, &buf);
-	if (len < 0)
+	if (len <= 0)
 		goto out_free;
 
 	len = 0;
@@ -471,7 +471,7 @@ static int load_events(struct tep_handle *tep,
 			goto next_event;
 
 		len = str_read_file(format, &buf);
-		if (len < 0)
+		if (len <= 0)
 			goto next_event;
 
 		ret = tep_parse_event(tep, buf, len, system);
@@ -501,7 +501,7 @@ static int read_header(struct tep_handle *tep, const char *tracing_dir)
 		goto out;
 
 	len = str_read_file(header, &buf);
-	if (len < 0)
+	if (len <= 0)
 		goto out;
 
 	tep_parse_header_page(tep, buf, len, sizeof(long));
-- 
2.25.4


             reply	other threads:[~2020-11-23 20:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-23 20:46 Steven Rostedt [this message]
2020-11-24 16:20 ` [PATCH v2] tracefs events: Do not process str_read_file() if size is zero Tzvetomir Stoyanov

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=20201123154607.5e43e1ff@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.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;
as well as URLs for NNTP newsgroup(s).