security.c 5.5 KB

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