1 /*- 2 * Copyright (c) 2003-2004 Networks Associates Technology, Inc. 3 * Copyright (c) 2006 SPARTA, Inc. 4 * Copyright (c) 2008 Apple Inc. 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 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_mac.h" 41 42 #include <sys/param.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mutex.h> 47 #include <sys/sbuf.h> 48 #include <sys/systm.h> 49 #include <sys/vnode.h> 50 #include <sys/mount.h> 51 #include <sys/file.h> 52 #include <sys/namei.h> 53 #include <sys/sysctl.h> 54 #include <sys/msg.h> 55 56 #include <security/mac/mac_framework.h> 57 #include <security/mac/mac_internal.h> 58 #include <security/mac/mac_policy.h> 59 60 static struct label * 61 mac_sysv_msgmsg_label_alloc(void) 62 { 63 struct label *label; 64 65 label = mac_labelzone_alloc(M_WAITOK); 66 MAC_PERFORM(sysvmsg_init_label, label); 67 return (label); 68 } 69 70 void 71 mac_sysvmsg_init(struct msg *msgptr) 72 { 73 74 if (mac_labeled & MPC_OBJECT_SYSVMSG) 75 msgptr->label = mac_sysv_msgmsg_label_alloc(); 76 else 77 msgptr->label = NULL; 78 } 79 80 static struct label * 81 mac_sysv_msgqueue_label_alloc(void) 82 { 83 struct label *label; 84 85 label = mac_labelzone_alloc(M_WAITOK); 86 MAC_PERFORM(sysvmsq_init_label, label); 87 return (label); 88 } 89 90 void 91 mac_sysvmsq_init(struct msqid_kernel *msqkptr) 92 { 93 94 if (mac_labeled & MPC_OBJECT_SYSVMSQ) 95 msqkptr->label = mac_sysv_msgqueue_label_alloc(); 96 else 97 msqkptr->label = NULL; 98 } 99 100 static void 101 mac_sysv_msgmsg_label_free(struct label *label) 102 { 103 104 MAC_PERFORM(sysvmsg_destroy_label, label); 105 mac_labelzone_free(label); 106 } 107 108 void 109 mac_sysvmsg_destroy(struct msg *msgptr) 110 { 111 112 if (msgptr->label != NULL) { 113 mac_sysv_msgmsg_label_free(msgptr->label); 114 msgptr->label = NULL; 115 } 116 } 117 118 static void 119 mac_sysv_msgqueue_label_free(struct label *label) 120 { 121 122 MAC_PERFORM(sysvmsq_destroy_label, label); 123 mac_labelzone_free(label); 124 } 125 126 void 127 mac_sysvmsq_destroy(struct msqid_kernel *msqkptr) 128 { 129 130 if (msqkptr->label != NULL) { 131 mac_sysv_msgqueue_label_free(msqkptr->label); 132 msqkptr->label = NULL; 133 } 134 } 135 136 void 137 mac_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr, 138 struct msg *msgptr) 139 { 140 141 MAC_PERFORM(sysvmsg_create, cred, msqkptr, msqkptr->label, 142 msgptr, msgptr->label); 143 } 144 145 void 146 mac_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr) 147 { 148 149 MAC_PERFORM(sysvmsq_create, cred, msqkptr, msqkptr->label); 150 } 151 152 void 153 mac_sysvmsg_cleanup(struct msg *msgptr) 154 { 155 156 MAC_PERFORM(sysvmsg_cleanup, msgptr->label); 157 } 158 159 void 160 mac_sysvmsq_cleanup(struct msqid_kernel *msqkptr) 161 { 162 163 MAC_PERFORM(sysvmsq_cleanup, msqkptr->label); 164 } 165 166 int 167 mac_sysvmsq_check_msgmsq(struct ucred *cred, struct msg *msgptr, 168 struct msqid_kernel *msqkptr) 169 { 170 int error; 171 172 MAC_CHECK(sysvmsq_check_msgmsq, cred, msgptr, msgptr->label, 173 msqkptr, msqkptr->label); 174 175 return (error); 176 } 177 178 int 179 mac_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr) 180 { 181 int error; 182 183 MAC_CHECK(sysvmsq_check_msgrcv, cred, msgptr, msgptr->label); 184 185 return (error); 186 } 187 188 int 189 mac_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr) 190 { 191 int error; 192 193 MAC_CHECK(sysvmsq_check_msgrmid, cred, msgptr, msgptr->label); 194 195 return (error); 196 } 197 198 int 199 mac_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr) 200 { 201 int error; 202 203 MAC_CHECK(sysvmsq_check_msqget, cred, msqkptr, msqkptr->label); 204 205 return (error); 206 } 207 208 int 209 mac_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr) 210 { 211 int error; 212 213 MAC_CHECK(sysvmsq_check_msqsnd, cred, msqkptr, msqkptr->label); 214 215 return (error); 216 } 217 218 int 219 mac_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr) 220 { 221 int error; 222 223 MAC_CHECK(sysvmsq_check_msqrcv, cred, msqkptr, msqkptr->label); 224 225 return (error); 226 } 227 228 int 229 mac_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr, 230 int cmd) 231 { 232 int error; 233 234 MAC_CHECK(sysvmsq_check_msqctl, cred, msqkptr, msqkptr->label, cmd); 235 236 return (error); 237 } 238