android_kernel_oneplus_msm8998/kernel/sched
Dmitry Vyukov 9b83f370dc BACKPORT: kernel: add kcov code coverage
kcov provides code coverage collection for coverage-guided fuzzing
(randomized testing).  Coverage-guided fuzzing is a testing technique
that uses coverage feedback to determine new interesting inputs to a
system.  A notable user-space example is AFL
(http://lcamtuf.coredump.cx/afl/).  However, this technique is not
widely used for kernel testing due to missing compiler and kernel
support.

kcov does not aim to collect as much coverage as possible.  It aims to
collect more or less stable coverage that is function of syscall inputs.
To achieve this goal it does not collect coverage in soft/hard
interrupts and instrumentation of some inherently non-deterministic or
non-interesting parts of kernel is disbled (e.g.  scheduler, locking).

Currently there is a single coverage collection mode (tracing), but the
API anticipates additional collection modes.  Initially I also
implemented a second mode which exposes coverage in a fixed-size hash
table of counters (what Quentin used in his original patch).  I've
dropped the second mode for simplicity.

This patch adds the necessary support on kernel side.  The complimentary
compiler support was added in gcc revision 231296.

We've used this support to build syzkaller system call fuzzer, which has
found 90 kernel bugs in just 2 months:

  https://github.com/google/syzkaller/wiki/Found-Bugs

We've also found 30+ bugs in our internal systems with syzkaller.
Another (yet unexplored) direction where kcov coverage would greatly
help is more traditional "blob mutation".  For example, mounting a
random blob as a filesystem, or receiving a random blob over wire.

Why not gcov.  Typical fuzzing loop looks as follows: (1) reset
coverage, (2) execute a bit of code, (3) collect coverage, repeat.  A
typical coverage can be just a dozen of basic blocks (e.g.  an invalid
input).  In such context gcov becomes prohibitively expensive as
reset/collect coverage steps depend on total number of basic
blocks/edges in program (in case of kernel it is about 2M).  Cost of
kcov depends only on number of executed basic blocks/edges.  On top of
that, kernel requires per-thread coverage because there are always
background threads and unrelated processes that also produce coverage.
With inlined gcov instrumentation per-thread coverage is not possible.

kcov exposes kernel PCs and control flow to user-space which is
insecure.  But debugfs should not be mapped as user accessible.

Based on a patch by Quentin Casasnovas.

[akpm@linux-foundation.org: make task_struct.kcov_mode have type `enum kcov_mode']
[akpm@linux-foundation.org: unbreak allmodconfig]
[akpm@linux-foundation.org: follow x86 Makefile layout standards]
Signed-off-by: Dmitry Vyukov <dvyukov@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Cc: syzkaller <syzkaller@googlegroups.com>
Cc: Vegard Nossum <vegard.nossum@oracle.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Tavis Ormandy <taviso@google.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Quentin Casasnovas <quentin.casasnovas@oracle.com>
Cc: Kostya Serebryany <kcc@google.com>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Kees Cook <keescook@google.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Sasha Levin <sasha.levin@oracle.com>
Cc: David Drysdale <drysdale@google.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Kirill A. Shutemov <kirill@shutemov.name>
Cc: Jiri Slaby <jslaby@suse.cz>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Bug: 64145065
(cherry-picked from 5c9a8750a6409c63a0f01d51a9024861022f6593)
Change-Id: I17b5e04f6e89b241924e78ec32ead79c38b860ce
Signed-off-by: Paul Lawrence <paullawrence@google.com>
2017-12-18 09:41:57 -08:00
..
auto_group.c sched/autogroup: Fix autogroup_move_group() to never skip sched_move_task() 2017-10-27 10:23:17 +02:00
auto_group.h sched, timer: Convert usages of ACCESS_ONCE() in the scheduler to READ_ONCE()/WRITE_ONCE() 2015-05-08 12:11:32 +02:00
clock.c treewide: Remove old email address 2015-11-23 09:44:58 +01:00
completion.c sched/completion: Serialize completion_done() with complete() 2015-02-18 14:27:40 +01:00
core.c This is the 4.4.103 stable release 2017-11-30 15:43:08 +00:00
cpuacct.c cgroup: rename cgroup_subsys->base_cftypes to ->legacy_cftypes 2014-07-15 11:05:09 -04:00
cpuacct.h sched/cpuacct: Initialize root cpuacct earlier 2013-04-10 13:54:20 +02:00
cpudeadline.c sched/deadline: Unify dl_time_before() usage 2015-09-23 09:51:25 +02:00
cpudeadline.h sched/deadline: Unify dl_time_before() usage 2015-09-23 09:51:25 +02:00
cpufreq.c sched: backport cpufreq hooks from 4.9-rc4 2017-06-02 08:01:50 -07:00
cpufreq_schedutil.c BACKPORT: schedutil: Reset cached freq if it is not in sync with next_freq 2017-12-12 16:45:55 +00:00
cpupri.c Merge commit '3cf2f34' into sched/core, to fix build error 2014-06-12 13:46:37 +02:00
cpupri.h sched/cpupri: Remove unnecessary definitions in cpupri.h 2014-11-16 10:58:59 +01:00
cputime.c Merge remote-tracking branch 'common/android-4.4' into android-4.4.y 2016-09-13 14:47:50 -07:00
deadline.c FROMLIST: sched/fair: Use wake_q length as a hint for wake_wide 2017-10-27 18:50:59 +00:00
debug.c sched/fair: Add eas (& cas) specific rq, sd and task stats 2017-06-02 08:01:54 -07:00
energy.c sched: Support for extracting EAS energy costs from DT 2016-05-10 16:49:53 +08:00
fair.c sched: EAS/WALT: Don't take into account of running task's util 2017-12-13 17:01:03 +00:00
features.h sched: Add Kconfig option DEFAULT_USE_ENERGY_AWARE to set ENERGY_AWARE feature flag 2016-09-21 14:29:06 -07:00
idle.c vmstat: make vmstat_updater deferrable again and shut down on idle 2016-08-11 14:26:53 -07:00
idle_task.c FROMLIST: sched/fair: Use wake_q length as a hint for wake_wide 2017-10-27 18:50:59 +00:00
loadavg.c sched/loadavg: Avoid loadavg spikes caused by delayed NO_HZ accounting 2017-07-05 14:37:21 +02:00
Makefile BACKPORT: kernel: add kcov code coverage 2017-12-18 09:41:57 -08:00
rt.c This is the 4.4.103 stable release 2017-11-30 15:43:08 +00:00
sched.h This is the 4.4.103 stable release 2017-11-30 15:43:08 +00:00
stats.c schedstats/eas: guard properly to avoid breaking non-smp schedstats users 2017-06-03 15:03:03 +01:00
stats.h sched/stat: Simplify the sched_info accounting dependency 2015-07-04 10:04:30 +02:00
stop_task.c FROMLIST: sched/fair: Use wake_q length as a hint for wake_wide 2017-10-27 18:50:59 +00:00
tune.c Revert "ANDROID: sched/tune: Initialize raw_spin_lock in boosted_groups" 2017-09-23 01:25:03 +00:00
tune.h sched/tune: Introducing a new schedtune attribute prefer_idle 2016-08-11 14:26:49 -07:00
wait.c sched/wait: Fix the signal handling fix 2015-12-13 14:30:59 -08:00
walt.c sched: walt: Correct WALT window size initialization 2017-10-27 11:58:48 -07:00
walt.h sched: walt: Correct WALT window size initialization 2017-10-27 11:58:48 -07:00