]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Root Plug sample LSM module | |
3 | * | |
4 | * Originally written for a Linux Journal. | |
5 | * | |
6 | * Copyright (C) 2002 Greg Kroah-Hartman <[email protected]> | |
7 | * | |
8 | * Prevents any programs running with egid == 0 if a specific USB device | |
9 | * is not present in the system. Yes, it can be gotten around, but is a | |
10 | * nice starting point for people to play with, and learn the LSM | |
11 | * interface. | |
12 | * | |
13 | * If you want to turn this into something with a semblance of security, | |
14 | * you need to hook the task_* functions also. | |
15 | * | |
16 | * See http://www.linuxjournal.com/article.php?sid=6279 for more information | |
17 | * about this code. | |
18 | * | |
19 | * This program is free software; you can redistribute it and/or | |
20 | * modify it under the terms of the GNU General Public License as | |
21 | * published by the Free Software Foundation, version 2 of the | |
22 | * License. | |
23 | */ | |
24 | ||
1da177e4 LT |
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 | /* flag to keep track of how we were registered */ | |
32 | static int secondary; | |
33 | ||
34 | /* default is a generic type of usb to serial converter */ | |
35 | static int vendor_id = 0x0557; | |
36 | static int product_id = 0x2008; | |
37 | ||
38 | module_param(vendor_id, uint, 0400); | |
39 | MODULE_PARM_DESC(vendor_id, "USB Vendor ID of device to look for"); | |
40 | ||
41 | module_param(product_id, uint, 0400); | |
42 | MODULE_PARM_DESC(product_id, "USB Product ID of device to look for"); | |
43 | ||
44 | /* should we print out debug messages */ | |
45 | static int debug = 0; | |
46 | ||
47 | module_param(debug, bool, 0600); | |
48 | MODULE_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 | ||
64 | static 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 | ||
84 | static struct security_operations rootplug_security_ops = { | |
85 | /* Use the capability functions for some of the hooks */ | |
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 | ||
101 | static int __init rootplug_init (void) | |
102 | { | |
103 | /* register ourselves with the security framework */ | |
104 | if (register_security (&rootplug_security_ops)) { | |
105 | printk (KERN_INFO | |
106 | "Failure registering Root Plug module with the kernel\n"); | |
107 | /* try registering with primary module */ | |
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 | ||
120 | static void __exit rootplug_exit (void) | |
121 | { | |
122 | /* remove ourselves from the security framework */ | |
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 | ||
136 | security_initcall (rootplug_init); | |
137 | module_exit (rootplug_exit); | |
138 | ||
139 | MODULE_DESCRIPTION("Root Plug sample LSM module, written for Linux Journal article"); | |
140 | MODULE_LICENSE("GPL"); | |
141 |