1 /*- 2 * Copyright (c) 1999-2002, 2007, 2009 Robert N. M. Watson 3 * Copyright (c) 2001 Ilmar S. Habibulin 4 * Copyright (c) 2001-2004 Networks Associates Technology, Inc. 5 * Copyright (c) 2006 SPARTA, Inc. 6 * Copyright (c) 2008 Apple Inc. 7 * All rights reserved. 8 * 9 * This software was developed by Robert Watson and Ilmar Habibulin for the 10 * TrustedBSD Project. 11 * 12 * This software was developed for the FreeBSD Project in part by Network 13 * Associates Laboratories, the Security Research Division of Network 14 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 15 * as part of the DARPA CHATS research program. 16 * 17 * This software was enhanced by SPARTA ISSO under SPAWAR contract 18 * N66001-04-C-6019 ("SEFOS"). 19 * 20 * This software was developed at the University of Cambridge Computer 21 * Laboratory with support from a grant from Google, Inc. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the above copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 */ 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include "opt_kdtrace.h" 49 #include "opt_mac.h" 50 51 #include <sys/param.h> 52 #include <sys/kernel.h> 53 #include <sys/lock.h> 54 #include <sys/malloc.h> 55 #include <sys/mutex.h> 56 #include <sys/sbuf.h> 57 #include <sys/sdt.h> 58 #include <sys/systm.h> 59 #include <sys/mount.h> 60 #include <sys/file.h> 61 #include <sys/namei.h> 62 #include <sys/protosw.h> 63 #include <sys/socket.h> 64 #include <sys/socketvar.h> 65 #include <sys/sysctl.h> 66 67 #include <net/if.h> 68 #include <net/if_var.h> 69 70 #include <netinet/in.h> 71 #include <netinet/in_pcb.h> 72 #include <netinet/ip_var.h> 73 74 #include <security/mac/mac_framework.h> 75 #include <security/mac/mac_internal.h> 76 #include <security/mac/mac_policy.h> 77 78 static struct label * 79 mac_inpcb_label_alloc(int flag) 80 { 81 struct label *label; 82 int error; 83 84 label = mac_labelzone_alloc(flag); 85 if (label == NULL) 86 return (NULL); 87 if (flag & M_WAITOK) 88 MAC_CHECK(inpcb_init_label, label, flag); 89 else 90 MAC_CHECK_NOSLEEP(inpcb_init_label, label, flag); 91 if (error) { 92 MAC_PERFORM_NOSLEEP(inpcb_destroy_label, label); 93 mac_labelzone_free(label); 94 return (NULL); 95 } 96 return (label); 97 } 98 99 int 100 mac_inpcb_init(struct inpcb *inp, int flag) 101 { 102 103 if (mac_labeled & MPC_OBJECT_INPCB) { 104 inp->inp_label = mac_inpcb_label_alloc(flag); 105 if (inp->inp_label == NULL) 106 return (ENOMEM); 107 } else 108 inp->inp_label = NULL; 109 return (0); 110 } 111 112 static struct label * 113 mac_ipq_label_alloc(int flag) 114 { 115 struct label *label; 116 int error; 117 118 label = mac_labelzone_alloc(flag); 119 if (label == NULL) 120 return (NULL); 121 122 if (flag & M_WAITOK) 123 MAC_CHECK(ipq_init_label, label, flag); 124 else 125 MAC_CHECK_NOSLEEP(ipq_init_label, label, flag); 126 if (error) { 127 MAC_PERFORM_NOSLEEP(ipq_destroy_label, label); 128 mac_labelzone_free(label); 129 return (NULL); 130 } 131 return (label); 132 } 133 134 int 135 mac_ipq_init(struct ipq *q, int flag) 136 { 137 138 if (mac_labeled & MPC_OBJECT_IPQ) { 139 q->ipq_label = mac_ipq_label_alloc(flag); 140 if (q->ipq_label == NULL) 141 return (ENOMEM); 142 } else 143 q->ipq_label = NULL; 144 return (0); 145 } 146 147 static void 148 mac_inpcb_label_free(struct label *label) 149 { 150 151 MAC_PERFORM_NOSLEEP(inpcb_destroy_label, label); 152 mac_labelzone_free(label); 153 } 154 155 void 156 mac_inpcb_destroy(struct inpcb *inp) 157 { 158 159 if (inp->inp_label != NULL) { 160 mac_inpcb_label_free(inp->inp_label); 161 inp->inp_label = NULL; 162 } 163 } 164 165 static void 166 mac_ipq_label_free(struct label *label) 167 { 168 169 MAC_PERFORM_NOSLEEP(ipq_destroy_label, label); 170 mac_labelzone_free(label); 171 } 172 173 void 174 mac_ipq_destroy(struct ipq *q) 175 { 176 177 if (q->ipq_label != NULL) { 178 mac_ipq_label_free(q->ipq_label); 179 q->ipq_label = NULL; 180 } 181 } 182 183 void 184 mac_inpcb_create(struct socket *so, struct inpcb *inp) 185 { 186 187 MAC_PERFORM_NOSLEEP(inpcb_create, so, so->so_label, inp, 188 inp->inp_label); 189 } 190 191 void 192 mac_ipq_reassemble(struct ipq *q, struct mbuf *m) 193 { 194 struct label *label; 195 196 label = mac_mbuf_to_label(m); 197 198 MAC_PERFORM_NOSLEEP(ipq_reassemble, q, q->ipq_label, m, label); 199 } 200 201 void 202 mac_netinet_fragment(struct mbuf *m, struct mbuf *frag) 203 { 204 struct label *mlabel, *fraglabel; 205 206 mlabel = mac_mbuf_to_label(m); 207 fraglabel = mac_mbuf_to_label(frag); 208 209 MAC_PERFORM_NOSLEEP(netinet_fragment, m, mlabel, frag, fraglabel); 210 } 211 212 void 213 mac_ipq_create(struct mbuf *m, struct ipq *q) 214 { 215 struct label *label; 216 217 label = mac_mbuf_to_label(m); 218 219 MAC_PERFORM_NOSLEEP(ipq_create, m, label, q, q->ipq_label); 220 } 221 222 void 223 mac_inpcb_create_mbuf(struct inpcb *inp, struct mbuf *m) 224 { 225 struct label *mlabel; 226 227 INP_LOCK_ASSERT(inp); 228 mlabel = mac_mbuf_to_label(m); 229 230 MAC_PERFORM_NOSLEEP(inpcb_create_mbuf, inp, inp->inp_label, m, 231 mlabel); 232 } 233 234 int 235 mac_ipq_match(struct mbuf *m, struct ipq *q) 236 { 237 struct label *label; 238 int result; 239 240 label = mac_mbuf_to_label(m); 241 242 result = 1; 243 MAC_BOOLEAN_NOSLEEP(ipq_match, &&, m, label, q, q->ipq_label); 244 245 return (result); 246 } 247 248 void 249 mac_netinet_arp_send(struct ifnet *ifp, struct mbuf *m) 250 { 251 struct label *mlabel; 252 253 mlabel = mac_mbuf_to_label(m); 254 255 MAC_IFNET_LOCK(ifp); 256 MAC_PERFORM_NOSLEEP(netinet_arp_send, ifp, ifp->if_label, m, mlabel); 257 MAC_IFNET_UNLOCK(ifp); 258 } 259 260 void 261 mac_netinet_icmp_reply(struct mbuf *mrecv, struct mbuf *msend) 262 { 263 struct label *mrecvlabel, *msendlabel; 264 265 mrecvlabel = mac_mbuf_to_label(mrecv); 266 msendlabel = mac_mbuf_to_label(msend); 267 268 MAC_PERFORM_NOSLEEP(netinet_icmp_reply, mrecv, mrecvlabel, msend, 269 msendlabel); 270 } 271 272 void 273 mac_netinet_icmp_replyinplace(struct mbuf *m) 274 { 275 struct label *label; 276 277 label = mac_mbuf_to_label(m); 278 279 MAC_PERFORM_NOSLEEP(netinet_icmp_replyinplace, m, label); 280 } 281 282 void 283 mac_netinet_igmp_send(struct ifnet *ifp, struct mbuf *m) 284 { 285 struct label *mlabel; 286 287 mlabel = mac_mbuf_to_label(m); 288 289 MAC_IFNET_LOCK(ifp); 290 MAC_PERFORM_NOSLEEP(netinet_igmp_send, ifp, ifp->if_label, m, 291 mlabel); 292 MAC_IFNET_UNLOCK(ifp); 293 } 294 295 void 296 mac_netinet_tcp_reply(struct mbuf *m) 297 { 298 struct label *label; 299 300 label = mac_mbuf_to_label(m); 301 302 MAC_PERFORM_NOSLEEP(netinet_tcp_reply, m, label); 303 } 304 305 void 306 mac_ipq_update(struct mbuf *m, struct ipq *q) 307 { 308 struct label *label; 309 310 label = mac_mbuf_to_label(m); 311 312 MAC_PERFORM_NOSLEEP(ipq_update, m, label, q, q->ipq_label); 313 } 314 315 MAC_CHECK_PROBE_DEFINE2(inpcb_check_deliver, "struct inpcb *", 316 "struct mbuf *"); 317 318 int 319 mac_inpcb_check_deliver(struct inpcb *inp, struct mbuf *m) 320 { 321 struct label *label; 322 int error; 323 324 M_ASSERTPKTHDR(m); 325 326 label = mac_mbuf_to_label(m); 327 328 MAC_CHECK_NOSLEEP(inpcb_check_deliver, inp, inp->inp_label, m, 329 label); 330 MAC_CHECK_PROBE2(inpcb_check_deliver, error, inp, m); 331 332 return (error); 333 } 334 335 MAC_CHECK_PROBE_DEFINE2(inpcb_check_visible, "struct ucred *", 336 "struct inpcb *"); 337 338 int 339 mac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp) 340 { 341 int error; 342 343 INP_LOCK_ASSERT(inp); 344 345 MAC_CHECK_NOSLEEP(inpcb_check_visible, cred, inp, inp->inp_label); 346 MAC_CHECK_PROBE2(inpcb_check_visible, error, cred, inp); 347 348 return (error); 349 } 350 351 void 352 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp) 353 { 354 355 INP_WLOCK_ASSERT(inp); 356 SOCK_LOCK_ASSERT(so); 357 358 MAC_PERFORM_NOSLEEP(inpcb_sosetlabel, so, so->so_label, inp, 359 inp->inp_label); 360 } 361 362 void 363 mac_netinet_firewall_reply(struct mbuf *mrecv, struct mbuf *msend) 364 { 365 struct label *mrecvlabel, *msendlabel; 366 367 M_ASSERTPKTHDR(mrecv); 368 M_ASSERTPKTHDR(msend); 369 370 mrecvlabel = mac_mbuf_to_label(mrecv); 371 msendlabel = mac_mbuf_to_label(msend); 372 373 MAC_PERFORM_NOSLEEP(netinet_firewall_reply, mrecv, mrecvlabel, msend, 374 msendlabel); 375 } 376 377 void 378 mac_netinet_firewall_send(struct mbuf *m) 379 { 380 struct label *label; 381 382 M_ASSERTPKTHDR(m); 383 384 label = mac_mbuf_to_label(m); 385 386 MAC_PERFORM_NOSLEEP(netinet_firewall_send, m, label); 387 } 388 389 /* 390 * These functions really should be referencing the syncache structure 391 * instead of the label. However, due to some of the complexities associated 392 * with exposing this syncache structure we operate directly on it's label 393 * pointer. This should be OK since we aren't making any access control 394 * decisions within this code directly, we are merely allocating and copying 395 * label storage so we can properly initialize mbuf labels for any packets 396 * the syncache code might create. 397 */ 398 void 399 mac_syncache_destroy(struct label **label) 400 { 401 402 if (*label != NULL) { 403 MAC_PERFORM_NOSLEEP(syncache_destroy_label, *label); 404 mac_labelzone_free(*label); 405 *label = NULL; 406 } 407 } 408 409 int 410 mac_syncache_init(struct label **label) 411 { 412 int error; 413 414 if (mac_labeled & MPC_OBJECT_SYNCACHE) { 415 *label = mac_labelzone_alloc(M_NOWAIT); 416 if (*label == NULL) 417 return (ENOMEM); 418 /* 419 * Since we are holding the inpcb locks the policy can not 420 * allocate policy specific label storage using M_WAITOK. So 421 * we need to do a MAC_CHECK instead of the typical 422 * MAC_PERFORM so we can propagate allocation failures back 423 * to the syncache code. 424 */ 425 MAC_CHECK_NOSLEEP(syncache_init_label, *label, M_NOWAIT); 426 if (error) { 427 MAC_PERFORM_NOSLEEP(syncache_destroy_label, *label); 428 mac_labelzone_free(*label); 429 } 430 return (error); 431 } else 432 *label = NULL; 433 return (0); 434 } 435 436 void 437 mac_syncache_create(struct label *label, struct inpcb *inp) 438 { 439 440 INP_WLOCK_ASSERT(inp); 441 442 MAC_PERFORM_NOSLEEP(syncache_create, label, inp); 443 } 444 445 void 446 mac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m) 447 { 448 struct label *mlabel; 449 450 M_ASSERTPKTHDR(m); 451 452 mlabel = mac_mbuf_to_label(m); 453 454 MAC_PERFORM_NOSLEEP(syncache_create_mbuf, sc_label, m, mlabel); 455 } 456