root_plug.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/security.h>
  27. #include <linux/usb.h>
  28. #include <linux/moduleparam.h>
  29. /* default is a generic type of usb to serial converter */
  30. static int vendor_id = 0x0557;
  31. static int product_id = 0x2008;
  32. module_param(vendor_id, uint, 0400);
  33. module_param(product_id, uint, 0400);
  34. /* should we print out debug messages */
  35. static int debug = 0;
  36. module_param(debug, bool, 0600);
  37. #define MY_NAME "root_plug"
  38. #define root_dbg(fmt, arg...) \
  39. do { \
  40. if (debug) \
  41. printk(KERN_DEBUG "%s: %s: " fmt , \
  42. MY_NAME , __func__ , \
  43. ## arg); \
  44. } while (0)
  45. static int rootplug_bprm_check_security (struct linux_binprm *bprm)
  46. {
  47. struct usb_device *dev;
  48. root_dbg("file %s, e_uid = %d, e_gid = %d\n",
  49. bprm->filename, bprm->e_uid, bprm->e_gid);
  50. if (bprm->e_gid == 0) {
  51. dev = usb_find_device(vendor_id, product_id);
  52. if (!dev) {
  53. root_dbg("e_gid = 0, and device not found, "
  54. "task not allowed to run...\n");
  55. return -EPERM;
  56. }
  57. usb_put_dev(dev);
  58. }
  59. return 0;
  60. }
  61. static struct security_operations rootplug_security_ops = {
  62. /* Use the capability functions for some of the hooks */
  63. .ptrace_may_access = cap_ptrace_may_access,
  64. .ptrace_traceme = cap_ptrace_traceme,
  65. .capget = cap_capget,
  66. .capset_check = cap_capset_check,
  67. .capset_set = cap_capset_set,
  68. .capable = cap_capable,
  69. .bprm_apply_creds = cap_bprm_apply_creds,
  70. .bprm_set_security = cap_bprm_set_security,
  71. .task_post_setuid = cap_task_post_setuid,
  72. .task_reparent_to_init = cap_task_reparent_to_init,
  73. .task_prctl = cap_task_prctl,
  74. .bprm_check_security = rootplug_bprm_check_security,
  75. };
  76. static int __init rootplug_init (void)
  77. {
  78. /* register ourselves with the security framework */
  79. if (register_security (&rootplug_security_ops)) {
  80. printk (KERN_INFO
  81. "Failure registering Root Plug module with the kernel\n");
  82. return -EINVAL;
  83. }
  84. printk (KERN_INFO "Root Plug module initialized, "
  85. "vendor_id = %4.4x, product id = %4.4x\n", vendor_id, product_id);
  86. return 0;
  87. }
  88. security_initcall (rootplug_init);