Skip to contentSkip to conversation

ring-buffer: Fixes for subbuf resizing and persistent buffers

PATCH Superseded Vincent Donnefort 3 participants 14 messages Aug 17 – Aug 25

Subsystems (2)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Vincent Donnefort

author maintainer
#0 lore raw

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

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

Changelog:

v7:

- 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           | 182 ++++++++++++++++++---------
 kernel/trace/ring_buffer_benchmark.c |   6 +-
 kernel/trace/trace.c                 | 101 +++++++--------
 kernel/trace/trace.h                 |   9 +-
 5 files changed, 181 insertions(+), 122 deletions(-)

base-commit: 8b502bf6eb3da15f4b954ad3632335ff10ed746a

Signature · 1 line
2.55.0.691.gc56d675ccc-goog
author maintainer
#1 lore raw

PATCH v7 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 let it handle the resizing of a previous buffer_data_read_page if necessary and add a new ring_buffer_read_page_size() which enables ring-buffer users to avoid using the racy ring_buffer_subbuf_size_get(). This makes the spare_size member of ftrace_buffer_info redundant.

Safely handle cases in both readers where the subbuf order is modified mid-read.

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 +72 -28

190 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 +48 -53

185 lines not on this page · raw message

kernel/trace/trace.h +4 -5
index bf77331f56a4..cbf54eded447 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.691.gc56d675ccc-goog
#2 lore raw

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

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

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

New issues:
- [High] Infinite busy-wait loop in tracing reads (tracing_buffers_read and tracing_buffers_splice_read) causing lockups during concurrent ring buffer resizes.

Pre-existing issues:
- [Critical] Concurrent io_uring reads trigger a double free on info->spare->data due to missing VFS locks.
- [High] Use-After-Free vulnerability in VFS pipe buffer destruction path due to lifecycle mismatch with the trace array. --

--- Patch [1]: [PATCH v7 1/3] tracing: Fix subbuf resize races with trace_pipe_raw readers --- commit 3b5685c224f6977e19f6580474cf69ff34b547a9
Author: Vincent Donnefort <[email protected]>

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 resizing and add ring_buffer_read_page_size() to avoid racy subbuf size gets.

Quoted 4 lines
kernel/trace/ring_buffer.c +0 -0
index 5fc009edc1ec9..ec13779922ffb 100644

[ ... ]

Quoted 4 lines
kernel/trace/ring_buffer.c +2 -0
@@ -6990,56 +6990,78 @@ EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
+int ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu,
+				struct buffer_data_read_page **rpage)
 {

[ ... ]

kernel/trace/ring_buffer.c EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu); · line 7032

Quoted 8 lines
kernel/trace/ring_buffer.c +6 -1
+	if (*rpage) {
+		if ((*rpage)->order == order)
+			return 0;
 
-	bpage->order = buffer->subbuf_order;
+		/* We can reuse rpage, but we discard the payload */
+		free_pages((unsigned long)(*rpage)->data, (*rpage)->order);
+		(*rpage)->data = NULL;

[Severity: Critical]
This is a pre-existing issue, but concurrent io_uring reads can call vfs_read() without holding f_pos_lock, leading to concurrent execution of tracing_buffers_read().

Neither tracing_buffers_read() nor this function protects the shared info->spare pointer with a lock.

If these threads race during a subbuffer resize, can both observe the order mismatch simultaneously and execute free_pages() on the same data address, causing a double free?

Quoted 8 lines
kernel/trace/ring_buffer.c +7 -0
+	} else {
+		*rpage = kzalloc_obj(**rpage);
+		if (!*rpage)
+			return -ENOMEM;
+	}
+
+	(*rpage)->order = order;
 	cpu_buffer = buffer->buffers[cpu];

[ ... ]

Quoted 4 lines
kernel/trace/trace.c +0 -0
index 395238b2b715c..737922b236d46 100644

[ ... ]

kernel/trace/trace.c ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, · line 7121

