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