tracing: Add trace_array_get/put() to handle instance refs better
authorSteven Rostedt (Red Hat) <rostedt@goodmis.org>
Tue, 2 Jul 2013 02:50:29 +0000 (22:50 -0400)
committerSteven Rostedt <rostedt@goodmis.org>
Tue, 2 Jul 2013 13:58:11 +0000 (09:58 -0400)
commitff451961a8b2a17667a7bfa39c86fb9b351445db
tree03f82366f860605664b4a7fe11a97f57fc0632a8
parenta82274151af2b075163e3c42c828529dee311487
tracing: Add trace_array_get/put() to handle instance refs better

Commit a695cb58162 "tracing: Prevent deleting instances when they are being read"
tried to fix a race between deleting a trace instance and reading contents
of a trace file. But it wasn't good enough. The following could crash the kernel:

 # cd /sys/kernel/debug/tracing/instances
 # ( while :; do mkdir foo; rmdir foo; done ) &
 # ( while :; do cat foo/trace &> /dev/null; done ) &

Luckily this can only be done by root user, but it should be fixed regardless.

The problem is that a delete of the file can happen after the reader starts
to open the file but before it grabs the trace_types_mutex.

The solution is to validate the trace array before using it. If the trace
array does not exist in the list of trace arrays, then it returns -ENODEV.

There's a possibility that a trace_array could be deleted and a new one
created and the open would open its file instead. But that is very minor as
it will just return the data of the new trace array, it may confuse the user
but it will not crash the system. As this can only be done by root anyway,
the race will only occur if root is deleting what its trying to read at
the same time.

Cc: stable@vger.kernel.org # 3.10
Reported-by: Alexander Lam <azl@google.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
kernel/trace/trace.c