Skip to content

Instantly share code, notes, and snippets.

@mezcel
Last active December 18, 2019 02:06
Show Gist options
  • Select an option

  • Save mezcel/19e90c58200ec60f970ef80464866700 to your computer and use it in GitHub Desktop.

Select an option

Save mezcel/19e90c58200ec60f970ef80464866700 to your computer and use it in GitHub Desktop.
trying out kernel module

practice-kernel

gist

An example kernel plugin using C on the Linux kernel.

It is a corny Rickroll demo.

  • The initial rickroll.c script was taken from the following Youtube walkthrough tutorial.
#include <linux/module.h>
#if defined(CONFIG_SMP)
#define __SMP__
#endif
#if defined(CONFIG_MODVERSIONS)
#define MODVERSIONS
#include <linux/modversions.h>
#endif
#include <linux/kernel.h>
static __init int init_module(void) {
printk(KERN_DEBUG "Hello, kernel!\n");
return 0;
}
static __init void cleanup_module(void) {
print(KERN_DEBUG "Good-bye, kernel!\n");
}
obj-m += rickroll.o
# sudo apt-get install linux-headers-$(uname -r)
# compile rickroll.c
# sudo insmod rickroll.ko
# sudo tail -f /var/log/kern.log
# moduleNumber=$(sudo grep 'Rickoll module has been loaded:' /var/log/kern.log | tail -n1 | awk '{printf $13}')
# cd /dev/
# sudo mknod rickroll c $moduleNumber 0
# sudo cat /dev/rickroll
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
linuxheaders:
sudo apt install linux-headers-$(uname -r)
insertmod:
sudo insmod rickroll.ko
sudo tail -f /var/log/kern.log
removemod:
sudo rmmod rickroll
makenode:
#cd /dev/
sudo mknod rickroll c $(sudo grep 'Rickoll module has been loaded:' /var/log/kern.log | tail -n1 | awk '{printf $13}') 0
run:
sudo cat /dev/rickroll
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
/*
* https://github.com/engineer-man/youtube/blob/master/062/rickroll.c
* */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#define DEVICE_NAME "rickroll"
static int dev_open(struct inode*, struct file*);
static int dev_release(struct inode*, struct file*);
static ssize_t dev_read(struct file*, char*, size_t, loff_t*);
static ssize_t dev_write(struct file*, const char*, size_t, loff_t*);
static struct file_operations fops = {
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release,
};
static int major;
static int __init rickroll_init(void) {
major = register_chrdev(0, DEVICE_NAME, &fops);
if (major < 0) {
printk(KERN_ALERT "Rickroll load failed\n");
return major;
}
printk(KERN_INFO "Rickroll module has been loaded: %d\n", major);
return 0;
}
static void __exit rickroll_exit(void) {
unregister_chrdev(major, DEVICE_NAME);
printk(KERN_INFO "Rickroll module has been unloaded\n");
}
static int dev_open(struct inode *inodep, struct file *filep) {
printk(KERN_INFO "Rickroll device opened\n");
return 0;
}
static ssize_t dev_write(struct file *filep, const char *buffer,
size_t len, loff_t *offset) {
printk(KERN_INFO "Sorry, rickroll is read only\n");
return -EFAULT;
}
static int dev_release(struct inode *inodep, struct file *filep) {
printk(KERN_INFO "Rickroll device closed\n");
return 0;
}
static ssize_t dev_read(struct file *filep, char *buffer, size_t len, loff_t *offset) {
int errors = 0;
char *message = "never gonna give you up, never gonna let you down... ";
int message_len = strlen(message);
errors = copy_to_user(buffer, message, message_len);
return errors == 0 ? message_len : -EFAULT;
}
module_init(rickroll_init);
module_exit(rickroll_exit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment