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_kdtrace.h" 45#include "opt_mac.h" 46 47#include <sys/param.h> 48#include <sys/kernel.h> 49#include <sys/lock.h> 50#include <sys/malloc.h> 51#include <sys/mutex.h> 52#include <sys/sbuf.h> 53#include <sys/sdt.h> 54#include <sys/systm.h> 55#include <sys/vnode.h> 56#include <sys/mount.h> 57#include <sys/file.h> 58#include <sys/namei.h> 59#include <sys/sysctl.h> 60#include <sys/msg.h> 61 62#include <security/mac/mac_framework.h> 63#include <security/mac/mac_internal.h> 64#include <security/mac/mac_policy.h> 65 66static struct label * 67mac_sysv_msgmsg_label_alloc(void) 68{ 69 struct label *label; 70 71 label = mac_labelzone_alloc(M_WAITOK); 72 MAC_POLICY_PERFORM(sysvmsg_init_label, label); 73 return (label); 74} 75 76void 77mac_sysvmsg_init(struct msg *msgptr) 78{ 79 80 if (mac_labeled & MPC_OBJECT_SYSVMSG) 81 msgptr->label = mac_sysv_msgmsg_label_alloc(); 82 else 83 msgptr->label = NULL; 84} 85 86static struct label * 87mac_sysv_msgqueue_label_alloc(void) 88{ 89 struct label *label; 90 91 label = mac_labelzone_alloc(M_WAITOK); 92 MAC_POLICY_PERFORM(sysvmsq_init_label, label); 93 return (label); 94} 95 96void 97mac_sysvmsq_init(struct msqid_kernel *msqkptr) 98{ 99 100 if (mac_labeled & MPC_OBJECT_SYSVMSQ) 101 msqkptr->label = mac_sysv_msgqueue_label_alloc(); 102 else 103 msqkptr->label = NULL; 104} 105 106static void 107mac_sysv_msgmsg_label_free(struct label *label) 108{ 109 110 MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_destroy_label, label); 111 mac_labelzone_free(label); 112} 113 114void 115mac_sysvmsg_destroy(struct msg *msgptr) 116{ 117 118 if (msgptr->label != NULL) { 119 mac_sysv_msgmsg_label_free(msgptr->label); 120 msgptr->label = NULL; 121 } 122} 123 124static void 125mac_sysv_msgqueue_label_free(struct label *label) 126{ 127 128 MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_destroy_label, label); 129 mac_labelzone_free(label); 130} 131 132void 133mac_sysvmsq_destroy(struct msqid_kernel *msqkptr) 134{ 135 136 if (msqkptr->label != NULL) { 137 mac_sysv_msgqueue_label_free(msqkptr->label); 138 msqkptr->label = NULL; 139 } 140} 141 142void 143mac_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr, 144 struct msg *msgptr) 145{ 146 147 MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_create, cred, msqkptr, 148 msqkptr->label, msgptr, msgptr->label); 149} 150 151void 152mac_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr) 153{ 154 155 MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_create, cred, msqkptr, 156 msqkptr->label); 157} 158 159void 160mac_sysvmsg_cleanup(struct msg *msgptr) 161{ 162 163 MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_cleanup, msgptr->label); 164} 165 166void 167mac_sysvmsq_cleanup(struct msqid_kernel *msqkptr) 168{ 169 170 MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_cleanup, msqkptr->label); 171} 172 173MAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msgmsq, "struct ucred *", 174 "struct msg *", "struct msqid_kernel *"); 175 176int 177mac_sysvmsq_check_msgmsq(struct ucred *cred, struct msg *msgptr, 178 struct msqid_kernel *msqkptr) 179{ 180 int error; 181 182 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgmsq, cred, msgptr, 183 msgptr->label, msqkptr, msqkptr->label); 184 MAC_CHECK_PROBE3(sysvmsq_check_msgmsq, error, cred, msgptr, msqkptr); 185 186 return (error); 187} 188 189MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrcv, "struct ucred *", 190 "struct msg *"); 191 192int 193mac_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr) 194{ 195 int error; 196 197 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgrcv, cred, msgptr, 198 msgptr->label); 199 MAC_CHECK_PROBE2(sysvmsq_check_msgrcv, error, cred, msgptr); 200 201 return (error); 202} 203 204MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrmid, "struct ucred *", 205 "struct msg *"); 206 207int 208mac_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr) 209{ 210 int error; 211 212 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgrmid, cred, msgptr, 213 msgptr->label); 214 MAC_CHECK_PROBE2(sysvmsq_check_msgrmid, error, cred, msgptr); 215 216 return (error); 217} 218 219MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqget, "struct ucred *", 220 "struct msqid_kernel *"); 221 222int 223mac_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr) 224{ 225 int error; 226 227 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqget, cred, msqkptr, 228 msqkptr->label); 229 MAC_CHECK_PROBE2(sysvmsq_check_msqget, error, cred, msqkptr); 230 231 return (error); 232} 233 234MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqsnd, "struct ucred *", 235 "struct msqid_kernel *"); 236 237int 238mac_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr) 239{ 240 int error; 241 242 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqsnd, cred, msqkptr, 243 msqkptr->label); 244 MAC_CHECK_PROBE2(sysvmsq_check_msqsnd, error, cred, msqkptr); 245 246 return (error); 247} 248 249MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqrcv, "struct ucred *", 250 "struct msqid_kernel *"); 251 252int 253mac_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr) 254{ 255 int error; 256 257 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqrcv, cred, msqkptr, 258 msqkptr->label); 259 MAC_CHECK_PROBE2(sysvmsq_check_msqrcv, error, cred, msqkptr); 260 261 return (error); 262} 263 264MAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msqctl, "struct ucred *", 265 "struct msqid_kernel *", "int"); 266 267int 268mac_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr, 269 int cmd) 270{ 271 int error; 272 273 MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqctl, cred, msqkptr, 274 msqkptr->label, cmd); 275 MAC_CHECK_PROBE3(sysvmsq_check_msqctl, error, cred, msqkptr, cmd); 276 277 return (error); 278} 279