mac_system.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*-
  2. * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
  3. * Copyright (c) 2006 SPARTA, Inc.
  4. * Copyright (c) 2007, 2009 Robert N. M. Watson
  5. * All rights reserved.
  6. *
  7. * This software was developed for the FreeBSD Project in part by Network
  8. * Associates Laboratories, the Security Research Division of Network
  9. * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
  10. * as part of the DARPA CHATS research program.
  11. *
  12. * Portions of this software were developed by Robert Watson for the
  13. * TrustedBSD Project.
  14. *
  15. * This software was enhanced by SPARTA ISSO under SPAWAR contract
  16. * N66001-04-C-6019 ("SEFOS").
  17. *
  18. * This software was developed at the University of Cambridge Computer
  19. * Laboratory with support from a grant from Google, Inc.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. * 1. Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in the
  28. * documentation and/or other materials provided with the distribution.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  31. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40. * SUCH DAMAGE.
  41. */
  42. /*
  43. * MAC Framework entry points relating to overall operation of system,
  44. * including global services such as the kernel environment and loadable
  45. * modules.
  46. *
  47. * System checks often align with existing privilege checks, but provide
  48. * additional security context that may be relevant to policies, such as the
  49. * specific object being operated on.
  50. */
  51. #include <sys/cdefs.h>
  52. #include "opt_mac.h"
  53. #include <sys/param.h>
  54. #include <sys/kernel.h>
  55. #include <sys/lock.h>
  56. #include <sys/malloc.h>
  57. #include <sys/module.h>
  58. #include <sys/mutex.h>
  59. #include <sys/sdt.h>
  60. #include <sys/systm.h>
  61. #include <sys/vnode.h>
  62. #include <sys/sysctl.h>
  63. #include <security/mac/mac_framework.h>
  64. #include <security/mac/mac_internal.h>
  65. #include <security/mac/mac_policy.h>
  66. MAC_CHECK_PROBE_DEFINE1(kenv_check_dump, "struct ucred *");
  67. int
  68. mac_kenv_check_dump(struct ucred *cred)
  69. {
  70. int error;
  71. MAC_POLICY_CHECK_NOSLEEP(kenv_check_dump, cred);
  72. MAC_CHECK_PROBE1(kenv_check_dump, error, cred);
  73. return (error);
  74. }
  75. MAC_CHECK_PROBE_DEFINE2(kenv_check_get, "struct ucred *", "char *");
  76. int
  77. mac_kenv_check_get(struct ucred *cred, char *name)
  78. {
  79. int error;
  80. MAC_POLICY_CHECK_NOSLEEP(kenv_check_get, cred, name);
  81. MAC_CHECK_PROBE2(kenv_check_get, error, cred, name);
  82. return (error);
  83. }
  84. MAC_CHECK_PROBE_DEFINE3(kenv_check_set, "struct ucred *", "char *",
  85. "char *");
  86. int
  87. mac_kenv_check_set(struct ucred *cred, char *name, char *value)
  88. {
  89. int error;
  90. MAC_POLICY_CHECK_NOSLEEP(kenv_check_set, cred, name, value);
  91. MAC_CHECK_PROBE3(kenv_check_set, error, cred, name, value);
  92. return (error);
  93. }
  94. MAC_CHECK_PROBE_DEFINE2(kenv_check_unset, "struct ucred *", "char *");
  95. int
  96. mac_kenv_check_unset(struct ucred *cred, char *name)
  97. {
  98. int error;
  99. MAC_POLICY_CHECK_NOSLEEP(kenv_check_unset, cred, name);
  100. MAC_CHECK_PROBE2(kenv_check_unset, error, cred, name);
  101. return (error);
  102. }
  103. MAC_CHECK_PROBE_DEFINE2(kld_check_load, "struct ucred *", "struct vnode *");
  104. int
  105. mac_kld_check_load(struct ucred *cred, struct vnode *vp)
  106. {
  107. int error;
  108. ASSERT_VOP_LOCKED(vp, "mac_kld_check_load");
  109. MAC_POLICY_CHECK(kld_check_load, cred, vp, vp->v_label);
  110. MAC_CHECK_PROBE2(kld_check_load, error, cred, vp);
  111. return (error);
  112. }
  113. MAC_CHECK_PROBE_DEFINE1(kld_check_stat, "struct ucred *");
  114. int
  115. mac_kld_check_stat(struct ucred *cred)
  116. {
  117. int error;
  118. MAC_POLICY_CHECK_NOSLEEP(kld_check_stat, cred);
  119. MAC_CHECK_PROBE1(kld_check_stat, error, cred);
  120. return (error);
  121. }
  122. MAC_CHECK_PROBE_DEFINE2(system_check_acct, "struct ucred *",
  123. "struct vnode *");
  124. int
  125. mac_system_check_acct(struct ucred *cred, struct vnode *vp)
  126. {
  127. int error;
  128. if (vp != NULL) {
  129. ASSERT_VOP_LOCKED(vp, "mac_system_check_acct");
  130. }
  131. MAC_POLICY_CHECK(system_check_acct, cred, vp,
  132. vp != NULL ? vp->v_label : NULL);
  133. MAC_CHECK_PROBE2(system_check_acct, error, cred, vp);
  134. return (error);
  135. }
  136. MAC_CHECK_PROBE_DEFINE2(system_check_reboot, "struct ucred *", "int");
  137. int
  138. mac_system_check_reboot(struct ucred *cred, int howto)
  139. {
  140. int error;
  141. MAC_POLICY_CHECK_NOSLEEP(system_check_reboot, cred, howto);
  142. MAC_CHECK_PROBE2(system_check_reboot, error, cred, howto);
  143. return (error);
  144. }
  145. MAC_CHECK_PROBE_DEFINE2(system_check_swapon, "struct ucred *",
  146. "struct vnode *");
  147. int
  148. mac_system_check_swapon(struct ucred *cred, struct vnode *vp)
  149. {
  150. int error;
  151. ASSERT_VOP_LOCKED(vp, "mac_system_check_swapon");
  152. MAC_POLICY_CHECK(system_check_swapon, cred, vp, vp->v_label);
  153. MAC_CHECK_PROBE2(system_check_swapon, error, cred, vp);
  154. return (error);
  155. }
  156. MAC_CHECK_PROBE_DEFINE2(system_check_swapoff, "struct ucred *",
  157. "struct vnode *");
  158. int
  159. mac_system_check_swapoff(struct ucred *cred, struct vnode *vp)
  160. {
  161. int error;
  162. ASSERT_VOP_LOCKED(vp, "mac_system_check_swapoff");
  163. MAC_POLICY_CHECK(system_check_swapoff, cred, vp, vp->v_label);
  164. MAC_CHECK_PROBE2(system_check_swapoff, error, cred, vp);
  165. return (error);
  166. }
  167. MAC_CHECK_PROBE_DEFINE3(system_check_sysctl, "struct ucred *",
  168. "struct sysctl_oid *", "struct sysctl_req *");
  169. int
  170. mac_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
  171. void *arg1, int arg2, struct sysctl_req *req)
  172. {
  173. int error;
  174. /*
  175. * XXXMAC: We would very much like to assert the SYSCTL_LOCK here,
  176. * but since it's not exported from kern_sysctl.c, we can't.
  177. */
  178. MAC_POLICY_CHECK_NOSLEEP(system_check_sysctl, cred, oidp, arg1, arg2,
  179. req);
  180. MAC_CHECK_PROBE3(system_check_sysctl, error, cred, oidp, req);
  181. return (error);
  182. }