While doing tests on tracing over the network, I found that the packets
were getting corrupted. In the process I found three bugs. One was the
culprit, but the other two scared me. After deeper investigation, they
were not as major as I thought they were, due to a signed compared to
an unsigned that prevented a negative number from doing actual harm.

The two bigger bugs:

 - Mask the ring buffer data page length. There are data flags at the
   high bits of the length field. These were not cleared via the
   length function, and the length could return a negative number.
   (Although the number returned was unsigned, but was assigned to a
   signed number) Luckily, this value was compared to PAGE_SIZE which is
   unsigned and kept it from entering the path that could have caused damage.

 - Check the page usage before reusing the ring buffer reader page.
   TCP increments the page ref when passing the page off to the network.
   The page is passed back to the ring buffer for use on free. But
   the page could still be in use by the TCP stack.

Minor bugs:

 - Related to the first bug. No need to clear out the unused ring buffer
   data before sending to user space. It is now done by the ring buffer
   code itself.

 - Reset pointers after free on error path. There were some cases in
   the error path that pointers were freed but not set to NULL, and could
   have them freed again, having a pointer freed twice.

tracing: Fix possible double free on failure of allocating trace buffer

Jing Xia and Chunyan Zhang reported that on failing to allocate part of the
tracing buffer, memory is freed, but the pointers that point to them are not
initialized back to NULL, and later paths may try to free the freed memory
again. Jing and Chunyan fixed one of the locations that does this, but
missed a spot.

Link: http://lkml.kernel.org/r/20171226071253.8968-1-chunyan.zhang@spreadtrum.com

Cc: stable@vger.kernel.org
Fixes: 737223fbca3b1 ("tracing: Consolidate buffer allocation code")
Reported-by: Jing Xia <jing.xia@spreadtrum.com>
Reported-by: Chunyan Zhang <chunyan.zhang@spreadtrum.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 file changed