From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Andrew Morton <akpm@linux-foundation.org>,
Tze-nan Wu <Tze-nan.Wu@mediatek.com>
Subject: [for-linus][PATCH 11/15] ring-buffer: Do not attempt to read past "commit"
Date: Fri, 08 Sep 2023 23:16:26 -0400 [thread overview]
Message-ID: <20230909032350.261976507@goodmis.org> (raw)
In-Reply-To: <20230909031615.047488015@goodmis.org>
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
When iterating over the ring buffer while the ring buffer is active, the
writer can corrupt the reader. There's barriers to help detect this and
handle it, but that code missed the case where the last event was at the
very end of the page and has only 4 bytes left.
The checks to detect the corruption by the writer to reads needs to see the
length of the event. If the length in the first 4 bytes is zero then the
length is stored in the second 4 bytes. But if the writer is in the process
of updating that code, there's a small window where the length in the first
4 bytes could be zero even though the length is only 4 bytes. That will
cause rb_event_length() to read the next 4 bytes which could happen to be off the
allocated page.
To protect against this, fail immediately if the next event pointer is
less than 8 bytes from the end of the commit (last byte of data), as all
events must be a minimum of 8 bytes anyway.
Link: https://lore.kernel.org/all/20230905141245.26470-1-Tze-nan.Wu@mediatek.com/
Link: https://lore.kernel.org/linux-trace-kernel/20230907122820.0899019c@gandalf.local.home
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Reported-by: Tze-nan Wu <Tze-nan.Wu@mediatek.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
kernel/trace/ring_buffer.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 72ccf75defd0..a1651edc48d5 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -2390,6 +2390,11 @@ rb_iter_head_event(struct ring_buffer_iter *iter)
*/
commit = rb_page_commit(iter_head_page);
smp_rmb();
+
+ /* An event needs to be at least 8 bytes in size */
+ if (iter->head > commit - 8)
+ goto reset;
+
event = __rb_page_index(iter_head_page, iter->head);
length = rb_event_length(event);
--
2.40.1
next prev parent reply other threads:[~2023-09-09 3:24 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-09 3:16 [for-linus][PATCH 00/15] tracing: Fixes for 6.6 Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 01/15] tracefs: Add missing lockdown check to tracefs_create_dir() Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 02/15] tracefs/eventfs: Add missing lockdown checks Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 03/15] tracefs/eventfs: Use dput to free the toplevel events directory Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 04/15] tracing: Increase trace array ref count on enable and filter files Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 05/15] tracing: Have tracing_max_latency inc the trace array ref count Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 06/15] tracing: Have current_trace " Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 07/15] tracing: Have option files " Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 08/15] tracing: Have event inject " Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 09/15] ring-buffer: Avoid softlockup in ring_buffer_resize() Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 10/15] tracefs/eventfs: Free top level files on removal Steven Rostedt
2023-09-09 3:16 ` Steven Rostedt [this message]
2023-09-09 3:16 ` [for-linus][PATCH 12/15] tracing: Use the new eventfs descriptor for print trigger Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 13/15] tracing: Remove unused trace_event_file dir field Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 14/15] selftests/ftrace: Fix dependencies for some of the synthetic event tests Steven Rostedt
2023-09-09 3:16 ` [for-linus][PATCH 15/15] tracing/synthetic: Fix order of struct trace_dynamic_info 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=20230909032350.261976507@goodmis.org \
--to=rostedt@goodmis.org \
--cc=Tze-nan.Wu@mediatek.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@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
all inboxes | Powered by JetHome®