trace: Fix race in trace_open and buffer resize call
Subsystems (4)
PATCH 1/1 trace: Fix race in trace_open and buffer resize call
Below race can come, if trace_open and resize of
cpu buffer is running parallely on different cpus
CPUX CPUY
ring_buffer_resize atomic_read(&buffer->resize_disabled)
tracing_opentracing_reset_online_cpus ring_buffer_reset_cpurb_reset_cpu
rb_update_pages remove/insert pages
resetting pointer
This race can cause data abort or some times infinte loop in rb_remove_pages and rb_insert_pages while checking pages for sanity.
Take buffer lock to fix this.
---
Changes since v0:
-Addressed Steven's review comments.
kernel/trace/ring_buffer.c +10 -0
@@ -4866,6 +4866,9 @@ void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)if (!cpumask_test_cpu (cpu, buffer->cpumask))return ; +/* prevent another thread from changing buffer sizes */ +mutex_lock (&buffer->mutex); +atomic_inc (&cpu_buffer->resize_disabled);atomic_inc (&cpu_buffer->record_disabled);
@@ -4876,6 +4879,8 @@ void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)atomic_dec (&cpu_buffer->record_disabled);atomic_dec (&cpu_buffer->resize_disabled); + +mutex_unlock (&buffer->mutex); }EXPORT_SYMBOL_GPL (ring_buffer_reset_cpu);
@@ -4889,6 +4894,9 @@ void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)struct ring_buffer_per_cpu *cpu_buffer;int cpu; +/* prevent another thread from changing buffer sizes */ +mutex_lock (&buffer->mutex); +for_each_online_buffer_cpu (buffer, cpu) { cpu_buffer = buffer->buffers[cpu];
@@ -4907,6 +4915,8 @@ void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)atomic_dec (&cpu_buffer->record_disabled);atomic_dec (&cpu_buffer->resize_disabled); } + +mutex_unlock (&buffer->mutex); }/**
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Hi,
This patch (CVE-2020-27825) was tagged with
I'm not an expert here but it seems like b23d7a5f4a07a only refactored ring_buffer_reset_cpu() by introducing reset_disabled_cpu_buffer() without significant changes. Hence, mutex_lock(&buffer->mutex)/mutex_unlock(&buffer->mutex) can be backported further than b23d7a5f4a07a~ and to all LTS kernels. Is b23d7a5f4a07a the actual cause of the bug?
Thanks,
Denis
Quoted 68 lines · Gaurav Kohli
Below race can come, if
trace_openand resize of
cpu buffer is running parallely on different cpus
CPUX CPUYring_buffer_resize atomic_read(&buffer->resize_disabled)
tracing_opentracing_reset_online_cpusring_buffer_reset_cpurb_reset_cpu
rb_update_pagesremove/insert pages
resetting pointerThis race can cause data abort or some times infinte loop in
rb_remove_pagesandrb_insert_pageswhile checking pages for sanity.Take buffer lock to fix this.
Signed-off-by Gaurav Kohli <[email protected]>---
Changes since v0:
-Addressed Steven's review comments.kernel/trace/ring_buffer.c +10
-0@@ -4866,6 +4866,9 @@ void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)if (!cpumask_test_cpu (cpu, buffer->cpumask))return ; +/* prevent another thread from changing buffer sizes */ +mutex_lock (&buffer->mutex); +atomic_inc (&cpu_buffer->resize_disabled);atomic_inc (&cpu_buffer->record_disabled);@@ -4876,6 +4879,8 @@ void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)atomic_dec (&cpu_buffer->record_disabled);atomic_dec (&cpu_buffer->resize_disabled); + +mutex_unlock (&buffer->mutex); }EXPORT_SYMBOL_GPL (ring_buffer_reset_cpu);@@ -4889,6 +4894,9 @@ void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)struct ring_buffer_per_cpu *cpu_buffer;int cpu; +/* prevent another thread from changing buffer sizes */ +mutex_lock (&buffer->mutex); +for_each_online_buffer_cpu (buffer, cpu) { cpu_buffer = buffer->buffers[cpu];@@ -4907,6 +4915,8 @@ void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)atomic_dec (&cpu_buffer->record_disabled);atomic_dec (&cpu_buffer->resize_disabled); } + +mutex_unlock (&buffer->mutex); }/**
Steven Rostedt
maintainerQuoted 11 lines · On Thu, 21 Jan 2021 17:30:40 +0300 Denis Efremov <[email protected]> wrote:
Hi,
This patch (CVE-2020-27825) was tagged with
Fixes b23d7a5f4a07a ("ring-buffer: speed up buffer resets by avoiding synchronize_rcu for each CPU")I'm not an expert here but it seems like
b23d7a5f4a07aonly refactoredring_buffer_reset_cpu()by introducingreset_disabled_cpu_buffer()without significant changes. Hence, mutex_lock(&buffer->mutex)/mutex_unlock(&buffer->mutex) can be backported further than b23d7a5f4a07a~ and to all LTS kernels. Isb23d7a5f4a07athe actual cause of the bug?
Ug, that looks to be a mistake. Looking back at the thread about this:
https://lore.kernel.org/linux-arm-msm/[email protected]/
That should have been:
Depends-on: b23d7a5f4a07 ("ring-buffer: speed up buffer resets by avoiding synchronize_rcu for each CPU")
-- Steve
Quoted 15 lines · Steven Rostedt
Quoted 11 lines · On Thu, 21 Jan 2021 17:30:40 +0300 Denis Efremov <[email protected]> wrote:
Hi,
This patch (CVE-2020-27825) was tagged with
Fixes b23d7a5f4a07a ("ring-buffer: speed up buffer resets by avoiding synchronize_rcu for each CPU")I'm not an expert here but it seems like
b23d7a5f4a07aonly refactoredring_buffer_reset_cpu()by introducingreset_disabled_cpu_buffer()without significant changes. Hence, mutex_lock(&buffer->mutex)/mutex_unlock(&buffer->mutex) can be backported further than b23d7a5f4a07a~ and to all LTS kernels. Isb23d7a5f4a07athe actual cause of the bug?
Quoted 3 lines
Ug, that looks to be a mistake. Looking back at the thread about this:
I see from the link that it was planned to backport the patch to LTS kernels:
Quoted 3 lines
Actually we are seeing issue in older kernel like 4.19/4.14/5.4 and there below patch was not present in stable branches:
Commitb23d7a5f4a07("ring-buffer: speed up buffer resets by avoidingsynchronize_rcufor each CPU")
The point is that it's not backported yet. Maybe because of Fixes tag. I've discovered this while trying to formalize CVE-2020-27825 bug in cvehound
https://github.com/evdenis/cvehound/blob/master/cvehound/cve/CVE-2020-27825.cocci
I think that the backport to the 4.4+ should be something like:
kernel/trace/ring_buffer.c +4 -0
@@ -4295,6 +4295,8 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)if (!cpumask_test_cpu (cpu, buffer->cpumask))return ; +mutex_lock (&buffer->mutex); +atomic_inc (&buffer->resize_disabled);atomic_inc (&cpu_buffer->record_disabled);
@@ -4317,6 +4319,8 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)atomic_dec (&cpu_buffer->record_disabled);atomic_dec (&buffer->resize_disabled); + +mutex_unlock (&buffer->mutex); }EXPORT_SYMBOL_GPL (ring_buffer_reset_cpu);
Thanks,
Denis
Steven Rostedt
maintainerQuoted 46 lines · On Thu, 21 Jan 2021 23:15:22 +0300 Denis Efremov <[email protected]> wrote:
Quoted 15 lines · Steven Rostedt
Quoted 11 lines · On Thu, 21 Jan 2021 17:30:40 +0300 Denis Efremov <[email protected]> wrote:
Hi,
This patch (CVE-2020-27825) was tagged with
Fixes b23d7a5f4a07a ("ring-buffer: speed up buffer resets by avoiding synchronize_rcu for each CPU")I'm not an expert here but it seems like
b23d7a5f4a07aonly refactoredring_buffer_reset_cpu()by introducingreset_disabled_cpu_buffer()without significant changes. Hence, mutex_lock(&buffer->mutex)/mutex_unlock(&buffer->mutex) can be backported further than b23d7a5f4a07a~ and to all LTS kernels. Isb23d7a5f4a07athe actual cause of the bug?Quoted 3 lines
Ug, that looks to be a mistake. Looking back at the thread about this:
I see from the link that it was planned to backport the patch to LTS kernels:
Quoted 3 lines
Actually we are seeing issue in older kernel like 4.19/4.14/5.4 and there below patch was not present in stable branches:
Commitb23d7a5f4a07("ring-buffer: speed up buffer resets by avoidingsynchronize_rcufor each CPU")The point is that it's not backported yet. Maybe because of Fixes tag. I've discovered this while trying to formalize CVE-2020-27825 bug in cvehound
https://github.com/evdenis/cvehound/blob/master/cvehound/cve/CVE-2020-27825.cocciI think that the backport to the 4.4+ should be something like:
kernel/trace/ring_buffer.c +2
-0@@ -4295,6 +4295,8 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)if (!cpumask_test_cpu (cpu, buffer->cpumask))return ; +mutex_lock (&buffer->mutex); +atomic_inc (&buffer->resize_disabled);atomic_inc (&cpu_buffer->record_disabled);
kernel/trace/ring_buffer.c void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu) · line 4326
Quoted 8 lines
kernel/trace/ring_buffer.c +2
-0@@ -4317,6 +4319,8 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)atomic_dec (&cpu_buffer->record_disabled);atomic_dec (&buffer->resize_disabled); + +mutex_unlock (&buffer->mutex); }EXPORT_SYMBOL_GPL (ring_buffer_reset_cpu);
That could possibly work.
-- Steve
Quoted 51 lines · Greg KH
Quoted 50 lines · Steven Rostedt
Quoted 47 lines · On Thu, 21 Jan 2021 23:15:22 +0300 Denis Efremov <[email protected]> wrote:
Quoted 15 lines · Steven Rostedt
Quoted 11 lines · On Thu, 21 Jan 2021 17:30:40 +0300 Denis Efremov <[email protected]> wrote:
Hi,
This patch (CVE-2020-27825) was tagged with
Fixes b23d7a5f4a07a ("ring-buffer: speed up buffer resets by avoiding synchronize_rcu for each CPU")I'm not an expert here but it seems like
b23d7a5f4a07aonly refactoredring_buffer_reset_cpu()by introducingreset_disabled_cpu_buffer()without significant changes. Hence, mutex_lock(&buffer->mutex)/mutex_unlock(&buffer->mutex) can be backported further than b23d7a5f4a07a~ and to all LTS kernels. Isb23d7a5f4a07athe actual cause of the bug?Quoted 3 lines
Ug, that looks to be a mistake. Looking back at the thread about this:
https://lore.kernel.org/linux-arm-msm/[email protected]/I see from the link that it was planned to backport the patch to LTS kernels:
Quoted 3 lines
Actually we are seeing issue in older kernel like 4.19/4.14/5.4 and there below patch was not present in stable branches:
Commitb23d7a5f4a07("ring-buffer: speed up buffer resets by avoidingsynchronize_rcufor each CPU")The point is that it's not backported yet. Maybe because of Fixes tag. I've discovered this while trying to formalize CVE-2020-27825 bug in cvehound
https://github.com/evdenis/cvehound/blob/master/cvehound/cve/CVE-2020-27825.cocciI think that the backport to the 4.4+ should be something like:
kernel/trace/ring_buffer.c +2
-0@@ -4295,6 +4295,8 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)if (!cpumask_test_cpu (cpu, buffer->cpumask))return ; +mutex_lock (&buffer->mutex); +atomic_inc (&buffer->resize_disabled);atomic_inc (&cpu_buffer->record_disabled);@@ -4317,6 +4319,8 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
Quoted 9 lines
Quoted 9 lines
kernel/trace/ring_buffer.c void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu) · line 4326
Quoted 7 lines
kernel/trace/ring_buffer.c +2
-0atomic_dec (&cpu_buffer->record_disabled);atomic_dec (&buffer->resize_disabled); + +mutex_unlock (&buffer->mutex); }EXPORT_SYMBOL_GPL (ring_buffer_reset_cpu);That could possibly work.
Yes, this will work, As i have tested similar patch for internal testing for kernel branches like 5.4/4.19.
Quoted 8 lines
Ok, so what can I do here? Can someone resend this as a backport to the other stable kernels in this way so that I can queue it up?
thanks,
greg k-h
Signature · 3 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Greg KH
maintainerQuoted 50 lines · Steven Rostedt
Quoted 47 lines · On Thu, 21 Jan 2021 23:15:22 +0300 Denis Efremov <[email protected]> wrote:
Quoted 15 lines · Steven Rostedt
Quoted 11 lines · On Thu, 21 Jan 2021 17:30:40 +0300 Denis Efremov <[email protected]> wrote:
Hi,
This patch (CVE-2020-27825) was tagged with
Fixes b23d7a5f4a07a ("ring-buffer: speed up buffer resets by avoiding synchronize_rcu for each CPU")I'm not an expert here but it seems like
b23d7a5f4a07aonly refactoredring_buffer_reset_cpu()by introducingreset_disabled_cpu_buffer()without significant changes. Hence, mutex_lock(&buffer->mutex)/mutex_unlock(&buffer->mutex) can be backported further than b23d7a5f4a07a~ and to all LTS kernels. Isb23d7a5f4a07athe actual cause of the bug?Quoted 3 lines
Ug, that looks to be a mistake. Looking back at the thread about this:
I see from the link that it was planned to backport the patch to LTS kernels:
Quoted 3 lines
Actually we are seeing issue in older kernel like 4.19/4.14/5.4 and there below patch was not present in stable branches:
Commitb23d7a5f4a07("ring-buffer: speed up buffer resets by avoidingsynchronize_rcufor each CPU")The point is that it's not backported yet. Maybe because of Fixes tag. I've discovered this while trying to formalize CVE-2020-27825 bug in cvehound
https://github.com/evdenis/cvehound/blob/master/cvehound/cve/CVE-2020-27825.cocciI think that the backport to the 4.4+ should be something like:
kernel/trace/ring_buffer.c +2
-0@@ -4295,6 +4295,8 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)if (!cpumask_test_cpu (cpu, buffer->cpumask))return ; +mutex_lock (&buffer->mutex); +atomic_inc (&buffer->resize_disabled);atomic_inc (&cpu_buffer->record_disabled);@@ -4317,6 +4319,8 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
Quoted 9 lines
kernel/trace/ring_buffer.c void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu) · line 4326
Quoted 7 lines
kernel/trace/ring_buffer.c +2
-0atomic_dec (&cpu_buffer->record_disabled);atomic_dec (&buffer->resize_disabled); + +mutex_unlock (&buffer->mutex); }EXPORT_SYMBOL_GPL (ring_buffer_reset_cpu);That could possibly work.
Ok, so what can I do here? Can someone resend this as a backport to the other stable kernels in this way so that I can queue it up?
thanks,
greg k-h
Steven Rostedt
maintainerQuoted 4 lines · On Fri, 22 Jan 2021 16:55:29 +0530 Gaurav Kohli <[email protected]> wrote:
Quoted 1 line
Quoted 1 line
That could possibly work.
Yes, this will work, As i have tested similar patch for internal testing for kernel branches like 5.4/4.19.
Can you or Denis send a proper patch for Greg to backport? I'll review it, test it and give my ack to it, so Greg can take it without issue.
Thanks!
-- Steve
Quoted 5 lines
Quoted 5 lines
Ok, so what can I do here? Can someone resend this as a backport to the other stable kernels in this way so that I can queue it up?
Quoted 11 lines · Steven Rostedt
Quoted 4 lines · On Fri, 22 Jan 2021 16:55:29 +0530 Gaurav Kohli <[email protected]> wrote:
Quoted 1 line
Quoted 1 line
That could possibly work.
Yes, this will work, As i have tested similar patch for internal testing for kernel branches like 5.4/4.19.
Can you or Denis send a proper patch for Greg to backport? I'll review it, test it and give my ack to it, so Greg can take it without issue.
I can prepare the patch, but it will be compile-tested only from my side. Honestly, I think it's better when the patch and its backports have the same author and commit message. And I can't test the fix by myself as I don't know how to reproduce conditions for the bug. I think it's better if Gaurav will prepare this backport, unless he have reasons for me to do it or maybe just don't have enough time nowadays. Gaurav, if you want to somehow mention me you add my Reported-by:
Thanks,
Denis
Quoted 22 lines · Denis Efremov
Quoted 11 lines · Steven Rostedt
Quoted 4 lines · On Fri, 22 Jan 2021 16:55:29 +0530 Gaurav Kohli <[email protected]> wrote:
Quoted 1 line
Quoted 1 line
That could possibly work.
Yes, this will work, As i have tested similar patch for internal testing for kernel branches like 5.4/4.19.
Can you or Denis send a proper patch for Greg to backport? I'll review it, test it and give my ack to it, so Greg can take it without issue.
I can prepare the patch, but it will be compile-tested only from my side. Honestly, I think it's better when the patch and its backports have the same author and commit message. And I can't test the fix by myself as I don't know how to reproduce conditions for the bug. I think it's better if Gaurav will prepare this backport, unless he have reasons for me to do it or maybe just don't have enough time nowadays. Gaurav, if you want to somehow mention me you add my Reported-by:
Quoted 3 lines
Thanks,
Denis
Sure I will do, I have never posted on backport branches. Let me check and post it.
Signature · 3 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Steven Rostedt
maintainerQuoted 3 lines · On Sat, 23 Jan 2021 22:03:27 +0530 Gaurav Kohli <[email protected]> wrote:
Sure I will do, I have never posted on backport branches. Let me check and post it.
Basically you take your original patch that was in mainline (as the subject and commit message), and make it work as if you were doing the same exact fix for the stable release.
Send it to me (and Cc everyone else), and I'll give it a test too.
Thanks!
-- Steve
Quoted 8 lines · Steven Rostedt
Quoted 3 lines · On Sat, 23 Jan 2021 22:03:27 +0530 Gaurav Kohli <[email protected]> wrote:
Sure I will do, I have never posted on backport branches. Let me check and post it.
Quoted 5 lines
Basically you take your original patch that was in mainline (as the subject and commit message), and make it work as if you were doing the same exact fix for the stable release.
Send it to me (and Cc everyone else), and I'll give it a test too.
Thanks for the guidance.
Just sent and tested it for 5.4 kernel, please review it once.
Quoted 4 lines
Thanks!
-- Steve
Signature · 3 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Greg KH
maintainerQuoted 11 lines · Gaurav Kohli
Quoted 8 lines · Steven Rostedt
Quoted 3 lines · On Sat, 23 Jan 2021 22:03:27 +0530 Gaurav Kohli <[email protected]> wrote:
Sure I will do, I have never posted on backport branches. Let me check and post it.
Quoted 8 lines
Quoted 5 lines
Basically you take your original patch that was in mainline (as the subject and commit message), and make it work as if you were doing the same exact fix for the stable release.
Send it to me (and Cc everyone else), and I'll give it a test too.
Thanks for the guidance.
Just sent and tested it for 5.4 kernel, please review it once.
<formletter>
This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.
</formletter>