android_kernel_oneplus_msm8998/arch/x86/kernel
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
..
acpi UPSTREAM: x86/suspend: fix false positive KASAN warning on suspend/resume 2017-12-14 08:24:41 -08:00
apic BACKPORT: kernel: add kcov code coverage 2017-12-18 09:41:57 -08:00
cpu BACKPORT: kernel: add kcov code coverage 2017-12-18 09:41:57 -08:00
fpu x86/fpu: Don't let userspace set bogus xcomp_bv 2017-10-05 09:41:47 +02:00
kprobes BACKPORT: kprobes: Unpoison stack in jprobe_return() for KASAN 2017-12-14 08:22:14 -08:00
.gitignore
alternative.c x86/alternatives: Make optimize_nops() interrupt safe and synced 2015-09-03 21:27:47 +02:00
amd_gart_64.c
amd_nb.c x86/amd_nb: Fix boot crash on non-AMD systems 2016-07-27 09:47:29 -07:00
apb_timer.c x86/asm/tsc: Rename native_read_tsc() to rdtsc() 2015-07-06 15:23:28 +02:00
aperture_64.c
apm_32.c apm32: Fix cputime == jiffies assumption 2015-07-29 15:44:58 +02:00
asm-offsets.c Merge branch 'x86-headers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2015-11-03 21:05:40 -08:00
asm-offsets_32.c
asm-offsets_64.c x86/asm/entry: (Re-)rename __NR_entry_INT80_compat_max to __NR_syscall_compat_max 2015-06-08 23:43:38 +02:00
audit_64.c
bootflag.c x86: don't use module_init for non-modular core bootflag code 2015-06-16 14:12:34 -04:00
check.c Linux 4.2-rc8 2015-08-25 09:59:19 +02:00
cpuid.c x86: Drop bogus __ref / __refdata annotations 2015-07-20 18:57:20 +02:00
crash.c x86/kexec: Remove obsolete 'in_crash_kexec' flag 2015-10-12 09:43:11 +02:00
crash_dump_32.c
crash_dump_64.c
devicetree.c Replace module_init with equivalent device_initcall in non modules. 2015-07-02 10:30:48 -07:00
doublefault.c
dumpstack.c
dumpstack_32.c
dumpstack_64.c
e820.c x86/e820: Don't merge consecutive E820_PRAM ranges 2016-10-28 03:01:33 -04:00
early-quirks.c drm/i915: Account for TSEG size when determining 865G stolen base 2016-10-31 04:13:58 -06:00
early_printk.c x86/early_printk: Set __iomem address space for IO 2015-10-13 21:45:56 +02:00
espfix_64.c Merge branch 'x86/urgent' into x86/asm, before applying dependent patches 2015-07-31 10:23:35 +02:00
ftrace.c This is the 4.4.66 stable release 2017-05-03 08:50:11 -07:00
head.c
head32.c
head64.c x86/kasan: Fix boot with KASAN=y and PROFILE_ANNOTATED_BRANCHES=y 2017-03-22 12:04:18 +01:00
head_32.S x86/traps: Ignore high word of regs->cs in early_fixup_exception() 2016-12-08 07:15:24 +01:00
head_64.S x86/cpu: Call verify_cpu() after having entered long mode too 2015-11-07 10:45:02 +01:00
hpet.c x86/hpet: Prevent might sleep splat on resume 2017-12-16 10:33:51 +01:00
hw_breakpoint.c perf/x86/hw_breakpoints: Fix check for kernel-space breakpoints 2015-08-04 10:16:55 +02:00
i386_ksyms_32.c BACKPORT: x86/hweight: Get rid of the special calling convention 2017-10-09 14:00:13 -07:00
i8237.c
i8253.c clockevents/drivers/i8253: Migrate to new 'set-state' interface 2015-08-10 11:40:30 +02:00
i8259.c x86/irq: Probe for PIC presence before allocating descs for legacy IRQs 2015-11-07 10:37:37 +01:00
io_delay.c
ioport.c x86/iopl: Fix iopl capability check on Xen PV 2016-04-12 09:08:38 -07:00
irq.c x86/irq: Call irq_force_move_complete with irq descriptor 2016-03-03 15:07:31 -08:00
irq_32.c genirq: Remove irq argument from irq flow handlers 2015-09-16 15:47:51 +02:00
irq_64.c x86/irq: Drop unlikely before IS_ERR_OR_NULL 2015-10-01 11:08:56 +02:00
irq_work.c treewide: Remove old email address 2015-11-23 09:44:58 +01:00
irqinit.c x86/irq: Store irq descriptor in vector array 2015-08-06 00:14:59 +02:00
jump_label.c jump_label: Rename JUMP_LABEL_{EN,DIS}ABLE to JUMP_LABEL_{JMP,NOP} 2015-08-03 11:34:12 +02:00
kdebugfs.c
kexec-bzimage64.c Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security 2015-09-08 12:41:25 -07:00
kgdb.c UPSTREAM: x86/mm: Always enable CONFIG_DEBUG_RODATA and remove the Kconfig option 2016-09-23 10:04:06 -07:00
ksysfs.c
kvm.c KVM: async_pf: make rcu irq exit if not triggered from idle task 2017-08-11 09:08:47 -07:00
kvmclock.c x86: kvmclock: abolish PVCLOCK_COUNTS_FROM_ZERO 2015-10-01 15:06:42 +02:00
ldt.c x86/entry: Use SYSCALL_DEFINE() macros for sys_modify_ldt() 2017-12-09 18:42:40 +01:00
livepatch.c livepatch: Fix crash with !CONFIG_DEBUG_SET_MODULE_RONX 2015-11-06 11:10:03 +01:00
machine_kexec_32.c
machine_kexec_64.c Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching 2015-06-23 14:07:26 -07:00
Makefile BACKPORT: mm, kasan: stackdepot implementation. Enable stackdepot for SLAB 2017-12-14 08:19:45 -08:00
mcount_64.S ftrace/x86: Set ftrace_stub to weak to prevent gcc from using short jumps to it 2017-01-26 08:23:46 +01:00
mmconf-fam10h_64.c
module.c
mpparse.c
msr.c
nmi.c Merge branch 'x86/urgent' into x86/asm, before applying dependent patches 2015-07-31 10:23:35 +02:00
nmi_selftest.c
paravirt-spinlocks.c
paravirt.c x86/paravirt: Do not trace _paravirt_ident_*() functions 2016-09-24 10:07:37 +02:00
paravirt_patch_32.c x86/asm/tsc, x86/paravirt: Remove read_tsc() and read_tscp() paravirt hooks 2015-07-06 15:23:26 +02:00
paravirt_patch_64.c Merge branch 'locking/core' into x86/core, to prepare for dependent patch 2015-06-03 10:07:35 +02:00
pci-calgary_64.c x86/pci-calgary: Fix iommu_free() comparison of unsigned expression >= 0 2017-05-14 13:32:55 +02:00
pci-dma.c mm, page_alloc: distinguish between being unable to sleep, unwilling to sleep and avoiding waking kswapd 2015-11-06 17:50:42 -08:00
pci-iommu_table.c
pci-nommu.c
pci-swiotlb.c x86/swiotlb: Try coherent allocations with __GFP_NOWARN 2015-06-11 08:28:38 +02:00
pcspeaker.c
perf_regs.c
pmem.c libnvdimm, e820: skip module loading when no type-12 2015-11-30 09:10:33 -08:00
probe_roms.c
process.c Move x86_64 idle notifiers to generic 2016-02-16 13:52:32 -08:00
process_32.c sched/core, sched/x86: Kill thread_info::saved_preempt_count 2015-10-06 17:08:18 +02:00
process_64.c x86/iopl/64: Properly context-switch IOPL on Xen PV 2016-04-12 09:08:38 -07:00
ptrace.c x86/dumpstack: Fix x86_32 kernel_stack_pointer() previous stack access 2016-10-16 17:36:15 +02:00
pvclock.c pvclock: Add CPU barriers to get correct version value 2016-08-10 11:49:27 +02:00
quirks.c timers/x86/hpet: Type adjustments 2015-10-21 11:17:32 +02:00
reboot.c x86/reboot/quirks: Add iMac10,1 to pci_reboot_dmi_table[] 2016-01-31 11:28:54 -08:00
reboot_fixups_32.c
relocate_kernel_32.S
relocate_kernel_64.S
resource.c
rtc.c x86/paravirt: Prevent rtc_cmos platform device init on PV guests 2015-12-19 21:35:13 +01:00
setup.c x86/mm/pat: Don't report PAT on CPUs that don't support it 2017-07-15 11:57:50 +02:00
setup_percpu.c
signal.c x86/signal: Fix restart_syscall number for x32 tasks 2015-12-05 18:52:14 +01:00
signal_compat.c x86/compat: Move copy_siginfo_*_user32() to signal_compat.c 2015-07-06 15:28:55 +02:00
smp.c x86/mce: Clear Local MCE opt-in before kexec 2015-08-13 10:12:52 +02:00
smpboot.c x86 smpboot: Re-enable init_udelay=0 by default on modern CPUs 2015-11-25 23:17:48 +01:00
stacktrace.c
step.c Merge branch 'x86/urgent' into x86/asm to fix up conflicts and to pick up fixes 2015-08-18 09:39:47 +02:00
sys_x86_64.c mm: larger stack guard gap, between vmas 2017-06-26 07:13:11 +02:00
sysfb.c
sysfb_efi.c x86/sysfb_efi: Fix valid BAR address range check 2016-05-11 11:21:20 +02:00
sysfb_simplefb.c
tboot.c
tce_64.c
test_nx.c UPSTREAM: x86/mm: Always enable CONFIG_DEBUG_RODATA and remove the Kconfig option 2016-09-23 10:04:06 -07:00
test_rodata.c UPSTREAM: x86/mm: Always enable CONFIG_DEBUG_RODATA and remove the Kconfig option 2016-09-23 10:04:06 -07:00
time.c
tls.c
tls.h
topology.c x86: Drop bogus __ref / __refdata annotations 2015-07-20 18:57:20 +02:00
trace_clock.c x86/asm/tsc: Add rdtsc_ordered() and use it in trivial call sites 2015-07-06 15:23:29 +02:00
tracepoint.c
traps.c Revert "x86/mm: Expand the exception table logic to allow new handling options" 2016-10-31 19:56:26 -06:00
tsc.c perf/x86: Fix time_shift in perf_event_mmap_page 2015-10-20 10:30:52 +02:00
tsc_msr.c x86/tsc: Read all ratio bits from MSR_PLATFORM_INFO 2016-05-11 11:21:19 +02:00
tsc_sync.c x86/asm/tsc/sync: Use rdtsc_ordered() in check_tsc_warp() and drop extra barriers 2015-07-06 15:23:29 +02:00
uprobes.c uprobes/x86: Fix RIP-relative handling of EVEX-encoded instructions 2016-09-07 08:32:36 +02:00
verify_cpu.S x86/cpu: Call verify_cpu() after having entered long mode too 2015-11-07 10:45:02 +01:00
vm86_32.c x86/vm86: Block non-root vm86(old) if mmap_min_addr != 0 2015-09-05 09:01:16 +02:00
vmlinux.lds.S UPSTREAM: arch, ftrace: for KASAN put hard/soft IRQ entries into separate sections 2017-12-14 08:26:37 -08:00
vsmp_64.c x86: replace __init_or_module with __init in non-modular vsmp_64.c 2015-06-16 14:12:41 -04:00
x86_init.c PCI changes for the v4.2 merge window: 2015-06-23 13:41:24 -07:00
x8664_ksyms_64.c BACKPORT: x86/hweight: Get rid of the special calling convention 2017-10-09 14:00:13 -07:00