Commit 3021e692 authored by Andrey Konovalov's avatar Andrey Konovalov Committed by Linus Torvalds

kcov: check kcov_softirq in kcov_remote_stop()

kcov_remote_stop() should check that the corresponding kcov_remote_start()
actually found the specified remote handle and started collecting
coverage.  This is done by checking the per thread kcov_softirq flag.

A particular failure scenario where this was observed involved a softirq
with a remote coverage collection section coming between check_kcov_mode()
and the access to t->kcov_area in __sanitizer_cov_trace_pc().  In that
softirq kcov_remote_start() bailed out after kcov_remote_find() check, but
the matching kcov_remote_stop() didn't check if kcov_remote_start()
succeeded, and overwrote per thread kcov parameters with invalid (zero)
values.

Fixes: 5ff3b30a ("kcov: collect coverage from interrupts")
Signed-off-by: default avatarAndrey Konovalov <andreyknvl@google.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Reviewed-by: default avatarDmitry Vyukov <dvyukov@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Marco Elver <elver@google.com>
Cc: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Link: http://lkml.kernel.org/r/fcd1cd16eac1d2c01a66befd8ea4afc6f8d09833.1591576806.git.andreyknvl@google.comSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 40590660
...@@ -427,7 +427,8 @@ void kcov_task_exit(struct task_struct *t) ...@@ -427,7 +427,8 @@ void kcov_task_exit(struct task_struct *t)
* WARN_ON(!kcov->remote && kcov->t != t); * WARN_ON(!kcov->remote && kcov->t != t);
* *
* For KCOV_REMOTE_ENABLE devices, the exiting task is either: * For KCOV_REMOTE_ENABLE devices, the exiting task is either:
* 2. A remote task between kcov_remote_start() and kcov_remote_stop(). *
* 1. A remote task between kcov_remote_start() and kcov_remote_stop().
* In this case we should print a warning right away, since a task * In this case we should print a warning right away, since a task
* shouldn't be exiting when it's in a kcov coverage collection * shouldn't be exiting when it's in a kcov coverage collection
* section. Here t points to the task that is collecting remote * section. Here t points to the task that is collecting remote
...@@ -437,7 +438,7 @@ void kcov_task_exit(struct task_struct *t) ...@@ -437,7 +438,7 @@ void kcov_task_exit(struct task_struct *t)
* WARN_ON(kcov->remote && kcov->t != t); * WARN_ON(kcov->remote && kcov->t != t);
* *
* 2. The task that created kcov exiting without calling KCOV_DISABLE, * 2. The task that created kcov exiting without calling KCOV_DISABLE,
* and then again we can make sure that t->kcov->t == t: * and then again we make sure that t->kcov->t == t:
* WARN_ON(kcov->remote && kcov->t != t); * WARN_ON(kcov->remote && kcov->t != t);
* *
* By combining all three checks into one we get: * By combining all three checks into one we get:
...@@ -764,7 +765,7 @@ static const struct file_operations kcov_fops = { ...@@ -764,7 +765,7 @@ static const struct file_operations kcov_fops = {
* Internally, kcov_remote_start() looks up the kcov device associated with the * Internally, kcov_remote_start() looks up the kcov device associated with the
* provided handle, allocates an area for coverage collection, and saves the * provided handle, allocates an area for coverage collection, and saves the
* pointers to kcov and area into the current task_struct to allow coverage to * pointers to kcov and area into the current task_struct to allow coverage to
* be collected via __sanitizer_cov_trace_pc() * be collected via __sanitizer_cov_trace_pc().
* In turns kcov_remote_stop() clears those pointers from task_struct to stop * In turns kcov_remote_stop() clears those pointers from task_struct to stop
* collecting coverage and copies all collected coverage into the kcov area. * collecting coverage and copies all collected coverage into the kcov area.
*/ */
...@@ -972,16 +973,25 @@ void kcov_remote_stop(void) ...@@ -972,16 +973,25 @@ void kcov_remote_stop(void)
local_irq_restore(flags); local_irq_restore(flags);
return; return;
} }
kcov = t->kcov; /*
area = t->kcov_area; * When in softirq, check if the corresponding kcov_remote_start()
size = t->kcov_size; * actually found the remote handle and started collecting coverage.
sequence = t->kcov_sequence; */
if (in_serving_softirq() && !t->kcov_softirq) {
local_irq_restore(flags);
return;
}
/* Make sure that kcov_softirq is only set when in softirq. */
if (WARN_ON(!in_serving_softirq() && t->kcov_softirq)) { if (WARN_ON(!in_serving_softirq() && t->kcov_softirq)) {
local_irq_restore(flags); local_irq_restore(flags);
return; return;
} }
kcov = t->kcov;
area = t->kcov_area;
size = t->kcov_size;
sequence = t->kcov_sequence;
kcov_stop(t); kcov_stop(t);
if (in_serving_softirq()) { if (in_serving_softirq()) {
t->kcov_softirq = 0; t->kcov_softirq = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment