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$");
48 #include "opt_kdtrace.h"
51 #include <sys/param.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
59 #include <sys/systm.h>
60 #include <sys/mount.h>
62 #include <sys/namei.h>
63 #include <sys/protosw.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66 #include <sys/sysctl.h>
68 #include <net/bpfdesc.h>
70 #include <net/if_var.h>
72 #include <netinet/in.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/ip_var.h>
76 #include <security/mac/mac_framework.h>
77 #include <security/mac/mac_internal.h>
78 #include <security/mac/mac_policy.h>
81 * Currently, sockets hold two labels: the label of the socket itself, and a
82 * peer label, which may be used by policies to hold a copy of the label of
83 * any remote endpoint.
85 * Possibly, this peer label should be maintained at the protocol layer
86 * (inpcb, unpcb, etc), as this would allow protocol-aware code to maintain
87 * the label consistently. For example, it might be copied live from a
88 * remote socket for UNIX domain sockets rather than keeping a local copy on
89 * this endpoint, but be cached and updated based on packets received for
92 * Unlike with many other object types, the lock protecting MAC labels on
93 * sockets (the socket lock) is not frequently held at the points in code
94 * where socket-related checks are called. The MAC Framework acquires the
95 * lock over some entry points in order to enforce atomicity (such as label
96 * copies) but in other cases the policy modules will have to acquire the
97 * lock themselves if they use labels. This approach (a) avoids lock
98 * acquisitions when policies don't require labels and (b) solves a number of
99 * potential lock order issues when multiple sockets are used in the same
104 mac_socket_label_alloc(int flag)
109 label = mac_labelzone_alloc(flag);
114 MAC_POLICY_CHECK(socket_init_label, label, flag);
116 MAC_POLICY_CHECK_NOSLEEP(socket_init_label, label, flag);
118 MAC_POLICY_PERFORM_NOSLEEP(socket_destroy_label, label);
119 mac_labelzone_free(label);
125 static struct label *
126 mac_socketpeer_label_alloc(int flag)
131 label = mac_labelzone_alloc(flag);
136 MAC_POLICY_CHECK(socketpeer_init_label, label, flag);
138 MAC_POLICY_CHECK_NOSLEEP(socketpeer_init_label, label, flag);
140 MAC_POLICY_PERFORM_NOSLEEP(socketpeer_destroy_label, label);
141 mac_labelzone_free(label);
148 mac_socket_init(struct socket *so, int flag)
151 if (mac_labeled & MPC_OBJECT_SOCKET) {
152 so->so_label = mac_socket_label_alloc(flag);
153 if (so->so_label == NULL)
155 so->so_peerlabel = mac_socketpeer_label_alloc(flag);
156 if (so->so_peerlabel == NULL) {
157 mac_socket_label_free(so->so_label);
163 so->so_peerlabel = NULL;
169 mac_socket_label_free(struct label *label)
172 MAC_POLICY_PERFORM_NOSLEEP(socket_destroy_label, label);
173 mac_labelzone_free(label);
177 mac_socketpeer_label_free(struct label *label)
180 MAC_POLICY_PERFORM_NOSLEEP(socketpeer_destroy_label, label);
181 mac_labelzone_free(label);
185 mac_socket_destroy(struct socket *so)
188 if (so->so_label != NULL) {
189 mac_socket_label_free(so->so_label);
191 mac_socketpeer_label_free(so->so_peerlabel);
192 so->so_peerlabel = NULL;
197 mac_socket_copy_label(struct label *src, struct label *dest)
200 MAC_POLICY_PERFORM_NOSLEEP(socket_copy_label, src, dest);
204 mac_socket_externalize_label(struct label *label, char *elements,
205 char *outbuf, size_t outbuflen)
209 MAC_POLICY_EXTERNALIZE(socket, label, elements, outbuf, outbuflen);
215 mac_socketpeer_externalize_label(struct label *label, char *elements,
216 char *outbuf, size_t outbuflen)
220 MAC_POLICY_EXTERNALIZE(socketpeer, label, elements, outbuf,
227 mac_socket_internalize_label(struct label *label, char *string)
231 MAC_POLICY_INTERNALIZE(socket, label, string);
237 mac_socket_create(struct ucred *cred, struct socket *so)
240 MAC_POLICY_PERFORM_NOSLEEP(socket_create, cred, so, so->so_label);
244 mac_socket_newconn(struct socket *oldso, struct socket *newso)
247 MAC_POLICY_PERFORM_NOSLEEP(socket_newconn, oldso, oldso->so_label,
248 newso, newso->so_label);
252 mac_socket_relabel(struct ucred *cred, struct socket *so,
253 struct label *newlabel)
256 SOCK_LOCK_ASSERT(so);
258 MAC_POLICY_PERFORM_NOSLEEP(socket_relabel, cred, so, so->so_label,
263 mac_socketpeer_set_from_mbuf(struct mbuf *m, struct socket *so)
267 if (mac_policy_count == 0)
270 label = mac_mbuf_to_label(m);
272 MAC_POLICY_PERFORM_NOSLEEP(socketpeer_set_from_mbuf, m, label, so,
277 mac_socketpeer_set_from_socket(struct socket *oldso, struct socket *newso)
280 if (mac_policy_count == 0)
283 MAC_POLICY_PERFORM_NOSLEEP(socketpeer_set_from_socket, oldso,
284 oldso->so_label, newso, newso->so_peerlabel);
288 mac_socket_create_mbuf(struct socket *so, struct mbuf *m)
292 if (mac_policy_count == 0)
295 label = mac_mbuf_to_label(m);
297 MAC_POLICY_PERFORM_NOSLEEP(socket_create_mbuf, so, so->so_label, m,
301 MAC_CHECK_PROBE_DEFINE2(socket_check_accept, "struct ucred *",
305 mac_socket_check_accept(struct ucred *cred, struct socket *so)
309 MAC_POLICY_CHECK_NOSLEEP(socket_check_accept, cred, so,
311 MAC_CHECK_PROBE2(socket_check_accept, error, cred, so);
316 MAC_CHECK_PROBE_DEFINE3(socket_check_bind, "struct ucred *",
317 "struct socket *", "struct sockaddr *");
320 mac_socket_check_bind(struct ucred *cred, struct socket *so,
325 MAC_POLICY_CHECK_NOSLEEP(socket_check_bind, cred, so, so->so_label,
327 MAC_CHECK_PROBE3(socket_check_bind, error, cred, so, sa);
332 MAC_CHECK_PROBE_DEFINE3(socket_check_connect, "struct ucred *",
333 "struct socket *", "struct sockaddr *");
336 mac_socket_check_connect(struct ucred *cred, struct socket *so,
341 MAC_POLICY_CHECK_NOSLEEP(socket_check_connect, cred, so,
343 MAC_CHECK_PROBE3(socket_check_connect, error, cred, so, sa);
348 MAC_CHECK_PROBE_DEFINE4(socket_check_create, "struct ucred *", "int", "int",
352 mac_socket_check_create(struct ucred *cred, int domain, int type, int proto)
356 MAC_POLICY_CHECK_NOSLEEP(socket_check_create, cred, domain, type,
358 MAC_CHECK_PROBE4(socket_check_create, error, cred, domain, type,
364 MAC_CHECK_PROBE_DEFINE2(socket_check_deliver, "struct socket *",
368 mac_socket_check_deliver(struct socket *so, struct mbuf *m)
373 if (mac_policy_count == 0)
376 label = mac_mbuf_to_label(m);
378 MAC_POLICY_CHECK_NOSLEEP(socket_check_deliver, so, so->so_label, m,
380 MAC_CHECK_PROBE2(socket_check_deliver, error, so, m);
385 MAC_CHECK_PROBE_DEFINE2(socket_check_listen, "struct ucred *",
389 mac_socket_check_listen(struct ucred *cred, struct socket *so)
393 MAC_POLICY_CHECK_NOSLEEP(socket_check_listen, cred, so,
395 MAC_CHECK_PROBE2(socket_check_listen, error, cred, so);
400 MAC_CHECK_PROBE_DEFINE2(socket_check_poll, "struct ucred *",
404 mac_socket_check_poll(struct ucred *cred, struct socket *so)
408 MAC_POLICY_CHECK_NOSLEEP(socket_check_poll, cred, so, so->so_label);
409 MAC_CHECK_PROBE2(socket_check_poll, error, cred, so);
414 MAC_CHECK_PROBE_DEFINE2(socket_check_receive, "struct ucred *",
418 mac_socket_check_receive(struct ucred *cred, struct socket *so)
422 MAC_POLICY_CHECK_NOSLEEP(socket_check_receive, cred, so,
424 MAC_CHECK_PROBE2(socket_check_receive, error, cred, so);
429 MAC_CHECK_PROBE_DEFINE3(socket_check_relabel, "struct ucred *",
430 "struct socket *", "struct label *");
433 mac_socket_check_relabel(struct ucred *cred, struct socket *so,
434 struct label *newlabel)
438 SOCK_LOCK_ASSERT(so);
440 MAC_POLICY_CHECK_NOSLEEP(socket_check_relabel, cred, so,
441 so->so_label, newlabel);
442 MAC_CHECK_PROBE3(socket_check_relabel, error, cred, so, newlabel);
447 MAC_CHECK_PROBE_DEFINE2(socket_check_send, "struct ucred *",
451 mac_socket_check_send(struct ucred *cred, struct socket *so)
455 MAC_POLICY_CHECK_NOSLEEP(socket_check_send, cred, so, so->so_label);
456 MAC_CHECK_PROBE2(socket_check_send, error, cred, so);
461 MAC_CHECK_PROBE_DEFINE2(socket_check_stat, "struct ucred *",
465 mac_socket_check_stat(struct ucred *cred, struct socket *so)
469 MAC_POLICY_CHECK_NOSLEEP(socket_check_stat, cred, so, so->so_label);
470 MAC_CHECK_PROBE2(socket_check_stat, error, cred, so);
475 MAC_CHECK_PROBE_DEFINE2(socket_check_visible, "struct ucred *",
479 mac_socket_check_visible(struct ucred *cred, struct socket *so)
483 MAC_POLICY_CHECK_NOSLEEP(socket_check_visible, cred, so,
485 MAC_CHECK_PROBE2(socket_check_visible, error, cred, so);
491 mac_socket_label_set(struct ucred *cred, struct socket *so,
497 * We acquire the socket lock when we perform the test and set, but
498 * have to release it as the pcb code needs to acquire the pcb lock,
499 * which will precede the socket lock in the lock order. However,
500 * this is fine, as any race will simply result in the inpcb being
501 * refreshed twice, but still consistently, as the inpcb code will
502 * acquire the socket lock before refreshing, holding both locks.
505 error = mac_socket_check_relabel(cred, so, label);
511 mac_socket_relabel(cred, so, label);
515 * If the protocol has expressed interest in socket layer changes,
516 * such as if it needs to propagate changes to a cached pcb label
517 * from the socket, notify it of the label change while holding the
520 if (so->so_proto->pr_usrreqs->pru_sosetlabel != NULL)
521 (so->so_proto->pr_usrreqs->pru_sosetlabel)(so);
527 mac_setsockopt_label(struct ucred *cred, struct socket *so, struct mac *mac)
529 struct label *intlabel;
533 if (!(mac_labeled & MPC_OBJECT_SOCKET))
536 error = mac_check_structmac_consistent(mac);
540 buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
541 error = copyinstr(mac->m_string, buffer, mac->m_buflen, NULL);
543 free(buffer, M_MACTEMP);
547 intlabel = mac_socket_label_alloc(M_WAITOK);
548 error = mac_socket_internalize_label(intlabel, buffer);
549 free(buffer, M_MACTEMP);
553 error = mac_socket_label_set(cred, so, intlabel);
555 mac_socket_label_free(intlabel);
560 mac_getsockopt_label(struct ucred *cred, struct socket *so, struct mac *mac)
562 char *buffer, *elements;
563 struct label *intlabel;
566 if (!(mac_labeled & MPC_OBJECT_SOCKET))
569 error = mac_check_structmac_consistent(mac);
573 elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
574 error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL);
576 free(elements, M_MACTEMP);
580 buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
581 intlabel = mac_socket_label_alloc(M_WAITOK);
583 mac_socket_copy_label(so->so_label, intlabel);
585 error = mac_socket_externalize_label(intlabel, elements, buffer,
587 mac_socket_label_free(intlabel);
589 error = copyout(buffer, mac->m_string, strlen(buffer)+1);
591 free(buffer, M_MACTEMP);
592 free(elements, M_MACTEMP);
598 mac_getsockopt_peerlabel(struct ucred *cred, struct socket *so,
601 char *elements, *buffer;
602 struct label *intlabel;
605 if (!(mac_labeled & MPC_OBJECT_SOCKET))
608 error = mac_check_structmac_consistent(mac);
612 elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
613 error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL);
615 free(elements, M_MACTEMP);
619 buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
620 intlabel = mac_socket_label_alloc(M_WAITOK);
622 mac_socket_copy_label(so->so_peerlabel, intlabel);
624 error = mac_socketpeer_externalize_label(intlabel, elements, buffer,
626 mac_socket_label_free(intlabel);
628 error = copyout(buffer, mac->m_string, strlen(buffer)+1);
630 free(buffer, M_MACTEMP);
631 free(elements, M_MACTEMP);