2 * Copyright (c) 1999-2002 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2004 Networks Associates Technology, Inc.
7 * This software was developed by Robert Watson and Ilmar Habibulin for the
10 * This software was developed for the FreeBSD Project in part by Network
11 * Associates Laboratories, the Security Research Division of Network
12 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
13 * as part of the DARPA CHATS research program.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
50 #include <sys/systm.h>
51 #include <sys/mount.h>
53 #include <sys/namei.h>
54 #include <sys/protosw.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
59 #include <net/bpfdesc.h>
61 #include <net/if_var.h>
63 #include <security/mac/mac_framework.h>
64 #include <security/mac/mac_internal.h>
65 #include <security/mac/mac_policy.h>
68 * XXXRW: struct ifnet locking is incomplete in the network code, so we use
69 * our own global mutex for struct ifnet. Non-ideal, but should help in the
72 static struct mtx mac_ifnet_mtx;
73 MTX_SYSINIT(mac_ifnet_mtx, &mac_ifnet_mtx, "mac_ifnet", MTX_DEF);
74 #define MAC_IFNET_LOCK(ifp) mtx_lock(&mac_ifnet_mtx)
75 #define MAC_IFNET_UNLOCK(ifp) mtx_unlock(&mac_ifnet_mtx)
78 * Retrieve the label associated with an mbuf by searching for the tag.
79 * Depending on the value of mac_labelmbufs, it's possible that a label will
80 * not be present, in which case NULL is returned. Policies must handle the
81 * possibility of an mbuf not having label storage if they do not enforce
85 mac_mbuf_to_label(struct mbuf *m)
92 tag = m_tag_find(m, PACKET_TAG_MACLABEL, NULL);
95 label = (struct label *)(tag+1);
100 mac_bpfdesc_label_alloc(void)
104 label = mac_labelzone_alloc(M_WAITOK);
105 MAC_PERFORM(init_bpfdesc_label, label);
110 mac_init_bpfdesc(struct bpf_d *d)
113 d->bd_label = mac_bpfdesc_label_alloc();
116 static struct label *
117 mac_ifnet_label_alloc(void)
121 label = mac_labelzone_alloc(M_WAITOK);
122 MAC_PERFORM(init_ifnet_label, label);
127 mac_init_ifnet(struct ifnet *ifp)
130 ifp->if_label = mac_ifnet_label_alloc();
134 mac_init_mbuf_tag(struct m_tag *tag, int flag)
139 label = (struct label *) (tag + 1);
140 mac_init_label(label);
142 MAC_CHECK(init_mbuf_label, label, flag);
144 MAC_PERFORM(destroy_mbuf_label, label);
145 mac_destroy_label(label);
151 mac_init_mbuf(struct mbuf *m, int flag)
158 #ifndef MAC_ALWAYS_LABEL_MBUF
160 * If conditionally allocating mbuf labels, don't allocate unless
166 tag = m_tag_get(PACKET_TAG_MACLABEL, sizeof(struct label),
170 error = mac_init_mbuf_tag(tag, flag);
175 m_tag_prepend(m, tag);
180 mac_bpfdesc_label_free(struct label *label)
183 MAC_PERFORM(destroy_bpfdesc_label, label);
184 mac_labelzone_free(label);
188 mac_destroy_bpfdesc(struct bpf_d *d)
191 mac_bpfdesc_label_free(d->bd_label);
196 mac_ifnet_label_free(struct label *label)
199 MAC_PERFORM(destroy_ifnet_label, label);
200 mac_labelzone_free(label);
204 mac_destroy_ifnet(struct ifnet *ifp)
207 mac_ifnet_label_free(ifp->if_label);
208 ifp->if_label = NULL;
212 mac_destroy_mbuf_tag(struct m_tag *tag)
216 label = (struct label *)(tag+1);
218 MAC_PERFORM(destroy_mbuf_label, label);
219 mac_destroy_label(label);
223 * mac_copy_mbuf_tag is called when an mbuf header is duplicated, in which
224 * case the labels must also be duplicated.
227 mac_copy_mbuf_tag(struct m_tag *src, struct m_tag *dest)
229 struct label *src_label, *dest_label;
231 src_label = (struct label *)(src+1);
232 dest_label = (struct label *)(dest+1);
235 * mac_init_mbuf_tag() is called on the target tag in m_tag_copy(),
236 * so we don't need to call it here.
238 MAC_PERFORM(copy_mbuf_label, src_label, dest_label);
242 mac_copy_mbuf(struct mbuf *m_from, struct mbuf *m_to)
244 struct label *src_label, *dest_label;
246 src_label = mac_mbuf_to_label(m_from);
247 dest_label = mac_mbuf_to_label(m_to);
249 MAC_PERFORM(copy_mbuf_label, src_label, dest_label);
253 mac_copy_ifnet_label(struct label *src, struct label *dest)
256 MAC_PERFORM(copy_ifnet_label, src, dest);
260 mac_externalize_ifnet_label(struct label *label, char *elements,
261 char *outbuf, size_t outbuflen)
265 MAC_EXTERNALIZE(ifnet, label, elements, outbuf, outbuflen);
271 mac_internalize_ifnet_label(struct label *label, char *string)
275 MAC_INTERNALIZE(ifnet, label, string);
281 mac_create_ifnet(struct ifnet *ifp)
285 MAC_PERFORM(create_ifnet, ifp, ifp->if_label);
286 MAC_IFNET_UNLOCK(ifp);
290 mac_create_bpfdesc(struct ucred *cred, struct bpf_d *d)
293 MAC_PERFORM(create_bpfdesc, cred, d, d->bd_label);
297 mac_create_mbuf_from_bpfdesc(struct bpf_d *d, struct mbuf *m)
303 label = mac_mbuf_to_label(m);
305 MAC_PERFORM(create_mbuf_from_bpfdesc, d, d->bd_label, m, label);
309 mac_create_mbuf_linklayer(struct ifnet *ifp, struct mbuf *m)
313 label = mac_mbuf_to_label(m);
316 MAC_PERFORM(create_mbuf_linklayer, ifp, ifp->if_label, m, label);
317 MAC_IFNET_UNLOCK(ifp);
321 mac_create_mbuf_from_ifnet(struct ifnet *ifp, struct mbuf *m)
325 label = mac_mbuf_to_label(m);
328 MAC_PERFORM(create_mbuf_from_ifnet, ifp, ifp->if_label, m, label);
329 MAC_IFNET_UNLOCK(ifp);
333 mac_create_mbuf_multicast_encap(struct mbuf *m, struct ifnet *ifp,
336 struct label *mlabel, *mnewlabel;
338 mlabel = mac_mbuf_to_label(m);
339 mnewlabel = mac_mbuf_to_label(mnew);
342 MAC_PERFORM(create_mbuf_multicast_encap, m, mlabel, ifp,
343 ifp->if_label, mnew, mnewlabel);
344 MAC_IFNET_UNLOCK(ifp);
348 mac_create_mbuf_netlayer(struct mbuf *m, struct mbuf *mnew)
350 struct label *mlabel, *mnewlabel;
352 mlabel = mac_mbuf_to_label(m);
353 mnewlabel = mac_mbuf_to_label(mnew);
355 MAC_PERFORM(create_mbuf_netlayer, m, mlabel, mnew, mnewlabel);
359 mac_check_bpfdesc_receive(struct bpf_d *d, struct ifnet *ifp)
366 MAC_CHECK(check_bpfdesc_receive, d, d->bd_label, ifp, ifp->if_label);
367 MAC_IFNET_UNLOCK(ifp);
373 mac_check_ifnet_transmit(struct ifnet *ifp, struct mbuf *m)
380 label = mac_mbuf_to_label(m);
383 MAC_CHECK(check_ifnet_transmit, ifp, ifp->if_label, m, label);
384 MAC_IFNET_UNLOCK(ifp);
390 mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
393 char *elements, *buffer;
394 struct label *intlabel;
398 error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
402 error = mac_check_structmac_consistent(&mac);
406 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
407 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
409 free(elements, M_MACTEMP);
413 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
414 intlabel = mac_ifnet_label_alloc();
416 mac_copy_ifnet_label(ifp->if_label, intlabel);
417 MAC_IFNET_UNLOCK(ifp);
418 error = mac_externalize_ifnet_label(intlabel, elements, buffer,
420 mac_ifnet_label_free(intlabel);
422 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
424 free(buffer, M_MACTEMP);
425 free(elements, M_MACTEMP);
431 mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr, struct ifnet *ifp)
433 struct label *intlabel;
438 error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
442 error = mac_check_structmac_consistent(&mac);
446 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
447 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
449 free(buffer, M_MACTEMP);
453 intlabel = mac_ifnet_label_alloc();
454 error = mac_internalize_ifnet_label(intlabel, buffer);
455 free(buffer, M_MACTEMP);
457 mac_ifnet_label_free(intlabel);
462 * XXX: Note that this is a redundant privilege check, since policies
463 * impose this check themselves if required by the policy
464 * Eventually, this should go away.
466 error = priv_check_cred(cred, PRIV_NET_SETIFMAC, 0);
468 mac_ifnet_label_free(intlabel);
473 MAC_CHECK(check_ifnet_relabel, cred, ifp, ifp->if_label, intlabel);
475 MAC_IFNET_UNLOCK(ifp);
476 mac_ifnet_label_free(intlabel);
480 MAC_PERFORM(relabel_ifnet, cred, ifp, ifp->if_label, intlabel);
481 MAC_IFNET_UNLOCK(ifp);
483 mac_ifnet_label_free(intlabel);