xref: /xnu-10002.61.3/security/mac_system.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1*0f4c859eSApple OSS Distributions /*
2*0f4c859eSApple OSS Distributions  * Copyright (c) 2007 Apple Inc. All rights reserved.
3*0f4c859eSApple OSS Distributions  *
4*0f4c859eSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*0f4c859eSApple OSS Distributions  *
6*0f4c859eSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*0f4c859eSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*0f4c859eSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*0f4c859eSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*0f4c859eSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*0f4c859eSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*0f4c859eSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*0f4c859eSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*0f4c859eSApple OSS Distributions  *
15*0f4c859eSApple OSS Distributions  * Please obtain a copy of the License at
16*0f4c859eSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*0f4c859eSApple OSS Distributions  *
18*0f4c859eSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*0f4c859eSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*0f4c859eSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*0f4c859eSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*0f4c859eSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*0f4c859eSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*0f4c859eSApple OSS Distributions  * limitations under the License.
25*0f4c859eSApple OSS Distributions  *
26*0f4c859eSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*0f4c859eSApple OSS Distributions  */
28*0f4c859eSApple OSS Distributions 
29*0f4c859eSApple OSS Distributions /*-
30*0f4c859eSApple OSS Distributions  * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
31*0f4c859eSApple OSS Distributions  * Copyright (c) 2001 Ilmar S. Habibulin
32*0f4c859eSApple OSS Distributions  * Copyright (c) 2001, 2002, 2003, 2004 Networks Associates Technology, Inc.
33*0f4c859eSApple OSS Distributions  *
34*0f4c859eSApple OSS Distributions  * This software was developed by Robert Watson and Ilmar Habibulin for the
35*0f4c859eSApple OSS Distributions  * TrustedBSD Project.
36*0f4c859eSApple OSS Distributions  *
37*0f4c859eSApple OSS Distributions  * This software was developed for the FreeBSD Project in part by Network
38*0f4c859eSApple OSS Distributions  * Associates Laboratories, the Security Research Division of Network
39*0f4c859eSApple OSS Distributions  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
40*0f4c859eSApple OSS Distributions  * as part of the DARPA CHATS research program.
41*0f4c859eSApple OSS Distributions  *
42*0f4c859eSApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
43*0f4c859eSApple OSS Distributions  * modification, are permitted provided that the following conditions
44*0f4c859eSApple OSS Distributions  * are met:
45*0f4c859eSApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
46*0f4c859eSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
47*0f4c859eSApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
48*0f4c859eSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
49*0f4c859eSApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
50*0f4c859eSApple OSS Distributions  *
51*0f4c859eSApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
52*0f4c859eSApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53*0f4c859eSApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54*0f4c859eSApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55*0f4c859eSApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56*0f4c859eSApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57*0f4c859eSApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*0f4c859eSApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59*0f4c859eSApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60*0f4c859eSApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61*0f4c859eSApple OSS Distributions  * SUCH DAMAGE.
62*0f4c859eSApple OSS Distributions  *
63*0f4c859eSApple OSS Distributions  */
64*0f4c859eSApple OSS Distributions 
65*0f4c859eSApple OSS Distributions #include <sys/param.h>
66*0f4c859eSApple OSS Distributions #include <sys/vnode.h>
67*0f4c859eSApple OSS Distributions #include <sys/vnode_internal.h>
68*0f4c859eSApple OSS Distributions 
69*0f4c859eSApple OSS Distributions #include <security/mac_internal.h>
70*0f4c859eSApple OSS Distributions 
71*0f4c859eSApple OSS Distributions 
72*0f4c859eSApple OSS Distributions int
mac_system_check_acct(kauth_cred_t cred,struct vnode * vp)73*0f4c859eSApple OSS Distributions mac_system_check_acct(kauth_cred_t cred, struct vnode *vp)
74*0f4c859eSApple OSS Distributions {
75*0f4c859eSApple OSS Distributions 	int error;
76*0f4c859eSApple OSS Distributions 
77*0f4c859eSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
78*0f4c859eSApple OSS Distributions 	/* 21167099 - only check if we allow write */
79*0f4c859eSApple OSS Distributions 	if (!mac_system_enforce) {
80*0f4c859eSApple OSS Distributions 		return 0;
81*0f4c859eSApple OSS Distributions 	}
82*0f4c859eSApple OSS Distributions #endif
83*0f4c859eSApple OSS Distributions 
84*0f4c859eSApple OSS Distributions 	MAC_CHECK(system_check_acct, cred, vp,
85*0f4c859eSApple OSS Distributions 	    vp != NULL ? mac_vnode_label(vp) : NULL);
86*0f4c859eSApple OSS Distributions 
87*0f4c859eSApple OSS Distributions 	return error;
88*0f4c859eSApple OSS Distributions }
89*0f4c859eSApple OSS Distributions 
90*0f4c859eSApple OSS Distributions int
mac_system_check_host_priv(kauth_cred_t cred)91*0f4c859eSApple OSS Distributions mac_system_check_host_priv(kauth_cred_t cred)
92*0f4c859eSApple OSS Distributions {
93*0f4c859eSApple OSS Distributions 	int error;
94*0f4c859eSApple OSS Distributions 
95*0f4c859eSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
96*0f4c859eSApple OSS Distributions 	/* 21167099 - only check if we allow write */
97*0f4c859eSApple OSS Distributions 	if (!mac_system_enforce) {
98*0f4c859eSApple OSS Distributions 		return 0;
99*0f4c859eSApple OSS Distributions 	}
100*0f4c859eSApple OSS Distributions #endif
101*0f4c859eSApple OSS Distributions 
102*0f4c859eSApple OSS Distributions 	MAC_CHECK(system_check_host_priv, cred);
103*0f4c859eSApple OSS Distributions 
104*0f4c859eSApple OSS Distributions 	return error;
105*0f4c859eSApple OSS Distributions }
106*0f4c859eSApple OSS Distributions 
107*0f4c859eSApple OSS Distributions int
mac_system_check_info(kauth_cred_t cred,const char * info_type)108*0f4c859eSApple OSS Distributions mac_system_check_info(kauth_cred_t cred, const char *info_type)
109*0f4c859eSApple OSS Distributions {
110*0f4c859eSApple OSS Distributions 	int error;
111*0f4c859eSApple OSS Distributions 
112*0f4c859eSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
113*0f4c859eSApple OSS Distributions 	/* 21167099 - only check if we allow write */
114*0f4c859eSApple OSS Distributions 	if (!mac_system_enforce) {
115*0f4c859eSApple OSS Distributions 		return 0;
116*0f4c859eSApple OSS Distributions 	}
117*0f4c859eSApple OSS Distributions #endif
118*0f4c859eSApple OSS Distributions 
119*0f4c859eSApple OSS Distributions 	MAC_CHECK(system_check_info, cred, info_type);
120*0f4c859eSApple OSS Distributions 
121*0f4c859eSApple OSS Distributions 	return error;
122*0f4c859eSApple OSS Distributions }
123*0f4c859eSApple OSS Distributions 
124*0f4c859eSApple OSS Distributions int
mac_system_check_nfsd(kauth_cred_t cred)125*0f4c859eSApple OSS Distributions mac_system_check_nfsd(kauth_cred_t cred)
126*0f4c859eSApple OSS Distributions {
127*0f4c859eSApple OSS Distributions 	int error;
128*0f4c859eSApple OSS Distributions 
129*0f4c859eSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
130*0f4c859eSApple OSS Distributions 	/* 21167099 - only check if we allow write */
131*0f4c859eSApple OSS Distributions 	if (!mac_system_enforce) {
132*0f4c859eSApple OSS Distributions 		return 0;
133*0f4c859eSApple OSS Distributions 	}
134*0f4c859eSApple OSS Distributions #endif
135*0f4c859eSApple OSS Distributions 
136*0f4c859eSApple OSS Distributions 	MAC_CHECK(system_check_nfsd, cred);
137*0f4c859eSApple OSS Distributions 
138*0f4c859eSApple OSS Distributions 	return error;
139*0f4c859eSApple OSS Distributions }
140*0f4c859eSApple OSS Distributions 
141*0f4c859eSApple OSS Distributions int
mac_system_check_reboot(kauth_cred_t cred,int howto)142*0f4c859eSApple OSS Distributions mac_system_check_reboot(kauth_cred_t cred, int howto)
143*0f4c859eSApple OSS Distributions {
144*0f4c859eSApple OSS Distributions 	int error;
145*0f4c859eSApple OSS Distributions 
146*0f4c859eSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
147*0f4c859eSApple OSS Distributions 	/* 21167099 - only check if we allow write */
148*0f4c859eSApple OSS Distributions 	if (!mac_system_enforce) {
149*0f4c859eSApple OSS Distributions 		return 0;
150*0f4c859eSApple OSS Distributions 	}
151*0f4c859eSApple OSS Distributions #endif
152*0f4c859eSApple OSS Distributions 
153*0f4c859eSApple OSS Distributions 	MAC_CHECK(system_check_reboot, cred, howto);
154*0f4c859eSApple OSS Distributions 
155*0f4c859eSApple OSS Distributions 	return error;
156*0f4c859eSApple OSS Distributions }
157*0f4c859eSApple OSS Distributions 
158*0f4c859eSApple OSS Distributions 
159*0f4c859eSApple OSS Distributions int
mac_system_check_settime(kauth_cred_t cred)160*0f4c859eSApple OSS Distributions mac_system_check_settime(kauth_cred_t cred)
161*0f4c859eSApple OSS Distributions {
162*0f4c859eSApple OSS Distributions 	int error;
163*0f4c859eSApple OSS Distributions 
164*0f4c859eSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
165*0f4c859eSApple OSS Distributions 	/* 21167099 - only check if we allow write */
166*0f4c859eSApple OSS Distributions 	if (!mac_system_enforce) {
167*0f4c859eSApple OSS Distributions 		return 0;
168*0f4c859eSApple OSS Distributions 	}
169*0f4c859eSApple OSS Distributions #endif
170*0f4c859eSApple OSS Distributions 
171*0f4c859eSApple OSS Distributions 	MAC_CHECK(system_check_settime, cred);
172*0f4c859eSApple OSS Distributions 
173*0f4c859eSApple OSS Distributions 	return error;
174*0f4c859eSApple OSS Distributions }
175*0f4c859eSApple OSS Distributions 
176*0f4c859eSApple OSS Distributions int
mac_system_check_swapon(kauth_cred_t cred,struct vnode * vp)177*0f4c859eSApple OSS Distributions mac_system_check_swapon(kauth_cred_t cred, struct vnode *vp)
178*0f4c859eSApple OSS Distributions {
179*0f4c859eSApple OSS Distributions 	int error;
180*0f4c859eSApple OSS Distributions 
181*0f4c859eSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
182*0f4c859eSApple OSS Distributions 	/* 21167099 - only check if we allow write */
183*0f4c859eSApple OSS Distributions 	if (!mac_system_enforce) {
184*0f4c859eSApple OSS Distributions 		return 0;
185*0f4c859eSApple OSS Distributions 	}
186*0f4c859eSApple OSS Distributions #endif
187*0f4c859eSApple OSS Distributions 
188*0f4c859eSApple OSS Distributions 	MAC_CHECK(system_check_swapon, cred, vp, mac_vnode_label(vp));
189*0f4c859eSApple OSS Distributions 	return error;
190*0f4c859eSApple OSS Distributions }
191*0f4c859eSApple OSS Distributions 
192*0f4c859eSApple OSS Distributions int
mac_system_check_swapoff(kauth_cred_t cred,struct vnode * vp)193*0f4c859eSApple OSS Distributions mac_system_check_swapoff(kauth_cred_t cred, struct vnode *vp)
194*0f4c859eSApple OSS Distributions {
195*0f4c859eSApple OSS Distributions 	int error;
196*0f4c859eSApple OSS Distributions 
197*0f4c859eSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
198*0f4c859eSApple OSS Distributions 	/* 21167099 - only check if we allow write */
199*0f4c859eSApple OSS Distributions 	if (!mac_system_enforce) {
200*0f4c859eSApple OSS Distributions 		return 0;
201*0f4c859eSApple OSS Distributions 	}
202*0f4c859eSApple OSS Distributions #endif
203*0f4c859eSApple OSS Distributions 
204*0f4c859eSApple OSS Distributions 	MAC_CHECK(system_check_swapoff, cred, vp, mac_vnode_label(vp));
205*0f4c859eSApple OSS Distributions 	return error;
206*0f4c859eSApple OSS Distributions }
207*0f4c859eSApple OSS Distributions 
208*0f4c859eSApple OSS Distributions int
mac_system_check_sysctlbyname(kauth_cred_t cred,const char * namestring,int * name,size_t namelen,user_addr_t oldctl,size_t oldlen,user_addr_t newctl,size_t newlen)209*0f4c859eSApple OSS Distributions mac_system_check_sysctlbyname(kauth_cred_t cred, const char *namestring, int *name,
210*0f4c859eSApple OSS Distributions     size_t namelen, user_addr_t oldctl, size_t oldlen,
211*0f4c859eSApple OSS Distributions     user_addr_t newctl, size_t newlen)
212*0f4c859eSApple OSS Distributions {
213*0f4c859eSApple OSS Distributions 	int error;
214*0f4c859eSApple OSS Distributions 
215*0f4c859eSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
216*0f4c859eSApple OSS Distributions 	/* 21167099 - only check if we allow write */
217*0f4c859eSApple OSS Distributions 	if (!mac_system_enforce) {
218*0f4c859eSApple OSS Distributions 		return 0;
219*0f4c859eSApple OSS Distributions 	}
220*0f4c859eSApple OSS Distributions #endif
221*0f4c859eSApple OSS Distributions 
222*0f4c859eSApple OSS Distributions 	MAC_CHECK(system_check_sysctlbyname, cred, namestring,
223*0f4c859eSApple OSS Distributions 	    name, namelen, oldctl, oldlen, newctl, newlen);
224*0f4c859eSApple OSS Distributions 
225*0f4c859eSApple OSS Distributions 	return error;
226*0f4c859eSApple OSS Distributions }
227*0f4c859eSApple OSS Distributions 
228*0f4c859eSApple OSS Distributions int
mac_system_check_kas_info(kauth_cred_t cred,int selector)229*0f4c859eSApple OSS Distributions mac_system_check_kas_info(kauth_cred_t cred, int selector)
230*0f4c859eSApple OSS Distributions {
231*0f4c859eSApple OSS Distributions 	int error;
232*0f4c859eSApple OSS Distributions 
233*0f4c859eSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
234*0f4c859eSApple OSS Distributions 	/* 21167099 - only check if we allow write */
235*0f4c859eSApple OSS Distributions 	if (!mac_system_enforce) {
236*0f4c859eSApple OSS Distributions 		return 0;
237*0f4c859eSApple OSS Distributions 	}
238*0f4c859eSApple OSS Distributions #endif
239*0f4c859eSApple OSS Distributions 
240*0f4c859eSApple OSS Distributions 	MAC_CHECK(system_check_kas_info, cred, selector);
241*0f4c859eSApple OSS Distributions 
242*0f4c859eSApple OSS Distributions 	return error;
243*0f4c859eSApple OSS Distributions }
244