linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	stable@vger.kernel.org
Subject: [PATCH 1/3] ring-buffer: Add uname to match criteria for persistent ring buffer
Date: Tue, 17 Dec 2024 12:32:38 -0500	[thread overview]
Message-ID: <20241217173520.314190793@goodmis.org> (raw)
In-Reply-To: 20241217173237.836878448@goodmis.org

From: Steven Rostedt <rostedt@goodmis.org>

The persistent ring buffer can live across boots. It is expected that the
content in the buffer can be translated to the current kernel with delta
offsets even with KASLR enabled. But it can only guarantee this if the
content of the ring buffer came from the same kernel as the one that is
currently running.

Add uname into the meta data and if the uname in the meta data from the
previous boot does not match the uname of the current boot, then clear the
buffer and re-initialize it.

This only handles the case of kernel versions. It does not clear the
buffer for development. There's several mechanisms to keep bad data from
crashing the kernel. The worse that can happen is some corrupt data may be
displayed.

Cc: stable@vger.kernel.org
Fixes: 8f3e6659656e6 ("ring-buffer: Save text and data locations in mapped meta data")
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 kernel/trace/ring_buffer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 7e257e855dd1..3c94c59d000c 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -17,6 +17,7 @@
 #include <linux/uaccess.h>
 #include <linux/hardirq.h>
 #include <linux/kthread.h>	/* for self test */
+#include <linux/utsname.h>
 #include <linux/module.h>
 #include <linux/percpu.h>
 #include <linux/mutex.h>
@@ -45,10 +46,13 @@
 static void update_pages_handler(struct work_struct *work);
 
 #define RING_BUFFER_META_MAGIC	0xBADFEED
+#define UNAME_SZ 64
 
 struct ring_buffer_meta {
 	int		magic;
 	int		struct_size;
+	char		uname[UNAME_SZ];
+
 	unsigned long	text_addr;
 	unsigned long	data_addr;
 	unsigned long	first_buffer;
@@ -1687,6 +1691,11 @@ static bool rb_meta_valid(struct ring_buffer_meta *meta, int cpu,
 		return false;
 	}
 
+	if (strncmp(init_utsname()->release, meta->uname, UNAME_SZ - 1)) {
+		pr_info("Ring buffer boot meta[%d] mismatch of uname\n", cpu);
+		return false;
+	}
+
 	/* The subbuffer's size and number of subbuffers must match */
 	if (meta->subbuf_size != subbuf_size ||
 	    meta->nr_subbufs != nr_pages + 1) {
@@ -1920,6 +1929,7 @@ static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages)
 
 		meta->magic = RING_BUFFER_META_MAGIC;
 		meta->struct_size = sizeof(*meta);
+		strscpy(meta->uname, init_utsname()->release, UNAME_SZ);
 
 		meta->nr_subbufs = nr_pages + 1;
 		meta->subbuf_size = PAGE_SIZE;
-- 
2.45.2



  reply	other threads:[~2024-12-17 17:34 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-17 17:32 [PATCH 0/3] ring-buffer: Hardening of the persistent buffer Steven Rostedt
2024-12-17 17:32 ` Steven Rostedt [this message]
2024-12-17 17:46   ` [PATCH 1/3] ring-buffer: Add uname to match criteria for persistent ring buffer Linus Torvalds
2024-12-17 18:04     ` Steven Rostedt
2024-12-17 18:19       ` Linus Torvalds
2024-12-17 18:24         ` Linus Torvalds
2024-12-17 18:42           ` Steven Rostedt
2024-12-17 19:22           ` Mathieu Desnoyers
2024-12-17 18:33         ` Steven Rostedt
2024-12-17 18:42           ` Linus Torvalds
2024-12-17 19:01             ` Steven Rostedt
2024-12-17 19:38               ` Linus Torvalds
2024-12-17 19:44                 ` Steven Rostedt
2024-12-17 22:24                   ` Linus Torvalds
2024-12-17 22:53                     ` Steven Rostedt
2024-12-17 23:32                       ` Linus Torvalds
2024-12-18  0:02                         ` Linus Torvalds
2024-12-18  0:48                           ` Steven Rostedt
2024-12-18  0:47                         ` Alexei Starovoitov
2024-12-18  1:26                           ` Linus Torvalds
2024-12-18  1:39                             ` Linus Torvalds
2024-12-18  1:53                               ` Alexei Starovoitov
2024-12-17 19:03             ` Linus Torvalds
2024-12-17 19:07               ` Steven Rostedt
2024-12-17 19:14                 ` Steven Rostedt
2024-12-17 18:17     ` Steven Rostedt
2024-12-17 17:32 ` [PATCH 2/3] trace/ring-buffer: Do not create module or dynamic events in boot mapped buffers Steven Rostedt
2024-12-17 17:32 ` [PATCH 3/3] trace/ring-buffer: Do not use TP_printk() formatting for " 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=20241217173520.314190793@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.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).