security.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Security plug functions
  3. *
  4. * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com>
  5. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  6. * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/capability.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/security.h>
  19. #define SECURITY_FRAMEWORK_VERSION "1.0.0"
  20. /* things that live in dummy.c */
  21. extern struct security_operations dummy_security_ops;
  22. extern void security_fixup_ops(struct security_operations *ops);
  23. struct security_operations *security_ops; /* Initialized to NULL */
  24. static inline int verify(struct security_operations *ops)
  25. {
  26. /* verify the security_operations structure exists */
  27. if (!ops)
  28. return -EINVAL;
  29. security_fixup_ops(ops);
  30. return 0;
  31. }
  32. static void __init do_security_initcalls(void)
  33. {
  34. initcall_t *call;
  35. call = __security_initcall_start;
  36. while (call < __security_initcall_end) {
  37. (*call) ();
  38. call++;
  39. }
  40. }
  41. /**
  42. * security_init - initializes the security framework
  43. *
  44. * This should be called early in the kernel initialization sequence.
  45. */
  46. int __init security_init(void)
  47. {
  48. printk(KERN_INFO "Security Framework v" SECURITY_FRAMEWORK_VERSION
  49. " initialized\n");
  50. if (verify(&dummy_security_ops)) {
  51. printk(KERN_ERR "%s could not verify "
  52. "dummy_security_ops structure.\n", __FUNCTION__);
  53. return -EIO;
  54. }
  55. security_ops = &dummy_security_ops;
  56. do_security_initcalls();
  57. return 0;
  58. }
  59. /**
  60. * register_security - registers a security framework with the kernel
  61. * @ops: a pointer to the struct security_options that is to be registered
  62. *
  63. * This function is to allow a security module to register itself with the
  64. * kernel security subsystem. Some rudimentary checking is done on the @ops
  65. * value passed to this function. A call to unregister_security() should be
  66. * done to remove this security_options structure from the kernel.
  67. *
  68. * If there is already a security module registered with the kernel,
  69. * an error will be returned. Otherwise 0 is returned on success.
  70. */
  71. int register_security(struct security_operations *ops)
  72. {
  73. if (verify(ops)) {
  74. printk(KERN_DEBUG "%s could not verify "
  75. "security_operations structure.\n", __FUNCTION__);
  76. return -EINVAL;
  77. }
  78. if (security_ops != &dummy_security_ops)
  79. return -EAGAIN;
  80. security_ops = ops;
  81. return 0;
  82. }
  83. /**
  84. * unregister_security - unregisters a security framework with the kernel
  85. * @ops: a pointer to the struct security_options that is to be registered
  86. *
  87. * This function removes a struct security_operations variable that had
  88. * previously been registered with a successful call to register_security().
  89. *
  90. * If @ops does not match the valued previously passed to register_security()
  91. * an error is returned. Otherwise the default security options is set to the
  92. * the dummy_security_ops structure, and 0 is returned.
  93. */
  94. int unregister_security(struct security_operations *ops)
  95. {
  96. if (ops != security_ops) {
  97. printk(KERN_INFO "%s: trying to unregister "
  98. "a security_opts structure that is not "
  99. "registered, failing.\n", __FUNCTION__);
  100. return -EINVAL;
  101. }
  102. security_ops = &dummy_security_ops;
  103. return 0;
  104. }
  105. /**
  106. * mod_reg_security - allows security modules to be "stacked"
  107. * @name: a pointer to a string with the name of the security_options to be registered
  108. * @ops: a pointer to the struct security_options that is to be registered
  109. *
  110. * This function allows security modules to be stacked if the currently loaded
  111. * security module allows this to happen. It passes the @name and @ops to the
  112. * register_security function of the currently loaded security module.
  113. *
  114. * The return value depends on the currently loaded security module, with 0 as
  115. * success.
  116. */
  117. int mod_reg_security(const char *name, struct security_operations *ops)
  118. {
  119. if (verify(ops)) {
  120. printk(KERN_INFO "%s could not verify "
  121. "security operations.\n", __FUNCTION__);
  122. return -EINVAL;
  123. }
  124. if (ops == security_ops) {
  125. printk(KERN_INFO "%s security operations "
  126. "already registered.\n", __FUNCTION__);
  127. return -EINVAL;
  128. }
  129. return security_ops->register_security(name, ops);
  130. }
  131. /**
  132. * mod_unreg_security - allows a security module registered with mod_reg_security() to be unloaded
  133. * @name: a pointer to a string with the name of the security_options to be removed
  134. * @ops: a pointer to the struct security_options that is to be removed
  135. *
  136. * This function allows security modules that have been successfully registered
  137. * with a call to mod_reg_security() to be unloaded from the system.
  138. * This calls the currently loaded security module's unregister_security() call
  139. * with the @name and @ops variables.
  140. *
  141. * The return value depends on the currently loaded security module, with 0 as
  142. * success.
  143. */
  144. int mod_unreg_security(const char *name, struct security_operations *ops)
  145. {
  146. if (ops == security_ops) {
  147. printk(KERN_INFO "%s invalid attempt to unregister "
  148. " primary security ops.\n", __FUNCTION__);
  149. return -EINVAL;
  150. }
  151. return security_ops->unregister_security(name, ops);
  152. }
  153. EXPORT_SYMBOL_GPL(register_security);
  154. EXPORT_SYMBOL_GPL(unregister_security);
  155. EXPORT_SYMBOL_GPL(mod_reg_security);
  156. EXPORT_SYMBOL_GPL(mod_unreg_security);
  157. EXPORT_SYMBOL(security_ops);