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.
9 * This software was developed by Robert Watson and Ilmar Habibulin for the
12 * This software was enhanced by SPARTA ISSO under SPAWAR contract
13 * N66001-04-C-6019 ("SEFOS").
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.
20 * This software was developed at the University of Cambridge Computer
21 * Laboratory with support from a grant from Google, Inc.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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.
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
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include "opt_kdtrace.h"
51 #include <sys/param.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
60 #include <sys/systm.h>
61 #include <sys/mount.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>
69 #include <net/bpfdesc.h>
71 #include <net/if_var.h>
73 #include <security/mac/mac_framework.h>
74 #include <security/mac/mac_internal.h>
75 #include <security/mac/mac_policy.h>
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
82 struct mtx mac_ifnet_mtx;
83 MTX_SYSINIT(mac_ifnet_mtx, &mac_ifnet_mtx, "mac_ifnet", MTX_DEF);
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
93 mac_mbuf_to_label(struct mbuf *m)
100 tag = m_tag_find(m, PACKET_TAG_MACLABEL, NULL);
103 label = (struct label *)(tag+1);
107 static struct label *
108 mac_bpfdesc_label_alloc(void)
112 label = mac_labelzone_alloc(M_WAITOK);
113 MAC_POLICY_PERFORM(bpfdesc_init_label, label);
118 mac_bpfdesc_init(struct bpf_d *d)
121 if (mac_labeled & MPC_OBJECT_BPFDESC)
122 d->bd_label = mac_bpfdesc_label_alloc();
127 static struct label *
128 mac_ifnet_label_alloc(void)
132 label = mac_labelzone_alloc(M_WAITOK);
133 MAC_POLICY_PERFORM(ifnet_init_label, label);
138 mac_ifnet_init(struct ifnet *ifp)
141 if (mac_labeled & MPC_OBJECT_IFNET)
142 ifp->if_label = mac_ifnet_label_alloc();
144 ifp->if_label = NULL;
148 mac_mbuf_tag_init(struct m_tag *tag, int flag)
153 label = (struct label *) (tag + 1);
154 mac_init_label(label);
157 MAC_POLICY_CHECK(mbuf_init_label, label, flag);
159 MAC_POLICY_CHECK_NOSLEEP(mbuf_init_label, label, flag);
161 MAC_POLICY_PERFORM_NOSLEEP(mbuf_destroy_label, label);
162 mac_destroy_label(label);
168 mac_mbuf_init(struct mbuf *m, int flag)
175 if (mac_labeled & MPC_OBJECT_MBUF) {
176 tag = m_tag_get(PACKET_TAG_MACLABEL, sizeof(struct label),
180 error = mac_mbuf_tag_init(tag, flag);
185 m_tag_prepend(m, tag);
191 mac_bpfdesc_label_free(struct label *label)
194 MAC_POLICY_PERFORM_NOSLEEP(bpfdesc_destroy_label, label);
195 mac_labelzone_free(label);
199 mac_bpfdesc_destroy(struct bpf_d *d)
202 if (d->bd_label != NULL) {
203 mac_bpfdesc_label_free(d->bd_label);
209 mac_ifnet_label_free(struct label *label)
212 MAC_POLICY_PERFORM_NOSLEEP(ifnet_destroy_label, label);
213 mac_labelzone_free(label);
217 mac_ifnet_destroy(struct ifnet *ifp)
220 if (ifp->if_label != NULL) {
221 mac_ifnet_label_free(ifp->if_label);
222 ifp->if_label = NULL;
227 mac_mbuf_tag_destroy(struct m_tag *tag)
231 label = (struct label *)(tag+1);
233 MAC_POLICY_PERFORM_NOSLEEP(mbuf_destroy_label, label);
234 mac_destroy_label(label);
238 * mac_mbuf_tag_copy is called when an mbuf header is duplicated, in which
239 * case the labels must also be duplicated.
242 mac_mbuf_tag_copy(struct m_tag *src, struct m_tag *dest)
244 struct label *src_label, *dest_label;
246 src_label = (struct label *)(src+1);
247 dest_label = (struct label *)(dest+1);
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.
253 MAC_POLICY_PERFORM_NOSLEEP(mbuf_copy_label, src_label, dest_label);
257 mac_mbuf_copy(struct mbuf *m_from, struct mbuf *m_to)
259 struct label *src_label, *dest_label;
261 if (mac_policy_count == 0)
264 src_label = mac_mbuf_to_label(m_from);
265 dest_label = mac_mbuf_to_label(m_to);
267 MAC_POLICY_PERFORM_NOSLEEP(mbuf_copy_label, src_label, dest_label);
271 mac_ifnet_copy_label(struct label *src, struct label *dest)
274 MAC_POLICY_PERFORM_NOSLEEP(ifnet_copy_label, src, dest);
278 mac_ifnet_externalize_label(struct label *label, char *elements,
279 char *outbuf, size_t outbuflen)
283 MAC_POLICY_EXTERNALIZE(ifnet, label, elements, outbuf, outbuflen);
289 mac_ifnet_internalize_label(struct label *label, char *string)
293 MAC_POLICY_INTERNALIZE(ifnet, label, string);
299 mac_ifnet_create(struct ifnet *ifp)
302 if (mac_policy_count == 0)
306 MAC_POLICY_PERFORM_NOSLEEP(ifnet_create, ifp, ifp->if_label);
307 MAC_IFNET_UNLOCK(ifp);
311 mac_bpfdesc_create(struct ucred *cred, struct bpf_d *d)
314 MAC_POLICY_PERFORM_NOSLEEP(bpfdesc_create, cred, d, d->bd_label);
318 mac_bpfdesc_create_mbuf(struct bpf_d *d, struct mbuf *m)
322 /* Assume reader lock is enough. */
325 if (mac_policy_count == 0)
328 label = mac_mbuf_to_label(m);
330 MAC_POLICY_PERFORM_NOSLEEP(bpfdesc_create_mbuf, d, d->bd_label, m,
335 mac_ifnet_create_mbuf(struct ifnet *ifp, struct mbuf *m)
339 if (mac_policy_count == 0)
342 label = mac_mbuf_to_label(m);
345 MAC_POLICY_PERFORM_NOSLEEP(ifnet_create_mbuf, ifp, ifp->if_label, m,
347 MAC_IFNET_UNLOCK(ifp);
350 MAC_CHECK_PROBE_DEFINE2(bpfdesc_check_receive, "struct bpf_d *",
354 mac_bpfdesc_check_receive(struct bpf_d *d, struct ifnet *ifp)
358 /* Assume reader lock is enough. */
361 if (mac_policy_count == 0)
365 MAC_POLICY_CHECK_NOSLEEP(bpfdesc_check_receive, d, d->bd_label, ifp,
367 MAC_CHECK_PROBE2(bpfdesc_check_receive, error, d, ifp);
368 MAC_IFNET_UNLOCK(ifp);
373 MAC_CHECK_PROBE_DEFINE2(ifnet_check_transmit, "struct ifnet *",
377 mac_ifnet_check_transmit(struct ifnet *ifp, struct mbuf *m)
384 if (mac_policy_count == 0)
387 label = mac_mbuf_to_label(m);
390 MAC_POLICY_CHECK_NOSLEEP(ifnet_check_transmit, ifp, ifp->if_label, m,
392 MAC_CHECK_PROBE2(ifnet_check_transmit, error, ifp, m);
393 MAC_IFNET_UNLOCK(ifp);
399 mac_ifnet_ioctl_get(struct ucred *cred, struct ifreq *ifr,
402 char *elements, *buffer;
403 struct label *intlabel;
407 if (!(mac_labeled & MPC_OBJECT_IFNET))
410 error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
414 error = mac_check_structmac_consistent(&mac);
418 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
419 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
421 free(elements, M_MACTEMP);
425 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
426 intlabel = mac_ifnet_label_alloc();
428 mac_ifnet_copy_label(ifp->if_label, intlabel);
429 MAC_IFNET_UNLOCK(ifp);
430 error = mac_ifnet_externalize_label(intlabel, elements, buffer,
432 mac_ifnet_label_free(intlabel);
434 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
436 free(buffer, M_MACTEMP);
437 free(elements, M_MACTEMP);
443 mac_ifnet_ioctl_set(struct ucred *cred, struct ifreq *ifr, struct ifnet *ifp)
445 struct label *intlabel;
450 if (!(mac_labeled & MPC_OBJECT_IFNET))
453 error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
457 error = mac_check_structmac_consistent(&mac);
461 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
462 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
464 free(buffer, M_MACTEMP);
468 intlabel = mac_ifnet_label_alloc();
469 error = mac_ifnet_internalize_label(intlabel, buffer);
470 free(buffer, M_MACTEMP);
472 mac_ifnet_label_free(intlabel);
477 * XXX: Note that this is a redundant privilege check, since policies
478 * impose this check themselves if required by the policy
479 * Eventually, this should go away.
481 error = priv_check_cred(cred, PRIV_NET_SETIFMAC, 0);
483 mac_ifnet_label_free(intlabel);
488 MAC_POLICY_CHECK_NOSLEEP(ifnet_check_relabel, cred, ifp,
489 ifp->if_label, intlabel);
491 MAC_IFNET_UNLOCK(ifp);
492 mac_ifnet_label_free(intlabel);
496 MAC_POLICY_PERFORM_NOSLEEP(ifnet_relabel, cred, ifp, ifp->if_label,
498 MAC_IFNET_UNLOCK(ifp);
500 mac_ifnet_label_free(intlabel);