Examining the Linux kernel safely with QEMU and GDB
Summary
On Hackaday, Al Williams presents the Python script easylkb by deepseagirl, which downloads, configures, builds and boots a Linux kernel in QEMU. It also creates a Debian image and opens SSH and a GDB interface on the local machine. This makes it possible to examine a kernel without putting the working system at risk.
The steps as switches
Each build step has its own switch: -k selects a kernel version, -p an existing source tree, -d downloads, -c configures, -m compiles, -i builds the image and -r runs it. -a does everything at once, for example easylkb -k 6.2 -a. With --kconfig you append your own options to the configuration; the bundled example.KConfig sets debug options for QEMU.
According to Williams, create-image.sh must be executable, otherwise packaging the image fails. Configuration follows the usual route with make menuconfig in the downloaded source tree.
Debugging in the running guest
According to the README, easylkb uses debootstrap to create a Debian 11 image. The guest is reachable via SSH on port 10021 as root without a password, and GDB connects with target remote :1234. Before that, the kernel's scripts/gdb/vmlinux-gdb.py must be added to ~/.gdbinit as a safe path; lx-symbols then loads the symbols.
From kernel 6.8, the project's known issues say DEBUG_INFO_DWARF5 may be needed in the configuration.
Ideas
- One script bundles download, configuration, build and boot of a test kernel.
- QEMU separates kernel experiments from your own working system.
- Individual switches allow repeating only certain build steps.
- Custom configuration fragments extend the default configuration in a targeted way.
- GDB attaches directly to the running kernel through QEMU.
- The kernel's GDB scripts provide commands such as lx-symbols.
Insights
- Good tools lower the entry barrier more than good explanations.
- A disposable guest turns risky experiments into routine.
Facts
- easylkb needs python3, gcc, make, qemu and debootstrap, among others.
- The guest is reachable via SSH on localhost port 10021.
- QEMU's GDB stub listens on port 1234.
- The project first appeared in tmp.0ut issue 3.
References
- Hackaday: Easy Kernel Debugging
- deepseagirl: easylkb (GitHub)
- Linux kernel documentation: Debugging kernel and modules via gdb
Critique
- Hackaday speaks of a Debian package; in fact easylkb builds a bootable Debian image.
Remarks
- The generated image is based on Debian 11 (Bullseye), now two releases behind current Debian.
Recommendations
- Boot the guest with nokaslr so GDB finds the kernel symbols at the expected addresses.
- Check that port 10021 listens on localhost only, because root logs in there without a password.
- Keep a separate source tree for each kernel version you examine.
Found a mistake or have something to add? Get in touch.
Links to the original source and the Web Archive open in a new tab.