xref: /xnu-8020.140.41/security/mac_socket.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions  * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
3*27b03b36SApple OSS Distributions  *
4*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions  *
6*27b03b36SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions  *
15*27b03b36SApple OSS Distributions  * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions  *
18*27b03b36SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions  * limitations under the License.
25*27b03b36SApple OSS Distributions  *
26*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions  */
28*27b03b36SApple OSS Distributions /*-
29*27b03b36SApple OSS Distributions  * Copyright (c) 1999-2002 Robert N. M. Watson
30*27b03b36SApple OSS Distributions  * Copyright (c) 2001 Ilmar S. Habibulin
31*27b03b36SApple OSS Distributions  * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
32*27b03b36SApple OSS Distributions  * Copyright (c) 2005 SPARTA, Inc.
33*27b03b36SApple OSS Distributions  * All rights reserved.
34*27b03b36SApple OSS Distributions  *
35*27b03b36SApple OSS Distributions  * This software was developed by Robert Watson and Ilmar Habibulin for the
36*27b03b36SApple OSS Distributions  * TrustedBSD Project.
37*27b03b36SApple OSS Distributions  *
38*27b03b36SApple OSS Distributions  * This software was developed for the FreeBSD Project in part by McAfee
39*27b03b36SApple OSS Distributions  * Research, the Technology Research Division of Network Associates, Inc.
40*27b03b36SApple OSS Distributions  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
41*27b03b36SApple OSS Distributions  * DARPA CHATS research program.
42*27b03b36SApple OSS Distributions  *
43*27b03b36SApple OSS Distributions  * This software was enhanced by SPARTA ISSO under SPAWAR contract
44*27b03b36SApple OSS Distributions  * N66001-04-C-6019 ("SEFOS").
45*27b03b36SApple OSS Distributions  *
46*27b03b36SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
47*27b03b36SApple OSS Distributions  * modification, are permitted provided that the following conditions
48*27b03b36SApple OSS Distributions  * are met:
49*27b03b36SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
50*27b03b36SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
51*27b03b36SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
52*27b03b36SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
53*27b03b36SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
54*27b03b36SApple OSS Distributions  *
55*27b03b36SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
56*27b03b36SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57*27b03b36SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58*27b03b36SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
59*27b03b36SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60*27b03b36SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61*27b03b36SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62*27b03b36SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63*27b03b36SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64*27b03b36SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65*27b03b36SApple OSS Distributions  * SUCH DAMAGE.
66*27b03b36SApple OSS Distributions  */
67*27b03b36SApple OSS Distributions 
68*27b03b36SApple OSS Distributions #include <sys/cdefs.h>
69*27b03b36SApple OSS Distributions 
70*27b03b36SApple OSS Distributions #include <sys/param.h>
71*27b03b36SApple OSS Distributions #include <sys/kernel.h>
72*27b03b36SApple OSS Distributions #include <sys/lock.h>
73*27b03b36SApple OSS Distributions #include <sys/malloc.h>
74*27b03b36SApple OSS Distributions #include <sys/sbuf.h>
75*27b03b36SApple OSS Distributions #include <sys/systm.h>
76*27b03b36SApple OSS Distributions #include <sys/mount.h>
77*27b03b36SApple OSS Distributions #include <sys/file.h>
78*27b03b36SApple OSS Distributions #include <sys/namei.h>
79*27b03b36SApple OSS Distributions #include <sys/protosw.h>
80*27b03b36SApple OSS Distributions #include <sys/socket.h>
81*27b03b36SApple OSS Distributions #include <sys/socketvar.h>
82*27b03b36SApple OSS Distributions #include <sys/sysctl.h>
83*27b03b36SApple OSS Distributions #include <sys/kpi_socket.h>
84*27b03b36SApple OSS Distributions 
85*27b03b36SApple OSS Distributions #include <security/mac_internal.h>
86*27b03b36SApple OSS Distributions 
87*27b03b36SApple OSS Distributions int
mac_socket_check_accept(kauth_cred_t cred,struct socket * so)88*27b03b36SApple OSS Distributions mac_socket_check_accept(kauth_cred_t cred, struct socket *so)
89*27b03b36SApple OSS Distributions {
90*27b03b36SApple OSS Distributions 	int error;
91*27b03b36SApple OSS Distributions 
92*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
93*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
94*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
95*27b03b36SApple OSS Distributions 		return 0;
96*27b03b36SApple OSS Distributions 	}
97*27b03b36SApple OSS Distributions #endif
98*27b03b36SApple OSS Distributions 
99*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_accept, cred,
100*27b03b36SApple OSS Distributions 	    (socket_t)so, NULL);
101*27b03b36SApple OSS Distributions 	return error;
102*27b03b36SApple OSS Distributions }
103*27b03b36SApple OSS Distributions 
104*27b03b36SApple OSS Distributions #if CONFIG_MACF_SOCKET_SUBSET
105*27b03b36SApple OSS Distributions int
mac_socket_check_accepted(kauth_cred_t cred,struct socket * so)106*27b03b36SApple OSS Distributions mac_socket_check_accepted(kauth_cred_t cred, struct socket *so)
107*27b03b36SApple OSS Distributions {
108*27b03b36SApple OSS Distributions 	struct sockaddr *sockaddr;
109*27b03b36SApple OSS Distributions 	int error;
110*27b03b36SApple OSS Distributions 
111*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
112*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
113*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
114*27b03b36SApple OSS Distributions 		return 0;
115*27b03b36SApple OSS Distributions 	}
116*27b03b36SApple OSS Distributions #endif
117*27b03b36SApple OSS Distributions 
118*27b03b36SApple OSS Distributions 	if (sock_getaddr((socket_t)so, &sockaddr, 1) != 0) {
119*27b03b36SApple OSS Distributions 		error = ECONNABORTED;
120*27b03b36SApple OSS Distributions 	} else {
121*27b03b36SApple OSS Distributions 		MAC_CHECK(socket_check_accepted, cred,
122*27b03b36SApple OSS Distributions 		    (socket_t)so, NULL, sockaddr);
123*27b03b36SApple OSS Distributions 		sock_freeaddr(sockaddr);
124*27b03b36SApple OSS Distributions 	}
125*27b03b36SApple OSS Distributions 	return error;
126*27b03b36SApple OSS Distributions }
127*27b03b36SApple OSS Distributions #endif
128*27b03b36SApple OSS Distributions 
129*27b03b36SApple OSS Distributions int
mac_socket_check_bind(kauth_cred_t ucred,struct socket * so,struct sockaddr * sockaddr)130*27b03b36SApple OSS Distributions mac_socket_check_bind(kauth_cred_t ucred, struct socket *so,
131*27b03b36SApple OSS Distributions     struct sockaddr *sockaddr)
132*27b03b36SApple OSS Distributions {
133*27b03b36SApple OSS Distributions 	int error;
134*27b03b36SApple OSS Distributions 
135*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
136*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
137*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
138*27b03b36SApple OSS Distributions 		return 0;
139*27b03b36SApple OSS Distributions 	}
140*27b03b36SApple OSS Distributions #endif
141*27b03b36SApple OSS Distributions 
142*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_bind, ucred,
143*27b03b36SApple OSS Distributions 	    (socket_t)so, NULL, sockaddr);
144*27b03b36SApple OSS Distributions 	return error;
145*27b03b36SApple OSS Distributions }
146*27b03b36SApple OSS Distributions 
147*27b03b36SApple OSS Distributions int
mac_socket_check_connect(kauth_cred_t cred,struct socket * so,struct sockaddr * sockaddr)148*27b03b36SApple OSS Distributions mac_socket_check_connect(kauth_cred_t cred, struct socket *so,
149*27b03b36SApple OSS Distributions     struct sockaddr *sockaddr)
150*27b03b36SApple OSS Distributions {
151*27b03b36SApple OSS Distributions 	int error;
152*27b03b36SApple OSS Distributions 
153*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
154*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
155*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
156*27b03b36SApple OSS Distributions 		return 0;
157*27b03b36SApple OSS Distributions 	}
158*27b03b36SApple OSS Distributions #endif
159*27b03b36SApple OSS Distributions 
160*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_connect, cred,
161*27b03b36SApple OSS Distributions 	    (socket_t)so, NULL,
162*27b03b36SApple OSS Distributions 	    sockaddr);
163*27b03b36SApple OSS Distributions 	return error;
164*27b03b36SApple OSS Distributions }
165*27b03b36SApple OSS Distributions 
166*27b03b36SApple OSS Distributions int
mac_socket_check_create(kauth_cred_t cred,int domain,int type,int protocol)167*27b03b36SApple OSS Distributions mac_socket_check_create(kauth_cred_t cred, int domain, int type, int protocol)
168*27b03b36SApple OSS Distributions {
169*27b03b36SApple OSS Distributions 	int error;
170*27b03b36SApple OSS Distributions 
171*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
172*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
173*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
174*27b03b36SApple OSS Distributions 		return 0;
175*27b03b36SApple OSS Distributions 	}
176*27b03b36SApple OSS Distributions #endif
177*27b03b36SApple OSS Distributions 
178*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_create, cred, domain, type, protocol);
179*27b03b36SApple OSS Distributions 	return error;
180*27b03b36SApple OSS Distributions }
181*27b03b36SApple OSS Distributions 
182*27b03b36SApple OSS Distributions int
mac_socket_check_ioctl(kauth_cred_t cred,struct socket * so,u_long cmd)183*27b03b36SApple OSS Distributions mac_socket_check_ioctl(kauth_cred_t cred, struct socket *so, u_long cmd)
184*27b03b36SApple OSS Distributions {
185*27b03b36SApple OSS Distributions 	int error;
186*27b03b36SApple OSS Distributions 
187*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
188*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
189*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
190*27b03b36SApple OSS Distributions 		return 0;
191*27b03b36SApple OSS Distributions 	}
192*27b03b36SApple OSS Distributions #endif
193*27b03b36SApple OSS Distributions 
194*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_ioctl, cred,
195*27b03b36SApple OSS Distributions 	    (socket_t)so, cmd, NULL);
196*27b03b36SApple OSS Distributions 	return error;
197*27b03b36SApple OSS Distributions }
198*27b03b36SApple OSS Distributions 
199*27b03b36SApple OSS Distributions int
mac_socket_check_stat(kauth_cred_t cred,struct socket * so)200*27b03b36SApple OSS Distributions mac_socket_check_stat(kauth_cred_t cred, struct socket *so)
201*27b03b36SApple OSS Distributions {
202*27b03b36SApple OSS Distributions 	int error;
203*27b03b36SApple OSS Distributions 
204*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
205*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
206*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
207*27b03b36SApple OSS Distributions 		return 0;
208*27b03b36SApple OSS Distributions 	}
209*27b03b36SApple OSS Distributions #endif
210*27b03b36SApple OSS Distributions 
211*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_stat, cred,
212*27b03b36SApple OSS Distributions 	    (socket_t)so, NULL);
213*27b03b36SApple OSS Distributions 	return error;
214*27b03b36SApple OSS Distributions }
215*27b03b36SApple OSS Distributions 
216*27b03b36SApple OSS Distributions int
mac_socket_check_listen(kauth_cred_t cred,struct socket * so)217*27b03b36SApple OSS Distributions mac_socket_check_listen(kauth_cred_t cred, struct socket *so)
218*27b03b36SApple OSS Distributions {
219*27b03b36SApple OSS Distributions 	int error;
220*27b03b36SApple OSS Distributions 
221*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
222*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
223*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
224*27b03b36SApple OSS Distributions 		return 0;
225*27b03b36SApple OSS Distributions 	}
226*27b03b36SApple OSS Distributions #endif
227*27b03b36SApple OSS Distributions 
228*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_listen, cred,
229*27b03b36SApple OSS Distributions 	    (socket_t)so, NULL);
230*27b03b36SApple OSS Distributions 	return error;
231*27b03b36SApple OSS Distributions }
232*27b03b36SApple OSS Distributions 
233*27b03b36SApple OSS Distributions int
mac_socket_check_receive(kauth_cred_t cred,struct socket * so)234*27b03b36SApple OSS Distributions mac_socket_check_receive(kauth_cred_t cred, struct socket *so)
235*27b03b36SApple OSS Distributions {
236*27b03b36SApple OSS Distributions 	int error;
237*27b03b36SApple OSS Distributions 
238*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
239*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
240*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
241*27b03b36SApple OSS Distributions 		return 0;
242*27b03b36SApple OSS Distributions 	}
243*27b03b36SApple OSS Distributions #endif
244*27b03b36SApple OSS Distributions 
245*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_receive, cred,
246*27b03b36SApple OSS Distributions 	    (socket_t)so, NULL);
247*27b03b36SApple OSS Distributions 	return error;
248*27b03b36SApple OSS Distributions }
249*27b03b36SApple OSS Distributions 
250*27b03b36SApple OSS Distributions int
mac_socket_check_received(kauth_cred_t cred,struct socket * so,struct sockaddr * saddr)251*27b03b36SApple OSS Distributions mac_socket_check_received(kauth_cred_t cred, struct socket *so, struct sockaddr *saddr)
252*27b03b36SApple OSS Distributions {
253*27b03b36SApple OSS Distributions 	int error;
254*27b03b36SApple OSS Distributions 
255*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
256*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
257*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
258*27b03b36SApple OSS Distributions 		return 0;
259*27b03b36SApple OSS Distributions 	}
260*27b03b36SApple OSS Distributions #endif
261*27b03b36SApple OSS Distributions 
262*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_received, cred,
263*27b03b36SApple OSS Distributions 	    so, NULL, saddr);
264*27b03b36SApple OSS Distributions 	return error;
265*27b03b36SApple OSS Distributions }
266*27b03b36SApple OSS Distributions 
267*27b03b36SApple OSS Distributions int
mac_socket_check_send(kauth_cred_t cred,struct socket * so,struct sockaddr * sockaddr)268*27b03b36SApple OSS Distributions mac_socket_check_send(kauth_cred_t cred, struct socket *so,
269*27b03b36SApple OSS Distributions     struct sockaddr *sockaddr)
270*27b03b36SApple OSS Distributions {
271*27b03b36SApple OSS Distributions 	int error;
272*27b03b36SApple OSS Distributions 
273*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
274*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
275*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
276*27b03b36SApple OSS Distributions 		return 0;
277*27b03b36SApple OSS Distributions 	}
278*27b03b36SApple OSS Distributions #endif
279*27b03b36SApple OSS Distributions 
280*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_send, cred,
281*27b03b36SApple OSS Distributions 	    (socket_t)so, NULL, sockaddr);
282*27b03b36SApple OSS Distributions 	return error;
283*27b03b36SApple OSS Distributions }
284*27b03b36SApple OSS Distributions 
285*27b03b36SApple OSS Distributions int
mac_socket_check_setsockopt(kauth_cred_t cred,struct socket * so,struct sockopt * sopt)286*27b03b36SApple OSS Distributions mac_socket_check_setsockopt(kauth_cred_t cred, struct socket *so,
287*27b03b36SApple OSS Distributions     struct sockopt *sopt)
288*27b03b36SApple OSS Distributions {
289*27b03b36SApple OSS Distributions 	int error;
290*27b03b36SApple OSS Distributions 
291*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
292*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
293*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
294*27b03b36SApple OSS Distributions 		return 0;
295*27b03b36SApple OSS Distributions 	}
296*27b03b36SApple OSS Distributions #endif
297*27b03b36SApple OSS Distributions 
298*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_setsockopt, cred,
299*27b03b36SApple OSS Distributions 	    (socket_t)so, NULL, sopt);
300*27b03b36SApple OSS Distributions 	return error;
301*27b03b36SApple OSS Distributions }
302*27b03b36SApple OSS Distributions 
303*27b03b36SApple OSS Distributions int
mac_socket_check_getsockopt(kauth_cred_t cred,struct socket * so,struct sockopt * sopt)304*27b03b36SApple OSS Distributions mac_socket_check_getsockopt(kauth_cred_t cred, struct socket *so,
305*27b03b36SApple OSS Distributions     struct sockopt *sopt)
306*27b03b36SApple OSS Distributions {
307*27b03b36SApple OSS Distributions 	int error;
308*27b03b36SApple OSS Distributions 
309*27b03b36SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
310*27b03b36SApple OSS Distributions 	/* 21167099 - only check if we allow write */
311*27b03b36SApple OSS Distributions 	if (!mac_socket_enforce) {
312*27b03b36SApple OSS Distributions 		return 0;
313*27b03b36SApple OSS Distributions 	}
314*27b03b36SApple OSS Distributions #endif
315*27b03b36SApple OSS Distributions 
316*27b03b36SApple OSS Distributions 	MAC_CHECK(socket_check_getsockopt, cred,
317*27b03b36SApple OSS Distributions 	    (socket_t)so, NULL, sopt);
318*27b03b36SApple OSS Distributions 	return error;
319*27b03b36SApple OSS Distributions }
320