root_plug.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  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. #include <linux/config.h>
  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. /* flag to keep track of how we were registered */
  31. static int secondary;
  32. /* default is a generic type of usb to serial converter */
  33. static int vendor_id = 0x0557;
  34. static int product_id = 0x2008;
  35. module_param(vendor_id, uint, 0400);
  36. MODULE_PARM_DESC(vendor_id, "USB Vendor ID of device to look for");
  37. module_param(product_id, uint, 0400);
  38. MODULE_PARM_DESC(product_id, "USB Product ID of device to look for");
  39. /* should we print out debug messages */
  40. static int debug = 0;
  41. module_param(debug, bool, 0600);
  42. MODULE_PARM_DESC(debug, "Debug enabled or not");
  43. #if defined(CONFIG_SECURITY_ROOTPLUG_MODULE)
  44. #define MY_NAME THIS_MODULE->name
  45. #else
  46. #define MY_NAME "root_plug"
  47. #endif
  48. #define root_dbg(fmt, arg...) \
  49. do { \
  50. if (debug) \
  51. printk(KERN_DEBUG "%s: %s: " fmt , \
  52. MY_NAME , __FUNCTION__ , \
  53. ## arg); \
  54. } while (0)
  55. static int rootplug_bprm_check_security (struct linux_binprm *bprm)
  56. {
  57. struct usb_device *dev;
  58. root_dbg("file %s, e_uid = %d, e_gid = %d\n",
  59. bprm->filename, bprm->e_uid, bprm->e_gid);
  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. return 0;
  70. }
  71. static struct security_operations rootplug_security_ops = {
  72. /* Use the capability functions for some of the hooks */
  73. .ptrace = cap_ptrace,
  74. .capget = cap_capget,
  75. .capset_check = cap_capset_check,
  76. .capset_set = cap_capset_set,
  77. .capable = cap_capable,
  78. .bprm_apply_creds = cap_bprm_apply_creds,
  79. .bprm_set_security = cap_bprm_set_security,
  80. .task_post_setuid = cap_task_post_setuid,
  81. .task_reparent_to_init = cap_task_reparent_to_init,
  82. .bprm_check_security = rootplug_bprm_check_security,
  83. };
  84. static int __init rootplug_init (void)
  85. {
  86. /* register ourselves with the security framework */
  87. if (register_security (&rootplug_security_ops)) {
  88. printk (KERN_INFO
  89. "Failure registering Root Plug module with the kernel\n");
  90. /* try registering with primary module */
  91. if (mod_reg_security (MY_NAME, &rootplug_security_ops)) {
  92. printk (KERN_INFO "Failure registering Root Plug "
  93. " module with primary security module.\n");
  94. return -EINVAL;
  95. }
  96. secondary = 1;
  97. }
  98. printk (KERN_INFO "Root Plug module initialized, "
  99. "vendor_id = %4.4x, product id = %4.4x\n", vendor_id, product_id);
  100. return 0;
  101. }
  102. static void __exit rootplug_exit (void)
  103. {
  104. /* remove ourselves from the security framework */
  105. if (secondary) {
  106. if (mod_unreg_security (MY_NAME, &rootplug_security_ops))
  107. printk (KERN_INFO "Failure unregistering Root Plug "
  108. " module with primary module.\n");
  109. } else {
  110. if (unregister_security (&rootplug_security_ops)) {
  111. printk (KERN_INFO "Failure unregistering Root Plug "
  112. "module with the kernel\n");
  113. }
  114. }
  115. printk (KERN_INFO "Root Plug module removed\n");
  116. }
  117. security_initcall (rootplug_init);
  118. module_exit (rootplug_exit);
  119. MODULE_DESCRIPTION("Root Plug sample LSM module, written for Linux Journal article");
  120. MODULE_LICENSE("GPL");