Skip to content

Instantly share code, notes, and snippets.

@worldOneo
Last active August 18, 2024 18:20
Show Gist options
  • Save worldOneo/c59cea82dec244966b82cb9312721198 to your computer and use it in GitHub Desktop.
Save worldOneo/c59cea82dec244966b82cb9312721198 to your computer and use it in GitHub Desktop.
Minimal kernel module to quickly introduce kernel crashes for durability testing
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
static char test_buffer[4];
static ssize_t crash_kernel(struct file* _file, const char* _buff, size_t _length, loff_t* _offset) {
if(_length < 4) {
return 0;
}
if(copy_from_user(test_buffer, _buff, 4)) {
return -EFAULT;
}
if(*((int*)(test_buffer)) != 0xdeadbeef) {
return 1;
}
panic("This is planned!");
}
static const struct proc_ops crasher = {
.proc_write = crash_kernel,
};
static int __init kernel_crasher_init(void) {
printk("Creating evil proc file.\n");
proc_create("PANIC_IF_WRITTEN_TO", 0, NULL, &crasher);
return 0;
}
static void __exit kernel_crasher_exit(void) {
printk("Removing evil proc file.\n");
remove_proc_entry("PANIC_IF_WRITTEN_TO", NULL);
}
MODULE_LICENSE("GPL");
module_init(kernel_crasher_init);
module_exit(kernel_crasher_exit);
ifneq ($(KERNELRELEASE),)
obj-m := kernel_crasher.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment