Created
July 24, 2023 08:25
-
-
Save M0nteCarl0/7dd87733a973fbc41d611cc9f72343f5 to your computer and use it in GitHub Desktop.
Linux kernel module usb filter example with userspace control application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/module.h> | |
#include <linux/kernel.h> | |
#include <linux/usb.h> | |
#define USB_FILTER_ADD_DEVICE _IO('u', 0) | |
#define USB_FILTER_REMOVE_DEVICE _IO('u', 1) | |
static int is_device_blacklisted(struct usb_device *dev) { | |
// Check if the device is blacklisted based on some criteria | |
return 0; // Return 1 if blacklisted, 0 otherwise | |
} | |
static long usb_filter_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { | |
struct usb_device *dev; | |
int result; | |
switch (cmd) { | |
case USB_FILTER_ADD_DEVICE: | |
dev = (struct usb_device *)arg; | |
result = is_device_blacklisted(dev); | |
if (result) { | |
printk(KERN_INFO "USB device (%04X:%04X) is already blacklisted\n", | |
le16_to_cpu(dev->descriptor.idVendor), | |
le16_to_cpu(dev->descriptor.idProduct)); | |
} else { | |
// Add the device to the blacklist | |
printk(KERN_INFO "Added USB device (%04X:%04X) to the blacklist\n", | |
le16_to_cpu(dev->descriptor.idVendor), | |
le16_to_cpu(dev->descriptor.idProduct)); | |
} | |
break; | |
case USB_FILTER_REMOVE_DEVICE: | |
dev = (struct usb_device *)arg; | |
result = is_device_blacklisted(dev); | |
if (result) { | |
// Remove the device from the blacklist | |
printk(KERN_INFO "Removed USB device (%04X:%04X) from the blacklist\n", | |
le16_to_cpu(dev->descriptor.idVendor), | |
le16_to_cpu(dev->descriptor.idProduct)); | |
} else { | |
printk(KERN_INFO "USB device (%04X:%04X) is not blacklisted\n", | |
le16_to_cpu(dev->descriptor.idVendor), | |
le16_to_cpu(dev->descriptor.idProduct)); | |
} | |
break; | |
default: | |
return -EINVAL; | |
} | |
return 0; | |
} | |
static struct file_operations usb_filter_fops = { | |
.owner = THIS_MODULE, | |
.unlocked_ioctl = usb_filter_ioctl, | |
}; | |
static int __init usb_filter_init(void) { | |
// Register the USB filter module | |
// ... | |
return 0; | |
} | |
static void __exit usb_filter_exit(void) { | |
// Unregister the USB filter module | |
// ... | |
} | |
module_init(usb_filter_init); | |
module_exit(usb_filter_exit); | |
MODULE_LICENSE("GPL"); | |
MODULE_AUTHOR("Your Name"); | |
MODULE_DESCRIPTION("USB filter kernel module"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <sys/ioctl.h> | |
#include <linux/usb.h> | |
#define USB_FILTER_ADD_DEVICE _IO('u', 0) | |
#define USB_FILTER_REMOVE_DEVICE _IO('u', 1) | |
int main() { | |
int fd; | |
struct usb_device dev; | |
fd = open("/dev/usb_filter", O_RDWR); | |
if (fd < 0) { | |
perror("Failed to open /dev/usb_filter"); | |
return EXIT_FAILURE; | |
} | |
// Add a device to the blacklist | |
if (ioctl(fd, USB_FILTER_ADD_DEVICE, &dev) < 0) { | |
perror("Failed to add device to the blacklist"); | |
close(fd); | |
return EXIT_FAILURE; | |
} | |
// Remove a device from the blacklist | |
if (ioctl(fd, USB_FILTER_REMOVE_DEVICE, &dev) < 0) { | |
perror("Failed to remove device from the blacklist"); | |
close(fd); | |
return EXIT_FAILURE; | |
} | |
close(fd); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment