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 MAC_CHECK(inpcb_init_label, label, flag); 88 if (error) { 89 MAC_PERFORM(inpcb_destroy_label, label); 90 mac_labelzone_free(label); 91 return (NULL); 92 } 93 return (label); 94 } 95 96 int 97 mac_inpcb_init(struct inpcb *inp, int flag) 98 { 99 100 if (mac_labeled & MPC_OBJECT_INPCB) { 101 inp->inp_label = mac_inpcb_label_alloc(flag); 102 if (inp->inp_label == NULL) 103 return (ENOMEM); 104 } else 105 inp->inp_label = NULL; 106 return (0); 107 } 108 109 static struct label * 110 mac_ipq_label_alloc(int flag) 111 { 112 struct label *label; 113 int error; 114 115 label = mac_labelzone_alloc(flag); 116 if (label == NULL) 117 return (NULL); 118 119 MAC_CHECK(ipq_init_label, label, flag); 120 if (error) { 121 MAC_PERFORM(ipq_destroy_label, label); 122 mac_labelzone_free(label); 123 return (NULL); 124 } 125 return (label); 126 } 127 128 int 129 mac_ipq_init(struct ipq *q, int flag) 130 { 131 132 if (mac_labeled & MPC_OBJECT_IPQ) { 133 q->ipq_label = mac_ipq_label_alloc(flag); 134 if (q->ipq_label == NULL) 135 return (ENOMEM); 136 } else 137 q->ipq_label = NULL; 138 return (0); 139 } 140 141 static void 142 mac_inpcb_label_free(struct label *label) 143 { 144 145 MAC_PERFORM(inpcb_destroy_label, label); 146 mac_labelzone_free(label); 147 } 148 149 void 150 mac_inpcb_destroy(struct inpcb *inp) 151 { 152 153 if (inp->inp_label != NULL) { 154 mac_inpcb_label_free(inp->inp_label); 155 inp->inp_label = NULL; 156 } 157 } 158 159 static void 160 mac_ipq_label_free(struct label *label) 161 { 162 163 MAC_PERFORM(ipq_destroy_label, label); 164 mac_labelzone_free(label); 165 } 166 167 void 168 mac_ipq_destroy(struct ipq *q) 169 { 170 171 if (q->ipq_label != NULL) { 172 mac_ipq_label_free(q->ipq_label); 173 q->ipq_label = NULL; 174 } 175 } 176 177 void 178 mac_inpcb_create(struct socket *so, struct inpcb *inp) 179 { 180 181 MAC_PERFORM(inpcb_create, so, so->so_label, inp, inp->inp_label); 182 } 183 184 void 185 mac_ipq_reassemble(struct ipq *q, struct mbuf *m) 186 { 187 struct label *label; 188 189 label = mac_mbuf_to_label(m); 190 191 MAC_PERFORM(ipq_reassemble, q, q->ipq_label, m, label); 192 } 193 194 void 195 mac_netinet_fragment(struct mbuf *m, struct mbuf *frag) 196 { 197 struct label *mlabel, *fraglabel; 198 199 mlabel = mac_mbuf_to_label(m); 200 fraglabel = mac_mbuf_to_label(frag); 201 202 MAC_PERFORM(netinet_fragment, m, mlabel, frag, fraglabel); 203 } 204 205 void 206 mac_ipq_create(struct mbuf *m, struct ipq *q) 207 { 208 struct label *label; 209 210 label = mac_mbuf_to_label(m); 211 212 MAC_PERFORM(ipq_create, m, label, q, q->ipq_label); 213 } 214 215 void 216 mac_inpcb_create_mbuf(struct inpcb *inp, struct mbuf *m) 217 { 218 struct label *mlabel; 219 220 INP_LOCK_ASSERT(inp); 221 mlabel = mac_mbuf_to_label(m); 222 223 MAC_PERFORM(inpcb_create_mbuf, inp, inp->inp_label, m, mlabel); 224 } 225 226 int 227 mac_ipq_match(struct mbuf *m, struct ipq *q) 228 { 229 struct label *label; 230 int result; 231 232 label = mac_mbuf_to_label(m); 233 234 result = 1; 235 MAC_BOOLEAN(ipq_match, &&, m, label, q, q->ipq_label); 236 237 return (result); 238 } 239 240 void 241 mac_netinet_arp_send(struct ifnet *ifp, struct mbuf *m) 242 { 243 struct label *mlabel; 244 245 mlabel = mac_mbuf_to_label(m); 246 247 MAC_IFNET_LOCK(ifp); 248 MAC_PERFORM(netinet_arp_send, ifp, ifp->if_label, m, mlabel); 249 MAC_IFNET_UNLOCK(ifp); 250 } 251 252 void 253 mac_netinet_icmp_reply(struct mbuf *mrecv, struct mbuf *msend) 254 { 255 struct label *mrecvlabel, *msendlabel; 256 257 mrecvlabel = mac_mbuf_to_label(mrecv); 258 msendlabel = mac_mbuf_to_label(msend); 259 260 MAC_PERFORM(netinet_icmp_reply, mrecv, mrecvlabel, msend, 261 msendlabel); 262 } 263 264 void 265 mac_netinet_icmp_replyinplace(struct mbuf *m) 266 { 267 struct label *label; 268 269 label = mac_mbuf_to_label(m); 270 271 MAC_PERFORM(netinet_icmp_replyinplace, m, label); 272 } 273 274 void 275 mac_netinet_igmp_send(struct ifnet *ifp, struct mbuf *m) 276 { 277 struct label *mlabel; 278 279 mlabel = mac_mbuf_to_label(m); 280 281 MAC_IFNET_LOCK(ifp); 282 MAC_PERFORM(netinet_igmp_send, ifp, ifp->if_label, m, mlabel); 283 MAC_IFNET_UNLOCK(ifp); 284 } 285 286 void 287 mac_netinet_tcp_reply(struct mbuf *m) 288 { 289 struct label *label; 290 291 label = mac_mbuf_to_label(m); 292 293 MAC_PERFORM(netinet_tcp_reply, m, label); 294 } 295 296 void 297 mac_ipq_update(struct mbuf *m, struct ipq *q) 298 { 299 struct label *label; 300 301 label = mac_mbuf_to_label(m); 302 303 MAC_PERFORM(ipq_update, m, label, q, q->ipq_label); 304 } 305 306 MAC_CHECK_PROBE_DEFINE2(inpcb_check_deliver, "struct inpcb *", 307 "struct mbuf *"); 308 309 int 310 mac_inpcb_check_deliver(struct inpcb *inp, struct mbuf *m) 311 { 312 struct label *label; 313 int error; 314 315 M_ASSERTPKTHDR(m); 316 317 label = mac_mbuf_to_label(m); 318 319 MAC_CHECK(inpcb_check_deliver, inp, inp->inp_label, m, label); 320 MAC_CHECK_PROBE2(inpcb_check_deliver, error, inp, m); 321 322 return (error); 323 } 324 325 MAC_CHECK_PROBE_DEFINE2(inpcb_check_visible, "struct ucred *", 326 "struct inpcb *"); 327 328 int 329 mac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp) 330 { 331 int error; 332 333 INP_LOCK_ASSERT(inp); 334 335 MAC_CHECK(inpcb_check_visible, cred, inp, inp->inp_label); 336 MAC_CHECK_PROBE2(inpcb_check_visible, error, cred, inp); 337 338 return (error); 339 } 340 341 void 342 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp) 343 { 344 345 INP_WLOCK_ASSERT(inp); 346 SOCK_LOCK_ASSERT(so); 347 MAC_PERFORM(inpcb_sosetlabel, so, so->so_label, inp, inp->inp_label); 348 } 349 350 void 351 mac_netinet_firewall_reply(struct mbuf *mrecv, struct mbuf *msend) 352 { 353 struct label *mrecvlabel, *msendlabel; 354 355 M_ASSERTPKTHDR(mrecv); 356 M_ASSERTPKTHDR(msend); 357 358 mrecvlabel = mac_mbuf_to_label(mrecv); 359 msendlabel = mac_mbuf_to_label(msend); 360 361 MAC_PERFORM(netinet_firewall_reply, mrecv, mrecvlabel, msend, 362 msendlabel); 363 } 364 365 void 366 mac_netinet_firewall_send(struct mbuf *m) 367 { 368 struct label *label; 369 370 M_ASSERTPKTHDR(m); 371 label = mac_mbuf_to_label(m); 372 MAC_PERFORM(netinet_firewall_send, m, label); 373 } 374 375 /* 376 * These functions really should be referencing the syncache structure 377 * instead of the label. However, due to some of the complexities associated 378 * with exposing this syncache structure we operate directly on it's label 379 * pointer. This should be OK since we aren't making any access control 380 * decisions within this code directly, we are merely allocating and copying 381 * label storage so we can properly initialize mbuf labels for any packets 382 * the syncache code might create. 383 */ 384 void 385 mac_syncache_destroy(struct label **label) 386 { 387 388 if (*label != NULL) { 389 MAC_PERFORM(syncache_destroy_label, *label); 390 mac_labelzone_free(*label); 391 *label = NULL; 392 } 393 } 394 395 int 396 mac_syncache_init(struct label **label) 397 { 398 int error; 399 400 if (mac_labeled & MPC_OBJECT_SYNCACHE) { 401 *label = mac_labelzone_alloc(M_NOWAIT); 402 if (*label == NULL) 403 return (ENOMEM); 404 /* 405 * Since we are holding the inpcb locks the policy can not 406 * allocate policy specific label storage using M_WAITOK. So 407 * we need to do a MAC_CHECK instead of the typical 408 * MAC_PERFORM so we can propagate allocation failures back 409 * to the syncache code. 410 */ 411 MAC_CHECK(syncache_init_label, *label, M_NOWAIT); 412 if (error) { 413 MAC_PERFORM(syncache_destroy_label, *label); 414 mac_labelzone_free(*label); 415 } 416 return (error); 417 } else 418 *label = NULL; 419 return (0); 420 } 421 422 void 423 mac_syncache_create(struct label *label, struct inpcb *inp) 424 { 425 426 INP_WLOCK_ASSERT(inp); 427 MAC_PERFORM(syncache_create, label, inp); 428 } 429 430 void 431 mac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m) 432 { 433 struct label *mlabel; 434 435 M_ASSERTPKTHDR(m); 436 mlabel = mac_mbuf_to_label(m); 437 MAC_PERFORM(syncache_create_mbuf, sc_label, m, mlabel); 438 } 439