1 /*- 2 * Copyright (c) 2003-2004 Networks Associates Technology, Inc. 3 * Copyright (c) 2006 SPARTA, Inc. 4 * Copyright (c) 2008 Apple Inc. 5 * Copyright (c) 2009 Robert N. M. Watson 6 * All rights reserved. 7 * 8 * This software was developed for the FreeBSD Project in part by Network 9 * Associates Laboratories, the Security Research Division of Network 10 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 11 * as part of the DARPA CHATS research program. 12 * 13 * This software was enhanced by SPARTA ISSO under SPAWAR contract 14 * N66001-04-C-6019 ("SEFOS"). 15 * 16 * This software was developed at the University of Cambridge Computer 17 * Laboratory with support from a grant from Google, Inc. 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include "opt_mac.h" 45 46 #include <sys/param.h> 47 #include <sys/kernel.h> 48 #include <sys/lock.h> 49 #include <sys/malloc.h> 50 #include <sys/mutex.h> 51 #include <sys/sbuf.h> 52 #include <sys/sdt.h> 53 #include <sys/systm.h> 54 #include <sys/vnode.h> 55 #include <sys/mount.h> 56 #include <sys/file.h> 57 #include <sys/namei.h> 58 #include <sys/sysctl.h> 59 #include <sys/msg.h> 60 61 #include <security/mac/mac_framework.h> 62 #include <security/mac/mac_internal.h> 63 #include <security/mac/mac_policy.h> 64 65 static struct label * 66 mac_sysv_msgmsg_label_alloc(void) 67 { 68 struct label *label; 69 70 label = mac_labelzone_alloc(M_WAITOK); 71 MAC_POLICY_PERFORM(sysvmsg_init_label, label); 72 return (label); 73 } 74 75 void 76 mac_sysvmsg_init(struct msg *msgptr) 77 { 78 79 if (mac_labeled & MPC_OBJECT_SYSVMSG) 80 msgptr->label = mac_sysv_msgmsg_label_alloc(); 81 else 82 msgptr->label = NULL; 83 } 84 85 static struct label * 86 mac_sysv_msgqueue_label_alloc(void) 87 { 88 struct label *label; 89 90 label = mac_labelzone_alloc(M_WAITOK); 91 MAC_POLICY_PERFORM(sysvmsq_init_label, label); 92 return (label); 93 } 94 95 void 96 mac_sysvmsq_init(struct msqid_kernel *msqkptr) 97 { 98 99 if (mac_labeled & MPC_OBJECT_SYSVMSQ) 100 msqkptr->label = mac_sysv_msgqueue_label_alloc(); 101 else 102 msqkptr->label = NULL; 103 } 104 105 static void 106 mac_sysv_msgmsg_label_free(struct label *label) 107 { 108 109 MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_destroy_label, label); 110 mac_labelzone_free(label); 111 } 112 113 void 114 mac_sysvmsg_destroy(struct msg *msgptr) 115 { 116 117 if (msgptr->label != NULL) { 118 mac_sysv_msgmsg_label_free(msgptr->label); 119 msgptr->label = NULL; 120 } 121 } 122 123 static void 124 mac_sysv_msgqueue_label_free(struct label *label) 125 { 126 127 MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_destroy_label, label); 128 mac_labelzone_free(label); 129 } 130 131 void 132 mac_sysvmsq_destroy(struct msqid_kernel *msqkptr) 133 { 134 135 if (msqkptr->label != NULL) { 136 mac_sysv_msgqueue_label_free(msqkptr->label); 137 msqkptr->label = NULL; 138 } 139 } 140 141 void 142 mac_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr, 143 struct msg *msgptr) 144 { 145 146 MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_create, cred, msqkptr, 147 msqkptr->label, msgptr, msgptr->label); 148 } 149 150 void 151 mac_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr) 152 { 153 154 MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_create, cred, msqkptr, 155 msqkptr->label); 156 } 157 158 void 159 mac_sysvmsg_cleanup(struct msg *msgptr) 160 { 161 162 MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_cleanup, msgptr->label); 163 } 164 165 void 166 mac_sysvmsq_cleanup(struct msqid_kernel *msqkptr) 167 { 168 169 MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_cleanup, msqkptr->label); 170 } 171 172 MAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msgmsq, "struct ucred *", 173 "struct msg *", "struct msqid_kernel *"); 174 175 int 176 mac_sysvmsq_check_msgmsq(struct ucred *cred, struct msg *msgptr, 177 struct msqid_kernel *msqkptr) 178 { 179 int error; 180 181 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgmsq, cred, msgptr, 182 msgptr->label, msqkptr, msqkptr->label); 183 MAC_CHECK_PROBE3(sysvmsq_check_msgmsq, error, cred, msgptr, msqkptr); 184 185 return (error); 186 } 187 188 MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrcv, "struct ucred *", 189 "struct msg *"); 190 191 int 192 mac_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr) 193 { 194 int error; 195 196 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgrcv, cred, msgptr, 197 msgptr->label); 198 MAC_CHECK_PROBE2(sysvmsq_check_msgrcv, error, cred, msgptr); 199 200 return (error); 201 } 202 203 MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrmid, "struct ucred *", 204 "struct msg *"); 205 206 int 207 mac_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr) 208 { 209 int error; 210 211 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgrmid, cred, msgptr, 212 msgptr->label); 213 MAC_CHECK_PROBE2(sysvmsq_check_msgrmid, error, cred, msgptr); 214 215 return (error); 216 } 217 218 MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqget, "struct ucred *", 219 "struct msqid_kernel *"); 220 221 int 222 mac_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr) 223 { 224 int error; 225 226 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqget, cred, msqkptr, 227 msqkptr->label); 228 MAC_CHECK_PROBE2(sysvmsq_check_msqget, error, cred, msqkptr); 229 230 return (error); 231 } 232 233 MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqsnd, "struct ucred *", 234 "struct msqid_kernel *"); 235 236 int 237 mac_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr) 238 { 239 int error; 240 241 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqsnd, cred, msqkptr, 242 msqkptr->label); 243 MAC_CHECK_PROBE2(sysvmsq_check_msqsnd, error, cred, msqkptr); 244 245 return (error); 246 } 247 248 MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqrcv, "struct ucred *", 249 "struct msqid_kernel *"); 250 251 int 252 mac_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr) 253 { 254 int error; 255 256 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqrcv, cred, msqkptr, 257 msqkptr->label); 258 MAC_CHECK_PROBE2(sysvmsq_check_msqrcv, error, cred, msqkptr); 259 260 return (error); 261 } 262 263 MAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msqctl, "struct ucred *", 264 "struct msqid_kernel *", "int"); 265 266 int 267 mac_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr, 268 int cmd) 269 { 270 int error; 271 272 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqctl, cred, msqkptr, 273 msqkptr->label, cmd); 274 MAC_CHECK_PROBE3(sysvmsq_check_msqctl, error, cred, msqkptr, cmd); 275 276 return (error); 277 } 278