LINUX KERNEL BUG HUNTINGLINUX KERNEL BUG HUNTING
Andrea Righi
<andrea.righi@canonical.com>
Twitter: @arighi
LINUX KERNEL IS COMPLEXLINUX KERNEL IS COMPLEX
26,947,416 lines of code right now
438 patches last week
813 files changed, 7714 insertions(+), 31289
deletions(-)
find -type f -name '*.[chS]' -exec wc -l {} ; | 
awk 'BEGIN{sum=0}{sum+=$1}END{print sum}'
git log --oneline v5.10-rc1..v5.10-rc2 | wc -l
git diff --stat v5.10-rc1..v5.10-rc2 | tail -1
PREPARING THE ENVIRONMENTPREPARING THE ENVIRONMENT
Development
git, vim, ctags, mutt, b4
Build
make, gcc
Testing
qemu, virtme, uvt-kvm, tmux
Debugging
gdb, crash, bp race
GET THE SOURCE CODEGET THE SOURCE CODE
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torval
KERNEL BUGSKERNEL BUGS
Kernel panic
Fatal error, system becomes unusable
Kernel oops
Non-fatal error
Wrong result
Fata error from user perspective
Security vulnerability
Data leakage
Performance regression
Everything correct, but slower
CRASHING THE KERNELCRASHING THE KERNEL
DEBUGGING TECHNIQUESDEBUGGING TECHNIQUES
blinking LED
printk() / dump_stack()
procfs
debugger (i.e., kgbd)
in-kernel tools (i.e., lockdep)
virtualization
profiling / tracing
PROFILING VS TRACINGPROFILING VS TRACING
Profiling
Tracing
Periodic timed interrupt that collects current
program counter, function address, stack back
trace
Record invocations of a specific event
PROFILING EXAMPLEPROFILING EXAMPLE
perf top
TRACING EXAMPLETRACING EXAMPLE
strace: system call tracer in Linux
It uses ptrace() syscall that pauses the targer
process any time it executes a syscall so that the
debugger can read the state
... and it's doing it twice: when the syscall begins
and when it ends!
STRACE OVERHEADSTRACE OVERHEAD
### Regular execution
$ dd if=/dev/zero of=/dev/null bs=1 count=500k
512000+0 records in
512000+0 records out
512000 bytes (512 kB, 500 KiB) copied, 0.147195 s, 3.5 MB/s
### Strace execution (tracing a syscall that is never called)
$ strace -e trace=bind 
dd if=/dev/zero of=/dev/null bs=1 count=500k
512000+0 records in
512000+0 records out
512000 bytes (512 kB, 500 KiB) copied, 8.0567 s, 63.5 kB/s
+++ exited with 0 +++
eBPFeBPF
Highly efficeint VM that lives in the kernel
Inject safe bytecode into the kernel that runs in a
sandbox
In-kernel JIT compiler
Can use kprobe to attach code to kernel functions
Access kernel memory without the risk of
crashing, hanging or breaking the system
KPROBESKPROBES
>
BTF & CO-REBTF & CO-RE
BPF type format
Metadata format which encodes debugging
information of kernel data structures
CO-RE
BPF Compile-Once Run-Everywhere
Allows eBPF bytecode to be relocatable
http://www.brendangregg.com/blog/2020-11-
04/bpf-co-re-btf-libbpf.html
eBPFeBPF TRACING TOOLSTRACING TOOLS
BPF Compiler Collection
git://github.com/iovisor/bcc.git
(deprecated)
bp race
git://github.com/iovisor/bp race.git
FLAME GRAPHSFLAME GRAPHS
EXAMPLESEXAMPLES
keylogger in eBPF
tracing 'ping' with eBPF
tracing task wait / wakeup events
KEYLOGGER WITH BPFTRACEKEYLOGGER WITH BPFTRACE
KEYLOGGER WITH BPFTRACE:KEYLOGGER WITH BPFTRACE:
COMMANDSCOMMANDS
sudo bpftrace -e 'kprobe:*interrupt* { @[probe] = count() }'
sudo bpftrace -e 'kprobe:atkbd_interrupt
{ printf("%xn", arg1) }'
TRACING 'PING' WITH BPFTRACETRACING 'PING' WITH BPFTRACE
TRACING 'PING' WITH BPFTRACE:TRACING 'PING' WITH BPFTRACE:
COMMANDSCOMMANDS
sudo bpftrace -e 'kprobe:*icmp* { @[probe] = count(); }'
sudo bpftrace -e 'kprobe:icmp_out_count
{ printf("%s: type=%d%sn", probe, arg1, kstack); }'
TRACING TASK WAIT / WAKUPTRACING TASK WAIT / WAKUP
EVENTS WITH BPFTRACEEVENTS WITH BPFTRACE
#!/usr/bin/env bpftrace
#include <linux/sched.h>
BEGIN
{
printf("Tracing off-CPU time (us) with waker stacks. Ctrl-C
}
kprobe:try_to_wake_up
{
$p = (struct task_struct *)arg0;
@waker[$p->pid] = kstack;
}
CONCLUSIONCONCLUSION
Virtualization is your friend to speed up kernel
development
Tracing is your friend to understand how the kernel
works
Kernel development can be fun!
REFERENCESREFERENCES
https://git.launchpad.net/~arighi/+git/kernel-tools
http://arighi.blogspot.com/2019/08/kerneldebuggingusingqemu.html
https://github.com/arighi/virtme
http://www.brendangregg.com/BPF/bp race-cheat-sheet.html
https://github.com/iovisor/bcc
THANK YOUTHANK YOU
Questions?
Andrea Righi
<andrea.righi@canonical.com>
twitter: @arighi

Linux kernel bug hunting