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#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/security.h>
29#include <linux/usb.h>
30
31
32static int secondary;
33
34
35static int vendor_id = 0x0557;
36static int product_id = 0x2008;
37
38module_param(vendor_id, uint, 0400);
39MODULE_PARM_DESC(vendor_id, "USB Vendor ID of device to look for");
40
41module_param(product_id, uint, 0400);
42MODULE_PARM_DESC(product_id, "USB Product ID of device to look for");
43
44
45static int debug = 0;
46
47module_param(debug, bool, 0600);
48MODULE_PARM_DESC(debug, "Debug enabled or not");
49
50#if defined(CONFIG_SECURITY_ROOTPLUG_MODULE)
51#define MY_NAME THIS_MODULE->name
52#else
53#define MY_NAME "root_plug"
54#endif
55
56#define root_dbg(fmt, arg...) \
57 do { \
58 if (debug) \
59 printk(KERN_DEBUG "%s: %s: " fmt , \
60 MY_NAME , __FUNCTION__ , \
61 ## arg); \
62 } while (0)
63
64static int rootplug_bprm_check_security (struct linux_binprm *bprm)
65{
66 struct usb_device *dev;
67
68 root_dbg("file %s, e_uid = %d, e_gid = %d\n",
69 bprm->filename, bprm->e_uid, bprm->e_gid);
70
71 if (bprm->e_gid == 0) {
72 dev = usb_find_device(vendor_id, product_id);
73 if (!dev) {
74 root_dbg("e_gid = 0, and device not found, "
75 "task not allowed to run...\n");
76 return -EPERM;
77 }
78 usb_put_dev(dev);
79 }
80
81 return 0;
82}
83
84static struct security_operations rootplug_security_ops = {
85
86 .ptrace = cap_ptrace,
87 .capget = cap_capget,
88 .capset_check = cap_capset_check,
89 .capset_set = cap_capset_set,
90 .capable = cap_capable,
91
92 .bprm_apply_creds = cap_bprm_apply_creds,
93 .bprm_set_security = cap_bprm_set_security,
94
95 .task_post_setuid = cap_task_post_setuid,
96 .task_reparent_to_init = cap_task_reparent_to_init,
97
98 .bprm_check_security = rootplug_bprm_check_security,
99};
100
101static int __init rootplug_init (void)
102{
103
104 if (register_security (&rootplug_security_ops)) {
105 printk (KERN_INFO
106 "Failure registering Root Plug module with the kernel\n");
107
108 if (mod_reg_security (MY_NAME, &rootplug_security_ops)) {
109 printk (KERN_INFO "Failure registering Root Plug "
110 " module with primary security module.\n");
111 return -EINVAL;
112 }
113 secondary = 1;
114 }
115 printk (KERN_INFO "Root Plug module initialized, "
116 "vendor_id = %4.4x, product id = %4.4x\n", vendor_id, product_id);
117 return 0;
118}
119
120static void __exit rootplug_exit (void)
121{
122
123 if (secondary) {
124 if (mod_unreg_security (MY_NAME, &rootplug_security_ops))
125 printk (KERN_INFO "Failure unregistering Root Plug "
126 " module with primary module.\n");
127 } else {
128 if (unregister_security (&rootplug_security_ops)) {
129 printk (KERN_INFO "Failure unregistering Root Plug "
130 "module with the kernel\n");
131 }
132 }
133 printk (KERN_INFO "Root Plug module removed\n");
134}
135
136security_initcall (rootplug_init);
137module_exit (rootplug_exit);
138
139MODULE_DESCRIPTION("Root Plug sample LSM module, written for Linux Journal article");
140MODULE_LICENSE("GPL");
141
142