2 * Copyright (c) 2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1999-2002 Robert N. M. Watson
30 * Copyright (c) 2001 Ilmar S. Habibulin
31 * Copyright (c) 2001-2004 Networks Associates Technology, Inc.
32 * Copyright (c) 2006 SPARTA, Inc.
33 * All rights reserved.
35 * This software was developed by Robert Watson and Ilmar Habibulin for the
38 * This software was developed for the FreeBSD Project in part by Network
39 * Associates Laboratories, the Security Research Division of Network
40 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
41 * as part of the DARPA CHATS research program.
43 * This software was enhanced by SPARTA ISSO under SPAWAR contract
44 * N66001-04-C-6019 ("SEFOS").
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
55 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 #include <sys/param.h>
69 #include <sys/kernel.h>
75 #include <bsd/bsm/audit.h>
76 #include <bsd/security/audit/audit.h>
78 #include <security/mac_internal.h>
81 mac_mbuf_to_label(struct mbuf
*mbuf
)
90 if ((mbuf
->m_flags
& M_PKTHDR
) == 0) {
91 printf("%s() got non-header MBUF!\n", __func__
);
95 tag
= m_tag_locate(mbuf
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_MACLABEL
,
98 printf("%s() m_tag_locate() returned NULL! (m->flags %04x)\n",
99 __func__
, mbuf
->m_flags
);
102 label
= (struct label
*)(tag
+ 1);
106 static struct label
*
107 mac_bpfdesc_label_alloc(void)
111 label
= mac_labelzone_alloc(M_WAITOK
);
115 MAC_PERFORM(bpfdesc_label_init
, label
);
120 mac_bpfdesc_label_init(struct bpf_d
*bpf_d
)
124 label
= mac_bpfdesc_label_alloc();
125 mac_bpfdesc_label_set(bpf_d
, label
);
128 static struct label
*
129 mac_ifnet_label_alloc(void)
133 label
= mac_labelzone_alloc(M_WAITOK
);
137 MAC_PERFORM(ifnet_label_init
, label
);
142 mac_ifnet_label_init(struct ifnet
*ifp
)
144 ifp
->if_label
= mac_ifnet_label_alloc();
148 * On failure, caller should cleanup with m_tag_free().
151 mac_mbuf_tag_init(struct m_tag
*tag
, int flag
)
156 label
= (struct label
*) (tag
+ 1);
157 mac_label_init(label
);
159 MAC_CHECK(mbuf_label_init
, label
, flag
);
161 printf("%s(): mpo_mbuf_label_init() failed!\n", __func__
);
168 mac_bpfdesc_label_free(struct label
*label
)
170 MAC_PERFORM(bpfdesc_label_destroy
, label
);
171 mac_labelzone_free(label
);
175 mac_bpfdesc_label_destroy(struct bpf_d
*bpf_d
)
179 label
= mac_bpfdesc_label_get(bpf_d
);
180 mac_bpfdesc_label_free(label
);
181 mac_bpfdesc_label_set(bpf_d
, NULL
);
185 mac_ifnet_label_free(struct label
*label
)
187 MAC_PERFORM(ifnet_label_destroy
, label
);
188 mac_labelzone_free(label
);
192 mac_ifnet_label_destroy(struct ifnet
*ifp
)
194 mac_ifnet_label_free(ifp
->if_label
);
195 ifp
->if_label
= NULL
;
199 mac_ifnet_label_recycle(struct ifnet
*ifp
)
201 MAC_PERFORM(ifnet_label_recycle
, ifp
->if_label
);
205 mac_mbuf_tag_destroy(struct m_tag
*tag
)
209 label
= (struct label
*)(tag
+ 1);
210 MAC_PERFORM(mbuf_label_destroy
, label
);
211 mac_label_destroy(label
);
217 mac_mbuf_tag_copy(struct m_tag
*src
, struct m_tag
*dest
)
219 struct label
*src_label
, *dest_label
;
221 src_label
= (struct label
*)(src
+ 1);
222 dest_label
= (struct label
*)(dest
+ 1);
224 if (src_label
== NULL
|| dest_label
== NULL
) {
229 * mac_mbuf_tag_init() is called on the target tag
230 * in m_tag_copy(), so we don't need to call it here.
232 MAC_PERFORM(mbuf_label_copy
, src_label
, dest_label
);
238 mac_mbuf_label_copy(struct mbuf
*m_from
, struct mbuf
*m_to
)
240 struct label
*src_label
, *dest_label
;
242 src_label
= mac_mbuf_to_label(m_from
);
243 dest_label
= mac_mbuf_to_label(m_to
);
245 MAC_PERFORM(mbuf_label_copy
, src_label
, dest_label
);
249 mac_ifnet_label_copy(struct label
*src
, struct label
*dest
)
251 MAC_PERFORM(ifnet_label_copy
, src
, dest
);
255 mac_ifnet_label_externalize(struct label
*label
, char *elements
,
256 char *outbuf
, size_t outbuflen
)
258 return MAC_EXTERNALIZE(ifnet
, label
, elements
, outbuf
, outbuflen
);
262 mac_ifnet_label_internalize(struct label
*label
, char *string
)
264 return MAC_INTERNALIZE(ifnet
, label
, string
);
268 mac_ifnet_label_associate(struct ifnet
*ifp
)
270 MAC_PERFORM(ifnet_label_associate
, ifp
, ifp
->if_label
);
274 mac_bpfdesc_label_associate(struct ucred
*cred
, struct bpf_d
*bpf_d
)
278 label
= mac_bpfdesc_label_get(bpf_d
);
279 MAC_PERFORM(bpfdesc_label_associate
, cred
, bpf_d
, label
);
283 mac_bpfdesc_check_receive(struct bpf_d
*bpf_d
, struct ifnet
*ifp
)
288 label
= mac_bpfdesc_label_get(bpf_d
);
289 ifnet_lock_shared(ifp
);
290 MAC_CHECK(bpfdesc_check_receive
, bpf_d
, label
, ifp
,
292 ifnet_lock_done(ifp
);
298 mac_mbuf_label_init(struct mbuf
*m
, int flag
)
303 if (mac_label_mbufs
== 0) {
307 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_MACLABEL
,
308 sizeof(struct label
), flag
, m
);
310 printf("%s(): m_tag_alloc() failed!\n", __func__
);
313 error
= mac_mbuf_tag_init(tag
, flag
);
315 printf("%s(): mac_mbuf_tag_init() failed!\n", __func__
);
319 m_tag_prepend(m
, tag
);
324 mac_mbuf_label_associate_bpfdesc(struct bpf_d
*bpf_d
, struct mbuf
*mbuf
)
326 struct label
*m_label
, *b_label
;
328 /* bpf_d must be locked */
330 m_label
= mac_mbuf_to_label(mbuf
);
331 b_label
= mac_bpfdesc_label_get(bpf_d
);
333 MAC_PERFORM(mbuf_label_associate_bpfdesc
, bpf_d
, b_label
, mbuf
,
338 mac_mbuf_label_associate_ifnet(struct ifnet
*ifp
, struct mbuf
*mbuf
)
340 struct label
*m_label
;
342 /* ifp must be locked */
344 m_label
= mac_mbuf_to_label(mbuf
);
346 MAC_PERFORM(mbuf_label_associate_ifnet
, ifp
, ifp
->if_label
, mbuf
,
351 mac_mbuf_label_associate_linklayer(struct ifnet
*ifp
, struct mbuf
*mbuf
)
353 struct label
*m_label
;
355 /* ifp must be locked */
357 m_label
= mac_mbuf_to_label(mbuf
);
359 MAC_PERFORM(mbuf_label_associate_linklayer
, ifp
, ifp
->if_label
, mbuf
,
364 mac_mbuf_label_associate_multicast_encap(struct mbuf
*oldmbuf
,
365 struct ifnet
*ifp
, struct mbuf
*newmbuf
)
367 struct label
*oldmbuflabel
, *newmbuflabel
;
369 oldmbuflabel
= mac_mbuf_to_label(oldmbuf
);
370 newmbuflabel
= mac_mbuf_to_label(newmbuf
);
372 /* ifp must be locked */
374 MAC_PERFORM(mbuf_label_associate_multicast_encap
, oldmbuf
, oldmbuflabel
,
375 ifp
, ifp
->if_label
, newmbuf
, newmbuflabel
);
379 mac_mbuf_label_associate_netlayer(struct mbuf
*oldmbuf
, struct mbuf
*newmbuf
)
381 struct label
*oldmbuflabel
, *newmbuflabel
;
383 oldmbuflabel
= mac_mbuf_to_label(oldmbuf
);
384 newmbuflabel
= mac_mbuf_to_label(newmbuf
);
386 MAC_PERFORM(mbuf_label_associate_netlayer
, oldmbuf
, oldmbuflabel
,
387 newmbuf
, newmbuflabel
);
391 mac_mbuf_label_associate_socket(struct socket
*socket
, struct mbuf
*mbuf
)
396 /* socket must be locked */
398 label
= mac_mbuf_to_label(mbuf
);
400 sotoxsocket(socket
, &xso
);
401 MAC_PERFORM(mbuf_label_associate_socket
, &xso
, socket
->so_label
,
406 mac_ifnet_check_transmit(struct ifnet
*ifp
, struct mbuf
*mbuf
, int family
,
412 label
= mac_mbuf_to_label(mbuf
);
414 ifnet_lock_shared(ifp
);
415 MAC_CHECK(ifnet_check_transmit
, ifp
, ifp
->if_label
, mbuf
, label
,
417 ifnet_lock_done(ifp
);
423 mac_ifnet_label_get(__unused
struct ucred
*cred
, struct ifreq
*ifr
,
426 char *elements
, *buffer
;
427 struct label
*intlabel
;
432 error
= copyin(CAST_USER_ADDR_T(ifr
->ifr_ifru
.ifru_data
),
438 error
= mac_check_structmac_consistent(&mac
);
443 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
444 error
= copyinstr(CAST_USER_ADDR_T(mac
.m_string
), elements
,
447 FREE(elements
, M_MACTEMP
);
450 AUDIT_ARG(mac_string
, elements
);
452 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
453 intlabel
= mac_ifnet_label_alloc();
454 ifnet_lock_shared(ifp
);
455 mac_ifnet_label_copy(ifp
->if_label
, intlabel
);
456 ifnet_lock_done(ifp
);
457 error
= mac_ifnet_label_externalize(intlabel
, elements
,
458 buffer
, mac
.m_buflen
);
459 mac_ifnet_label_free(intlabel
);
460 FREE(elements
, M_MACTEMP
);
463 error
= copyout(buffer
, CAST_USER_ADDR_T(mac
.m_string
),
466 FREE(buffer
, M_MACTEMP
);
472 mac_ifnet_label_set(struct ucred
*cred
, struct ifreq
*ifr
,
475 struct label
*intlabel
;
481 error
= copyin(CAST_USER_ADDR_T(ifr
->ifr_ifru
.ifru_data
),
487 error
= mac_check_structmac_consistent(&mac
);
492 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
493 error
= copyinstr(CAST_USER_ADDR_T(mac
.m_string
), buffer
,
496 FREE(buffer
, M_MACTEMP
);
499 AUDIT_ARG(mac_string
, buffer
);
501 intlabel
= mac_ifnet_label_alloc();
502 error
= mac_ifnet_label_internalize(intlabel
, buffer
);
503 FREE(buffer
, M_MACTEMP
);
505 mac_ifnet_label_free(intlabel
);
510 * XXX: Note that this is a redundant privilege check, since
511 * policies impose this check themselves if required by the
512 * policy. Eventually, this should go away.
514 error
= suser(cred
, NULL
);
516 mac_ifnet_label_free(intlabel
);
520 ifnet_lock_exclusive(ifp
);
521 MAC_CHECK(ifnet_check_label_update
, cred
, ifp
, ifp
->if_label
,
524 ifnet_lock_done(ifp
);
525 mac_ifnet_label_free(intlabel
);
529 MAC_PERFORM(ifnet_label_update
, cred
, ifp
, ifp
->if_label
, intlabel
);
530 ifnet_lock_done(ifp
);
531 mac_ifnet_label_free(intlabel
);