Skip to content
Naveen Naidu

Panic the Kernel!! 🙀

— lkmp, linux, kernel, panic — 1 min read

Hallo Robots o/

I was feeling a bit adventurous today so I wanted to go ahead and try to panic the kernel. I mean, the Linux Kernel is so robust that I have never seen it panic until now in normal day to day usage, but I am wondering what does it look like when kernel does panic 🎃

NOTE: Running the following codes, WILL CRASH YOUR SYSTEM. An easy poweroff poweron should fix it though :)

There are two way that I was able to figure out, which can help us panic our kernels.

1. Using panic() from a custom kernel module

In this approach, we will create a custom kernel module and use the kernel's panic() function to panic the kernel.

1. Set things up
1mkdir panic-module
2cd panic-module
3touch panic.c Makefile

2. Create the custom kernel module

1/*
2 * panic_module.c -- A Kernel module that panics the kernel
3 */
4#include<linux/init.h>
5#include<linux/module.h>
6#include<linux/panic.h>
7
8static int __init panic_module_init(void)
9{
10 printk(KERN_INFO KBUILD_MODNAME ": Panic Module Init.\n");
11
12 /* Panic the kernel using the panic() function */
13 panic("panic_module: Kernel panicked due to panic().\n");
14
15 printk(KERN_INFO KBUILD_MODNAME ": Panic module: %d\n", y);
16
17 return 0;
18}
19
20static void __exit panic_module_cleanup(void)
21{
22 printk(KERN_INFO KBUILD_MODNAME ": Panic Module Cleanup.\n");
23}
24
25/* Register the module */
26module_init(panic_module_init);
27module_exit(panic_module_cleanup);
28
29/* Information regarding the module */
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Naveen Naidu");
32MODULE_DESCRIPTION("A Kernel Module to panic te kernel");

3. Create our Makefile

1obj-m := panic_module.o
2KDIR := /lib/modules/$(shell uname -r)/build
3
4all:
5 $(MAKE) -C $(KDIR) M=$(PWD) modules
6clean:
7 $(MAKE) -C $(KDIR) M=$(PWD) clean

4. Time to panic the kernel

Now that we have everything set up, let's go panic the kernel :D

WARNING: DOING THE FOLLOWING WILL CRASH THE KERNEL, SAVE EVERYTHING BEFORE TRYING Restarting the system will fix everything :)

1cd panic-module
2make all
3sudo insmod panic_module

TADA! Your kernel has crashed :P



2. Panicking the kernel using CONFIG_PANIC_ON_OOPS

I was wondering if there were any other ways using which we could panic the kernel.

After a bit of creative searching :P, I figured that kernel has a configuration option called as CONFIG_PANIC_ON_OOPS which will panic the kernel whenever any OOPS happens.

So I was wondering, if we switch on these options, and make our panic-module.c file create an OOPS instead of using the panic() then we can cause the panic.

And guess what! That does indeed work \o/

But! Note that for this to work, you would have to compile the mainline kernel with CONFIG_PANIC_ON_OOPS_VALUE=1 and CONFIG_PANIC_ON_OOPS=y and boot into that kernel and then load the following module.

This is a very time intensive work though :(

But for the highly motivated, I'll still provide the updated panic_module.c file :)

1/*
2 * panic_module.c -- A Kernel module that panics the kernel
3 */
4#include<linux/init.h>
5#include<linux/module.h>
6#include<linux/panic.h>
7
8static int __init panic_module_init(void)
9{
10 printk(KERN_INFO KBUILD_MODNAME ": Panic Module Init.\n");
11
12 /* Panic the kernel by dereferencing a NULL pointer.
13 * Note that, this would only cause an OOPS. To panic the kernel on OOPS
14 * you would also have to set `CONFIG_PANIC_ON_OOPS=y` and set the
15 * `CONFIG_PANIC_ON_OOPS_VALUE=1`.
16 */
17 int *p = NULL;
18 int y = *p;
19
20 printk(KERN_INFO KBUILD_MODNAME ": Panic module: %d\n", y);
21
22 return 0;
23}
24
25static void __exit panic_module_cleanup(void)
26{
27 printk(KERN_INFO KBUILD_MODNAME ": Panic Module Cleanup.\n");
28}
29
30/* Register the module */
31module_init(panic_module_init);
32module_exit(panic_module_cleanup);
33
34/* Information regarding the module */
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Naveen Naidu");
37MODULE_DESCRIPTION("A Kernel Module to panic te kernel");

Once you make the above module using the Makefile provided in the Approach 1, and insert the module. You'll observe something like the following:

PANIC output



That's it for now folks!
:wq