1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2007-2020 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions /*-
29*c54f35caSApple OSS Distributions * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
30*c54f35caSApple OSS Distributions * Copyright (c) 2001 Ilmar S. Habibulin
31*c54f35caSApple OSS Distributions * Copyright (c) 2001, 2002, 2003, 2004 Networks Associates Technology, Inc.
32*c54f35caSApple OSS Distributions * Copyright (c) 2005-2006 SPARTA, Inc.
33*c54f35caSApple OSS Distributions *
34*c54f35caSApple OSS Distributions * This software was developed by Robert Watson and Ilmar Habibulin for the
35*c54f35caSApple OSS Distributions * TrustedBSD Project.
36*c54f35caSApple OSS Distributions *
37*c54f35caSApple OSS Distributions * This software was developed for the FreeBSD Project in part by Network
38*c54f35caSApple OSS Distributions * Associates Laboratories, the Security Research Division of Network
39*c54f35caSApple OSS Distributions * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
40*c54f35caSApple OSS Distributions * as part of the DARPA CHATS research program.
41*c54f35caSApple OSS Distributions *
42*c54f35caSApple OSS Distributions * Redistribution and use in source and binary forms, with or without
43*c54f35caSApple OSS Distributions * modification, are permitted provided that the following conditions
44*c54f35caSApple OSS Distributions * are met:
45*c54f35caSApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
46*c54f35caSApple OSS Distributions * notice, this list of conditions and the following disclaimer.
47*c54f35caSApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
48*c54f35caSApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
49*c54f35caSApple OSS Distributions * documentation and/or other materials provided with the distribution.
50*c54f35caSApple OSS Distributions *
51*c54f35caSApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
52*c54f35caSApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53*c54f35caSApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54*c54f35caSApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55*c54f35caSApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56*c54f35caSApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57*c54f35caSApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*c54f35caSApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59*c54f35caSApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60*c54f35caSApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61*c54f35caSApple OSS Distributions * SUCH DAMAGE.
62*c54f35caSApple OSS Distributions *
63*c54f35caSApple OSS Distributions */
64*c54f35caSApple OSS Distributions
65*c54f35caSApple OSS Distributions /*-
66*c54f35caSApple OSS Distributions * Framework for extensible kernel access control. This file contains
67*c54f35caSApple OSS Distributions * Kernel and userland interface to the framework, policy registration
68*c54f35caSApple OSS Distributions * and composition. Per-object interfaces, controls, and labeling may be
69*c54f35caSApple OSS Distributions * found in src/sys/mac/. Sample policies may be found in src/sys/mac*.
70*c54f35caSApple OSS Distributions */
71*c54f35caSApple OSS Distributions
72*c54f35caSApple OSS Distributions #include <stdarg.h>
73*c54f35caSApple OSS Distributions #include <string.h>
74*c54f35caSApple OSS Distributions #include <security/mac_internal.h>
75*c54f35caSApple OSS Distributions #include <security/mac_mach_internal.h>
76*c54f35caSApple OSS Distributions #include <sys/param.h>
77*c54f35caSApple OSS Distributions #include <sys/vnode.h>
78*c54f35caSApple OSS Distributions #include <sys/vnode_internal.h>
79*c54f35caSApple OSS Distributions #include <sys/vfs_context.h>
80*c54f35caSApple OSS Distributions #include <sys/namei.h>
81*c54f35caSApple OSS Distributions #include <bsd/bsm/audit.h>
82*c54f35caSApple OSS Distributions #include <bsd/security/audit/audit.h>
83*c54f35caSApple OSS Distributions #include <bsd/security/audit/audit_private.h>
84*c54f35caSApple OSS Distributions #include <sys/file.h>
85*c54f35caSApple OSS Distributions #include <sys/file_internal.h>
86*c54f35caSApple OSS Distributions #include <sys/filedesc.h>
87*c54f35caSApple OSS Distributions #include <sys/proc.h>
88*c54f35caSApple OSS Distributions #include <sys/proc_internal.h>
89*c54f35caSApple OSS Distributions #include <sys/kauth.h>
90*c54f35caSApple OSS Distributions #include <sys/sysproto.h>
91*c54f35caSApple OSS Distributions
92*c54f35caSApple OSS Distributions #include <mach/exception_types.h>
93*c54f35caSApple OSS Distributions #include <mach/vm_types.h>
94*c54f35caSApple OSS Distributions #include <mach/vm_prot.h>
95*c54f35caSApple OSS Distributions
96*c54f35caSApple OSS Distributions #include <kern/kalloc.h>
97*c54f35caSApple OSS Distributions #include <kern/sched_prim.h>
98*c54f35caSApple OSS Distributions #include <kern/task.h>
99*c54f35caSApple OSS Distributions
100*c54f35caSApple OSS Distributions #if CONFIG_MACF
101*c54f35caSApple OSS Distributions #include <security/mac.h>
102*c54f35caSApple OSS Distributions #include <security/mac_policy.h>
103*c54f35caSApple OSS Distributions #include <security/mac_framework.h>
104*c54f35caSApple OSS Distributions #include <security/mac_internal.h>
105*c54f35caSApple OSS Distributions #include <security/mac_mach_internal.h>
106*c54f35caSApple OSS Distributions #endif
107*c54f35caSApple OSS Distributions
108*c54f35caSApple OSS Distributions #include <libkern/section_keywords.h>
109*c54f35caSApple OSS Distributions
110*c54f35caSApple OSS Distributions /*
111*c54f35caSApple OSS Distributions * define MB_DEBUG to display run-time debugging information
112*c54f35caSApple OSS Distributions * #define MB_DEBUG 1
113*c54f35caSApple OSS Distributions */
114*c54f35caSApple OSS Distributions
115*c54f35caSApple OSS Distributions #ifdef MB_DEBUG
116*c54f35caSApple OSS Distributions #define DPRINTF(x) printf x
117*c54f35caSApple OSS Distributions #else
118*c54f35caSApple OSS Distributions #define MB_DEBUG
119*c54f35caSApple OSS Distributions #define DPRINTF(x)
120*c54f35caSApple OSS Distributions #endif
121*c54f35caSApple OSS Distributions
122*c54f35caSApple OSS Distributions #if CONFIG_MACF
123*c54f35caSApple OSS Distributions SYSCTL_NODE(, OID_AUTO, security, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
124*c54f35caSApple OSS Distributions "Security Controls");
125*c54f35caSApple OSS Distributions SYSCTL_EXTENSIBLE_NODE(_security, OID_AUTO, mac, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
126*c54f35caSApple OSS Distributions "TrustedBSD MAC policy controls");
127*c54f35caSApple OSS Distributions
128*c54f35caSApple OSS Distributions /*
129*c54f35caSApple OSS Distributions * Declare that the kernel provides MAC support, version 1. This permits
130*c54f35caSApple OSS Distributions * modules to refuse to be loaded if the necessary support isn't present,
131*c54f35caSApple OSS Distributions * even if it's pre-boot.
132*c54f35caSApple OSS Distributions */
133*c54f35caSApple OSS Distributions #if 0
134*c54f35caSApple OSS Distributions MODULE_VERSION(kernel_mac_support, 1);
135*c54f35caSApple OSS Distributions #endif
136*c54f35caSApple OSS Distributions
137*c54f35caSApple OSS Distributions #if MAC_MAX_SLOTS > 32
138*c54f35caSApple OSS Distributions #error "MAC_MAX_SLOTS too large"
139*c54f35caSApple OSS Distributions #endif
140*c54f35caSApple OSS Distributions
141*c54f35caSApple OSS Distributions static unsigned int mac_max_slots = MAC_MAX_SLOTS;
142*c54f35caSApple OSS Distributions static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1;
143*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD | CTLFLAG_LOCKED,
144*c54f35caSApple OSS Distributions &mac_max_slots, 0, "");
145*c54f35caSApple OSS Distributions
146*c54f35caSApple OSS Distributions /*
147*c54f35caSApple OSS Distributions * Has the kernel started generating labeled objects yet? All read/write
148*c54f35caSApple OSS Distributions * access to this variable is serialized during the boot process. Following
149*c54f35caSApple OSS Distributions * the end of serialization, we don't update this flag; no locking.
150*c54f35caSApple OSS Distributions */
151*c54f35caSApple OSS Distributions int mac_late = 0;
152*c54f35caSApple OSS Distributions
153*c54f35caSApple OSS Distributions /*
154*c54f35caSApple OSS Distributions * Flag to indicate whether or not we should allocate label storage for
155*c54f35caSApple OSS Distributions * new vnodes. Since most dynamic policies we currently work with don't
156*c54f35caSApple OSS Distributions * rely on vnode labeling, try to avoid paying the cost of mtag allocation
157*c54f35caSApple OSS Distributions * unless specifically notified of interest. One result of this is
158*c54f35caSApple OSS Distributions * that if a dynamically loaded policy requests vnode labels, it must
159*c54f35caSApple OSS Distributions * be able to deal with a NULL label being returned on any vnodes that
160*c54f35caSApple OSS Distributions * were already in flight when the policy was loaded. Since the policy
161*c54f35caSApple OSS Distributions * already has to deal with uninitialized labels, this probably won't
162*c54f35caSApple OSS Distributions * be a problem.
163*c54f35caSApple OSS Distributions */
164*c54f35caSApple OSS Distributions #if CONFIG_MACF_LAZY_VNODE_LABELS
165*c54f35caSApple OSS Distributions unsigned int mac_label_vnodes = 1;
166*c54f35caSApple OSS Distributions #else
167*c54f35caSApple OSS Distributions unsigned int mac_label_vnodes = 0;
168*c54f35caSApple OSS Distributions #endif /* CONFIG_MACF_LAZY_VNODE_LABELS */
169*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, labelvnodes, SECURITY_MAC_CTLFLAGS
170*c54f35caSApple OSS Distributions #if CONFIG_MACF_LAZY_VNODE_LABELS
171*c54f35caSApple OSS Distributions | CTLFLAG_RD
172*c54f35caSApple OSS Distributions #endif
173*c54f35caSApple OSS Distributions , &mac_label_vnodes, 0, "Label all vnodes");
174*c54f35caSApple OSS Distributions
175*c54f35caSApple OSS Distributions unsigned int mac_vnode_label_count = 0;
176*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, vnode_label_count, SECURITY_MAC_CTLFLAGS | CTLFLAG_RD,
177*c54f35caSApple OSS Distributions &mac_vnode_label_count, 0, "Count of vnode labels");
178*c54f35caSApple OSS Distributions
179*c54f35caSApple OSS Distributions unsigned int mac_device_enforce = 1;
180*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, device_enforce, SECURITY_MAC_CTLFLAGS,
181*c54f35caSApple OSS Distributions &mac_device_enforce, 0, "Enforce MAC policy on device operations");
182*c54f35caSApple OSS Distributions
183*c54f35caSApple OSS Distributions unsigned int mac_pipe_enforce = 1;
184*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, pipe_enforce, SECURITY_MAC_CTLFLAGS,
185*c54f35caSApple OSS Distributions &mac_pipe_enforce, 0, "Enforce MAC policy on pipe operations");
186*c54f35caSApple OSS Distributions
187*c54f35caSApple OSS Distributions unsigned int mac_posixsem_enforce = 1;
188*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, posixsem_enforce, SECURITY_MAC_CTLFLAGS,
189*c54f35caSApple OSS Distributions &mac_posixsem_enforce, 0, "Enforce MAC policy on POSIX semaphores");
190*c54f35caSApple OSS Distributions
191*c54f35caSApple OSS Distributions unsigned int mac_posixshm_enforce = 1;
192*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, posixshm_enforce, SECURITY_MAC_CTLFLAGS,
193*c54f35caSApple OSS Distributions &mac_posixshm_enforce, 0, "Enforce MAC policy on Posix Shared Memory");
194*c54f35caSApple OSS Distributions
195*c54f35caSApple OSS Distributions unsigned int mac_proc_enforce = 1;
196*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, proc_enforce, SECURITY_MAC_CTLFLAGS,
197*c54f35caSApple OSS Distributions &mac_proc_enforce, 0, "Enforce MAC policy on process operations");
198*c54f35caSApple OSS Distributions
199*c54f35caSApple OSS Distributions unsigned int mac_socket_enforce = 1;
200*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, socket_enforce, SECURITY_MAC_CTLFLAGS,
201*c54f35caSApple OSS Distributions &mac_socket_enforce, 0, "Enforce MAC policy on socket operations");
202*c54f35caSApple OSS Distributions
203*c54f35caSApple OSS Distributions unsigned int mac_system_enforce = 1;
204*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, system_enforce, SECURITY_MAC_CTLFLAGS,
205*c54f35caSApple OSS Distributions &mac_system_enforce, 0, "Enforce MAC policy on system-wide interfaces");
206*c54f35caSApple OSS Distributions
207*c54f35caSApple OSS Distributions unsigned int mac_sysvmsg_enforce = 1;
208*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, sysvmsg_enforce, SECURITY_MAC_CTLFLAGS,
209*c54f35caSApple OSS Distributions &mac_sysvmsg_enforce, 0, "Enforce MAC policy on System V IPC message queues");
210*c54f35caSApple OSS Distributions
211*c54f35caSApple OSS Distributions unsigned int mac_sysvsem_enforce = 1;
212*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, sysvsem_enforce, SECURITY_MAC_CTLFLAGS,
213*c54f35caSApple OSS Distributions &mac_sysvsem_enforce, 0, "Enforce MAC policy on System V IPC semaphores");
214*c54f35caSApple OSS Distributions
215*c54f35caSApple OSS Distributions unsigned int mac_sysvshm_enforce = 1;
216*c54f35caSApple OSS Distributions SYSCTL_INT(_security_mac, OID_AUTO, sysvshm_enforce, SECURITY_MAC_CTLFLAGS,
217*c54f35caSApple OSS Distributions &mac_sysvshm_enforce, 0, "Enforce MAC policy on System V Shared Memory");
218*c54f35caSApple OSS Distributions
219*c54f35caSApple OSS Distributions unsigned int mac_vm_enforce = 1;
220*c54f35caSApple OSS Distributions SYSCTL_INT(_security_mac, OID_AUTO, vm_enforce, SECURITY_MAC_CTLFLAGS,
221*c54f35caSApple OSS Distributions &mac_vm_enforce, 0, "Enforce MAC policy on VM operations");
222*c54f35caSApple OSS Distributions
223*c54f35caSApple OSS Distributions unsigned int mac_vnode_enforce = 1;
224*c54f35caSApple OSS Distributions SYSCTL_UINT(_security_mac, OID_AUTO, vnode_enforce, SECURITY_MAC_CTLFLAGS,
225*c54f35caSApple OSS Distributions &mac_vnode_enforce, 0, "Enforce MAC policy on vnode operations");
226*c54f35caSApple OSS Distributions
227*c54f35caSApple OSS Distributions /*
228*c54f35caSApple OSS Distributions * mac_policy_list holds the list of policy modules. Modules with a
229*c54f35caSApple OSS Distributions * handle lower than staticmax are considered "static" and cannot be
230*c54f35caSApple OSS Distributions * unloaded. Such policies can be invoked without holding the busy count.
231*c54f35caSApple OSS Distributions *
232*c54f35caSApple OSS Distributions * Modules with a handle at or above the staticmax high water mark
233*c54f35caSApple OSS Distributions * are considered to be "dynamic" policies. A busy count is maintained
234*c54f35caSApple OSS Distributions * for the list, stored in mac_policy_busy. The busy count is protected
235*c54f35caSApple OSS Distributions * by mac_policy_mtx; the list may be modified only while the busy
236*c54f35caSApple OSS Distributions * count is 0, requiring that the lock be held to prevent new references
237*c54f35caSApple OSS Distributions * to the list from being acquired. For almost all operations,
238*c54f35caSApple OSS Distributions * incrementing the busy count is sufficient to guarantee consistency,
239*c54f35caSApple OSS Distributions * as the list cannot be modified while the busy count is elevated.
240*c54f35caSApple OSS Distributions * For a few special operations involving a change to the list of
241*c54f35caSApple OSS Distributions * active policies, the mtx itself must be held.
242*c54f35caSApple OSS Distributions */
243*c54f35caSApple OSS Distributions static LCK_GRP_DECLARE(mac_lck_grp, "MAC lock");
244*c54f35caSApple OSS Distributions static LCK_MTX_DECLARE(mac_policy_mtx, &mac_lck_grp);
245*c54f35caSApple OSS Distributions
246*c54f35caSApple OSS Distributions /*
247*c54f35caSApple OSS Distributions * Policy list array allocation chunk size. Each entry holds a pointer.
248*c54f35caSApple OSS Distributions */
249*c54f35caSApple OSS Distributions #define MAC_POLICY_LIST_CHUNKSIZE 8
250*c54f35caSApple OSS Distributions
251*c54f35caSApple OSS Distributions static int mac_policy_busy;
252*c54f35caSApple OSS Distributions
253*c54f35caSApple OSS Distributions #if !XNU_TARGET_OS_OSX
254*c54f35caSApple OSS Distributions SECURITY_READ_ONLY_LATE(mac_policy_list_t) mac_policy_list;
255*c54f35caSApple OSS Distributions SECURITY_READ_ONLY_LATE(static struct mac_policy_list_element) mac_policy_static_entries[MAC_POLICY_LIST_CHUNKSIZE];
256*c54f35caSApple OSS Distributions #else
257*c54f35caSApple OSS Distributions mac_policy_list_t mac_policy_list;
258*c54f35caSApple OSS Distributions #endif
259*c54f35caSApple OSS Distributions
260*c54f35caSApple OSS Distributions /*
261*c54f35caSApple OSS Distributions * mac_label_element_list holds the master list of label namespaces for
262*c54f35caSApple OSS Distributions * all the policies. When a policy is loaded, each of it's label namespace
263*c54f35caSApple OSS Distributions * elements is added to the master list if not already present. When a
264*c54f35caSApple OSS Distributions * policy is unloaded, the namespace elements are removed if no other
265*c54f35caSApple OSS Distributions * policy is interested in that namespace element.
266*c54f35caSApple OSS Distributions */
267*c54f35caSApple OSS Distributions struct mac_label_element_list_t mac_label_element_list;
268*c54f35caSApple OSS Distributions struct mac_label_element_list_t mac_static_label_element_list;
269*c54f35caSApple OSS Distributions
270*c54f35caSApple OSS Distributions static __inline void
mac_policy_grab_exclusive(void)271*c54f35caSApple OSS Distributions mac_policy_grab_exclusive(void)
272*c54f35caSApple OSS Distributions {
273*c54f35caSApple OSS Distributions lck_mtx_lock(&mac_policy_mtx);
274*c54f35caSApple OSS Distributions while (mac_policy_busy != 0) {
275*c54f35caSApple OSS Distributions lck_mtx_sleep(&mac_policy_mtx, LCK_SLEEP_UNLOCK,
276*c54f35caSApple OSS Distributions (event_t)&mac_policy_busy, THREAD_UNINT);
277*c54f35caSApple OSS Distributions lck_mtx_lock(&mac_policy_mtx);
278*c54f35caSApple OSS Distributions }
279*c54f35caSApple OSS Distributions }
280*c54f35caSApple OSS Distributions
281*c54f35caSApple OSS Distributions static __inline void
mac_policy_release_exclusive(void)282*c54f35caSApple OSS Distributions mac_policy_release_exclusive(void)
283*c54f35caSApple OSS Distributions {
284*c54f35caSApple OSS Distributions KASSERT(mac_policy_busy == 0,
285*c54f35caSApple OSS Distributions ("mac_policy_release_exclusive(): not exclusive"));
286*c54f35caSApple OSS Distributions lck_mtx_unlock(&mac_policy_mtx);
287*c54f35caSApple OSS Distributions thread_wakeup((event_t) &mac_policy_busy);
288*c54f35caSApple OSS Distributions }
289*c54f35caSApple OSS Distributions
290*c54f35caSApple OSS Distributions void
mac_policy_list_busy(void)291*c54f35caSApple OSS Distributions mac_policy_list_busy(void)
292*c54f35caSApple OSS Distributions {
293*c54f35caSApple OSS Distributions lck_mtx_lock(&mac_policy_mtx);
294*c54f35caSApple OSS Distributions mac_policy_busy++;
295*c54f35caSApple OSS Distributions lck_mtx_unlock(&mac_policy_mtx);
296*c54f35caSApple OSS Distributions }
297*c54f35caSApple OSS Distributions
298*c54f35caSApple OSS Distributions int
mac_policy_list_conditional_busy(void)299*c54f35caSApple OSS Distributions mac_policy_list_conditional_busy(void)
300*c54f35caSApple OSS Distributions {
301*c54f35caSApple OSS Distributions int ret;
302*c54f35caSApple OSS Distributions
303*c54f35caSApple OSS Distributions if (mac_policy_list.numloaded <= mac_policy_list.staticmax) {
304*c54f35caSApple OSS Distributions return 0;
305*c54f35caSApple OSS Distributions }
306*c54f35caSApple OSS Distributions
307*c54f35caSApple OSS Distributions lck_mtx_lock(&mac_policy_mtx);
308*c54f35caSApple OSS Distributions if (mac_policy_list.numloaded > mac_policy_list.staticmax) {
309*c54f35caSApple OSS Distributions mac_policy_busy++;
310*c54f35caSApple OSS Distributions ret = 1;
311*c54f35caSApple OSS Distributions } else {
312*c54f35caSApple OSS Distributions ret = 0;
313*c54f35caSApple OSS Distributions }
314*c54f35caSApple OSS Distributions lck_mtx_unlock(&mac_policy_mtx);
315*c54f35caSApple OSS Distributions return ret;
316*c54f35caSApple OSS Distributions }
317*c54f35caSApple OSS Distributions
318*c54f35caSApple OSS Distributions void
mac_policy_list_unbusy(void)319*c54f35caSApple OSS Distributions mac_policy_list_unbusy(void)
320*c54f35caSApple OSS Distributions {
321*c54f35caSApple OSS Distributions lck_mtx_lock(&mac_policy_mtx);
322*c54f35caSApple OSS Distributions mac_policy_busy--;
323*c54f35caSApple OSS Distributions KASSERT(mac_policy_busy >= 0, ("MAC_POLICY_LIST_LOCK"));
324*c54f35caSApple OSS Distributions if (mac_policy_busy == 0) {
325*c54f35caSApple OSS Distributions thread_wakeup(&mac_policy_busy);
326*c54f35caSApple OSS Distributions }
327*c54f35caSApple OSS Distributions lck_mtx_unlock(&mac_policy_mtx);
328*c54f35caSApple OSS Distributions }
329*c54f35caSApple OSS Distributions
330*c54f35caSApple OSS Distributions /*
331*c54f35caSApple OSS Distributions * Early pre-malloc MAC initialization, including appropriate SMP locks.
332*c54f35caSApple OSS Distributions */
333*c54f35caSApple OSS Distributions void
mac_policy_init(void)334*c54f35caSApple OSS Distributions mac_policy_init(void)
335*c54f35caSApple OSS Distributions {
336*c54f35caSApple OSS Distributions mac_policy_list.numloaded = 0;
337*c54f35caSApple OSS Distributions mac_policy_list.max = MAC_POLICY_LIST_CHUNKSIZE;
338*c54f35caSApple OSS Distributions mac_policy_list.maxindex = 0;
339*c54f35caSApple OSS Distributions mac_policy_list.staticmax = 0;
340*c54f35caSApple OSS Distributions mac_policy_list.freehint = 0;
341*c54f35caSApple OSS Distributions mac_policy_list.chunks = 1;
342*c54f35caSApple OSS Distributions
343*c54f35caSApple OSS Distributions #if !XNU_TARGET_OS_OSX
344*c54f35caSApple OSS Distributions mac_policy_list.entries = mac_policy_static_entries;
345*c54f35caSApple OSS Distributions #else
346*c54f35caSApple OSS Distributions mac_policy_list.entries = kalloc_type(struct mac_policy_list_element,
347*c54f35caSApple OSS Distributions MAC_POLICY_LIST_CHUNKSIZE, Z_WAITOK | Z_ZERO);
348*c54f35caSApple OSS Distributions #endif
349*c54f35caSApple OSS Distributions
350*c54f35caSApple OSS Distributions SLIST_INIT(&mac_label_element_list);
351*c54f35caSApple OSS Distributions SLIST_INIT(&mac_static_label_element_list);
352*c54f35caSApple OSS Distributions }
353*c54f35caSApple OSS Distributions
354*c54f35caSApple OSS Distributions /* Function pointer set up for loading security extensions.
355*c54f35caSApple OSS Distributions * It is set to an actual function after OSlibkernInit()
356*c54f35caSApple OSS Distributions * has been called, and is set back to 0 by OSKextRemoveKextBootstrap()
357*c54f35caSApple OSS Distributions * after bsd_init().
358*c54f35caSApple OSS Distributions */
359*c54f35caSApple OSS Distributions void (*load_security_extensions_function)(void) = 0;
360*c54f35caSApple OSS Distributions
361*c54f35caSApple OSS Distributions /*
362*c54f35caSApple OSS Distributions * Init after early Mach startup, but before BSD
363*c54f35caSApple OSS Distributions */
364*c54f35caSApple OSS Distributions void
mac_policy_initmach(void)365*c54f35caSApple OSS Distributions mac_policy_initmach(void)
366*c54f35caSApple OSS Distributions {
367*c54f35caSApple OSS Distributions /*
368*c54f35caSApple OSS Distributions * For the purposes of modules that want to know if they were
369*c54f35caSApple OSS Distributions * loaded "early", set the mac_late flag once we've processed
370*c54f35caSApple OSS Distributions * modules either linked into the kernel, or loaded before the
371*c54f35caSApple OSS Distributions * kernel startup.
372*c54f35caSApple OSS Distributions */
373*c54f35caSApple OSS Distributions
374*c54f35caSApple OSS Distributions if (load_security_extensions_function) {
375*c54f35caSApple OSS Distributions load_security_extensions_function();
376*c54f35caSApple OSS Distributions }
377*c54f35caSApple OSS Distributions mac_late = 1;
378*c54f35caSApple OSS Distributions }
379*c54f35caSApple OSS Distributions
380*c54f35caSApple OSS Distributions /*
381*c54f35caSApple OSS Distributions * BSD startup.
382*c54f35caSApple OSS Distributions */
383*c54f35caSApple OSS Distributions void
mac_policy_initbsd(void)384*c54f35caSApple OSS Distributions mac_policy_initbsd(void)
385*c54f35caSApple OSS Distributions {
386*c54f35caSApple OSS Distributions struct mac_policy_conf *mpc;
387*c54f35caSApple OSS Distributions u_int i;
388*c54f35caSApple OSS Distributions
389*c54f35caSApple OSS Distributions printf("MAC Framework successfully initialized\n");
390*c54f35caSApple OSS Distributions
391*c54f35caSApple OSS Distributions /* Call bsd init functions of already loaded policies */
392*c54f35caSApple OSS Distributions
393*c54f35caSApple OSS Distributions /*
394*c54f35caSApple OSS Distributions * Using the exclusive lock means no other framework entry
395*c54f35caSApple OSS Distributions * points can proceed while initializations are running.
396*c54f35caSApple OSS Distributions * This may not be necessary.
397*c54f35caSApple OSS Distributions */
398*c54f35caSApple OSS Distributions mac_policy_grab_exclusive();
399*c54f35caSApple OSS Distributions
400*c54f35caSApple OSS Distributions for (i = 0; i <= mac_policy_list.maxindex; i++) {
401*c54f35caSApple OSS Distributions mpc = mac_get_mpc(i);
402*c54f35caSApple OSS Distributions if ((mpc != NULL) && (mpc->mpc_ops->mpo_policy_initbsd != NULL)) {
403*c54f35caSApple OSS Distributions (*(mpc->mpc_ops->mpo_policy_initbsd))(mpc);
404*c54f35caSApple OSS Distributions }
405*c54f35caSApple OSS Distributions }
406*c54f35caSApple OSS Distributions
407*c54f35caSApple OSS Distributions mac_policy_release_exclusive();
408*c54f35caSApple OSS Distributions }
409*c54f35caSApple OSS Distributions
410*c54f35caSApple OSS Distributions /*
411*c54f35caSApple OSS Distributions * After a policy has been loaded, add the label namespaces managed by the
412*c54f35caSApple OSS Distributions * policy to either the static or non-static label namespace list.
413*c54f35caSApple OSS Distributions * A namespace is added to the the list only if it is not already on one of
414*c54f35caSApple OSS Distributions * the lists.
415*c54f35caSApple OSS Distributions */
416*c54f35caSApple OSS Distributions void
mac_policy_addto_labellist(mac_policy_handle_t handle,int static_entry)417*c54f35caSApple OSS Distributions mac_policy_addto_labellist(mac_policy_handle_t handle, int static_entry)
418*c54f35caSApple OSS Distributions {
419*c54f35caSApple OSS Distributions struct mac_label_element *mle, *mle_tmp;
420*c54f35caSApple OSS Distributions struct mac_label_listener *mll, *mll_tmp;
421*c54f35caSApple OSS Distributions struct mac_label_element_list_t *list;
422*c54f35caSApple OSS Distributions struct mac_policy_conf *mpc;
423*c54f35caSApple OSS Distributions const char *name, *name2;
424*c54f35caSApple OSS Distributions struct mac_label_element_list_t mles = SLIST_HEAD_INITIALIZER(mles);
425*c54f35caSApple OSS Distributions struct mac_label_listeners_t mlls = SLIST_HEAD_INITIALIZER(mlls);
426*c54f35caSApple OSS Distributions
427*c54f35caSApple OSS Distributions mpc = mac_get_mpc(handle);
428*c54f35caSApple OSS Distributions
429*c54f35caSApple OSS Distributions if (mpc->mpc_labelnames == NULL) {
430*c54f35caSApple OSS Distributions return;
431*c54f35caSApple OSS Distributions }
432*c54f35caSApple OSS Distributions
433*c54f35caSApple OSS Distributions if (mpc->mpc_labelname_count == 0) {
434*c54f35caSApple OSS Distributions return;
435*c54f35caSApple OSS Distributions }
436*c54f35caSApple OSS Distributions
437*c54f35caSApple OSS Distributions if (static_entry) {
438*c54f35caSApple OSS Distributions list = &mac_static_label_element_list;
439*c54f35caSApple OSS Distributions } else {
440*c54f35caSApple OSS Distributions list = &mac_label_element_list;
441*c54f35caSApple OSS Distributions }
442*c54f35caSApple OSS Distributions
443*c54f35caSApple OSS Distributions /*
444*c54f35caSApple OSS Distributions * Before we grab the policy list lock, allocate enough memory
445*c54f35caSApple OSS Distributions * to contain the potential new elements so we don't have to
446*c54f35caSApple OSS Distributions * give up the lock, or allocate with the lock held.
447*c54f35caSApple OSS Distributions */
448*c54f35caSApple OSS Distributions for (uint32_t idx = 0; idx < mpc->mpc_labelname_count; idx++) {
449*c54f35caSApple OSS Distributions mle = kalloc_type(struct mac_label_element, Z_WAITOK_ZERO_NOFAIL);
450*c54f35caSApple OSS Distributions SLIST_INSERT_HEAD(&mles, mle, mle_list);
451*c54f35caSApple OSS Distributions
452*c54f35caSApple OSS Distributions mll = kalloc_type(struct mac_label_listener, Z_WAITOK);
453*c54f35caSApple OSS Distributions SLIST_INSERT_HEAD(&mlls, mll, mll_list);
454*c54f35caSApple OSS Distributions }
455*c54f35caSApple OSS Distributions
456*c54f35caSApple OSS Distributions if (mac_late) {
457*c54f35caSApple OSS Distributions mac_policy_grab_exclusive();
458*c54f35caSApple OSS Distributions }
459*c54f35caSApple OSS Distributions for (uint32_t idx = 0; idx < mpc->mpc_labelname_count; idx++) {
460*c54f35caSApple OSS Distributions if (*(name = mpc->mpc_labelnames[idx]) == '?') {
461*c54f35caSApple OSS Distributions name++;
462*c54f35caSApple OSS Distributions }
463*c54f35caSApple OSS Distributions /*
464*c54f35caSApple OSS Distributions * Check both label element lists and add to the
465*c54f35caSApple OSS Distributions * appropriate list only if not already on a list.
466*c54f35caSApple OSS Distributions */
467*c54f35caSApple OSS Distributions SLIST_FOREACH(mle, &mac_static_label_element_list, mle_list) {
468*c54f35caSApple OSS Distributions if (*(name2 = mle->mle_name) == '?') {
469*c54f35caSApple OSS Distributions name2++;
470*c54f35caSApple OSS Distributions }
471*c54f35caSApple OSS Distributions if (strcmp(name, name2) == 0) {
472*c54f35caSApple OSS Distributions break;
473*c54f35caSApple OSS Distributions }
474*c54f35caSApple OSS Distributions }
475*c54f35caSApple OSS Distributions if (mle == NULL) {
476*c54f35caSApple OSS Distributions SLIST_FOREACH(mle, &mac_label_element_list, mle_list) {
477*c54f35caSApple OSS Distributions if (*(name2 = mle->mle_name) == '?') {
478*c54f35caSApple OSS Distributions name2++;
479*c54f35caSApple OSS Distributions }
480*c54f35caSApple OSS Distributions if (strcmp(name, name2) == 0) {
481*c54f35caSApple OSS Distributions break;
482*c54f35caSApple OSS Distributions }
483*c54f35caSApple OSS Distributions }
484*c54f35caSApple OSS Distributions }
485*c54f35caSApple OSS Distributions if (mle == NULL) {
486*c54f35caSApple OSS Distributions mle = SLIST_FIRST(&mles);
487*c54f35caSApple OSS Distributions SLIST_REMOVE_HEAD(&mles, mle_list);
488*c54f35caSApple OSS Distributions strlcpy(mle->mle_name, mpc->mpc_labelnames[idx],
489*c54f35caSApple OSS Distributions MAC_MAX_LABEL_ELEMENT_NAME);
490*c54f35caSApple OSS Distributions SLIST_INIT(&mle->mle_listeners);
491*c54f35caSApple OSS Distributions SLIST_INSERT_HEAD(list, mle, mle_list);
492*c54f35caSApple OSS Distributions }
493*c54f35caSApple OSS Distributions
494*c54f35caSApple OSS Distributions mll = SLIST_FIRST(&mlls);
495*c54f35caSApple OSS Distributions SLIST_REMOVE_HEAD(&mlls, mll_list);
496*c54f35caSApple OSS Distributions /* Add policy handler as a listener. */
497*c54f35caSApple OSS Distributions mll->mll_handle = handle;
498*c54f35caSApple OSS Distributions SLIST_INSERT_HEAD(&mle->mle_listeners, mll, mll_list);
499*c54f35caSApple OSS Distributions }
500*c54f35caSApple OSS Distributions if (mac_late) {
501*c54f35caSApple OSS Distributions mac_policy_release_exclusive();
502*c54f35caSApple OSS Distributions }
503*c54f35caSApple OSS Distributions
504*c54f35caSApple OSS Distributions SLIST_FOREACH_SAFE(mle, &mles, mle_list, mle_tmp) {
505*c54f35caSApple OSS Distributions kfree_type(struct mac_label_element, mle);
506*c54f35caSApple OSS Distributions }
507*c54f35caSApple OSS Distributions SLIST_FOREACH_SAFE(mll, &mlls, mll_list, mll_tmp) {
508*c54f35caSApple OSS Distributions kfree_type(struct mac_label_listener, mll);
509*c54f35caSApple OSS Distributions }
510*c54f35caSApple OSS Distributions }
511*c54f35caSApple OSS Distributions
512*c54f35caSApple OSS Distributions /*
513*c54f35caSApple OSS Distributions * After a policy has been unloaded, remove the label namespaces that the
514*c54f35caSApple OSS Distributions * the policy manages from the non-static list of namespaces.
515*c54f35caSApple OSS Distributions * The removal only takes place when no other policy is interested in the
516*c54f35caSApple OSS Distributions * namespace.
517*c54f35caSApple OSS Distributions *
518*c54f35caSApple OSS Distributions * Must be called with the policy exclusive lock held.
519*c54f35caSApple OSS Distributions */
520*c54f35caSApple OSS Distributions void
mac_policy_removefrom_labellist(mac_policy_handle_t handle)521*c54f35caSApple OSS Distributions mac_policy_removefrom_labellist(mac_policy_handle_t handle)
522*c54f35caSApple OSS Distributions {
523*c54f35caSApple OSS Distributions struct mac_label_listener *mll, **mllp;
524*c54f35caSApple OSS Distributions struct mac_label_element *mle, **mlep;
525*c54f35caSApple OSS Distributions struct mac_policy_conf *mpc;
526*c54f35caSApple OSS Distributions
527*c54f35caSApple OSS Distributions mpc = mac_get_mpc(handle);
528*c54f35caSApple OSS Distributions
529*c54f35caSApple OSS Distributions if (mpc->mpc_labelnames == NULL) {
530*c54f35caSApple OSS Distributions return;
531*c54f35caSApple OSS Distributions }
532*c54f35caSApple OSS Distributions
533*c54f35caSApple OSS Distributions if (mpc->mpc_labelname_count == 0) {
534*c54f35caSApple OSS Distributions return;
535*c54f35caSApple OSS Distributions }
536*c54f35caSApple OSS Distributions
537*c54f35caSApple OSS Distributions /*
538*c54f35caSApple OSS Distributions * Unregister policy as being interested in any label
539*c54f35caSApple OSS Distributions * namespaces. If no other policy is listening, remove
540*c54f35caSApple OSS Distributions * that label element from the list. Note that we only
541*c54f35caSApple OSS Distributions * have to worry about the non-static list.
542*c54f35caSApple OSS Distributions */
543*c54f35caSApple OSS Distributions SLIST_FOREACH_PREVPTR(mle, mlep, &mac_label_element_list, mle_list) {
544*c54f35caSApple OSS Distributions SLIST_FOREACH_PREVPTR(mll, mllp, &mle->mle_listeners, mll_list) {
545*c54f35caSApple OSS Distributions if (mll->mll_handle == handle) {
546*c54f35caSApple OSS Distributions *mllp = SLIST_NEXT(mll, mll_list);
547*c54f35caSApple OSS Distributions kfree_type(struct mac_label_listener, mll);
548*c54f35caSApple OSS Distributions if (SLIST_EMPTY(&mle->mle_listeners)) {
549*c54f35caSApple OSS Distributions *mlep = SLIST_NEXT(mle, mle_list);
550*c54f35caSApple OSS Distributions kfree_type(struct mac_label_element, mle);
551*c54f35caSApple OSS Distributions }
552*c54f35caSApple OSS Distributions return;
553*c54f35caSApple OSS Distributions }
554*c54f35caSApple OSS Distributions }
555*c54f35caSApple OSS Distributions }
556*c54f35caSApple OSS Distributions }
557*c54f35caSApple OSS Distributions
558*c54f35caSApple OSS Distributions /*
559*c54f35caSApple OSS Distributions * After the policy list has changed, walk the list to update any global
560*c54f35caSApple OSS Distributions * flags.
561*c54f35caSApple OSS Distributions */
562*c54f35caSApple OSS Distributions static void
mac_policy_updateflags(void)563*c54f35caSApple OSS Distributions mac_policy_updateflags(void)
564*c54f35caSApple OSS Distributions {
565*c54f35caSApple OSS Distributions }
566*c54f35caSApple OSS Distributions
567*c54f35caSApple OSS Distributions static __inline void
mac_policy_fixup_mmd_list(struct mac_module_data * new)568*c54f35caSApple OSS Distributions mac_policy_fixup_mmd_list(struct mac_module_data *new)
569*c54f35caSApple OSS Distributions {
570*c54f35caSApple OSS Distributions struct mac_module_data *old;
571*c54f35caSApple OSS Distributions struct mac_module_data_element *ele, *aele;
572*c54f35caSApple OSS Distributions struct mac_module_data_list *arr, *dict;
573*c54f35caSApple OSS Distributions unsigned int i, j, k;
574*c54f35caSApple OSS Distributions
575*c54f35caSApple OSS Distributions old = new->base_addr;
576*c54f35caSApple OSS Distributions DPRINTF(("fixup_mmd: old %p new %p\n", old, new));
577*c54f35caSApple OSS Distributions for (i = 0; i < new->count; i++) {
578*c54f35caSApple OSS Distributions ele = &(new->data[i]);
579*c54f35caSApple OSS Distributions DPRINTF(("fixup_mmd: ele %p\n", ele));
580*c54f35caSApple OSS Distributions DPRINTF((" key %p value %p\n", ele->key, ele->value));
581*c54f35caSApple OSS Distributions mmd_fixup_ele(old, new, ele); /* Fix up key/value ptrs. */
582*c54f35caSApple OSS Distributions DPRINTF((" key %p value %p\n", ele->key, ele->value));
583*c54f35caSApple OSS Distributions if (ele->value_type == MAC_DATA_TYPE_ARRAY) {
584*c54f35caSApple OSS Distributions arr = (struct mac_module_data_list *)ele->value;
585*c54f35caSApple OSS Distributions DPRINTF(("fixup_mmd: array @%p\n", arr));
586*c54f35caSApple OSS Distributions for (j = 0; j < arr->count; j++) {
587*c54f35caSApple OSS Distributions aele = &(arr->list[j]);
588*c54f35caSApple OSS Distributions DPRINTF(("fixup_mmd: aele %p\n", aele));
589*c54f35caSApple OSS Distributions DPRINTF((" key %p value %p\n", aele->key, aele->value));
590*c54f35caSApple OSS Distributions mmd_fixup_ele(old, new, aele);
591*c54f35caSApple OSS Distributions DPRINTF((" key %p value %p\n", aele->key, aele->value));
592*c54f35caSApple OSS Distributions if (arr->type == MAC_DATA_TYPE_DICT) {
593*c54f35caSApple OSS Distributions dict = (struct mac_module_data_list *)aele->value;
594*c54f35caSApple OSS Distributions DPRINTF(("fixup_mmd: dict @%p\n", dict));
595*c54f35caSApple OSS Distributions for (k = 0; k < dict->count; k++) {
596*c54f35caSApple OSS Distributions mmd_fixup_ele(old, new,
597*c54f35caSApple OSS Distributions &(dict->list[k]));
598*c54f35caSApple OSS Distributions }
599*c54f35caSApple OSS Distributions }
600*c54f35caSApple OSS Distributions }
601*c54f35caSApple OSS Distributions }
602*c54f35caSApple OSS Distributions }
603*c54f35caSApple OSS Distributions new->base_addr = new;
604*c54f35caSApple OSS Distributions }
605*c54f35caSApple OSS Distributions
606*c54f35caSApple OSS Distributions int
mac_policy_register(struct mac_policy_conf * mpc,mac_policy_handle_t * handlep,void * xd)607*c54f35caSApple OSS Distributions mac_policy_register(struct mac_policy_conf *mpc, mac_policy_handle_t *handlep,
608*c54f35caSApple OSS Distributions void *xd)
609*c54f35caSApple OSS Distributions {
610*c54f35caSApple OSS Distributions #if XNU_TARGET_OS_OSX
611*c54f35caSApple OSS Distributions struct mac_policy_list_element *tmac_policy_list_element;
612*c54f35caSApple OSS Distributions #endif
613*c54f35caSApple OSS Distributions int error, slot, static_entry = 0;
614*c54f35caSApple OSS Distributions u_int i;
615*c54f35caSApple OSS Distributions
616*c54f35caSApple OSS Distributions /*
617*c54f35caSApple OSS Distributions * Some preliminary checks to make sure the policy's conf structure
618*c54f35caSApple OSS Distributions * contains the required fields.
619*c54f35caSApple OSS Distributions */
620*c54f35caSApple OSS Distributions if (mpc->mpc_name == NULL) {
621*c54f35caSApple OSS Distributions panic("policy's name is not set");
622*c54f35caSApple OSS Distributions }
623*c54f35caSApple OSS Distributions
624*c54f35caSApple OSS Distributions if (mpc->mpc_fullname == NULL) {
625*c54f35caSApple OSS Distributions panic("policy's full name is not set");
626*c54f35caSApple OSS Distributions }
627*c54f35caSApple OSS Distributions
628*c54f35caSApple OSS Distributions if (mpc->mpc_labelname_count > MAC_MAX_MANAGED_NAMESPACES) {
629*c54f35caSApple OSS Distributions panic("policy's managed label namespaces exceeds maximum");
630*c54f35caSApple OSS Distributions }
631*c54f35caSApple OSS Distributions
632*c54f35caSApple OSS Distributions if (mpc->mpc_ops == NULL) {
633*c54f35caSApple OSS Distributions panic("policy's OPs field is NULL");
634*c54f35caSApple OSS Distributions }
635*c54f35caSApple OSS Distributions
636*c54f35caSApple OSS Distributions error = 0;
637*c54f35caSApple OSS Distributions
638*c54f35caSApple OSS Distributions if (mac_late) {
639*c54f35caSApple OSS Distributions if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE) {
640*c54f35caSApple OSS Distributions printf("Module %s does not support late loading.\n",
641*c54f35caSApple OSS Distributions mpc->mpc_name);
642*c54f35caSApple OSS Distributions return EPERM;
643*c54f35caSApple OSS Distributions }
644*c54f35caSApple OSS Distributions mac_policy_grab_exclusive();
645*c54f35caSApple OSS Distributions }
646*c54f35caSApple OSS Distributions
647*c54f35caSApple OSS Distributions if (mac_policy_list.numloaded >= mac_policy_list.max) {
648*c54f35caSApple OSS Distributions #if XNU_TARGET_OS_OSX
649*c54f35caSApple OSS Distributions /* allocate new policy list array, zero new chunk */
650*c54f35caSApple OSS Distributions tmac_policy_list_element =
651*c54f35caSApple OSS Distributions kalloc_type(struct mac_policy_list_element,
652*c54f35caSApple OSS Distributions MAC_POLICY_LIST_CHUNKSIZE * (mac_policy_list.chunks + 1),
653*c54f35caSApple OSS Distributions Z_WAITOK | Z_ZERO);
654*c54f35caSApple OSS Distributions
655*c54f35caSApple OSS Distributions /* copy old entries into new list */
656*c54f35caSApple OSS Distributions memcpy(tmac_policy_list_element, mac_policy_list.entries,
657*c54f35caSApple OSS Distributions sizeof(struct mac_policy_list_element) *
658*c54f35caSApple OSS Distributions MAC_POLICY_LIST_CHUNKSIZE * mac_policy_list.chunks);
659*c54f35caSApple OSS Distributions
660*c54f35caSApple OSS Distributions /* free old array */
661*c54f35caSApple OSS Distributions kfree_type(struct mac_policy_list_element,
662*c54f35caSApple OSS Distributions MAC_POLICY_LIST_CHUNKSIZE * mac_policy_list.chunks,
663*c54f35caSApple OSS Distributions mac_policy_list.entries);
664*c54f35caSApple OSS Distributions
665*c54f35caSApple OSS Distributions mac_policy_list.entries = tmac_policy_list_element;
666*c54f35caSApple OSS Distributions
667*c54f35caSApple OSS Distributions /* Update maximums, etc */
668*c54f35caSApple OSS Distributions mac_policy_list.max += MAC_POLICY_LIST_CHUNKSIZE;
669*c54f35caSApple OSS Distributions mac_policy_list.chunks++;
670*c54f35caSApple OSS Distributions #else
671*c54f35caSApple OSS Distributions printf("out of space in mac_policy_list.\n");
672*c54f35caSApple OSS Distributions return ENOMEM;
673*c54f35caSApple OSS Distributions #endif /* XNU_TARGET_OS_OSX */
674*c54f35caSApple OSS Distributions }
675*c54f35caSApple OSS Distributions
676*c54f35caSApple OSS Distributions /* Check for policy with same name already loaded */
677*c54f35caSApple OSS Distributions for (i = 0; i <= mac_policy_list.maxindex; i++) {
678*c54f35caSApple OSS Distributions if (mac_policy_list.entries[i].mpc == NULL) {
679*c54f35caSApple OSS Distributions continue;
680*c54f35caSApple OSS Distributions }
681*c54f35caSApple OSS Distributions
682*c54f35caSApple OSS Distributions if (strcmp(mac_policy_list.entries[i].mpc->mpc_name,
683*c54f35caSApple OSS Distributions mpc->mpc_name) == 0) {
684*c54f35caSApple OSS Distributions error = EEXIST;
685*c54f35caSApple OSS Distributions goto out;
686*c54f35caSApple OSS Distributions }
687*c54f35caSApple OSS Distributions }
688*c54f35caSApple OSS Distributions
689*c54f35caSApple OSS Distributions if (mpc->mpc_field_off != NULL) {
690*c54f35caSApple OSS Distributions slot = ffs(mac_slot_offsets_free);
691*c54f35caSApple OSS Distributions if (slot == 0) {
692*c54f35caSApple OSS Distributions error = ENOMEM;
693*c54f35caSApple OSS Distributions goto out;
694*c54f35caSApple OSS Distributions }
695*c54f35caSApple OSS Distributions slot--;
696*c54f35caSApple OSS Distributions mac_slot_offsets_free &= ~(1 << slot);
697*c54f35caSApple OSS Distributions *mpc->mpc_field_off = slot;
698*c54f35caSApple OSS Distributions }
699*c54f35caSApple OSS Distributions mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
700*c54f35caSApple OSS Distributions
701*c54f35caSApple OSS Distributions if (xd) {
702*c54f35caSApple OSS Distributions struct mac_module_data *mmd = xd; /* module data from plist */
703*c54f35caSApple OSS Distributions
704*c54f35caSApple OSS Distributions /* Make a copy of the data. */
705*c54f35caSApple OSS Distributions mpc->mpc_data = (void *)kalloc_data(mmd->size, Z_WAITOK);
706*c54f35caSApple OSS Distributions if (mpc->mpc_data != NULL) {
707*c54f35caSApple OSS Distributions memcpy(mpc->mpc_data, mmd, mmd->size);
708*c54f35caSApple OSS Distributions
709*c54f35caSApple OSS Distributions /* Fix up pointers after copy. */
710*c54f35caSApple OSS Distributions mac_policy_fixup_mmd_list(mpc->mpc_data);
711*c54f35caSApple OSS Distributions }
712*c54f35caSApple OSS Distributions }
713*c54f35caSApple OSS Distributions
714*c54f35caSApple OSS Distributions /* Find the first free handle in the list (using our hint). */
715*c54f35caSApple OSS Distributions for (i = mac_policy_list.freehint; i < mac_policy_list.max; i++) {
716*c54f35caSApple OSS Distributions if (mac_policy_list.entries[i].mpc == NULL) {
717*c54f35caSApple OSS Distributions *handlep = i;
718*c54f35caSApple OSS Distributions mac_policy_list.freehint = ++i;
719*c54f35caSApple OSS Distributions break;
720*c54f35caSApple OSS Distributions }
721*c54f35caSApple OSS Distributions }
722*c54f35caSApple OSS Distributions
723*c54f35caSApple OSS Distributions /*
724*c54f35caSApple OSS Distributions * If we are loading a MAC module before the framework has
725*c54f35caSApple OSS Distributions * finished initializing or the module is not unloadable and
726*c54f35caSApple OSS Distributions * we can place its handle adjacent to the last static entry,
727*c54f35caSApple OSS Distributions * bump the static policy high water mark.
728*c54f35caSApple OSS Distributions * Static policies can get by with weaker locking requirements.
729*c54f35caSApple OSS Distributions */
730*c54f35caSApple OSS Distributions if (!mac_late ||
731*c54f35caSApple OSS Distributions ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0 &&
732*c54f35caSApple OSS Distributions *handlep == mac_policy_list.staticmax)) {
733*c54f35caSApple OSS Distributions static_entry = 1;
734*c54f35caSApple OSS Distributions mac_policy_list.staticmax++;
735*c54f35caSApple OSS Distributions }
736*c54f35caSApple OSS Distributions
737*c54f35caSApple OSS Distributions mac_policy_list.entries[*handlep].mpc = mpc;
738*c54f35caSApple OSS Distributions
739*c54f35caSApple OSS Distributions /* Update counters, etc */
740*c54f35caSApple OSS Distributions if (*handlep > mac_policy_list.maxindex) {
741*c54f35caSApple OSS Distributions mac_policy_list.maxindex = *handlep;
742*c54f35caSApple OSS Distributions }
743*c54f35caSApple OSS Distributions mac_policy_list.numloaded++;
744*c54f35caSApple OSS Distributions
745*c54f35caSApple OSS Distributions /* Per-policy initialization. */
746*c54f35caSApple OSS Distributions printf("calling mpo_policy_init for %s\n", mpc->mpc_name);
747*c54f35caSApple OSS Distributions if (mpc->mpc_ops->mpo_policy_init != NULL) {
748*c54f35caSApple OSS Distributions (*(mpc->mpc_ops->mpo_policy_init))(mpc);
749*c54f35caSApple OSS Distributions }
750*c54f35caSApple OSS Distributions
751*c54f35caSApple OSS Distributions if (mac_late && mpc->mpc_ops->mpo_policy_initbsd != NULL) {
752*c54f35caSApple OSS Distributions printf("calling mpo_policy_initbsd for %s\n", mpc->mpc_name);
753*c54f35caSApple OSS Distributions (*(mpc->mpc_ops->mpo_policy_initbsd))(mpc);
754*c54f35caSApple OSS Distributions }
755*c54f35caSApple OSS Distributions
756*c54f35caSApple OSS Distributions mac_policy_updateflags();
757*c54f35caSApple OSS Distributions
758*c54f35caSApple OSS Distributions if (mac_late) {
759*c54f35caSApple OSS Distributions mac_policy_release_exclusive();
760*c54f35caSApple OSS Distributions }
761*c54f35caSApple OSS Distributions
762*c54f35caSApple OSS Distributions mac_policy_addto_labellist(*handlep, static_entry);
763*c54f35caSApple OSS Distributions
764*c54f35caSApple OSS Distributions printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
765*c54f35caSApple OSS Distributions mpc->mpc_name);
766*c54f35caSApple OSS Distributions
767*c54f35caSApple OSS Distributions return 0;
768*c54f35caSApple OSS Distributions
769*c54f35caSApple OSS Distributions out:
770*c54f35caSApple OSS Distributions if (mac_late) {
771*c54f35caSApple OSS Distributions mac_policy_release_exclusive();
772*c54f35caSApple OSS Distributions }
773*c54f35caSApple OSS Distributions
774*c54f35caSApple OSS Distributions return error;
775*c54f35caSApple OSS Distributions }
776*c54f35caSApple OSS Distributions
777*c54f35caSApple OSS Distributions int
mac_policy_unregister(mac_policy_handle_t handle)778*c54f35caSApple OSS Distributions mac_policy_unregister(mac_policy_handle_t handle)
779*c54f35caSApple OSS Distributions {
780*c54f35caSApple OSS Distributions struct mac_policy_conf *mpc;
781*c54f35caSApple OSS Distributions
782*c54f35caSApple OSS Distributions /*
783*c54f35caSApple OSS Distributions * If we fail the load, we may get a request to unload. Check
784*c54f35caSApple OSS Distributions * to see if we did the run-time registration, and if not,
785*c54f35caSApple OSS Distributions * silently succeed.
786*c54f35caSApple OSS Distributions */
787*c54f35caSApple OSS Distributions mac_policy_grab_exclusive();
788*c54f35caSApple OSS Distributions mpc = mac_get_mpc(handle);
789*c54f35caSApple OSS Distributions if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
790*c54f35caSApple OSS Distributions mac_policy_release_exclusive();
791*c54f35caSApple OSS Distributions return 0;
792*c54f35caSApple OSS Distributions }
793*c54f35caSApple OSS Distributions
794*c54f35caSApple OSS Distributions #if 0
795*c54f35caSApple OSS Distributions /*
796*c54f35caSApple OSS Distributions * Don't allow unloading modules with private data.
797*c54f35caSApple OSS Distributions */
798*c54f35caSApple OSS Distributions if (mpc->mpc_field_off != NULL) {
799*c54f35caSApple OSS Distributions MAC_POLICY_LIST_UNLOCK();
800*c54f35caSApple OSS Distributions return EBUSY;
801*c54f35caSApple OSS Distributions }
802*c54f35caSApple OSS Distributions #endif
803*c54f35caSApple OSS Distributions /*
804*c54f35caSApple OSS Distributions * Only allow the unload to proceed if the module is unloadable
805*c54f35caSApple OSS Distributions * by its own definition.
806*c54f35caSApple OSS Distributions */
807*c54f35caSApple OSS Distributions if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
808*c54f35caSApple OSS Distributions mac_policy_release_exclusive();
809*c54f35caSApple OSS Distributions return EBUSY;
810*c54f35caSApple OSS Distributions }
811*c54f35caSApple OSS Distributions
812*c54f35caSApple OSS Distributions mac_policy_removefrom_labellist(handle);
813*c54f35caSApple OSS Distributions
814*c54f35caSApple OSS Distributions mac_get_mpc(handle) = NULL;
815*c54f35caSApple OSS Distributions if (handle < mac_policy_list.freehint &&
816*c54f35caSApple OSS Distributions handle >= mac_policy_list.staticmax) {
817*c54f35caSApple OSS Distributions mac_policy_list.freehint = handle;
818*c54f35caSApple OSS Distributions }
819*c54f35caSApple OSS Distributions
820*c54f35caSApple OSS Distributions if (handle == mac_policy_list.maxindex) {
821*c54f35caSApple OSS Distributions mac_policy_list.maxindex--;
822*c54f35caSApple OSS Distributions }
823*c54f35caSApple OSS Distributions
824*c54f35caSApple OSS Distributions mac_policy_list.numloaded--;
825*c54f35caSApple OSS Distributions if (mpc->mpc_field_off != NULL) {
826*c54f35caSApple OSS Distributions mac_slot_offsets_free |= (1 << *mpc->mpc_field_off);
827*c54f35caSApple OSS Distributions }
828*c54f35caSApple OSS Distributions
829*c54f35caSApple OSS Distributions if (mpc->mpc_ops->mpo_policy_destroy != NULL) {
830*c54f35caSApple OSS Distributions (*(mpc->mpc_ops->mpo_policy_destroy))(mpc);
831*c54f35caSApple OSS Distributions }
832*c54f35caSApple OSS Distributions
833*c54f35caSApple OSS Distributions mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
834*c54f35caSApple OSS Distributions mac_policy_updateflags();
835*c54f35caSApple OSS Distributions
836*c54f35caSApple OSS Distributions mac_policy_release_exclusive();
837*c54f35caSApple OSS Distributions
838*c54f35caSApple OSS Distributions if (mpc->mpc_data) {
839*c54f35caSApple OSS Distributions struct mac_module_data *mmd = mpc->mpc_data;
840*c54f35caSApple OSS Distributions __typed_allocators_ignore(kfree_data(mmd, mmd->size)); // rdar://87952845
841*c54f35caSApple OSS Distributions mpc->mpc_data = NULL;
842*c54f35caSApple OSS Distributions }
843*c54f35caSApple OSS Distributions
844*c54f35caSApple OSS Distributions printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
845*c54f35caSApple OSS Distributions mpc->mpc_name);
846*c54f35caSApple OSS Distributions
847*c54f35caSApple OSS Distributions return 0;
848*c54f35caSApple OSS Distributions }
849*c54f35caSApple OSS Distributions
850*c54f35caSApple OSS Distributions /*
851*c54f35caSApple OSS Distributions * Define an error value precedence, and given two arguments, selects the
852*c54f35caSApple OSS Distributions * value with the higher precedence.
853*c54f35caSApple OSS Distributions */
854*c54f35caSApple OSS Distributions int
mac_error_select(int error1,int error2)855*c54f35caSApple OSS Distributions mac_error_select(int error1, int error2)
856*c54f35caSApple OSS Distributions {
857*c54f35caSApple OSS Distributions /* Certain decision-making errors take top priority. */
858*c54f35caSApple OSS Distributions if (error1 == EDEADLK || error2 == EDEADLK) {
859*c54f35caSApple OSS Distributions return EDEADLK;
860*c54f35caSApple OSS Distributions }
861*c54f35caSApple OSS Distributions
862*c54f35caSApple OSS Distributions /* Invalid arguments should be reported where possible. */
863*c54f35caSApple OSS Distributions if (error1 == EINVAL || error2 == EINVAL) {
864*c54f35caSApple OSS Distributions return EINVAL;
865*c54f35caSApple OSS Distributions }
866*c54f35caSApple OSS Distributions
867*c54f35caSApple OSS Distributions /* Precedence goes to "visibility", with both process and file. */
868*c54f35caSApple OSS Distributions if (error1 == ESRCH || error2 == ESRCH) {
869*c54f35caSApple OSS Distributions return ESRCH;
870*c54f35caSApple OSS Distributions }
871*c54f35caSApple OSS Distributions
872*c54f35caSApple OSS Distributions if (error1 == ENOENT || error2 == ENOENT) {
873*c54f35caSApple OSS Distributions return ENOENT;
874*c54f35caSApple OSS Distributions }
875*c54f35caSApple OSS Distributions
876*c54f35caSApple OSS Distributions /* Precedence goes to DAC/MAC protections. */
877*c54f35caSApple OSS Distributions if (error1 == EACCES || error2 == EACCES) {
878*c54f35caSApple OSS Distributions return EACCES;
879*c54f35caSApple OSS Distributions }
880*c54f35caSApple OSS Distributions
881*c54f35caSApple OSS Distributions /* Precedence goes to privilege. */
882*c54f35caSApple OSS Distributions if (error1 == EPERM || error2 == EPERM) {
883*c54f35caSApple OSS Distributions return EPERM;
884*c54f35caSApple OSS Distributions }
885*c54f35caSApple OSS Distributions
886*c54f35caSApple OSS Distributions /* Precedence goes to error over success; otherwise, arbitrary. */
887*c54f35caSApple OSS Distributions if (error1 != 0) {
888*c54f35caSApple OSS Distributions return error1;
889*c54f35caSApple OSS Distributions }
890*c54f35caSApple OSS Distributions return error2;
891*c54f35caSApple OSS Distributions }
892*c54f35caSApple OSS Distributions
893*c54f35caSApple OSS Distributions int
mac_check_structmac_consistent(struct user_mac * mac)894*c54f35caSApple OSS Distributions mac_check_structmac_consistent(struct user_mac *mac)
895*c54f35caSApple OSS Distributions {
896*c54f35caSApple OSS Distributions if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN || mac->m_buflen == 0) {
897*c54f35caSApple OSS Distributions return EINVAL;
898*c54f35caSApple OSS Distributions }
899*c54f35caSApple OSS Distributions
900*c54f35caSApple OSS Distributions return 0;
901*c54f35caSApple OSS Distributions }
902*c54f35caSApple OSS Distributions
903*c54f35caSApple OSS Distributions /*
904*c54f35caSApple OSS Distributions * Get the external forms of labels from all policies, for a single
905*c54f35caSApple OSS Distributions * label namespace or "*" for all namespaces. Returns ENOENT if no policy
906*c54f35caSApple OSS Distributions * is registered for the namespace, unless the namespace begins with a '?'.
907*c54f35caSApple OSS Distributions */
908*c54f35caSApple OSS Distributions static int
mac_label_externalize(size_t mpo_externalize_off,struct label * label,const char * element,struct sbuf * sb)909*c54f35caSApple OSS Distributions mac_label_externalize(size_t mpo_externalize_off, struct label *label,
910*c54f35caSApple OSS Distributions const char *element, struct sbuf *sb)
911*c54f35caSApple OSS Distributions {
912*c54f35caSApple OSS Distributions struct mac_policy_conf *mpc;
913*c54f35caSApple OSS Distributions struct mac_label_listener *mll;
914*c54f35caSApple OSS Distributions struct mac_label_element *mle;
915*c54f35caSApple OSS Distributions struct mac_label_element_list_t *element_list;
916*c54f35caSApple OSS Distributions const char *name;
917*c54f35caSApple OSS Distributions int (*mpo_externalize)(struct label *, char *, struct sbuf *);
918*c54f35caSApple OSS Distributions int all_labels = 0, ignorenotfound = 0, error = 0, busy = FALSE;
919*c54f35caSApple OSS Distributions int sb_pos;
920*c54f35caSApple OSS Distributions unsigned int count = 0;
921*c54f35caSApple OSS Distributions
922*c54f35caSApple OSS Distributions if (element[0] == '?') {
923*c54f35caSApple OSS Distributions element++;
924*c54f35caSApple OSS Distributions ignorenotfound = 1;
925*c54f35caSApple OSS Distributions } else if (element[0] == '*' && element[1] == '\0') {
926*c54f35caSApple OSS Distributions all_labels = 1;
927*c54f35caSApple OSS Distributions }
928*c54f35caSApple OSS Distributions
929*c54f35caSApple OSS Distributions element_list = &mac_static_label_element_list;
930*c54f35caSApple OSS Distributions element_loop:
931*c54f35caSApple OSS Distributions SLIST_FOREACH(mle, element_list, mle_list) {
932*c54f35caSApple OSS Distributions name = mle->mle_name;
933*c54f35caSApple OSS Distributions if (all_labels) {
934*c54f35caSApple OSS Distributions if (*name == '?') {
935*c54f35caSApple OSS Distributions continue;
936*c54f35caSApple OSS Distributions }
937*c54f35caSApple OSS Distributions } else {
938*c54f35caSApple OSS Distributions if (*name == '?') {
939*c54f35caSApple OSS Distributions name++;
940*c54f35caSApple OSS Distributions }
941*c54f35caSApple OSS Distributions if (strcmp(name, element) != 0) {
942*c54f35caSApple OSS Distributions continue;
943*c54f35caSApple OSS Distributions }
944*c54f35caSApple OSS Distributions }
945*c54f35caSApple OSS Distributions SLIST_FOREACH(mll, &mle->mle_listeners, mll_list) {
946*c54f35caSApple OSS Distributions mpc = mac_policy_list.entries[mll->mll_handle].mpc;
947*c54f35caSApple OSS Distributions if (mpc == NULL) {
948*c54f35caSApple OSS Distributions continue;
949*c54f35caSApple OSS Distributions }
950*c54f35caSApple OSS Distributions mpo_externalize = *(const typeof(mpo_externalize) *)
951*c54f35caSApple OSS Distributions ((const char *)mpc->mpc_ops + mpo_externalize_off);
952*c54f35caSApple OSS Distributions if (mpo_externalize == NULL) {
953*c54f35caSApple OSS Distributions continue;
954*c54f35caSApple OSS Distributions }
955*c54f35caSApple OSS Distributions sb_pos = sbuf_len(sb);
956*c54f35caSApple OSS Distributions error = sbuf_printf(sb, "%s/", name);
957*c54f35caSApple OSS Distributions if (error) {
958*c54f35caSApple OSS Distributions goto done;
959*c54f35caSApple OSS Distributions }
960*c54f35caSApple OSS Distributions error = mpo_externalize(label, mle->mle_name, sb);
961*c54f35caSApple OSS Distributions if (error) {
962*c54f35caSApple OSS Distributions if (error != ENOENT) {
963*c54f35caSApple OSS Distributions goto done;
964*c54f35caSApple OSS Distributions }
965*c54f35caSApple OSS Distributions /*
966*c54f35caSApple OSS Distributions * If a policy doesn't have a label to
967*c54f35caSApple OSS Distributions * externalize it returns ENOENT. This
968*c54f35caSApple OSS Distributions * may occur for policies that support
969*c54f35caSApple OSS Distributions * multiple label elements for some
970*c54f35caSApple OSS Distributions * (but not all) object types.
971*c54f35caSApple OSS Distributions */
972*c54f35caSApple OSS Distributions sbuf_setpos(sb, sb_pos);
973*c54f35caSApple OSS Distributions error = 0;
974*c54f35caSApple OSS Distributions continue;
975*c54f35caSApple OSS Distributions }
976*c54f35caSApple OSS Distributions error = sbuf_putc(sb, ',');
977*c54f35caSApple OSS Distributions if (error) {
978*c54f35caSApple OSS Distributions goto done;
979*c54f35caSApple OSS Distributions }
980*c54f35caSApple OSS Distributions count++;
981*c54f35caSApple OSS Distributions }
982*c54f35caSApple OSS Distributions }
983*c54f35caSApple OSS Distributions /* If there are dynamic policies present, check their elements too. */
984*c54f35caSApple OSS Distributions if (!busy && mac_policy_list_conditional_busy() == 1) {
985*c54f35caSApple OSS Distributions element_list = &mac_label_element_list;
986*c54f35caSApple OSS Distributions busy = TRUE;
987*c54f35caSApple OSS Distributions goto element_loop;
988*c54f35caSApple OSS Distributions }
989*c54f35caSApple OSS Distributions done:
990*c54f35caSApple OSS Distributions if (busy) {
991*c54f35caSApple OSS Distributions mac_policy_list_unbusy();
992*c54f35caSApple OSS Distributions }
993*c54f35caSApple OSS Distributions if (!error && count == 0) {
994*c54f35caSApple OSS Distributions if (!all_labels && !ignorenotfound) {
995*c54f35caSApple OSS Distributions error = ENOENT; /* XXX: ENOLABEL? */
996*c54f35caSApple OSS Distributions }
997*c54f35caSApple OSS Distributions }
998*c54f35caSApple OSS Distributions return error;
999*c54f35caSApple OSS Distributions }
1000*c54f35caSApple OSS Distributions
1001*c54f35caSApple OSS Distributions /*
1002*c54f35caSApple OSS Distributions * Get the external forms of labels from all policies, for all label
1003*c54f35caSApple OSS Distributions * namespaces contained in a list.
1004*c54f35caSApple OSS Distributions *
1005*c54f35caSApple OSS Distributions * XXX This may be leaking an sbuf.
1006*c54f35caSApple OSS Distributions */
1007*c54f35caSApple OSS Distributions int
mac_externalize(size_t mpo_externalize_off,struct label * label,const char * elementlist,char * outbuf,size_t outbuflen)1008*c54f35caSApple OSS Distributions mac_externalize(size_t mpo_externalize_off, struct label *label,
1009*c54f35caSApple OSS Distributions const char *elementlist, char *outbuf, size_t outbuflen)
1010*c54f35caSApple OSS Distributions {
1011*c54f35caSApple OSS Distributions char *element;
1012*c54f35caSApple OSS Distributions char *scratch_base;
1013*c54f35caSApple OSS Distributions char *scratch;
1014*c54f35caSApple OSS Distributions struct sbuf sb;
1015*c54f35caSApple OSS Distributions int error = 0, len;
1016*c54f35caSApple OSS Distributions size_t buf_len = strlen(elementlist) + 1;
1017*c54f35caSApple OSS Distributions
1018*c54f35caSApple OSS Distributions /* allocate a scratch buffer the size of the string */
1019*c54f35caSApple OSS Distributions scratch_base = kalloc_data(buf_len, Z_WAITOK);
1020*c54f35caSApple OSS Distributions if (scratch_base == NULL) {
1021*c54f35caSApple OSS Distributions error = ENOMEM;
1022*c54f35caSApple OSS Distributions goto out;
1023*c54f35caSApple OSS Distributions }
1024*c54f35caSApple OSS Distributions
1025*c54f35caSApple OSS Distributions /* copy the elementlist to the scratch buffer */
1026*c54f35caSApple OSS Distributions strlcpy(scratch_base, elementlist, buf_len);
1027*c54f35caSApple OSS Distributions
1028*c54f35caSApple OSS Distributions /*
1029*c54f35caSApple OSS Distributions * set up a temporary pointer that can be used to iterate the
1030*c54f35caSApple OSS Distributions * scratch buffer without losing the allocation address
1031*c54f35caSApple OSS Distributions */
1032*c54f35caSApple OSS Distributions scratch = scratch_base;
1033*c54f35caSApple OSS Distributions
1034*c54f35caSApple OSS Distributions /*
1035*c54f35caSApple OSS Distributions * initialize an sbuf mapping over the output buffer (or newly-allocated internal buffer, if
1036*c54f35caSApple OSS Distributions * outbuf is NULL), up to sbuf's limit of INT_MAX.
1037*c54f35caSApple OSS Distributions */
1038*c54f35caSApple OSS Distributions if (outbuflen > INT_MAX) {
1039*c54f35caSApple OSS Distributions outbuflen = INT_MAX;
1040*c54f35caSApple OSS Distributions }
1041*c54f35caSApple OSS Distributions if (sbuf_new(&sb, outbuf, (int)outbuflen, SBUF_FIXEDLEN) == NULL) {
1042*c54f35caSApple OSS Distributions /* could not allocate interior buffer */
1043*c54f35caSApple OSS Distributions error = ENOMEM;
1044*c54f35caSApple OSS Distributions goto out;
1045*c54f35caSApple OSS Distributions }
1046*c54f35caSApple OSS Distributions /* iterate the scratch buffer; NOTE: buffer contents modified! */
1047*c54f35caSApple OSS Distributions while ((element = strsep(&scratch, ",")) != NULL) {
1048*c54f35caSApple OSS Distributions error = mac_label_externalize(mpo_externalize_off, label,
1049*c54f35caSApple OSS Distributions element, &sb);
1050*c54f35caSApple OSS Distributions if (error) {
1051*c54f35caSApple OSS Distributions break;
1052*c54f35caSApple OSS Distributions }
1053*c54f35caSApple OSS Distributions }
1054*c54f35caSApple OSS Distributions if ((len = sbuf_len(&sb)) > 0) {
1055*c54f35caSApple OSS Distributions sbuf_setpos(&sb, len - 1); /* trim trailing comma */
1056*c54f35caSApple OSS Distributions }
1057*c54f35caSApple OSS Distributions sbuf_finish(&sb);
1058*c54f35caSApple OSS Distributions
1059*c54f35caSApple OSS Distributions out:
1060*c54f35caSApple OSS Distributions if (scratch_base != NULL) {
1061*c54f35caSApple OSS Distributions kfree_data(scratch_base, buf_len);
1062*c54f35caSApple OSS Distributions }
1063*c54f35caSApple OSS Distributions
1064*c54f35caSApple OSS Distributions return error;
1065*c54f35caSApple OSS Distributions }
1066*c54f35caSApple OSS Distributions
1067*c54f35caSApple OSS Distributions /*
1068*c54f35caSApple OSS Distributions * Have all policies set the internal form of a label, for a single
1069*c54f35caSApple OSS Distributions * label namespace.
1070*c54f35caSApple OSS Distributions */
1071*c54f35caSApple OSS Distributions static int
mac_label_internalize(size_t mpo_internalize_off,struct label * label,char * element_name,char * element_data)1072*c54f35caSApple OSS Distributions mac_label_internalize(size_t mpo_internalize_off, struct label *label,
1073*c54f35caSApple OSS Distributions char *element_name, char *element_data)
1074*c54f35caSApple OSS Distributions {
1075*c54f35caSApple OSS Distributions struct mac_policy_conf *mpc;
1076*c54f35caSApple OSS Distributions struct mac_label_listener *mll;
1077*c54f35caSApple OSS Distributions struct mac_label_element *mle;
1078*c54f35caSApple OSS Distributions struct mac_label_element_list_t *element_list;
1079*c54f35caSApple OSS Distributions int (*mpo_internalize)(struct label *, char *, char *);
1080*c54f35caSApple OSS Distributions int error = 0, busy = FALSE;
1081*c54f35caSApple OSS Distributions unsigned int count = 0;
1082*c54f35caSApple OSS Distributions const char *name;
1083*c54f35caSApple OSS Distributions
1084*c54f35caSApple OSS Distributions element_list = &mac_static_label_element_list;
1085*c54f35caSApple OSS Distributions element_loop:
1086*c54f35caSApple OSS Distributions SLIST_FOREACH(mle, element_list, mle_list) {
1087*c54f35caSApple OSS Distributions if (*(name = mle->mle_name) == '?') {
1088*c54f35caSApple OSS Distributions name++;
1089*c54f35caSApple OSS Distributions }
1090*c54f35caSApple OSS Distributions if (strcmp(element_name, name) != 0) {
1091*c54f35caSApple OSS Distributions continue;
1092*c54f35caSApple OSS Distributions }
1093*c54f35caSApple OSS Distributions SLIST_FOREACH(mll, &mle->mle_listeners, mll_list) {
1094*c54f35caSApple OSS Distributions mpc = mac_policy_list.entries[mll->mll_handle].mpc;
1095*c54f35caSApple OSS Distributions if (mpc == NULL) {
1096*c54f35caSApple OSS Distributions continue;
1097*c54f35caSApple OSS Distributions }
1098*c54f35caSApple OSS Distributions mpo_internalize = *(const typeof(mpo_internalize) *)
1099*c54f35caSApple OSS Distributions ((const char *)mpc->mpc_ops + mpo_internalize_off);
1100*c54f35caSApple OSS Distributions if (mpo_internalize == NULL) {
1101*c54f35caSApple OSS Distributions continue;
1102*c54f35caSApple OSS Distributions }
1103*c54f35caSApple OSS Distributions error = mpo_internalize(label, element_name,
1104*c54f35caSApple OSS Distributions element_data);
1105*c54f35caSApple OSS Distributions if (error) {
1106*c54f35caSApple OSS Distributions goto done;
1107*c54f35caSApple OSS Distributions }
1108*c54f35caSApple OSS Distributions count++;
1109*c54f35caSApple OSS Distributions }
1110*c54f35caSApple OSS Distributions }
1111*c54f35caSApple OSS Distributions /* If there are dynamic policies present, check their elements too. */
1112*c54f35caSApple OSS Distributions if (!busy && mac_policy_list_conditional_busy() == 1) {
1113*c54f35caSApple OSS Distributions element_list = &mac_label_element_list;
1114*c54f35caSApple OSS Distributions busy = TRUE;
1115*c54f35caSApple OSS Distributions goto element_loop;
1116*c54f35caSApple OSS Distributions }
1117*c54f35caSApple OSS Distributions done:
1118*c54f35caSApple OSS Distributions if (busy) {
1119*c54f35caSApple OSS Distributions mac_policy_list_unbusy();
1120*c54f35caSApple OSS Distributions }
1121*c54f35caSApple OSS Distributions if (!error && count == 0) {
1122*c54f35caSApple OSS Distributions error = ENOPOLICY;
1123*c54f35caSApple OSS Distributions }
1124*c54f35caSApple OSS Distributions return error;
1125*c54f35caSApple OSS Distributions }
1126*c54f35caSApple OSS Distributions
1127*c54f35caSApple OSS Distributions int
mac_internalize(size_t mpo_internalize_off,struct label * label,char * textlabels)1128*c54f35caSApple OSS Distributions mac_internalize(size_t mpo_internalize_off, struct label *label,
1129*c54f35caSApple OSS Distributions char *textlabels)
1130*c54f35caSApple OSS Distributions {
1131*c54f35caSApple OSS Distributions char *element_name, *element_data;
1132*c54f35caSApple OSS Distributions int error = 0;
1133*c54f35caSApple OSS Distributions
1134*c54f35caSApple OSS Distributions while (!error && (element_name = strsep(&textlabels, ",")) != NULL) {
1135*c54f35caSApple OSS Distributions element_data = strchr(element_name, '/');
1136*c54f35caSApple OSS Distributions if (element_data == NULL) {
1137*c54f35caSApple OSS Distributions error = EINVAL;
1138*c54f35caSApple OSS Distributions break;
1139*c54f35caSApple OSS Distributions }
1140*c54f35caSApple OSS Distributions *element_data++ = '\0';
1141*c54f35caSApple OSS Distributions error = mac_label_internalize(mpo_internalize_off, label,
1142*c54f35caSApple OSS Distributions element_name, element_data);
1143*c54f35caSApple OSS Distributions }
1144*c54f35caSApple OSS Distributions return error;
1145*c54f35caSApple OSS Distributions }
1146*c54f35caSApple OSS Distributions
1147*c54f35caSApple OSS Distributions static int
user_mac_copyin(struct proc * p,user_addr_t mac_p,struct user_mac * mac)1148*c54f35caSApple OSS Distributions user_mac_copyin(struct proc *p, user_addr_t mac_p, struct user_mac *mac)
1149*c54f35caSApple OSS Distributions {
1150*c54f35caSApple OSS Distributions int error;
1151*c54f35caSApple OSS Distributions
1152*c54f35caSApple OSS Distributions if (IS_64BIT_PROCESS(p)) {
1153*c54f35caSApple OSS Distributions struct user64_mac mac64;
1154*c54f35caSApple OSS Distributions if ((error = copyin(mac_p, &mac64, sizeof(mac64)))) {
1155*c54f35caSApple OSS Distributions return error;
1156*c54f35caSApple OSS Distributions }
1157*c54f35caSApple OSS Distributions
1158*c54f35caSApple OSS Distributions mac->m_buflen = mac64.m_buflen;
1159*c54f35caSApple OSS Distributions mac->m_string = mac64.m_string;
1160*c54f35caSApple OSS Distributions } else {
1161*c54f35caSApple OSS Distributions struct user32_mac mac32;
1162*c54f35caSApple OSS Distributions if ((error = copyin(mac_p, &mac32, sizeof(mac32)))) {
1163*c54f35caSApple OSS Distributions return error;
1164*c54f35caSApple OSS Distributions }
1165*c54f35caSApple OSS Distributions
1166*c54f35caSApple OSS Distributions mac->m_buflen = mac32.m_buflen;
1167*c54f35caSApple OSS Distributions mac->m_string = mac32.m_string;
1168*c54f35caSApple OSS Distributions }
1169*c54f35caSApple OSS Distributions
1170*c54f35caSApple OSS Distributions return mac_check_structmac_consistent(mac);
1171*c54f35caSApple OSS Distributions }
1172*c54f35caSApple OSS Distributions
1173*c54f35caSApple OSS Distributions int
mac_do_get(struct proc * p,user_addr_t mac_p,mac_getter_t getter)1174*c54f35caSApple OSS Distributions mac_do_get(struct proc *p, user_addr_t mac_p, mac_getter_t getter)
1175*c54f35caSApple OSS Distributions {
1176*c54f35caSApple OSS Distributions struct user_mac mac;
1177*c54f35caSApple OSS Distributions char *input;
1178*c54f35caSApple OSS Distributions char *output;
1179*c54f35caSApple OSS Distributions size_t len;
1180*c54f35caSApple OSS Distributions size_t ulen;
1181*c54f35caSApple OSS Distributions int error;
1182*c54f35caSApple OSS Distributions
1183*c54f35caSApple OSS Distributions if ((error = user_mac_copyin(p, mac_p, &mac))) {
1184*c54f35caSApple OSS Distributions return error;
1185*c54f35caSApple OSS Distributions }
1186*c54f35caSApple OSS Distributions
1187*c54f35caSApple OSS Distributions len = mac.m_buflen;
1188*c54f35caSApple OSS Distributions input = kalloc_data(len, Z_WAITOK);
1189*c54f35caSApple OSS Distributions if ((error = copyinstr(mac.m_string, input, len, &ulen))) {
1190*c54f35caSApple OSS Distributions kfree_data(input, len);
1191*c54f35caSApple OSS Distributions return error;
1192*c54f35caSApple OSS Distributions }
1193*c54f35caSApple OSS Distributions
1194*c54f35caSApple OSS Distributions AUDIT_ARG(mac_string, input);
1195*c54f35caSApple OSS Distributions
1196*c54f35caSApple OSS Distributions output = kalloc_data(len, Z_WAITOK | Z_ZERO);
1197*c54f35caSApple OSS Distributions
1198*c54f35caSApple OSS Distributions error = getter(input, output, len);
1199*c54f35caSApple OSS Distributions if (error == 0) {
1200*c54f35caSApple OSS Distributions /* mac_check_structmac_consistent => len > 0 */
1201*c54f35caSApple OSS Distributions output[len - 1] = '\0';
1202*c54f35caSApple OSS Distributions error = copyout(output, mac.m_string, strlen(output) + 1);
1203*c54f35caSApple OSS Distributions }
1204*c54f35caSApple OSS Distributions
1205*c54f35caSApple OSS Distributions kfree_data(output, len);
1206*c54f35caSApple OSS Distributions kfree_data(input, len);
1207*c54f35caSApple OSS Distributions return error;
1208*c54f35caSApple OSS Distributions }
1209*c54f35caSApple OSS Distributions
1210*c54f35caSApple OSS Distributions int
mac_do_set(struct proc * p,user_addr_t mac_p,mac_setter_t setter)1211*c54f35caSApple OSS Distributions mac_do_set(struct proc *p, user_addr_t mac_p, mac_setter_t setter)
1212*c54f35caSApple OSS Distributions {
1213*c54f35caSApple OSS Distributions struct user_mac mac;
1214*c54f35caSApple OSS Distributions char *input;
1215*c54f35caSApple OSS Distributions size_t len;
1216*c54f35caSApple OSS Distributions size_t ulen;
1217*c54f35caSApple OSS Distributions int error;
1218*c54f35caSApple OSS Distributions
1219*c54f35caSApple OSS Distributions if ((error = user_mac_copyin(p, mac_p, &mac))) {
1220*c54f35caSApple OSS Distributions return error;
1221*c54f35caSApple OSS Distributions }
1222*c54f35caSApple OSS Distributions
1223*c54f35caSApple OSS Distributions len = mac.m_buflen;
1224*c54f35caSApple OSS Distributions input = kalloc_data(len, Z_WAITOK);
1225*c54f35caSApple OSS Distributions if ((error = copyinstr(mac.m_string, input, len, &ulen))) {
1226*c54f35caSApple OSS Distributions kfree_data(input, len);
1227*c54f35caSApple OSS Distributions return error;
1228*c54f35caSApple OSS Distributions }
1229*c54f35caSApple OSS Distributions
1230*c54f35caSApple OSS Distributions AUDIT_ARG(mac_string, input);
1231*c54f35caSApple OSS Distributions
1232*c54f35caSApple OSS Distributions error = setter(input, len);
1233*c54f35caSApple OSS Distributions
1234*c54f35caSApple OSS Distributions kfree_data(input, len);
1235*c54f35caSApple OSS Distributions return error;
1236*c54f35caSApple OSS Distributions }
1237*c54f35caSApple OSS Distributions
1238*c54f35caSApple OSS Distributions /* system calls */
1239*c54f35caSApple OSS Distributions
1240*c54f35caSApple OSS Distributions int
__mac_get_pid(struct proc * p,struct __mac_get_pid_args * uap,int * ret __unused)1241*c54f35caSApple OSS Distributions __mac_get_pid(struct proc *p, struct __mac_get_pid_args *uap, int *ret __unused)
1242*c54f35caSApple OSS Distributions {
1243*c54f35caSApple OSS Distributions return mac_do_get(p, uap->mac_p,
1244*c54f35caSApple OSS Distributions ^(char *input, char *output, size_t len) {
1245*c54f35caSApple OSS Distributions struct proc *tproc;
1246*c54f35caSApple OSS Distributions struct ucred *tcred;
1247*c54f35caSApple OSS Distributions int error;
1248*c54f35caSApple OSS Distributions
1249*c54f35caSApple OSS Distributions AUDIT_ARG(pid, uap->pid);
1250*c54f35caSApple OSS Distributions
1251*c54f35caSApple OSS Distributions tproc = proc_find(uap->pid);
1252*c54f35caSApple OSS Distributions if (tproc == NULL) {
1253*c54f35caSApple OSS Distributions return ESRCH;
1254*c54f35caSApple OSS Distributions }
1255*c54f35caSApple OSS Distributions
1256*c54f35caSApple OSS Distributions tcred = kauth_cred_proc_ref(tproc);
1257*c54f35caSApple OSS Distributions proc_rele(tproc);
1258*c54f35caSApple OSS Distributions
1259*c54f35caSApple OSS Distributions error = mac_cred_label_externalize(mac_cred_label(tcred),
1260*c54f35caSApple OSS Distributions input, output, len, M_WAITOK);
1261*c54f35caSApple OSS Distributions
1262*c54f35caSApple OSS Distributions kauth_cred_unref(&tcred);
1263*c54f35caSApple OSS Distributions return error;
1264*c54f35caSApple OSS Distributions });
1265*c54f35caSApple OSS Distributions }
1266*c54f35caSApple OSS Distributions
1267*c54f35caSApple OSS Distributions int
__mac_get_proc(proc_t p,struct __mac_get_proc_args * uap,int * ret __unused)1268*c54f35caSApple OSS Distributions __mac_get_proc(proc_t p, struct __mac_get_proc_args *uap, int *ret __unused)
1269*c54f35caSApple OSS Distributions {
1270*c54f35caSApple OSS Distributions return mac_do_get(p, uap->mac_p,
1271*c54f35caSApple OSS Distributions ^(char *input, char *output, size_t len) {
1272*c54f35caSApple OSS Distributions struct label *label;
1273*c54f35caSApple OSS Distributions
1274*c54f35caSApple OSS Distributions label = mac_cred_label(kauth_cred_get());
1275*c54f35caSApple OSS Distributions
1276*c54f35caSApple OSS Distributions return mac_cred_label_externalize(label, input, output, len, M_WAITOK);
1277*c54f35caSApple OSS Distributions });
1278*c54f35caSApple OSS Distributions }
1279*c54f35caSApple OSS Distributions
1280*c54f35caSApple OSS Distributions int
__mac_set_proc(proc_t p,struct __mac_set_proc_args * uap,int * ret __unused)1281*c54f35caSApple OSS Distributions __mac_set_proc(proc_t p, struct __mac_set_proc_args *uap, int *ret __unused)
1282*c54f35caSApple OSS Distributions {
1283*c54f35caSApple OSS Distributions return mac_do_set(p, uap->mac_p,
1284*c54f35caSApple OSS Distributions ^(char *input, __unused size_t len) {
1285*c54f35caSApple OSS Distributions struct label *intlabel;
1286*c54f35caSApple OSS Distributions int error;
1287*c54f35caSApple OSS Distributions
1288*c54f35caSApple OSS Distributions intlabel = mac_cred_label_alloc();
1289*c54f35caSApple OSS Distributions if ((error = mac_cred_label_internalize(intlabel, input))) {
1290*c54f35caSApple OSS Distributions goto out;
1291*c54f35caSApple OSS Distributions }
1292*c54f35caSApple OSS Distributions
1293*c54f35caSApple OSS Distributions if ((error = mac_cred_check_label_update(kauth_cred_get(), intlabel))) {
1294*c54f35caSApple OSS Distributions goto out;
1295*c54f35caSApple OSS Distributions }
1296*c54f35caSApple OSS Distributions
1297*c54f35caSApple OSS Distributions error = kauth_proc_label_update(p, intlabel);
1298*c54f35caSApple OSS Distributions
1299*c54f35caSApple OSS Distributions out:
1300*c54f35caSApple OSS Distributions mac_cred_label_free(intlabel);
1301*c54f35caSApple OSS Distributions return error;
1302*c54f35caSApple OSS Distributions });
1303*c54f35caSApple OSS Distributions }
1304*c54f35caSApple OSS Distributions
1305*c54f35caSApple OSS Distributions int
__mac_get_fd(proc_t p,struct __mac_get_fd_args * uap,int * ret __unused)1306*c54f35caSApple OSS Distributions __mac_get_fd(proc_t p, struct __mac_get_fd_args *uap, int *ret __unused)
1307*c54f35caSApple OSS Distributions {
1308*c54f35caSApple OSS Distributions return mac_do_get(p, uap->mac_p,
1309*c54f35caSApple OSS Distributions ^(char *input, char *output, size_t len) {
1310*c54f35caSApple OSS Distributions struct fileproc *fp;
1311*c54f35caSApple OSS Distributions struct vnode *vp;
1312*c54f35caSApple OSS Distributions int error;
1313*c54f35caSApple OSS Distributions struct label *intlabel;
1314*c54f35caSApple OSS Distributions
1315*c54f35caSApple OSS Distributions AUDIT_ARG(fd, uap->fd);
1316*c54f35caSApple OSS Distributions
1317*c54f35caSApple OSS Distributions if ((error = fp_lookup(p, uap->fd, &fp, 0))) {
1318*c54f35caSApple OSS Distributions return error;
1319*c54f35caSApple OSS Distributions }
1320*c54f35caSApple OSS Distributions
1321*c54f35caSApple OSS Distributions error = mac_file_check_get(kauth_cred_get(), fp->fp_glob, input, len);
1322*c54f35caSApple OSS Distributions if (error) {
1323*c54f35caSApple OSS Distributions fp_drop(p, uap->fd, fp, 0);
1324*c54f35caSApple OSS Distributions return error;
1325*c54f35caSApple OSS Distributions }
1326*c54f35caSApple OSS Distributions
1327*c54f35caSApple OSS Distributions switch (FILEGLOB_DTYPE(fp->fp_glob)) {
1328*c54f35caSApple OSS Distributions case DTYPE_VNODE:
1329*c54f35caSApple OSS Distributions intlabel = mac_vnode_label_alloc(NULL);
1330*c54f35caSApple OSS Distributions if (intlabel == NULL) {
1331*c54f35caSApple OSS Distributions error = ENOMEM;
1332*c54f35caSApple OSS Distributions break;
1333*c54f35caSApple OSS Distributions }
1334*c54f35caSApple OSS Distributions vp = (struct vnode *)fp_get_data(fp);
1335*c54f35caSApple OSS Distributions error = vnode_getwithref(vp);
1336*c54f35caSApple OSS Distributions if (error == 0) {
1337*c54f35caSApple OSS Distributions mac_vnode_label_copy(mac_vnode_label(vp), intlabel);
1338*c54f35caSApple OSS Distributions error = mac_vnode_label_externalize(intlabel,
1339*c54f35caSApple OSS Distributions input, output, len, M_WAITOK);
1340*c54f35caSApple OSS Distributions vnode_put(vp);
1341*c54f35caSApple OSS Distributions }
1342*c54f35caSApple OSS Distributions mac_vnode_label_free(intlabel);
1343*c54f35caSApple OSS Distributions break;
1344*c54f35caSApple OSS Distributions case DTYPE_SOCKET:
1345*c54f35caSApple OSS Distributions case DTYPE_PSXSHM:
1346*c54f35caSApple OSS Distributions case DTYPE_PSXSEM:
1347*c54f35caSApple OSS Distributions case DTYPE_PIPE:
1348*c54f35caSApple OSS Distributions case DTYPE_KQUEUE:
1349*c54f35caSApple OSS Distributions case DTYPE_FSEVENTS:
1350*c54f35caSApple OSS Distributions case DTYPE_ATALK:
1351*c54f35caSApple OSS Distributions case DTYPE_NETPOLICY:
1352*c54f35caSApple OSS Distributions case DTYPE_CHANNEL:
1353*c54f35caSApple OSS Distributions case DTYPE_NEXUS:
1354*c54f35caSApple OSS Distributions default:
1355*c54f35caSApple OSS Distributions error = ENOSYS; // only sockets/vnodes so far
1356*c54f35caSApple OSS Distributions break;
1357*c54f35caSApple OSS Distributions }
1358*c54f35caSApple OSS Distributions fp_drop(p, uap->fd, fp, 0);
1359*c54f35caSApple OSS Distributions return error;
1360*c54f35caSApple OSS Distributions });
1361*c54f35caSApple OSS Distributions }
1362*c54f35caSApple OSS Distributions
1363*c54f35caSApple OSS Distributions static int
mac_get_filelink(proc_t p,user_addr_t mac_p,user_addr_t path_p,int follow)1364*c54f35caSApple OSS Distributions mac_get_filelink(proc_t p, user_addr_t mac_p, user_addr_t path_p, int follow)
1365*c54f35caSApple OSS Distributions {
1366*c54f35caSApple OSS Distributions return mac_do_get(p, mac_p,
1367*c54f35caSApple OSS Distributions ^(char *input, char *output, size_t len) {
1368*c54f35caSApple OSS Distributions struct vnode *vp;
1369*c54f35caSApple OSS Distributions struct nameidata nd;
1370*c54f35caSApple OSS Distributions struct label *intlabel;
1371*c54f35caSApple OSS Distributions int error;
1372*c54f35caSApple OSS Distributions
1373*c54f35caSApple OSS Distributions NDINIT(&nd, LOOKUP, OP_LOOKUP,
1374*c54f35caSApple OSS Distributions LOCKLEAF | (follow ? FOLLOW : NOFOLLOW) | AUDITVNPATH1,
1375*c54f35caSApple OSS Distributions UIO_USERSPACE, path_p,
1376*c54f35caSApple OSS Distributions vfs_context_current());
1377*c54f35caSApple OSS Distributions if ((error = namei(&nd))) {
1378*c54f35caSApple OSS Distributions return error;
1379*c54f35caSApple OSS Distributions }
1380*c54f35caSApple OSS Distributions vp = nd.ni_vp;
1381*c54f35caSApple OSS Distributions
1382*c54f35caSApple OSS Distributions nameidone(&nd);
1383*c54f35caSApple OSS Distributions
1384*c54f35caSApple OSS Distributions intlabel = mac_vnode_label_alloc(NULL);
1385*c54f35caSApple OSS Distributions mac_vnode_label_copy(mac_vnode_label(vp), intlabel);
1386*c54f35caSApple OSS Distributions error = mac_vnode_label_externalize(intlabel, input, output,
1387*c54f35caSApple OSS Distributions len, M_WAITOK);
1388*c54f35caSApple OSS Distributions mac_vnode_label_free(intlabel);
1389*c54f35caSApple OSS Distributions
1390*c54f35caSApple OSS Distributions vnode_put(vp);
1391*c54f35caSApple OSS Distributions return error;
1392*c54f35caSApple OSS Distributions });
1393*c54f35caSApple OSS Distributions }
1394*c54f35caSApple OSS Distributions
1395*c54f35caSApple OSS Distributions int
__mac_get_file(proc_t p,struct __mac_get_file_args * uap,int * ret __unused)1396*c54f35caSApple OSS Distributions __mac_get_file(proc_t p, struct __mac_get_file_args *uap,
1397*c54f35caSApple OSS Distributions int *ret __unused)
1398*c54f35caSApple OSS Distributions {
1399*c54f35caSApple OSS Distributions return mac_get_filelink(p, uap->mac_p, uap->path_p, 1);
1400*c54f35caSApple OSS Distributions }
1401*c54f35caSApple OSS Distributions
1402*c54f35caSApple OSS Distributions int
__mac_get_link(proc_t p,struct __mac_get_link_args * uap,int * ret __unused)1403*c54f35caSApple OSS Distributions __mac_get_link(proc_t p, struct __mac_get_link_args *uap,
1404*c54f35caSApple OSS Distributions int *ret __unused)
1405*c54f35caSApple OSS Distributions {
1406*c54f35caSApple OSS Distributions return mac_get_filelink(p, uap->mac_p, uap->path_p, 0);
1407*c54f35caSApple OSS Distributions }
1408*c54f35caSApple OSS Distributions
1409*c54f35caSApple OSS Distributions int
__mac_set_fd(proc_t p,struct __mac_set_fd_args * uap,int * ret __unused)1410*c54f35caSApple OSS Distributions __mac_set_fd(proc_t p, struct __mac_set_fd_args *uap, int *ret __unused)
1411*c54f35caSApple OSS Distributions {
1412*c54f35caSApple OSS Distributions return mac_do_set(p, uap->mac_p,
1413*c54f35caSApple OSS Distributions ^(char *input, size_t len) {
1414*c54f35caSApple OSS Distributions struct fileproc *fp;
1415*c54f35caSApple OSS Distributions struct vfs_context *ctx = vfs_context_current();
1416*c54f35caSApple OSS Distributions int error;
1417*c54f35caSApple OSS Distributions struct label *intlabel;
1418*c54f35caSApple OSS Distributions struct vnode *vp;
1419*c54f35caSApple OSS Distributions
1420*c54f35caSApple OSS Distributions AUDIT_ARG(fd, uap->fd);
1421*c54f35caSApple OSS Distributions
1422*c54f35caSApple OSS Distributions if ((error = fp_lookup(p, uap->fd, &fp, 0))) {
1423*c54f35caSApple OSS Distributions return error;
1424*c54f35caSApple OSS Distributions }
1425*c54f35caSApple OSS Distributions
1426*c54f35caSApple OSS Distributions error = mac_file_check_set(vfs_context_ucred(ctx), fp->fp_glob, input, len);
1427*c54f35caSApple OSS Distributions if (error) {
1428*c54f35caSApple OSS Distributions fp_drop(p, uap->fd, fp, 0);
1429*c54f35caSApple OSS Distributions return error;
1430*c54f35caSApple OSS Distributions }
1431*c54f35caSApple OSS Distributions
1432*c54f35caSApple OSS Distributions switch (FILEGLOB_DTYPE(fp->fp_glob)) {
1433*c54f35caSApple OSS Distributions case DTYPE_VNODE:
1434*c54f35caSApple OSS Distributions if (mac_label_vnodes == 0) {
1435*c54f35caSApple OSS Distributions error = ENOSYS;
1436*c54f35caSApple OSS Distributions break;
1437*c54f35caSApple OSS Distributions }
1438*c54f35caSApple OSS Distributions
1439*c54f35caSApple OSS Distributions intlabel = mac_vnode_label_alloc(NULL);
1440*c54f35caSApple OSS Distributions
1441*c54f35caSApple OSS Distributions error = mac_vnode_label_internalize(intlabel, input);
1442*c54f35caSApple OSS Distributions if (error) {
1443*c54f35caSApple OSS Distributions mac_vnode_label_free(intlabel);
1444*c54f35caSApple OSS Distributions break;
1445*c54f35caSApple OSS Distributions }
1446*c54f35caSApple OSS Distributions
1447*c54f35caSApple OSS Distributions vp = (struct vnode *)fp_get_data(fp);
1448*c54f35caSApple OSS Distributions
1449*c54f35caSApple OSS Distributions error = vnode_getwithref(vp);
1450*c54f35caSApple OSS Distributions if (error == 0) {
1451*c54f35caSApple OSS Distributions error = vn_setlabel(vp, intlabel, ctx);
1452*c54f35caSApple OSS Distributions vnode_put(vp);
1453*c54f35caSApple OSS Distributions }
1454*c54f35caSApple OSS Distributions mac_vnode_label_free(intlabel);
1455*c54f35caSApple OSS Distributions break;
1456*c54f35caSApple OSS Distributions
1457*c54f35caSApple OSS Distributions case DTYPE_SOCKET:
1458*c54f35caSApple OSS Distributions case DTYPE_PSXSHM:
1459*c54f35caSApple OSS Distributions case DTYPE_PSXSEM:
1460*c54f35caSApple OSS Distributions case DTYPE_PIPE:
1461*c54f35caSApple OSS Distributions case DTYPE_KQUEUE:
1462*c54f35caSApple OSS Distributions case DTYPE_FSEVENTS:
1463*c54f35caSApple OSS Distributions case DTYPE_ATALK:
1464*c54f35caSApple OSS Distributions case DTYPE_NETPOLICY:
1465*c54f35caSApple OSS Distributions case DTYPE_CHANNEL:
1466*c54f35caSApple OSS Distributions case DTYPE_NEXUS:
1467*c54f35caSApple OSS Distributions default:
1468*c54f35caSApple OSS Distributions error = ENOSYS; // only sockets/vnodes so far
1469*c54f35caSApple OSS Distributions break;
1470*c54f35caSApple OSS Distributions }
1471*c54f35caSApple OSS Distributions
1472*c54f35caSApple OSS Distributions fp_drop(p, uap->fd, fp, 0);
1473*c54f35caSApple OSS Distributions return error;
1474*c54f35caSApple OSS Distributions });
1475*c54f35caSApple OSS Distributions }
1476*c54f35caSApple OSS Distributions
1477*c54f35caSApple OSS Distributions static int
mac_set_filelink(proc_t p,user_addr_t mac_p,user_addr_t path_p,int follow)1478*c54f35caSApple OSS Distributions mac_set_filelink(proc_t p, user_addr_t mac_p, user_addr_t path_p,
1479*c54f35caSApple OSS Distributions int follow)
1480*c54f35caSApple OSS Distributions {
1481*c54f35caSApple OSS Distributions return mac_do_set(p, mac_p,
1482*c54f35caSApple OSS Distributions ^(char *input, __unused size_t len) {
1483*c54f35caSApple OSS Distributions struct vnode *vp;
1484*c54f35caSApple OSS Distributions struct vfs_context *ctx = vfs_context_current();
1485*c54f35caSApple OSS Distributions struct label *intlabel;
1486*c54f35caSApple OSS Distributions struct nameidata nd;
1487*c54f35caSApple OSS Distributions int error;
1488*c54f35caSApple OSS Distributions
1489*c54f35caSApple OSS Distributions if (mac_label_vnodes == 0) {
1490*c54f35caSApple OSS Distributions return ENOSYS;
1491*c54f35caSApple OSS Distributions }
1492*c54f35caSApple OSS Distributions
1493*c54f35caSApple OSS Distributions intlabel = mac_vnode_label_alloc(NULL);
1494*c54f35caSApple OSS Distributions error = mac_vnode_label_internalize(intlabel, input);
1495*c54f35caSApple OSS Distributions if (error) {
1496*c54f35caSApple OSS Distributions mac_vnode_label_free(intlabel);
1497*c54f35caSApple OSS Distributions return error;
1498*c54f35caSApple OSS Distributions }
1499*c54f35caSApple OSS Distributions
1500*c54f35caSApple OSS Distributions NDINIT(&nd, LOOKUP, OP_LOOKUP,
1501*c54f35caSApple OSS Distributions LOCKLEAF | (follow ? FOLLOW : NOFOLLOW) | AUDITVNPATH1,
1502*c54f35caSApple OSS Distributions UIO_USERSPACE, path_p, ctx);
1503*c54f35caSApple OSS Distributions error = namei(&nd);
1504*c54f35caSApple OSS Distributions if (error) {
1505*c54f35caSApple OSS Distributions mac_vnode_label_free(intlabel);
1506*c54f35caSApple OSS Distributions return error;
1507*c54f35caSApple OSS Distributions }
1508*c54f35caSApple OSS Distributions vp = nd.ni_vp;
1509*c54f35caSApple OSS Distributions
1510*c54f35caSApple OSS Distributions nameidone(&nd);
1511*c54f35caSApple OSS Distributions
1512*c54f35caSApple OSS Distributions error = vn_setlabel(vp, intlabel, ctx);
1513*c54f35caSApple OSS Distributions vnode_put(vp);
1514*c54f35caSApple OSS Distributions mac_vnode_label_free(intlabel);
1515*c54f35caSApple OSS Distributions
1516*c54f35caSApple OSS Distributions return error;
1517*c54f35caSApple OSS Distributions });
1518*c54f35caSApple OSS Distributions }
1519*c54f35caSApple OSS Distributions
1520*c54f35caSApple OSS Distributions int
__mac_set_file(proc_t p,struct __mac_set_file_args * uap,int * ret __unused)1521*c54f35caSApple OSS Distributions __mac_set_file(proc_t p, struct __mac_set_file_args *uap,
1522*c54f35caSApple OSS Distributions int *ret __unused)
1523*c54f35caSApple OSS Distributions {
1524*c54f35caSApple OSS Distributions return mac_set_filelink(p, uap->mac_p, uap->path_p, 1);
1525*c54f35caSApple OSS Distributions }
1526*c54f35caSApple OSS Distributions
1527*c54f35caSApple OSS Distributions int
__mac_set_link(proc_t p,struct __mac_set_link_args * uap,int * ret __unused)1528*c54f35caSApple OSS Distributions __mac_set_link(proc_t p, struct __mac_set_link_args *uap,
1529*c54f35caSApple OSS Distributions int *ret __unused)
1530*c54f35caSApple OSS Distributions {
1531*c54f35caSApple OSS Distributions return mac_set_filelink(p, uap->mac_p, uap->path_p, 0);
1532*c54f35caSApple OSS Distributions }
1533*c54f35caSApple OSS Distributions
1534*c54f35caSApple OSS Distributions static int
mac_proc_check_mac_syscall(proc_t p,const char * target,int callnum)1535*c54f35caSApple OSS Distributions mac_proc_check_mac_syscall(proc_t p, const char *target, int callnum)
1536*c54f35caSApple OSS Distributions {
1537*c54f35caSApple OSS Distributions int error;
1538*c54f35caSApple OSS Distributions
1539*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1540*c54f35caSApple OSS Distributions /* 21167099 - only check if we allow write */
1541*c54f35caSApple OSS Distributions if (!mac_proc_enforce) {
1542*c54f35caSApple OSS Distributions return 0;
1543*c54f35caSApple OSS Distributions }
1544*c54f35caSApple OSS Distributions #endif
1545*c54f35caSApple OSS Distributions
1546*c54f35caSApple OSS Distributions MAC_CHECK(proc_check_syscall_mac, p, target, callnum);
1547*c54f35caSApple OSS Distributions
1548*c54f35caSApple OSS Distributions return error;
1549*c54f35caSApple OSS Distributions }
1550*c54f35caSApple OSS Distributions
1551*c54f35caSApple OSS Distributions /*
1552*c54f35caSApple OSS Distributions * __mac_syscall: Perform a MAC policy system call
1553*c54f35caSApple OSS Distributions *
1554*c54f35caSApple OSS Distributions * Parameters: p Process calling this routine
1555*c54f35caSApple OSS Distributions * uap User argument descriptor (see below)
1556*c54f35caSApple OSS Distributions * retv (Unused)
1557*c54f35caSApple OSS Distributions *
1558*c54f35caSApple OSS Distributions * Indirect: uap->policy Name of target MAC policy
1559*c54f35caSApple OSS Distributions * uap->call MAC policy-specific system call to perform
1560*c54f35caSApple OSS Distributions * uap->arg MAC policy-specific system call arguments
1561*c54f35caSApple OSS Distributions *
1562*c54f35caSApple OSS Distributions * Returns: 0 Success
1563*c54f35caSApple OSS Distributions * !0 Not success
1564*c54f35caSApple OSS Distributions *
1565*c54f35caSApple OSS Distributions */
1566*c54f35caSApple OSS Distributions int
__mac_syscall(proc_t p,struct __mac_syscall_args * uap,int * retv __unused)1567*c54f35caSApple OSS Distributions __mac_syscall(proc_t p, struct __mac_syscall_args *uap, int *retv __unused)
1568*c54f35caSApple OSS Distributions {
1569*c54f35caSApple OSS Distributions struct mac_policy_conf *mpc;
1570*c54f35caSApple OSS Distributions char target[MAC_MAX_POLICY_NAME];
1571*c54f35caSApple OSS Distributions int error;
1572*c54f35caSApple OSS Distributions u_int i;
1573*c54f35caSApple OSS Distributions size_t ulen;
1574*c54f35caSApple OSS Distributions
1575*c54f35caSApple OSS Distributions error = copyinstr(uap->policy, target, sizeof(target), &ulen);
1576*c54f35caSApple OSS Distributions if (error) {
1577*c54f35caSApple OSS Distributions return error;
1578*c54f35caSApple OSS Distributions }
1579*c54f35caSApple OSS Distributions AUDIT_ARG(value32, uap->call);
1580*c54f35caSApple OSS Distributions AUDIT_ARG(mac_string, target);
1581*c54f35caSApple OSS Distributions
1582*c54f35caSApple OSS Distributions error = mac_proc_check_mac_syscall(p, target, uap->call);
1583*c54f35caSApple OSS Distributions if (error) {
1584*c54f35caSApple OSS Distributions return error;
1585*c54f35caSApple OSS Distributions }
1586*c54f35caSApple OSS Distributions
1587*c54f35caSApple OSS Distributions error = ENOPOLICY;
1588*c54f35caSApple OSS Distributions
1589*c54f35caSApple OSS Distributions for (i = 0; i < mac_policy_list.staticmax; i++) {
1590*c54f35caSApple OSS Distributions mpc = mac_policy_list.entries[i].mpc;
1591*c54f35caSApple OSS Distributions if (mpc == NULL) {
1592*c54f35caSApple OSS Distributions continue;
1593*c54f35caSApple OSS Distributions }
1594*c54f35caSApple OSS Distributions
1595*c54f35caSApple OSS Distributions if (strcmp(mpc->mpc_name, target) == 0 &&
1596*c54f35caSApple OSS Distributions mpc->mpc_ops->mpo_policy_syscall != NULL) {
1597*c54f35caSApple OSS Distributions error = mpc->mpc_ops->mpo_policy_syscall(p,
1598*c54f35caSApple OSS Distributions uap->call, uap->arg);
1599*c54f35caSApple OSS Distributions goto done;
1600*c54f35caSApple OSS Distributions }
1601*c54f35caSApple OSS Distributions }
1602*c54f35caSApple OSS Distributions if (mac_policy_list_conditional_busy() != 0) {
1603*c54f35caSApple OSS Distributions for (; i <= mac_policy_list.maxindex; i++) {
1604*c54f35caSApple OSS Distributions mpc = mac_policy_list.entries[i].mpc;
1605*c54f35caSApple OSS Distributions if (mpc == NULL) {
1606*c54f35caSApple OSS Distributions continue;
1607*c54f35caSApple OSS Distributions }
1608*c54f35caSApple OSS Distributions
1609*c54f35caSApple OSS Distributions if (strcmp(mpc->mpc_name, target) == 0 &&
1610*c54f35caSApple OSS Distributions mpc->mpc_ops->mpo_policy_syscall != NULL) {
1611*c54f35caSApple OSS Distributions error = mpc->mpc_ops->mpo_policy_syscall(p,
1612*c54f35caSApple OSS Distributions uap->call, uap->arg);
1613*c54f35caSApple OSS Distributions break;
1614*c54f35caSApple OSS Distributions }
1615*c54f35caSApple OSS Distributions }
1616*c54f35caSApple OSS Distributions mac_policy_list_unbusy();
1617*c54f35caSApple OSS Distributions }
1618*c54f35caSApple OSS Distributions
1619*c54f35caSApple OSS Distributions done:
1620*c54f35caSApple OSS Distributions return error;
1621*c54f35caSApple OSS Distributions }
1622*c54f35caSApple OSS Distributions
1623*c54f35caSApple OSS Distributions int
mac_mount_label_get(struct mount * mp,user_addr_t mac_p)1624*c54f35caSApple OSS Distributions mac_mount_label_get(struct mount *mp, user_addr_t mac_p)
1625*c54f35caSApple OSS Distributions {
1626*c54f35caSApple OSS Distributions return mac_do_get(current_proc(), mac_p,
1627*c54f35caSApple OSS Distributions ^(char *input, char *output, size_t len) {
1628*c54f35caSApple OSS Distributions return mac_mount_label_externalize(mac_mount_label(mp), input,
1629*c54f35caSApple OSS Distributions output, len);
1630*c54f35caSApple OSS Distributions });
1631*c54f35caSApple OSS Distributions }
1632*c54f35caSApple OSS Distributions
1633*c54f35caSApple OSS Distributions /*
1634*c54f35caSApple OSS Distributions * __mac_get_mount: Get mount point label information for a given pathname
1635*c54f35caSApple OSS Distributions *
1636*c54f35caSApple OSS Distributions * Parameters: p (ignored)
1637*c54f35caSApple OSS Distributions * uap User argument descriptor (see below)
1638*c54f35caSApple OSS Distributions * ret (ignored)
1639*c54f35caSApple OSS Distributions *
1640*c54f35caSApple OSS Distributions * Indirect: uap->path Pathname
1641*c54f35caSApple OSS Distributions * uap->mac_p MAC info
1642*c54f35caSApple OSS Distributions *
1643*c54f35caSApple OSS Distributions * Returns: 0 Success
1644*c54f35caSApple OSS Distributions * !0 Not success
1645*c54f35caSApple OSS Distributions */
1646*c54f35caSApple OSS Distributions int
__mac_get_mount(proc_t p __unused,struct __mac_get_mount_args * uap,int * ret __unused)1647*c54f35caSApple OSS Distributions __mac_get_mount(proc_t p __unused, struct __mac_get_mount_args *uap,
1648*c54f35caSApple OSS Distributions int *ret __unused)
1649*c54f35caSApple OSS Distributions {
1650*c54f35caSApple OSS Distributions struct nameidata nd;
1651*c54f35caSApple OSS Distributions struct vfs_context *ctx = vfs_context_current();
1652*c54f35caSApple OSS Distributions struct mount *mp;
1653*c54f35caSApple OSS Distributions int error;
1654*c54f35caSApple OSS Distributions
1655*c54f35caSApple OSS Distributions NDINIT(&nd, LOOKUP, OP_LOOKUP, FOLLOW | AUDITVNPATH1,
1656*c54f35caSApple OSS Distributions UIO_USERSPACE, uap->path, ctx);
1657*c54f35caSApple OSS Distributions error = namei(&nd);
1658*c54f35caSApple OSS Distributions if (error) {
1659*c54f35caSApple OSS Distributions return error;
1660*c54f35caSApple OSS Distributions }
1661*c54f35caSApple OSS Distributions mp = nd.ni_vp->v_mount;
1662*c54f35caSApple OSS Distributions mount_ref(mp, 0);
1663*c54f35caSApple OSS Distributions vnode_put(nd.ni_vp);
1664*c54f35caSApple OSS Distributions nameidone(&nd);
1665*c54f35caSApple OSS Distributions
1666*c54f35caSApple OSS Distributions error = mac_mount_label_get(mp, uap->mac_p);
1667*c54f35caSApple OSS Distributions mount_drop(mp, 0);
1668*c54f35caSApple OSS Distributions return error;
1669*c54f35caSApple OSS Distributions }
1670*c54f35caSApple OSS Distributions
1671*c54f35caSApple OSS Distributions /*
1672*c54f35caSApple OSS Distributions * mac_schedule_userret()
1673*c54f35caSApple OSS Distributions *
1674*c54f35caSApple OSS Distributions * Schedule a callback to the mpo_thread_userret hook. The mpo_thread_userret
1675*c54f35caSApple OSS Distributions * hook is called just before the thread exit from the kernel in ast_taken().
1676*c54f35caSApple OSS Distributions *
1677*c54f35caSApple OSS Distributions * Returns: 0 Success
1678*c54f35caSApple OSS Distributions * !0 Not successful
1679*c54f35caSApple OSS Distributions */
1680*c54f35caSApple OSS Distributions int
mac_schedule_userret(void)1681*c54f35caSApple OSS Distributions mac_schedule_userret(void)
1682*c54f35caSApple OSS Distributions {
1683*c54f35caSApple OSS Distributions act_set_astmacf(current_thread());
1684*c54f35caSApple OSS Distributions return 0;
1685*c54f35caSApple OSS Distributions }
1686*c54f35caSApple OSS Distributions
1687*c54f35caSApple OSS Distributions /*
1688*c54f35caSApple OSS Distributions * mac_do_machexc()
1689*c54f35caSApple OSS Distributions *
1690*c54f35caSApple OSS Distributions * Do a Mach exception. This should only be done in the mpo_thread_userret
1691*c54f35caSApple OSS Distributions * callback.
1692*c54f35caSApple OSS Distributions *
1693*c54f35caSApple OSS Distributions * params: code exception code
1694*c54f35caSApple OSS Distributions * subcode exception subcode
1695*c54f35caSApple OSS Distributions * flags flags:
1696*c54f35caSApple OSS Distributions * MAC_DOEXCF_TRACED Only do exception if being
1697*c54f35caSApple OSS Distributions * ptrace()'ed.
1698*c54f35caSApple OSS Distributions *
1699*c54f35caSApple OSS Distributions *
1700*c54f35caSApple OSS Distributions * Returns: 0 Success
1701*c54f35caSApple OSS Distributions * !0 Not successful
1702*c54f35caSApple OSS Distributions */
1703*c54f35caSApple OSS Distributions int
mac_do_machexc(int64_t code,int64_t subcode,uint32_t flags)1704*c54f35caSApple OSS Distributions mac_do_machexc(int64_t code, int64_t subcode, uint32_t flags)
1705*c54f35caSApple OSS Distributions {
1706*c54f35caSApple OSS Distributions mach_exception_data_type_t codes[EXCEPTION_CODE_MAX];
1707*c54f35caSApple OSS Distributions proc_t p = current_proc();
1708*c54f35caSApple OSS Distributions
1709*c54f35caSApple OSS Distributions /* Only allow execption codes in MACF's reserved range. */
1710*c54f35caSApple OSS Distributions if ((code < EXC_MACF_MIN) || (code > EXC_MACF_MAX)) {
1711*c54f35caSApple OSS Distributions return 1;
1712*c54f35caSApple OSS Distributions }
1713*c54f35caSApple OSS Distributions
1714*c54f35caSApple OSS Distributions if (flags & MAC_DOEXCF_TRACED &&
1715*c54f35caSApple OSS Distributions !(p->p_lflag & P_LTRACED && (p->p_lflag & P_LPPWAIT) == 0)) {
1716*c54f35caSApple OSS Distributions return 0;
1717*c54f35caSApple OSS Distributions }
1718*c54f35caSApple OSS Distributions
1719*c54f35caSApple OSS Distributions
1720*c54f35caSApple OSS Distributions /* Send the Mach exception */
1721*c54f35caSApple OSS Distributions codes[0] = (mach_exception_data_type_t)code;
1722*c54f35caSApple OSS Distributions codes[1] = (mach_exception_data_type_t)subcode;
1723*c54f35caSApple OSS Distributions
1724*c54f35caSApple OSS Distributions return bsd_exception(EXC_SOFTWARE, codes, 2) != KERN_SUCCESS;
1725*c54f35caSApple OSS Distributions }
1726*c54f35caSApple OSS Distributions
1727*c54f35caSApple OSS Distributions #else /* MAC */
1728*c54f35caSApple OSS Distributions
1729*c54f35caSApple OSS Distributions void (*load_security_extensions_function)(void) = 0;
1730*c54f35caSApple OSS Distributions
1731*c54f35caSApple OSS Distributions struct sysctl_oid_list sysctl__security_mac_children;
1732*c54f35caSApple OSS Distributions
1733*c54f35caSApple OSS Distributions int
mac_policy_register(struct mac_policy_conf * mpc __unused,mac_policy_handle_t * handlep __unused,void * xd __unused)1734*c54f35caSApple OSS Distributions mac_policy_register(struct mac_policy_conf *mpc __unused,
1735*c54f35caSApple OSS Distributions mac_policy_handle_t *handlep __unused, void *xd __unused)
1736*c54f35caSApple OSS Distributions {
1737*c54f35caSApple OSS Distributions return 0;
1738*c54f35caSApple OSS Distributions }
1739*c54f35caSApple OSS Distributions
1740*c54f35caSApple OSS Distributions int
mac_policy_unregister(mac_policy_handle_t handle __unused)1741*c54f35caSApple OSS Distributions mac_policy_unregister(mac_policy_handle_t handle __unused)
1742*c54f35caSApple OSS Distributions {
1743*c54f35caSApple OSS Distributions return 0;
1744*c54f35caSApple OSS Distributions }
1745*c54f35caSApple OSS Distributions
1746*c54f35caSApple OSS Distributions int
mac_audit_text(char * text __unused,mac_policy_handle_t handle __unused)1747*c54f35caSApple OSS Distributions mac_audit_text(char *text __unused, mac_policy_handle_t handle __unused)
1748*c54f35caSApple OSS Distributions {
1749*c54f35caSApple OSS Distributions return 0;
1750*c54f35caSApple OSS Distributions }
1751*c54f35caSApple OSS Distributions
1752*c54f35caSApple OSS Distributions int
mac_vnop_setxattr(struct vnode * vp __unused,const char * name __unused,char * buf __unused,size_t len __unused)1753*c54f35caSApple OSS Distributions mac_vnop_setxattr(struct vnode *vp __unused, const char *name __unused, char *buf __unused, size_t len __unused)
1754*c54f35caSApple OSS Distributions {
1755*c54f35caSApple OSS Distributions return ENOENT;
1756*c54f35caSApple OSS Distributions }
1757*c54f35caSApple OSS Distributions
1758*c54f35caSApple OSS Distributions int
mac_vnop_getxattr(struct vnode * vp __unused,const char * name __unused,char * buf __unused,size_t len __unused,size_t * attrlen __unused)1759*c54f35caSApple OSS Distributions mac_vnop_getxattr(struct vnode *vp __unused, const char *name __unused,
1760*c54f35caSApple OSS Distributions char *buf __unused, size_t len __unused, size_t *attrlen __unused)
1761*c54f35caSApple OSS Distributions {
1762*c54f35caSApple OSS Distributions return ENOENT;
1763*c54f35caSApple OSS Distributions }
1764*c54f35caSApple OSS Distributions
1765*c54f35caSApple OSS Distributions int
mac_vnop_removexattr(struct vnode * vp __unused,const char * name __unused)1766*c54f35caSApple OSS Distributions mac_vnop_removexattr(struct vnode *vp __unused, const char *name __unused)
1767*c54f35caSApple OSS Distributions {
1768*c54f35caSApple OSS Distributions return ENOENT;
1769*c54f35caSApple OSS Distributions }
1770*c54f35caSApple OSS Distributions
1771*c54f35caSApple OSS Distributions int
mac_file_setxattr(struct fileglob * fg __unused,const char * name __unused,char * buf __unused,size_t len __unused)1772*c54f35caSApple OSS Distributions mac_file_setxattr(struct fileglob *fg __unused, const char *name __unused, char *buf __unused, size_t len __unused)
1773*c54f35caSApple OSS Distributions {
1774*c54f35caSApple OSS Distributions return ENOENT;
1775*c54f35caSApple OSS Distributions }
1776*c54f35caSApple OSS Distributions
1777*c54f35caSApple OSS Distributions int
mac_file_getxattr(struct fileglob * fg __unused,const char * name __unused,char * buf __unused,size_t len __unused,size_t * attrlen __unused)1778*c54f35caSApple OSS Distributions mac_file_getxattr(struct fileglob *fg __unused, const char *name __unused,
1779*c54f35caSApple OSS Distributions char *buf __unused, size_t len __unused, size_t *attrlen __unused)
1780*c54f35caSApple OSS Distributions {
1781*c54f35caSApple OSS Distributions return ENOENT;
1782*c54f35caSApple OSS Distributions }
1783*c54f35caSApple OSS Distributions
1784*c54f35caSApple OSS Distributions int
mac_file_removexattr(struct fileglob * fg __unused,const char * name __unused)1785*c54f35caSApple OSS Distributions mac_file_removexattr(struct fileglob *fg __unused, const char *name __unused)
1786*c54f35caSApple OSS Distributions {
1787*c54f35caSApple OSS Distributions return ENOENT;
1788*c54f35caSApple OSS Distributions }
1789*c54f35caSApple OSS Distributions
1790*c54f35caSApple OSS Distributions intptr_t
mac_label_get(struct label * l __unused,int slot __unused)1791*c54f35caSApple OSS Distributions mac_label_get(struct label *l __unused, int slot __unused)
1792*c54f35caSApple OSS Distributions {
1793*c54f35caSApple OSS Distributions return 0;
1794*c54f35caSApple OSS Distributions }
1795*c54f35caSApple OSS Distributions
1796*c54f35caSApple OSS Distributions void
mac_label_set(struct label * l __unused,int slot __unused,intptr_t v __unused)1797*c54f35caSApple OSS Distributions mac_label_set(struct label *l __unused, int slot __unused, intptr_t v __unused)
1798*c54f35caSApple OSS Distributions {
1799*c54f35caSApple OSS Distributions return;
1800*c54f35caSApple OSS Distributions }
1801*c54f35caSApple OSS Distributions
1802*c54f35caSApple OSS Distributions int mac_iokit_check_hid_control(kauth_cred_t cred __unused);
1803*c54f35caSApple OSS Distributions int
mac_iokit_check_hid_control(kauth_cred_t cred __unused)1804*c54f35caSApple OSS Distributions mac_iokit_check_hid_control(kauth_cred_t cred __unused)
1805*c54f35caSApple OSS Distributions {
1806*c54f35caSApple OSS Distributions return 0;
1807*c54f35caSApple OSS Distributions }
1808*c54f35caSApple OSS Distributions
1809*c54f35caSApple OSS Distributions int mac_vnode_check_open(vfs_context_t ctx, struct vnode *vp, int acc_mode);
1810*c54f35caSApple OSS Distributions int
mac_vnode_check_open(vfs_context_t ctx __unused,struct vnode * vp __unused,int acc_mode __unused)1811*c54f35caSApple OSS Distributions mac_vnode_check_open(vfs_context_t ctx __unused, struct vnode *vp __unused, int acc_mode __unused)
1812*c54f35caSApple OSS Distributions {
1813*c54f35caSApple OSS Distributions return 0;
1814*c54f35caSApple OSS Distributions }
1815*c54f35caSApple OSS Distributions
1816*c54f35caSApple OSS Distributions int mac_mount_check_snapshot_mount(vfs_context_t ctx, struct vnode *rvp, struct vnode *vp, struct componentname *cnp,
1817*c54f35caSApple OSS Distributions const char *name, const char *vfc_name);
1818*c54f35caSApple OSS Distributions int
mac_mount_check_snapshot_mount(vfs_context_t ctx __unused,struct vnode * rvp __unused,struct vnode * vp __unused,struct componentname * cnp __unused,const char * name __unused,const char * vfc_name __unused)1819*c54f35caSApple OSS Distributions mac_mount_check_snapshot_mount(vfs_context_t ctx __unused, struct vnode *rvp __unused, struct vnode *vp __unused,
1820*c54f35caSApple OSS Distributions struct componentname *cnp __unused, const char *name __unused, const char *vfc_name __unused)
1821*c54f35caSApple OSS Distributions {
1822*c54f35caSApple OSS Distributions return 0;
1823*c54f35caSApple OSS Distributions }
1824*c54f35caSApple OSS Distributions
1825*c54f35caSApple OSS Distributions int mac_vnode_check_trigger_resolve(vfs_context_t ctx __unused, struct vnode *dvp __unused, struct componentname *cnp __unused);
1826*c54f35caSApple OSS Distributions int
mac_vnode_check_trigger_resolve(vfs_context_t ctx __unused,struct vnode * dvp __unused,struct componentname * cnp __unused)1827*c54f35caSApple OSS Distributions mac_vnode_check_trigger_resolve(vfs_context_t ctx __unused, struct vnode *dvp __unused, struct componentname *cnp __unused)
1828*c54f35caSApple OSS Distributions {
1829*c54f35caSApple OSS Distributions return 0;
1830*c54f35caSApple OSS Distributions }
1831*c54f35caSApple OSS Distributions
1832*c54f35caSApple OSS Distributions #endif /* !MAC */
1833