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