ring-buffer: Use kaslr address instead of text delta
authorSteven Rostedt <rostedt@goodmis.org>
Wed, 5 Mar 2025 16:45:40 +0000 (11:45 -0500)
committerSteven Rostedt (Google) <rostedt@goodmis.org>
Fri, 28 Mar 2025 12:39:26 +0000 (08:39 -0400)
Instead of saving off the text and data pointers and using them to compare
with the current boot's text and data pointers, just save off the KASLR
offset. Then that can be used to figure out how to read the previous boots
buffer.

The last_boot_info will now show this offset, but only if it is for a
previous boot:

  ~# cat instances/boot_mapped/last_boot_info
  39000000 [kernel]

  ~# echo function > instances/boot_mapped/current_tracer
  ~# cat instances/boot_mapped/last_boot_info
  # Current

If the KASLR offset saved is for the current boot, the last_boot_info will
show the value of "current".

Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Link: https://lore.kernel.org/20250305164608.274956504@goodmis.org
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
include/linux/ring_buffer.h
kernel/trace/ring_buffer.c
kernel/trace/trace.c
kernel/trace/trace.h

index 17fbb78552952d9bad163208fa8bd621b38c6a65..8de035f4f0d9a2ea506acdbf7ce6b2144c6f9199 100644 (file)
@@ -94,8 +94,7 @@ struct trace_buffer *__ring_buffer_alloc_range(unsigned long size, unsigned flag
                                               unsigned long range_size,
                                               struct lock_class_key *key);
 
-bool ring_buffer_last_boot_delta(struct trace_buffer *buffer, long *text,
-                                long *data);
+bool ring_buffer_last_boot_delta(struct trace_buffer *buffer, unsigned long *kaslr_addr);
 
 /*
  * Because the ring buffer is generic, if other users of the ring buffer get
index 510409f979923b5f9cfaab40301b1056fb300b2b..9aa2e818eced70b3ce9aebe201593c1d5855aa34 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <asm/local64.h>
 #include <asm/local.h>
+#include <asm/setup.h>
 
 #include "trace.h"
 
@@ -49,8 +50,7 @@ static void update_pages_handler(struct work_struct *work);
 struct ring_buffer_meta {
        int             magic;
        int             struct_size;
-       unsigned long   text_addr;
-       unsigned long   data_addr;
+       unsigned long   kaslr_addr;
        unsigned long   first_buffer;
        unsigned long   head_buffer;
        unsigned long   commit_buffer;
@@ -550,8 +550,7 @@ struct trace_buffer {
        unsigned long                   range_addr_start;
        unsigned long                   range_addr_end;
 
-       long                            last_text_delta;
-       long                            last_data_delta;
+       unsigned long                   kaslr_addr;
 
        unsigned int                    subbuf_size;
        unsigned int                    subbuf_order;
@@ -1891,16 +1890,13 @@ static void rb_meta_validate_events(struct ring_buffer_per_cpu *cpu_buffer)
        }
 }
 
-/* Used to calculate data delta */
-static char rb_data_ptr[] = "";
-
-#define THIS_TEXT_PTR          ((unsigned long)rb_meta_init_text_addr)
-#define THIS_DATA_PTR          ((unsigned long)rb_data_ptr)
-
 static void rb_meta_init_text_addr(struct ring_buffer_meta *meta)
 {
-       meta->text_addr = THIS_TEXT_PTR;
-       meta->data_addr = THIS_DATA_PTR;
+#ifdef CONFIG_RANDOMIZE_BASE
+       meta->kaslr_addr = kaslr_offset();
+#else
+       meta->kaslr_addr = 0;
+#endif
 }
 
 static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages)
@@ -1928,8 +1924,7 @@ static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages)
                        meta->first_buffer += delta;
                        meta->head_buffer += delta;
                        meta->commit_buffer += delta;
-                       buffer->last_text_delta = THIS_TEXT_PTR - meta->text_addr;
-                       buffer->last_data_delta = THIS_DATA_PTR - meta->data_addr;
+                       buffer->kaslr_addr = meta->kaslr_addr;
                        continue;
                }
 
