blob: be0ebec2580b14165f24f9cbc9fac3778b73b15b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Root Plug sample LSM module
3 *
4 * Originally written for a Linux Journal.
5 *
6 * Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/security.h>
28#include <linux/usb.h>
James Morris20510f22007-10-16 23:31:32 -070029#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* default is a generic type of usb to serial converter */
32static int vendor_id = 0x0557;
33static int product_id = 0x2008;
34
35module_param(vendor_id, uint, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036module_param(product_id, uint, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* should we print out debug messages */
39static int debug = 0;
40
41module_param(debug, bool, 0600);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define MY_NAME "root_plug"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define root_dbg(fmt, arg...) \
46 do { \
47 if (debug) \
48 printk(KERN_DEBUG "%s: %s: " fmt , \
Harvey Harrisondd6f9532008-03-06 10:03:59 +110049 MY_NAME , __func__ , \
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 ## arg); \
51 } while (0)
52
53static int rootplug_bprm_check_security (struct linux_binprm *bprm)
54{
55 struct usb_device *dev;
56
57 root_dbg("file %s, e_uid = %d, e_gid = %d\n",
58 bprm->filename, bprm->e_uid, bprm->e_gid);
59
60 if (bprm->e_gid == 0) {
61 dev = usb_find_device(vendor_id, product_id);
62 if (!dev) {
63 root_dbg("e_gid = 0, and device not found, "
64 "task not allowed to run...\n");
65 return -EPERM;
66 }
67 usb_put_dev(dev);
68 }
69
70 return 0;
71}
72
73static struct security_operations rootplug_security_ops = {
74 /* Use the capability functions for some of the hooks */
75 .ptrace = cap_ptrace,
76 .capget = cap_capget,
77 .capset_check = cap_capset_check,
78 .capset_set = cap_capset_set,
79 .capable = cap_capable,
80
81 .bprm_apply_creds = cap_bprm_apply_creds,
82 .bprm_set_security = cap_bprm_set_security,
83
84 .task_post_setuid = cap_task_post_setuid,
85 .task_reparent_to_init = cap_task_reparent_to_init,
Serge E. Hallyn55d00cc2008-04-28 02:13:42 -070086 .task_prctl = cap_task_prctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 .bprm_check_security = rootplug_bprm_check_security,
89};
90
91static int __init rootplug_init (void)
92{
93 /* register ourselves with the security framework */
94 if (register_security (&rootplug_security_ops)) {
95 printk (KERN_INFO
96 "Failure registering Root Plug module with the kernel\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99 printk (KERN_INFO "Root Plug module initialized, "
100 "vendor_id = %4.4x, product id = %4.4x\n", vendor_id, product_id);
101 return 0;
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104security_initcall (rootplug_init);