linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ring-buffer: fix array bounds checking
@ 2024-12-16 16:49 Jeongjun Park
  2024-12-16 17:54 ` Vincent Donnefort
  0 siblings, 1 reply; 6+ messages in thread
From: Jeongjun Park @ 2024-12-16 16:49 UTC (permalink / raw)
  To: rostedt, mhiramat, mathieu.desnoyers
  Cc: david, vdonnefort, linux-kernel, linux-trace-kernel,
	Jeongjun Park

If there is a case where the variable s is greater than or equal to nr_subbufs
before entering the loop, oob read or use-after-free will occur. This problem
occurs because the variable s is used as an index to dereference the
struct page before the variable value range check. This logic prevents the
wrong address value from being copied to the pages array through the subsequent
range check, but oob read still occurs, so the code needs to be modified.

Fixes: 117c39200d9d ("ring-buffer: Introducing ring-buffer mapping functions")
Signed-off-by: Jeongjun Park <aha310510@gmail.com>
---
 kernel/trace/ring_buffer.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 7e257e855dd1..83da74bf7bd6 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -6994,9 +6994,9 @@ static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
 {
 	unsigned long nr_subbufs, nr_pages, nr_vma_pages, pgoff = vma->vm_pgoff;
 	unsigned int subbuf_pages, subbuf_order;
-	struct page **pages;
+	struct page **pages, *page;
 	int p = 0, s = 0;
-	int err;
+	int err, off;
 
 	/* Refuse MP_PRIVATE or writable mappings */
 	if (vma->vm_flags & VM_WRITE || vma->vm_flags & VM_EXEC ||
@@ -7055,14 +7055,14 @@ static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
 	}
 
 	while (p < nr_pages) {
-		struct page *page = virt_to_page((void *)cpu_buffer->subbuf_ids[s]);
-		int off = 0;
-
 		if (WARN_ON_ONCE(s >= nr_subbufs)) {
 			err = -EINVAL;
 			goto out;
 		}
 
+		page = virt_to_page((void *)cpu_buffer->subbuf_ids[s]);
+		off = 0;
+
 		for (; off < (1 << (subbuf_order)); off++, page++) {
 			if (p >= nr_pages)
 				break;
--

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-12-18 16:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-16 16:49 [PATCH] ring-buffer: fix array bounds checking Jeongjun Park
2024-12-16 17:54 ` Vincent Donnefort
2024-12-17  1:28   ` Jeongjun Park
2024-12-17  8:38     ` Vincent Donnefort
2024-12-17 13:42       ` Jeongjun Park
2024-12-18 17:00         ` Steven Rostedt

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).