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-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>
86 #include <net/if_var.h>
88 #include <netinet/in.h>
89 #include <netinet/ip_var.h>
91 #include <security/mac_internal.h>
93 #if CONFIG_MACF_SOCKET
95 mac_socket_label_alloc(int flag
)
100 label
= mac_labelzone_alloc(flag
);
104 MAC_CHECK(socket_label_init
, label
, flag
);
106 MAC_PERFORM(socket_label_destroy
, label
);
107 mac_labelzone_free(label
);
114 static struct label
*
115 mac_socket_peer_label_alloc(int flag
)
120 label
= mac_labelzone_alloc(flag
);
124 MAC_CHECK(socketpeer_label_init
, label
, flag
);
126 MAC_PERFORM(socketpeer_label_destroy
, label
);
127 mac_labelzone_free(label
);
135 mac_socket_label_init(struct socket
*so
, int flag
)
138 so
->so_label
= mac_socket_label_alloc(flag
);
139 if (so
->so_label
== NULL
)
141 so
->so_peerlabel
= mac_socket_peer_label_alloc(flag
);
142 if (so
->so_peerlabel
== NULL
) {
143 mac_socket_label_free(so
->so_label
);
151 mac_socket_label_free(struct label
*label
)
154 MAC_PERFORM(socket_label_destroy
, label
);
155 mac_labelzone_free(label
);
159 mac_socket_peer_label_free(struct label
*label
)
162 MAC_PERFORM(socketpeer_label_destroy
, label
);
163 mac_labelzone_free(label
);
167 mac_socket_label_destroy(struct socket
*so
)
170 if (so
->so_label
!= NULL
) {
171 mac_socket_label_free(so
->so_label
);
174 if (so
->so_peerlabel
!= NULL
) {
175 mac_socket_peer_label_free(so
->so_peerlabel
);
176 so
->so_peerlabel
= NULL
;
181 mac_socket_label_copy(struct label
*src
, struct label
*dest
)
184 MAC_PERFORM(socket_label_copy
, src
, dest
);
188 mac_socket_label_externalize(struct label
*label
, char *elements
,
189 char *outbuf
, size_t outbuflen
)
193 error
= MAC_EXTERNALIZE(socket
, label
, elements
, outbuf
, outbuflen
);
199 mac_socketpeer_label_externalize(struct label
*label
, char *elements
,
200 char *outbuf
, size_t outbuflen
)
204 error
= MAC_EXTERNALIZE(socketpeer
, label
, elements
, outbuf
, outbuflen
);
210 mac_socket_label_internalize(struct label
*label
, char *string
)
214 error
= MAC_INTERNALIZE(socket
, label
, string
);
220 mac_socket_label_associate(struct ucred
*cred
, struct socket
*so
)
222 if (!mac_socket_enforce
)
225 MAC_PERFORM(socket_label_associate
, cred
,
226 (socket_t
)so
, so
->so_label
);
230 mac_socket_label_associate_accept(struct socket
*oldsocket
,
231 struct socket
*newsocket
)
233 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 (!mac_socket_enforce
&& !mac_net_enforce
)
250 label
= mac_mbuf_to_label(mbuf
);
252 /* Policy must deal with NULL label (unlabeled mbufs) */
253 MAC_PERFORM(socketpeer_label_associate_mbuf
, mbuf
, label
,
254 (socket_t
)so
, so
->so_peerlabel
);
258 mac_socketpeer_label_associate_mbuf(__unused
struct mbuf
*mbuf
,
259 __unused
struct socket
*so
)
266 mac_socketpeer_label_associate_socket(struct socket
*oldsocket
,
267 struct socket
*newsocket
)
269 if (!mac_socket_enforce
)
272 MAC_PERFORM(socketpeer_label_associate_socket
,
273 (socket_t
)oldsocket
, oldsocket
->so_label
,
274 (socket_t
)newsocket
, newsocket
->so_peerlabel
);
278 mac_socket_check_kqfilter(kauth_cred_t cred
, struct knote
*kn
,
283 if (!mac_socket_enforce
)
286 MAC_CHECK(socket_check_kqfilter
, cred
, kn
,
287 (socket_t
)so
, so
->so_label
);
292 mac_socket_check_label_update(kauth_cred_t cred
, struct socket
*so
,
293 struct label
*newlabel
)
297 if (!mac_socket_enforce
)
300 MAC_CHECK(socket_check_label_update
, cred
,
301 (socket_t
)so
, so
->so_label
,
307 mac_socket_check_select(kauth_cred_t cred
, struct socket
*so
, int which
)
311 if (!mac_socket_enforce
)
314 MAC_CHECK(socket_check_select
, cred
,
315 (socket_t
)so
, so
->so_label
, which
);
320 mac_socket_check_stat(kauth_cred_t cred
, struct socket
*so
)
324 if (!mac_socket_enforce
)
327 MAC_CHECK(socket_check_stat
, cred
,
328 (socket_t
)so
, so
->so_label
);
334 mac_socket_label_update(kauth_cred_t cred
, struct socket
*so
, struct label
*label
)
338 if (!mac_socket_enforce
)
341 error
= mac_socket_check_label_update(cred
, so
, label
);
345 MAC_PERFORM(socket_label_update
, cred
,
346 (socket_t
)so
, so
->so_label
, label
);
350 * If the protocol has expressed interest in socket layer changes,
351 * such as if it needs to propagate changes to a cached pcb
352 * label from the socket, notify it of the label change while
353 * holding the socket lock.
354 * XXXMAC - are there cases when we should not do this?
356 mac_inpcb_label_update(so
);
362 mac_setsockopt_label(kauth_cred_t cred
, struct socket
*so
, struct mac
*mac
)
364 struct label
*intlabel
;
369 error
= mac_check_structmac_consistent(mac
);
373 MALLOC(buffer
, char *, mac
->m_buflen
, M_MACTEMP
, M_WAITOK
);
374 error
= copyinstr(CAST_USER_ADDR_T(mac
->m_string
), buffer
,
375 mac
->m_buflen
, &len
);
377 FREE(buffer
, M_MACTEMP
);
381 intlabel
= mac_socket_label_alloc(MAC_WAITOK
);
382 error
= mac_socket_label_internalize(intlabel
, buffer
);
383 FREE(buffer
, M_MACTEMP
);
387 error
= mac_socket_label_update(cred
, so
, intlabel
);
389 mac_socket_label_free(intlabel
);
394 mac_socket_label_get(__unused kauth_cred_t cred
, struct socket
*so
,
397 char *buffer
, *elements
;
398 struct label
*intlabel
;
402 error
= mac_check_structmac_consistent(mac
);
406 MALLOC(elements
, char *, mac
->m_buflen
, M_MACTEMP
, M_WAITOK
);
407 error
= copyinstr(CAST_USER_ADDR_T(mac
->m_string
), elements
,
408 mac
->m_buflen
, &len
);
410 FREE(elements
, M_MACTEMP
);
414 MALLOC(buffer
, char *, mac
->m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
415 intlabel
= mac_socket_label_alloc(MAC_WAITOK
);
416 mac_socket_label_copy(so
->so_label
, intlabel
);
417 error
= mac_socket_label_externalize(intlabel
, elements
, buffer
,
419 mac_socket_label_free(intlabel
);
421 error
= copyout(buffer
, CAST_USER_ADDR_T(mac
->m_string
),
424 FREE(buffer
, M_MACTEMP
);
425 FREE(elements
, M_MACTEMP
);
431 mac_socketpeer_label_get(__unused kauth_cred_t cred
, struct socket
*so
,
434 char *elements
, *buffer
;
435 struct label
*intlabel
;
439 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
,
445 mac
->m_buflen
, &len
);
447 FREE(elements
, M_MACTEMP
);
451 MALLOC(buffer
, char *, mac
->m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
452 intlabel
= mac_socket_label_alloc(MAC_WAITOK
);
453 mac_socket_label_copy(so
->so_peerlabel
, intlabel
);
454 error
= mac_socketpeer_label_externalize(intlabel
, elements
, buffer
,
456 mac_socket_label_free(intlabel
);
458 error
= copyout(buffer
, CAST_USER_ADDR_T(mac
->m_string
),
461 FREE(buffer
, M_MACTEMP
);
462 FREE(elements
, M_MACTEMP
);
466 #endif /* MAC_SOCKET */
469 mac_socket_check_accept(kauth_cred_t cred
, struct socket
*so
)
473 if (!mac_socket_enforce
)
476 MAC_CHECK(socket_check_accept
, cred
,
477 (socket_t
)so
, so
->so_label
);
482 mac_socket_check_accepted(kauth_cred_t cred
, struct socket
*so
)
484 struct sockaddr
*sockaddr
;
487 if (!mac_socket_enforce
)
490 if (sock_getaddr((socket_t
)so
, &sockaddr
, 1) != 0) {
491 error
= ECONNABORTED
;
493 MAC_CHECK(socket_check_accepted
, cred
,
494 (socket_t
)so
, so
->so_label
, sockaddr
);
495 sock_freeaddr(sockaddr
);
501 mac_socket_check_bind(kauth_cred_t ucred
, struct socket
*so
,
502 struct sockaddr
*sockaddr
)
506 if (!mac_socket_enforce
)
509 MAC_CHECK(socket_check_bind
, ucred
,
510 (socket_t
)so
, so
->so_label
, sockaddr
);
515 mac_socket_check_connect(kauth_cred_t cred
, struct socket
*so
,
516 struct sockaddr
*sockaddr
)
520 if (!mac_socket_enforce
)
523 MAC_CHECK(socket_check_connect
, cred
,
524 (socket_t
)so
, so
->so_label
,
530 mac_socket_check_create(kauth_cred_t cred
, int domain
, int type
, int protocol
)
534 if (!mac_socket_enforce
)
537 MAC_CHECK(socket_check_create
, cred
, domain
, type
, protocol
);
541 #if CONFIG_MACF_SOCKET && CONFIG_MACF_NET
543 mac_socket_check_deliver(struct socket
*so
, struct mbuf
*mbuf
)
548 if (!mac_socket_enforce
)
551 label
= mac_mbuf_to_label(mbuf
);
553 /* Policy must deal with NULL label (unlabeled mbufs) */
554 MAC_CHECK(socket_check_deliver
,
555 (socket_t
)so
, so
->so_label
, mbuf
, label
);
560 mac_socket_check_deliver(__unused
struct socket
*so
, __unused
struct mbuf
*mbuf
)
567 mac_socket_check_listen(kauth_cred_t cred
, struct socket
*so
)
571 if (!mac_socket_enforce
)
574 MAC_CHECK(socket_check_listen
, cred
,
575 (socket_t
)so
, so
->so_label
);
580 mac_socket_check_receive(kauth_cred_t cred
, struct socket
*so
)
584 if (!mac_socket_enforce
)
587 MAC_CHECK(socket_check_receive
, cred
,
588 (socket_t
)so
, so
->so_label
);
593 mac_socket_check_received(kauth_cred_t cred
, struct socket
*so
, struct sockaddr
*saddr
)
597 if (!mac_socket_enforce
)
600 MAC_CHECK(socket_check_received
, cred
,
601 (socket_t
)so
, so
->so_label
, saddr
);
606 mac_socket_check_send(kauth_cred_t cred
, struct socket
*so
,
607 struct sockaddr
*sockaddr
)
611 if (!mac_socket_enforce
)
614 MAC_CHECK(socket_check_send
, cred
,
615 (socket_t
)so
, so
->so_label
, sockaddr
);
620 mac_socket_check_setsockopt(kauth_cred_t cred
, struct socket
*so
,
621 struct sockopt
*sopt
)
625 if (!mac_socket_enforce
)
628 MAC_CHECK(socket_check_setsockopt
, cred
,
629 (socket_t
)so
, so
->so_label
, sopt
);
633 int mac_socket_check_getsockopt(kauth_cred_t cred
, struct socket
*so
,
634 struct sockopt
*sopt
)
638 if (!mac_socket_enforce
)
641 MAC_CHECK(socket_check_getsockopt
, cred
,
642 (socket_t
)so
, so
->so_label
, sopt
);