android_kernel_google_msm/kernel/trace
Steven Rostedt (Red Hat) 862c1fdf15 ring-buffer: Prevent overflow of size in ring_buffer_resize()
If the size passed to ring_buffer_resize() is greater than MAX_LONG - BUF_PAGE_SIZE
then the DIV_ROUND_UP() will return zero.

Here's the details:

  # echo 18014398509481980 > /sys/kernel/debug/tracing/buffer_size_kb

tracing_entries_write() processes this and converts kb to bytes.

 18014398509481980 << 10 = 18446744073709547520

and this is passed to ring_buffer_resize() as unsigned long size.

 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);

Where DIV_ROUND_UP(a, b) is (a + b - 1)/b

BUF_PAGE_SIZE is 4080 and here

 18446744073709547520 + 4080 - 1 = 18446744073709551599

where 18446744073709551599 is still smaller than 2^64

 2^64 - 18446744073709551599 = 17

But now 18446744073709551599 / 4080 = 4521260802379792

and size = size * 4080 = 18446744073709551360

This is checked to make sure its still greater than 2 * 4080,
which it is.

Then we convert to the number of buffer pages needed.

 nr_page = DIV_ROUND_UP(size, BUF_PAGE_SIZE)

but this time size is 18446744073709551360 and

 2^64 - (18446744073709551360 + 4080 - 1) = -3823

Thus it overflows and the resulting number is less than 4080, which makes

  3823 / 4080 = 0

an nr_pages is set to this. As we already checked against the minimum that
nr_pages may be, this causes the logic to fail as well, and we crash the
kernel.

There's no reason to have the two DIV_ROUND_UP() (that's just result of
historical code changes), clean up the code and fix this bug.

Change-Id: I7744dfdd1c3be9676f767139002b5f57c41d87b2
Cc: stable@vger.kernel.org # 3.5+
Fixes: 83f40318da ("ring-buffer: Make removal of ring buffer pages atomic")
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
2017-06-26 21:35:28 +03:00
..
blktrace.c block: ioctl support for sanitize in eMMC 4.5 2013-02-20 01:32:26 -08:00
ftrace.c ftrace: Fix function_graph for archs that test ftrace_trace_function 2012-03-13 15:07:37 -04:00
gpu-traces.c trace/events: add gpu trace events 2013-04-18 16:08:19 -07:00
Kconfig trace/events: add gpu trace events 2013-04-18 16:08:19 -07:00
Makefile trace/events: add gpu trace events 2013-04-18 16:08:19 -07:00
power-traces.c
ring_buffer.c ring-buffer: Prevent overflow of size in ring_buffer_resize() 2017-06-26 21:35:28 +03:00
ring_buffer_benchmark.c
rpm-traces.c
trace.c trace: resolve stack corruption due to string copy 2017-05-01 19:35:12 -06:00
trace.h tracing: Fix build breakage without CONFIG_PERF_EVENTS (again) 2012-04-13 21:37:04 -04:00
trace_branch.c
trace_clock.c
trace_cpu_freq_switch.c trace: cpu_freq_switch: Add profiler for CPU frequency switch times 2013-02-08 15:14:30 -08:00
trace_entries.h Merge branch 'tip/perf/urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace into perf/urgent 2012-03-24 08:19:09 +01:00
trace_event_perf.c msm: null pointer dereferencing 2016-01-21 15:15:08 -08:00
trace_events.c tracing: Do not enable function event with enable 2012-05-10 15:55:43 -04:00
trace_events_filter.c tracing/filter: Correct potential uninitialized variable 2013-02-08 15:14:34 -08:00
trace_events_filter_test.h
trace_export.c tracing: Do not enable function event with enable 2012-05-10 15:55:43 -04:00
trace_functions.c
trace_functions_graph.c
trace_irqsoff.c
trace_kdb.c
trace_kprobe.c ftrace, perf: Add add/del tracepoint perf registration actions 2012-02-21 11:08:25 -05:00
trace_mmiotrace.c
trace_nop.c
trace_output.c tracing: Fix stacktrace of latency tracers (irqsoff and friends) 2012-04-19 17:00:13 -04:00
trace_output.h
trace_printk.c
trace_sched_switch.c
trace_sched_wakeup.c
trace_selftest.c
trace_selftest_dynamic.c
trace_stack.c
trace_stat.c
trace_stat.h
trace_syscalls.c tracing/syscalls: Ignore numbers outside NR_syscalls' range 2016-10-29 23:12:11 +08:00
trace_workqueue.c