Turning off kASLR to debug linux kernel using qemu and gdb

9,923

Solution 1

Kernel boot parameters can be set temporarily per boot or always via some configuration file; how this is done depends on the bootloader which for current versions of Ubuntu is grub2;

$ grep GRUB_CMDLINE_LINUX_DEFAULT /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet"
$ sudo perl -i -pe 'm/quiet/ and s//quiet nokaslr/' /etc/default/grub
$ grep quiet /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet nokaslr"
$ sudo update-grub

and then reboot; confirm at the grub menu that the parameters appear as expected.

Solution 2

qemu-system-x86_64 -append nokaslr

This is the way to go if you are using QEMU itself to load the kernel with -kernel, as opposed to having a bootloader inside the disk image, as you would do e.g. with an Ubuntu ISO installer.

The nokasrl Linux command line parameter disables KASRL.

This has been needed since v4.12, when KASLR was turned on by default.

Here is a highly automated Buildroot example that uses it.

Share:
9,923

Related videos on Youtube

Winston
Author by

Winston

Updated on September 18, 2022

Comments

  • Winston
    Winston over 1 year

    I'm trying to debug Linux kernel using qemu and gdb. The problem is that gdb won't stop at the breakpoint. I've searched about it and found that turning kASLR off may help because kASLR confuses gdb.

    -- Install that kernel on the guest.

    +- Install that kernel on the guest, turn off KASLR by adding "nokaslr" to the kernel command line .

    Unfortunately, I don't know what it means to add nokaslr to the command line and the way to do that. Any ideas would be appreciated.

    • Winston
      Winston over 6 years
      I added "nokaslr" as it says in the link and the kernel boots normally. Therefore, it must be the right way. Thanks.