@@ -2482,17 +2477,15 @@ struct trace_buffer *__ring_buffer_alloc_range(unsigned long size, unsigned flag
  *
  * Returns: The true if the delta is non zero
  */
-bool ring_buffer_last_boot_delta(struct trace_buffer *buffer, long *text,
-                                long *data)
+bool ring_buffer_last_boot_delta(struct trace_buffer *buffer, unsigned long *kaslr_addr)
 {
        if (!buffer)
                return false;
 
-       if (!buffer->last_text_delta)
+       if (!buffer->kaslr_addr)
                return false;
 
-       *text = buffer->last_text_delta;
-       *data = buffer->last_data_delta;
+       *kaslr_addr = buffer->kaslr_addr;
 
        return true;
 }
index 0e6d517e74e0fd19bfb31fcbcb977d57f894c78b..934658cda5706c0f1c609e805e7f0e43a5c4ad43 100644 (file)
@@ -50,7 +50,7 @@
 #include <linux/irq_work.h>
 #include <linux/workqueue.h>
 
-#include <asm/setup.h> /* COMMAND_LINE_SIZE */
+#include <asm/setup.h> /* COMMAND_LINE_SIZE and kaslr_offset() */
 
 #include "trace.h"
 #include "trace_output.h"
@@ -4193,7 +4193,7 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
                 * safe to use if the array has delta offsets
                 * Force printing via the fields.
                 */
-               if ((tr->text_delta || tr->data_delta) &&
+               if ((tr->text_delta) &&
                    event->type > __TRACE_LAST_TYPE)
                        return print_event_fields(iter, event);
 
@@ -5990,7 +5990,7 @@ ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
 
 static void update_last_data(struct trace_array *tr)
 {
-       if (!tr->text_delta && !tr->data_delta)
+       if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
                return;
 
        /*
@@ -6003,7 +6003,8 @@ static void update_last_data(struct trace_array *tr)
 
        /* Using current data now */
        tr->text_delta = 0;
-       tr->data_delta = 0;
+
+       tr->flags &= ~TRACE_ARRAY_FL_LAST_BOOT;
 }
 
 /**
@@ -6821,8 +6822,17 @@ tracing_last_boot_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t
 
        seq_buf_init(&seq, buf, 64);
 
-       seq_buf_printf(&seq, "text delta:\t%ld\n", tr->text_delta);
-       seq_buf_printf(&seq, "data delta:\t%ld\n", tr->data_delta);
+       /*
+        * Do not leak KASLR address. This only shows the KASLR address of
+        * the last boot. When the ring buffer is started, the LAST_BOOT
+        * flag gets cleared, and this should only report "current".
+        * Otherwise it shows the KASLR address from the previous boot which
+        * should not be the same as the current boot.
+        */
+       if (tr->flags & TRACE_ARRAY_FL_LAST_BOOT)
+               seq_buf_printf(&seq, "%lx\t[kernel]\n", tr->kaslr_addr);
+       else
+               seq_buf_puts(&seq, "# Current\n");
 
        return simple_read_from_buffer(ubuf, cnt, ppos, buf, seq_buf_used(&seq));
 }
@@ -9210,8 +9220,10 @@ allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size
                                                      tr->range_addr_start,
                                                      tr->range_addr_size);
 
-               ring_buffer_last_boot_delta(buf->buffer,
-                                           &tr->text_delta, &tr->data_delta);
+#ifdef CONFIG_RANDOMIZE_BASE
+               if (ring_buffer_last_boot_delta(buf->buffer, &tr->kaslr_addr))
+                       tr->text_delta = kaslr_offset() - tr->kaslr_addr;
+#endif
                /*
                 * This is basically the same as a mapped buffer,
                 * with the same restrictions.
@@ -10459,7 +10471,7 @@ __init static void enable_instances(void)
                 * to it.
                 */
                if (start) {
-                       tr->flags |= TRACE_ARRAY_FL_BOOT;
+                       tr->flags |= TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT;
                        tr->ref++;
                }
 
index 9c21ba45b7af6bdbe6dce2732e38c929e54834cc..abe8169c3e879c27c6c64b4effd0ca5afd7e88f3 100644 (file)
@@ -348,8 +348,8 @@ struct trace_array {
        unsigned int            mapped;
        unsigned long           range_addr_start;
        unsigned long           range_addr_size;
+       unsigned long           kaslr_addr;
        long                    text_delta;
-       long                    data_delta;
 
        struct trace_pid_list   __rcu *filtered_pids;
        struct trace_pid_list   __rcu *filtered_no_pids;
@@ -433,9 +433,10 @@ struct trace_array {
 };
 
 enum {
-       TRACE_ARRAY_FL_GLOBAL   = BIT(0),
-       TRACE_ARRAY_FL_BOOT     = BIT(1),
-       TRACE_ARRAY_FL_MOD_INIT = BIT(2),
+       TRACE_ARRAY_FL_GLOBAL           = BIT(0),
+       TRACE_ARRAY_FL_BOOT             = BIT(1),
+       TRACE_ARRAY_FL_LAST_BOOT        = BIT(2),
+       TRACE_ARRAY_FL_MOD_INIT         = BIT(3),
 };
 
 #ifdef CONFIG_MODULES