linux/drivers
Dmitry Vyukov 5c9a8750a6 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>
2016-03-22 15:36:02 -07:00
..
accessibility
acpi libnvdimm for 4.6 2016-03-16 17:45:56 -07:00
amba
android
ata Merge branch 'for-4.6' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/libata 2016-03-18 20:06:46 -07:00
atm Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial 2016-03-17 21:38:27 -07:00
auxdisplay
base Merge branch 'for-linus' of git://ftp.arm.linux.org.uk/~rmk/linux-arm 2016-03-19 16:31:54 -07:00
bcma
block virtio/vhost: new features, performance improvements, cleanups 2016-03-20 13:28:18 -07:00
bluetooth
bus arm[64] perf updates for 4.6: 2016-03-21 13:14:16 -07:00
cdrom
char ipmi/watchdog: use nmi_panic() when kernel panics in NMI handler 2016-03-22 15:36:02 -07:00
clk ARM: SoC driver updates for v4.6 2016-03-20 15:40:32 -07:00
clocksource One of the largest releases for KVM... Hardly any generic improvement, 2016-03-16 09:55:35 -07:00
connector
cpufreq ARM: SoC driver updates for v4.6 2016-03-20 15:40:32 -07:00
cpuidle
crypto Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial 2016-03-17 21:38:27 -07:00
dca
devfreq
dio
dma ARM: SoC platform updates for v4.6 2016-03-20 14:57:08 -07:00
dma-buf
edac EDAC queue for 4.6 2016-03-16 08:36:55 -07:00
eisa
extcon
firewire firewire: use in_compat_syscall to check ioctl compatness 2016-03-22 15:36:02 -07:00
firmware kernel: add kcov code coverage 2016-03-22 15:36:02 -07:00
fmc
fpga
gpio - New Drivers 2016-03-18 10:15:11 -07:00
gpu drivers/gpu/drm/amd/amdkfd: use in_compat_syscall to check open() caller type 2016-03-22 15:36:02 -07:00
hid drivers/hid/uhid.c: check write() bitness using in_compat_syscall 2016-03-22 15:36:02 -07:00
hsi
hv Char/Misc patches for 4.6-rc1 2016-03-17 13:47:50 -07:00
hwmon ARM: SoC driver updates for v4.6 2016-03-20 15:40:32 -07:00
hwspinlock
hwtracing
i2c
ide ide: hpt366: convert to use match_string() helper 2016-03-17 15:09:34 -07:00
idle
iio - New Drivers 2016-03-18 10:15:11 -07:00
infiniband Merge branch 'mm-pkeys-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2016-03-20 19:08:56 -07:00
input input: redefine INPUT_COMPAT_TEST as in_compat_syscall() 2016-03-22 15:36:02 -07:00
iommu Merge branch 'mm-pkeys-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2016-03-20 19:08:56 -07:00
ipack
irqchip
isdn Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next 2016-03-19 10:05:34 -07:00
leds leds: triggers: simplify led_trigger_store 2016-03-14 09:22:23 +01:00
lguest
lightnvm Merge branch 'for-4.6/drivers' of git://git.kernel.dk/linux-block 2016-03-18 17:13:31 -07:00
macintosh
mailbox Merge branch 'mailbox-for-next' of git://git.linaro.org/landing-teams/working/fujitsu/integration 2016-03-20 12:58:32 -07:00
mcb
md Merge tag 'md/4.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shli/md 2016-03-21 14:18:10 -07:00
media [media] vsp1: use proper dma alloc/free functions 2016-03-21 13:49:01 -07:00
memory
memstick
message
mfd - New Drivers 2016-03-18 10:15:11 -07:00
misc Merge branch 'mm-pkeys-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2016-03-20 19:08:56 -07:00
mmc MMC core: 2016-03-21 14:35:52 -07:00
mtd This is the bulk of GPIO changes for kernel v4.6: 2016-03-17 21:05:32 -07:00
net rapidio/rionet: add mport removal handling 2016-03-22 15:36:02 -07:00
nfc Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next 2016-03-19 10:05:34 -07:00
ntb
nubus
nvdimm Merge branch 'for-linus' of git://ftp.arm.linux.org.uk/~rmk/linux-arm 2016-03-19 16:31:54 -07:00
nvme Merge branch 'for-4.6/drivers' of git://git.kernel.dk/linux-block 2016-03-18 17:13:31 -07:00
nvmem
of DeviceTree updates for 4.6: 2016-03-19 15:15:07 -07:00
oprofile
parisc PCI changes for the v4.6 merge window: 2016-03-16 14:45:55 -07:00
parport
pci powerpc updates for 4.6 2016-03-19 15:38:41 -07:00
pcmcia
perf drivers/perf: arm_pmu: avoid NULL dereference when not using devicetree 2016-03-21 11:36:17 +00:00
phy
pinctrl Merge branch 'akpm' (patches from Andrew) 2016-03-18 19:26:54 -07:00
platform Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux 2016-03-21 13:48:00 -07:00
pnp
power Merge branch 'akpm' (patches from Andrew) 2016-03-18 19:26:54 -07:00
powercap
pps
ps3
ptp Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2016-03-15 12:13:56 -07:00
pwm
rapidio rapidio: add mport char device driver 2016-03-22 15:36:02 -07:00
ras
regulator - New Drivers 2016-03-18 10:15:11 -07:00
remoteproc
reset
rpmsg
rtc Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial 2016-03-17 21:38:27 -07:00
s390 virtio/vhost: new features, performance improvements, cleanups 2016-03-20 13:28:18 -07:00
sbus
scsi Merge branch 'mm-pkeys-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2016-03-20 19:08:56 -07:00
sfi
sh
sn
soc ARM: SoC driver updates for v4.6 2016-03-20 15:40:32 -07:00
spi dmaengine updates for 4.6 2016-03-17 12:34:54 -07:00
spmi
ssb
staging staging/lustre: switch from is_compat_task to in_compat_syscall 2016-03-22 15:36:02 -07:00
target Configfs changes for the 4.6 merge window: 2016-03-17 16:25:46 -07:00
tc
thermal
thunderbolt
tty
uio
usb ARM: SoC driver updates for v4.6 2016-03-20 15:40:32 -07:00
uwb
vfio VFIO updates for v4.6-rc1 2016-03-17 13:05:09 -07:00
vhost
video Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux 2016-03-21 13:48:00 -07:00
virt
virtio virtio/vhost: new features, performance improvements, cleanups 2016-03-20 13:28:18 -07:00
vlynq
vme
w1
watchdog hpwdt: use nmi_panic() when kernel panics in NMI handler 2016-03-22 15:36:02 -07:00
xen xen_balloon: support memory auto onlining policy 2016-03-15 16:55:16 -07:00
zorro
Kconfig
Makefile