2 * Copyright (c) 1999-2002, 2009 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
5 * Copyright (c) 2005-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 developed for the FreeBSD Project in part by McAfee
13 * Research, the Technology Research Division of Network Associates, Inc.
14 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
15 * DARPA CHATS research program.
17 * This software was enhanced by SPARTA ISSO under SPAWAR contract
18 * N66001-04-C-6019 ("SEFOS").
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$");
50 #include <sys/param.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
58 #include <sys/systm.h>
59 #include <sys/mount.h>
61 #include <sys/namei.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
67 #include <net/bpfdesc.h>
69 #include <net/if_var.h>
71 #include <netinet/in.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/ip_var.h>
75 #include <security/mac/mac_framework.h>
76 #include <security/mac/mac_internal.h>
77 #include <security/mac/mac_policy.h>
80 * Currently, sockets hold two labels: the label of the socket itself, and a
81 * peer label, which may be used by policies to hold a copy of the label of
82 * any remote endpoint.
84 * Possibly, this peer label should be maintained at the protocol layer
85 * (inpcb, unpcb, etc), as this would allow protocol-aware code to maintain
86 * the label consistently. For example, it might be copied live from a
87 * remote socket for UNIX domain sockets rather than keeping a local copy on
88 * this endpoint, but be cached and updated based on packets received for
91 * Unlike with many other object types, the lock protecting MAC labels on
92 * sockets (the socket lock) is not frequently held at the points in code
93 * where socket-related checks are called. The MAC Framework acquires the
94 * lock over some entry points in order to enforce atomicity (such as label
95 * copies) but in other cases the policy modules will have to acquire the
96 * lock themselves if they use labels. This approach (a) avoids lock
97 * acquisitions when policies don't require labels and (b) solves a number of
98 * potential lock order issues when multiple sockets are used in the same
103 mac_socket_label_alloc(int flag)
108 label = mac_labelzone_alloc(flag);
113 MAC_POLICY_CHECK(socket_init_label, label, flag);
115 MAC_POLICY_CHECK_NOSLEEP(socket_init_label, label, flag);
117 MAC_POLICY_PERFORM_NOSLEEP(socket_destroy_label, label);
118 mac_labelzone_free(label);
124 static struct label *
125 mac_socketpeer_label_alloc(int flag)
130 label = mac_labelzone_alloc(flag);
135 MAC_POLICY_CHECK(socketpeer_init_label, label, flag);
137 MAC_POLICY_CHECK_NOSLEEP(socketpeer_init_label, label, flag);
139 MAC_POLICY_PERFORM_NOSLEEP(socketpeer_destroy_label, label);
140 mac_labelzone_free(label);
147 mac_socket_init(struct socket *so, int flag)
150 if (mac_labeled & MPC_OBJECT_SOCKET) {
151 so->so_label = mac_socket_label_alloc(flag);
152 if (so->so_label == NULL)
154 so->so_peerlabel = mac_socketpeer_label_alloc(flag);
155 if (so->so_peerlabel == NULL) {
156 mac_socket_label_free(so->so_label);
162 so->so_peerlabel = NULL;
168 mac_socket_label_free(struct label *label)
171 MAC_POLICY_PERFORM_NOSLEEP(socket_destroy_label, label);
172 mac_labelzone_free(label);
176 mac_socketpeer_label_free(struct label *label)
179 MAC_POLICY_PERFORM_NOSLEEP(socketpeer_destroy_label, label);
180 mac_labelzone_free(label);
184 mac_socket_destroy(struct socket *so)
187 if (so->so_label != NULL) {
188 mac_socket_label_free(so->so_label);
190 mac_socketpeer_label_free(so->so_peerlabel);
191 so->so_peerlabel = NULL;
196 mac_socket_copy_label(struct label *src, struct label *dest)
199 MAC_POLICY_PERFORM_NOSLEEP(socket_copy_label, src, dest);
203 mac_socket_externalize_label(struct label *label, char *elements,
204 char *outbuf, size_t outbuflen)
208 MAC_POLICY_EXTERNALIZE(socket, label, elements, outbuf, outbuflen);
214 mac_socketpeer_externalize_label(struct label *label, char *elements,
215 char *outbuf, size_t outbuflen)
219 MAC_POLICY_EXTERNALIZE(socketpeer, label, elements, outbuf,
226 mac_socket_internalize_label(struct label *label, char *string)
230 MAC_POLICY_INTERNALIZE(socket, label, string);
236 mac_socket_create(struct ucred *cred, struct socket *so)
239 MAC_POLICY_PERFORM_NOSLEEP(socket_create, cred, so, so->so_label);
243 mac_socket_newconn(struct socket *oldso, struct socket *newso)
246 MAC_POLICY_PERFORM_NOSLEEP(socket_newconn, oldso, oldso->so_label,
247 newso, newso->so_label);
251 mac_socket_relabel(struct ucred *cred, struct socket *so,
252 struct label *newlabel)
255 SOCK_LOCK_ASSERT(so);
257 MAC_POLICY_PERFORM_NOSLEEP(socket_relabel, cred, so, so->so_label,
262 mac_socketpeer_set_from_mbuf(struct mbuf *m, struct socket *so)
266 if (mac_policy_count == 0)
269 label = mac_mbuf_to_label(m);
271 MAC_POLICY_PERFORM_NOSLEEP(socketpeer_set_from_mbuf, m, label, so,
276 mac_socketpeer_set_from_socket(struct socket *oldso, struct socket *newso)
279 if (mac_policy_count == 0)
282 MAC_POLICY_PERFORM_NOSLEEP(socketpeer_set_from_socket, oldso,
283 oldso->so_label, newso, newso->so_peerlabel);
287 mac_socket_create_mbuf(struct socket *so, struct mbuf *m)
291 if (mac_policy_count == 0)
294 label = mac_mbuf_to_label(m);
296 MAC_POLICY_PERFORM_NOSLEEP(socket_create_mbuf, so, so->so_label, m,
300 MAC_CHECK_PROBE_DEFINE2(socket_check_accept, "struct ucred *",
304 mac_socket_check_accept(struct ucred *cred, struct socket *so)
308 MAC_POLICY_CHECK_NOSLEEP(socket_check_accept, cred, so,
310 MAC_CHECK_PROBE2(socket_check_accept, error, cred, so);
315 MAC_CHECK_PROBE_DEFINE3(socket_check_bind, "struct ucred *",
316 "struct socket *", "struct sockaddr *");
319 mac_socket_check_bind(struct ucred *cred, struct socket *so,
324 MAC_POLICY_CHECK_NOSLEEP(socket_check_bind, cred, so, so->so_label,
326 MAC_CHECK_PROBE3(socket_check_bind, error, cred, so, sa);
331 MAC_CHECK_PROBE_DEFINE3(socket_check_connect, "struct ucred *",
332 "struct socket *", "struct sockaddr *");
335 mac_socket_check_connect(struct ucred *cred, struct socket *so,
340 MAC_POLICY_CHECK_NOSLEEP(socket_check_connect, cred, so,
342 MAC_CHECK_PROBE3(socket_check_connect, error, cred, so, sa);
347 MAC_CHECK_PROBE_DEFINE4(socket_check_create, "struct ucred *", "int", "int",
351 mac_socket_check_create(struct ucred *cred, int domain, int type, int proto)
355 MAC_POLICY_CHECK_NOSLEEP(socket_check_create, cred, domain, type,
357 MAC_CHECK_PROBE4(socket_check_create, error, cred, domain, type,
363 MAC_CHECK_PROBE_DEFINE2(socket_check_deliver, "struct socket *",
367 mac_socket_check_deliver(struct socket *so, struct mbuf *m)
372 if (mac_policy_count == 0)
375 label = mac_mbuf_to_label(m);
377 MAC_POLICY_CHECK_NOSLEEP(socket_check_deliver, so, so->so_label, m,
379 MAC_CHECK_PROBE2(socket_check_deliver, error, so, m);
384 MAC_CHECK_PROBE_DEFINE2(socket_check_listen, "struct ucred *",
388 mac_socket_check_listen(struct ucred *cred, struct socket *so)
392 MAC_POLICY_CHECK_NOSLEEP(socket_check_listen, cred, so,
394 MAC_CHECK_PROBE2(socket_check_listen, error, cred, so);
399 MAC_CHECK_PROBE_DEFINE2(socket_check_poll, "struct ucred *",
403 mac_socket_check_poll(struct ucred *cred, struct socket *so)
407 MAC_POLICY_CHECK_NOSLEEP(socket_check_poll, cred, so, so->so_label);
408 MAC_CHECK_PROBE2(socket_check_poll, error, cred, so);
413 MAC_CHECK_PROBE_DEFINE2(socket_check_receive, "struct ucred *",
417 mac_socket_check_receive(struct ucred *cred, struct socket *so)
421 MAC_POLICY_CHECK_NOSLEEP(socket_check_receive, cred, so,
423 MAC_CHECK_PROBE2(socket_check_receive, error, cred, so);
428 MAC_CHECK_PROBE_DEFINE3(socket_check_relabel, "struct ucred *",
429 "struct socket *", "struct label *");
432 mac_socket_check_relabel(struct ucred *cred, struct socket *so,
433 struct label *newlabel)
437 SOCK_LOCK_ASSERT(so);
439 MAC_POLICY_CHECK_NOSLEEP(socket_check_relabel, cred, so,
440 so->so_label, newlabel);
441 MAC_CHECK_PROBE3(socket_check_relabel, error, cred, so, newlabel);
446 MAC_CHECK_PROBE_DEFINE2(socket_check_send, "struct ucred *",
450 mac_socket_check_send(struct ucred *cred, struct socket *so)
454 MAC_POLICY_CHECK_NOSLEEP(socket_check_send, cred, so, so->so_label);
455 MAC_CHECK_PROBE2(socket_check_send, error, cred, so);
460 MAC_CHECK_PROBE_DEFINE2(socket_check_stat, "struct ucred *",
464 mac_socket_check_stat(struct ucred *cred, struct socket *so)
468 MAC_POLICY_CHECK_NOSLEEP(socket_check_stat, cred, so, so->so_label);
469 MAC_CHECK_PROBE2(socket_check_stat, error, cred, so);
474 MAC_CHECK_PROBE_DEFINE2(socket_check_visible, "struct ucred *",
478 mac_socket_check_visible(struct ucred *cred, struct socket *so)
482 MAC_POLICY_CHECK_NOSLEEP(socket_check_visible, cred, so,
484 MAC_CHECK_PROBE2(socket_check_visible, error, cred, so);
490 mac_socket_label_set(struct ucred *cred, struct socket *so,
496 * We acquire the socket lock when we perform the test and set, but
497 * have to release it as the pcb code needs to acquire the pcb lock,
498 * which will precede the socket lock in the lock order. However,
499 * this is fine, as any race will simply result in the inpcb being
500 * refreshed twice, but still consistently, as the inpcb code will
501 * acquire the socket lock before refreshing, holding both locks.
504 error = mac_socket_check_relabel(cred, so, label);
510 mac_socket_relabel(cred, so, label);
514 * If the protocol has expressed interest in socket layer changes,
515 * such as if it needs to propagate changes to a cached pcb label
516 * from the socket, notify it of the label change while holding the
519 if (so->so_proto->pr_usrreqs->pru_sosetlabel != NULL)
520 (so->so_proto->pr_usrreqs->pru_sosetlabel)(so);
526 mac_setsockopt_label(struct ucred *cred, struct socket *so, struct mac *mac)
528 struct label *intlabel;
532 if (!(mac_labeled & MPC_OBJECT_SOCKET))
535 error = mac_check_structmac_consistent(mac);
539 buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
540 error = copyinstr(mac->m_string, buffer, mac->m_buflen, NULL);
542 free(buffer, M_MACTEMP);
546 intlabel = mac_socket_label_alloc(M_WAITOK);
547 error = mac_socket_internalize_label(intlabel, buffer);
548 free(buffer, M_MACTEMP);
552 error = mac_socket_label_set(cred, so, intlabel);
554 mac_socket_label_free(intlabel);
559 mac_getsockopt_label(struct ucred *cred, struct socket *so, struct mac *mac)
561 char *buffer, *elements;
562 struct label *intlabel;
565 if (!(mac_labeled & MPC_OBJECT_SOCKET))
568 error = mac_check_structmac_consistent(mac);
572 elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
573 error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL);
575 free(elements, M_MACTEMP);
579 buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
580 intlabel = mac_socket_label_alloc(M_WAITOK);
582 mac_socket_copy_label(so->so_label, intlabel);
584 error = mac_socket_externalize_label(intlabel, elements, buffer,
586 mac_socket_label_free(intlabel);
588 error = copyout(buffer, mac->m_string, strlen(buffer)+1);
590 free(buffer, M_MACTEMP);
591 free(elements, M_MACTEMP);
597 mac_getsockopt_peerlabel(struct ucred *cred, struct socket *so,
600 char *elements, *buffer;
601 struct label *intlabel;
604 if (!(mac_labeled & MPC_OBJECT_SOCKET))
607 error = mac_check_structmac_consistent(mac);
611 elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
612 error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL);
614 free(elements, M_MACTEMP);
618 buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
619 intlabel = mac_socket_label_alloc(M_WAITOK);
621 mac_socket_copy_label(so->so_peerlabel, intlabel);
623 error = mac_socketpeer_externalize_label(intlabel, elements, buffer,
625 mac_socket_label_free(intlabel);
627 error = copyout(buffer, mac->m_string, strlen(buffer)+1);
629 free(buffer, M_MACTEMP);
630 free(elements, M_MACTEMP);