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