linux-stable/kernel/trace
Haoran Luo 6a99bfee7f tracing: Fix bug in rb_per_cpu_empty() that might cause deadloop.
commit 67f0d6d988 upstream.

The "rb_per_cpu_empty()" misinterpret the condition (as not-empty) when
"head_page" and "commit_page" of "struct ring_buffer_per_cpu" points to
the same buffer page, whose "buffer_data_page" is empty and "read" field
is non-zero.

An error scenario could be constructed as followed (kernel perspective):

1. All pages in the buffer has been accessed by reader(s) so that all of
them will have non-zero "read" field.

2. Read and clear all buffer pages so that "rb_num_of_entries()" will
return 0 rendering there's no more data to read. It is also required
that the "read_page", "commit_page" and "tail_page" points to the same
page, while "head_page" is the next page of them.

3. Invoke "ring_buffer_lock_reserve()" with large enough "length"
so that it shot pass the end of current tail buffer page. Now the
"head_page", "commit_page" and "tail_page" points to the same page.

4. Discard current event with "ring_buffer_discard_commit()", so that
"head_page", "commit_page" and "tail_page" points to a page whose buffer
data page is now empty.

When the error scenario has been constructed, "tracing_read_pipe" will
be trapped inside a deadloop: "trace_empty()" returns 0 since
"rb_per_cpu_empty()" returns 0 when it hits the CPU containing such
constructed ring buffer. Then "trace_find_next_entry_inc()" always
return NULL since "rb_num_of_entries()" reports there's no more entry
to read. Finally "trace_seq_to_user()" returns "-EBUSY" spanking
"tracing_read_pipe" back to the start of the "waitagain" loop.

I've also written a proof-of-concept script to construct the scenario
and trigger the bug automatically, you can use it to trace and validate
my reasoning above:

  https://github.com/aegistudio/RingBufferDetonator.git

Tests has been carried out on linux kernel 5.14-rc2
(2734d6c1b1), my fixed version
of kernel (for testing whether my update fixes the bug) and
some older kernels (for range of affected kernels). Test result is
also attached to the proof-of-concept repository.

Link: https://lore.kernel.org/linux-trace-devel/YPaNxsIlb2yjSi5Y@aegistudio/
Link: https://lore.kernel.org/linux-trace-devel/YPgrN85WL9VyrZ55@aegistudio

