Skip to contentSkip to conversation

tracing/ring-buffer: Only do full wait when cpu != RING_BUFFER_ALL_CPUS

PATCH Applied Pratyush Yadav 3 participants 4 messages Dec 16, 2022 – Dec 19, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing/ring-buffer: Only do full wait when cpu != RING_BUFFER_ALL_CPUS

full_hit() directly uses cpu as an array index. Since RING_BUFFER_ALL_CPUS == -1, calling full_hit() with cpu == RING_BUFFER_ALL_CPUS will cause an invalid memory access.

The upstream commit 42fb0a1e84ff ("tracing/ring-buffer: Have polling block on watermark") already does this. This was missed when backporting to v5.4.y.

This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.

Fixes e65ac2bdda54 ("tracing/ring-buffer: Have polling block on watermark")

---

I am not familiar with this code. This was just pointed out by our static analysis tool and I wrote a quick patch fixing this. Only compile-tested.

 kernel/trace/ring_buffer.c | 1 +
 1 file changed, 1 insertion(+)
kernel/trace/ring_buffer.c +1 -0
index 176d858903bd..11e8189dd8ae 100644
@@ -727,6 +727,7 @@ __poll_t ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
 
 	if (cpu == RING_BUFFER_ALL_CPUS) {
 		work = &buffer->irq_work;
+		full = 0;
 	} else {
 		if (!cpumask_test_cpu(cpu, buffer->cpumask))
 			return -EINVAL;

--
2.38.1

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · On Fri, 16 Dec 2022 14:42:41 +0100 Pratyush Yadav <[email protected]> wrote:

full_hit() directly uses cpu as an array index. Since RING_BUFFER_ALL_CPUS == -1, calling full_hit() with cpu == RING_BUFFER_ALL_CPUS will cause an invalid memory access.

The upstream commit 42fb0a1e84ff ("tracing/ring-buffer: Have polling block on watermark") already does this. This was missed when backporting to v5.4.y.

This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.

Nice.

Quoted 17 lines
Fixes e65ac2bdda54 ("tracing/ring-buffer: Have polling block on watermark")

---

I am not familiar with this code. This was just pointed out by our static analysis tool and I wrote a quick patch fixing this. Only compile-tested.

 kernel/trace/ring_buffer.c | 1 +
 1 file changed, 1 insertion(+)
kernel/trace/ring_buffer.c +0 -0
index 176d858903bd..11e8189dd8ae 100644

kernel/trace/ring_buffer.c __poll_t ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu, · line 730

Quoted 4 lines
kernel/trace/ring_buffer.c +1 -0
@@ -727,6 +727,7 @@ __poll_t ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
 
 	if (cpu == RING_BUFFER_ALL_CPUS) {
 		work = &buffer->irq_work;
+		full = 0;

Good catch. This was indeed missed in the backport. The backported patch even added the comment:

* @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS

Greg, please take this patch.

Thanks,

-- Steve

Quoted 18 lines
kernel/trace/ring_buffer.c +0 -1
 	} else {
 		if (!cpumask_test_cpu(cpu, buffer->cpumask))
 			return -EINVAL;
--

2.38.1

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

maintainer
#2 lore raw
Quoted 41 lines · Steven Rostedt
Quoted 10 lines · On Fri, 16 Dec 2022 14:42:41 +0100 Pratyush Yadav <[email protected]> wrote:

full_hit() directly uses cpu as an array index. Since RING_BUFFER_ALL_CPUS == -1, calling full_hit() with cpu == RING_BUFFER_ALL_CPUS will cause an invalid memory access.

The upstream commit 42fb0a1e84ff ("tracing/ring-buffer: Have polling block on watermark") already does this. This was missed when backporting to v5.4.y.

This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.

Nice.

Quoted 17 lines
Fixes e65ac2bdda54 ("tracing/ring-buffer: Have polling block on watermark")

---

I am not familiar with this code. This was just pointed out by our static analysis tool and I wrote a quick patch fixing this. Only compile-tested.

 kernel/trace/ring_buffer.c | 1 +
 1 file changed, 1 insertion(+)
kernel/trace/ring_buffer.c +0 -0
index 176d858903bd..11e8189dd8ae 100644

kernel/trace/ring_buffer.c __poll_t ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu, · line 730

Quoted 4 lines
kernel/trace/ring_buffer.c +1 -0
@@ -727,6 +727,7 @@ __poll_t ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
 
 	if (cpu == RING_BUFFER_ALL_CPUS) {
 		work = &buffer->irq_work;
+		full = 0;

Good catch. This was indeed missed in the backport. The backported patch even added the comment:

Quoted 5 lines

* @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS

Greg, please take this patch.

Now queued up, thanks.

greg k-h

maintainer Applied
#3 lore raw

Patch "tracing/ring-buffer: Only do full wait when cpu != RING_BUFFER_ALL_CPUS" has been added to the 5.4-stable tree

In reply to Pratyush Yadav

This is a note to let you know that I've just added the patch titled

    tracing/ring-buffer: Only do full wait when cpu != RING_BUFFER_ALL_CPUS

to the 5.4-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
tracing-ring-buffer-only-do-full-wait-when-cpu-ring_buffer_all_cpus.patch
and it can be found in the queue-5.4 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.

From [email protected] Mon Dec 19 13:19:42 2022
From: Pratyush Yadav <[email protected]>
Date: Fri, 16 Dec 2022 14:42:41 +0100
Subject: tracing/ring-buffer: Only do full wait when cpu != RING_BUFFER_ALL_CPUS
To: <[email protected]>
Cc: Pratyush Yadav <[email protected]>, Greg Kroah-Hartman <[email protected]>, <[email protected]>, Linux Trace Kernel <[email protected]>, Masami Hiramatsu <[email protected]>, Mathieu Desnoyers <[email protected]>, Primiano Tucci <[email protected]>, "Steven Rostedt (Google)" <[email protected]>
Message-ID: <[email protected]>

From: Pratyush Yadav <[email protected]>

full_hit() directly uses cpu as an array index. Since RING_BUFFER_ALL_CPUS == -1, calling full_hit() with cpu == RING_BUFFER_ALL_CPUS will cause an invalid memory access.

The upstream commit 42fb0a1e84ff ("tracing/ring-buffer: Have polling block on watermark") already does this. This was missed when backporting to v5.4.y.

This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.

Fixes e65ac2bdda54 ("tracing/ring-buffer: Have polling block on watermark")
---
 kernel/trace/ring_buffer.c |    1 +
 1 file changed, 1 insertion(+)
kernel/trace/ring_buffer.c +1 -0
@@ -727,6 +727,7 @@ __poll_t ring_buffer_poll_wait(struct ri
 
 	if (cpu == RING_BUFFER_ALL_CPUS) {
 		work = &buffer->irq_work;
+		full = 0;
 	} else {
 		if (!cpumask_test_cpu(cpu, buffer->cpumask))
 			return -EINVAL;

Patches currently in stable-queue which might be from [email protected] are

queue-5.4/tracing-ring-buffer-only-do-full-wait-when-cpu-ring_buffer_all_cpus.patch