1/*- 2 * Copyright (c) 1999-2002, 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 enhanced by SPARTA ISSO under SPAWAR contract 13 * N66001-04-C-6019 ("SEFOS"). 14 * 15 * This software was developed for the FreeBSD Project in part by Network 16 * Associates Laboratories, the Security Research Division of Network 17 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 18 * as part of the DARPA CHATS research program. 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: head/sys/security/mac/mac_net.c 189797 2009-03-14 16:06:06Z rwatson $");
|
46__FBSDID("$FreeBSD: head/sys/security/mac/mac_net.c 191731 2009-05-01 21:05:40Z rwatson $"); |
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/mac.h> 57#include <sys/priv.h> 58#include <sys/sbuf.h> 59#include <sys/sdt.h> 60#include <sys/systm.h> 61#include <sys/mount.h> 62#include <sys/file.h> 63#include <sys/namei.h> 64#include <sys/protosw.h> 65#include <sys/socket.h> 66#include <sys/socketvar.h> 67#include <sys/sysctl.h> 68 69#include <net/bpfdesc.h> 70#include <net/if.h> 71#include <net/if_var.h> 72 73#include <security/mac/mac_framework.h> 74#include <security/mac/mac_internal.h> 75#include <security/mac/mac_policy.h> 76 77/* 78 * XXXRW: struct ifnet locking is incomplete in the network code, so we use 79 * our own global mutex for struct ifnet. Non-ideal, but should help in the 80 * SMP environment. 81 */ 82struct mtx mac_ifnet_mtx; 83MTX_SYSINIT(mac_ifnet_mtx, &mac_ifnet_mtx, "mac_ifnet", MTX_DEF); 84 85/* 86 * Retrieve the label associated with an mbuf by searching for the tag. 87 * Depending on the value of mac_labelmbufs, it's possible that a label will 88 * not be present, in which case NULL is returned. Policies must handle the 89 * possibility of an mbuf not having label storage if they do not enforce 90 * early loading. 91 */ 92struct label * 93mac_mbuf_to_label(struct mbuf *m) 94{ 95 struct m_tag *tag; 96 struct label *label; 97 98 if (m == NULL) 99 return (NULL); 100 tag = m_tag_find(m, PACKET_TAG_MACLABEL, NULL); 101 if (tag == NULL) 102 return (NULL); 103 label = (struct label *)(tag+1); 104 return (label); 105} 106 107static struct label * 108mac_bpfdesc_label_alloc(void) 109{ 110 struct label *label; 111 112 label = mac_labelzone_alloc(M_WAITOK);
|
113 MAC_PERFORM(bpfdesc_init_label, label);
|
113 MAC_POLICY_PERFORM(bpfdesc_init_label, label); |
114 return (label); 115} 116 117void 118mac_bpfdesc_init(struct bpf_d *d) 119{ 120 121 if (mac_labeled & MPC_OBJECT_BPFDESC) 122 d->bd_label = mac_bpfdesc_label_alloc(); 123 else 124 d->bd_label = NULL; 125} 126 127static struct label * 128mac_ifnet_label_alloc(void) 129{ 130 struct label *label; 131 132 label = mac_labelzone_alloc(M_WAITOK);
|
133 MAC_PERFORM(ifnet_init_label, label);
|
133 MAC_POLICY_PERFORM(ifnet_init_label, label); |
134 return (label); 135} 136 137void 138mac_ifnet_init(struct ifnet *ifp) 139{ 140 141 if (mac_labeled & MPC_OBJECT_IFNET) 142 ifp->if_label = mac_ifnet_label_alloc(); 143 else 144 ifp->if_label = NULL; 145} 146 147int 148mac_mbuf_tag_init(struct m_tag *tag, int flag) 149{ 150 struct label *label; 151 int error; 152 153 label = (struct label *) (tag + 1); 154 mac_init_label(label); 155 156 if (flag & M_WAITOK)
|
157 MAC_CHECK(mbuf_init_label, label, flag);
|
157 MAC_POLICY_CHECK(mbuf_init_label, label, flag); |
158 else
|
159 MAC_CHECK_NOSLEEP(mbuf_init_label, label, flag);
|
159 MAC_POLICY_CHECK_NOSLEEP(mbuf_init_label, label, flag); |
160 if (error) {
|
161 MAC_PERFORM_NOSLEEP(mbuf_destroy_label, label);
|
161 MAC_POLICY_PERFORM_NOSLEEP(mbuf_destroy_label, label); |
162 mac_destroy_label(label); 163 } 164 return (error); 165} 166 167int 168mac_mbuf_init(struct mbuf *m, int flag) 169{ 170 struct m_tag *tag; 171 int error; 172 173 M_ASSERTPKTHDR(m); 174 175 if (mac_labeled & MPC_OBJECT_MBUF) { 176 tag = m_tag_get(PACKET_TAG_MACLABEL, sizeof(struct label), 177 flag); 178 if (tag == NULL) 179 return (ENOMEM); 180 error = mac_mbuf_tag_init(tag, flag); 181 if (error) { 182 m_tag_free(tag); 183 return (error); 184 } 185 m_tag_prepend(m, tag); 186 } 187 return (0); 188} 189 190static void 191mac_bpfdesc_label_free(struct label *label) 192{ 193
|
194 MAC_PERFORM_NOSLEEP(bpfdesc_destroy_label, label);
|
194 MAC_POLICY_PERFORM_NOSLEEP(bpfdesc_destroy_label, label); |
195 mac_labelzone_free(label); 196} 197 198void 199mac_bpfdesc_destroy(struct bpf_d *d) 200{ 201 202 if (d->bd_label != NULL) { 203 mac_bpfdesc_label_free(d->bd_label); 204 d->bd_label = NULL; 205 } 206} 207 208static void 209mac_ifnet_label_free(struct label *label) 210{ 211
|
212 MAC_PERFORM_NOSLEEP(ifnet_destroy_label, label);
|
212 MAC_POLICY_PERFORM_NOSLEEP(ifnet_destroy_label, label); |
213 mac_labelzone_free(label); 214} 215 216void 217mac_ifnet_destroy(struct ifnet *ifp) 218{ 219 220 if (ifp->if_label != NULL) { 221 mac_ifnet_label_free(ifp->if_label); 222 ifp->if_label = NULL; 223 } 224} 225 226void 227mac_mbuf_tag_destroy(struct m_tag *tag) 228{ 229 struct label *label; 230 231 label = (struct label *)(tag+1); 232
|
233 MAC_PERFORM_NOSLEEP(mbuf_destroy_label, label);
|
233 MAC_POLICY_PERFORM_NOSLEEP(mbuf_destroy_label, label); |
234 mac_destroy_label(label); 235} 236 237/* 238 * mac_mbuf_tag_copy is called when an mbuf header is duplicated, in which 239 * case the labels must also be duplicated. 240 */ 241void 242mac_mbuf_tag_copy(struct m_tag *src, struct m_tag *dest) 243{ 244 struct label *src_label, *dest_label; 245 246 src_label = (struct label *)(src+1); 247 dest_label = (struct label *)(dest+1); 248 249 /* 250 * mac_mbuf_tag_init() is called on the target tag in m_tag_copy(), 251 * so we don't need to call it here. 252 */
|
253 MAC_PERFORM_NOSLEEP(mbuf_copy_label, src_label, dest_label);
|
253 MAC_POLICY_PERFORM_NOSLEEP(mbuf_copy_label, src_label, dest_label); |
254} 255 256void 257mac_mbuf_copy(struct mbuf *m_from, struct mbuf *m_to) 258{ 259 struct label *src_label, *dest_label; 260 261 src_label = mac_mbuf_to_label(m_from); 262 dest_label = mac_mbuf_to_label(m_to); 263
|
264 MAC_PERFORM_NOSLEEP(mbuf_copy_label, src_label, dest_label);
|
264 MAC_POLICY_PERFORM_NOSLEEP(mbuf_copy_label, src_label, dest_label); |
265} 266 267static void 268mac_ifnet_copy_label(struct label *src, struct label *dest) 269{ 270
|
271 MAC_PERFORM_NOSLEEP(ifnet_copy_label, src, dest);
|
271 MAC_POLICY_PERFORM_NOSLEEP(ifnet_copy_label, src, dest); |
272} 273 274static int 275mac_ifnet_externalize_label(struct label *label, char *elements, 276 char *outbuf, size_t outbuflen) 277{ 278 int error; 279
|
280 MAC_EXTERNALIZE(ifnet, label, elements, outbuf, outbuflen);
|
280 MAC_POLICY_EXTERNALIZE(ifnet, label, elements, outbuf, outbuflen); |
281 282 return (error); 283} 284 285static int 286mac_ifnet_internalize_label(struct label *label, char *string) 287{ 288 int error; 289
|
290 MAC_INTERNALIZE(ifnet, label, string);
|
290 MAC_POLICY_INTERNALIZE(ifnet, label, string); |
291 292 return (error); 293} 294 295void 296mac_ifnet_create(struct ifnet *ifp) 297{ 298 299 MAC_IFNET_LOCK(ifp);
|
300 MAC_PERFORM_NOSLEEP(ifnet_create, ifp, ifp->if_label);
|
300 MAC_POLICY_PERFORM_NOSLEEP(ifnet_create, ifp, ifp->if_label); |
301 MAC_IFNET_UNLOCK(ifp); 302} 303 304void 305mac_bpfdesc_create(struct ucred *cred, struct bpf_d *d) 306{ 307
|
308 MAC_PERFORM_NOSLEEP(bpfdesc_create, cred, d, d->bd_label);
|
308 MAC_POLICY_PERFORM_NOSLEEP(bpfdesc_create, cred, d, d->bd_label); |
309} 310 311void 312mac_bpfdesc_create_mbuf(struct bpf_d *d, struct mbuf *m) 313{ 314 struct label *label; 315 316 BPFD_LOCK_ASSERT(d); 317 318 label = mac_mbuf_to_label(m); 319
|
320 MAC_PERFORM_NOSLEEP(bpfdesc_create_mbuf, d, d->bd_label, m, label);
|
320 MAC_POLICY_PERFORM_NOSLEEP(bpfdesc_create_mbuf, d, d->bd_label, m, 321 label); |
322} 323 324void 325mac_ifnet_create_mbuf(struct ifnet *ifp, struct mbuf *m) 326{ 327 struct label *label; 328 329 label = mac_mbuf_to_label(m); 330 331 MAC_IFNET_LOCK(ifp);
|
331 MAC_PERFORM_NOSLEEP(ifnet_create_mbuf, ifp, ifp->if_label, m, label);
|
332 MAC_POLICY_PERFORM_NOSLEEP(ifnet_create_mbuf, ifp, ifp->if_label, m, 333 label); |
334 MAC_IFNET_UNLOCK(ifp); 335} 336 337MAC_CHECK_PROBE_DEFINE2(bpfdesc_check_receive, "struct bpf_d *", 338 "struct ifnet *"); 339 340int 341mac_bpfdesc_check_receive(struct bpf_d *d, struct ifnet *ifp) 342{ 343 int error; 344 345 BPFD_LOCK_ASSERT(d); 346 347 MAC_IFNET_LOCK(ifp);
|
346 MAC_CHECK_NOSLEEP(bpfdesc_check_receive, d, d->bd_label, ifp,
|
348 MAC_POLICY_CHECK_NOSLEEP(bpfdesc_check_receive, d, d->bd_label, ifp, |
349 ifp->if_label); 350 MAC_CHECK_PROBE2(bpfdesc_check_receive, error, d, ifp); 351 MAC_IFNET_UNLOCK(ifp); 352 353 return (error); 354} 355 356MAC_CHECK_PROBE_DEFINE2(ifnet_check_transmit, "struct ifnet *", 357 "struct mbuf *"); 358 359int 360mac_ifnet_check_transmit(struct ifnet *ifp, struct mbuf *m) 361{ 362 struct label *label; 363 int error; 364 365 M_ASSERTPKTHDR(m); 366 367 label = mac_mbuf_to_label(m); 368 369 MAC_IFNET_LOCK(ifp);
|
368 MAC_CHECK_NOSLEEP(ifnet_check_transmit, ifp, ifp->if_label, m,
|
370 MAC_POLICY_CHECK_NOSLEEP(ifnet_check_transmit, ifp, ifp->if_label, m, |
371 label); 372 MAC_CHECK_PROBE2(ifnet_check_transmit, error, ifp, m); 373 MAC_IFNET_UNLOCK(ifp); 374 375 return (error); 376} 377 378int 379mac_ifnet_ioctl_get(struct ucred *cred, struct ifreq *ifr, 380 struct ifnet *ifp) 381{ 382 char *elements, *buffer; 383 struct label *intlabel; 384 struct mac mac; 385 int error; 386 387 if (!(mac_labeled & MPC_OBJECT_IFNET)) 388 return (EINVAL); 389 390 error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac)); 391 if (error) 392 return (error); 393 394 error = mac_check_structmac_consistent(&mac); 395 if (error) 396 return (error); 397 398 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); 399 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL); 400 if (error) { 401 free(elements, M_MACTEMP); 402 return (error); 403 } 404 405 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO); 406 intlabel = mac_ifnet_label_alloc(); 407 MAC_IFNET_LOCK(ifp); 408 mac_ifnet_copy_label(ifp->if_label, intlabel); 409 MAC_IFNET_UNLOCK(ifp); 410 error = mac_ifnet_externalize_label(intlabel, elements, buffer, 411 mac.m_buflen); 412 mac_ifnet_label_free(intlabel); 413 if (error == 0) 414 error = copyout(buffer, mac.m_string, strlen(buffer)+1); 415 416 free(buffer, M_MACTEMP); 417 free(elements, M_MACTEMP); 418 419 return (error); 420} 421 422int 423mac_ifnet_ioctl_set(struct ucred *cred, struct ifreq *ifr, struct ifnet *ifp) 424{ 425 struct label *intlabel; 426 struct mac mac; 427 char *buffer; 428 int error; 429 430 if (!(mac_labeled & MPC_OBJECT_IFNET)) 431 return (EINVAL); 432 433 error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac)); 434 if (error) 435 return (error); 436 437 error = mac_check_structmac_consistent(&mac); 438 if (error) 439 return (error); 440 441 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); 442 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL); 443 if (error) { 444 free(buffer, M_MACTEMP); 445 return (error); 446 } 447 448 intlabel = mac_ifnet_label_alloc(); 449 error = mac_ifnet_internalize_label(intlabel, buffer); 450 free(buffer, M_MACTEMP); 451 if (error) { 452 mac_ifnet_label_free(intlabel); 453 return (error); 454 } 455 456 /* 457 * XXX: Note that this is a redundant privilege check, since policies 458 * impose this check themselves if required by the policy 459 * Eventually, this should go away. 460 */ 461 error = priv_check_cred(cred, PRIV_NET_SETIFMAC, 0); 462 if (error) { 463 mac_ifnet_label_free(intlabel); 464 return (error); 465 } 466 467 MAC_IFNET_LOCK(ifp);
|
466 MAC_CHECK_NOSLEEP(ifnet_check_relabel, cred, ifp, ifp->if_label,
467 intlabel);
|
468 MAC_POLICY_CHECK_NOSLEEP(ifnet_check_relabel, cred, ifp, 469 ifp->if_label, intlabel); |
470 if (error) { 471 MAC_IFNET_UNLOCK(ifp); 472 mac_ifnet_label_free(intlabel); 473 return (error); 474 } 475
|
474 MAC_PERFORM_NOSLEEP(ifnet_relabel, cred, ifp, ifp->if_label,
|
476 MAC_POLICY_PERFORM_NOSLEEP(ifnet_relabel, cred, ifp, ifp->if_label, |
477 intlabel); 478 MAC_IFNET_UNLOCK(ifp); 479 480 mac_ifnet_label_free(intlabel); 481 return (0); 482}
|