2 * Copyright (c) 2007-2012 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-2005 Networks Associates Technology, Inc.
32 * Copyright (c) 2005 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 McAfee
39 * Research, the Technology Research Division of Network Associates, Inc.
40 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
41 * 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/cdefs.h>
70 #include <sys/param.h>
71 #include <sys/kernel.h>
73 #include <sys/malloc.h>
75 #include <sys/systm.h>
76 #include <sys/mount.h>
78 #include <sys/namei.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/sysctl.h>
83 #include <sys/kpi_socket.h>
85 #include <security/mac_internal.h>
87 #if CONFIG_MACF_SOCKET
89 mac_socket_label_alloc(int flag
)
94 label
= mac_labelzone_alloc(flag
);
99 MAC_CHECK(socket_label_init
, label
, flag
);
101 MAC_PERFORM(socket_label_destroy
, label
);
102 mac_labelzone_free(label
);
109 static struct label
*
110 mac_socket_peer_label_alloc(int flag
)
115 label
= mac_labelzone_alloc(flag
);
120 MAC_CHECK(socketpeer_label_init
, label
, flag
);
122 MAC_PERFORM(socketpeer_label_destroy
, label
);
123 mac_labelzone_free(label
);
131 mac_socket_label_init(struct socket
*so
, int flag
)
133 so
->so_label
= mac_socket_label_alloc(flag
);
134 if (so
->so_label
== NULL
) {
137 so
->so_peerlabel
= mac_socket_peer_label_alloc(flag
);
138 if (so
->so_peerlabel
== NULL
) {
139 mac_socket_label_free(so
->so_label
);
147 mac_socket_label_free(struct label
*label
)
149 MAC_PERFORM(socket_label_destroy
, label
);
150 mac_labelzone_free(label
);
154 mac_socket_peer_label_free(struct label
*label
)
156 MAC_PERFORM(socketpeer_label_destroy
, label
);
157 mac_labelzone_free(label
);
161 mac_socket_label_destroy(struct socket
*so
)
163 if (so
->so_label
!= NULL
) {
164 mac_socket_label_free(so
->so_label
);
167 if (so
->so_peerlabel
!= NULL
) {
168 mac_socket_peer_label_free(so
->so_peerlabel
);
169 so
->so_peerlabel
= NULL
;
174 mac_socket_label_copy(struct label
*src
, struct label
*dest
)
176 MAC_PERFORM(socket_label_copy
, src
, dest
);
180 mac_socket_label_externalize(struct label
*label
, char *elements
,
181 char *outbuf
, size_t outbuflen
)
185 error
= MAC_EXTERNALIZE(socket
, label
, elements
, outbuf
, outbuflen
);
191 mac_socketpeer_label_externalize(struct label
*label
, char *elements
,
192 char *outbuf
, size_t outbuflen
)
196 error
= MAC_EXTERNALIZE(socketpeer
, label
, elements
, outbuf
, outbuflen
);
202 mac_socket_label_internalize(struct label
*label
, char *string
)
206 error
= MAC_INTERNALIZE(socket
, label
, string
);
212 mac_socket_label_associate(struct ucred
*cred
, struct socket
*so
)
214 #if SECURITY_MAC_CHECK_ENFORCE
215 /* 21167099 - only check if we allow write */
216 if (!mac_socket_enforce
) {
221 MAC_PERFORM(socket_label_associate
, cred
,
222 (socket_t
)so
, so
->so_label
);
226 mac_socket_label_associate_accept(struct socket
*oldsocket
,
227 struct socket
*newsocket
)
229 #if SECURITY_MAC_CHECK_ENFORCE
230 /* 21167099 - only check if we allow write */
231 if (!mac_socket_enforce
) {
236 MAC_PERFORM(socket_label_associate_accept
,
237 (socket_t
)oldsocket
, oldsocket
->so_label
,
238 (socket_t
)newsocket
, newsocket
->so_label
);
241 #if CONFIG_MACF_SOCKET && CONFIG_MACF_NET
243 mac_socketpeer_label_associate_mbuf(struct mbuf
*mbuf
, struct socket
*so
)
247 #if SECURITY_MAC_CHECK_ENFORCE
248 /* 21167099 - only check if we allow write */
249 if (!mac_socket_enforce
&& !mac_net_enforce
) {
254 label
= mac_mbuf_to_label(mbuf
);
256 /* Policy must deal with NULL label (unlabeled mbufs) */
257 MAC_PERFORM(socketpeer_label_associate_mbuf
, mbuf
, label
,
258 (socket_t
)so
, so
->so_peerlabel
);
262 mac_socketpeer_label_associate_mbuf(__unused
struct mbuf
*mbuf
,
263 __unused
struct socket
*so
)
270 mac_socketpeer_label_associate_socket(struct socket
*oldsocket
,
271 struct socket
*newsocket
)
273 #if SECURITY_MAC_CHECK_ENFORCE
274 /* 21167099 - only check if we allow write */
275 if (!mac_socket_enforce
) {
280 MAC_PERFORM(socketpeer_label_associate_socket
,
281 (socket_t
)oldsocket
, oldsocket
->so_label
,
282 (socket_t
)newsocket
, newsocket
->so_peerlabel
);
286 mac_socket_check_kqfilter(kauth_cred_t cred
, struct knote
*kn
,
291 #if SECURITY_MAC_CHECK_ENFORCE
292 /* 21167099 - only check if we allow write */
293 if (!mac_socket_enforce
) {
298 MAC_CHECK(socket_check_kqfilter
, cred
, kn
,
299 (socket_t
)so
, so
->so_label
);
305 mac_socket_check_select(kauth_cred_t cred
, struct socket
*so
, int which
)
309 #if SECURITY_MAC_CHECK_ENFORCE
310 /* 21167099 - only check if we allow write */
311 if (!mac_socket_enforce
) {
316 MAC_CHECK(socket_check_select
, cred
,
317 (socket_t
)so
, so
->so_label
, which
);
321 mac_socket_check_label_update(kauth_cred_t cred
, struct socket
*so
,
322 struct label
*newlabel
)
326 #if SECURITY_MAC_CHECK_ENFORCE
327 /* 21167099 - only check if we allow write */
328 if (!mac_socket_enforce
) {
333 MAC_CHECK(socket_check_label_update
, cred
,
334 (socket_t
)so
, so
->so_label
,
340 mac_socket_label_update(kauth_cred_t cred
, struct socket
*so
, struct label
*label
)
344 #if SECURITY_MAC_CHECK_ENFORCE
345 /* 21167099 - only check if we allow write */
346 if (!mac_socket_enforce
) {
351 error
= mac_socket_check_label_update(cred
, so
, label
);
356 MAC_PERFORM(socket_label_update
, cred
,
357 (socket_t
)so
, so
->so_label
, label
);
361 * If the protocol has expressed interest in socket layer changes,
362 * such as if it needs to propagate changes to a cached pcb
363 * label from the socket, notify it of the label change while
364 * holding the socket lock.
365 * XXXMAC - are there cases when we should not do this?
367 mac_inpcb_label_update(so
);
373 mac_setsockopt_label(kauth_cred_t cred
, struct socket
*so
, struct mac
*mac
)
375 struct label
*intlabel
;
380 error
= mac_check_structmac_consistent(mac
);
385 MALLOC(buffer
, char *, mac
->m_buflen
, M_MACTEMP
, M_WAITOK
);
386 error
= copyinstr(CAST_USER_ADDR_T(mac
->m_string
), buffer
,
387 mac
->m_buflen
, &len
);
389 FREE(buffer
, M_MACTEMP
);
393 intlabel
= mac_socket_label_alloc(MAC_WAITOK
);
394 error
= mac_socket_label_internalize(intlabel
, buffer
);
395 FREE(buffer
, M_MACTEMP
);
400 error
= mac_socket_label_update(cred
, so
, intlabel
);
402 mac_socket_label_free(intlabel
);
407 mac_socket_label_get(__unused kauth_cred_t cred
, struct socket
*so
,
410 char *buffer
, *elements
;
411 struct label
*intlabel
;
415 error
= mac_check_structmac_consistent(mac
);
420 MALLOC(elements
, char *, mac
->m_buflen
, M_MACTEMP
, M_WAITOK
);
421 error
= copyinstr(CAST_USER_ADDR_T(mac
->m_string
), elements
,
422 mac
->m_buflen
, &len
);
424 FREE(elements
, M_MACTEMP
);
428 MALLOC(buffer
, char *, mac
->m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
429 intlabel
= mac_socket_label_alloc(MAC_WAITOK
);
430 mac_socket_label_copy(so
->so_label
, intlabel
);
431 error
= mac_socket_label_externalize(intlabel
, elements
, buffer
,
433 mac_socket_label_free(intlabel
);
435 error
= copyout(buffer
, CAST_USER_ADDR_T(mac
->m_string
),
439 FREE(buffer
, M_MACTEMP
);
440 FREE(elements
, M_MACTEMP
);
446 mac_socketpeer_label_get(__unused kauth_cred_t cred
, struct socket
*so
,
449 char *elements
, *buffer
;
450 struct label
*intlabel
;
454 error
= mac_check_structmac_consistent(mac
);
459 MALLOC(elements
, char *, mac
->m_buflen
, M_MACTEMP
, M_WAITOK
);
460 error
= copyinstr(CAST_USER_ADDR_T(mac
->m_string
), elements
,
461 mac
->m_buflen
, &len
);
463 FREE(elements
, M_MACTEMP
);
467 MALLOC(buffer
, char *, mac
->m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
468 intlabel
= mac_socket_label_alloc(MAC_WAITOK
);
469 mac_socket_label_copy(so
->so_peerlabel
, intlabel
);
470 error
= mac_socketpeer_label_externalize(intlabel
, elements
, buffer
,
472 mac_socket_label_free(intlabel
);
474 error
= copyout(buffer
, CAST_USER_ADDR_T(mac
->m_string
),
478 FREE(buffer
, M_MACTEMP
);
479 FREE(elements
, M_MACTEMP
);
484 #endif /* MAC_SOCKET */
487 mac_socket_check_accept(kauth_cred_t cred
, struct socket
*so
)
491 #if SECURITY_MAC_CHECK_ENFORCE
492 /* 21167099 - only check if we allow write */
493 if (!mac_socket_enforce
) {
498 MAC_CHECK(socket_check_accept
, cred
,
499 (socket_t
)so
, so
->so_label
);
503 #if CONFIG_MACF_SOCKET_SUBSET
505 mac_socket_check_accepted(kauth_cred_t cred
, struct socket
*so
)
507 struct sockaddr
*sockaddr
;
510 #if SECURITY_MAC_CHECK_ENFORCE
511 /* 21167099 - only check if we allow write */
512 if (!mac_socket_enforce
) {
517 if (sock_getaddr((socket_t
)so
, &sockaddr
, 1) != 0) {
518 error
= ECONNABORTED
;
520 MAC_CHECK(socket_check_accepted
, cred
,
521 (socket_t
)so
, so
->so_label
, sockaddr
);
522 sock_freeaddr(sockaddr
);
529 mac_socket_check_bind(kauth_cred_t ucred
, struct socket
*so
,
530 struct sockaddr
*sockaddr
)
534 #if SECURITY_MAC_CHECK_ENFORCE
535 /* 21167099 - only check if we allow write */
536 if (!mac_socket_enforce
) {
541 MAC_CHECK(socket_check_bind
, ucred
,
542 (socket_t
)so
, so
->so_label
, sockaddr
);
547 mac_socket_check_connect(kauth_cred_t cred
, struct socket
*so
,
548 struct sockaddr
*sockaddr
)
552 #if SECURITY_MAC_CHECK_ENFORCE
553 /* 21167099 - only check if we allow write */
554 if (!mac_socket_enforce
) {
559 MAC_CHECK(socket_check_connect
, cred
,
560 (socket_t
)so
, so
->so_label
,
566 mac_socket_check_create(kauth_cred_t cred
, int domain
, int type
, int protocol
)
570 #if SECURITY_MAC_CHECK_ENFORCE
571 /* 21167099 - only check if we allow write */
572 if (!mac_socket_enforce
) {
577 MAC_CHECK(socket_check_create
, cred
, domain
, type
, protocol
);
581 #if CONFIG_MACF_SOCKET && CONFIG_MACF_NET
583 mac_socket_check_deliver(struct socket
*so
, struct mbuf
*mbuf
)
588 #if SECURITY_MAC_CHECK_ENFORCE
589 /* 21167099 - only check if we allow write */
590 if (!mac_socket_enforce
) {
595 label
= mac_mbuf_to_label(mbuf
);
597 /* Policy must deal with NULL label (unlabeled mbufs) */
598 MAC_CHECK(socket_check_deliver
,
599 (socket_t
)so
, so
->so_label
, mbuf
, label
);
604 mac_socket_check_deliver(__unused
struct socket
*so
, __unused
struct mbuf
*mbuf
)
611 mac_socket_check_ioctl(kauth_cred_t cred
, struct socket
*so
,
616 #if SECURITY_MAC_CHECK_ENFORCE
617 /* 21167099 - only check if we allow write */
618 if (!mac_socket_enforce
) {
623 MAC_CHECK(socket_check_ioctl
, cred
,
624 (socket_t
)so
, cmd
, so
->so_label
);
629 mac_socket_check_stat(kauth_cred_t cred
, struct socket
*so
)
633 #if SECURITY_MAC_CHECK_ENFORCE
634 /* 21167099 - only check if we allow write */
635 if (!mac_socket_enforce
) {
640 MAC_CHECK(socket_check_stat
, cred
,
641 (socket_t
)so
, so
->so_label
);
646 mac_socket_check_listen(kauth_cred_t cred
, struct socket
*so
)
650 #if SECURITY_MAC_CHECK_ENFORCE
651 /* 21167099 - only check if we allow write */
652 if (!mac_socket_enforce
) {
657 MAC_CHECK(socket_check_listen
, cred
,
658 (socket_t
)so
, so
->so_label
);
663 mac_socket_check_receive(kauth_cred_t cred
, struct socket
*so
)
667 #if SECURITY_MAC_CHECK_ENFORCE
668 /* 21167099 - only check if we allow write */
669 if (!mac_socket_enforce
) {
674 MAC_CHECK(socket_check_receive
, cred
,
675 (socket_t
)so
, so
->so_label
);
680 mac_socket_check_received(kauth_cred_t cred
, struct socket
*so
, struct sockaddr
*saddr
)
684 #if SECURITY_MAC_CHECK_ENFORCE
685 /* 21167099 - only check if we allow write */
686 if (!mac_socket_enforce
) {
691 MAC_CHECK(socket_check_received
, cred
,
692 so
, so
->so_label
, saddr
);
697 mac_socket_check_send(kauth_cred_t cred
, struct socket
*so
,
698 struct sockaddr
*sockaddr
)
702 #if SECURITY_MAC_CHECK_ENFORCE
703 /* 21167099 - only check if we allow write */
704 if (!mac_socket_enforce
) {
709 MAC_CHECK(socket_check_send
, cred
,
710 (socket_t
)so
, so
->so_label
, sockaddr
);
715 mac_socket_check_setsockopt(kauth_cred_t cred
, struct socket
*so
,
716 struct sockopt
*sopt
)
720 #if SECURITY_MAC_CHECK_ENFORCE
721 /* 21167099 - only check if we allow write */
722 if (!mac_socket_enforce
) {
727 MAC_CHECK(socket_check_setsockopt
, cred
,
728 (socket_t
)so
, so
->so_label
, sopt
);
733 mac_socket_check_getsockopt(kauth_cred_t cred
, struct socket
*so
,
734 struct sockopt
*sopt
)
738 #if SECURITY_MAC_CHECK_ENFORCE
739 /* 21167099 - only check if we allow write */
740 if (!mac_socket_enforce
) {
745 MAC_CHECK(socket_check_getsockopt
, cred
,
746 (socket_t
)so
, so
->so_label
, sopt
);