Cc: stable@vger.kernel.org
Fixes: bf41a158ca ("ring-buffer: make reentrant")
Suggested-by: Linus Torvalds <torvalds@linuxfoundation.org>
Signed-off-by: Haoran Luo <www@aegistudio.net>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-07-28 11:13:50 +02:00
..
blktrace.c blktrace: fix debugfs use after free 2020-11-10 12:35:54 +01:00
bpf_trace.c tracepoint: Add tracepoint_probe_register_may_exist() for BPF tracing 2021-07-20 16:15:42 +02:00
ftrace.c ftrace: Do not blindly read the ip address in ftrace_bug() 2021-06-16 11:55:02 +02:00
Kconfig tracing/kprobes: Do the notrace functions check without kprobes on ftrace 2021-01-19 18:22:35 +01:00
Makefile tracing: Allow gcov profiling on only ftrace subsystem 2018-08-21 09:11:49 -04:00
power-traces.c
preemptirq_delay_test.c tracing: Use trace_clock_local() for looping in preemptirq_delay_test.c 2018-10-17 15:35:33 -04:00
ring_buffer.c tracing: Fix bug in rb_per_cpu_empty() that might cause deadloop. 2021-07-28 11:13:50 +02:00
ring_buffer_benchmark.c tracing: Add SPDX License format tags to tracing files 2018-08-16 19:08:06 -04:00
rpm-traces.c
trace.c tracing: Resize tgid_map to pid_max, not PID_MAX_DEFAULT 2021-07-20 16:16:05 +02:00
trace.h tracing: Fix userstacktrace option for instances 2020-12-11 13:25:03 +01:00
trace_benchmark.c
trace_benchmark.h tracing: Fix SPDX format headers to use C++ style comments 2018-08-16 19:08:06 -04:00
trace_branch.c Revert "x86/uaccess, ftrace: Fix ftrace_likely_update() vs. SMAP" 2019-07-03 13:14:41 +02:00
trace_clock.c tracing: Do no increment trace_clock_global() by one 2021-06-30 08:48:23 -04:00
trace_entries.h tracing: Set kernel_stack's caller size properly 2020-10-01 13:14:31 +02:00
trace_event_perf.c tracing: Fix race in perf_trace_buf initialization 2019-10-29 09:20:03 +01:00
trace_events.c tracing: Do not count ftrace events in top level enable output 2021-02-23 15:00:55 +01:00
trace_events_filter.c tracing: Have error path in predicate_parse() free its allocated memory 2020-08-05 10:05:59 +02:00
trace_events_filter_test.h tracing: Fix SPDX format headers to use C++ style comments 2018-08-16 19:08:06 -04:00
trace_events_hist.c tracing: Do not reference char * as a string in histograms 2021-07-20 16:16:07 +02:00
trace_events_trigger.c tracing: Fix event trigger to accept redundant spaces 2020-06-30 23:17:17 -04:00
trace_export.c
trace_functions.c
trace_functions_graph.c tracing/fgraph: Fix set_graph_function from showing interrupts 2018-12-08 12:59:07 +01:00
trace_hwlat.c tracing: Remove WARN_ON in start_thread() 2020-12-08 10:18:55 +01:00
trace_irqsoff.c tracing/fgraph: Fix set_graph_function from showing interrupts 2018-12-08 12:59:07 +01:00
trace_kdb.c tracing: Silence GCC 9 array bounds warning 2019-06-25 11:35:52 +08:00
trace_kprobe.c tracing/kprobe: Fix to support kretprobe events on unloaded modules 2021-02-13 13:51:13 +01:00
trace_kprobe_selftest.c
trace_kprobe_selftest.h tracing: Fix SPDX format headers to use C++ style comments 2018-08-16 19:08:06 -04:00
trace_mmiotrace.c
trace_nop.c
trace_output.c tracing: Add SPDX License format tags to tracing files 2018-08-16 19:08:06 -04:00
trace_output.h tracing: Fix SPDX format headers to use C++ style comments 2018-08-16 19:08:06 -04:00
trace_preemptirq.c lockdep: fix order in trace_hardirqs_off_caller() 2020-10-01 13:14:50 +02:00
trace_printk.c tracing: Add SPDX License format tags to tracing files 2018-08-16 19:08:06 -04:00
trace_probe.c tracing: Add SPDX License format tags to tracing files 2018-08-16 19:08:06 -04:00
trace_probe.h tracing: Add SPDX License format tags to tracing files 2018-08-16 19:08:06 -04:00
trace_sched_switch.c tracing: Fix sched switch start/stop refcount racy updates 2020-02-11 04:33:55 -08:00
trace_sched_wakeup.c kernel/trace: Fix do not unregister tracepoints when register sched_migrate_task fail 2020-01-14 20:06:59 +01:00
trace_selftest.c ftrace: Handle tracing when switching between context 2020-11-10 12:35:58 +01:00
trace_selftest_dynamic.c
trace_seq.c tracing: Add SPDX License format tags to tracing files 2018-08-16 19:08:06 -04:00
trace_stack.c tracing: Have stack tracer compile when MCOUNT_INSN_SIZE is not defined 2020-01-14 20:06:59 +01:00
trace_stat.c tracing: Fix very unlikely race of registering two stat tracers 2020-02-24 08:34:39 +01:00
trace_stat.h tracing: Fix SPDX format headers to use C++ style comments 2018-08-16 19:08:06 -04:00
trace_syscalls.c
trace_uprobe.c tracing/uprobes: Fix output for multiple string arguments 2019-02-20 10:25:48 +01:00
tracing_map.c tracing: Have the histogram compare functions convert to u64 first 2020-01-09 10:19:02 +01:00
tracing_map.h tracing: Fix SPDX format headers to use C++ style comments 2018-08-16 19:08:06 -04:00