mac_sysv_shm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*-
  2. * Copyright (c) 2003-2004 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/mutex.h>
  46. #include <sys/sbuf.h>
  47. #include <sys/systm.h>
  48. #include <sys/vnode.h>
  49. #include <sys/mount.h>
  50. #include <sys/file.h>
  51. #include <sys/namei.h>
  52. #include <sys/sdt.h>
  53. #include <sys/sysctl.h>
  54. #include <sys/shm.h>
  55. #include <security/mac/mac_framework.h>
  56. #include <security/mac/mac_internal.h>
  57. #include <security/mac/mac_policy.h>
  58. static struct label *
  59. mac_sysv_shm_label_alloc(void)
  60. {
  61. struct label *label;
  62. label = mac_labelzone_alloc(M_WAITOK);
  63. MAC_POLICY_PERFORM(sysvshm_init_label, label);
  64. return (label);
  65. }
  66. void
  67. mac_sysvshm_init(struct shmid_kernel *shmsegptr)
  68. {
  69. if (mac_labeled & MPC_OBJECT_SYSVSHM)
  70. shmsegptr->label = mac_sysv_shm_label_alloc();
  71. else
  72. shmsegptr->label = NULL;
  73. }
  74. static void
  75. mac_sysv_shm_label_free(struct label *label)
  76. {
  77. MAC_POLICY_PERFORM_NOSLEEP(sysvshm_destroy_label, label);
  78. mac_labelzone_free(label);
  79. }
  80. void
  81. mac_sysvshm_destroy(struct shmid_kernel *shmsegptr)
  82. {
  83. if (shmsegptr->label != NULL) {
  84. mac_sysv_shm_label_free(shmsegptr->label);
  85. shmsegptr->label = NULL;
  86. }
  87. }
  88. void
  89. mac_sysvshm_create(struct ucred *cred, struct shmid_kernel *shmsegptr)
  90. {
  91. MAC_POLICY_PERFORM_NOSLEEP(sysvshm_create, cred, shmsegptr,
  92. shmsegptr->label);
  93. }
  94. void
  95. mac_sysvshm_cleanup(struct shmid_kernel *shmsegptr)
  96. {
  97. MAC_POLICY_PERFORM_NOSLEEP(sysvshm_cleanup, shmsegptr->label);
  98. }
  99. MAC_CHECK_PROBE_DEFINE3(sysvshm_check_shmat, "struct ucred *",
  100. "struct shmid_kernel *", "int");
  101. int
  102. mac_sysvshm_check_shmat(struct ucred *cred, struct shmid_kernel *shmsegptr,
  103. int shmflg)
  104. {
  105. int error;
  106. MAC_POLICY_CHECK_NOSLEEP(sysvshm_check_shmat, cred, shmsegptr,
  107. shmsegptr->label, shmflg);
  108. MAC_CHECK_PROBE3(sysvshm_check_shmat, error, cred, shmsegptr,
  109. shmflg);
  110. return (error);
  111. }
  112. MAC_CHECK_PROBE_DEFINE3(sysvshm_check_shmctl, "struct ucred *",
  113. "struct shmid_kernel *", "int");
  114. int
  115. mac_sysvshm_check_shmctl(struct ucred *cred, struct shmid_kernel *shmsegptr,
  116. int cmd)
  117. {
  118. int error;
  119. MAC_POLICY_CHECK_NOSLEEP(sysvshm_check_shmctl, cred, shmsegptr,
  120. shmsegptr->label, cmd);
  121. MAC_CHECK_PROBE3(sysvshm_check_shmctl, error, cred, shmsegptr, cmd);
  122. return (error);
  123. }
  124. MAC_CHECK_PROBE_DEFINE2(sysvshm_check_shmdt, "struct ucred *",
  125. "struct shmid *");
  126. int
  127. mac_sysvshm_check_shmdt(struct ucred *cred, struct shmid_kernel *shmsegptr)
  128. {
  129. int error;
  130. MAC_POLICY_CHECK_NOSLEEP(sysvshm_check_shmdt, cred, shmsegptr,
  131. shmsegptr->label);
  132. MAC_CHECK_PROBE2(sysvshm_check_shmdt, error, cred, shmsegptr);
  133. return (error);
  134. }
  135. MAC_CHECK_PROBE_DEFINE3(sysvshm_check_shmget, "struct ucred *",
  136. "struct shmid_kernel *", "int");
  137. int
  138. mac_sysvshm_check_shmget(struct ucred *cred, struct shmid_kernel *shmsegptr,
  139. int shmflg)
  140. {
  141. int error;
  142. MAC_POLICY_CHECK_NOSLEEP(sysvshm_check_shmget, cred, shmsegptr,
  143. shmsegptr->label, shmflg);
  144. MAC_CHECK_PROBE3(sysvshm_check_shmget, error, cred, shmsegptr,
  145. shmflg);
  146. return (error);
  147. }