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