Quoted 8 lines
kernel/trace/trace.c +3 -1
@@ -7128,7 +7116,9 @@ ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
 				    iter->cpu_file, 0);
 	trace_access_unlock(iter->cpu_file);
 
-	if (ret < 0) {
+	if (ret == -EAGAIN) {
+		goto again;
+	} else if (ret < 0) {

[Severity: High]
If ring_buffer_read_page() returns -EAGAIN because the subbuffer order was updated but cpu_buffer->reader_page->order hasn't been updated yet, this branches back to the again label.

Since the loop does not yield the CPU or call cond_resched(), could this cause a hard lockup if the reader and the resize thread are on the same CPU?

Quoted 3 lines
kernel/trace/trace.c +0 -0
 		if (trace_empty(iter) && !iter->closed) {
 			if (update_last_data_if_empty(iter->tr))
 				return 0;

[ ... ]

Quoted 7 lines
kernel/trace/trace.c +0 -1
@@ -7268,23 +7259,12 @@ ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
 	};
 	struct buffer_ref *ref;
 	bool woken = false;
-	int page_size;
 	int entries, i;
 	ssize_t ret = 0;

[ ... ]

kernel/trace/trace.c ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, · line 7287

Quoted 9 lines
kernel/trace/trace.c +1 -0
 	for (i = 0; i < spd.nr_pages_max && len && entries; i++) {
+		unsigned int page_size;
 		struct page *page;
 		int r;
@@ -7304,25 +7285,38 @@ ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
 
 		refcount_set(&ref->refcount, 1);
 		ref->buffer = iter->array_buffer->buffer;

[Severity: High]
This is a pre-existing issue, but splice_to_pipe() links kernel pages to a pipe buffer and stashes ref->buffer as private data.

The VFS operation never acquires a reference on the trace_array. If the user closes the trace file and deletes the trace instance, the trace_buffer is freed.

When the pipe buffer is subsequently read or destroyed, won't buffer_ref_release() dereference the dangling ref->buffer pointer when calling ring_buffer_free_read_page(), causing a use-after-free?

Quoted 12 lines
kernel/trace/trace.c +4 -4
-		ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
-		if (IS_ERR(ref->page)) {
-			ret = PTR_ERR(ref->page);
-			ref->page = NULL;
+
+new_read_page:
+		ret = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file, &ref->rpage);
+		if (ret) {
 			kfree(ref);
 			break;
 		}
 		ref->cpu = iter->cpu_file;

kernel/trace/trace.c ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, · line 7303

Quoted 10 lines
kernel/trace/trace.c +7 -2
 
-		r = ring_buffer_read_page(ref->buffer, ref->page,
-					  len, iter->cpu_file, 1);
+		page_size = ring_buffer_read_page_size(ref->rpage);
+
+		r = -EINVAL;
+		if (IS_ALIGNED(*ppos, page_size) && len >= page_size) {
+			r = ring_buffer_read_page(ref->buffer, ref->rpage, len, iter->cpu_file, 1);
+			if (r == -EAGAIN)
+				goto new_read_page;

[Severity: High]
Similar to the read path above, this catches -EAGAIN and loops back to new_read_page.

Because goto new_read_page executes continuously without releasing trace_access_lock() or yielding the CPU, can this permanently deadlock the system during a resize by preventing the resize thread from acquiring the necessary locks?

Quoted 8 lines
kernel/trace/trace.c +8 -0
+		} else if (!i) {
+			/*
+			 * If the first iteration fails this is an invalid userspace input.
+			 * Otherwise, this is because the subbuf order has been modified. Do not
+			 * report an error and finish the read.
+			 */
+			ret = -EINVAL;
+		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#3 lore raw

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

Quoted 19 lines · On Mon, 17 Aug 2026 14:08:11 +0000 [email protected] wrote:
Quoted 4 lines
kernel/trace/trace.c +0 -0
index 395238b2b715c..737922b236d46 100644

[ ... ]

kernel/trace/trace.c ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, · line 7121

Quoted 8 lines
kernel/trace/trace.c +3 -1
@@ -7128,7 +7116,9 @@ ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
 				    iter->cpu_file, 0);
 	trace_access_unlock(iter->cpu_file);
 
-	if (ret < 0) {
+	if (ret == -EAGAIN) {
+		goto again;
+	} else if (ret < 0) {  

[Severity: High]
If ring_buffer_read_page() returns -EAGAIN because the subbuffer order was updated but cpu_buffer->reader_page->order hasn't been updated yet, this branches back to the again label.

Quoted 2 lines

Since the loop does not yield the CPU or call cond_resched(), could this cause a hard lockup if the reader and the resize thread are on the same CPU?

Hmm, this looks legit. We should at least have a way to break out if it loops too many times.

Quoted 33 lines
Quoted 4 lines
kernel/trace/trace.c +0 -0
 		if (trace_empty(iter) && !iter->closed) {
 			if (update_last_data_if_empty(iter->tr))
 				return 0;  

[ ... ]

Quoted 12 lines
kernel/trace/trace.c +4 -4
-		ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
-		if (IS_ERR(ref->page)) {
-			ret = PTR_ERR(ref->page);
-			ref->page = NULL;
+
+new_read_page:
+		ret = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file, &ref->rpage);
+		if (ret) {
 			kfree(ref);
 			break;
 		}
 		ref->cpu = iter->cpu_file;

kernel/trace/trace.c ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, · line 7303

Quoted 10 lines
kernel/trace/trace.c +7 -2
 
-		r = ring_buffer_read_page(ref->buffer, ref->page,
-					  len, iter->cpu_file, 1);
+		page_size = ring_buffer_read_page_size(ref->rpage);
+
+		r = -EINVAL;
+		if (IS_ALIGNED(*ppos, page_size) && len >= page_size) {
+			r = ring_buffer_read_page(ref->buffer, ref->rpage, len, iter->cpu_file, 1);
+			if (r == -EAGAIN)
+				goto new_read_page;  

[Severity: High]
Similar to the read path above, this catches -EAGAIN and loops back to new_read_page.

Quoted 4 lines

Because goto new_read_page executes continuously without releasing trace_access_lock() or yielding the CPU, can this permanently deadlock the system during a resize by preventing the resize thread from acquiring the necessary locks?

This also looks legit.

-- Steve

Quoted 10 lines
Quoted 9 lines
kernel/trace/trace.c +8 -0
+		} else if (!i) {
+			/*
+			 * If the first iteration fails this is an invalid userspace input.
+			 * Otherwise, this is because the subbuf order has been modified. Do not
+			 * report an error and finish the read.
+			 */
+			ret = -EINVAL;
+		}  
author maintainer
#4 lore raw

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

Quoted 22 lines · Steven Rostedt
Quoted 19 lines · On Mon, 17 Aug 2026 14:08:11 +0000 [email protected] wrote:
Quoted 4 lines
kernel/trace/trace.c +0 -0
index 395238b2b715c..737922b236d46 100644

[ ... ]

kernel/trace/trace.c ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, · line 7121

Quoted 8 lines
kernel/trace/trace.c +3 -1
@@ -7128,7 +7116,9 @@ ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
 				    iter->cpu_file, 0);
 	trace_access_unlock(iter->cpu_file);
 
-	if (ret < 0) {
+	if (ret == -EAGAIN) {
+		goto again;
+	} else if (ret < 0) {  

[Severity: High]
If ring_buffer_read_page() returns -EAGAIN because the subbuffer order was updated but cpu_buffer->reader_page->order hasn't been updated yet, this branches back to the again label.

Quoted 5 lines
Quoted 2 lines

Since the loop does not yield the CPU or call cond_resched(), could this cause a hard lockup if the reader and the resize thread are on the same CPU?

Hmm, this looks legit. We should at least have a way to break out if it loops too many times.

Can this happen only on !CONFIG_PREEMPT systems?

I can add a cond_resched() here though, I believe that would make sense: if an order modification is pending, writing is disabled anyway and we are about to loose the stored events.

Additionally I can reduce the window where buffer::subbuf_order differs from the reader page order by adding an "order" argument to __rb_allocate_pages() and only set buffer::subbuf_order later in ring_buffer_subbuf_order_set().

WDYS?

Signature · 1 line
Vincent
Quoted 54 lines
Quoted 34 lines
Quoted 5 lines
kernel/trace/trace.c +0 -0
 		if (trace_empty(iter) && !iter->closed) {
 			if (update_last_data_if_empty(iter->tr))
 				return 0;  

[ ... ]

Quoted 12 lines
kernel/trace/trace.c +4 -4
-		ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
-		if (IS_ERR(ref->page)) {
-			ret = PTR_ERR(ref->page);
-			ref->page = NULL;
+
+new_read_page:
+		ret = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file, &ref->rpage);
+		if (ret) {
 			kfree(ref);
 			break;
 		}
 		ref->cpu = iter->cpu_file;

kernel/trace/trace.c ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, · line 7303

Quoted 10 lines
kernel/trace/trace.c +7 -2
 
-		r = ring_buffer_read_page(ref->buffer, ref->page,
-					  len, iter->cpu_file, 1);
+		page_size = ring_buffer_read_page_size(ref->rpage);
+
+		r = -EINVAL;
+		if (IS_ALIGNED(*ppos, page_size) && len >= page_size) {
+			r = ring_buffer_read_page(ref->buffer, ref->rpage, len, iter->cpu_file, 1);
+			if (r == -EAGAIN)
+				goto new_read_page;  

[Severity: High]
Similar to the read path above, this catches -EAGAIN and loops back to new_read_page.

Quoted 4 lines

Because goto new_read_page executes continuously without releasing trace_access_lock() or yielding the CPU, can this permanently deadlock the system during a resize by preventing the resize thread from acquiring the necessary locks?

This also looks legit.

-- Steve

Quoted 10 lines
Quoted 9 lines
kernel/trace/trace.c +8 -0
+		} else if (!i) {
+			/*
+			 * If the first iteration fails this is an invalid userspace input.
+			 * Otherwise, this is because the subbuf order has been modified. Do not
+			 * report an error and finish the read.
+			 */
+			ret = -EINVAL;
+		}  
maintainer
#5 lore raw

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

Quoted 5 lines · On Sun, 23 Aug 2026 21:09:05 +0100 Vincent Donnefort <[email protected]>…
Quoted 5 lines
Quoted 5 lines

[Severity: High]
If ring_buffer_read_page() returns -EAGAIN because the subbuffer order was updated but cpu_buffer->reader_page->order hasn't been updated yet, this branches back to the again label.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Since the loop does not yield the CPU or call cond_resched(), could this cause a hard lockup if the reader and the resize thread are on the same CPU?

Hmm, this looks legit. We should at least have a way to break out if it loops too many times.

Can this happen only on !CONFIG_PREEMPT systems?

It can happen with RT tasks as they don't yield. If this task is of higher priority than the one resizing, then it will loop forever even with a cond_resched().

Quoted 4 lines

I can add a cond_resched() here though, I believe that would make sense: if an order modification is pending, writing is disabled anyway and we are about to loose the stored events.

For RT tasks, cond_resched() wouldn't be enough.

Quoted 4 lines

Additionally I can reduce the window where buffer::subbuf_order differs from the reader page order by adding an "order" argument to __rb_allocate_pages() and only set buffer::subbuf_order later in ring_buffer_subbuf_order_set().

I'm wondering if we even care if data_page->order != cpu_buffer->reader_page->order and should change it to being a force copy instead.

If the reader_page is bigger than the dpage, and only copy what the dpage can hold. It would also update the read value of the reader_page which would allow another copy to get the rest. If the data page is bigger, then just copy everything.

It's not like this will happen often.

-- Steve

author maintainer
#6 lore raw

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

Quoted 11 lines · Steven Rostedt
Quoted 8 lines · On Sun, 23 Aug 2026 21:09:05 +0100 Vincent Donnefort <[email protected]>…
Quoted 8 lines
Quoted 7 lines

[Severity: High]
If ring_buffer_read_page() returns -EAGAIN because the subbuffer order was updated but cpu_buffer->reader_page->order hasn't been updated yet, this branches back to the again label.

Since the loop does not yield the CPU or call cond_resched(), could this cause a hard lockup if the reader and the resize thread are on the same CPU?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Hmm, this looks legit. We should at least have a way to break out if it loops too many times.

Can this happen only on !CONFIG_PREEMPT systems?

It can happen with RT tasks as they don't yield. If this task is of higher priority than the one resizing, then it will loop forever even with a cond_resched().

Ha yes of course!

Quoted 16 lines
Quoted 5 lines

I can add a cond_resched() here though, I believe that would make sense: if an order modification is pending, writing is disabled anyway and we are about to loose the stored events.

For RT tasks, cond_resched() wouldn't be enough.

Quoted 4 lines

Additionally I can reduce the window where buffer::subbuf_order differs from the reader page order by adding an "order" argument to __rb_allocate_pages() and only set buffer::subbuf_order later in ring_buffer_subbuf_order_set().

I'm wondering if we even care if data_page->order != cpu_buffer->reader_page->order and should change it to being a force copy instead.

Quoted 8 lines

If the reader_page is bigger than the dpage, and only copy what the dpage can hold. It would also update the read value of the reader_page which would allow another copy to get the rest. If the data page is bigger, then just copy everything.

It's not like this will happen often.

-- Steve

That sounds interesting... that would get rid of the -EAGAIN. Let me try.

Do you think it is still worth to reduce the window of discrepency between buffer->subbuf_order and buffer_page->order?

Signature · 1 line
Vincent
maintainer
#7 lore raw

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

Quoted 3 lines · On Tue, 25 Aug 2026 09:26:07 +0100 Vincent Donnefort <[email protected]>…

Do you think it is still worth to reduce the window of discrepency between buffer->subbuf_order and buffer_page->order?

Can you elaborate more on what you mean?

-- Steve

author maintainer
#8 lore raw

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

Quoted 10 lines · Steven Rostedt
Quoted 3 lines · On Tue, 25 Aug 2026 09:26:07 +0100 Vincent Donnefort <[email protected]>…

Do you think it is still worth to reduce the window of discrepency between buffer->subbuf_order and buffer_page->order?

Can you elaborate more on what you mean?

-- Steve

Something like this.

@@ -7469,8 +7473,6 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)

        /* Make sure all commits have finished */
        synchronize_rcu();

- buffer->subbuf_order = order;
-

        /* Make sure all new buffers are allocated, before deleting the old ones */
        for_each_buffer_cpu(buffer, cpu) {

@@ -7500,6 +7502,7 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)

                /* Allocate the new size buffer */
                INIT_LIST_HEAD(&cpu_buffer->new_pages);
                if (__rb_allocate_pages(cpu_buffer, nr_pages,
+                                       order,
                                        &cpu_buffer->new_pages)) {
                        /* not enough memory for new pages */
                        err = -ENOMEM;
@@ -7507,6 +7510,8 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
                }
        }

+ buffer->subbuf_order = order;
+

        for_each_buffer_cpu(buffer, cpu) {
                struct buffer_data_read_page old_free_data_page;
                struct list_head old_pages;

But now with the idea of copying the data when data_page->order != reader_page->order, I am less convinced there's any gain.

Signature · 1 line
Vincent
maintainer
#9 lore raw

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

Quoted 2 lines · On Tue, 25 Aug 2026 13:52:11 +0100 Vincent Donnefort <[email protected]>…

But now with the idea of copying the data when data_page->order != reader_page->order, I am less convinced there's any gain.

Yeah, let's leave this alone for now.

-- Steve

author maintainer
#10 lore raw

PATCH v7 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 ec13779922ff..5e26eeff588f 100644
@@ -345,7 +345,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 */
 };
@@ -652,6 +652,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;
@@ -2837,6 +2846,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;
@@ -2869,6 +2882,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 {
@@ -7834,6 +7851,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.691.gc56d675ccc-goog
#11 lore raw

Re: [PATCH v7 2/3] ring-buffer: Cap static ring buffer nr_pages

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

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

Pre-existing issues:
- [High] Integer underflow silently bypasses the newly introduced rb_static_max_pages() bound check on 32-bit platforms.
- [High] Integer overflow in pointer arithmetic when ring buffer is large causes out-of-bounds memory mapping.
- [Medium] Mixing goto-based error handling with scope-based cleanup (__free and guard) violates subsystem guidelines. --

--- Patch [2]: [PATCH v7 2/3] ring-buffer: Cap static ring buffer nr_pages --- commit 5ef3e045e7449785cd305ffb3c9bd4573685ad34
Author: Vincent Donnefort <[email protected]>

ring-buffer: Cap static ring buffer nr_pages

This commit introduces a maximum page limit for static ring buffers to ensure the page count fits within the buffer_page::id field. It enforces this limit during buffer allocation and user-mapping, while increasing the id field to 31 bits.

Quoted 4 lines
kernel/trace/ring_buffer.c +0 -0
index ec13779922ffb..5e26eeff588f2 100644

[ ... ]

kernel/trace/ring_buffer.c static bool rb_is_static(struct ring_buffer_per_cpu *cpu_buffer) · line 662

Quoted 12 lines
kernel/trace/ring_buffer.c +8 -0
@@ -652,6 +652,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;
+}

[Severity: High]
This is a pre-existing issue, but does a maximum page limit this large cause integer overflow in pointer arithmetic when mapping memory?

While the limit restricts the number of pages to (1UL << 31) - 1, configuring a very large memory-mapped trace buffer (e.g., >= 2GB per CPU, or ~524,288 pages) can cause multiplication overflows elsewhere:

kernel/trace/ring_buffer.c:rb_range_meta() {
    ...
    ptr += subbuf_size * nr_subbufs;
    ...
}

And similarly in rb_range_buffer():

kernel/trace/ring_buffer.c:rb_range_buffer() {
    ...
    ptr += subbuf_size * idx;
    ...
}

Since the operands are treated as 32-bit signed integers, configuring a buffer with 524,288 pages causes the multiplication to exceed INT_MAX and overflow into a negative value. This negative offset is added to the starting pointer, resulting in the mapped buffer incorrectly pointing to memory before the valid allocation range.

Quoted 4 lines
kernel/trace/ring_buffer.c +1 -0
+
 struct ring_buffer_iter {
 	struct ring_buffer_per_cpu	*cpu_buffer;
 	unsigned long			head;

[ ... ]

kernel/trace/ring_buffer.c static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags, · line 2848

Quoted 4 lines
kernel/trace/ring_buffer.c +0 -0
@@ -2837,6 +2846,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));

[Severity: High]
This is a pre-existing issue, but does an integer underflow here silently bypass the newly introduced bound check on 32-bit platforms?

If the memory-mapped ring buffer size is extremely small, the calculation of the available size for per-CPU data underflows, causing the size variable to become a massive positive value.

On 32-bit architectures, the nr_pages variable evaluates to approximately 1 million pages. Since this is less than the rb_static_max_pages() limit, the check passes. The subsequent loop calculating subbuffer pointers then overflows 32-bit arithmetic, bypassing bounds checks and allowing out-of-bounds memory corruption.

kernel/trace/ring_buffer.c static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags, · line 2851

Quoted 3 lines
kernel/trace/ring_buffer.c +3 -0
+
+		if (nr_pages > rb_static_max_pages())
+			goto fail_free_buffers;

[Severity: Medium]
This is a pre-existing issue, but does adding new goto error paths here perpetuate a mixture of goto-based error handling and scope-based cleanup?

The alloc_buffer() function uses scope-based cleanup:

    struct trace_buffer *buffer __free(kfree) = NULL;

Mixing __free() declarations and manual goto cleanup labels in the same function violates subsystem guidelines and increases the risk of memory leaks or confusing ownership semantics.

Quoted 4 lines
kernel/trace/ring_buffer.c +1 -0
+
 		/* Need at least two pages plus the reader page */
 		if (nr_pages < 3)
 			goto fail_free_buffers;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#12 lore raw

PATCH v7 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 5e26eeff588f..7d22f10df3d6 100644
@@ -1678,7 +1678,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;
@@ -1688,13 +1688,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)
@@ -1740,7 +1739,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;
@@ -1752,11 +1751,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)
@@ -1772,7 +1771,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;
 
@@ -1849,13 +1848,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;
@@ -2104,8 +2103,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;
@@ -2156,7 +2155,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)
@@ -2240,7 +2239,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;
@@ -2340,8 +2340,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;
 	}
 
@@ -2426,7 +2426,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;
@@ -2554,7 +2554,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);
@@ -2711,8 +2711,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;
@@ -2782,8 +2782,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;
@@ -5867,12 +5867,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);
@@ -6190,8 +6190,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;
@@ -7438,8 +7438,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;
@@ -7621,10 +7621,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.691.gc56d675ccc-goog
#13 lore raw

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

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

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

