Skip to content

Commit 757bdba

Browse files
aegistudiogregkh
authored andcommitted
tracing: Fix bug in rb_per_cpu_empty() that might cause deadloop.
commit 67f0d6d 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 (2734d6c), 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: bf41a15 ("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>
1 parent a5e1aff commit 757bdba

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

‎kernel/trace/ring_buffer.c‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3649,10 +3649,30 @@ static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
36493649
if (unlikely(!head))
36503650
return true;
36513651

3652-
return reader->read == rb_page_commit(reader) &&
3653-
(commit == reader ||
3654-
(commit == head &&
3655-
head->read == rb_page_commit(commit)));
3652+
/* Reader should exhaust content in reader page */
3653+
if (reader->read != rb_page_commit(reader))
3654+
return false;
3655+
3656+
/*
3657+
* If writers are committing on the reader page, knowing all
3658+
* committed content has been read, the ring buffer is empty.
3659+
*/
3660+
if (commit == reader)
3661+
return true;
3662+
3663+
/*
3664+
* If writers are committing on a page other than reader page
3665+
* and head page, there should always be content to read.
3666+
*/
3667+
if (commit != head)
3668+
return false;
3669+
3670+
/*
3671+
* Writers are committing on the head page, we just need
3672+
* to care about there're committed data, and the reader will
3673+
* swap reader page with head page when it is to read data.
3674+
*/
3675+
return rb_page_commit(commit) == 0;
36563676
}
36573677

36583678
/**

0 commit comments

Comments
 (0)