Files
linux/kernel
Steven Rostedt (Red Hat) f8174e3c1f 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: Ib98147073391342b8ad4164817763098b99af113
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>
2018-11-02 10:58:17 +09:00
..
2018-11-02 10:58:13 +09:00
2013-11-13 12:09:34 +09:00
2014-06-30 20:11:58 -07:00
2014-11-21 09:23:01 -08:00
2012-05-31 17:49:27 -07:00
2014-10-09 12:21:28 -07:00
2012-03-28 18:30:03 +01:00
2014-10-05 14:52:20 -07:00
2014-03-19 10:38:27 -07:00
2013-12-04 14:09:46 +10:30
2013-12-18 19:04:50 -08:00
2015-01-12 16:49:26 -08:00
2015-06-11 23:47:33 -07:00
2013-09-11 15:58:27 -07:00
2014-06-07 10:28:09 -07:00