0

I am trying to debug the Ubuntu kernel using KDB + qemu. However, I am not able to debug it. Following is the error I am getting. Can someone help?

Starting QEMU:

sudo qemu-system-x86_64 -enable-kvm -s -machine type=pc,accel=kvm -cpu host -nographic  -k de -usb -m 2048  -net nic -net user,hostfwd=tcp::3389-:22  ubuntu16.04.qcow2

Starting Debugger:

sudo gdb ./debian/build/build-generic/vmlinux -iex 'add-auto-load-safe-path .'  -ex 'target remote localhost:1234'

Setting break point:

gdb-peda$ b printk
Breakpoint 1 at 0xffffffff811ad8f3: file /home/test/ubuntu/linux-hwe-4.10.0/kernel/printk/printk.c, line 1864.
gdb-peda$ c
Continuing.
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0xffffffff811ad8f3
Command aborted.
gdb-peda$  disassemble printk
Dump of assembler code for function printk:
   0xffffffff811ad8f3 <+0>: Cannot access memory at address 

What am I doing wrong here?

0

Make sure you have disabled KASLR (Kernel address space layout randomization) or use add-symbol-file to add kernel symbol file instead of file, so that you can manually specify .text .data and .bss.

KASLR make exploits harder by placing various objects at random, rather than fixed, addresses.

To disable KASLR, append "nokaslr" parameter to GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub file and do update-grub. e.g.

$ grep "GRUB_CMDLINE_LINUX_DEFAULT" /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="nokaslr"
$ update-grub && reboot
0

As @vg598 said it's about KASLR, but using update-grub you will update everything including KGDB options in the grub.cfg file.

A better approach is to manually modify /boot/grub/grub.conf adding the 'nokaslr' flag before or after the KGDB options.

0

Like avg598 said, Kernel address space randomization KASLR needs to be disabled for debugging.

Quick solution:

Alternative:

  • Disable CONFIG_RANDOMIZE_BASE in the kernel configuration and rebuild the kernel. (E.g. if you're using a special kernel .config for debugging anyway, this might be more convenient than the kernel bootline.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.