mac_pipe.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*-
  2. * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
  3. * Copyright (c) 2006 SPARTA, Inc.
  4. * Copyright (c) 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. * This software was enhanced by SPARTA ISSO under SPAWAR contract
  13. * N66001-04-C-6019 ("SEFOS").
  14. *
  15. * This software was developed at the University of Cambridge Computer
  16. * Laboratory with support from a grant from Google, Inc.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. */
  39. #include <sys/cdefs.h>
  40. #include "opt_mac.h"
  41. #include <sys/param.h>
  42. #include <sys/kernel.h>
  43. #include <sys/lock.h>
  44. #include <sys/malloc.h>
  45. #include <sys/module.h>
  46. #include <sys/mutex.h>
  47. #include <sys/sbuf.h>
  48. #include <sys/sdt.h>
  49. #include <sys/systm.h>
  50. #include <sys/vnode.h>
  51. #include <sys/pipe.h>
  52. #include <sys/sysctl.h>
  53. #include <security/mac/mac_framework.h>
  54. #include <security/mac/mac_internal.h>
  55. #include <security/mac/mac_policy.h>
  56. struct label *
  57. mac_pipe_label_alloc(void)
  58. {
  59. struct label *label;
  60. label = mac_labelzone_alloc(M_WAITOK);
  61. MAC_POLICY_PERFORM(pipe_init_label, label);
  62. return (label);
  63. }
  64. void
  65. mac_pipe_init(struct pipepair *pp)
  66. {
  67. if (mac_labeled & MPC_OBJECT_PIPE)
  68. pp->pp_label = mac_pipe_label_alloc();
  69. else
  70. pp->pp_label = NULL;
  71. }
  72. void
  73. mac_pipe_label_free(struct label *label)
  74. {
  75. MAC_POLICY_PERFORM_NOSLEEP(pipe_destroy_label, label);
  76. mac_labelzone_free(label);
  77. }
  78. void
  79. mac_pipe_destroy(struct pipepair *pp)
  80. {
  81. if (pp->pp_label != NULL) {
  82. mac_pipe_label_free(pp->pp_label);
  83. pp->pp_label = NULL;
  84. }
  85. }
  86. void
  87. mac_pipe_copy_label(struct label *src, struct label *dest)
  88. {
  89. MAC_POLICY_PERFORM_NOSLEEP(pipe_copy_label, src, dest);
  90. }
  91. int
  92. mac_pipe_externalize_label(struct label *label, char *elements,
  93. char *outbuf, size_t outbuflen)
  94. {
  95. int error;
  96. MAC_POLICY_EXTERNALIZE(pipe, label, elements, outbuf, outbuflen);
  97. return (error);
  98. }
  99. int
  100. mac_pipe_internalize_label(struct label *label, char *string)
  101. {
  102. int error;
  103. MAC_POLICY_INTERNALIZE(pipe, label, string);
  104. return (error);
  105. }
  106. void
  107. mac_pipe_create(struct ucred *cred, struct pipepair *pp)
  108. {
  109. MAC_POLICY_PERFORM_NOSLEEP(pipe_create, cred, pp, pp->pp_label);
  110. }
  111. static void
  112. mac_pipe_relabel(struct ucred *cred, struct pipepair *pp,
  113. struct label *newlabel)
  114. {
  115. MAC_POLICY_PERFORM_NOSLEEP(pipe_relabel, cred, pp, pp->pp_label,
  116. newlabel);
  117. }
  118. MAC_CHECK_PROBE_DEFINE4(pipe_check_ioctl, "struct ucred *",
  119. "struct pipepair *", "unsigned long", "void *");
  120. int
  121. mac_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp,
  122. unsigned long cmd, void *data)
  123. {
  124. int error;
  125. mtx_assert(&pp->pp_mtx, MA_OWNED);
  126. MAC_POLICY_CHECK_NOSLEEP(pipe_check_ioctl, cred, pp, pp->pp_label,
  127. cmd, data);
  128. MAC_CHECK_PROBE4(pipe_check_ioctl, error, cred, pp, cmd, data);
  129. return (error);
  130. }
  131. MAC_CHECK_PROBE_DEFINE2(pipe_check_poll, "struct ucred *",
  132. "struct pipepair *");
  133. int
  134. mac_pipe_check_poll_impl(struct ucred *cred, struct pipepair *pp)
  135. {
  136. int error;
  137. mtx_assert(&pp->pp_mtx, MA_OWNED);
  138. MAC_POLICY_CHECK_NOSLEEP(pipe_check_poll, cred, pp, pp->pp_label);
  139. MAC_CHECK_PROBE2(pipe_check_poll, error, cred, pp);
  140. return (error);
  141. }
  142. MAC_CHECK_PROBE_DEFINE2(pipe_check_read, "struct ucred *",
  143. "struct pipepair *");
  144. int
  145. mac_pipe_check_read_impl(struct ucred *cred, struct pipepair *pp)
  146. {
  147. int error;
  148. mtx_assert(&pp->pp_mtx, MA_OWNED);
  149. MAC_POLICY_CHECK_NOSLEEP(pipe_check_read, cred, pp, pp->pp_label);
  150. MAC_CHECK_PROBE2(pipe_check_read, error, cred, pp);
  151. return (error);
  152. }
  153. MAC_CHECK_PROBE_DEFINE3(pipe_check_relabel, "struct ucred *",
  154. "struct pipepair *", "struct label *");
  155. static int
  156. mac_pipe_check_relabel(struct ucred *cred, struct pipepair *pp,
  157. struct label *newlabel)
  158. {
  159. int error;
  160. mtx_assert(&pp->pp_mtx, MA_OWNED);
  161. MAC_POLICY_CHECK_NOSLEEP(pipe_check_relabel, cred, pp, pp->pp_label,
  162. newlabel);
  163. MAC_CHECK_PROBE3(pipe_check_relabel, error, cred, pp, newlabel);
  164. return (error);
  165. }
  166. MAC_CHECK_PROBE_DEFINE2(pipe_check_stat, "struct ucred *",
  167. "struct pipepair *");
  168. int
  169. mac_pipe_check_stat(struct ucred *cred, struct pipepair *pp)
  170. {
  171. int error;
  172. mtx_assert(&pp->pp_mtx, MA_OWNED);
  173. MAC_POLICY_CHECK_NOSLEEP(pipe_check_stat, cred, pp, pp->pp_label);
  174. MAC_CHECK_PROBE2(pipe_check_stat, error, cred, pp);
  175. return (error);
  176. }
  177. MAC_CHECK_PROBE_DEFINE2(pipe_check_write, "struct ucred *",
  178. "struct pipepair *");
  179. int
  180. mac_pipe_check_write(struct ucred *cred, struct pipepair *pp)
  181. {
  182. int error;
  183. mtx_assert(&pp->pp_mtx, MA_OWNED);
  184. MAC_POLICY_CHECK_NOSLEEP(pipe_check_write, cred, pp, pp->pp_label);
  185. MAC_CHECK_PROBE2(pipe_check_write, error, cred, pp);
  186. return (error);
  187. }
  188. int
  189. mac_pipe_label_set(struct ucred *cred, struct pipepair *pp,
  190. struct label *label)
  191. {
  192. int error;
  193. mtx_assert(&pp->pp_mtx, MA_OWNED);
  194. error = mac_pipe_check_relabel(cred, pp, label);
  195. if (error)
  196. return (error);
  197. mac_pipe_relabel(cred, pp, label);
  198. return (0);
  199. }