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