Skip to contentSkip to conversation

ring-buffer: Fixes for subbuf resizing and persistent buffers

PATCH Superseded Vincent Donnefort 3 participants 15 messages Aug 26 – Aug 28

Subsystems (2)

v8 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Vincent Donnefort

author maintainer
#0 lore raw

v8 cover ring-buffer: Fixes for subbuf resizing and persistent buffers

This series addresses multiple issues discovered with the dynamic ring buffer resizing.

I have managed to reproduce a ring_buffer_read_page() race with

 $ while true; do for i in 8 16 32; do echo $i > /sys/kernel/tracing/buffer_subbuf_size_kb; sleep 0.1; done; done

Changelog:

v8:

- ring_buffer_read_page() handles gracefully read_page/reader_page order mismatch (Steven)

v7 (https://lore.kernel.org/linux-trace-kernel/[email protected]/):

- Match the "static" rb limit with bpage::id bitwidth
- Cover another 32-bit truncation in rb_range_buffer (Sashiko)
- Fix uninitialized spare_size (Sashiko)

v6 (https://lore.kernel.org/all/[email protected]/):

- New prototype for ring_buffer_alloc_read_page() (Steven)
- ring_buffer_read_page() to return -EAGAIN (Steven)
- Keep nr_pages "unsigned long" (Steven)
- Repase on ring-buffer/next (Drop most of the patches)

v5 (https://lore.kernel.org/all/[email protected]/):

- Reset info->spare_read only when data is in the ring-buffer (Sashiko)
- Use unsigned long for subbuf_size declaration to avoid 32-bit truncation. (Sashiko)
- Make cpu_buffer::free_page a buffer_read_data_page
- Update kerneldoc for ring_buffer_alloc_read_page()

v4 (https://lore.kernel.org/all/[email protected]/):

- Add rb_subbuf_start() helper (Steven)
- kerneldoc additions
- Fix races in trace_pipe_raw readers
- Use rb_subbuf_capacity() in ring_buffer_subbuf_order_set()
- use rb_page_capacity() in ring_buffer_map_get_reader (Sashiko)
- Fix 32-bit overflow in ring_buffer_subbuf_order_set() (Sashiko)
- Hold cpu_buffer::lock when modifying cpu_buffer->free_page in ring_buffer_subbuf_order_set (Sashiko)

v3 (https://lore.kernel.org/all/[email protected]/):

- Drop first 3 patches (Rebased on 7.2-rc7)
- Add a patch to align "nr_pages" to unsigned int
- Add a patch to remove useless trace_buffer::cpus
- Add unsigned long cast for rb_subbuf_size()
- subbuf_order fix for rb_free_cpu_buffer() (Sashiko)
- Use __always_inline just like the other accessors for the hot-path.

v2 (https://lore.kernel.org/all/[email protected]/):

- Prevent resizing of the persistent ring buffer
- Add missing bpage::order init
- Rework subbuf_size/subbuf_order (Sashiko)
- Remove ring_buffer_per_cpu::mapped
- Dynamically calculate trace_buffer::max_data_size

v1 (https://lore.kernel.org/all/[email protected]/)

Vincent Donnefort (3):
tracing: Fix subbuf resize races with trace_pipe_raw readers
ring-buffer: Cap static ring buffer nr_pages
ring-buffer: Prevent truncation of nr_pages / nr_subbufs

 include/linux/ring_buffer.h          |   5 +-
 kernel/trace/ring_buffer.c           | 198 ++++++++++++++++++---------
 kernel/trace/ring_buffer_benchmark.c |   6 +-
 kernel/trace/trace.c                 |  94 ++++++-------
 kernel/trace/trace.h                 |   9 +-
 5 files changed, 185 insertions(+), 127 deletions(-)

base-commit: 66498c75b4f8017f62d720d9b59675bdf3abce91

Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
author maintainer
#1 lore raw

PATCH v8 1/3 tracing: Fix subbuf resize races with trace_pipe_raw readers

Concurrent subbuffer resizes may crash trace_pipe_raw readers or leak uninitialized memory to userspace due to stale size values.

Modify ring_buffer_alloc_read_page() to handle the resizing of an existing buffer_data_read_page if necessary and add a new ring_buffer_read_page_size(). This new function enables ring-buffer buffer_data_read_page users to not call the racy ring_buffer_subbuf_size_get(). This makes the spare_size member of ftrace_buffer_info redundant.

Finally, handle buffer_data_read_page/reader_page order discrepancy in ring_buffer_read_page(). On a mismatch simply copy manually the data to the buffer_data_read_page.

Fixes bce761d75745 ("ring-buffer: Read and write to ring buffers with custom sub buffer size")
include/linux/ring_buffer.h +3 -2
index 0670742b2d60..afc7daa6ee7d 100644
@@ -218,14 +218,15 @@ bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer);
 size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu);
 
 struct buffer_data_read_page;
-struct buffer_data_read_page *
-ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu);
+int ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu,
+				struct buffer_data_read_page **rpage);
 void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu,
 				struct buffer_data_read_page *page);
 int ring_buffer_read_page(struct trace_buffer *buffer,
 			  struct buffer_data_read_page *data_page,
 			  size_t len, int cpu, int full);
 void *ring_buffer_read_page_data(struct buffer_data_read_page *page);
+unsigned int ring_buffer_read_page_size(struct buffer_data_read_page *rpage);
 
 struct trace_seq;
 
kernel/trace/ring_buffer.c +82 -34

225 lines not on this page · raw message

kernel/trace/ring_buffer_benchmark.c +3 -3
index 593e3b59e42e..c3d34c0e64e2 100644
@@ -104,7 +104,7 @@ static enum event_status read_event(int cpu)
 
 static enum event_status read_page(int cpu)
 {
-	struct buffer_data_read_page *bpage;
+	struct buffer_data_read_page *bpage = NULL;
 	struct ring_buffer_event *event;
 	struct rb_page *rpage;
 	unsigned long commit;
@@ -114,8 +114,8 @@ static enum event_status read_page(int cpu)
 	int inc;
 	int i;
 
-	bpage = ring_buffer_alloc_read_page(buffer, cpu);
-	if (IS_ERR(bpage))
+	ret = ring_buffer_alloc_read_page(buffer, cpu, &bpage);
+	if (ret < 0)
 		return EVENT_DROPPED;
 
 	page_size = ring_buffer_subbuf_size_get(buffer);
kernel/trace/trace.c +42 -52

172 lines not on this page · raw message

kernel/trace/trace.h +4 -5
index 74a7a50d1e78..203d098ee14e 100644
@@ -745,11 +745,10 @@ static inline int tracing_get_cpu(struct inode *inode)
 void tracing_reset_cpu(struct array_buffer *buf, int cpu);
 
 struct ftrace_buffer_info {
-	struct trace_iterator	iter;
-	void			*spare;
-	unsigned int		spare_cpu;
-	unsigned int		spare_size;
-	unsigned int		read;
+	struct trace_iterator		iter;
+	struct buffer_data_read_page	*spare;
+	unsigned int			spare_cpu;
+	unsigned int			read;
 };
 
 /**
Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
#2 lore raw

Re: [PATCH v8 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] ring_buffer_read_page() incorrectly returns -1 for splice_read requests on subbuf order mismatch --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ring_buffer_read_page() incorrectly returns -1 for splice_read requests on subbuf order mismatch --

commit c4d6ee6f55f23c8ec6da6f812ec3b722b2b7f6b6
Author: Vincent Donnefort <[email protected]>

tracing: Fix subbuf resize races with trace_pipe_raw readers

This commit modifies ring_buffer_alloc_read_page() and ring_buffer_read_page() to handle dynamic resizing of trace ring buffers, allowing readers to avoid stale size values. It introduces a manual data copy fallback in ring_buffer_read_page() when the subbuffer orders do not match.

Quoted 3 lines
kernel/trace/ring_buffer.c +0 -0

[ ... ]

kernel/trace/ring_buffer.c int ring_buffer_read_page(struct trace_buffer *buffer, · line 7225

Quoted 10 lines
kernel/trace/ring_buffer.c +2 -1
@@ -7183,7 +7218,8 @@ int ring_buffer_read_page(struct trace_buffer *buffer,
 	 */
 	if (read || (len < (size - read)) ||
 	    cpu_buffer->reader_page == cpu_buffer->commit_page ||
-	    rb_is_static(cpu_buffer)) {
+	    rb_is_static(cpu_buffer) ||
+	    data_page->order != reader->order) {
 		struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
 		unsigned int rpos = read;
 		unsigned int pos = 0;

[Severity: High]
Does this new condition break splice reads when the buffer order changes?

When tracing_buffers_splice_read() allocates a read page with the new order and calls ring_buffer_read_page() with full=1, it will now enter this manual copy block because the subbuffer orders no longer match.

However, just a few lines down inside this block in ring_buffer_read_page(), there is the following check:

        if (full &&
            (!read || (len < (size - read)) ||
             cpu_buffer->reader_page == cpu_buffer->commit_page))
                return -1;

Since the reader page typically hasn't been partially read yet (read == 0), and full == 1 from splice, this check evaluates to true and immediately returns -1 instead of copying the data.

Because tracing_buffers_splice_read() loops and ultimately returns -EAGAIN on failure, will this cause an infinite loop of -EAGAIN for userspace tools trying to read across buffer resizes?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#3 lore raw

Re: [PATCH v8 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers

Quoted 30 lines · On Wed, 26 Aug 2026 09:59:46 +0000 [email protected] wrote:
Quoted 3 lines
kernel/trace/ring_buffer.c +0 -0

[ ... ]

kernel/trace/ring_buffer.c int ring_buffer_read_page(struct trace_buffer *buffer, · line 7225

Quoted 10 lines
kernel/trace/ring_buffer.c +2 -1
@@ -7183,7 +7218,8 @@ int ring_buffer_read_page(struct trace_buffer *buffer,
 	 */
 	if (read || (len < (size - read)) ||
 	    cpu_buffer->reader_page == cpu_buffer->commit_page ||
-	    rb_is_static(cpu_buffer)) {
+	    rb_is_static(cpu_buffer) ||
+	    data_page->order != reader->order) {
 		struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
 		unsigned int rpos = read;
 		unsigned int pos = 0;  

[Severity: High]
Does this new condition break splice reads when the buffer order changes?

When tracing_buffers_splice_read() allocates a read page with the new order and calls ring_buffer_read_page() with full=1, it will now enter this manual copy block because the subbuffer orders no longer match.

However, just a few lines down inside this block in ring_buffer_read_page(), there is the following check:

        if (full &&
            (!read || (len < (size - read)) ||
             cpu_buffer->reader_page == cpu_buffer->commit_page))
                return -1;
Quoted 8 lines

Since the reader page typically hasn't been partially read yet (read == 0), and full == 1 from splice, this check evaluates to true and immediately returns -1 instead of copying the data.

Because tracing_buffers_splice_read() loops and ultimately returns -EAGAIN on failure, will this cause an infinite loop of -EAGAIN for userspace tools trying to read across buffer resizes?

This is a legitimate concern. But with an easy solution. Perhaps just add:

	if (full &&
	    (!read || (len < (size - read)) ||
-	     cpu_buffer->reader_page == cpu_buffer->commit_page))
+	     cpu_buffer->reader_page == cpu_buffer->commit_page) &&
+	    data_page->order == buffer->subbuf_order)
		return -1;

If the user is changing the buffer size at the same time as reading raw pages, they get what they deserve! I just don't want to let the kernel go into an infinite loop.

-- Steve

author maintainer
#4 lore raw

Re: [PATCH v8 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers

Quoted 52 lines · Steven Rostedt
Quoted 30 lines · On Wed, 26 Aug 2026 09:59:46 +0000 [email protected] wrote:
Quoted 3 lines
kernel/trace/ring_buffer.c +0 -0

[ ... ]

kernel/trace/ring_buffer.c int ring_buffer_read_page(struct trace_buffer *buffer, · line 7225

Quoted 10 lines
kernel/trace/ring_buffer.c +2 -1
@@ -7183,7 +7218,8 @@ int ring_buffer_read_page(struct trace_buffer *buffer,
 	 */
 	if (read || (len < (size - read)) ||
 	    cpu_buffer->reader_page == cpu_buffer->commit_page ||
-	    rb_is_static(cpu_buffer)) {
+	    rb_is_static(cpu_buffer) ||
+	    data_page->order != reader->order) {
 		struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
 		unsigned int rpos = read;
 		unsigned int pos = 0;  

[Severity: High]
Does this new condition break splice reads when the buffer order changes?

When tracing_buffers_splice_read() allocates a read page with the new order and calls ring_buffer_read_page() with full=1, it will now enter this manual copy block because the subbuffer orders no longer match.

However, just a few lines down inside this block in ring_buffer_read_page(), there is the following check:

        if (full &&
            (!read || (len < (size - read)) ||
             cpu_buffer->reader_page == cpu_buffer->commit_page))
                return -1;
Quoted 8 lines

Since the reader page typically hasn't been partially read yet (read == 0), and full == 1 from splice, this check evaluates to true and immediately returns -1 instead of copying the data.

Because tracing_buffers_splice_read() loops and ultimately returns -EAGAIN on failure, will this cause an infinite loop of -EAGAIN for userspace tools trying to read across buffer resizes?

This is a legitimate concern. But with an easy solution. Perhaps just add:

	if (full &&
	    (!read || (len < (size - read)) ||
(quoted diff) +2 -1
-	     cpu_buffer->reader_page == cpu_buffer->commit_page))
+	     cpu_buffer->reader_page == cpu_buffer->commit_page) &&
+	    data_page->order == buffer->subbuf_order)
		return -1;
Quoted 6 lines

If the user is changing the buffer size at the same time as reading raw pages, they get what they deserve! I just don't want to let the kernel go into an infinite loop.

-- Steve

Sounds good. Do you prefer to get a v9 or you fold this into the existing commit?

Signature · 1 line
Vincent
maintainer
#5 lore raw

Re: [PATCH v8 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers

Quoted 9 lines · On Wed, 26 Aug 2026 17:24:35 +0100 Vincent Donnefort <[email protected]>…
Quoted 9 lines

This is a legitimate concern. But with an easy solution. Perhaps just add:

	if (full &&
	    (!read || (len < (size - read)) ||
(quoted diff) +2 -1
-	     cpu_buffer->reader_page == cpu_buffer->commit_page))
+	     cpu_buffer->reader_page == cpu_buffer->commit_page) &&
+	    data_page->order == buffer->subbuf_order)
		return -1;
Quoted 9 lines
Quoted 6 lines

If the user is changing the buffer size at the same time as reading raw pages, they get what they deserve! I just don't want to let the kernel go into an infinite loop.

-- Steve

Sounds good. Do you prefer to get a v9 or you fold this into the existing commit?

Please send a v9. It makes the patchwork flow much better. If I modify a patch, it makes patchwork not be able to automate the status updates as it moves around git trees.

Thanks,

-- Steve

author maintainer
#6 lore raw

Re: [PATCH v8 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers

Quoted 22 lines · Steven Rostedt
Quoted 9 lines · On Wed, 26 Aug 2026 17:24:35 +0100 Vincent Donnefort <[email protected]>…
Quoted 9 lines

This is a legitimate concern. But with an easy solution. Perhaps just add:

	if (full &&
	    (!read || (len < (size - read)) ||
(quoted diff) +2 -1
-	     cpu_buffer->reader_page == cpu_buffer->commit_page))
+	     cpu_buffer->reader_page == cpu_buffer->commit_page) &&
+	    data_page->order == buffer->subbuf_order)
		return -1;
Quoted 9 lines
Quoted 6 lines

If the user is changing the buffer size at the same time as reading raw pages, they get what they deserve! I just don't want to let the kernel go into an infinite loop.

-- Steve

Sounds good. Do you prefer to get a v9 or you fold this into the existing commit?

Quoted 8 lines

Please send a v9. It makes the patchwork flow much better. If I modify a patch, it makes patchwork not be able to automate the status updates as it moves around git trees.

Thanks,

-- Steve

Looking at the function again, shouldn't we add !rb_is_static(cpu_buffer) as well to the full page test?

Signature · 1 line
Vincent
maintainer
#7 lore raw

Re: [PATCH v8 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers

Quoted 2 lines · On Thu, 27 Aug 2026 07:31:23 +0100 Vincent Donnefort <[email protected]>…

Looking at the function again, shouldn't we add !rb_is_static(cpu_buffer) as well to the full page test?

Why?

-- Steve

author maintainer
#8 lore raw

Re: [PATCH v8 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers

Quoted 9 lines · Steven Rostedt
Quoted 2 lines · On Thu, 27 Aug 2026 07:31:23 +0100 Vincent Donnefort <[email protected]>…

Looking at the function again, shouldn't we add !rb_is_static(cpu_buffer) as well to the full page test?

Why?

-- Steve

So that the splice can make progress when the ring-buffer is presistent or user-mapped. Or do we say that's expected behaviour?

Signature · 1 line
Vincent
maintainer
#9 lore raw

Re: [PATCH v8 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers

Quoted 7 lines · On Thu, 27 Aug 2026 17:21:39 +0100 Vincent Donnefort <[email protected]>…
Quoted 6 lines · Steven Rostedt
Quoted 2 lines · On Thu, 27 Aug 2026 07:31:23 +0100 Vincent Donnefort <[email protected]>…

Looking at the function again, shouldn't we add !rb_is_static(cpu_buffer) as well to the full page test?

Quoted 7 lines
Quoted 3 lines

Why?

-- Steve

So that the splice can make progress when the ring-buffer is presistent or user-mapped. Or do we say that's expected behaviour?

Ah yeah, I see. Hmm, looking at the code to see how this would work, I think I found another bug. Let me look deeper into it.

-- Steve

author maintainer
#10 lore raw

Re: [PATCH v8 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers

Quoted 14 lines · Steven Rostedt
Quoted 11 lines · On Thu, 27 Aug 2026 17:21:39 +0100 Vincent Donnefort <[email protected]>…
Quoted 9 lines · Steven Rostedt
Quoted 2 lines · On Thu, 27 Aug 2026 07:31:23 +0100 Vincent Donnefort <[email protected]>…

Looking at the function again, shouldn't we add !rb_is_static(cpu_buffer) as well to the full page test?

Why?

-- Steve

Quoted 8 lines
Quoted 3 lines

So that the splice can make progress when the ring-buffer is presistent or user-mapped. Or do we say that's expected behaviour?

Ah yeah, I see. Hmm, looking at the code to see how this would work, I think I found another bug. Let me look deeper into it.

-- Steve

A bug if we allow rb_is_static()?

If not, shall I add it to my series?

Signature · 1 line
Vincent
maintainer
#11 lore raw

Re: [PATCH v8 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers

Quoted 19 lines · Vincent Donnefort
Quoted 18 lines · Steven Rostedt
Quoted 14 lines · On Thu, 27 Aug 2026 17:21:39 +0100 Vincent Donnefort <[email protected]>…
Quoted 9 lines · Steven Rostedt
Quoted 2 lines · On Thu, 27 Aug 2026 07:31:23 +0100 Vincent Donnefort <[email protected]>…

Looking at the function again, shouldn't we add !rb_is_static(cpu_buffer) as well to the full page test?

Why?

-- Steve

So that the splice can make progress when the ring-buffer is presistent or user-mapped. Or do we say that's expected behaviour?

Quoted 6 lines
Quoted 4 lines

Ah yeah, I see. Hmm, looking at the code to see how this would work, I think I found another bug. Let me look deeper into it.

-- Steve

A bug if we allow rb_is_static()?

No, something else.

Quoted 2 lines

If not, shall I add it to my series?

Yeah, go ahead and update it.

Thanks,

-- Steve

author maintainer
#12 lore raw

PATCH v8 2/3 ring-buffer: Cap static ring buffer nr_pages

In reply to Vincent Donnefort

Static ring buffers (i.e. persistent, user-mapped and remote) rely on the bpage::id field. The number of pages for those ring buffers must fit into that variable. Enforce this limit on ring buffer creation or user-mapping.

While at it, make buffer_page::id 31 bits. This does not change the struct buffer_page size.

Fixes be68d63a139b ("ring-buffer: Add ring_buffer_alloc_range()")
kernel/trace/ring_buffer.c +21 -1
index b8e6bd309707..6089fcc67e2b 100644
@@ -350,7 +350,7 @@ struct buffer_page {
 	local_t		 entries;	/* entries on this page */
 	unsigned long	 real_end;	/* real end of data */
 	unsigned	 order;		/* order of the page */
-	u32		 id:30;		/* ID for external mapping */
+	u32		 id:31;		/* ID for external mapping */
 	u32		 range:1;	/* Mapped via a range */
 	struct buffer_data_page *page;	/* Actual data page */
 };
@@ -657,6 +657,15 @@ static bool rb_is_static(struct ring_buffer_per_cpu *cpu_buffer)
 	return cpu_buffer->user_mapped || cpu_buffer->remote || cpu_buffer->ring_meta;
 }
 
+static unsigned long rb_static_max_pages(void)
+{
+	/*
+	 * Static ring buffers are using bpage::id and must account for the
+	 * reader page.
+	 */
+	return (1UL << 31) - 1;
+}
+
 struct ring_buffer_iter {
 	struct ring_buffer_per_cpu	*cpu_buffer;
 	unsigned long			head;
@@ -2842,6 +2851,10 @@ static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags,
 		 */
 		nr_pages = (size - sizeof(struct ring_buffer_cpu_meta)) /
 			(subbuf_size + sizeof(int));
+
+		if (nr_pages > rb_static_max_pages())
+			goto fail_free_buffers;
+
 		/* Need at least two pages plus the reader page */
 		if (nr_pages < 3)
 			goto fail_free_buffers;
@@ -2874,6 +2887,10 @@ static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags,
 		/* The writer is remote. This ring-buffer is read-only */
 		atomic_inc(&buffer->record_disabled);
 		nr_pages = desc->nr_page_va - 1;
+
+		if (nr_pages > rb_static_max_pages())
+			goto fail_free_buffers;
+
 		if (nr_pages < 2)
 			goto fail_free_buffers;
 	} else {
@@ -7839,6 +7856,9 @@ int ring_buffer_map(struct trace_buffer *buffer, int cpu,
 	/* prevent another thread from changing buffer/sub-buffer sizes */
 	guard(mutex)(&buffer->mutex);
 
+	if (cpu_buffer->nr_pages > rb_static_max_pages())
+		return -E2BIG;
+
 	err = rb_alloc_meta_page(cpu_buffer);
 	if (err)
 		return err;
Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
author maintainer
#13 lore raw

PATCH v8 3/3 ring-buffer: Prevent truncation of nr_pages / nr_subbufs

In reply to Vincent Donnefort

Although ring_buffer_per_cpu::nr_pages is defined as unsigned long, it is capped to 32-bits in a few places, limiting the operations possible on a very large buffer. Use unsigned long where appropriate and prevent truncation of values using nr_pages (or nr_subbufs).

While at it, subbuf_size must be at least unsigned int.

Note that persistent, remote and user-mapped ring buffers are capping the number of pages to 31 bits already, making "int" safe in many places.

kernel/trace/ring_buffer.c +30 -30
index 6089fcc67e2b..1fcf55e2ca81 100644
@@ -1683,7 +1683,7 @@ static void rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
  * This is used to help find the next per cpu subbuffer within a mapped range.
  */
 static unsigned long
-rb_range_align_subbuf(unsigned long addr, int subbuf_size, int nr_subbufs)
+rb_range_align_subbuf(unsigned long addr, unsigned int subbuf_size, unsigned long nr_subbufs)
 {
 	addr += sizeof(struct ring_buffer_cpu_meta) +
 		sizeof(int) * nr_subbufs;
@@ -1693,13 +1693,12 @@ rb_range_align_subbuf(unsigned long addr, int subbuf_size, int nr_subbufs)
 /*
  * Return the ring_buffer_meta for a given @cpu.
  */
-static void *rb_range_meta(struct trace_buffer *buffer, int nr_pages, int cpu)
+static void *rb_range_meta(struct trace_buffer *buffer, unsigned long nr_pages, int cpu)
 {
-	int subbuf_size = rb_subbuf_size(buffer);
+	unsigned int subbuf_size = rb_subbuf_size(buffer);
 	struct ring_buffer_cpu_meta *meta;
 	struct ring_buffer_meta *bmeta;
-	unsigned long ptr;
-	int nr_subbufs;
+	unsigned long ptr, nr_subbufs;
 
 	bmeta = buffer->meta;
 	if (!bmeta)
@@ -1745,7 +1744,7 @@ static void *rb_range_meta(struct trace_buffer *buffer, int nr_pages, int cpu)
 /* Return the start of subbufs given the meta pointer */
 static void *rb_subbufs_from_meta(struct ring_buffer_cpu_meta *meta)
 {
-	int subbuf_size = meta->subbuf_size;
+	unsigned int subbuf_size = meta->subbuf_size;
 	unsigned long ptr;
 
 	ptr = (unsigned long)meta;
@@ -1757,11 +1756,11 @@ static void *rb_subbufs_from_meta(struct ring_buffer_cpu_meta *meta)
 /*
  * Return a specific sub-buffer for a given @cpu defined by @idx.
  */
-static void *rb_range_buffer(struct ring_buffer_per_cpu *cpu_buffer, int idx)
+static void *rb_range_buffer(struct ring_buffer_per_cpu *cpu_buffer, unsigned int idx)
 {
 	struct ring_buffer_cpu_meta *meta;
+	unsigned int subbuf_size;
 	unsigned long ptr;
-	int subbuf_size;
 
 	meta = rb_range_meta(cpu_buffer->buffer, 0, cpu_buffer->cpu);
 	if (!meta)
@@ -1777,7 +1776,7 @@ static void *rb_range_buffer(struct ring_buffer_per_cpu *cpu_buffer, int idx)
 
 	ptr = (unsigned long)rb_subbufs_from_meta(meta);
 
-	ptr += subbuf_size * idx;
+	ptr += (unsigned long)subbuf_size * idx;
 	if (ptr + subbuf_size > cpu_buffer->buffer->range_addr_end)
 		return NULL;
 
@@ -1854,13 +1853,13 @@ static bool rb_meta_init(struct trace_buffer *buffer, int scratch_size)
  * must be the same.
  */
 static bool rb_cpu_meta_valid(struct ring_buffer_cpu_meta *meta, int cpu,
-			      struct trace_buffer *buffer, int nr_pages,
+			      struct trace_buffer *buffer, unsigned long nr_pages,
 			      unsigned long *subbuf_mask)
 {
-	int subbuf_size = PAGE_SIZE;
+	unsigned long subbuf_size = PAGE_SIZE;
 	unsigned long buffers_start;
 	unsigned long buffers_end;
-	int i;
+	unsigned long i;
 
 	if (!subbuf_mask)
 		return false;
@@ -2109,8 +2108,8 @@ static void rb_meta_validate_events(struct ring_buffer_per_cpu *cpu_buffer)
 	struct buffer_page *head_page, *orig_head, *orig_reader;
 	struct rb_validation_state state = { 0 };
 	bool skip = false;
+	unsigned long i;
 	int ret;
-	int i;
 
 	if (!meta || !meta->head_buffer)
 		return;
@@ -2161,7 +2160,7 @@ static void rb_meta_validate_events(struct ring_buffer_per_cpu *cpu_buffer)
 		rb_validate_buffer(head_page, cpu_buffer, meta, &state, 0, state.ts);
 	}
 	if (i)
-		pr_info("Ring buffer [%d] rewound %d pages\n", cpu_buffer->cpu, i);
+		pr_info("Ring buffer [%d] rewound %lu pages\n", cpu_buffer->cpu, i);
 
 	/* The last rewound page must be skipped. */
 	if (head_page != orig_head)
@@ -2245,7 +2244,8 @@ static void rb_meta_validate_events(struct ring_buffer_per_cpu *cpu_buffer)
 	}
 }
 
-static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages, int scratch_size)
+static void rb_range_meta_init(struct trace_buffer *buffer,
+			       unsigned long nr_pages, int scratch_size)
 {
 	struct ring_buffer_cpu_meta *meta;
 	unsigned long *subbuf_mask;
@@ -2345,8 +2345,8 @@ static int rbm_show(struct seq_file *m, void *v)
 			   rb_meta_subbuf_idx(meta, (void *)meta->head_buffer));
 		seq_printf(m, "commit_buffer: %d\n",
 			   rb_meta_subbuf_idx(meta, (void *)meta->commit_buffer));
-		seq_printf(m, "subbuf_size:   %d\n", meta->subbuf_size);
-		seq_printf(m, "nr_subbufs:    %d\n", meta->nr_subbufs);
+		seq_printf(m, "subbuf_size:   %u\n", meta->subbuf_size);
+		seq_printf(m, "nr_subbufs:    %u\n", meta->nr_subbufs);
 		return 0;
 	}
 
@@ -2431,7 +2431,7 @@ static void *ring_buffer_desc_page(struct ring_buffer_desc *desc, unsigned int p
 }
 
 static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
-		long nr_pages, struct list_head *pages)
+			       unsigned long nr_pages, struct list_head *pages)
 {
 	struct trace_buffer *buffer = cpu_buffer->buffer;
 	struct ring_buffer_cpu_meta *meta = NULL;
@@ -2559,7 +2559,7 @@ static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
 }
 
 static struct ring_buffer_per_cpu *
-rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
+rb_allocate_cpu_buffer(struct trace_buffer *buffer, unsigned long nr_pages, int cpu)
 {
 	struct ring_buffer_per_cpu *cpu_buffer __free(kfree) =
 		alloc_cpu_buffer(cpu);
@@ -2716,8 +2716,8 @@ static void rb_test_inject_invalid_pages(struct trace_buffer *buffer)
 	struct ring_buffer_cpu_meta *meta;
 	struct buffer_data_page *dpage;
 	unsigned long entry_bytes = 0;
+	unsigned int subbuf_size;
 	unsigned long ptr;
-	int subbuf_size;
 	int invalid = 0;
 	int cpu;
 	int i;
@@ -2787,8 +2787,8 @@ static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags,
 					 struct ring_buffer_remote *remote)
 {
 	struct trace_buffer *buffer __free(kfree) = NULL;
-	long nr_pages;
-	int subbuf_size;
+	unsigned int subbuf_size;
+	unsigned long nr_pages;
 	int bsize;
 	int cpu;
 	int ret;
@@ -5872,12 +5872,12 @@ __rb_get_reader_page_from_remote(struct ring_buffer_per_cpu *cpu_buffer)
 static struct buffer_page *
 __rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
 {
-	int max_loops = cpu_buffer->ring_meta ? cpu_buffer->nr_pages : 3;
+	unsigned long max_loops = cpu_buffer->ring_meta ? cpu_buffer->nr_pages : 3;
 	struct buffer_page *reader = NULL;
+	unsigned long nr_loops = 0;
 	unsigned long overwrite;
 	unsigned long flags;
 	int missed_events = 0;
-	int nr_loops = 0;
 	bool ret;
 
 	local_irq_save(flags);
@@ -6195,8 +6195,8 @@ rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
 	struct trace_buffer *buffer;
 	struct ring_buffer_per_cpu *cpu_buffer;
 	struct ring_buffer_event *event;
-	int nr_loops = 0;
-	int max_loops;
+	unsigned long nr_loops = 0;
+	unsigned long max_loops;
 
 	if (ts)
 		*ts = 0;
@@ -7442,8 +7442,8 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
 	struct ring_buffer_per_cpu *cpu_buffer;
 	struct buffer_page *bpage, *tmp;
 	unsigned int old_capacity;
+	unsigned long nr_pages;
 	int old_order;
-	int nr_pages;
 	int psize;
 	int err;
 	int cpu;
@@ -7625,10 +7625,10 @@ static void rb_setup_ids_meta_page(struct ring_buffer_per_cpu *cpu_buffer,
 				   struct buffer_page **subbuf_ids)
 {
 	struct trace_buffer_meta *meta = cpu_buffer->meta_page;
-	unsigned int nr_subbufs = cpu_buffer->nr_pages + 1;
+	unsigned long nr_subbufs = cpu_buffer->nr_pages + 1;
 	struct buffer_page *first_subbuf, *subbuf;
-	int cnt = 0;
-	int id = 0;
+	unsigned int cnt = 0;
+	unsigned int id = 0;
 
 	id = rb_page_id(cpu_buffer, cpu_buffer->reader_page, id);
 	subbuf_ids[id++] = cpu_buffer->reader_page;
Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
#14 lore raw

Re: [PATCH v8 3/3] ring-buffer: Prevent truncation of nr_pages / nr_subbufs

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The kernel fails to validate the untrusted meta->nr_subbufs value from persistent memory against the expected nr_pages + 1, leading to arbitrary heap out-of-bounds writes and potential arbitrary kernel memory corruption. --

commit 5ed0ab549782b2ff87e1bd2394abb78b10c4b602
Author: Vincent Donnefort <[email protected]>

ring-buffer: Prevent truncation of nr_pages / nr_subbufs

Although ring_buffer_per_cpu::nr_pages is defined as unsigned long, it is capped to 32-bits in a few places, limiting the operations possible on a very large buffer. Use unsigned long where appropriate and prevent truncation of values using nr_pages (or nr_subbufs).

While at it, subbuf_size must be at least unsigned int.

Note that persistent, remote and user-mapped ring buffers are capping the number of pages to 31 bits already, making "int" safe in many places.

kernel/trace/ring_buffer.c static void *rb_range_meta(struct trace_buffer *buffer, int nr_pages, int cpu) · line 1750

Quoted 13 lines
kernel/trace/ring_buffer.c +1 -1
index 6089fcc67e2b6..1fcf55e2ca81f 100644
@@ -1745,7 +1744,7 @@ static void *rb_range_meta(struct trace_buffer *buffer, int nr_pages, int cpu)
 /* Return the start of subbufs given the meta pointer */
 static void *rb_subbufs_from_meta(struct ring_buffer_cpu_meta *meta)
 {
-	int subbuf_size = meta->subbuf_size;
+	unsigned int subbuf_size = meta->subbuf_size;
 	unsigned long ptr;
 
 	ptr = (unsigned long)meta;

[Severity: High]
This isn't a bug introduced by this patch, but does the next line in this function perform pointer arithmetic using an unvalidated meta->nr_subbufs?

	ptr = rb_range_align_subbuf(ptr, subbuf_size, meta->nr_subbufs);

Could this yield a corrupted or attacker-controlled subbuf base pointer if meta->nr_subbufs is read from persistent memory without being checked against expected limits?

[ ... ]

Quoted 7 lines
kernel/trace/ring_buffer.c +1 -1
@@ -1854,13 +1853,13 @@ static bool rb_meta_init(struct trace_buffer *buffer, int scratch_size)
  * must be the same.
  */
 static bool rb_cpu_meta_valid(struct ring_buffer_cpu_meta *meta, int cpu,
-			      struct trace_buffer *buffer, int nr_pages,
+			      struct trace_buffer *buffer, unsigned long nr_pages,
 			      unsigned long *subbuf_mask)

kernel/trace/ring_buffer.c static bool rb_meta_init(struct trace_buffer *buffer, int scratch_size) · line 1865

Quoted 10 lines
kernel/trace/ring_buffer.c +2 -2
 {
-	int subbuf_size = PAGE_SIZE;
+	unsigned long subbuf_size = PAGE_SIZE;
 	unsigned long buffers_start;
 	unsigned long buffers_end;
-	int i;
+	unsigned long i;
 
 	if (!subbuf_mask)
 		return false;

[Severity: High]
This isn't a bug introduced by this patch, but is there a risk of an out-of-bounds heap write further down in this function?

The subbuf_mask is allocated with nr_pages + 1 bits in rb_range_meta_init(), but later in rb_cpu_meta_valid() operations use the unvalidated meta->nr_subbufs value read from persistent memory:

	bitmap_clear(subbuf_mask, 0, meta->nr_subbufs);

	/*
	 * Ensure the meta::buffers array has correct data. The data in each subbufs
	 * are checked later in rb_meta_validate_events().
	 */
	for (i = 0; i < meta->nr_subbufs; i++) {
		if (meta->buffers[i] < 0 ||
		    meta->buffers[i] >= meta->nr_subbufs) {
			pr_info("Ring buffer boot meta [%d] array out of range\n", cpu);
			return false;
		}

		if (test_bit(meta->buffers[i], subbuf_mask)) {
			pr_info("Ring buffer boot meta [%d] array has duplicates\n", cpu);
			return false;
		}

		set_bit(meta->buffers[i], subbuf_mask);
	}

If meta->nr_subbufs is larger than nr_pages + 1, could this write out of bounds and corrupt kernel memory?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3