Pre-existing issues:
- [High] Integer underflow in alloc_buffer() for mapped ring buffers leads to memory corruption on 32-bit systems.
- [High] Out-of-bounds memory corruption in rb_cpu_meta_valid during ring buffer boot meta validation due to unvalidated meta->nr_subbufs. --

--- Patch [3]: [PATCH v7 3/3] ring-buffer: Prevent truncation of nr_pages / nr_subbufs --- commit ca74d5f745c91c8b4a7ee42fae91ab814132919c
Author: Vincent Donnefort <[email protected]>

ring-buffer: Prevent truncation of nr_pages / nr_subbufs

This commit prevents truncation operations on large ring buffers by changing nr_pages and nr_subbufs definitions to unsigned long. It also upgrades subbuf_size to unsigned int.

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

[ ... ]

Quoted 7 lines
kernel/trace/ring_buffer.c +1 -1
@@ -1849,13 +1848,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 1860

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 is a pre-existing issue, but can this lead to an out-of-bounds memory overwrite on the kernel heap?

In rb_range_meta_init(), subbuf_mask is allocated using the current boot's nr_pages + 1:

    subbuf_mask = bitmap_alloc(nr_pages + 1, GFP_KERNEL);

However, later in rb_cpu_meta_valid(), the legacy persistent metadata is used to clear and set bits in this mask:

    bitmap_clear(subbuf_mask, 0, meta->nr_subbufs);
    ...
    for (i = 0; i < meta->nr_subbufs; i++) {
        ...
        set_bit(meta->buffers[i], subbuf_mask);
    }

If the system boots with more CPUs than the previous cycle, the per-CPU nr_pages constraint decreases. Consequently, the legacy meta->nr_subbufs could be larger than the newly allocated subbuf_mask, causing bitmap_clear() and set_bit() to overflow the allocation.

[ ... ]

kernel/trace/ring_buffer.c static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags, · line 2789

Quoted 11 lines
kernel/trace/ring_buffer.c +2 -2
@@ -2782,8 +2782,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;

[Severity: High]
This is a pre-existing issue, but does this function suffer from an integer underflow that could bypass length boundary validations?

Further down in alloc_buffer(), when calculating the per-CPU size for mapped buffers:

    size = end - buffers_start;
    size = size / nr_cpu_ids;
    nr_pages = (size - sizeof(struct ring_buffer_cpu_meta)) /
               (subbuf_size + sizeof(int));

If the user-provided mapped range is smaller than the meta structure size, size - sizeof(struct ring_buffer_cpu_meta) will underflow. On 32-bit systems, this yields a massive nr_pages value that successfully passes the rb_static_max_pages() threshold check.

The subsequently calculated pointers will wrap around the 32-bit address space, defeating the bounds check against the end boundary, and allowing mapped metadata structures to be written into arbitrary out-of-bounds memory.

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