forked from NateBrune/silk-guardian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsilk.c
147 lines (123 loc) · 3.67 KB
/
silk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/usb.h>
#include <linux/reboot.h>
#include "config.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Greg Kroah-Hartman and Nate Brune");
MODULE_DESCRIPTION("A module that protects you from having a terrible horrible no good very bad day.");
static void panic_time(struct usb_device *usb)
{
int i;
struct device *dev;
pr_info("shredding...\n");
for (i = 0; remove_files[i] != NULL; ++i) {
char *shred_argv[] = {
"/usr/bin/shred",
"-f", "-u", "-n",
shredIterations,
remove_files[i],
NULL,
};
call_usermodehelper(shred_argv[0], shred_argv,
NULL, UMH_WAIT_EXEC);
}
printk("...done.\n");
#ifdef WIPE_RAM
printk("running sdmem");
call_usermodehelper(sdmem_argv[0], sdmem_argv, NULL, UMH_WAIT_EXEC);
#endif
for (dev = &usb->dev; dev; dev = dev->parent)
mutex_unlock(&dev->mutex);
printk("Syncing & powering off.\n");
#ifdef USE_ORDERLY_SHUTDOWN
orderly_poweroff(true);
#else
kernel_power_off();
#endif
}
/*
* returns 0 if no match, 1 if match
*
* Taken from drivers/usb/core/driver.c, as it's not exported for our use :(
*/
static int usb_match_device(struct usb_device *dev,
const struct usb_device_id *id)
{
if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
return 0;
/* No need to test id->bcdDevice_lo != 0, since 0 is never
greater than any unsigned number. */
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
(id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
(id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
(id->bDeviceClass != dev->descriptor.bDeviceClass))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
(id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
(id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
return 0;
return 1;
}
static void usb_dev_change(struct usb_device *dev)
{
const struct usb_device_id *dev_id;
/* Check our whitelist to see if we want to ignore this device */
unsigned long whitelist_len = sizeof(whitelist_table)/sizeof(whitelist_table[0]);
int i; // GNU89 standard
for(i = 0; i < whitelist_len; i++)
{
dev_id = &whitelist_table[i];
if (usb_match_device(dev, dev_id))
{
pr_info("Device is ignored\n");
return;
}
}
/* Not a device we were ignoring, something bad went wrong, panic! */
panic_time(dev);
}
static int notify(struct notifier_block *self, unsigned long action, void *dev)
{
switch (action) {
case USB_DEVICE_ADD:
/* We added a new device, lets check if its known */
usb_dev_change(dev);
break;
case USB_DEVICE_REMOVE:
/* A USB device was removed, possibly as security measure */
usb_dev_change(dev);
break;
default:
break;
}
return 0;
}
static struct notifier_block usb_notify = {
.notifier_call = notify,
};
static int __init silk_init(void)
{
usb_register_notify(&usb_notify);
pr_info("Now watching USB devices...\n");
return 0;
}
module_init(silk_init);
static void __exit silk_exit(void)
{
usb_unregister_notify(&usb_notify);
pr_info("No longer watching USB devices.\n");
}
module_exit(silk_exit);