1 // Copyright (C) 2013 Jann Horn <jann@thejh.net>
2 // This file is licensed under the GNU GPL v2 (see
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/file.h>
15 #include <linux/ioctl.h>
16 #include <linux/uaccess.h>
17 #include <linux/miscdevice.h>
18 #include <linux/kallsyms.h>
19 #include <linux/blkdev.h>
21 struct fetch_fops_args {
26 #define MOCTEL_FETCH_FOPS _IOR('m', 1, struct fetch_fops_args)
28 static int ioctl_open(struct inode *nodp, struct file *filp) {
32 static long ioctl_ioctl(struct file *filp_, unsigned int cmd, unsigned long arg) {
33 struct fetch_fops_args args;
35 void *ioctl_method = NULL;
36 char ioctl_method_name[KSYM_NAME_LEN];
37 int (*lookup_symbol_name_)(unsigned long addr, char *symname);
39 lookup_symbol_name_ = (void*)kallsyms_lookup_name("lookup_symbol_name");
41 if (cmd != MOCTEL_FETCH_FOPS) return -EINVAL;
42 memset(ioctl_method_name, 0, sizeof(ioctl_method_name));
43 if (copy_from_user(&args, (struct fetch_fops_args __user *)arg, sizeof(args))) return -EINVAL;
45 if (!filp) return -EBADF;
46 if (filp->f_op) ioctl_method = filp->f_op->unlocked_ioctl;
47 lookup_symbol_name_((unsigned long)ioctl_method, ioctl_method_name);
48 if (strcmp(ioctl_method_name, "block_ioctl") == 0) {
49 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
50 ioctl_method = bdev->bd_disk->fops->ioctl;
51 lookup_symbol_name_((unsigned long)ioctl_method, ioctl_method_name);
54 if (copy_to_user((struct file_operations __user *)args.retp, ioctl_method_name, KSYM_NAME_LEN)) return -EINVAL;
58 static int ioctl_release(struct inode *nodp, struct file *filp) {
62 static const struct file_operations ioctl_fops = {
64 .unlocked_ioctl = ioctl_ioctl,
66 .release = ioctl_release
69 static struct miscdevice ioctl_miscdev = {
75 static int __init init_ioctl(void) {
77 printk(KERN_INFO "loading ioctl helper\n");
79 ret = misc_register(&ioctl_miscdev);
83 static void __exit cleanup_ioctl(void) {
84 printk(KERN_INFO "unloading ioctl helper\n");
85 misc_deregister(&ioctl_miscdev);
88 module_init(init_ioctl);
89 module_exit(cleanup_ioctl);
91 MODULE_LICENSE("GPL v2");